gchr_export_volkswagen.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import csv
  2. import xml.etree.ElementTree as ET
  3. from pathlib import Path
  4. from xml.dom import minidom
  5. from gchr.gchr_model import GchrExportConfig
  6. # <summary>
  7. # V = Volkswagen,
  8. # A = Audi,
  9. # S = Seat,
  10. # C = Skoda,
  11. # E = Bentley,
  12. # L = Lamborghini
  13. # </summary>
  14. # internal enum Hauptmarke { V, A, S, C, E, L };
  15. MODEL_CODE = {
  16. "01": "0600",
  17. "02": "0603",
  18. }
  19. def export_volkswagen_xml(export_cfg: GchrExportConfig):
  20. header = {
  21. "tns:PartnerKey": {
  22. "tns:Country": "DEU",
  23. "tns:Brand": "V",
  24. "tns:PartnerNumber": "21996",
  25. },
  26. "tns:IsCumulative": "true",
  27. "tns:AccountingDate": {
  28. "tns:AccountingMonth": export_cfg.current_month,
  29. "tns:AccountingYear": export_cfg.current_year,
  30. },
  31. "tns:Currency": "EUR",
  32. "tns:Level": "1",
  33. }
  34. records = []
  35. for row in sorted(export_cfg.bookkeep_records, key=lambda x: x["Account"] + x["CostAccountingString"]):
  36. val = "{0:.2f}".format(row["CumulatedYear"] * -1)
  37. if val[0] != "-":
  38. val = "+" + val
  39. records.append(
  40. {
  41. "tns:ProfitCenter": "00",
  42. "tns:AccountKey": account_number(row),
  43. "tns:AccountValue": val,
  44. }
  45. )
  46. add_values = (
  47. Path(export_cfg.export_file).parent.parent.parent
  48. / "data"
  49. / f"Manuelle_Eingabe_{export_cfg.current_year}-{export_cfg.current_month}.csv"
  50. )
  51. if add_values.exists():
  52. with add_values.open("r", encoding="latin-1") as frh:
  53. csv_frh = csv.DictReader(frh, delimiter=";")
  54. for row in csv_frh:
  55. records.append(
  56. {
  57. "tns:ProfitCenter": "00",
  58. "tns:AccountKey": row["Kontonummer"][1:],
  59. "tns:AccountValue": "+{0:.2f}".format(float(row["Akt.Monat"].replace(",", "."))),
  60. }
  61. )
  62. nsmap = {"xmlns:tns": "http://xmldefs.volkswagenag.com/Retail/AccountBalanceDTS/V1"}
  63. root = ET.Element("tns:ShowAccountBalance", nsmap)
  64. root = dict_to_xml(root, header)
  65. record_list = ET.SubElement(root, "tns:Accounts")
  66. for r in records:
  67. dict_to_xml(ET.SubElement(record_list, "tns:Account"), r)
  68. raw_xml = ET.tostring(root, encoding="latin-1")
  69. with open(export_cfg.export_file, "w", encoding="latin-1") as fwh:
  70. fwh.write(minidom.parseString(raw_xml).toprettyxml(indent=" "))
  71. def dict_to_xml(root: ET.Element, subtree: dict):
  72. if isinstance(subtree, list):
  73. for item in subtree:
  74. dict_to_xml(root, item)
  75. return root
  76. for key, value in subtree.items():
  77. e = ET.SubElement(root, key)
  78. if isinstance(value, dict):
  79. dict_to_xml(e, value)
  80. else:
  81. e.text = str(value)
  82. return root
  83. def account_number(row: dict[str, str]) -> str:
  84. res = {
  85. "Brand": row["Make"],
  86. "ModelCode": MODEL_CODE.get(row["Make"], "0000"),
  87. "Account": row["Account"],
  88. "CostCentre": row["Origin"],
  89. "TradeChannel": row["SalesChannel"],
  90. "CostUnit": row["CostCarrier"],
  91. "Location": row["Site"],
  92. "TaxCode": "000",
  93. }
  94. return "".join(res.values())