diff --git a/addons/account/models/account_invoice.py b/addons/account/models/account_invoice.py index b7a885ef53e..f036e997b44 100644 --- a/addons/account/models/account_invoice.py +++ b/addons/account/models/account_invoice.py @@ -103,7 +103,7 @@ class AccountInvoice(models.Model): def _compute_residual(self): residual = 0.0 residual_company_signed = 0.0 - sign = self.type in ['in_refund', 'out_refund'] and -1 or 1 + sign = self.type in ['in_invoice', 'out_refund'] and -1 or 1 for line in self.sudo().move_id.line_ids: if line.account_id == self.account_id: residual_company_signed += line.amount_residual diff --git a/addons/account/models/account_payment.py b/addons/account/models/account_payment.py index 7d1231ece2e..398982ec836 100644 --- a/addons/account/models/account_payment.py +++ b/addons/account/models/account_payment.py @@ -118,11 +118,11 @@ class account_register_payments(models.TransientModel): total = 0 for inv in invoice_ids: if inv.currency_id == payment_currency: - total += MAP_INVOICE_TYPE_PAYMENT_SIGN[inv.type] * inv.residual_signed + total += inv.residual_signed else: amount_residual = inv.company_currency_id.with_context(date=self.payment_date).compute( inv.residual_company_signed, payment_currency) - total += MAP_INVOICE_TYPE_PAYMENT_SIGN[inv.type] * amount_residual + total += amount_residual return total @api.onchange('journal_id') @@ -569,8 +569,9 @@ class account_payment(models.Model): # to avoid loss of precision during the currency rate computations. See revision 20935462a0cabeb45480ce70114ff2f4e91eaf79 for a detailed example. total_residual_company_signed = sum(invoice.residual_company_signed for invoice in self.invoice_ids) total_payment_company_signed = self.currency_id.with_context(date=self.payment_date).compute(self.amount, self.company_id.currency_id) + # amout_wo must be positive for out_invoice and in_refund and negative for in_invoice and out_refund in standard use case if self.invoice_ids[0].type in ['in_invoice', 'out_refund']: - amount_wo = total_payment_company_signed - total_residual_company_signed + amount_wo = total_payment_company_signed + total_residual_company_signed else: amount_wo = total_residual_company_signed - total_payment_company_signed # Align the sign of the secondary currency writeoff amount with the sign of the writeoff diff --git a/addons/account/tests/test_payment.py b/addons/account/tests/test_payment.py index 9855452e209..8f671f957c9 100644 --- a/addons/account/tests/test_payment.py +++ b/addons/account/tests/test_payment.py @@ -370,3 +370,29 @@ class TestPayment(AccountingTestCase): {'account_id': self.account_payable.id, 'debit': 0.0, 'credit': 8.65, 'amount_currency': -13.22, 'currency_id': self.currency_usd_id}, {'account_id': self.account_receivable.id, 'debit': 0.0, 'credit': 7.7, 'amount_currency': -11.78, 'currency_id': self.currency_usd_id}, ]) + + def test_payment_and_writeoff_out_refund(self): + # Use case: + # Company is in EUR, create a credit note for 100 EUR and register payment of 90. + # Mark invoice as fully paid with a write_off + # Check that all the aml are correctly created. + invoice = self.create_invoice(amount=100, type='out_refund', currency_id=self.currency_eur_id, partner=self.partner_agrolait.id) + # register payment on invoice + payment = self.payment_model.create({'payment_type': 'outbound', + 'payment_method_id': self.env.ref('account.account_payment_method_manual_in').id, + 'partner_type': 'customer', + 'partner_id': self.partner_agrolait.id, + 'amount': 90, + 'payment_date': time.strftime('%Y') + '-07-15', + 'payment_difference_handling': 'reconcile', + 'writeoff_account_id': self.account_payable.id, + 'journal_id': self.bank_journal_euro.id, + 'invoice_ids': [(4, invoice.id, None)] + }) + payment.post() + self.check_journal_items(payment.move_line_ids, [ + {'account_id': self.account_eur.id, 'debit': 0.0, 'credit': 90.0, 'amount_currency': 0.0, 'currency_id': False}, + {'account_id': self.account_payable.id, 'debit': 0.0, 'credit': 10.0, 'amount_currency': 0.0, 'currency_id': False}, + {'account_id': self.account_receivable.id, 'debit': 100.0, 'credit': 0.0, 'amount_currency': 0.0, 'currency_id': False}, + ]) + self.assertEqual(invoice.state, 'paid')