marketing_plan.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import pandas as pd
  2. import numpy as np
  3. from pathlib import Path
  4. import json
  5. year = "2024"
  6. base_dir = Path(__file__).parent.parent.resolve()
  7. filename = base_dir / "data" / f"Marketingplanung_AHR_{year}_V1.xls"
  8. output = base_dir / "export" / f"marketing_{year}.json"
  9. output_csv = base_dir / "export" / f"marketing_{year}.csv"
  10. debug_csv = base_dir / "export" / f"marketing_{year}_rest.csv"
  11. department = {" MM": "10", " ULM": "40", " LL": "50", "KRU": "30", "GZ": "55", "AAM": "82", "AAM-MOT": "81"}
  12. columns = [
  13. "Bezeichnung",
  14. "Segment",
  15. "Termin",
  16. "Wert1",
  17. "Wert2",
  18. "Wert3",
  19. "Wert4",
  20. "Wert5",
  21. "Wert6",
  22. "Wert7",
  23. "Wert8",
  24. "Wert9",
  25. "Wert10",
  26. "Wert11",
  27. "Wert12",
  28. "Summe",
  29. ]
  30. defaults = ["", "", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  31. types = [str] * 3 # + [float] * 13
  32. xls = pd.read_excel(
  33. filename,
  34. sheet_name=list(department.keys()),
  35. skiprows=4,
  36. usecols="A:P",
  37. names=columns,
  38. converters=dict(zip(columns, types)),
  39. )
  40. xls_temp = []
  41. for sheet, dept_no in department.items():
  42. xls[sheet]["Betrieb Nr"] = dept_no
  43. xls[sheet] = xls[sheet].fillna(value=dict(zip(columns, defaults)))
  44. xls[sheet]["Konto Nr"] = np.where(
  45. xls[sheet]["Segment"].str.startswith("Kto "), xls[sheet]["Segment"].str.slice(4, 9), "Rest"
  46. )
  47. # xls[sheet]['Konto Nr'] = np.where(xls[sheet]['Segment'].str.startswith('50% '),
  48. # xls[sheet]['Segment'].str.slice(-5), xls[sheet]['Konto Nr'])
  49. temp = xls[sheet][xls[sheet]["Segment"].str.startswith("50% ")].copy()
  50. temp["Konto Nr"] = temp["Segment"].str.slice(8, 13)
  51. xls_temp.append(temp)
  52. temp2 = temp.copy()
  53. temp2["Konto Nr"] = temp2["Segment"].str.slice(-5)
  54. xls_temp.append(temp2)
  55. xls_temp_concat = pd.concat(xls_temp)
  56. for c in columns[3:]:
  57. xls_temp_concat[c] = xls_temp_concat[c] * 0.5
  58. df = pd.concat(list(xls.values()) + [xls_temp_concat])
  59. df_rest = df[(df["Konto Nr"] == "Rest") & (df["Summe"] != 0)]
  60. df_rest.to_csv(debug_csv, sep=";", decimal=",")
  61. df = df[df["Konto Nr"] != "Rest"]
  62. df["Wert11"] = df["Wert11"].astype(float)
  63. df["Wert12"] = df["Wert12"].astype(float)
  64. for c in columns[3:]:
  65. df[c] = df[c].round()
  66. df = df.drop(labels=["Summe"], axis=1)
  67. df = df.groupby(["Konto Nr", "Betrieb Nr"]).sum()
  68. print(df.info())
  69. # df.reset_index(inplace=True)
  70. df = df.drop(labels=["Bezeichnung", "Segment", "Termin"], axis=1)
  71. df.to_csv(output_csv, sep=";", decimal=",")
  72. dict_split = df.to_dict(orient="split")
  73. res = {}
  74. for index, data in zip(dict_split["index"], dict_split["data"]):
  75. kto, dept = index
  76. if kto not in res:
  77. res[kto] = {}
  78. res[kto][dept] = data
  79. json.dump(res, open(output, "w"), indent=2)