csv_accounts.py 3.0 KB

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