From 432986cc01e137042584d1177afcab29bc86b018 Mon Sep 17 00:00:00 2001 From: tafaRU Date: Tue, 29 Mar 2022 10:39:32 +0200 Subject: [PATCH] [FIX] l10n_it_withholding_tax: allow to record payments by selecting vendor bills from Bills tree view Steps to reproduce the behavior: * create and confirm a bill invoice with withholding tax * select it from the Bills tree view * click on Register Payment button * click on Create Payment button The following error is raised: Traceback (most recent call last): File "/home/tafaru/dev/envs/odoo14/l10n-italy/l10n_it_withholding_tax/tests/test_withholding_tax.py", line 305, in test_create_payments payment_register.with_context(default_move_type="in_invoice").action_create_payments() File "/home/tafaru/dev/envs/odoo14/odoo/addons/account/wizard/account_payment_register.py", line 668, in action_create_payments payments = self._create_payments() File "/home/tafaru/dev/envs/odoo14/odoo/addons/account/wizard/account_payment_register.py", line 664, in _create_payments self._reconcile_payments(to_process, edit_mode=edit_mode) File "/home/tafaru/dev/envs/odoo14/odoo/addons/account/wizard/account_payment_register.py", line 627, in _reconcile_payments .filtered_domain([('account_id', '=', account.id), ('reconciled', '=', False)])\ File "/home/tafaru/dev/envs/odoo14/odoo/addons/account_edi/models/account_move.py", line 427, in reconcile res = super().reconcile() File "/home/tafaru/dev/envs/odoo14/odoo/addons/account/models/account_move.py", line 4838, in reconcile partials = self.env['account.partial.reconcile'].create(sorted_lines._prepare_reconciliation_partials()) File "", line 2, in create File "/home/tafaru/dev/envs/odoo14/odoo/odoo/api.py", line 329, in _model_create_single return self.browse().concat(*(create(self, vals) for vals in arg)) File "/home/tafaru/dev/envs/odoo14/odoo/odoo/api.py", line 329, in return self.browse().concat(*(create(self, vals) for vals in arg)) File "/home/tafaru/dev/envs/odoo14/l10n-italy/l10n_it_withholding_tax/models/account.py", line 100, in create reconcile.generate_wt_moves() File "/home/tafaru/dev/envs/odoo14/l10n-italy/l10n_it_withholding_tax/models/account.py", line 162, in generate_wt_moves wt_move.generate_account_move() File "/home/tafaru/dev/envs/odoo14/l10n-italy/l10n_it_withholding_tax/models/withholding_tax.py", line 440, in generate_account_move move = self.env["account.move"].create(move_vals) File "", line 2, in create File "/home/tafaru/dev/envs/odoo14/odoo/odoo/api.py", line 347, in _model_create_multi return create(self, [arg]) File "/home/tafaru/dev/envs/odoo14/odoo/addons/account/models/account_move.py", line 1978, in create vals_list = self._move_autocomplete_invoice_lines_create(vals_list) File "/home/tafaru/dev/envs/odoo14/odoo/addons/account/models/account_move.py", line 1914, in _move_autocomplete_invoice_lines_create vals = self_ctx._add_missing_default_values(vals) File "/home/tafaru/dev/envs/odoo14/odoo/odoo/models.py", line 1854, in _add_missing_default_values defaults = self.default_get(list(missing_defaults)) File "/home/tafaru/dev/envs/odoo14/odoo/odoo/models.py", line 1309, in default_get defaults[name] = field.default(self) File "/home/tafaru/dev/envs/odoo14/odoo/addons/account/models/account_move.py", line 124, in _get_default_currency journal = self._get_default_journal() File "/home/tafaru/dev/envs/odoo14/odoo/addons/account/models/account_move.py", line 108, in _get_default_journal journal_type=journal.type, odoo.exceptions.UserError: Cannot create an invoice of type in_invoice with a journal having general as type. --- .../models/withholding_tax.py | 6 +- .../tests/test_withholding_tax.py | 64 +++++++++++-------- 2 files changed, 43 insertions(+), 27 deletions(-) diff --git a/l10n_it_withholding_tax/models/withholding_tax.py b/l10n_it_withholding_tax/models/withholding_tax.py index eae7d9b93253..bcd8a88ca5e1 100644 --- a/l10n_it_withholding_tax/models/withholding_tax.py +++ b/l10n_it_withholding_tax/models/withholding_tax.py @@ -437,7 +437,11 @@ def generate_account_move(self): move_lines.append((0, 0, ml_vals)) move_vals["line_ids"] = move_lines - move = self.env["account.move"].create(move_vals) + move = ( + self.env["account.move"] + .with_context(default_move_type="entry") + .create(move_vals) + ) move.action_post() # Save move in the wt move diff --git a/l10n_it_withholding_tax/tests/test_withholding_tax.py b/l10n_it_withholding_tax/tests/test_withholding_tax.py index da8594c7db8c..7386339d38b5 100644 --- a/l10n_it_withholding_tax/tests/test_withholding_tax.py +++ b/l10n_it_withholding_tax/tests/test_withholding_tax.py @@ -40,6 +40,10 @@ def setUp(self): {"name": "Bank", "type": "bank", "code": "BNK67"} ) + # Payment Register + self.payment_register_model = self.env["account.payment.register"] + self.register_view_id = "account.view_account_payment_register_form" + # Payments vals_payment = { "name": "", @@ -139,19 +143,15 @@ def test_withholding_tax(self): "active_model": "account.move", "active_ids": [self.invoice.id], } - register_payments = ( - self.env["account.payment.register"] - .with_context(ctx) - .create( - { - "payment_date": time.strftime("%Y") + "-07-15", - "amount": 800, - "journal_id": self.journal_bank.id, - "payment_method_id": self.env.ref( - "account.account_payment_method_manual_out" - ).id, - } - ) + register_payments = self.payment_register_model.with_context(ctx).create( + { + "payment_date": time.strftime("%Y") + "-07-15", + "amount": 800, + "journal_id": self.journal_bank.id, + "payment_method_id": self.env.ref( + "account.account_payment_method_manual_out" + ).id, + } ) register_payments.action_create_payments() @@ -183,19 +183,15 @@ def test_partial_payment(self): "active_id": self.invoice.id, "default_reconciled_invoice_ids": [(4, self.invoice.id, None)], } - register_payments = ( - self.env["account.payment.register"] - .with_context(ctx) - .create( - { - "payment_date": time.strftime("%Y") + "-07-15", - "amount": 600, - "journal_id": self.journal_bank.id, - "payment_method_id": self.env.ref( - "account.account_payment_method_manual_out" - ).id, - } - ) + register_payments = self.payment_register_model.with_context(ctx).create( + { + "payment_date": time.strftime("%Y") + "-07-15", + "amount": 600, + "journal_id": self.journal_bank.id, + "payment_method_id": self.env.ref( + "account.account_payment_method_manual_out" + ).id, + } ) register_payments.action_create_payments() @@ -283,3 +279,19 @@ def test_duplicating_wt(self): new_tax = self.wt1040.copy() self.assertEqual(new_tax.code, "1040 (copy)") self.assertEqual(new_tax.name, "Code 1040") + + def test_create_payments(self): + """Test create payment when Register Payment wizard is open from Bill tree view""" + ctx = { + "active_ids": [self.invoice.id], + "active_model": "account.move", + } + f = Form( + self.payment_register_model.with_context(ctx), view=self.register_view_id + ) + payment_register = f.save() + # passing default_move_type="in_invoice" in the context in order + # to simulate opening of payment_register from Bills tree view + payment_register.with_context( + default_move_type="in_invoice" + ).action_create_payments()