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.

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