From 679e9040bc216ebb71829b064a011ee5038df76b Mon Sep 17 00:00:00 2001 From: Simone Rubino Date: Thu, 27 Jun 2024 10:51:25 +0200 Subject: [PATCH 1/2] [IMP] l10n_it_riba: Show company bank account used for supplier --- l10n_it_riba/models/account.py | 8 ++++++++ l10n_it_riba/models/partner.py | 17 +++++++++++++++++ l10n_it_riba/tests/test_riba.py | 23 +++++++++++++++++++++++ l10n_it_riba/views/partner_view.xml | 12 ++++++++++++ 4 files changed, 60 insertions(+) diff --git a/l10n_it_riba/models/account.py b/l10n_it_riba/models/account.py index be73ea8b5fc5..26fbcdf847f9 100644 --- a/l10n_it_riba/models/account.py +++ b/l10n_it_riba/models/account.py @@ -39,6 +39,14 @@ class ResPartnerBankAdd(models.Model): help="Identification Code of the Company in the Interbank System.", ) + @api.model + def _domain_riba_partner_bank_id(self): + """Domain to select bank accounts linked to the current company.""" + company = self.env.company + return [ + ("partner_id", "=", company.partner_id.id), + ] + class AccountMove(models.Model): _inherit = "account.move" diff --git a/l10n_it_riba/models/partner.py b/l10n_it_riba/models/partner.py index 7da6ba1c7817..0f1b6243e533 100644 --- a/l10n_it_riba/models/partner.py +++ b/l10n_it_riba/models/partner.py @@ -16,3 +16,20 @@ class ResPartner(models.Model): group_riba = fields.Boolean( "Group RiBa", help="Group RiBa by customer while issuing." ) + is_supplier_payment_riba = fields.Boolean( + string="Is RiBa Payment", + related="property_supplier_payment_term_id.riba", + readonly=True, + ) + + def _domain_property_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() + + property_riba_supplier_company_bank_id = fields.Many2one( + comodel_name="res.partner.bank", + company_dependent=True, + string="Company Bank Account for Supplier", + domain=_domain_property_riba_supplier_company_bank_id, + help="Bank account used for the RiBa of this supplier.", + ) diff --git a/l10n_it_riba/tests/test_riba.py b/l10n_it_riba/tests/test_riba.py index 404c7455b520..40e33c62fbc5 100644 --- a/l10n_it_riba/tests/test_riba.py +++ b/l10n_it_riba/tests/test_riba.py @@ -839,3 +839,26 @@ def test_riba_payment_date_multiple_lines(self): self.assertEqual(slip.state, "paid") payment_move = slip.payment_ids.move_id self.assertEqual(payment_move.date, payment_date) + + def test_supplier_company_bank_account_domain(self): + """The domain for Company Bank Account for Supplier + only shows bank accounts of current company.""" + # Arrange + current_company, other_company = self.env.company, self.company2 + current_bank_account = self.company_bank + other_bank_account = self.company2_bank + # pre-condition: Bank accounts belong to different companies + self.assertNotEqual(current_company, other_company) + self.assertEqual(current_bank_account.partner_id, current_company.partner_id) + self.assertEqual(other_bank_account.partner_id, other_company.partner_id) + + # Act: Search bank accounts + domain = self.env["res.partner"].fields_get( + allfields=["property_riba_supplier_company_bank_id"], + attributes=["domain"], + )["property_riba_supplier_company_bank_id"]["domain"] + bank_accounts = self.env["res.partner.bank"].search(domain) + + # Assert: only the bank account of current company is found + self.assertIn(current_bank_account, bank_accounts) + self.assertNotIn(other_bank_account, bank_accounts) diff --git a/l10n_it_riba/views/partner_view.xml b/l10n_it_riba/views/partner_view.xml index 056168977bc2..444f234d9920 100644 --- a/l10n_it_riba/views/partner_view.xml +++ b/l10n_it_riba/views/partner_view.xml @@ -18,6 +18,18 @@ + + + + From cadca473356cb5a3b419c87c814d832f67e5ffa7 Mon Sep 17 00:00:00 2001 From: Simone Rubino Date: Thu, 27 Jun 2024 11:42:45 +0200 Subject: [PATCH 2/2] [IMP] l10n_it_riba: Propagate supplier company bank account to bill --- l10n_it_riba/models/account.py | 35 +++++++++++++++++++++++++++++ l10n_it_riba/tests/test_riba.py | 23 +++++++++++++++++++ l10n_it_riba/views/account_view.xml | 10 +++++++++ 3 files changed, 68 insertions(+) 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)]" /> +