Skip to content

Commit

Permalink
[IMP] l10n_it_fatturapa: use parent codice_destinatario
Browse files Browse the repository at this point in the history
Uses the parent codice_destinatario for each child
if the parent has set the flag l10n_it_use_parent_codice_destinatario.
  • Loading branch information
toita86 committed Nov 12, 2024
1 parent 54a3979 commit 3d8f29b
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 10 deletions.
1 change: 1 addition & 0 deletions l10n_it_fatturapa/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ Contributors
* `Ooops <https://www.ooops404.com>`_:

* Giovanni Serra <giovanni@gslab.it>
* Eduard Brahas <eduard@ooops404.com>

* `Aion Tech <https://aiontech.company/>`_:

Expand Down
2 changes: 1 addition & 1 deletion l10n_it_fatturapa/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, "
Expand Down
68 changes: 60 additions & 8 deletions l10n_it_fatturapa/models/partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
)

Expand Down Expand Up @@ -168,25 +178,67 @@ 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:
@api.depends(
"country_id",
"parent_id",
"is_company",
"electronic_invoice_subjected",
"electronic_invoice_obliged_subject",
)
def _compute_codice_destinatario(self):
if (
not self.is_company
and not self.electronic_invoice_use_this_address
and self.parent_id.l10n_it_use_parent_codice_destinatario
):
self.codice_destinatario = self._recursive_parent_codice_destinatario(
self.parent_id
)
elif self.country_id.code != "IT":
self.codice_destinatario = "XXXXXXX"
else:
self.codice_destinatario = STANDARD_ADDRESSEE_CODE

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 STANDARD_ADDRESSEE_CODE 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 STANDARD_ADDRESSEE_CODE

@api.onchange("electronic_invoice_subjected")
def onchange_electronic_invoice_subjected(self):
if not self.electronic_invoice_subjected:
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 = ""
1 change: 1 addition & 0 deletions l10n_it_fatturapa/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* `Ooops <https://www.ooops404.com>`_:

* Giovanni Serra <giovanni@gslab.it>
* Eduard Brahas <eduard@ooops404.com>

* `Aion Tech <https://aiontech.company/>`_:

Expand Down
1 change: 1 addition & 0 deletions l10n_it_fatturapa/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ <h2><a class="toc-backref" href="#toc-entry-7">Contributors</a></h2>
<blockquote>
<ul class="simple">
<li>Giovanni Serra &lt;<a class="reference external" href="mailto:giovanni&#64;gslab.it">giovanni&#64;gslab.it</a>&gt;</li>
<li>Eduard Brahas &lt;<a class="reference external" href="mailto:eduard&#64;ooops404.com">eduard&#64;ooops404.com</a>&gt;</li>
</ul>
</blockquote>
</li>
Expand Down
8 changes: 7 additions & 1 deletion l10n_it_fatturapa/views/partner_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@
placeholder="IPA123"
attrs="{'invisible': [('is_pa','=', False)]}"
/>
<field
name="l10n_it_use_parent_codice_destinatario"
attrs="{'invisible': [('is_company', '=', False)]}"
/>
<field
name="codice_destinatario"
attrs="{'invisible': [('is_pa', '=', True)]}"
attrs="{'invisible': [('is_pa', '=', True)],
'readonly': [('l10n_it_use_parent_codice_destinatario', '=', False), ('is_company', '=', False)]
}"
/>
<field
name="pec_destinatario"
Expand Down

0 comments on commit 3d8f29b

Please sign in to comment.