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.

99 lines
4.9 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 set_cooperator_effective(self, effective_date):
  19. # flag the partner as a effective member
  20. obj_sequence = self.env['ir.sequence']
  21. mail_template_id = 'easy_my_coop.email_template_certificat'
  22. # if not yet cooperator we generate a cooperator number
  23. if self.partner_id.member is False and self.partner_id.old_member is False:
  24. sequence_id = self.env.ref('easy_my_coop.sequence_subscription', False)
  25. sub_reg_num = sequence_id.next_by_id()
  26. self.partner_id.write({'member': True, 'old_member': False,
  27. 'cooperator_register_number': int(sub_reg_num)})
  28. elif self.partner_id.old_member:
  29. self.partner_id.write({'member': True, 'old_member': False})
  30. else:
  31. mail_template_id = 'easy_my_coop.email_template_certificat_increase'
  32. sequence_operation = self.env.ref('easy_my_coop.sequence_register_operation', False)
  33. sub_reg_operation = sequence_operation.next_by_id()
  34. for line in self.invoice_line_ids:
  35. sub_reg = self.env['subscription.register'].create(
  36. {'name': sub_reg_operation,
  37. 'register_number_operation': int(sub_reg_operation),
  38. 'partner_id': self.partner_id.id,
  39. 'quantity': line.quantity,
  40. 'share_product_id': line.product_id.id,
  41. 'share_unit_price': line.price_unit,
  42. 'date': effective_date,
  43. 'type': 'subscription'})
  44. self.env['share.line'].create({'share_number': line.quantity,
  45. 'share_product_id': line.product_id.id,
  46. 'partner_id': self.partner_id.id,
  47. 'share_unit_price': line.price_unit,
  48. 'effective_date': effective_date})
  49. email_template_obj = self.env['mail.template']
  50. certificat_email_template = self.env.ref(mail_template_id, False)
  51. #certificat_email_template = email_template_obj.search([('name', '=', mail_template_name)])[0]
  52. # we send the email with the certificat in attachment
  53. certificat_email_template.send_mail(self.partner_id.id, False)
  54. return True
  55. def post_process_confirm_paid(self, effective_date):
  56. self.set_cooperator_effective(effective_date)
  57. return True
  58. @api.multi
  59. def confirm_paid(self):
  60. for invoice in self:
  61. #self.env.cr.commit()
  62. super(account_invoice, invoice).confirm_paid()
  63. # we check if there is an open refund for this invoice. in this case we
  64. # don't run the process_subscription function as the invoice has been
  65. # reconciled with a refund and not a payment.
  66. refund = self.search([('type', '=', 'out_refund'), ('origin', '=', invoice.move_name)])
  67. if invoice.partner_id.cooperator and invoice.release_capital_request \
  68. and invoice.type == 'out_invoice' and not refund:
  69. # take the effective date from the payment.
  70. # by default the confirmation date is the payment date
  71. effective_date = datetime.now().strftime("%d/%m/%Y")
  72. if invoice.payment_move_line_ids:
  73. move_line = invoice.payment_move_line_ids[0]
  74. effective_date = move_line.date
  75. invoice.subscription_request.state = 'paid'
  76. invoice.post_process_confirm_paid(effective_date)
  77. # if there is a open refund we mark the subscription as cancelled
  78. elif invoice.partner_id.cooperator and invoice.release_capital_request \
  79. and invoice.type == 'out_invoice' and refund:
  80. invoice.subscription_request.state = 'cancelled'
  81. return True