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.

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