From 25001b80e3b7377e7bbad9cd7f738bf45bab4fad Mon Sep 17 00:00:00 2001 From: Eduard Brahas Date: Tue, 5 Nov 2024 17:07:56 +0100 Subject: [PATCH] [IMP] l10n_it_fatturapa: use parent codice_destinatario Uses the parent codice_destinatario for each child if the parent has set the flag l10n_it_use_parent_codice_destinatario. --- l10n_it_fatturapa/README.rst | 1 + l10n_it_fatturapa/__manifest__.py | 2 +- l10n_it_fatturapa/models/partner.py | 75 ++++++++++++++++--- l10n_it_fatturapa/readme/CONTRIBUTORS.rst | 1 + .../static/description/index.html | 1 + l10n_it_fatturapa/views/partner_view.xml | 8 +- .../tests/test_fatturapa_xml_validation.py | 2 +- 7 files changed, 78 insertions(+), 12 deletions(-) diff --git a/l10n_it_fatturapa/README.rst b/l10n_it_fatturapa/README.rst index ede8dbeb5829..a4002cece93e 100644 --- a/l10n_it_fatturapa/README.rst +++ b/l10n_it_fatturapa/README.rst @@ -164,6 +164,7 @@ Contributors * `Ooops `_: * Giovanni Serra + * Eduard Brahas * `Aion Tech `_: diff --git a/l10n_it_fatturapa/__manifest__.py b/l10n_it_fatturapa/__manifest__.py index 891251f361c1..ec72bac46186 100644 --- a/l10n_it_fatturapa/__manifest__.py +++ b/l10n_it_fatturapa/__manifest__.py @@ -6,7 +6,7 @@ { "name": "ITA - Fattura elettronica - Base", - "version": "14.0.2.3.2", + "version": "14.0.2.4.2", "category": "Localization/Italy", "summary": "Fatture elettroniche", "author": "Davide Corio, Agile Business Group, Innoviu, " diff --git a/l10n_it_fatturapa/models/partner.py b/l10n_it_fatturapa/models/partner.py index c622931e391f..856892be55bd 100644 --- a/l10n_it_fatturapa/models/partner.py +++ b/l10n_it_fatturapa/models/partner.py @@ -29,12 +29,22 @@ class ResPartner(models.Model): # 1.1.4 codice_destinatario = fields.Char( "Addressee Code", + compute="_compute_codice_destinatario", + inverse="_inverse_codice_destinatario", + store=True, help="The code, 7 characters long, assigned by ES to subjects with an " "accredited channel; if the addressee didn't accredit a channel " "to ES and invoices are received by PEC, the field must be " "the standard value ('%s')." % STANDARD_ADDRESSEE_CODE, default=STANDARD_ADDRESSEE_CODE, ) + l10n_it_use_parent_codice_destinatario = fields.Boolean( + string="Use addressee code for child contacts", + store=True, + default=False, + help="Instead of using the deafult 0000000 code" + " uses for the childs the one set in the parent", + ) # 1.1.6 pec_destinatario = fields.Char( "Addressee PEC", @@ -54,7 +64,7 @@ class ResPartner(models.Model): ) electronic_invoice_use_this_address = fields.Boolean( - "Use this e-invoicing data when invoicing to this address", + "Use different e-invoicing data when invoicing to this address", help="Set this when the main company has got several Addressee Codes or PEC", ) @@ -168,12 +178,59 @@ def _check_ftpa_partner_data(self): % partner.name ) - @api.onchange("country_id") - def onchange_country_id_e_inv(self): - if self.country_id.code == "IT": - self.codice_destinatario = STANDARD_ADDRESSEE_CODE - else: - self.codice_destinatario = "XXXXXXX" + @api.depends( + "country_id", + "parent_id", + "is_company", + "electronic_invoice_subjected", + "electronic_invoice_obliged_subject", + ) + def _compute_codice_destinatario(self): + for partner in self: + codice_destinatario = None + if ( + not partner.is_company + and not partner.electronic_invoice_use_this_address + and partner.parent_id.l10n_it_use_parent_codice_destinatario + ): + codice_destinatario = self._recursive_parent_codice_destinatario( + partner.parent_id + ) + if codice_destinatario is None: + if partner.country_id.code == "IT": + codice_destinatario = STANDARD_ADDRESSEE_CODE + else: + codice_destinatario = "XXXXXXX" + + partner.codice_destinatario = codice_destinatario + + def _inverse_codice_destinatario(self): + """ + Inverse method to propagate changes in codice_destinatario to child records. + """ + for record in self: + # Find child records that inherit codice_destinatario from this parent + child_records = self.search( + [ + ("parent_id", "=", record.id), + ("l10n_it_use_parent_codice_destinatario", "=", False), + ] + ) + # Update the codice_destinatario of each child record to match the parent + for child in child_records: + child.codice_destinatario = record.codice_destinatario + + def _recursive_parent_codice_destinatario(self, parent): + """ + Recursively finds the codice_destinatario from the first ancestor + that has set the flag l10n_it_use_parent_codice_destinatario. + Returns None if no codice_destinatario is found. + """ + if parent.l10n_it_use_parent_codice_destinatario: + return parent.codice_destinatario + elif parent.parent_id: + return self._recursive_parent_codice_destinatario(parent.parent_id) + return None @api.onchange("electronic_invoice_subjected") def onchange_electronic_invoice_subjected(self): @@ -181,12 +238,12 @@ def onchange_electronic_invoice_subjected(self): self.electronic_invoice_obliged_subject = False else: if self.supplier_rank > 0: - self.onchange_country_id_e_inv() + self._compute_codice_destinatario() self.electronic_invoice_obliged_subject = True @api.onchange("electronic_invoice_obliged_subject") def onchange_e_inv_obliged_subject(self): if not self.electronic_invoice_obliged_subject: - self.onchange_country_id_e_inv() + self._compute_codice_destinatario() self.pec_destinatario = "" self.eori_code = "" diff --git a/l10n_it_fatturapa/readme/CONTRIBUTORS.rst b/l10n_it_fatturapa/readme/CONTRIBUTORS.rst index c8af4792ccc1..283749e8d3ec 100644 --- a/l10n_it_fatturapa/readme/CONTRIBUTORS.rst +++ b/l10n_it_fatturapa/readme/CONTRIBUTORS.rst @@ -9,6 +9,7 @@ * `Ooops `_: * Giovanni Serra + * Eduard Brahas * `Aion Tech `_: diff --git a/l10n_it_fatturapa/static/description/index.html b/l10n_it_fatturapa/static/description/index.html index 944438dcab1b..475cb47c20c2 100644 --- a/l10n_it_fatturapa/static/description/index.html +++ b/l10n_it_fatturapa/static/description/index.html @@ -501,6 +501,7 @@

Contributors

diff --git a/l10n_it_fatturapa/views/partner_view.xml b/l10n_it_fatturapa/views/partner_view.xml index 1a0bd8a10d13..06a631d40404 100644 --- a/l10n_it_fatturapa/views/partner_view.xml +++ b/l10n_it_fatturapa/views/partner_view.xml @@ -32,9 +32,15 @@ placeholder="IPA123" attrs="{'invisible': [('is_pa','=', False)]}" /> +