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.

117 lines
5.1 KiB

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