gchr_export_volkswagen.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import xml.etree.ElementTree as ET
  2. from xml.dom import minidom
  3. from gchr.gchr_model import GchrExportConfig
  4. def export_volkswagen_xml(export_cfg: GchrExportConfig):
  5. header = {
  6. "PartnerKey": {
  7. "Country": "DEU",
  8. "Brand": "V",
  9. "PartnerNumber": "21996",
  10. },
  11. "IsCumulative": "true",
  12. "AccountingDate": {
  13. "AccountingMonth": "09",
  14. "AccountingYear": "2024",
  15. },
  16. "Currency": "EUR",
  17. "Level": "1",
  18. }
  19. record_elements = {"ProfitCenter": "00", "AccountKey": "010600002700000007000", "AccountValue": "+11.00"}
  20. ET.register_namespace("tns", "http://xmldefs.volkswagenag.com/Retail/AccountBalanceDTS/V1")
  21. root = ET.Element("tns:ShowAccountBalance")
  22. root = dict_to_xml(root, header)
  23. record_list = ET.SubElement(root, "Accounts")
  24. for row in export_cfg.bookkeep_records:
  25. record = ET.SubElement(record_list, "Account")
  26. for e, v in record_elements.items():
  27. child = ET.SubElement(record, e)
  28. field = row.get(e, v)
  29. if str(field) == "nan":
  30. field = "0"
  31. elif type(field) is float:
  32. field = "{:.2f}".format(field * -1)
  33. child.text = str(field)
  34. with open(export_cfg.export_file, "w", encoding="latin-1") as fwh:
  35. fwh.write(
  36. minidom.parseString(ET.tostring(root, encoding="ISO-8859-1")).toprettyxml(indent=" ", encoding="latin-1")
  37. )
  38. def dict_to_xml(root: ET.Element, subtree: dict):
  39. # if isinstance(subtree, list):
  40. # for item in subtree:
  41. # dict_to_xml(root, item)
  42. # return root
  43. for key, value in subtree.items():
  44. e = ET.SubElement(root, key)
  45. if isinstance(value, dict):
  46. dict_to_xml(e, value)
  47. else:
  48. e.text = str(value)
  49. return root