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.

157 lines
6.3 KiB

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