|
@@ -0,0 +1,91 @@
|
|
|
+# -*- 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'
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+ # step 2 create voucher
|
|
|
+ # account_voucher = self.create({
|
|
|
+ # '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': [[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': abs(line.debit) if amount > line.debit else amount,
|
|
|
+ # 'reconcile': line.move_id.date <= line.date_maturity,
|
|
|
+ # 'currency_id': currency_id
|
|
|
+ # }] for line in move_line]
|
|
|
+ # })
|