csv_accounts.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import pandas as pd
  2. import numpy as np
  3. from re import match
  4. import json
  5. from pathlib import Path
  6. def actuals(period):
  7. base_dir = Path('.').absolute()
  8. df1 = pd.read_csv(base_dir.joinpath('planner/Planung/Belege_Planung_Ist_FC_AHR.csv'), sep=';', decimal=',',
  9. dtype={0: str, 1: str, 2: str, 3: float})
  10. df2 = pd.read_csv(base_dir.joinpath('planner/Planung/Belege_Planung_Ist_FC_AHA.csv'), sep=';', decimal=',',
  11. dtype={0: str, 1: str, 2: str, 3: float})
  12. df12 = pd.concat([df1, df2])
  13. df3 = pd.read_csv(base_dir.joinpath('planner/Planung/NW_GW_Stk_Planung_AHR.csv'), sep=';', decimal=',',
  14. dtype={0: str, 1: str, 2: str, 3: float})
  15. df4 = pd.read_csv(base_dir.joinpath('planner/Planung/NW_GW_Stk_Planung_AHA.csv'), sep=';', decimal=',',
  16. dtype={0: str, 1: str, 2: str, 3: float})
  17. df34 = pd.concat([df3, df4])
  18. df = pd.merge(df12, df34, how='left', on=['Bookkeep_Period', 'Betrieb_Nr', 'Konto_Nr'])
  19. # df = pd.read_csv('Planung/Belege_Planung_Ist_FC_Dresen.csv', sep=';', decimal=',',
  20. # dtype={0: str, 1: str, 2: str, 3: str, 4: str, 5: float, 6: float})
  21. df['Jahr'] = df['Bookkeep_Period'].apply(lambda x: x[:4])
  22. current_year = period[:4]
  23. prev_year = str(int(current_year) - 1)
  24. next_year = str(int(current_year) + 1)
  25. month_no = int(period[4:])
  26. # df = df[df['Bookkeep_Period'] <= period]
  27. df['PY'] = np.where(df['Jahr'] == prev_year, df['Betrag'], 0)
  28. df['PYQ'] = np.where(df['Jahr'] == prev_year, df['Menge'], 0)
  29. df['CY'] = np.where(df['Jahr'] == current_year, df['Betrag'], 0)
  30. df['CYQ'] = np.where(df['Jahr'] == current_year, df['Menge'], 0)
  31. df['YTD'] = np.where(df['Bookkeep_Period'] <= period, df['CY'], 0)
  32. df['YTDQ'] = np.where(df['Bookkeep_Period'] <= period, df['CYQ'], 0)
  33. df['FC'] = df['YTD'] * 12 / month_no
  34. df['FCQ'] = df['YTDQ'] * 12 / month_no
  35. df.drop(columns=['Menge', 'Betrag'], inplace=True)
  36. # df2 = pd.pivot_table(df, values='Betrag', index=['Konto Nr', 'Betrieb Nr'], columns=['Jahr'], aggfunc=np.sum, fill_value=0.0)
  37. df = df.groupby(['Konto_Nr', 'Betrieb_Nr']).sum()
  38. print(df.head())
  39. res = {}
  40. for (acct, dept), values in df.to_dict(orient='index').items():
  41. if acct not in res:
  42. res[acct] = {}
  43. res[acct][dept] = [round(v, 2) for v in values.values()]
  44. data = {'values': res}
  45. json.dump(data, open(base_dir.joinpath(f'planner/export/accounts_{next_year}.json'), 'w'), indent=2)
  46. def planning_prev():
  47. df1 = pd.read_csv('planner/Planung/Global Planner_2018_ohne_Marketing.csv',
  48. sep=';', decimal=',', encoding='latin-1', dtype={'Betrieb Nr': str, 'Bereich': str})
  49. df1 = df1[['Jahr', 'Betrieb Nr', 'Vstufe 1', 'Bereich', 'Zeile mit Bez', 'Version', 'Menge', 'Wert']]
  50. df2 = pd.read_csv('planner/Planung/AHA_Global Planner_2018_PKW_MOT_ohne_Marketing.csv',
  51. sep=';', decimal=',', encoding='latin-1', dtype={'Betrieb Nr': str, 'Bereich': str})
  52. df2 = df2[['Jahr', 'Betrieb Nr', 'Vstufe 1', 'Bereich', 'Zeile mit Bez', 'Version', 'Menge', 'Wert']]
  53. df = pd.concat([df1, df2])
  54. df['Bereich'] = df['Bereich'].fillna('NA').replace('VW (inkl. GF)', '?')
  55. df['Zeile'] = df['Zeile mit Bez'].apply(lambda x: x[:4])
  56. df['Konto'] = ''
  57. df['regex'] = df['Vstufe 1'] + ";" + df['Bereich'] + ";.*" + df['Zeile'] + ' - [^;]*;;'
  58. df = df[df['Wert'] != 0]
  59. gcstruct = json.load(open('GCStruct_Reisacher_Planung/gcstruct_reisacher.json', 'r'))
  60. structure_ids = [s['id'] for s in gcstruct['flat']['Struktur_FB']]
  61. df['id'] = df['regex'].apply(lambda x: (list(filter(lambda y: match(x, y), structure_ids)) + [''])[0])
  62. df = df[df['id'] != '']
  63. res = {}
  64. for item in df.to_dict(orient='records'):
  65. if item['id'] not in res:
  66. res[item['id']] = {}
  67. res[item['id']][item['Betrieb Nr']] = [item['Wert'], item['Menge']]
  68. data = {'values': res}
  69. json.dump(data, open('planner/export/planning_2021.json', 'w'), indent=2)
  70. def planning_new(filename):
  71. with open('planner/export/' + filename, 'r') as frh:
  72. structure = json.load(frh)
  73. year = str(int(filename[:4]) + 1)
  74. result = {}
  75. for s in structure:
  76. if len(s['accounts']) == 0:
  77. continue
  78. result[s['id']] = dict([(k, [v[10], v[5]]) for k, v in s['values2'].items()])
  79. with open(f"planner/export/planning_{year}.json", 'w') as fwh:
  80. json.dump({'values': result}, fwh, indent=2)
  81. if __name__ == '__main__':
  82. # planning_prev()
  83. # actuals('202209')
  84. planning_new('2022_V2_20220407150009.json')