import json from pathlib import Path class PlannerLoad: base_dir: Path account_values: list account_info: list structure: list config = { 'department': [1, 2, 3, 10, 30, 40, 50, 55, 81, 82], 'translation': [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 3, 4, 5, 6, 7, 4, 0, 4, 1, 4, 2, 4, 4, 4, 8, 9] } def __init__(self, base_dir: str): self.base_dir = Path(base_dir) def new_file(self, year: str): with open(self.base_dir.joinpath(f'accounts_{year}.json'), 'r') as frh: self.account_values = json.load(frh)['values'] with open(self.base_dir.joinpath(f'planning_{year}.json'), 'r') as frh: self.plan_values = json.load(frh)['values'] with open(self.base_dir.joinpath('gcstruct.json'), 'r') as frh: gcstruct = json.load(frh) self.account_info = dict([(a['Konto_Nr'], a) for a in gcstruct['accounts']]) structure_source = gcstruct['flat']['Struktur_FB'] self.structure = [] for s in structure_source: accts = s['accounts'] s['accounts'] = [] s['planlevel'] = (len(accts) > 0) self.structure.append(s) for a in accts: acct_text: str = a + ' - ' + self.account_info[a]['Konto_Bezeichnung'] acct_id = s['id'].replace(';;', ';' + acct_text + ';', 1) s['children'].append(acct_id) new_account = { "id": acct_id, "text": acct_text, "children": [], "children2": [], "parents": [s['id']] + s['parents'], "accounts": [a], "costcenter": self.account_info[a]['Kostenstelle'], "level": s['level'] + 1, "drilldown": False, "form": s['form'], "accountlevel": False, "absolute": True, "seasonal": True, "status": "0", "values": [], "values2": {} } self.structure.append(new_account) for s in self.structure: s['values2'] = self.get_values2(s) s['options'] = self.get_options(s) del s['accountlevel'] del s['absolute'] del s['seasonal'] del s['status'] s['sumvalues'] = [0] * 30 return self.structure def get_values2(self, s): a_values = {} if s['accounts']: a_values = self.account_values.get(s['accounts'][0], dict()) p_values = self.plan_values.get(s['id'], dict()) values = dict([(str(d), [0] * 30) for d in self.config['department']]) for d in values.keys(): if d in s['values2']: if len(s['values2'][d]) == 30: values[d] = s['values2'][d] else: values[d] = [s['values2'][d].get(i, 0) for i in self.config['translation']] if d in p_values: for i, v in enumerate(p_values[d], 28): values[d][i] = v if d in a_values: for i, v in enumerate(a_values[d], 20): values[d][i] = v return values def get_options(self, s): status = "0" seasonal = True absolute = True planlevel = False if isinstance(s['absolute'], bool): absolute = s['absolute'] s['absolute'] = {} if isinstance(s['seasonal'], bool): seasonal = s['seasonal'] s['seasonal'] = {} if isinstance(s['status'], str): status = s['status'] s['status'] = {} if 'planlevel' in s: planlevel = s['planlevel'] opts = dict([(str(d), { 'absolute': absolute, 'seasonal': seasonal, 'status': status, 'planlevel': planlevel }) for d in self.config['department']]) for d in opts.keys(): if d in s['absolute']: opts[d]['absolute'] = s['absolute'][d] if d in s['seasonal']: opts[d]['seasonal'] = s['seasonal'][d] if d in s['status']: opts[d]['status'] = s['status'][d] return opts if __name__ == '__main__': planner_dir = Path(__file__).parent.parent.joinpath('export') p_load = PlannerLoad(planner_dir) print(p_load.new_file('2022')[12])