plan_export.py 4.8 KB

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