skr51.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from itertools import product
  2. # import numpy as np
  3. from pathlib import Path
  4. import pandas as pd
  5. class skr51_validation:
  6. current_dir = Path("/home/robert/projekte/python/gcstruct/SKR51")
  7. def __init__(self):
  8. filter_csv = pd.read_csv(
  9. self.current_dir.joinpath("SKR51_Kostenrechnungsmerkmale.csv"), sep=";", quotechar='"'
  10. ) # , encoding='latin-1')
  11. filter_csv["Filter"] = filter_csv["Filter"].apply(self.create_list)
  12. self.filter = filter_csv.set_index("Block").to_dict(orient="dict")["Filter"]
  13. # print(self.filter)
  14. explode_cols = ["Kostenstelle", "Absatzkanal", "Kostenträger"]
  15. matrix_csv = pd.read_csv(
  16. self.current_dir.joinpath("Plausibilitaetsmatrix.csv"), sep=";", converters={i: str for i in range(0, 200)}
  17. ) # , encoding='latin-1' .set_index('Konto_Nr')
  18. matrix_csv["valid"] = True
  19. for col in explode_cols:
  20. matrix_csv[col] = matrix_csv[col].apply(self.create_list)
  21. self.matrix = matrix_csv
  22. # self.matrix['Kostenstelle'] = matrix_csv[['Konto_Nr', 'Kostenstelle', 'valid']].explode('Kostenstelle')
  23. # print(self.matrix[self.matrix['Konto_Nr'] == '3300'])
  24. def create_list(self, whitelist):
  25. if whitelist == "":
  26. # return [str(i).zfill(2) for i in range(0, 100)]
  27. return ["00"]
  28. result = []
  29. for x in whitelist.split(","):
  30. if "-" in x:
  31. start, end = map(int, x.split("-"))
  32. result += [str(i).zfill(2) for i in range(start, end + 1)]
  33. else:
  34. result.append(x)
  35. return result
  36. def check_bookings(self, bookings_file):
  37. export_csv = pd.read_csv(
  38. self.current_dir.joinpath(bookings_file),
  39. sep=";",
  40. decimal=",",
  41. converters={i: str for i in range(0, 200)},
  42. encoding="latin-1",
  43. )
  44. export_csv = export_csv.merge(
  45. self.matrix[["Konto_Nr", "valid"]], how="left", on="Konto_Nr", suffixes=(None, "_matrix")
  46. )
  47. export_csv["Konto_Nr_valid"] = export_csv["valid"] == True
  48. temp = export_csv["Konto_Nr_Händler"]
  49. export_csv.drop(["Konto_Nr_Händler"], axis=1, inplace=True)
  50. export_csv.insert(0, "Konto_Nr_Händler", temp)
  51. for col in ["Marke", "Standort"]:
  52. export_csv[col + "_valid"] = export_csv[col].isin(self.filter[col])
  53. export_csv["valid"] = export_csv["valid"] & export_csv[col + "_valid"]
  54. for col in ["Kostenstelle", "Absatzkanal", "Kostenträger"]:
  55. export_csv = export_csv.merge(
  56. self.matrix[["Konto_Nr", col, "valid"]].explode(col),
  57. how="left",
  58. on=["Konto_Nr", col],
  59. suffixes=(None, "_" + col),
  60. )
  61. export_csv[col + "_valid"] = export_csv[col].isin(self.filter[col]) & export_csv["valid_" + col] == True
  62. export_csv.drop(["valid_" + col], axis=1, inplace=True)
  63. export_csv["valid"] = export_csv["valid"] & export_csv[col + "_valid"]
  64. return export_csv
  65. def maximum_accounts(self):
  66. accounts = self.matrix.to_dict(orient="records")
  67. header = [
  68. "Account",
  69. "Make",
  70. "Site",
  71. "Origin",
  72. "SalesChannel",
  73. "CostCarrier",
  74. "CostAccountingString",
  75. "Decimals",
  76. "OpeningBalance",
  77. "Period01",
  78. "CumulatedYear",
  79. "Marke_HBV",
  80. ]
  81. with open(self.current_dir.joinpath("maximum.csv"), "w", encoding="latin-1") as fwh:
  82. fwh.write(";".join(header) + "\n")
  83. for a in accounts:
  84. for entry in product(a["Kostenstelle"], a["Absatzkanal"], a["Kostenträger"]):
  85. row = [a["Konto_Nr"], "01", "01", *entry, "0101" + "".join(entry), "2", "0", "100", "100", "0000"]
  86. fwh.write(";".join(row) + "\n")
  87. return None
  88. @staticmethod
  89. def format_konto(k):
  90. return "{0}-{1}-{2}-{3}-{4}-{5}".format(
  91. k["Kontonummer"], k["Marke"], k["Standort"], k["Kostenstelle"], k["Absatzkanal"], k["Kostentraeger"]
  92. )
  93. @staticmethod
  94. def format_konto_neu(k):
  95. return "{0}-{1}-{2}-{3}-{4}-{5}".format(
  96. k["Kontonummer_neu"],
  97. k["Marke_neu"],
  98. k["Standort_neu"],
  99. k["Kostenstelle_neu"],
  100. k["Absatzkanal_neu"],
  101. k["Kostentraeger_neu"],
  102. )
  103. if __name__ == "__main__":
  104. skr = skr51_validation()
  105. skr.maximum_accounts()
  106. # df = skr.check_bookings('Kontenrahmen_kombiniert.csv')
  107. # df[['Konto_Nr_Händler', 'Marke', 'Marke_valid', 'Standort', 'Standort_valid', 'Konto_Nr', 'Konto_Nr_valid',
  108. # 'Kostenstelle', 'Kostenstelle_valid', 'Absatzkanal', 'Absatzkanal_valid', 'Kostenträger', 'Kostenträger_valid',
  109. # 'Kostenträger_Ebene', 'valid']].to_csv(skr.current_dir.joinpath('SKR51_Validierung.csv'),
  110. # sep=';', encoding='latin-1', index=False)