From 62520fbefeea6bb91b78e9a902367b303d455d27 Mon Sep 17 00:00:00 2001 From: "Guillaume (guva)" Date: Wed, 27 Jul 2022 14:24:13 +0000 Subject: [PATCH] [FIX] account: payment method for different payment types This commit fix a bug noticed when fixing this other one in v15 https://github.com/odoo/odoo/pull/93943. We notice that, in addition to not set correctly the payment type, the payment method was wrong too. With this commit, when creating payments from batch, we check for each payment if its type is the same as the one set on the wizard. If not (which means that there are payments of differents types), we set the payment method regarding the payment type. opw-2852186 closes odoo/odoo#98153 X-original-commit: af1fab8dab141c49d98b431a40a5ce98f2f3249c Signed-off-by: Laurent Smet Signed-off-by: Guillaume Vanleynseele (guva) --- .../tests/test_account_payment_register.py | 73 ++++++++++++++++++- .../wizard/account_payment_register.py | 7 +- 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/addons/account/tests/test_account_payment_register.py b/addons/account/tests/test_account_payment_register.py index de9d4b66459..59d900285cc 100644 --- a/addons/account/tests/test_account_payment_register.py +++ b/addons/account/tests/test_account_payment_register.py @@ -537,7 +537,7 @@ class TestAccountPaymentRegister(AccountTestInvoicingCommon): }, { 'ref': 'RBILL/2017/01/0001', - 'payment_method_line_id': self.outbound_payment_method_line.id, + 'payment_method_line_id': self.inbound_payment_method_line.id, }, ]) self.assertRecordValues(payments[0].line_ids.sorted('balance') + payments[1].line_ids.sorted('balance') + payments[2].line_ids.sorted('balance'), [ @@ -1159,3 +1159,74 @@ class TestAccountPaymentRegister(AccountTestInvoicingCommon): {'amount_residual': 0.0, 'amount_residual_currency': 0.0, 'currency_id': self.company_data['currency'].id, 'reconciled': True}, {'amount_residual': 0.0, 'amount_residual_currency': 0.0, 'currency_id': self.currency_data['currency'].id, 'reconciled': True}, ]) + + def test_payment_method_different_type_single_batch_not_grouped(self): + """ Test payment methods when paying a bill and a refund with separated payments (1000 + -2000).""" + in_refund = self.env['account.move'].create({ + 'move_type': 'in_refund', + 'date': '2017-01-01', + 'invoice_date': '2017-01-01', + 'partner_id': self.partner_a.id, + 'invoice_line_ids': [(0, 0, {'product_id': self.product_a.id, 'price_unit': 1600.0, 'tax_ids': False})], + }) + in_refund.action_post() + + active_ids = (self.in_invoice_1 + in_refund).ids + payments = self.env['account.payment.register'].with_context(active_model='account.move', active_ids=active_ids).create({ + 'group_payment': False, + })._create_payments() + + self.assertRecordValues(payments[0], [ + { + 'ref': 'BILL/2017/01/0001', + 'payment_method_line_id': self.bank_journal_1.outbound_payment_method_line_ids[0].id, + 'payment_type': 'outbound', + } + ]) + + self.assertRecordValues(payments[1], [ + { + 'ref': 'RBILL/2017/01/0002', + 'payment_method_line_id': self.bank_journal_1.inbound_payment_method_line_ids[0].id, + 'payment_type': 'inbound', + }, + ]) + + self.assertRecordValues(payments[0].line_ids.sorted('balance'), [ + # == Payment 1: to pay in_invoice_1 == + # Liquidity line: + { + 'debit': 0.0, + 'credit': 1000.0, + 'currency_id': self.company_data['currency'].id, + 'amount_currency': -1000.0, + 'reconciled': False, + }, + # Payable line: + { + 'debit': 1000.0, + 'credit': 0.0, + 'currency_id': self.company_data['currency'].id, + 'amount_currency': 1000.0, + 'reconciled': True, + }, + ]) + self.assertRecordValues(payments[1].line_ids.sorted('balance'), [ + # == Payment 2: to pay in_refund_1 == + # Payable line: + { + 'debit': 0.0, + 'credit': 1600.0, + 'currency_id': self.company_data['currency'].id, + 'amount_currency': -1600.0, + 'reconciled': True, + }, + # Liquidity line: + { + 'debit': 1600.0, + 'credit': 0.0, + 'currency_id': self.company_data['currency'].id, + 'amount_currency': 1600.0, + 'reconciled': False, + }, + ]) diff --git a/addons/account/wizard/account_payment_register.py b/addons/account/wizard/account_payment_register.py index 81feee97191..d515c8a533c 100644 --- a/addons/account/wizard/account_payment_register.py +++ b/addons/account/wizard/account_payment_register.py @@ -595,6 +595,11 @@ class AccountPaymentRegister(models.TransientModel): else: partner_bank_id = batch_result['payment_values']['partner_bank_id'] + payment_method_line = self.payment_method_line_id + + if batch_values['payment_type'] != payment_method_line.payment_type: + payment_method_line = self.journal_id._get_available_payment_method_lines(batch_values['payment_type'])[:1] + return { 'date': self.payment_date, 'amount': batch_values['source_amount_currency'], @@ -605,7 +610,7 @@ class AccountPaymentRegister(models.TransientModel): 'currency_id': batch_values['source_currency_id'], 'partner_id': batch_values['partner_id'], 'partner_bank_id': partner_bank_id, - 'payment_method_line_id': self.payment_method_line_id.id, + 'payment_method_line_id': payment_method_line.id, 'destination_account_id': batch_result['lines'][0].account_id.id }