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.

122 lines
5.4 KiB

  1. # -*- coding: utf-8 -*-
  2. from datetime import datetime
  3. from openerp import api, fields, models
  4. class AccountInvoice(models.Model):
  5. _inherit = 'account.invoice'
  6. subscription_request = fields.Many2one('subscription.request',
  7. string='Subscription request')
  8. release_capital_request = fields.Boolean(
  9. string='Release of capital request')
  10. @api.model
  11. def _prepare_refund(self, invoice, date_invoice=None, date=None,
  12. description=None, journal_id=None):
  13. values = super(AccountInvoice, self)._prepare_refund(
  14. invoice, date_invoice, date,
  15. description, journal_id)
  16. values['release_capital_request'] = self.release_capital_request
  17. return values
  18. def create_user(self, partner):
  19. user_obj = self.env['res.users']
  20. email = partner.email
  21. user = user_obj.search([('login', '=', email)])
  22. if not user:
  23. user = user_obj.search([('login', '=', email),
  24. ('active', '=', False)])
  25. if user:
  26. user.sudo().write({'active': True})
  27. else:
  28. user_values = {'partner_id': partner.id, 'login': email}
  29. user_id = user_obj.sudo()._signup_create_user(user_values)
  30. user = user_obj.browse(user_id)
  31. user.sudo().with_context({'create_user': True}).action_reset_password()
  32. return True
  33. def set_cooperator_effective(self, effective_date):
  34. # flag the partner as a effective member
  35. mail_template_id = 'easy_my_coop.email_template_certificat'
  36. # if not yet cooperator we generate a cooperator number
  37. if self.partner_id.member is False and self.partner_id.old_member is False:
  38. sequence_id = self.env.ref('easy_my_coop.sequence_subscription', False)
  39. sub_reg_num = sequence_id.next_by_id()
  40. self.partner_id.write({
  41. 'member': True, 'old_member': False,
  42. 'cooperator_register_number': int(sub_reg_num)})
  43. elif self.partner_id.old_member:
  44. self.partner_id.write({'member': True, 'old_member': False})
  45. else:
  46. mail_template_id = 'easy_my_coop.email_template_certificat_increase'
  47. sequence_operation = self.env.ref('easy_my_coop.sequence_register_operation', False)
  48. sub_reg_operation = sequence_operation.next_by_id()
  49. certificat_email_template = self.env.ref(mail_template_id, False)
  50. for line in self.invoice_line_ids:
  51. self.env['subscription.register'].create({
  52. 'name': sub_reg_operation,
  53. 'register_number_operation': int(sub_reg_operation),
  54. 'partner_id': self.partner_id.id,
  55. 'quantity': line.quantity,
  56. 'share_product_id': line.product_id.id,
  57. 'share_unit_price': line.price_unit,
  58. 'date': effective_date,
  59. 'type': 'subscription'})
  60. self.env['share.line'].create({
  61. 'share_number': line.quantity,
  62. 'share_product_id': line.product_id.id,
  63. 'partner_id': self.partner_id.id,
  64. 'share_unit_price': line.price_unit,
  65. 'effective_date': effective_date
  66. })
  67. if line.product_id.mail_template:
  68. certificat_email_template = line.product_id.mail_template
  69. # we send the email with the certificat in attachment
  70. certificat_email_template.send_mail(self.partner_id.id, False)
  71. if self.company_id.create_user:
  72. self.create_user(self.partner_id)
  73. return True
  74. def post_process_confirm_paid(self, effective_date):
  75. self.set_cooperator_effective(effective_date)
  76. return True
  77. @api.multi
  78. def confirm_paid(self):
  79. for invoice in self:
  80. super(AccountInvoice, invoice).confirm_paid()
  81. # we check if there is an open refund for this invoice. in this
  82. # case we don't run the process_subscription function as the
  83. # invoice has been reconciled with a refund and not a payment.
  84. refund = self.search([('type', '=', 'out_refund'),
  85. ('origin', '=', invoice.move_name)])
  86. if invoice.partner_id.cooperator and invoice.release_capital_request \
  87. and invoice.type == 'out_invoice' and not refund:
  88. # take the effective date from the payment.
  89. # by default the confirmation date is the payment date
  90. effective_date = datetime.now().strftime("%d/%m/%Y")
  91. if invoice.payment_move_line_ids:
  92. move_line = invoice.payment_move_line_ids[0]
  93. effective_date = move_line.date
  94. invoice.subscription_request.state = 'paid'
  95. invoice.post_process_confirm_paid(effective_date)
  96. # if there is a open refund we mark the subscription as cancelled
  97. elif invoice.partner_id.cooperator and invoice.release_capital_request \
  98. and invoice.type == 'out_invoice' and refund:
  99. invoice.subscription_request.state = 'cancelled'
  100. return True