You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

949 lines
30 KiB

  1. # Copyright 2019 Coop IT Easy SCRL fs
  2. # Houssine Bakkali <houssine@coopiteasy.be>
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. from datetime import datetime
  5. # pylint: disable=missing-manifest-dependency
  6. from addons.base_iban.models.res_partner_bank import validate_iban
  7. from odoo import _, api, fields, models
  8. from odoo.exceptions import UserError, ValidationError
  9. # This structure is only used in easy_my_coop_webstite's controller
  10. _REQUIRED = [
  11. "email",
  12. "firstname",
  13. "lastname",
  14. "birthdate",
  15. "street_name",
  16. "house_number",
  17. "share_product_id",
  18. "ordered_parts",
  19. "zip_code",
  20. "city",
  21. "iban",
  22. "gender",
  23. ]
  24. @api.model
  25. def _lang_get(self):
  26. languages = self.env["res.lang"].search([])
  27. return [(language.code, language.name) for language in languages]
  28. # todo move to subscription_request.py
  29. class SubscriptionRequest(models.Model):
  30. _name = "subscription.request"
  31. _description = "Subscription Request"
  32. # This function is only used in easy_my_coop_webstite's controller
  33. def get_required_field(self):
  34. required_fields = _REQUIRED
  35. company = self.env["res.company"]._company_default_get()
  36. if company.data_policy_approval_required:
  37. required_fields.append("data_policy_approved")
  38. if company.internal_rules_approval_required:
  39. required_fields.append("internal_rules_approved")
  40. if company.financial_risk_approval_required:
  41. required_fields.append("financial_risk_approved")
  42. return required_fields
  43. def get_mail_template_notif(self, is_company=False):
  44. if is_company:
  45. mail_template = "easy_my_coop.email_template_confirmation_company"
  46. else:
  47. mail_template = "easy_my_coop.email_template_confirmation"
  48. return self.env.ref(mail_template, False)
  49. def is_member(self, vals, cooperator):
  50. if cooperator.member:
  51. vals["type"] = "increase"
  52. vals["already_cooperator"] = True
  53. return vals
  54. @api.model
  55. def create(self, vals):
  56. partner_obj = self.env["res.partner"]
  57. if not vals.get("partner_id"):
  58. cooperator = False
  59. if vals.get("email"):
  60. cooperator = partner_obj.get_cooperator_from_email(
  61. vals.get("email")
  62. )
  63. if cooperator:
  64. # TODO remove the following line once it has
  65. # been found a way to avoid double encoding
  66. cooperator = cooperator[0]
  67. vals["type"] = "subscription"
  68. vals = self.is_member(vals, cooperator)
  69. vals["partner_id"] = cooperator.id
  70. else:
  71. cooperator_id = vals.get("partner_id")
  72. cooperator = partner_obj.browse(cooperator_id)
  73. vals = self.is_member(vals, cooperator)
  74. if not cooperator.cooperator:
  75. cooperator.write({"cooperator": True})
  76. subscr_request = super(SubscriptionRequest, self).create(vals)
  77. mail_template_notif = subscr_request.get_mail_template_notif(False)
  78. mail_template_notif.send_mail(subscr_request.id)
  79. return subscr_request
  80. @api.model
  81. def create_comp_sub_req(self, vals):
  82. vals["name"] = vals["company_name"]
  83. if not vals.get("partner_id"):
  84. cooperator = self.env["res.partner"].get_cooperator_from_crn(
  85. vals.get("company_register_number")
  86. )
  87. if cooperator:
  88. vals["partner_id"] = cooperator.id
  89. vals["type"] = "increase"
  90. vals["already_cooperator"] = True
  91. subscr_request = super(SubscriptionRequest, self).create(vals)
  92. confirmation_mail_template = subscr_request.get_mail_template_notif(
  93. True
  94. )
  95. confirmation_mail_template.send_mail(subscr_request.id)
  96. return subscr_request
  97. def check_empty_string(self, value):
  98. if value is None or value is False or value == "":
  99. return False
  100. return True
  101. def check_iban(self, iban):
  102. try:
  103. if iban:
  104. validate_iban(iban)
  105. return True
  106. else:
  107. return False
  108. except ValidationError:
  109. return False
  110. @api.multi
  111. @api.depends("iban", "skip_control_ng")
  112. def _compute_validated_lines(self):
  113. for sub_request in self:
  114. validated = sub_request.skip_control_ng or self.check_iban(
  115. sub_request.iban
  116. )
  117. sub_request.validated = validated
  118. @api.multi
  119. @api.depends(
  120. "share_product_id", "share_product_id.list_price", "ordered_parts"
  121. )
  122. def _compute_subscription_amount(self):
  123. for sub_request in self:
  124. sub_request.subscription_amount = (
  125. sub_request.share_product_id.list_price
  126. * sub_request.ordered_parts
  127. )
  128. already_cooperator = fields.Boolean(
  129. string="I'm already cooperator",
  130. readonly=True,
  131. states={"draft": [("readonly", False)]},
  132. )
  133. name = fields.Char(
  134. string="Name",
  135. required=True,
  136. readonly=True,
  137. states={"draft": [("readonly", False)]},
  138. )
  139. firstname = fields.Char(
  140. string="Firstname",
  141. readonly=True,
  142. states={"draft": [("readonly", False)]},
  143. )
  144. lastname = fields.Char(
  145. string="Lastname",
  146. readonly=True,
  147. states={"draft": [("readonly", False)]},
  148. )
  149. birthdate = fields.Date(
  150. string="Birthdate",
  151. readonly=True,
  152. states={"draft": [("readonly", False)]},
  153. )
  154. gender = fields.Selection(
  155. [("male", _("Male")), ("female", _("Female")), ("other", _("Other"))],
  156. string="Gender",
  157. readonly=True,
  158. states={"draft": [("readonly", False)]},
  159. )
  160. type = fields.Selection(
  161. [
  162. ("new", "New Cooperator"),
  163. ("subscription", "Subscription"),
  164. ("increase", "Increase number of share"),
  165. ],
  166. string="Type",
  167. default="new",
  168. readonly=True,
  169. states={"draft": [("readonly", False)]},
  170. )
  171. state = fields.Selection(
  172. [
  173. ("draft", "Draft"),
  174. ("block", "Blocked"),
  175. ("done", "Done"),
  176. ("waiting", "Waiting"),
  177. ("transfer", "Transfer"),
  178. ("cancelled", "Cancelled"),
  179. ("paid", "paid"),
  180. ],
  181. string="State",
  182. required=True,
  183. default="draft",
  184. )
  185. email = fields.Char(
  186. string="Email",
  187. required=True,
  188. readonly=True,
  189. states={"draft": [("readonly", False)]},
  190. )
  191. iban = fields.Char(
  192. string="Account Number",
  193. readonly=True,
  194. states={"draft": [("readonly", False)]},
  195. )
  196. partner_id = fields.Many2one(
  197. "res.partner",
  198. string="Cooperator",
  199. readonly=True,
  200. states={"draft": [("readonly", False)]},
  201. )
  202. share_product_id = fields.Many2one(
  203. "product.product",
  204. string="Share type",
  205. domain=[("is_share", "=", True)],
  206. required=True,
  207. readonly=True,
  208. states={"draft": [("readonly", False)]},
  209. )
  210. share_short_name = fields.Char(
  211. related="share_product_id.short_name",
  212. string="Share type name",
  213. readonly=True,
  214. states={"draft": [("readonly", False)]},
  215. )
  216. share_unit_price = fields.Float(
  217. related="share_product_id.list_price",
  218. string="Share price",
  219. readonly=True,
  220. states={"draft": [("readonly", False)]},
  221. )
  222. subscription_amount = fields.Monetary(
  223. compute="_compute_subscription_amount",
  224. string="Subscription amount",
  225. currency_field="company_currency_id",
  226. readonly=True,
  227. states={"draft": [("readonly", False)]},
  228. )
  229. ordered_parts = fields.Integer(
  230. string="Number of Share",
  231. required=True,
  232. readonly=True,
  233. default=1,
  234. states={"draft": [("readonly", False)]},
  235. )
  236. address = fields.Char(
  237. string="Address",
  238. required=True,
  239. readonly=True,
  240. states={"draft": [("readonly", False)]},
  241. )
  242. city = fields.Char(
  243. string="City",
  244. required=True,
  245. readonly=True,
  246. states={"draft": [("readonly", False)]},
  247. )
  248. zip_code = fields.Char(
  249. string="Zip Code",
  250. required=True,
  251. readonly=True,
  252. states={"draft": [("readonly", False)]},
  253. )
  254. country_id = fields.Many2one(
  255. "res.country",
  256. string="Country",
  257. ondelete="restrict",
  258. required=True,
  259. readonly=True,
  260. states={"draft": [("readonly", False)]},
  261. )
  262. phone = fields.Char(
  263. string="Phone", readonly=True, states={"draft": [("readonly", False)]}
  264. )
  265. user_id = fields.Many2one("res.users", string="Responsible", readonly=True)
  266. # todo rename to valid_subscription_request
  267. validated = fields.Boolean(
  268. compute="_compute_validated_lines",
  269. string="Valid Subscription request?",
  270. readonly=True,
  271. )
  272. skip_control_ng = fields.Boolean(
  273. string="Skip control",
  274. help="if this field is checked then no"
  275. " control will be done on the national"
  276. " register number and on the iban bank"
  277. " account. To be done in case of the id"
  278. " card is from abroad or in case of"
  279. " a passport",
  280. )
  281. lang = fields.Selection(
  282. _lang_get,
  283. string="Language",
  284. required=True,
  285. readonly=True,
  286. states={"draft": [("readonly", False)]},
  287. default=lambda self: self.env["res.company"]
  288. ._company_default_get()
  289. .default_lang_id.code,
  290. )
  291. date = fields.Date(
  292. string="Subscription date request",
  293. required=True,
  294. readonly=True,
  295. states={"draft": [("readonly", False)]},
  296. default=lambda self: datetime.strftime(datetime.now(), "%Y-%m-%d"),
  297. )
  298. company_id = fields.Many2one(
  299. "res.company",
  300. string="Company",
  301. required=True,
  302. change_default=True,
  303. readonly=True,
  304. default=lambda self: self.env["res.company"]._company_default_get(),
  305. )
  306. company_currency_id = fields.Many2one(
  307. "res.currency",
  308. related="company_id.currency_id",
  309. string="Company Currency",
  310. readonly=True,
  311. )
  312. is_company = fields.Boolean(
  313. string="Is a company",
  314. readonly=True,
  315. states={"draft": [("readonly", False)]},
  316. )
  317. is_operation = fields.Boolean(
  318. string="Is an operation",
  319. readonly=True,
  320. states={"draft": [("readonly", False)]},
  321. )
  322. company_name = fields.Char(
  323. string="Company name",
  324. readonly=True,
  325. states={"draft": [("readonly", False)]},
  326. )
  327. company_email = fields.Char(
  328. string="Company email",
  329. readonly=True,
  330. states={"draft": [("readonly", False)]},
  331. )
  332. company_register_number = fields.Char(
  333. string="Company register number",
  334. readonly=True,
  335. states={"draft": [("readonly", False)]},
  336. )
  337. company_type = fields.Selection(
  338. [("", "")],
  339. string="Company type",
  340. readonly=True,
  341. states={"draft": [("readonly", False)]},
  342. )
  343. same_address = fields.Boolean(
  344. string="Same address",
  345. readonly=True,
  346. states={"draft": [("readonly", False)]},
  347. )
  348. activities_address = fields.Char(
  349. string="Activities address",
  350. readonly=True,
  351. states={"draft": [("readonly", False)]},
  352. )
  353. activities_city = fields.Char(
  354. string="Activities city",
  355. readonly=True,
  356. states={"draft": [("readonly", False)]},
  357. )
  358. activities_zip_code = fields.Char(
  359. string="Activities zip Code",
  360. readonly=True,
  361. states={"draft": [("readonly", False)]},
  362. )
  363. activities_country_id = fields.Many2one(
  364. "res.country",
  365. string="Activities country",
  366. ondelete="restrict",
  367. readonly=True,
  368. states={"draft": [("readonly", False)]},
  369. )
  370. contact_person_function = fields.Char(
  371. string="Function",
  372. readonly=True,
  373. states={"draft": [("readonly", False)]},
  374. )
  375. operation_request_id = fields.Many2one(
  376. "operation.request",
  377. string="Operation Request",
  378. readonly=True,
  379. states={"draft": [("readonly", False)]},
  380. )
  381. capital_release_request = fields.One2many(
  382. "account.invoice",
  383. "subscription_request",
  384. string="Capital release request",
  385. readonly=True,
  386. states={"draft": [("readonly", False)]},
  387. )
  388. capital_release_request_date = fields.Date(
  389. string="Force the capital " "release request date",
  390. help="Keep empty to use the " "current date",
  391. copy=False,
  392. readonly=True,
  393. states={"draft": [("readonly", False)]},
  394. )
  395. source = fields.Selection(
  396. [
  397. ("website", "Website"),
  398. ("crm", "CRM"),
  399. ("manual", "Manual"),
  400. ("operation", "Operation"),
  401. ],
  402. string="Source",
  403. default="website",
  404. readonly=True,
  405. states={"draft": [("readonly", False)]},
  406. )
  407. data_policy_approved = fields.Boolean(
  408. string="Data Policy Approved", default=False
  409. )
  410. internal_rules_approved = fields.Boolean(
  411. string="Approved Internal Rules", default=False
  412. )
  413. financial_risk_approved = fields.Boolean(
  414. string="Financial Risk Approved", default=False
  415. )
  416. _order = "id desc"
  417. def get_person_info(self, partner):
  418. self.firstname = partner.firstname
  419. self.name = partner.name
  420. self.lastname = partner.lastname
  421. self.email = partner.email
  422. self.birthdate = partner.birthdate_date
  423. self.gender = partner.gender
  424. self.address = partner.street
  425. self.city = partner.city
  426. self.zip_code = partner.zip
  427. self.country_id = partner.country_id
  428. self.phone = partner.phone
  429. self.lang = partner.lang
  430. @api.onchange("partner_id")
  431. def onchange_partner(self):
  432. partner = self.partner_id
  433. if partner:
  434. self.is_company = partner.is_company
  435. self.already_cooperator = partner.member
  436. if partner.bank_ids:
  437. self.iban = partner.bank_ids[0].acc_number
  438. if partner.member:
  439. self.type = "increase"
  440. if partner.is_company:
  441. self.company_name = partner.name
  442. self.company_email = partner.email
  443. self.company_register_number = partner.company_register_number
  444. representative = partner.get_representative()
  445. self.get_person_info(representative)
  446. self.contact_person_function = representative.function
  447. else:
  448. self.get_person_info(partner)
  449. # declare this function in order to be overriden
  450. def get_eater_vals(self, partner, share_product_id): # noqa
  451. return {}
  452. def _prepare_invoice_line(self, product, partner, qty):
  453. self.ensure_one()
  454. account = (
  455. product.property_account_income_id
  456. or product.categ_id.property_account_income_categ_id
  457. )
  458. if not account:
  459. raise UserError(
  460. _(
  461. "Please define income account for this product:"
  462. ' "%s" (id:%d) - or for its category: "%s".'
  463. )
  464. % (product.name, product.id, product.categ_id.name)
  465. )
  466. fpos = partner.property_account_position_id
  467. if fpos:
  468. account = fpos.map_account(account)
  469. res = {
  470. "name": product.name,
  471. "account_id": account.id,
  472. "price_unit": product.lst_price,
  473. "quantity": qty,
  474. "uom_id": product.uom_id.id,
  475. "product_id": product.id or False,
  476. }
  477. return res
  478. def get_capital_release_mail_template(self):
  479. template = "easy_my_coop.email_template_release_capital"
  480. return self.env.ref(template, False)
  481. def send_capital_release_request(self, invoice):
  482. email_template = self.get_capital_release_mail_template()
  483. # we send the email with the capital release request in attachment
  484. # TODO remove sudo() and give necessary access right
  485. email_template.sudo().send_mail(invoice.id, True)
  486. invoice.sent = True
  487. def get_journal(self):
  488. return self.env.ref("easy_my_coop.subscription_journal")
  489. def get_accounting_account(self):
  490. account_obj = self.env["account.account"]
  491. if self.company_id.property_cooperator_account:
  492. account = self.company_id.property_cooperator_account
  493. else:
  494. accounts = account_obj.search([("code", "=", "416000")])
  495. if accounts:
  496. account = accounts[0]
  497. else:
  498. raise UserError(
  499. _("You must set a cooperator account on you company.")
  500. )
  501. return account
  502. def get_invoice_vals(self, partner):
  503. return {
  504. "partner_id": partner.id,
  505. "journal_id": self.get_journal().id,
  506. "account_id": self.get_accounting_account().id,
  507. "type": "out_invoice",
  508. "release_capital_request": True,
  509. "subscription_request": self.id,
  510. }
  511. def create_invoice(self, partner):
  512. # creating invoice and invoice lines
  513. invoice_vals = self.get_invoice_vals(partner)
  514. if self.capital_release_request_date:
  515. invoice_vals["date_invoice"] = self.capital_release_request_date
  516. invoice = self.env["account.invoice"].create(invoice_vals)
  517. vals = self._prepare_invoice_line(
  518. self.share_product_id, partner, self.ordered_parts
  519. )
  520. vals["invoice_id"] = invoice.id
  521. self.env["account.invoice.line"].create(vals)
  522. # validate the capital release request
  523. invoice.action_invoice_open()
  524. self.send_capital_release_request(invoice)
  525. return invoice
  526. def get_partner_company_vals(self):
  527. partner_vals = {
  528. "name": self.company_name,
  529. "last_name": self.company_name,
  530. "is_company": self.is_company,
  531. "company_register_number": self.company_register_number, # noqa
  532. "cooperator": True,
  533. "street": self.address,
  534. "zip": self.zip_code,
  535. "city": self.city,
  536. "email": self.company_email,
  537. "out_inv_comm_type": "bba",
  538. "customer": self.share_product_id.customer,
  539. "country_id": self.country_id.id,
  540. "lang": self.lang,
  541. "data_policy_approved": self.data_policy_approved,
  542. "internal_rules_approved": self.internal_rules_approved,
  543. "financial_risk_approved": self.financial_risk_approved,
  544. }
  545. return partner_vals
  546. def get_partner_vals(self):
  547. partner_vals = {
  548. "name": self.name,
  549. "firstname": self.firstname,
  550. "lastname": self.lastname,
  551. "street": self.address,
  552. "zip": self.zip_code,
  553. "email": self.email,
  554. "gender": self.gender,
  555. "cooperator": True,
  556. "city": self.city,
  557. "phone": self.phone,
  558. "country_id": self.country_id.id,
  559. "lang": self.lang,
  560. "birthdate_date": self.birthdate,
  561. "customer": self.share_product_id.customer,
  562. "data_policy_approved": self.data_policy_approved,
  563. "internal_rules_approved": self.internal_rules_approved,
  564. "financial_risk_approved": self.financial_risk_approved,
  565. }
  566. return partner_vals
  567. def get_representative_vals(self):
  568. contact_vals = {
  569. "name": self.name,
  570. "firstname": self.firstname,
  571. "lastname": self.lastname,
  572. "customer": False,
  573. "is_company": False,
  574. "cooperator": True,
  575. "street": self.address,
  576. "gender": self.gender,
  577. "zip": self.zip_code,
  578. "city": self.city,
  579. "phone": self.phone,
  580. "email": self.email,
  581. "country_id": self.country_id.id,
  582. "out_inv_comm_type": "bba",
  583. "out_inv_comm_algorithm": "random",
  584. "lang": self.lang,
  585. "birthdate_date": self.birthdate,
  586. "parent_id": self.partner_id.id,
  587. "representative": True,
  588. "function": self.contact_person_function,
  589. "type": "representative",
  590. "data_policy_approved": self.data_policy_approved,
  591. "internal_rules_approved": self.internal_rules_approved,
  592. "financial_risk_approved": self.financial_risk_approved,
  593. }
  594. return contact_vals
  595. def create_coop_partner(self):
  596. partner_obj = self.env["res.partner"]
  597. if self.is_company:
  598. partner_vals = self.get_partner_company_vals()
  599. else:
  600. partner_vals = self.get_partner_vals()
  601. partner = partner_obj.create(partner_vals)
  602. if self.iban:
  603. self.env["res.partner.bank"].create(
  604. {"partner_id": partner.id, "acc_number": self.iban}
  605. )
  606. return partner
  607. def set_membership(self):
  608. # To be overridden
  609. return True
  610. @api.multi
  611. def validate_subscription_request(self):
  612. self.ensure_one()
  613. # todo rename to validate (careful with iwp dependencies)
  614. partner_obj = self.env["res.partner"]
  615. if self.ordered_parts <= 0:
  616. raise UserError(_("Number of share must be greater than 0."))
  617. if self.partner_id:
  618. partner = self.partner_id
  619. else:
  620. partner = None
  621. domain = []
  622. if self.already_cooperator:
  623. raise UserError(
  624. _(
  625. "The checkbox already cooperator is"
  626. " checked please select a cooperator."
  627. )
  628. )
  629. elif self.is_company and self.company_register_number:
  630. domain = [
  631. (
  632. "company_register_number",
  633. "=",
  634. self.company_register_number,
  635. )
  636. ] # noqa
  637. elif not self.is_company and self.email:
  638. domain = [("email", "=", self.email)]
  639. if domain:
  640. partner = partner_obj.search(domain)
  641. if not partner:
  642. partner = self.create_coop_partner()
  643. self.partner_id = partner
  644. else:
  645. partner = partner[0]
  646. partner.cooperator = True
  647. if self.is_company and not partner.has_representative():
  648. contact = False
  649. if self.email:
  650. domain = [("email", "=", self.email)]
  651. contact = partner_obj.search(domain)
  652. if contact:
  653. contact.type = "representative"
  654. if not contact:
  655. contact_vals = self.get_representative_vals()
  656. partner_obj.create(contact_vals)
  657. else:
  658. if len(contact) > 1:
  659. raise UserError(
  660. _(
  661. "There is two different persons with the"
  662. " same national register number. Please"
  663. " proceed to a merge before to continue"
  664. )
  665. )
  666. if contact.parent_id and contact.parent_id.id != partner.id:
  667. raise UserError(
  668. _(
  669. "This contact person is already defined"
  670. " for another company. Please select"
  671. " another contact"
  672. )
  673. )
  674. else:
  675. contact.write(
  676. {"parent_id": partner.id, "representative": True}
  677. )
  678. invoice = self.create_invoice(partner)
  679. self.write({"state": "done"})
  680. self.set_membership()
  681. return invoice
  682. @api.multi
  683. def block_subscription_request(self):
  684. self.ensure_one()
  685. self.write({"state": "block"})
  686. @api.multi
  687. def unblock_subscription_request(self):
  688. self.ensure_one()
  689. self.write({"state": "draft"})
  690. @api.multi
  691. def cancel_subscription_request(self):
  692. self.ensure_one()
  693. self.write({"state": "cancelled"})
  694. @api.multi
  695. def put_on_waiting_list(self):
  696. self.ensure_one()
  697. waiting_list_mail_template = self.env.ref(
  698. "easy_my_coop.email_template_waiting_list", False
  699. )
  700. waiting_list_mail_template.send_mail(self.id, True)
  701. self.write({"state": "waiting"})
  702. # todo move to share_line.py
  703. class ShareLine(models.Model):
  704. _name = "share.line"
  705. _description = "Share line"
  706. @api.multi
  707. def _compute_total_line(self):
  708. res = {}
  709. for line in self:
  710. line.total_amount_line = line.share_unit_price * line.share_number
  711. return res
  712. share_product_id = fields.Many2one(
  713. "product.product", string="Share type", required=True, readonly=True
  714. )
  715. share_number = fields.Integer(
  716. string="Number of Share", required=True, readonly=True
  717. )
  718. share_short_name = fields.Char(
  719. related="share_product_id.short_name",
  720. string="Share type name",
  721. readonly=True,
  722. )
  723. share_unit_price = fields.Monetary(
  724. string="Share price",
  725. currency_field="company_currency_id",
  726. readonly=True,
  727. )
  728. effective_date = fields.Date(string="Effective Date", readonly=True)
  729. partner_id = fields.Many2one(
  730. "res.partner",
  731. string="Cooperator",
  732. required=True,
  733. ondelete="cascade",
  734. readonly=True,
  735. )
  736. total_amount_line = fields.Monetary(
  737. string="Total amount line",
  738. currency_field="company_currency_id",
  739. compute="_compute_total_line",
  740. )
  741. company_id = fields.Many2one(
  742. "res.company",
  743. string="Company",
  744. required=True,
  745. change_default=True,
  746. readonly=True,
  747. default=lambda self: self.env["res.company"]._company_default_get(),
  748. )
  749. company_currency_id = fields.Many2one(
  750. "res.currency",
  751. string="Company Currency",
  752. related="company_id.currency_id",
  753. readonly=True,
  754. )
  755. # todo move to subscription_register.py
  756. class SubscriptionRegister(models.Model):
  757. _name = "subscription.register"
  758. _description = "Subscription register"
  759. @api.multi
  760. def _compute_total_line(self):
  761. for line in self:
  762. line.total_amount_line = line.share_unit_price * line.quantity
  763. name = fields.Char(string="Number Operation", required=True, readonly=True)
  764. register_number_operation = fields.Integer(
  765. string="Register Number Operation", required=True, readonly=True
  766. )
  767. partner_id = fields.Many2one(
  768. "res.partner", string="Cooperator", required=True, readonly=True
  769. )
  770. partner_id_to = fields.Many2one(
  771. "res.partner", string="Transfered to", readonly=True
  772. )
  773. date = fields.Date(
  774. string="Subscription Date", required=True, readonly=True
  775. )
  776. quantity = fields.Integer(string="Number of share", readonly=True)
  777. share_unit_price = fields.Monetary(
  778. string="Share price",
  779. currency_field="company_currency_id",
  780. readonly=True,
  781. )
  782. total_amount_line = fields.Monetary(
  783. string="Total amount line",
  784. currency_field="company_currency_id",
  785. compute="_compute_total_line",
  786. )
  787. share_product_id = fields.Many2one(
  788. "product.product",
  789. string="Share type",
  790. required=True,
  791. readonly=True,
  792. domain=[("is_share", "=", True)],
  793. )
  794. share_short_name = fields.Char(
  795. related="share_product_id.short_name",
  796. string="Share type name",
  797. readonly=True,
  798. )
  799. share_to_product_id = fields.Many2one(
  800. "product.product",
  801. string="Share to type",
  802. readonly=True,
  803. domain=[("is_share", "=", True)],
  804. )
  805. share_to_short_name = fields.Char(
  806. related="share_to_product_id.short_name",
  807. string="Share to type name",
  808. readonly=True,
  809. )
  810. quantity_to = fields.Integer(string="Number of share to", readonly=True)
  811. share_to_unit_price = fields.Monetary(
  812. string="Share to price",
  813. currency_field="company_currency_id",
  814. readonly=True,
  815. )
  816. type = fields.Selection(
  817. [
  818. ("subscription", "Subscription"),
  819. ("transfer", "Transfer"),
  820. ("sell_back", "Sell Back"),
  821. ("convert", "Conversion"),
  822. ],
  823. string="Operation Type",
  824. readonly=True,
  825. )
  826. company_id = fields.Many2one(
  827. "res.company",
  828. string="Company",
  829. required=True,
  830. change_default=True,
  831. readonly=True,
  832. default=lambda self: self.env["res.company"]._company_default_get(),
  833. )
  834. company_currency_id = fields.Many2one(
  835. "res.currency",
  836. related="company_id.currency_id",
  837. string="Company Currency",
  838. readonly=True,
  839. )
  840. user_id = fields.Many2one(
  841. "res.users",
  842. string="Responsible",
  843. readonly=True,
  844. default=lambda self: self.env.user,
  845. )
  846. _order = "register_number_operation asc"
  847. @api.model
  848. def read_group(
  849. self,
  850. domain,
  851. fields,
  852. groupby,
  853. offset=0,
  854. limit=None,
  855. orderby=False,
  856. lazy=True,
  857. ):
  858. if "share_unit_price" in fields:
  859. fields.remove("share_unit_price")
  860. if "register_number_operation" in fields:
  861. fields.remove("register_number_operation")
  862. res = super(SubscriptionRegister, self).read_group(
  863. domain,
  864. fields,
  865. groupby,
  866. offset=offset,
  867. limit=limit,
  868. orderby=orderby,
  869. lazy=lazy,
  870. )
  871. if "total_amount_line" in fields:
  872. for line in res:
  873. if "__domain" in line:
  874. lines = self.search(line["__domain"])
  875. inv_value = 0.0
  876. for line2 in lines:
  877. inv_value += line2.total_amount_line
  878. line["total_amount_line"] = inv_value
  879. return res