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.

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