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.

173 lines
6.7 KiB

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