Forráskód Böngészése

Unterordner für Export-Formate

gc-server3 3 hónapja
szülő
commit
afdf948956

+ 93 - 0
gchr/export_format/honda.py

@@ -0,0 +1,93 @@
+import csv
+import xml.etree.ElementTree as ET
+from datetime import datetime
+from pathlib import Path
+from xml.dom import minidom
+
+from gchr.gchr_model import GchrExportConfig
+
+MODEL_CODE = {
+    "01": "0600",
+    "02": "0603",
+}
+
+
+def export_honda_csv(export_cfg: GchrExportConfig):
+    dealer_number = "HDE10020"
+    fiscal_year = export_cfg.current_year
+    ts = datetime.now().strftime("%m%d%Y;%H%M%S")
+    header = "\n".join(
+        f"{dealer_number};Dealernumber",
+        ";Honda Germany",
+        f"{export_cfg.current_year};Evaluation;{export_cfg.current_month}"
+        f"{fiscal_year};Fiscal-Year;{export_cfg.first_month}"
+        f";Timestamp;{ts}",
+    )
+
+    records = []
+    for row in sorted(export_cfg.bookkeep_records, key=lambda x: x["Account"] + x["CostAccountingString"]):
+        val = "{0:.2f}".format(row["CumulatedYear"] * -1)
+        if val[0] != "-":
+            val = "+" + val
+        records.append(
+            {
+                "tns:ProfitCenter": "00",
+                "tns:AccountKey": account_number(row),
+                "tns:AccountValue": val,
+            }
+        )
+
+    base_dir = Path(export_cfg.export_file).parent.parent.parent
+    add_values = f"{base_dir}\\data\\Manuelle_Eingabe_{export_cfg.current_year}-{export_cfg.current_month}.csv"
+    if Path(add_values).exists():
+        with open(add_values, "r", encoding="latin-1") as frh:
+            csv_frh = csv.DictReader(frh, delimiter=";")
+            for row in csv_frh:
+                records.append(
+                    {
+                        "tns:ProfitCenter": "00",
+                        "tns:AccountKey": row["Kontonummer"][1:],
+                        "tns:AccountValue": "+{0:.2f}".format(float(row["Akt.Monat"].replace(",", "."))),
+                    }
+                )
+
+    nsmap = {"xmlns:tns": "http://xmldefs.volkswagenag.com/Retail/AccountBalanceDTS/V1"}
+    root = ET.Element("tns:ShowAccountBalance", nsmap)
+    root = dict_to_xml(root, header)
+
+    record_list = ET.SubElement(root, "tns:Accounts")
+    for r in records:
+        dict_to_xml(ET.SubElement(record_list, "tns:Account"), r)
+
+    raw_xml = ET.tostring(root, encoding="latin-1")
+    with open(export_cfg.export_file, "w", encoding="latin-1") as fwh:
+        fwh.write(minidom.parseString(raw_xml).toprettyxml(indent="  "))
+
+
+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
+
+
+def account_number(row: dict[str, str]) -> str:
+    res = {
+        "Brand": row["Make"],
+        "ModelCode": MODEL_CODE.get(row["Make"], "0000"),
+        "Account": row["Account"],
+        "CostCentre": row["Origin"],
+        "TradeChannel": row["SalesChannel"],
+        "CostUnit": row["CostCarrier"],
+        "Location": row["Site"],
+        "TaxCode": "000",
+    }
+    return "".join(res.values())

+ 0 - 0
gchr/gchr_export_skr51.py → gchr/export_format/skr51.py


+ 4 - 7
gchr/gchr_export_volkswagen.py → gchr/export_format/volkswagen.py

@@ -50,13 +50,10 @@ def export_volkswagen_xml(export_cfg: GchrExportConfig):
             }
         )
 
-    add_values = (
-        Path(export_cfg.export_file).parent.parent.parent
-        / "data"
-        / f"Manuelle_Eingabe_{export_cfg.current_year}-{export_cfg.current_month}.csv"
-    )
-    if add_values.exists():
-        with add_values.open("r", encoding="latin-1") as frh:
+    base_dir = Path(export_cfg.export_file).parent.parent.parent
+    add_values = f"{base_dir}\\data\\Manuelle_Eingabe_{export_cfg.current_year}-{export_cfg.current_month}.csv"
+    if Path(add_values).exists():
+        with open(add_values, "r", encoding="latin-1") as frh:
             csv_frh = csv.DictReader(frh, delimiter=";")
             for row in csv_frh:
                 records.append(

+ 5 - 3
gchr/gchr_export.py

@@ -1,21 +1,23 @@
 from collections.abc import Callable
 from enum import StrEnum, auto
 
-from gchr.gchr_export_skr51 import export_skr51_xml
-from gchr.gchr_export_volkswagen import export_volkswagen_xml
+from gchr.export_format.honda import export_honda_csv
+from gchr.export_format.skr51 import export_skr51_xml
+from gchr.export_format.volkswagen import export_volkswagen_xml
 from gchr.gchr_model import GchrExportConfig
 
 
 class GchrExportFormat(StrEnum):
+    Honda = auto()
     SKR51 = auto()
     Volkswagen = auto()
-    Opel = auto()
 
 
 GchrExportFn = Callable[[GchrExportConfig], None]
 
 
 EXPORT_FN: dict[GchrExportFormat, GchrExportFn] = {
+    GchrExportFormat.Honda: export_honda_csv,
     GchrExportFormat.SKR51: export_skr51_xml,
     GchrExportFormat.Volkswagen: export_volkswagen_xml,
 }