csv_accounts.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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__).absolute().parent.parent
  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"], inplace=True)
  43. # df2 = pd.pivot_table(df, values='Betrag', index=['Konto Nr', 'Betrieb Nr'], columns=['Jahr'], aggfunc=np.sum, fill_value=0.0)
  44. df = df.groupby(["Konto_Nr", "Betrieb_Nr"]).sum()
  45. print(df.head())
  46. res = {}
  47. for (acct, dept), values in df.to_dict(orient="index").items():
  48. if acct not in res:
  49. res[acct] = {}
  50. res[acct][dept] = [round(v, 2) for v in values.values()]
  51. data = {"values": res}
  52. json.dump(data, open(base_dir / f"export/accounts_{next_year}.json", "w"), indent=2)
  53. @plac.pos("year", "", type=str)
  54. def planning_prev(year):
  55. base_dir = Path.cwd().parent
  56. print(base_dir)
  57. df1 = pd.read_csv(
  58. base_dir / f"data/Planner_{year}_V1_Plan.csv",
  59. sep=";",
  60. decimal=",",
  61. encoding="latin-1",
  62. dtype={"Betrieb Nr": str, "Bereich": str, "Zeile": str},
  63. )
  64. df1["Wert"] = df1["Gesamt"]
  65. df1 = df1[["Jahr", "Betrieb Nr", "Vstufe 1", "Bereich", "Zeile", "Konto", "Version", "Wert"]]
  66. df2 = pd.read_csv(
  67. base_dir / f"data/Planner_{year}_V1_Stk.csv",
  68. sep=";",
  69. decimal=",",
  70. encoding="latin-1",
  71. dtype={"Betrieb Nr": str, "Bereich": str, "Zeile": str},
  72. )
  73. df2["Menge"] = df2["Gesamt"]
  74. df2["Vstufe 1"] = "Umsatzerlöse"
  75. df2 = df2[["Jahr", "Betrieb Nr", "Vstufe 1", "Bereich", "Zeile", "Konto", "Version", "Menge"]]
  76. df = pd.merge(df1, df2, how="left", on=["Jahr", "Betrieb Nr", "Vstufe 1", "Bereich", "Zeile", "Konto", "Version"])
  77. df["Menge"] = df["Menge"].fillna(0)
  78. df["Wert"] = df["Wert"].fillna(0)
  79. df["Wert"] = np.where(df["Vstufe 1"] == "Umsatzerlöse", df["Wert"], df["Wert"] * -1)
  80. df["Bereich"] = df["Bereich"].fillna("NA").replace("VW (inkl. GF)", "?")
  81. df["regex"] = df["Vstufe 1"] + ";" + df["Bereich"] + ";.*" + df["Zeile"] + " - [^;]*;;"
  82. df = df[df["Wert"] != 0]
  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 = df[df["id"] != ""]
  87. res = {}
  88. for item in df.to_dict(orient="records"):
  89. if item["id"] not in res:
  90. res[item["id"]] = {}
  91. res[item["id"]][item["Betrieb Nr"]] = [item["Wert"], item["Menge"]]
  92. data = {"values": res}
  93. next_year = str(int(year) + 1)
  94. json.dump(data, open(base_dir / f"export/planning_{next_year}.json", "w"), indent=2)
  95. def planning_new(filename):
  96. with open("planner/export/" + filename, "r") as frh:
  97. structure = json.load(frh)
  98. year = str(int(filename[:4]) + 1)
  99. result = {}
  100. for s in structure:
  101. if len(s["accounts"]) == 0:
  102. continue
  103. result[s["id"]] = dict([(k, [v[10], v[5]]) for k, v in s["values2"].items()])
  104. with open(f"planner/export/planning_{year}.json", "w") as fwh:
  105. json.dump({"values": result}, fwh, indent=2)
  106. if __name__ == "__main__":
  107. actuals("202310")
  108. planning_prev("2023")
  109. # plac.call(actuals)
  110. # planning_new('2022_V2_20220407150009.json')