import xml.etree.ElementTree as ET from xml.dom import minidom from gchr.gchr_model import GchrExportConfig def export_volkswagen_xml(export_cfg: GchrExportConfig): header = { "PartnerKey": { "Country": "DEU", "Brand": "V", "PartnerNumber": "21996", }, "IsCumulative": "true", "AccountingDate": { "AccountingMonth": "09", "AccountingYear": "2024", }, "Currency": "EUR", "Level": "1", } record_elements = {"ProfitCenter": "00", "AccountKey": "010600002700000007000", "AccountValue": "+11.00"} ET.register_namespace("tns", "http://xmldefs.volkswagenag.com/Retail/AccountBalanceDTS/V1") root = ET.Element("tns:ShowAccountBalance") root = dict_to_xml(root, header) record_list = ET.SubElement(root, "Accounts") for row in export_cfg.bookkeep_records: record = ET.SubElement(record_list, "Account") for e, v in record_elements.items(): child = ET.SubElement(record, e) field = row.get(e, v) if str(field) == "nan": field = "0" elif type(field) is float: field = "{:.2f}".format(field * -1) child.text = str(field) with open(export_cfg.export_file, "w", encoding="latin-1") as fwh: fwh.write( minidom.parseString(ET.tostring(root, encoding="ISO-8859-1")).toprettyxml(indent=" ", encoding="latin-1") ) def dict_to_xml(root: ET.Element, subtree: dict): # if isinstance(subtree, list): # for item in subtree: # dict_to_xml(root, item) # return root for key, value in subtree.items(): e = ET.SubElement(root, key) if isinstance(value, dict): dict_to_xml(e, value) else: e.text = str(value) return root