plan_export.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. from pathlib import Path
  2. from plan_values2 import VALUES2_HEADER
  3. import pandas as pd
  4. import numpy as np
  5. base_dir = Path(__file__).parent.parent.resolve() / "export"
  6. id_header = ["Ebene" + str(i) for i in range(1, 11)]
  7. # values2_header = ['VJ', 'AJ', 'FC', 'Plan_ori', 'Plan_Prozent', 'Stk', 'VAK', 'BE_Prozent', 'Plan_VJ',
  8. # 'Plan_Stk_VJ', 'Plan', 'Jan', 'Feb', 'Mar', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt',
  9. # 'Nov', 'Dez', 'Periode13']
  10. season_header = ["Jan", "Feb", "Mar", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"]
  11. info_header = ["text", "costcenter", "department"]
  12. header = info_header + id_header + VALUES2_HEADER
  13. season_export_header = info_header + season_header
  14. source_header = ["department", "text", "costcenter", "Ebene1", "Plan", "Periode13"]
  15. export_header = [
  16. "Betrieb Nr",
  17. "Zeile mit Bez",
  18. "Bereich",
  19. "Vstufe 1",
  20. "Gesamt",
  21. "Periode13",
  22. ] # 'Version', 'Konto', 'Jahr']
  23. def expand(df, header, values_label):
  24. for i, key in enumerate(header):
  25. df[key] = df[values_label].str[i]
  26. return df
  27. def apply_season(df):
  28. df["Saison"] = df["Ebene1"].str.contains("Umsatzerlöse|Materialaufwand|Verkaufsabh. Kosten")
  29. df["saison_sum"] = df[season_header].sum(axis=1)
  30. for i, key in enumerate(season_header):
  31. df["temp"] = np.where(
  32. (df["Saison"]) & (df[key + "_2"] != 8.3333), df["Plan"] * df[key + "_2"] / 100, df["Plan"] / 12
  33. )
  34. df[key] = np.where(df["saison_sum"] == 0, df["temp"], df[key] * df["Minus1"])
  35. df["Dez"] = df["Plan"] - df[season_header].sum(axis=1) + df["Dez"]
  36. return df
  37. def data_cleansing(filename):
  38. df = pd.read_json(filename)
  39. df["values2"] = df["values2"].apply(lambda v: list(v.items()))
  40. df = df.explode("values2")
  41. df["department"], df["values2"] = zip(*df["values2"])
  42. df["id"] = df["id"].str.split(";")
  43. df = expand(df, id_header, "id")
  44. df = expand(df, VALUES2_HEADER, "values2")
  45. return df
  46. def export_plan(filename, version, target_year, amount_value):
  47. df = data_cleansing(f"{base_dir}/{filename}.json")
  48. season = df[(df["level"] == 2) & (df["Ebene1"] == "Umsatzerlöse")].copy()
  49. season["Dez"] = (100 - season[season_header].sum(axis=1) + season["Dez"]).round(4)
  50. season[season_export_header].to_csv(
  51. f"{base_dir}/Planner_{target_year}_{version}_Saison.csv", encoding="latin_1", sep=";", decimal=",", index=False
  52. )
  53. df["Minus1"] = np.where(df["Ebene1"] != "Umsatzerlöse", -1, 1)
  54. df["Plan"] = df[amount_value] * df["Minus1"]
  55. if amount_value == "Plan":
  56. df["Periode13"] = df["Periode13"] * df["Minus1"]
  57. else:
  58. df["Periode13"] = 0
  59. plan = df[df["planlevel"] == True]
  60. plan = pd.merge(plan, season, how="left", on=["Ebene2", "department"], suffixes=("", "_2"))
  61. plan = apply_season(plan)
  62. plan = plan[source_header + season_header].rename(columns=dict(zip(source_header, export_header)))
  63. # Reisacher Spezialbedingungen
  64. plan["Zeile"] = plan["Zeile mit Bez"].str.slice(stop=4)
  65. plan["Zeile"] = np.where(
  66. plan["Zeile mit Bez"].isin(["BMW aus Leasingrücklauf BFS", "BMW aus Leasingrücklauf Alphabet"]),
  67. "3040",
  68. plan["Zeile"],
  69. )
  70. plan["Zeile"] = np.where(
  71. plan["Zeile mit Bez"].isin(["BMW an Wiederverkäufer BFS", "BMW an Wiederverkäufer Alphabet"]),
  72. "3120",
  73. plan["Zeile"],
  74. )
  75. desciption = pd.read_csv(
  76. f"{base_dir}/../data/Planner_Zeilen_Bez.csv", sep=";", encoding="latin-1", dtype={0: str, 1: str}
  77. )
  78. plan = pd.merge(plan, desciption, how="left", on=["Zeile"], suffixes=["", "_3"])
  79. plan["Zeile mit Bez"] = plan["Zeile mit Bez_3"]
  80. plan.drop(["Zeile mit Bez_3"], axis=1, inplace=True)
  81. if amount_value == "Stk":
  82. plan = plan[plan["Vstufe 1"] == "Umsatzerlöse"]
  83. plan["Vstufe 1"] = "Verk. Stückzahlen"
  84. plan["Version"] = version
  85. plan["Konto"] = ""
  86. plan["Jahr"] = target_year
  87. plan.to_csv(
  88. f"{base_dir}/Planner_{target_year}_{version}_{amount_value}.csv",
  89. encoding="latin_1",
  90. sep=";",
  91. decimal=",",
  92. index=False,
  93. )
  94. if __name__ == "__main__":
  95. # export_plan('V3', '2021', 'Plan')
  96. # export_plan('V3', '2021', 'Stk')
  97. filename = "../save/2023_V1_20230214225753"
  98. filename = "../save/2024_V1_20240119104555"
  99. filename = "../save/2024_V1_20240606161102"
  100. export_plan(filename, "V1", "2024", "Plan")
  101. export_plan(filename, "V1", "2024", "Stk")