Fehlendenden Namespace QDT hart im XML eintragen
Bernd Wurst

Bernd Wurst commited on 2024-11-20 07:37:50
Zeige 1 geänderte Dateien mit 17 Einfügungen und 1 Löschungen.

... ...
@@ -22,7 +22,9 @@ from .InvoiceObjects import InvoiceTable, InvoiceText, RECHNUNG, GUTSCHRIFT, KOR
22 22
 from drafthorse.models.party import TaxRegistration, URIUniversalCommunication
23 23
 from drafthorse.models.payment import PaymentTerms
24 24
 from drafthorse.models.note import IncludedNote
25
+from drafthorse.models import NS_QDT
25 26
 from drafthorse.pdf import attach_xml
27
+import re
26 28
 
27 29
 
28 30
 def InvoiceToXML(invoice):
... ...
@@ -311,5 +313,19 @@ def InvoiceToXML(invoice):
311 313
     doc.trade.settlement.monetary_summation.due_amount = Decimal(f"{rest:.2f}")
312 314
 
313 315
     # Generate XML file
314
-    xml = doc.serialize(schema="FACTUR-X_EXTENDED")
316
+    xml = add_namespace(doc.serialize(schema="FACTUR-X_EXTENDED"))
315 317
     return xml
318
+
319
+def add_namespace(xml):
320
+    """insert xmlns:qdt if it is not in namespaces"""
321
+    decoded = xml.decode('utf-8')
322
+    searchstr = re.search(r'<rsm:CrossIndustryInvoice(.*)>',
323
+                          decoded).group()
324
+    nsmap = searchstr.split(' ')
325
+    _QDT = 'xmlns:qdt='
326
+    QDT = _QDT + '\"' + NS_QDT + '\"'
327
+    if QDT not in nsmap:
328
+        nsmap.insert(1, QDT)
329
+        decoded = decoded.replace(searchstr, ' '.join(nsmap))
330
+
331
+    return decoded.encode('utf-8')
316 332