account_voucher.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. # Si no tiene deuda actualizar la factura a pagada
  54. if invoice.residual <= 0:
  55. invoice.write({
  56. 'state': 'paid'
  57. })
  58. # Usuario id
  59. user = self.env.user
  60. # Fecha del servidor
  61. today = fields.Date.context_today(self)
  62. # Crea la linea en la caja del pago realizado
  63. bank_statement_line = [[0, False, {
  64. 'name': account_voucher.reference,
  65. 'partner_id': account_voucher.partner_id.id,
  66. 'amount': account_voucher.amount,
  67. 'voucher_id': account_voucher.id,
  68. 'journal_id': account_voucher.journal_id.id,
  69. 'account_id': account_voucher.account_id.id,
  70. 'journal_entry_id': account_voucher.move_id.id,
  71. 'currency_id': account_voucher.currency_id.id,
  72. 'ref': 'NP'
  73. }]]
  74. # Consultar Caja Abierta, Método de Pagos, Fecha de Hoy
  75. bank_statement = self.env['account.bank.statement'].search([('journal_id', '=', [journal.id]), ('date', '=', today)])
  76. bank = {
  77. 'journal_id': account_voucher.journal_id.id,
  78. 'period_id': account_voucher.period_id.id,
  79. 'date': today,
  80. 'user_id': user.id,
  81. 'state': 'open' if account_voucher.journal_id.type == 'cash' else 'draft',
  82. 'line_ids':bank_statement_line
  83. }
  84. if bank_statement:
  85. if len(bank_statement) == 1:
  86. bank_statement.write(bank)
  87. else:
  88. bank_statement[len(bank_statement) -1].write(bank)
  89. else:
  90. bank_statement = bank_statement.create(bank)
  91. # Retorna el ticket de pagos
  92. return {
  93. 'action_id': self.env['ir.actions.report.xml'].search([('report_name', '=', 'voucher_print.report_voucher')]).id,
  94. 'voucher_id': account_voucher.id
  95. }