volkswagen.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. "05": "0600",
  17. "06": "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. factor = 1 if row["Kontoart"] == "1" else -1
  37. val = "{0:.2f}".format(row["CumulatedYear"] * factor)
  38. if val[0] != "-":
  39. val = "+" + val
  40. records.append(
  41. {
  42. "tns:ProfitCenter": "00",
  43. "tns:AccountKey": account_number(row),
  44. "tns:AccountValue": val,
  45. }
  46. )
  47. base_dir = Path(export_cfg.export_file).parent.parent.parent
  48. add_values = f"{base_dir}\\data\\Manuelle_Eingabe_{export_cfg.current_year}-{export_cfg.current_month}.csv"
  49. if Path(add_values).exists():
  50. with open(add_values, "r", encoding="latin-1") as frh:
  51. csv_frh = csv.DictReader(frh, delimiter=";")
  52. for row in csv_frh:
  53. records.append(
  54. {
  55. "tns:ProfitCenter": "00",
  56. "tns:AccountKey": row["Kontonummer"][1:],
  57. "tns:AccountValue": "+{0:.2f}".format(float(row["Akt.Monat"].replace(",", "."))),
  58. }
  59. )
  60. nsmap = {"xmlns:tns": "http://xmldefs.volkswagenag.com/Retail/AccountBalanceDTS/V1"}
  61. root = ET.Element("tns:ShowAccountBalance", nsmap)
  62. root = dict_to_xml(root, header)
  63. record_list = ET.SubElement(root, "tns:Accounts")
  64. for r in records:
  65. dict_to_xml(ET.SubElement(record_list, "tns:Account"), r)
  66. raw_xml = ET.tostring(root, encoding="latin-1")
  67. with open(export_cfg.export_file, "w", encoding="latin-1") as fwh:
  68. fwh.write(minidom.parseString(raw_xml).toprettyxml(indent=" "))
  69. def dict_to_xml(root: ET.Element, subtree: dict):
  70. if isinstance(subtree, list):
  71. for item in subtree:
  72. dict_to_xml(root, item)
  73. return root
  74. for key, value in subtree.items():
  75. e = ET.SubElement(root, key)
  76. if isinstance(value, dict):
  77. dict_to_xml(e, value)
  78. else:
  79. e.text = str(value)
  80. return root
  81. def account_number(row: dict[str, str]) -> str:
  82. res = {
  83. "Brand": row["Make"], # if row["Make"] in MODEL_CODE else "99",
  84. "ModelCode": MODEL_CODE.get(row["Make"], "0000"),
  85. "Account": row["Account"],
  86. "CostCentre": row["Origin"],
  87. "TradeChannel": row["SalesChannel"],
  88. "CostUnit": row["CostCarrier"],
  89. "Location": row["Site"],
  90. "TaxCode": "000",
  91. }
  92. return "".join(res.values())