csv_accounts.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import pandas as pd
  2. import numpy as np
  3. from re import match
  4. import json
  5. import plac
  6. from pathlib import Path
  7. @plac.pos("period", "", type=str)
  8. def actuals(period):
  9. base_dir = Path(__file__).parent.parent.resolve()
  10. print(base_dir)
  11. df1 = pd.read_csv(
  12. base_dir / "data/Belege_Planung_Ist_FC_AHR.csv", sep=";", decimal=",", dtype={0: str, 1: str, 2: str, 3: float}
  13. )
  14. df2 = pd.read_csv(
  15. base_dir / "data/Belege_Planung_Ist_FC_AHA.csv", sep=";", decimal=",", dtype={0: str, 1: str, 2: str, 3: float}
  16. )
  17. df12 = pd.concat([df1, df2])
  18. df3 = pd.read_csv(
  19. base_dir / "data/NW_GW_Stk_Planung_AHR.csv", sep=";", decimal=",", dtype={0: str, 1: str, 2: str, 3: float}
  20. )
  21. df4 = pd.read_csv(
  22. base_dir / "data/NW_GW_Stk_Planung_AHA.csv", sep=";", decimal=",", dtype={0: str, 1: str, 2: str, 3: float}
  23. )
  24. df34 = pd.concat([df3, df4])
  25. df = pd.merge(df12, df34, how="left", on=["Bookkeep_Period", "Betrieb_Nr", "Konto_Nr"])
  26. # df = pd.read_csv('Planung/Belege_Planung_Ist_FC_Dresen.csv', sep=';', decimal=',',
  27. # dtype={0: str, 1: str, 2: str, 3: str, 4: str, 5: float, 6: float})
  28. df["Jahr"] = df["Bookkeep_Period"].apply(lambda x: x[:4])
  29. current_year = period[:4]
  30. prev_year = str(int(current_year) - 1)
  31. next_year = str(int(current_year) + 1)
  32. month_no = int(period[4:])
  33. # df = df[df['Bookkeep_Period'] <= period]
  34. df["PY"] = np.where(df["Jahr"] == prev_year, df["Betrag"], 0)
  35. df["PYQ"] = np.where(df["Jahr"] == prev_year, df["Menge"], 0)
  36. df["CY"] = np.where(df["Jahr"] == current_year, df["Betrag"], 0)
  37. df["CYQ"] = np.where(df["Jahr"] == current_year, df["Menge"], 0)
  38. df["YTD"] = np.where(df["Bookkeep_Period"] <= period, df["CY"], 0)
  39. df["YTDQ"] = np.where(df["Bookkeep_Period"] <= period, df["CYQ"], 0)
  40. df["FC"] = df["YTD"] * 12 / month_no
  41. df["FCQ"] = df["YTDQ"] * 12 / month_no
  42. df.drop(columns=["Menge", "Betrag", "Jahr", "Bookkeep_Period"], inplace=True)
  43. # df2 = pd.pivot_table(df, values='Betrag', index=['Konto Nr', 'Betrieb Nr'],
  44. # columns=['Jahr'], aggfunc=np.sum, fill_value=0.0)
  45. df = df.groupby(["Konto_Nr", "Betrieb_Nr"]).sum()
  46. print(df.head())
  47. res = {}
  48. for (acct, dept), values in df.to_dict(orient="index").items():
  49. if acct not in res:
  50. res[acct] = {}
  51. res[acct][dept] = [round(v, 2) for v in values.values()]
  52. data = {"values": res}
  53. json.dump(data, open(base_dir / f"export/accounts_{next_year}.json", "w"), indent=2)
  54. @plac.pos("year", "", type=str)
  55. def planning_prev(year):
  56. base_dir = Path(__file__).parent.parent.resolve()
  57. print(base_dir)
  58. df1 = pd.read_csv(
  59. base_dir / f"data/Planner_{year}_V1_Plan.csv",
  60. sep=";",
  61. decimal=",",
  62. encoding="latin-1",
  63. dtype={"Betrieb Nr": str, "Bereich": str, "Zeile": str},
  64. )
  65. df1["Wert"] = df1["Gesamt"] + df1["Periode13"]
  66. df1["Wert"] = np.where(df1["Vstufe 1"] == "Umsatzerlöse", df1["Wert"], df1["Wert"] * -1)
  67. df1["Menge"] = 0
  68. df1 = df1[["Jahr", "Betrieb Nr", "Vstufe 1", "Bereich", "Zeile", "Konto", "Version", "Wert", "Menge"]]
  69. df2 = pd.read_csv(
  70. base_dir / f"data/Planner_{year}_V1_Stk.csv",
  71. sep=";",
  72. decimal=",",
  73. encoding="latin-1",
  74. dtype={"Betrieb Nr": str, "Bereich": str, "Zeile": str},
  75. )
  76. df2.rename(columns={"Gesamt": "Menge"}, inplace=True)
  77. df2["Wert"] = 0
  78. df2["Vstufe 1"] = "Umsatzerlöse"
  79. df2 = df2[["Jahr", "Betrieb Nr", "Vstufe 1", "Bereich", "Zeile", "Konto", "Version", "Wert", "Menge"]]
  80. df = pd.concat([df1, df2])
  81. df["Bereich"] = df["Bereich"].fillna("NA").replace("VW (inkl. GF)", "?")
  82. df["regex"] = df["Vstufe 1"] + ";" + df["Bereich"] + ";.*" + df["Zeile"] + " - [^;]*;;"
  83. gcstruct = json.load(open(base_dir / "export/gcstruct.json", "r"))
  84. structure_ids = [s["id"] for s in gcstruct["flat"]["Struktur_FB"]]
  85. df["id"] = df["regex"].apply(lambda x: (list(filter(lambda y: match(x, y), structure_ids)) + [""])[0])
  86. df["order_by"] = df.index
  87. df = df[df["id"] != ""]
  88. df_grouped = df.groupby(by=["id", "Betrieb Nr"], sort=False).aggregate("sum").reset_index()
  89. df_grouped = df_grouped[df_grouped["Wert"] != 0] # .sort_values(by="order_by")
  90. res = {}
  91. for item in df_grouped.to_dict(orient="records"):
  92. if item["id"] not in res:
  93. res[item["id"]] = {}
  94. res[item["id"]][item["Betrieb Nr"]] = [item["Wert"], item["Menge"]]
  95. data = {"values": res}
  96. next_year = str(int(year) + 1)
  97. json.dump(data, open(base_dir / f"export/planning_{next_year}.json", "w"), indent=2)
  98. def planning_new(filename):
  99. with open("planner/export/" + filename, "r") as frh:
  100. structure = json.load(frh)
  101. year = str(int(filename[:4]) + 1)
  102. result = {}
  103. for s in structure:
  104. if len(s["accounts"]) == 0:
  105. continue
  106. result[s["id"]] = dict([(k, [v[10], v[5]]) for k, v in s["values2"].items()])
  107. with open(f"planner/export/planning_{year}.json", "w") as fwh:
  108. json.dump({"values": result}, fwh, indent=2)
  109. if __name__ == "__main__":
  110. actuals("202310")
  111. planning_prev("2023")
  112. # plac.call(actuals)
  113. # planning_new('2022_V2_20220407150009.json')