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.

135 lines
5.8 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 set_cooperator_effective(self, effective_date):
  42. # flag the partner as a effective member
  43. mail_template_id = self.get_mail_template_certificate()
  44. # if not yet cooperator we generate a cooperator number
  45. if self.partner_id.member is False and self.partner_id.old_member is False:
  46. sequence_id = self.get_sequence_register()
  47. sub_reg_num = sequence_id.next_by_id()
  48. self.partner_id.write({
  49. 'member': True, 'old_member': False,
  50. 'cooperator_register_number': int(sub_reg_num)})
  51. elif self.partner_id.old_member:
  52. self.partner_id.write({'member': True, 'old_member': False})
  53. else:
  54. mail_template_id = self.get_mail_template_certificate()
  55. sequence_operation = self.get_sequence_operation()
  56. sub_reg_operation = sequence_operation.next_by_id()
  57. certificate_email_template = self.env.ref(mail_template_id, False)
  58. for line in self.invoice_line_ids:
  59. self.env['subscription.register'].create({
  60. 'name': sub_reg_operation,
  61. 'register_number_operation': int(sub_reg_operation),
  62. 'partner_id': self.partner_id.id,
  63. 'quantity': line.quantity,
  64. 'share_product_id': line.product_id.id,
  65. 'share_unit_price': line.price_unit,
  66. 'date': effective_date,
  67. 'type': 'subscription'})
  68. self.env['share.line'].create({
  69. 'share_number': line.quantity,
  70. 'share_product_id': line.product_id.id,
  71. 'partner_id': self.partner_id.id,
  72. 'share_unit_price': line.price_unit,
  73. 'effective_date': effective_date
  74. })
  75. if line.product_id.mail_template:
  76. certificate_email_template = line.product_id.mail_template
  77. # we send the email with the certificate in attachment
  78. certificate_email_template.send_mail(self.partner_id.id, False)
  79. if self.company_id.create_user:
  80. self.create_user(self.partner_id)
  81. return True
  82. def post_process_confirm_paid(self, effective_date):
  83. self.set_cooperator_effective(effective_date)
  84. return True
  85. @api.multi
  86. def action_invoice_paid(self):
  87. super(account_invoice, self).action_invoice_paid()
  88. for invoice in self:
  89. # we check if there is an open refund for this invoice. in this
  90. # case we don't run the process_subscription function as the
  91. # invoice has been reconciled with a refund and not a payment.
  92. refund = self.search([('type', '=', 'out_refund'),
  93. ('origin', '=', invoice.move_name)])
  94. if invoice.partner_id.cooperator \
  95. and invoice.release_capital_request \
  96. and invoice.type == 'out_invoice' and not refund:
  97. # take the effective date from the payment.
  98. # by default the confirmation date is the payment date
  99. effective_date = datetime.now().strftime("%d/%m/%Y")
  100. if invoice.payment_move_line_ids:
  101. move_line = invoice.payment_move_line_ids[0]
  102. effective_date = move_line.date
  103. invoice.subscription_request.state = 'paid'
  104. invoice.post_process_confirm_paid(effective_date)
  105. # if there is a open refund we mark the subscription as cancelled
  106. elif invoice.partner_id.cooperator and invoice.release_capital_request \
  107. and invoice.type == 'out_invoice' and refund:
  108. invoice.subscription_request.state = 'cancelled'
  109. return True