From 8c18b870ecc04314380fcdb6b61179de78e28703 Mon Sep 17 00:00:00 2001
From: Paolo Gatti <pgi@odoo.com>
Date: Tue, 8 Nov 2022 16:06:24 +0000
Subject: [PATCH] [FIX] l10n_it_edi: Non EU VAT fix

Non EU vendor bills are sent to the Tax Agency as self-invoices,
so the partner actually is the seller, but the same checks on the
VAT must apply as it was the buyer.

Task: https://www.odoo.com/web#id=3010849&model=project.task
opw-3010849

closes odoo/odoo#104779

Signed-off-by: Josse Colpaert <jco@odoo.com>
---
 addons/l10n_it_edi/data/invoice_it_template.xml | 10 +++++++++-
 addons/l10n_it_edi/models/account_edi_format.py |  4 ++--
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/addons/l10n_it_edi/data/invoice_it_template.xml b/addons/l10n_it_edi/data/invoice_it_template.xml
index 4d067c459546..25e7c9e53a29 100644
--- a/addons/l10n_it_edi/data/invoice_it_template.xml
+++ b/addons/l10n_it_edi/data/invoice_it_template.xml
@@ -58,10 +58,18 @@
         </DatiTrasmissione>
         <CedentePrestatore>
             <DatiAnagrafici>
-                <IdFiscaleIVA>
+                <IdFiscaleIVA t-if="seller.vat and in_eu(seller)">
                     <IdPaese t-esc="get_vat_country(seller.vat)"/>
                     <IdCodice t-esc="get_vat_number(seller.vat)"/>
                 </IdFiscaleIVA>
+                <IdFiscaleIVA t-if="seller.vat and not in_eu(seller)">
+                    <IdPaese t-esc="seller.country_id.code"/>
+                    <IdCodice t-esc="'OO99999999999'"/>
+                </IdFiscaleIVA>
+                <IdFiscaleIVA t-if="not seller.vat and seller.country_id.code != 'IT'">
+                    <IdPaese t-esc="seller.country_id.code"/>
+                    <IdCodice t-esc="'0000000'"/>
+                </IdFiscaleIVA>
                 <CodiceFiscale t-if="seller.l10n_it_codice_fiscale" t-esc="normalize_codice_fiscale(seller.l10n_it_codice_fiscale)"/>
                 <Anagrafica>
                     <Denominazione t-esc="format_alphanumeric(seller_partner.display_name[:80])"/>
diff --git a/addons/l10n_it_edi/models/account_edi_format.py b/addons/l10n_it_edi/models/account_edi_format.py
index 059ab798d349..c07b18da2689 100644
--- a/addons/l10n_it_edi/models/account_edi_format.py
+++ b/addons/l10n_it_edi/models/account_edi_format.py
@@ -88,9 +88,9 @@ class AccountEdiFormat(models.Model):
             errors.append(_("%s must have a country", seller.display_name))
 
         # <1.1.1.2>
-        if not seller.vat:
+        if not invoice.company_id.vat:
             errors.append(_("%s must have a VAT number", seller.display_name))
-        elif len(seller.vat) > 30:
+        if seller.vat and len(seller.vat) > 30:
             errors.append(_("The maximum length for VAT number is 30. %s have a VAT number too long: %s.", seller.display_name, seller.vat))
 
         # <1.2.1.2>
-- 
GitLab