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.

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