Skip to content

Commit 3a52536

Browse files
committed
[IMP] l10n_it_fatturapa_in: intermediary contact creation depending by vat
1 parent 45bbecf commit 3a52536

File tree

8 files changed

+296
-70
lines changed

8 files changed

+296
-70
lines changed

l10n_it_fatturapa_in/__manifest__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"views/partner_view.xml",
2626
"wizard/wizard_import_fatturapa_view.xml",
2727
"wizard/link_to_existing_invoice.xml",
28+
"wizard/wizard_check_intermediary_vat.xml",
2829
"views/company_view.xml",
2930
"security/ir.model.access.csv",
3031
"security/rules.xml",

l10n_it_fatturapa_in/security/ir.model.access.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ access_einvoice_line_other_data,access_einvoice_line_other_data,model_einvoice_l
66
access_wizard_import_fatturapa,access_wizard_import_fatturapa,model_wizard_import_fatturapa,account.group_account_invoice,1,1,1,1
77
access_wizard_link_to_invoice,access_wizard_link_to_invoice,model_wizard_link_to_invoice,account.group_account_invoice,1,1,1,1
88
access_wizard_link_to_invoice_line,access_wizard_link_to_invoice_line,model_wizard_link_to_invoice_line,account.group_account_invoice,1,1,1,1
9+
access_wizard_check_intermediary_all,access_wizard_check_intermediary_all,model_wizard_check_intermediary,account.group_account_invoice,1,1,1,1
10+
access_wizard_check_intermediary_all_line,access_wizard_check_intermediary_all_line,model_wizard_check_intermediary_line,account.group_account_invoice,1,1,1,1
911
fatturapa_in_decimal_precision_access_manager,fatturapa_in_decimal_precision_access_manager,base.model_decimal_precision,account.group_account_manager,1,1,0,0
1012
fatturapa_in_decimal_precision_access_billing,fatturapa_in_decimal_precision_access_billing,base.model_decimal_precision,account.group_account_invoice,1,1,0,0
1113
fatturapa_in_mail_followers_access_manager,fatturapa_in_mail_followers_access_manager,mail.model_mail_followers,account.group_account_manager,1,1,0,0

l10n_it_fatturapa_in/tests/fatturapa_common.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,51 @@ def create_attachment(self, name, file_name, module_name=None):
272272
attach = attach_form.save()
273273
return attach
274274

275+
def open_wizard(self, test, name):
276+
data = None
277+
res = self.run_wizard(test, name)
278+
279+
# Case where the confirmation wizard is shown
280+
if res.get("res_model") == "wizard.check.intermediary":
281+
282+
# fatturaPA can contains more invoices (intermediaries) with same header and
283+
# in this case we have only one line
284+
line = self.env["wizard.check.intermediary"].browse(res["res_id"]).line_ids
285+
286+
invoice_id = line.invoice_id
287+
name = line.name
288+
vat = line.vat
289+
wizard_id = line.wizard_id
290+
291+
# Check that the wizard was opened with the correct values
292+
self.assertTrue(invoice_id, "invoice_id not present in context")
293+
294+
# Simulate the wizard
295+
wizard = self.env["wizard.check.intermediary"].create(
296+
{
297+
"line_ids": [
298+
(
299+
0,
300+
0,
301+
{
302+
"invoice_id": invoice_id.id,
303+
"name": name,
304+
"vat": vat,
305+
"wizard_id": wizard_id,
306+
},
307+
)
308+
],
309+
"invoice_ids": [(6, 0, [invoice_id.id])],
310+
}
311+
)
312+
313+
# Simulate user confirmation
314+
data = wizard.action_confirm()
315+
return data, invoice_id
316+
317+
else:
318+
return res, None
319+
275320
def run_wizard(
276321
self, name, file_name, mode="import", wiz_values=None, module_name=None
277322
):

l10n_it_fatturapa_in/tests/test_import_fatturapa_xml.py

Lines changed: 77 additions & 67 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
from . import wizard_import_fatturapa
2+
from . import wizard_check_intermediary_vat
23
from . import link_to_existing_invoice
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from odoo import fields, models
2+
3+
4+
class WizardCheckIntermediaryVat(models.TransientModel):
5+
_name = "wizard.check.intermediary"
6+
_description = "Intermediary VAT Check"
7+
8+
line_ids = fields.One2many(
9+
comodel_name="wizard.check.intermediary.line", inverse_name="wizard_id"
10+
)
11+
invoice_ids = fields.Many2many(comodel_name="account.move")
12+
13+
def action_confirm(self):
14+
for line in self.line_ids:
15+
partner = self.env["res.partner"].create(
16+
{
17+
"name": line.name,
18+
"vat": line.vat,
19+
}
20+
)
21+
22+
line.invoice_id.write({"intermediary": partner.id})
23+
24+
return {
25+
"view_type": "form",
26+
"name": "Electronic Bills",
27+
"view_mode": "tree,form",
28+
"res_model": "account.move",
29+
"type": "ir.actions.act_window",
30+
"domain": [("id", "in", self.invoice_ids.ids)],
31+
}
32+
33+
def action_cancel(self):
34+
return {
35+
"view_type": "form",
36+
"name": "Electronic Bills",
37+
"view_mode": "tree,form",
38+
"res_model": "account.move",
39+
"type": "ir.actions.act_window",
40+
"domain": [("id", "in", self.invoice_ids.ids)],
41+
}
42+
43+
44+
class WizardCheckIntermediaryVatLine(models.TransientModel):
45+
_name = "wizard.check.intermediary.line"
46+
_description = "Intermediary VAT Check Line"
47+
48+
invoice_id = fields.Many2one("account.move", string="Invoice", readonly=True)
49+
name = fields.Char()
50+
vat = fields.Char()
51+
wizard_id = fields.Many2one("wizard.check.intermediary")
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<odoo>
3+
<record id="view_check_intermediary_vat" model="ir.ui.view">
4+
<field name="name">check.intermediary.vat.form</field>
5+
<field name="model">wizard.check.intermediary</field>
6+
<field name="arch" type="xml">
7+
<form string="Verify Intermediary">
8+
<sheet>
9+
<group>
10+
<group>
11+
<p
12+
>These intermediaries do not exist on the system. Do you want to create them?</p>
13+
</group>
14+
<field name="line_ids" nolabel="1">
15+
<tree create="0">
16+
<field name="name" />
17+
<field name="vat" />
18+
<field name="invoice_id" />
19+
</tree>
20+
</field>
21+
</group>
22+
<footer>
23+
<button
24+
string="Confirm"
25+
type="object"
26+
name="action_confirm"
27+
class="btn-primary"
28+
/>
29+
<button
30+
string="No"
31+
type="object"
32+
name="action_cancel"
33+
class="btn-secondary"
34+
/>
35+
</footer>
36+
</sheet>
37+
</form>
38+
</field>
39+
</record>
40+
</odoo>

