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