account_voucher.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # -*- coding: utf-8 -*-
  2. from openerp import api, fields, models
  3. from openerp.exceptions import except_orm
  4. class AccountVoucher(models.Model):
  5. _inherit = 'account.voucher'
  6. @api.model
  7. def create_from_payments(self, values):
  8. # step 1 Verificar Datos
  9. #period Actual
  10. period = self.env['account.period'].search([('date_start', '<=', fields.Date.context_today(self) ),('date_stop', '>=', fields.Date.context_today(self))])
  11. # Diario & Moneda
  12. journal = self.env['account.journal'].browse(int(values['journal_id']))
  13. currency_id = journal.default_credit_account_id.currency_id.id or journal.default_credit_account_id.company_currency_id.id
  14. # Move line
  15. move_line = self.env['account.move.line'].browse(values['line_cr_ids']).sorted(key=lambda r: r.id)
  16. #company
  17. company = self.env['res.company'].browse(int(values['company']))
  18. #partner
  19. partner = self.env['res.partner'].browse(int(values['partner_id']))
  20. #invoice
  21. invoice = self.env['account.invoice'].browse(int(values['invoice']))
  22. line_cr_ids = []
  23. amount = float(values['amount'])
  24. for line in move_line:
  25. line_cr_ids.append([0, False, {
  26. 'date_due': line.date_maturity,
  27. 'account_id': line.account_id.id,
  28. 'date_original': line.move_id.date,
  29. 'move_line_id': line.id,
  30. 'amount_original': abs(line.credit or line.debit or 0.0),
  31. 'amount_unreconciled': abs(line.amount_residual),
  32. 'amount': min(abs(amount), line.amount_residual),
  33. 'reconcile': line.move_id.date <= line.date_maturity,
  34. 'currency_id': currency_id
  35. }])
  36. amount -= min(abs(amount), line.amount_residual)
  37. values = {
  38. 'reference': values['reference'],
  39. 'type': 'receipt',
  40. 'journal_id': journal.id,
  41. 'company_id': company.id,
  42. 'pre_line': True,
  43. 'amount': float(values['amount']),
  44. 'period_id': int(period.id),
  45. 'date': fields.Date.context_today(self),
  46. 'partner_id': partner.id,
  47. 'account_id': journal.default_credit_account_id.id,
  48. 'currency_id': currency_id,
  49. 'line_cr_ids': line_cr_ids
  50. }
  51. account_voucher = self.create(values)
  52. account_voucher.action_move_line_create()
  53. if invoice.residual <= 0:
  54. invoice.write({
  55. 'state': 'paid'
  56. })
  57. return {
  58. 'action_id': self.env['ir.actions.report.xml'].search([('report_name', '=', 'voucher_print.report_voucher')]).id,
  59. 'voucher_id': account_voucher.id
  60. }