from invoice_base import *
import xml.etree.ElementTree as ET
import re
class Invoice(BaseInvoice):
def filter_elements_by_country(self, xml_string):
root = ET.fromstring(xml_string)
ET.register_namespace("ubl", "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2")
ET.register_namespace("cac", "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")
ET.register_namespace("cbc", "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")
ET.register_namespace("cec", "urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2")
ET.register_namespace("vrbl", "urn:vertexinc:vrbl:ExtensionComponent:1")
for parent in root.iter():
for child in list(parent):
countries = child.attrib.get("countries")
# No include attribute → keep element as-is
if countries is None:
continue
values = [v.strip() for v in countries.split(",")]
# Remove element if include does not contains the country
if 'not' in values:
if self.country() in values:
parent.remove(child)
else:
# Otherwise, remove only the include attribute
del child.attrib["countries"]
else:
if self.country() not in values:
parent.remove(child)
else:
# Otherwise, remove only the include attribute
del child.attrib["countries"]
return ET.tostring(root, encoding="unicode", xml_declaration=True)
def __str__(self):
self.totals.get_total_lines_netsalesprice()
taxestotal = ''
for x in self.taxes:
taxestotal += str(x)
allarticles = ''
for a in self.articles:
allarticles += str(a)
alltenders = ''
paymentTermText = ''
if ONLY_ONE_TENDER:
if len(self.tenders)>1:
paymentTermText = 'with '
for t in self.tenders:
paymentTermText += f"{t.description}:{t.amount:.2f} "
alltenders += str(self.tenders[0])
else:
for t in self.tenders:
alltenders += str(t)
xml = f'''
VRBL:HR:{'P1' if self.totals.grandAmount>0 else 'P9'}
{self.seller.taxID}
{self.seller.vrbl_receiver}
true
urn:vertexinc:vrbl:billing:1
urn:vertexinc:vrbl:billing:1#Invoice#VRBL-Invoice-HR-CIUS-1p0
urn:vertexinc:vrbl:billing:1
{self.nr}
false
{self.receipt.year}-{self.receipt.month}-{self.receipt.day}
{self.receipt.time_str}
{self.receipt.year}-{self.receipt.month}-{self.receipt.day}
380
{self.currency()}
{self.receipt.store}{self.receipt.year}{self.receipt.month}{self.receipt.day}{self.receipt.till}{self.receipt.seq.zfill(6)}
{ self.header.fiscalseq }
{ self.header.get_taxref('JIR') }
JIR
{ self.header.get_taxref('ZKI') }
ZKI
{self.seller}
{self.buyer}
{alltenders}
{self.receipt.year}-{self.receipt.month}-{self.receipt.day}. Settled in full {paymentTermText}
{taxestotal}
{self.totals}
{allarticles}
'''
return self.filter_elements_by_country(xml)
return xml
class Discount(BaseDiscount):
def __str__(self):
return f'''
false
95
{self.promotionID}
{self.net_amount():.2f}
'''
class Tax(BaseTax):
spaces = 'tax'
def __str__(self):
return f"{self.spaces},{self.typesubtype},{self.code},{self.amount},{self.percentage}%,{self.translated_group()},{self.taxableamount}\n"
class TotalTax(Tax):
def __str__(self):
zerovat = ''
if abs(self.amount)<0.001:
zerovat = "Not subject to VAT"
return f'''
{self.amount:.2f}
{self.taxableamount:.2f}
{self.amount:.2f}
{self.translated_group()}
{self.percentage}
{zerovat}
VAT
'''
class Header(BaseHeader):
pass
class Totals(BaseTotals):
def __str__(self):
return f'''
{self.totalLinesAmount:.2f}
{self.netAmount:.2f}
{(self.grandAmount - self.roundingAmount):.2f}
{self.grandAmount:.2f}
{self.roundingAmount:.2f}
{self.grandAmount:.2f}
0.00
'''
class Receipt(BaseReceipt):
pass
class Buyer(BaseBuyer):
def id_schema(self, element):
return vrbl.translate(element, type='scheme', country=self._country())
def __str__(self):
phone = f'{self.phone}\n' if len(self.phone)>0 else ''
email = f'{self.email}\n' if len(self.email)>0 else ''
return f'''
{self.orgid}
{self.orgid}
{self.name}
{self.street1}
{self.street2}
{self.city}
{self.zip}
{self.country}
{self.country}{self.taxID}
VAT
{self.name}
{self.orgid}
{self.name}
{phone}
{email}
'''
class Delivery():
pass
class Seller(BaseSeller):
def __str__(self):
return f'''
{self.orgid}
{self.orgid}
{self.orgid}
{self.orgid}
{self.name}
{self.street1}
{self.street2}
{self.city}
{self.zip}
{self.country}
{self.taxID}
VAT
{self.name}
{self.legalid}
Customer service
'''
class Tender(BaseTender):
def translate(self, code):
return '54' # credit card
def __str__(self):
return f'''
{self.translated_type()}
'''
class Article(BaseArticle):
def __str__(self):
discounts = ''
for d in self.discounts:
discounts += str(d)
return f'''
{int(self.sequence) + 1}
{self.line_uuid()}
{self.description}
{'-' if self.returnflag else ''}{self.quantity}
{self.netSalesPrice():.2f}
{self.tax.amount:.2f}
{discounts}
{self.description}
{self.description}
{self.itemid}
{self.upc}
{self.upc}
47.71.00
{self.tax.translated_group()}
{self.tax.percentage}
VAT
{self.netBasePrice():.2f}
{self.quantity}
{self.netBasePrice():.2f}
{self.netSalesPrice():.2f}
'''