123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- # -*- 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 = self.create(values)
- account_voucher.action_move_line_create()
- if invoice.residual <= 0:
- invoice.write({
- 'state': 'paid'
- })
- return {
- 'action_id': self.env['ir.actions.report.xml'].search([('report_name', '=', 'voucher_print.report_voucher')]).id,
- 'voucher_id': account_voucher.id
- }
|