l10n_it_fatturapa_in/wizard/wizard_import_fatturapa.py

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,7 +1850,7 @@ def _get_invoice_partner_id(self, fatt):
18501850
partner_id = self.getCedPrest(cedentePrestatore)
18511851
return partner_id
18521852

1853-
def importFatturaPA(self):
1853+
def importFatturaPA(self): # noqa: C901
18541854
self.ensure_one()
18551855

18561856
(
@@ -1873,6 +1873,7 @@ def importFatturaPA(self):
18731873

18741874
new_invoices = []
18751875
# convert to dict in order to be able to modify context
1876+
new_intermediary_data = []
18761877
fatturapa_attachments = self._get_selected_records()
18771878
self.env.context = dict(self.env.context)
18781879
for fatturapa_attachment in fatturapa_attachments:
@@ -1924,8 +1925,55 @@ def importFatturaPA(self):
19241925
)
19251926
invoice.write({"tax_representative_id": tax_partner_id})
19261927
if Intermediary:
1927-
Intermediary_id = self.getPartnerBase(Intermediary.DatiAnagrafici)
1928-
invoice.write({"intermediary": Intermediary_id})
1928+
vat = False
1929+
name = False
1930+
if Intermediary.DatiAnagrafici.Anagrafica.Denominazione:
1931+
name = Intermediary.DatiAnagrafici.Anagrafica.Denominazione
1932+
else:
1933+
nome = Intermediary.DatiAnagrafici.Anagrafica.Nome or ""
1934+
cognome = Intermediary.DatiAnagrafici.Anagrafica.Cognome or ""
1935+
name = f"{nome} {cognome}".strip()
1936+
1937+
if Intermediary.DatiAnagrafici.IdFiscaleIVA:
1938+
id_paese = (
1939+
Intermediary.DatiAnagrafici.IdFiscaleIVA.IdPaese.upper()
1940+
)
1941+
id_codice = re.sub(
1942+
r"\W+",
1943+
"",
1944+
Intermediary.DatiAnagrafici.IdFiscaleIVA.IdCodice,
1945+
).upper()
1946+
# Format Italian VAT ID to always have 11 char
1947+
# to avoid validation error when creating the given partner
1948+
if id_paese == "IT" and not id_codice.startswith("IT"):
1949+
vat = "IT{}".format(id_codice.rjust(11, "0")[:11])
1950+
# XXX maybe San Marino needs special formatting too?
1951+
else:
1952+
vat = id_codice
1953+
1954+
vat_check = self.env["res.partner"].search(
1955+
[("vat", "=", vat), ("name", "=", name)]
1956+
)
1957+
1958+
if vat_check:
1959+
invoice.write({"intermediary": vat_check.id})
1960+
else:
1961+
new_intermediary_data.append(
1962+
{
1963+
"name": name,
1964+
"vat": vat,
1965+
"invoice_id": invoice.id,
1966+
}
1967+
)
1968+
else:
1969+
new_intermediary_data.append(
1970+
{
1971+
"name": name,
1972+
"vat": vat,
1973+
"invoice_id": invoice.id,
1974+
}
1975+
)
1976+
19291977
new_invoices.append(invoice.id)
19301978
self.check_invoice_amount(invoice, fattura)
19311979

@@ -1949,6 +1997,34 @@ def importFatturaPA(self):
19491997
discount_precision, original_discount_precision
19501998
)
19511999

2000+
if new_intermediary_data:
2001+
res = self.env["wizard.check.intermediary"].create(
2002+
{
2003+
"line_ids": [
2004+
(
2005+
0,
2006+
0,
2007+
{
2008+
"name": new_data["name"],
2009+
"vat": new_data["vat"],
2010+
"invoice_id": new_data["invoice_id"],
2011+
},
2012+
)
2013+
for new_data in new_intermediary_data
2014+
],
2015+
"invoice_ids": [(6, 0, new_invoices)],
2016+
}
2017+
)
2018+
2019+
return {
2020+
"type": "ir.actions.act_window",
2021+
"name": "Confirm action",
2022+
"res_model": "wizard.check.intermediary",
2023+
"res_id": res.id,
2024+
"view_mode": "form",
2025+
"target": "new",
2026+
}
2027+
19522028
return {
19532029
"view_type": "form",
19542030
"name": "Electronic Bills",

0 commit comments

Comments
 (0)