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.

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