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.

197 lines
6.7 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. from odoo import api, fields, models
  6. class AccountInvoice(models.Model):
  7. _inherit = "account.invoice"
  8. subscription_request = fields.Many2one(
  9. "subscription.request", string="Subscription request"
  10. )
  11. release_capital_request = fields.Boolean(
  12. string="Release of capital request"
  13. )
  14. @api.model
  15. def _prepare_refund(
  16. self,
  17. invoice,
  18. date_invoice=None,
  19. date=None,
  20. description=None,
  21. journal_id=None,
  22. ):
  23. values = super(AccountInvoice, self)._prepare_refund(
  24. invoice, date_invoice, date, description, journal_id
  25. )
  26. values["release_capital_request"] = self.release_capital_request
  27. return values
  28. def create_user(self, partner):
  29. user_obj = self.env["res.users"]
  30. email = partner.email
  31. user = user_obj.search([("login", "=", email)])
  32. if not user:
  33. user = user_obj.search(
  34. [("login", "=", email), ("active", "=", False)]
  35. )
  36. if user:
  37. user.sudo().write({"active": True})
  38. else:
  39. user_values = {"partner_id": partner.id, "login": email}
  40. user = user_obj.sudo()._signup_create_user(user_values)
  41. user.sudo().with_context(
  42. {"create_user": True}
  43. ).action_reset_password()
  44. return user
  45. def get_mail_template_certificate(self):
  46. if self.partner_id.member:
  47. mail_template = "easy_my_coop.email_template_certificat_increase"
  48. else:
  49. mail_template = "easy_my_coop.email_template_certificat"
  50. return self.env.ref(mail_template)
  51. def get_sequence_register(self):
  52. return self.env.ref("easy_my_coop.sequence_subscription", False)
  53. def get_sequence_operation(self):
  54. return self.env.ref("easy_my_coop.sequence_register_operation", False)
  55. def get_share_line_vals(self, line, effective_date):
  56. return {
  57. "share_number": line.quantity,
  58. "share_product_id": line.product_id.id,
  59. "partner_id": self.partner_id.id,
  60. "share_unit_price": line.price_unit,
  61. "effective_date": effective_date,
  62. }
  63. def get_subscription_register_vals(self, line, effective_date):
  64. return {
  65. "partner_id": self.partner_id.id,
  66. "quantity": line.quantity,
  67. "share_product_id": line.product_id.id,
  68. "share_unit_price": line.price_unit,
  69. "date": effective_date,
  70. "type": "subscription",
  71. }
  72. def get_membership_vals(self):
  73. # flag the partner as an effective member
  74. # if not yet cooperator we generate a cooperator number
  75. vals = {}
  76. if (
  77. self.partner_id.member is False
  78. and self.partner_id.old_member is False
  79. ):
  80. sequence_id = self.get_sequence_register()
  81. sub_reg_num = sequence_id.next_by_id()
  82. vals = {
  83. "member": True,
  84. "old_member": False,
  85. "cooperator_register_number": int(sub_reg_num),
  86. }
  87. elif self.partner_id.old_member:
  88. vals = {"member": True, "old_member": False}
  89. return vals
  90. def set_membership(self):
  91. vals = self.get_membership_vals()
  92. self.partner_id.write(vals)
  93. return True
  94. def send_certificate_email(self, certificate_email_template, sub_reg_line):
  95. # we send the email with the certificate in attachment
  96. certificate_email_template.sudo().send_mail(self.partner_id.id, False)
  97. def set_cooperator_effective(self, effective_date):
  98. sub_register_obj = self.env["subscription.register"]
  99. share_line_obj = self.env["share.line"]
  100. certificate_email_template = self.get_mail_template_certificate()
  101. self.set_membership()
  102. sequence_operation = self.get_sequence_operation()
  103. sub_reg_operation = sequence_operation.next_by_id()
  104. for line in self.invoice_line_ids:
  105. sub_reg_vals = self.get_subscription_register_vals(
  106. line, effective_date
  107. )
  108. sub_reg_vals["name"] = sub_reg_operation
  109. sub_reg_vals["register_number_operation"] = int(sub_reg_operation)
  110. sub_reg_line = sub_register_obj.create(sub_reg_vals)
  111. share_line_vals = self.get_share_line_vals(line, effective_date)
  112. share_line_obj.create(share_line_vals)
  113. if line.product_id.mail_template:
  114. certificate_email_template = line.product_id.mail_template
  115. self.send_certificate_email(certificate_email_template, sub_reg_line)
  116. if self.company_id.create_user:
  117. self.create_user(self.partner_id)
  118. return True
  119. def post_process_confirm_paid(self, effective_date):
  120. self.set_cooperator_effective(effective_date)
  121. return True
  122. def get_refund_domain(self, invoice):
  123. return [
  124. ("type", "=", "out_refund"),
  125. ("origin", "=", invoice.move_name),
  126. ]
  127. @api.multi
  128. def action_invoice_paid(self):
  129. super(AccountInvoice, self).action_invoice_paid()
  130. for invoice in self:
  131. # we check if there is an open refund for this invoice. in this
  132. # case we don't run the process_subscription function as the
  133. # invoice has been reconciled with a refund and not a payment.
  134. domain = self.get_refund_domain(invoice)
  135. refund = self.search(domain)
  136. if (
  137. invoice.partner_id.cooperator
  138. and invoice.release_capital_request
  139. and invoice.type == "out_invoice"
  140. and not refund
  141. ):
  142. # take the effective date from the payment.
  143. # by default the confirmation date is the payment date
  144. effective_date = datetime.now().strftime("%d/%m/%Y")
  145. if invoice.payment_move_line_ids:
  146. move_line = invoice.payment_move_line_ids[0]
  147. effective_date = move_line.date
  148. invoice.subscription_request.state = "paid"
  149. invoice.post_process_confirm_paid(effective_date)
  150. # if there is a open refund we mark the subscription as cancelled
  151. elif (
  152. invoice.partner_id.cooperator
  153. and invoice.release_capital_request
  154. and invoice.type == "out_invoice"
  155. and refund
  156. ):
  157. invoice.subscription_request.state = "cancelled"
  158. return True