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.

168 lines
6.5 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_vals(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 get_membership_vals(self):
  59. # flag the partner as an effective member
  60. # if not yet cooperator we generate a cooperator number
  61. if self.partner_id.member is False \
  62. and self.partner_id.old_member is False:
  63. sequence_id = self.get_sequence_register()
  64. sub_reg_num = sequence_id.next_by_id()
  65. vals = {'member': True, 'old_member': False,
  66. 'cooperator_register_number': int(sub_reg_num)
  67. }
  68. elif self.partner_id.old_member:
  69. vals = {'member': True, 'old_member': False}
  70. return vals
  71. def set_membership(self):
  72. vals = self.get_membership_vals()
  73. self.partner_id.write(vals)
  74. return True
  75. def set_cooperator_effective(self, effective_date):
  76. sub_register_obj = self.env['subscription.register']
  77. share_line_obj = self.env['share.line']
  78. mail_template_id = self.get_mail_template_certificate()
  79. self.set_membership()
  80. sequence_operation = self.get_sequence_operation()
  81. sub_reg_operation = sequence_operation.next_by_id()
  82. certificate_email_template = self.env.ref(mail_template_id, False)
  83. for line in self.invoice_line_ids:
  84. sub_reg_vals = self.get_subscription_register_vals(line,
  85. effective_date)
  86. sub_reg_vals['name'] = sub_reg_operation
  87. sub_reg_vals['register_number_operation'] = int(sub_reg_operation)
  88. sub_register_obj.create(sub_reg_vals)
  89. share_line_vals = self.get_share_line_vals(line, effective_date)
  90. share_line_obj.create(share_line_vals)
  91. if line.product_id.mail_template:
  92. certificate_email_template = line.product_id.mail_template
  93. # we send the email with the certificate in attachment
  94. certificate_email_template.send_mail(self.partner_id.id, False)
  95. if self.company_id.create_user:
  96. self.create_user(self.partner_id)
  97. return True
  98. def post_process_confirm_paid(self, effective_date):
  99. self.set_cooperator_effective(effective_date)
  100. return True
  101. def get_refund_domain(self, invoice):
  102. return [
  103. ('type', '=', 'out_refund'),
  104. ('origin', '=', invoice.move_name)
  105. ]
  106. @api.multi
  107. def action_invoice_paid(self):
  108. super(account_invoice, self).action_invoice_paid()
  109. for invoice in self:
  110. # we check if there is an open refund for this invoice. in this
  111. # case we don't run the process_subscription function as the
  112. # invoice has been reconciled with a refund and not a payment.
  113. domain = self.get_refund_domain(invoice)
  114. refund = self.search(domain)
  115. if invoice.partner_id.cooperator \
  116. and invoice.release_capital_request \
  117. and invoice.type == 'out_invoice' and not refund:
  118. # take the effective date from the payment.
  119. # by default the confirmation date is the payment date
  120. effective_date = datetime.now().strftime("%d/%m/%Y")
  121. if invoice.payment_move_line_ids:
  122. move_line = invoice.payment_move_line_ids[0]
  123. effective_date = move_line.date
  124. invoice.subscription_request.state = 'paid'
  125. invoice.post_process_confirm_paid(effective_date)
  126. # if there is a open refund we mark the subscription as cancelled
  127. elif invoice.partner_id.cooperator \
  128. and invoice.release_capital_request \
  129. and invoice.type == 'out_invoice' and refund:
  130. invoice.subscription_request.state = 'cancelled'
  131. return True