diff --git a/account_payment_disperse/models/account.py b/account_payment_disperse/models/account.py index 896ee9e2..60ba0026 100644 --- a/account_payment_disperse/models/account.py +++ b/account_payment_disperse/models/account.py @@ -10,9 +10,10 @@ class AccountPayment(models.Model): wizard = self.env['account.register.payments'].browse(wizard_id) assert wizard if wizard.is_manual_disperse: - return self._create_payment_entry_manual_disperse( - -sum(wizard.invoice_line_ids.filtered(lambda p: p.partner_id == self.partner_id).mapped('amount')), - wizard) + payment_amount = sum(wizard.invoice_line_ids.filtered(lambda p: p.partner_id == self.partner_id).mapped('amount')) + if amount < 0: + payment_amount = -payment_amount + return self._create_payment_entry_manual_disperse(payment_amount, wizard) return super(AccountPayment, self)._create_payment_entry(amount) diff --git a/account_payment_disperse/tests/test_payment_multi.py b/account_payment_disperse/tests/test_payment_multi.py index d62c8c05..92c6e310 100644 --- a/account_payment_disperse/tests/test_payment_multi.py +++ b/account_payment_disperse/tests/test_payment_multi.py @@ -122,3 +122,35 @@ class PaymentMultiTest(TestPayment): self.assertEqual(inv_1.residual_signed, 0.0) self.assertEqual(inv_2.residual_signed, 200.0) + + def test_vendor_multiple_payments_write_off(self): + inv_1 = self.create_invoice(amount=100, type='in_invoice', currency_id=self.currency_eur_id, partner=self.partner_agrolait.id) + inv_2 = self.create_invoice(amount=500, type='in_invoice', currency_id=self.currency_eur_id, partner=self.partner_agrolait.id) + ids = [inv_1.id, inv_2.id] + register_payments = self.register_payments_model.with_context(active_ids=ids).create({ + 'payment_date': time.strftime('%Y') + '-07-15', + 'journal_id': self.bank_journal_euro.id, + 'payment_method_id': self.payment_method_manual_out.id, + }) + register_payments.amount = 400.0 + register_payments.is_manual_disperse = True + register_payments.writeoff_journal_id = inv_1.journal_id + + with self.assertRaises(ValidationError): + register_payments.create_payments() + + for line in register_payments.invoice_line_ids: + if line.invoice_id == inv_1: + line.amount = 100.0 + if line.invoice_id == inv_2: + line.amount = 300.0 + line.writeoff_acc_id = self.account_revenue + + register_payments.create_payments() + + payment_ids = self.payment_model.search([('invoice_ids', 'in', ids)], order="id desc") + self.assertEqual(len(payment_ids), 1, 'Need only one payment.') + self.assertEqual(payment_ids.amount, 400.0) + + self.assertEqual(inv_1.residual_signed, 0.0) + self.assertEqual(inv_2.residual_signed, 0.0) diff --git a/account_payment_disperse/wizard/register_payment_wizard.py b/account_payment_disperse/wizard/register_payment_wizard.py index c81cd47a..80d4d703 100644 --- a/account_payment_disperse/wizard/register_payment_wizard.py +++ b/account_payment_disperse/wizard/register_payment_wizard.py @@ -53,16 +53,17 @@ class AccountRegisterPaymentsInvoiceLine(models.TransientModel): wizard_id = fields.Many2one('account.register.payments') invoice_id = fields.Many2one('account.invoice', string='Invoice', required=True) - partner_id = fields.Many2one('res.partner', string='Partner', compute='_compute_balances') - residual = fields.Float(string='Remaining', compute='_compute_balances') - residual_due = fields.Float(string='Due', compute='_compute_balances') + partner_id = fields.Many2one('res.partner', string='Partner', compute='_compute_balances', compute_sudo=True) + residual = fields.Float(string='Remaining', compute='_compute_balances', compute_sudo=True) + residual_due = fields.Float(string='Due', compute='_compute_balances', compute_sudo=True) difference = fields.Float(string='Difference', default=0.0) amount = fields.Float(string='Amount') writeoff_acc_id = fields.Many2one('account.account', string='Write-off Account') @api.depends('invoice_id.residual', 'wizard_id.due_date_cutoff', 'invoice_id.partner_id') def _compute_balances(self): - for line in self: + sudo_self = self.sudo() + for line in sudo_self: line.residual = line.invoice_id.residual cutoff_date = line.wizard_id.due_date_cutoff @@ -83,5 +84,6 @@ class AccountRegisterPaymentsInvoiceLine(models.TransientModel): @api.onchange('amount') def _onchange_amount(self): - for line in self: + sudo_self = self.sudo() + for line in sudo_self: line.difference = line.residual - line.amount