csv_accounts.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import pandas as pd
  2. import numpy as np
  3. from re import match
  4. import json
  5. def actuals(period):
  6. df = pd.read_csv('Planung/Belege_Planung_Ist_FC.csv', sep=';', decimal=',',
  7. dtype={0: str, 1: str, 2: str, 3: float})
  8. df = df[df['Bookkeep_Period'] <= period]
  9. df['Jahr'] = df['Bookkeep_Period'].apply(lambda x: 'AJ' if x[:4] == '2020' else 'VJ')
  10. df['primary_key'] = df['Betrieb Nr'] + '_' + df['Konto Nr']
  11. df = pd.pivot_table(df, values='Betrag', index=['primary_key'], columns=['Jahr'], aggfunc=np.sum, fill_value=0.0)
  12. df['FC'] = df['AJ'] * 12 / int(period[4:])
  13. res = {}
  14. for (pkey, values) in df.to_dict(orient='index').items():
  15. split = pkey.split('_')
  16. if not split[1] in res:
  17. res[split[1]] = {}
  18. res[split[1]][split[0]] = [values['VJ'], values['AJ'], values['FC'], 0.0, 0.0, 0.0]
  19. data = {'values': res}
  20. json.dump(data, open('Planung/export/accounts.json', 'w'), indent=2)
  21. def planning_prev():
  22. df1 = pd.read_csv('Planung/Global Planner_2018_ohne_Marketing.csv',
  23. sep=';', decimal=',', encoding='ansi', dtype={'Betrieb Nr': str, 'Bereich': str})
  24. df1 = df1[['Jahr', 'Betrieb Nr', 'Vstufe 1', 'Bereich', 'Zeile mit Bez', 'Version', 'Menge', 'Wert']]
  25. df2 = pd.read_csv('Planung/AHA_Global Planner_2018_PKW_MOT_ohne_Marketing.csv',
  26. sep=';', decimal=',', encoding='ansi', dtype={'Betrieb Nr': str, 'Bereich': str})
  27. df2 = df2[['Jahr', 'Betrieb Nr', 'Vstufe 1', 'Bereich', 'Zeile mit Bez', 'Version', 'Menge', 'Wert']]
  28. df = pd.concat([df1, df2])
  29. df['Bereich'] = df['Bereich'].fillna('NA').replace('VW (inkl. GF)', '?')
  30. df['Zeile'] = df['Zeile mit Bez'].apply(lambda x: x[:4])
  31. df['Konto'] = ''
  32. df['regex'] = df['Vstufe 1'] + ";" + df['Bereich'] + ";.*" + df['Zeile'] + ' - [^;]*;;'
  33. df = df[df['Wert'] != 0]
  34. gcstruct = json.load(open('GCStruct_Reisacher_Planung/gcstruct_reisacher.json', 'r'))
  35. structure_ids = [s['id'] for s in gcstruct['flat']['Struktur_FB']]
  36. # print(structure_ids)
  37. df['id'] = df['regex'].apply(lambda x: (list(filter(lambda y: match(x, y), structure_ids)) + [''])[0])
  38. df = df[df['id'] != '']
  39. # df['pkey'] = df['id'] + "_" + df['Betrieb Nr']
  40. # df = df.set_index('pkey')
  41. res = {}
  42. for item in df.to_dict(orient='records'):
  43. if not item['id'] in res:
  44. res[item['id']] = {}
  45. res[item['id']][item['Betrieb Nr']] = [item['Wert'], item['Menge']]
  46. data = {'values': res}
  47. json.dump(data, open('Planung/export/planning.json', 'w'), indent=2)
  48. if __name__ == '__main__':
  49. planning_prev()
  50. actuals('202010')