Skip to content

Commit

Permalink
DES: use lxml objectify to improve readability of XML generation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexis-via committed Nov 22, 2023
1 parent 6e88a15 commit 9d31c41
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions l10n_fr_intrastat_service/models/intrastat_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import logging

from dateutil.relativedelta import relativedelta
from lxml import etree
from lxml import etree, objectify
from stdnum.vatin import is_valid

from odoo import _, api, fields, models
Expand Down Expand Up @@ -292,32 +292,24 @@ def _generate_des_xml_root(self):
raise UserError(
_("Missing VAT number on company '%s'.") % self.company_id.display_name
)
my_company_vat = self.company_id.partner_id.vat.replace(" ", "")
my_company_vat = self.company_id.partner_id.vat

# Tech spec of XML export are available here :
# https://www.douane.gouv.fr/sites/default/files/uploads/files/2020-10/ManuelDesXML.pdf # noqa
root = etree.Element("fichier_des")
decl = etree.SubElement(root, "declaration_des")
num_des = etree.SubElement(decl, "num_des")
num_des.text = self.year_month.replace("-", "")
num_tva = etree.SubElement(decl, "num_tvaFr")
num_tva.text = my_company_vat
mois_des = etree.SubElement(decl, "mois_des")
mois_des.text = self.year_month[5:7]
# month 2 digits
an_des = etree.SubElement(decl, "an_des")
an_des.text = self.year_month[:4]
root = objectify.Element("fichier_des")
decl = objectify.SubElement(root, "declaration_des")
decl.num_des = self.year_month.replace("-", "")
decl.num_tvaFr = my_company_vat
decl.mois_des = str(self.start_date.month).zfill(2)
decl.an_des = str(self.start_date.year)
line = 0
# we now go through each service line
for sline in self.declaration_line_ids:
line += 1 # increment line number
ligne_des = etree.SubElement(decl, "ligne_des")
numlin_des = etree.SubElement(ligne_des, "numlin_des")
numlin_des.text = str(line)
valeur = etree.SubElement(ligne_des, "valeur")
valeur.text = str(sline.amount_company_currency)
partner_des = etree.SubElement(ligne_des, "partner_des")
vat = sline.partner_vat.replace(" ", "")
ligne_des = objectify.SubElement(decl, "ligne_des")
ligne_des.numlin_des = str(line)
ligne_des.valeur = str(sline.amount_company_currency)
vat = sline.partner_vat
if not vat:
raise UserError(
_("Missing VAT number on partner '%s'.")
Expand All @@ -332,7 +324,7 @@ def _generate_des_xml_root(self):
)
% vat
)
partner_des.text = vat
ligne_des.partner_des = vat
return root

def generate_xml(self):
Expand All @@ -341,6 +333,8 @@ def generate_xml(self):
if not self.declaration_line_ids:
return
root = self._generate_des_xml_root()
objectify.deannotate(root, xsi_nil=True)
etree.cleanup_namespaces(root)
xml_bytes = etree.tostring(
root, pretty_print=True, encoding="UTF-8", xml_declaration=True
)
Expand Down

0 comments on commit 9d31c41

Please sign in to comment.