honda.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import csv
  2. import xml.etree.ElementTree as ET
  3. from datetime import datetime
  4. from pathlib import Path
  5. from xml.dom import minidom
  6. from gchr.gchr_model import GchrExportConfig
  7. MODEL_CODE = {
  8. "01": "0600",
  9. "02": "0603",
  10. }
  11. def export_honda_csv(export_cfg: GchrExportConfig):
  12. dealer_number = "HDE10020"
  13. fiscal_year = export_cfg.current_year
  14. ts = datetime.now().strftime("%m%d%Y;%H%M%S")
  15. header = "\n".join(
  16. f"{dealer_number};Dealernumber",
  17. ";Honda Germany",
  18. f"{export_cfg.current_year};Evaluation;{export_cfg.current_month}"
  19. f"{fiscal_year};Fiscal-Year;{export_cfg.first_month}"
  20. f";Timestamp;{ts}",
  21. )
  22. records = []
  23. for row in sorted(export_cfg.bookkeep_records, key=lambda x: x["Account"] + x["CostAccountingString"]):
  24. val = "{0:.2f}".format(row["CumulatedYear"] * -1)
  25. if val[0] != "-":
  26. val = "+" + val
  27. records.append(
  28. {
  29. "tns:ProfitCenter": "00",
  30. "tns:AccountKey": account_number(row),
  31. "tns:AccountValue": val,
  32. }
  33. )
  34. base_dir = Path(export_cfg.export_file).parent.parent.parent
  35. add_values = f"{base_dir}\\data\\Manuelle_Eingabe_{export_cfg.current_year}-{export_cfg.current_month}.csv"
  36. if Path(add_values).exists():
  37. with open(add_values, "r", encoding="latin-1") as frh:
  38. csv_frh = csv.DictReader(frh, delimiter=";")
  39. for row in csv_frh:
  40. records.append(
  41. {
  42. "tns:ProfitCenter": "00",
  43. "tns:AccountKey": row["Kontonummer"][1:],
  44. "tns:AccountValue": "+{0:.2f}".format(float(row["Akt.Monat"].replace(",", "."))),
  45. }
  46. )
  47. nsmap = {"xmlns:tns": "http://xmldefs.volkswagenag.com/Retail/AccountBalanceDTS/V1"}
  48. root = ET.Element("tns:ShowAccountBalance", nsmap)
  49. root = dict_to_xml(root, header)
  50. record_list = ET.SubElement(root, "tns:Accounts")
  51. for r in records:
  52. dict_to_xml(ET.SubElement(record_list, "tns:Account"), r)
  53. raw_xml = ET.tostring(root, encoding="latin-1")
  54. with open(export_cfg.export_file, "w", encoding="latin-1") as fwh:
  55. fwh.write(minidom.parseString(raw_xml).toprettyxml(indent=" "))
  56. def dict_to_xml(root: ET.Element, subtree: dict):
  57. if isinstance(subtree, list):
  58. for item in subtree:
  59. dict_to_xml(root, item)
  60. return root
  61. for key, value in subtree.items():
  62. e = ET.SubElement(root, key)
  63. if isinstance(value, dict):
  64. dict_to_xml(e, value)
  65. else:
  66. e.text = str(value)
  67. return root
  68. def account_number(row: dict[str, str]) -> str:
  69. res = {
  70. "Brand": row["Make"],
  71. "ModelCode": MODEL_CODE.get(row["Make"], "0000"),
  72. "Account": row["Account"],
  73. "CostCentre": row["Origin"],
  74. "TradeChannel": row["SalesChannel"],
  75. "CostUnit": row["CostCarrier"],
  76. "Location": row["Site"],
  77. "TaxCode": "000",
  78. }
  79. return "".join(res.values())