diff --git a/l10n_it_riba/models/account.py b/l10n_it_riba/models/account.py index 26fbcdf847f9..8e88ffc70eb1 100644 --- a/l10n_it_riba/models/account.py +++ b/l10n_it_riba/models/account.py @@ -119,6 +119,41 @@ def _compute_open_amount(self): states={"draft": [("readonly", False)]}, ) + def _domain_riba_supplier_company_bank_id(self): + """Allow to select bank accounts linked to the current company.""" + return self.env["res.partner.bank"]._domain_riba_partner_bank_id() + + riba_supplier_company_bank_id = fields.Many2one( + comodel_name="res.partner.bank", + compute="_compute_riba_supplier_company_bank_id", + domain=_domain_riba_supplier_company_bank_id, + help="Bank account used for the RiBa of this vendor bill.", + readonly=False, + states={ + "posted": [ + ("readonly", True), + ], + }, + store=True, + string="Company Bank Account for Supplier", + ) + + @api.depends( + "is_riba_payment", + ) + def _compute_riba_supplier_company_bank_id(self): + for move in self: + is_riba_payment = move.is_riba_payment + is_purchase_document = move.is_purchase_document(include_receipts=True) + partner_riba_bank_account = ( + move.partner_id.property_riba_supplier_company_bank_id + ) + if is_riba_payment and is_purchase_document and partner_riba_bank_account: + riba_bank_account = partner_riba_bank_account + else: + riba_bank_account = False + move.riba_supplier_company_bank_id = riba_bank_account + @api.model_create_multi def create(self, vals_list): invoices = super().create(vals_list) diff --git a/l10n_it_riba/tests/test_riba.py b/l10n_it_riba/tests/test_riba.py index 40e33c62fbc5..bb47422a03a8 100644 --- a/l10n_it_riba/tests/test_riba.py +++ b/l10n_it_riba/tests/test_riba.py @@ -862,3 +862,26 @@ def test_supplier_company_bank_account_domain(self): # Assert: only the bank account of current company is found self.assertIn(current_bank_account, bank_accounts) self.assertNotIn(other_bank_account, bank_accounts) + + def test_supplier_to_bill_company_bank_account(self): + """A supplier has a company bank account, + it is propagated to its vendor bill.""" + # Arrange + bank_account = self.company_bank + payment_term = self.payment_term1 + supplier = self.partner + supplier.property_supplier_payment_term_id = payment_term + supplier.property_riba_supplier_company_bank_id = bank_account + self.assertTrue(payment_term.riba) + + # Act: Create the vendor bill + bill = self.env["account.move"].create( + { + "move_type": "in_invoice", + "partner_id": supplier.id, + "invoice_payment_term_id": payment_term.id, + } + ) + + # Assert + self.assertEqual(bill.riba_supplier_company_bank_id, bank_account) diff --git a/l10n_it_riba/views/account_view.xml b/l10n_it_riba/views/account_view.xml index 36b5d690959b..b2afd8326cda 100644 --- a/l10n_it_riba/views/account_view.xml +++ b/l10n_it_riba/views/account_view.xml @@ -149,6 +149,16 @@ attrs="{'invisible': ['|',('is_riba_payment','=', False),('move_type','!=','out_invoice')], 'required': ['&',('is_riba_payment','=', True),('move_type','=', 'out_invoice')]}" domain="[('partner_id','=', commercial_partner_id)]" /> +