# -*- coding: utf-8 -*-
import os.path
import sys
from decimal import Decimal
# Search for included submodule python-drafthorse
atoms = os.path.abspath(os.path.dirname(__file__)).split('/')
dir = ''
while atoms:
candidate = os.path.join('/'.join(atoms), 'external/python-drafthorse')
if os.path.exists(candidate):
dir = candidate
break
atoms = atoms[:-1]
sys.path.insert(0, dir)
from drafthorse.models.document import Document
from drafthorse.models.accounting import ApplicableTradeTax
from drafthorse.models.tradelines import LineItem
from .InvoiceObjects import InvoiceTable, InvoiceText, RECHNUNG, GUTSCHRIFT, KORREKTUR, \
VAT_REGULAR, VAT_KLEINUNTERNEHMER, VAT_INNERGEM, PAYMENT_UEBERWEISUNG, PAYMENT_LASTSCHRIFT, UNITS
from drafthorse.models.party import TaxRegistration, URIUniversalCommunication
from drafthorse.models.payment import PaymentTerms
from drafthorse.models.note import IncludedNote
from drafthorse.models import NS_QDT
from drafthorse.pdf import attach_xml
import re
def InvoiceToXML(invoice):
doc = Document()
doc.context.guideline_parameter.id = "urn:cen.eu:en16931:2017"
if invoice.leitweg_id:
doc.context.guideline_parameter.id = "urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_3.0"
# Standardwert für XRechnung 3.0.1
doc.context.business_parameter.id = "urn:fdc:peppol.eu:2017:poacc:billing:01:1.0"
doc.header.id = invoice.id
# Typecodes:
# 380: Handelsrechnungen
# 381: Gutschrift
# 384: Korrekturrechnung
# 389: Eigenrechnung (vom Käufer im Namen des Lieferanten erstellt).
# 261: Selbstverfasste Gutschrift.
# 386: Vorauszahlungsrechnung
# 326: Teilrechnung
# 751: Rechnungsinformation - KEINE RECHNUNG
if invoice.type == RECHNUNG:
doc.header.type_code = "380"
elif invoice.type == GUTSCHRIFT:
doc.header.type_code = "381"