planner_load.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. from webservice.plan_values2 import VALUES2_MAPPING
  2. import json
  3. from pathlib import Path
  4. class PlannerLoad:
  5. base_dir: Path
  6. account_values: list = None
  7. account_info: list = None
  8. plan_values: list = None
  9. marketing_values: list = None
  10. new_structure: list
  11. structure: list
  12. config = {
  13. "department": [1, 2, 3, 10, 30, 40, 50, 55, 81, 82],
  14. }
  15. def __init__(self, base_dir: str):
  16. self.base_dir = Path(base_dir)
  17. def new_file(self, year: str):
  18. self.load_values(year)
  19. return self.convert_file(self.new_structure)
  20. def load_values(self, year: str):
  21. with open(self.base_dir.joinpath(f"accounts_{year}.json"), "r") as frh:
  22. self.account_values = json.load(frh)["values"]
  23. with open(self.base_dir.joinpath(f"planning_{year}.json"), "r") as frh:
  24. self.plan_values = json.load(frh)["values"]
  25. with open(self.base_dir.joinpath(f"marketing_{year}.json"), "r") as frh:
  26. self.marketing_values = json.load(frh)
  27. with open(self.base_dir.joinpath("gcstruct.json"), "r") as frh:
  28. gcstruct = json.load(frh)
  29. self.account_info = dict([(a["Konto_Nr"], a) for a in gcstruct["accounts"]])
  30. self.new_structure = gcstruct["flat"]["Struktur_FB"]
  31. def set_structure(self, structure: list):
  32. self.structure = structure
  33. def convert_file(self, structure_source):
  34. self.structure = []
  35. for s in structure_source:
  36. accts = s["accounts"]
  37. s["accounts"] = []
  38. s["planlevel"] = len(accts) > 0
  39. self.structure.append(s)
  40. for a in accts:
  41. if type(a) is str:
  42. acct = {
  43. "text": a + " - " + self.account_info[a]["Konto_Bezeichnung"],
  44. "id": a,
  45. "costcenter": self.account_info[a]["Konto_KST"],
  46. "values": [],
  47. "values2": None,
  48. }
  49. else:
  50. acct = a
  51. a = acct["id"]
  52. acct["id"] = s["id"].replace(";;", ";" + acct["text"] + ";", 1)
  53. s["children"].append(acct["id"])
  54. new_account = {
  55. "id": acct["id"],
  56. "text": acct["text"],
  57. "children": [],
  58. "children2": [],
  59. "parents": [s["id"]] + s["parents"],
  60. "accounts": [a],
  61. "costcenter": acct["costcenter"],
  62. "level": s["level"] + 1,
  63. "drilldown": False,
  64. "form": s["form"],
  65. "accountlevel": False,
  66. "absolute": True,
  67. "seasonal": True,
  68. "status": "0",
  69. "values": acct["values"],
  70. "values2": acct["values2"],
  71. }
  72. self.structure.append(new_account)
  73. for s in self.structure:
  74. self.get_values2(s)
  75. s["options"] = self.get_options(s)
  76. del s["accountlevel"]
  77. del s["absolute"]
  78. del s["seasonal"]
  79. del s["status"]
  80. s["sumvalues"] = [0] * 30
  81. return self.structure
  82. def get_values2(self, s):
  83. if "values2" not in s or s["values2"] is None or len(s["values2"].keys()) == 0:
  84. s["values2"] = dict([(str(d), [0] * 30) for d in self.config["department"]])
  85. else:
  86. for d in s["values2"].keys():
  87. if len(s["values2"][d]) < 30:
  88. s["values2"][d] = [
  89. s["values2"][d][i] if 0 <= i < len(s["values2"][d]) else 0 for i in VALUES2_MAPPING
  90. ]
  91. self.update_account_values(s)
  92. self.update_plan_values(s)
  93. self.update_marketing_values(s)
  94. def update_values(self, value_type):
  95. v_types = {
  96. "accounts": self.update_account_values,
  97. "plan": self.update_plan_values,
  98. "marketing": self.update_marketing_values,
  99. }
  100. parents = []
  101. for s in self.structure:
  102. if v_types[value_type](s):
  103. parents.append(s["parents"][0])
  104. if value_type == "marketing":
  105. for p_id in set(parents):
  106. parent = self.get_structure_by_id(p_id)
  107. parent["form"] = "9"
  108. for d in parent["options"].keys():
  109. parent["options"][d]["status"] = "0"
  110. for d in parent["values2"].keys():
  111. for i in range(15):
  112. parent["values2"][d][i] = 0.0
  113. return self.structure
  114. def get_structure_by_id(self, id):
  115. for s in self.structure:
  116. if s["id"] == id:
  117. return s
  118. return None
  119. def update_account_values(self, s):
  120. if len(s["accounts"]) == 0:
  121. return False
  122. a_values = self.account_values.get(s["accounts"][0], dict())
  123. for d in s["values2"].keys():
  124. if d in a_values:
  125. for i, v in enumerate(a_values[d], 20):
  126. s["values2"][d][i] = v
  127. return s["values2"]
  128. def update_plan_values(self, s):
  129. if self.plan_values is None:
  130. return False
  131. p_values = self.plan_values.get(s["id"], dict())
  132. for d in s["values2"].keys():
  133. if d in p_values:
  134. for i, v in enumerate(p_values[d], 28):
  135. s["values2"][d][i] = v
  136. return True
  137. def update_marketing_values(self, s):
  138. if len(s["accounts"]) == 0:
  139. return False
  140. m_values = self.marketing_values.get(s["accounts"][0], dict())
  141. if not m_values:
  142. return False
  143. for d in s["values2"].keys():
  144. if d in m_values:
  145. for i, v in enumerate(m_values[d], 1):
  146. s["values2"][d][i] = v
  147. s["values2"][d][0] = sum(m_values[d])
  148. s["values2"][d][14] = sum(m_values[d])
  149. if "options" in s:
  150. s["options"][d]["seasonal"] = False
  151. return True
  152. def convert_values2(self, s):
  153. a_values = {}
  154. if s["accounts"]:
  155. a_values = self.account_values.get(s["accounts"][0], dict())
  156. p_values = self.plan_values.get(s["id"], dict())
  157. values = dict([(str(d), [0] * 30) for d in self.config["department"]])
  158. for d in values.keys():
  159. if d in s["values2"]:
  160. if len(s["values2"][d]) == 30:
  161. values[d] = s["values2"][d]
  162. else:
  163. values[d] = [s["values2"][d].get(i, 0) for i in VALUES2_MAPPING]
  164. if d in p_values:
  165. for i, v in enumerate(p_values[d], 28):
  166. values[d][i] = v
  167. if d in a_values:
  168. for i, v in enumerate(a_values[d], 20):
  169. values[d][i] = v
  170. return values
  171. def get_options(self, s):
  172. status = "0"
  173. seasonal = True
  174. absolute = True
  175. planlevel = False
  176. if isinstance(s["absolute"], bool):
  177. absolute = s["absolute"]
  178. s["absolute"] = {}
  179. if isinstance(s["seasonal"], bool):
  180. seasonal = s["seasonal"]
  181. s["seasonal"] = {}
  182. if isinstance(s["status"], str):
  183. status = s["status"]
  184. s["status"] = {}
  185. if "planlevel" in s:
  186. planlevel = s["planlevel"]
  187. opts = dict(
  188. [
  189. (str(d), {"absolute": absolute, "seasonal": seasonal, "status": status, "planlevel": planlevel})
  190. for d in self.config["department"]
  191. ]
  192. )
  193. for d in opts.keys():
  194. if d in s["absolute"]:
  195. opts[d]["absolute"] = s["absolute"][d]
  196. if d in s["seasonal"]:
  197. opts[d]["seasonal"] = s["seasonal"][d]
  198. if d in s["status"]:
  199. opts[d]["status"] = s["status"][d]
  200. return opts
  201. if __name__ == "__main__":
  202. planner_dir = Path(__file__).parent.parent.resolve() / "export"
  203. p_load = PlannerLoad(planner_dir)
  204. print(p_load.convert_file(json.load(open(planner_dir / "2022_V1.json", "r")))[12])