hbv_export.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import pandas as pd
  2. import numpy as np
  3. from datetime import datetime
  4. from gnupg_encrypt import encrypt
  5. import os
  6. from pathlib import Path
  7. current_year = '2023'
  8. current_version = 'V1'
  9. base_dir = Path().cwd()
  10. config_dir = base_dir / 'config/hbv'
  11. export_dir = base_dir / 'export'
  12. hb_format = config_dir / 'hb_format.csv'
  13. hb_department = config_dir / 'hb_department.csv'
  14. hb_translation = config_dir / 'hb_translation.csv'
  15. plan_amount = export_dir / f'Planner_{current_year}_{current_version}_Stk.csv'
  16. plan_values = export_dir / f'Planner_{current_year}_{current_version}_Plan.csv'
  17. hb_ignored = export_dir / 'hbv/ignoriert.csv'
  18. current_date = datetime.now().strftime('%d%m%Y%H%M%S')
  19. # current_date = '24032021112656'
  20. def main():
  21. # Übersetzungstabelle importieren
  22. df_translation = pd.read_csv(hb_translation, decimal=',', sep=';',
  23. encoding='latin-1', converters={i: str for i in range(0, 200)})
  24. # df_translation['column_no_join'] = np.where(df_translation['column_no']
  25. # .isin(['1', '3', '4']), df_translation['column_no'], '0')
  26. # Department-Zuordnung importieren
  27. df_department = pd.read_csv(hb_department, decimal=',', sep=';',
  28. encoding='latin-1', converters={i: str for i in range(0, 200)})
  29. # Planwerte importieren
  30. values_converter = {i: str for i in range(0, 200)}
  31. values_converter[4] = lambda x: np.float64(x.replace(',', '.') if x != '' else 0.0)
  32. values_converter[5] = values_converter[4]
  33. df_values = pd.read_csv(plan_values, decimal=',', sep=';',
  34. encoding='latin-1', converters=values_converter)
  35. df_values['Gesamt'] = df_values['Gesamt'] + df_values['Periode13']
  36. df_values['type'] = '2'
  37. df_values['type'] = np.where(df_values['Vstufe 1'].isin(['Materialaufwand']), '3', df_values['type'])
  38. df_amount = pd.read_csv(plan_amount, decimal=',', sep=';',
  39. encoding='latin-1', converters=values_converter)
  40. df_amount['type'] = '1'
  41. df: pd.DataFrame = df_values.append(df_amount)
  42. # Planwerte alle positiv
  43. df['Minus1'] = np.where(df['Vstufe 1'].isin(['Umsatzerlöse', 'Verk. Stückzahlen']) | df['Zeile'].isin(['7410', '7440']), 1, -1)
  44. df['Gesamt'] = df['Gesamt'] * df['Minus1']
  45. # Planwerte übersetzen
  46. df = df.merge(df_department, how='inner', left_on='Betrieb Nr', right_on='department_id')
  47. df = df.merge(df_translation, how='left', left_on=['Zeile', 'type'], right_on=['from', 'type'])
  48. # fehlende Übersetzung
  49. df_ignored = df[(df['to'].isna()) & (df['Gesamt'] != 0)]
  50. df_ignored.to_csv(hb_ignored, decimal=',', sep=';', encoding='latin-1', index=False)
  51. # Planwerte formatieren und exportieren
  52. rename_from = ['bm_code', 'BV_NUMMER', 'FILIAL_NR', 'to', 'column_no', 'Jahr', 'Gesamt']
  53. rename_to = ['BM_CODE', 'BV_NUMMER', 'FILIAL_NR', 'ZEILE', 'SPALTE', 'JAHR', 'WERT']
  54. df_valid = df[df['to'].notna()].rename(columns=dict(zip(rename_from, rename_to)))
  55. df_valid['SPALTE'] = df_valid['SPALTE'].str.zfill(3)
  56. group_by = ['BM_CODE', 'BV_NUMMER', 'FILIAL_NR']
  57. df_valid = df_valid[rename_to].groupby(group_by)
  58. for group in df_valid.groups:
  59. g = dict(zip(group_by, group))
  60. filename = export_dir / f"hbv/{current_year}/{g['BV_NUMMER']}_{g['FILIAL_NR']}/HB{g['BM_CODE']}{current_year}00{g['BV_NUMMER']}{g['FILIAL_NR']}0{current_date}.dat"
  61. os.makedirs(filename.parent, exist_ok=True)
  62. df_group = df_valid.get_group(group).groupby(rename_to[:-1]).sum().reset_index()
  63. with open(filename, 'w') as fwh:
  64. for row in df_group.to_dict(orient='records'):
  65. fwh.write("I0155{BV_NUMMER}{FILIAL_NR}0{ZEILE}{SPALTE}00{JAHR}{WERT:16.2f}03\n".format(**row))
  66. encrypt(filename)
  67. if __name__ == '__main__':
  68. main()