12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- import csv
- from datetime import datetime
- from pathlib import Path
- from gchr.gchr_model import GchrExportConfig
- MAKE_CODE = {
- "08": "04",
- }
- SITE_CODE = {
- "07": "01",
- "04": "02",
- }
- 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}",
- ]
- )
- base_dir = Path(export_cfg.export_file).parent.parent.parent
- account_desc = f"{base_dir}\\data\\Kontenrahmen.csv"
- if Path(account_desc).exists():
- with open(account_desc, "r", encoding="latin-1") as frh:
- csv_frh = csv.DictReader(frh, delimiter=";")
- accounts = {row["Konto_Nr"]: row["Konto_Bezeichnung"] for row in csv_frh}
- records = []
- for row in sorted(export_cfg.bookkeep_records, key=lambda x: account_number(x)):
- records.append(
- [
- account_number(row),
- accounts.get(row["Account"], ""),
- "{0:.2f}".format(row["Period" + export_cfg.current_month]),
- "{0:.2f}".format(row["CumulatedYear"]),
- ]
- )
- 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(
- [
- row["Kontonummer"],
- "",
- "+{0:.2f}".format(float(row["Akt.Monat"].replace(",", "."))),
- "+{0:.2f}".format(float(row["Akt.Monat"].replace(",", "."))),
- ]
- )
- with open(export_cfg.export_file[:-4] + "_Honda.csv", "w", encoding="latin-1") as fwh:
- fwh.write(header + "\n")
- for row in records:
- fwh.write(";".join(row) + "\n")
- def account_number(row: dict[str, str]) -> str:
- res = {
- "Konto": row["Account"],
- "Marke": MAKE_CODE.get(row["Make"], "00"),
- "Betrieb": SITE_CODE.get(row["Site"], "00"),
- "Kostenstelle": row["Origin"],
- "Absatzkanal": row["SalesChannel"],
- "Kostenträger": row["CostCarrier"],
- }
- return "".join(res.values())
|