honda.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import csv
  2. from datetime import datetime
  3. from pathlib import Path
  4. from gchr.gchr_model import GchrExportConfig
  5. MAKE_CODE = {
  6. "08": "04",
  7. }
  8. SITE_CODE = {
  9. "07": "01",
  10. "04": "02",
  11. }
  12. def export_honda_csv(export_cfg: GchrExportConfig):
  13. dealer_number = "HDE10020"
  14. fiscal_year = export_cfg.current_year
  15. ts = datetime.now().strftime("%m%d%Y;%H%M%S")
  16. header = "\n".join(
  17. [
  18. f"{dealer_number};Dealernumber",
  19. ";Honda Germany",
  20. f"{export_cfg.current_year};Evaluation;{export_cfg.current_month}",
  21. f"{fiscal_year};Fiscal-Year;{export_cfg.first_month}",
  22. f";Timestamp;{ts}",
  23. ]
  24. )
  25. base_dir = Path(export_cfg.export_file).parent.parent.parent
  26. account_desc = f"{base_dir}\\data\\Kontenrahmen.csv"
  27. if Path(account_desc).exists():
  28. with open(account_desc, "r", encoding="latin-1") as frh:
  29. csv_frh = csv.DictReader(frh, delimiter=";")
  30. accounts = {row["Konto_Nr"]: row["Konto_Bezeichnung"] for row in csv_frh}
  31. records = []
  32. for row in sorted(export_cfg.bookkeep_records, key=lambda x: account_number(x)):
  33. records.append(
  34. [
  35. account_number(row),
  36. accounts.get(row["Account"], ""),
  37. "{0:.2f}".format(row["Period" + export_cfg.current_month]),
  38. "{0:.2f}".format(row["CumulatedYear"]),
  39. ]
  40. )
  41. add_values = f"{base_dir}\\data\\Manuelle_Eingabe_{export_cfg.current_year}-{export_cfg.current_month}.csv"
  42. if Path(add_values).exists():
  43. with open(add_values, "r", encoding="latin-1") as frh:
  44. csv_frh = csv.DictReader(frh, delimiter=";")
  45. for row in csv_frh:
  46. records.append(
  47. [
  48. row["Kontonummer"],
  49. "",
  50. "+{0:.2f}".format(float(row["Akt.Monat"].replace(",", "."))),
  51. "+{0:.2f}".format(float(row["Akt.Monat"].replace(",", "."))),
  52. ]
  53. )
  54. with open(export_cfg.export_file[:-4] + "_Honda.csv", "w", encoding="latin-1") as fwh:
  55. fwh.write(header + "\n")
  56. for row in records:
  57. fwh.write(";".join(row) + "\n")
  58. def account_number(row: dict[str, str]) -> str:
  59. res = {
  60. "Konto": row["Account"],
  61. "Marke": MAKE_CODE.get(row["Make"], "00"),
  62. "Betrieb": SITE_CODE.get(row["Site"], "00"),
  63. "Kostenstelle": row["Origin"],
  64. "Absatzkanal": row["SalesChannel"],
  65. "Kostenträger": row["CostCarrier"],
  66. }
  67. return "".join(res.values())