123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- # -*- coding: utf-8 -*-
- from openerp import api, fields, models
- from openerp.exceptions import except_orm
- class AccountVoucher(models.Model):
- _inherit = 'account.voucher'
- @api.model
- def create_from_payments(self, values):
- # step 1 Verificar Datos
- #period Actual
- period = self.env['account.period'].search([('date_start', '<=', fields.Date.context_today(self) ),('date_stop', '>=', fields.Date.context_today(self))])
- # Diario & Moneda
- journal = self.env['account.journal'].browse(int(values['journal_id']))
- currency_id = journal.default_credit_account_id.currency_id.id or journal.default_credit_account_id.company_currency_id.id
- # Move line
- move_line = self.env['account.move.line'].browse(values['line_cr_ids']).sorted(key=lambda r: r.id)
- #company
- company = self.env['res.company'].browse(int(values['company']))
- #partner
- partner = self.env['res.partner'].browse(int(values['partner_id']))
- #invoice
- invoice = self.env['account.invoice'].browse(int(values['invoice']))
- line_cr_ids = []
- amount = float(values['amount'])
- for line in move_line:
- line_cr_ids.append([0, False, {
- 'date_due': line.date_maturity,
- 'account_id': line.account_id.id,
- 'date_original': line.move_id.date,
- 'move_line_id': line.id,
- 'amount_original': abs(line.credit or line.debit or 0.0),
- 'amount_unreconciled': abs(line.amount_residual),
- 'amount': min(abs(amount), line.amount_residual),
- 'reconcile': line.move_id.date <= line.date_maturity,
- 'currency_id': currency_id
- }])
- amount -= min(abs(amount), line.amount_residual)
- values = {
- 'reference': values['reference'],
- 'type': 'receipt',
- 'journal_id': journal.id,
- 'company_id': company.id,
- 'pre_line': True,
- 'amount': float(values['amount']),
- 'period_id': int(period.id),
- 'date': fields.Date.context_today(self),
- 'partner_id': partner.id,
- 'account_id': journal.default_credit_account_id.id,
- 'currency_id': currency_id,
- 'line_cr_ids': line_cr_ids
- }
- account_voucher.create(values)
- # account_voucher.action_move_line_create()
- # Si no tiene deuda actualizar la factura a pagada
- if invoice.residual <= 0:
- invoice.write({
- 'state': 'paid'
- })
- # Usuario id
- user = self.env.user
- # Fecha del servidor
- today = fields.Date.context_today(self)
- # Crea la linea en la caja del pago realizado
- bank_statement_line = [[0, False, {
- 'name': account_voucher.reference,
- 'partner_id': account_voucher.partner_id.id,
- 'amount': account_voucher.amount,
- 'voucher_id': account_voucher.id,
- 'journal_id': account_voucher.journal_id.id,
- 'account_id': account_voucher.account_id.id,
- 'journal_entry_id': account_voucher.move_id.id,
- 'currency_id': account_voucher.currency_id.id,
- 'ref': 'NP'
- }]]
- # Consultar Caja Abierta, Método de Pagos, Fecha de Hoy
- bank_statement = self.env['account.bank.statement'].search([('journal_id', '=', [journal.id]), ('date', '=', today)])
- bank = {
- 'journal_id': account_voucher.journal_id.id,
- 'period_id': account_voucher.period_id.id,
- 'date': today,
- 'user_id': user.id,
- 'state': 'open' if account_voucher.journal_id.type == 'cash' else 'draft',
- 'line_ids':bank_statement_line
- }
- if bank_statement:
- if len(bank_statement) == 1:
- bank_statement.write(bank)
- else:
- bank_statement[len(bank_statement) -1].write(bank)
- else:
- bank_statement = bank_statement.create(bank)
- # Retorna el ticket de pagos
- return {
- 'action_id': self.env['ir.actions.report.xml'].search([('report_name', '=', 'voucher_print.report_voucher')]).id,
- 'voucher_id': account_voucher.id
- }
|