planner_load.py 8.2 KB

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