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.

90 lines
4.6 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', string='Subscription request')
  7. release_capital_request = fields.Boolean(string='Release of capital request')
  8. @api.model
  9. def _prepare_refund(self, invoice, date_invoice=None, date=None, description=None, journal_id=None):
  10. values = super(account_invoice, self)._prepare_refund(invoice, date_invoice, date, description, journal_id)
  11. values['release_capital_request'] = self.release_capital_request
  12. return values
  13. def set_cooperator_effective(self, effective_date):
  14. # flag the partner as a effective member
  15. obj_sequence = self.env['ir.sequence']
  16. mail_template_name = 'Payment Received Confirmation - Send By Email'
  17. # if not yet cooperator we generate a cooperator number
  18. if self.partner_id.member == False and self.partner_id.old_member == False:
  19. sequence_id = obj_sequence.search([('name','=','Subscription Register')])[0]
  20. sub_reg_num = sequence_id.next_by_id()
  21. self.partner_id.write({'member':True,'old_member':False,
  22. 'cooperator_register_number':int(sub_reg_num)})
  23. elif self.partner_id.old_member == True:
  24. self.partner_id.write({'member':True,'old_member':False})
  25. else:
  26. mail_template_name = 'Share Increase - Payment Received Confirmation - Send By Email'
  27. sequence_operation = obj_sequence.search([('name','=','Register Operation')])[0]
  28. sub_reg_operation = sequence_operation.next_by_id()
  29. for line in self.invoice_line_ids:
  30. sub_reg = self.env['subscription.register'].create(
  31. {'name':sub_reg_operation,
  32. 'register_number_operation':int(sub_reg_operation),
  33. 'partner_id':self.partner_id.id,
  34. 'quantity':line.quantity,
  35. 'share_product_id':line.product_id.id,
  36. 'share_unit_price':line.price_unit,
  37. 'date':effective_date,
  38. 'type':'subscription'})
  39. self.env['share.line'].create({'share_number':line.quantity,
  40. 'share_product_id':line.product_id.id,
  41. 'partner_id':self.partner_id.id,
  42. 'share_unit_price':line.price_unit,
  43. 'effective_date':effective_date})
  44. email_template_obj = self.env['mail.template']
  45. certificat_email_template = email_template_obj.search([('name', '=', mail_template_name)])[0]
  46. # we send the email with the certificat in attachment
  47. certificat_email_template.send_mail(self.partner_id.id, False)
  48. return True
  49. def post_process_confirm_paid(self, effective_date):
  50. self.set_cooperator_effective(effective_date)
  51. return True
  52. @api.multi
  53. def confirm_paid(self):
  54. for invoice in self:
  55. super(account_invoice, invoice).confirm_paid()
  56. # we check if there is an open refund for this invoice. in this case we
  57. # don't run the process_subscription function as the invoice has been
  58. # reconciled with a refund and not a payment.
  59. refund = self.search([('type','=','out_refund'),('origin','=',invoice.number)]).filtered(lambda record: record.state == 'open')
  60. if invoice.partner_id.cooperator and invoice.release_capital_request \
  61. and invoice.type == 'out_invoice' and not refund:
  62. #take the effective date from the payment. by default the confirmation date is the payment date
  63. effective_date = datetime.now().strftime("%d/%m/%Y")
  64. if invoice.payment_move_line_ids:
  65. move_line = invoice.payment_move_line_ids[0]
  66. effective_date = move_line.date
  67. invoice.subscription_request.state = 'paid'
  68. invoice.post_process_confirm_paid(effective_date)
  69. # if there is a open refund we mark the subscription as cancelled
  70. elif invoice.partner_id.cooperator and invoice.release_capital_request \
  71. and invoice.type == 'out_invoice' and refund:
  72. invoice.subscription_request.state = 'cancelled'
  73. return True