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.

85 lines
4.2 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. # if not yet cooperator we generate a cooperator number
  17. if self.partner_id.member == False :
  18. sequence_id = obj_sequence.search([('name','=','Subscription Register')])[0]
  19. sub_reg_num = sequence_id.next_by_id()
  20. self.partner_id.write({'member':True,
  21. 'cooperator_register_number':int(sub_reg_num)})
  22. sequence_operation = obj_sequence.search([('name','=','Register Operation')])[0]
  23. sub_reg_operation = sequence_operation.next_by_id()
  24. for line in self.invoice_line_ids:
  25. sub_reg = self.env['subscription.register'].create(
  26. {'name':sub_reg_operation,
  27. 'register_number_operation':int(sub_reg_operation),
  28. 'partner_id':self.partner_id.id,
  29. 'quantity':line.quantity,
  30. 'share_product_id':line.product_id.id,
  31. 'share_unit_price':line.price_unit,
  32. 'date':effective_date,
  33. 'type':'subscription'})
  34. self.env['share.line'].create({'share_number':line.quantity,
  35. 'share_product_id':line.product_id.id,
  36. 'partner_id':self.partner_id.id,
  37. 'share_unit_price':line.price_unit,
  38. 'effective_date':effective_date})
  39. mail_template_name = 'Payment Received Confirmation - Send By Email'
  40. if self.partner_id.member :
  41. mail_template_name = 'Share Increase - Payment Received Confirmation - Send By Email'
  42. email_template_obj = self.env['mail.template']
  43. certificat_email_template = email_template_obj.search([('name', '=', mail_template_name)])[0]
  44. # we send the email with the certificat in attachment
  45. certificat_email_template.send_mail(self.partner_id.id, False)
  46. return True
  47. def post_process_confirm_paid(self, effective_date):
  48. self.set_cooperator_effective(effective_date)
  49. return True
  50. @api.multi
  51. def confirm_paid(self):
  52. super(account_invoice, self).confirm_paid()
  53. for invoice in self:
  54. if invoice.partner_id.cooperator and invoice.release_capital_request and invoice.type == 'out_invoice':
  55. effective_date = datetime.now().strftime("%d/%m/%Y")
  56. #take the effective date from the payment. by default the confirmation date is the payment date
  57. if invoice.payment_move_line_ids :
  58. move_line = invoice.payment_move_line_ids[0]
  59. effective_date = move_line.date
  60. invoice.subscription_request.state = 'paid'
  61. invoice.post_process_confirm_paid(effective_date)
  62. return True
  63. @api.multi
  64. def invoice_print(self):
  65. """ Print the invoice and mark it as sent, so that we can see more
  66. easily the next step of the workflow
  67. """
  68. self.ensure_one()
  69. self.sent = True
  70. return self.env['report'].get_action(self, 'easy_my_coop.report_sexy_invoice')