csv_accounts.py 2.8 KB

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