|  | @@ -7,39 +7,45 @@ from pathlib import Path
 | 
	
		
			
				|  |  |  import pandas as pd
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  from gchr.gchr_bookings import GchrBookings
 | 
	
		
			
				|  |  | -from gchr.gchr_export import GchrExportFormat, get_export_fn
 | 
	
		
			
				|  |  | +from gchr.gchr_export import get_export_fn
 | 
	
		
			
				|  |  |  from gchr.gchr_model import ACCOUNT_INFO, GchrConfig, GchrExportConfig
 | 
	
		
			
				|  |  |  from gchr.gchr_translate import load_translation
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  class GCHR:
 | 
	
		
			
				|  |  |      cfg: GchrConfig
 | 
	
		
			
				|  |  | -    booking_date: datetime
 | 
	
		
			
				|  |  |      bookings: GchrBookings
 | 
	
		
			
				|  |  |      _df_translate: pd.DataFrame = None
 | 
	
		
			
				|  |  |      df_translate2: pd.DataFrame = None
 | 
	
		
			
				|  |  |      makes: dict[str, str] = None
 | 
	
		
			
				|  |  |      sites: dict[str, str] = None
 | 
	
		
			
				|  |  | -    current_year: str
 | 
	
		
			
				|  |  | -    current_month: str
 | 
	
		
			
				|  |  |      timestamp: str
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      def __init__(self, base_dir: str) -> None:
 | 
	
		
			
				|  |  |          self.base_dir = base_dir
 | 
	
		
			
				|  |  | -        cfg_file = self.base_dir + "\\config\\gchr2.json"
 | 
	
		
			
				|  |  | +        cfg_file = f"{self.base_dir}\\config\\gchr2.json"
 | 
	
		
			
				|  |  |          if Path(cfg_file).exists():
 | 
	
		
			
				|  |  |              with open(cfg_file, "r") as frh:
 | 
	
		
			
				|  |  |                  self.cfg = GchrConfig(**json.load(frh))
 | 
	
		
			
				|  |  |          else:
 | 
	
		
			
				|  |  | -            self.cfg = GchrConfig()
 | 
	
		
			
				|  |  | -        os.makedirs(self.base_dir + "/data", exist_ok=True)
 | 
	
		
			
				|  |  | -        os.makedirs(self.base_dir + "/export/temp", exist_ok=True)
 | 
	
		
			
				|  |  | -        os.makedirs(self.base_dir + "/logs", exist_ok=True)
 | 
	
		
			
				|  |  | +            os.makedirs(f"{self.base_dir}\\config", exist_ok=True)
 | 
	
		
			
				|  |  | +            self.cfg = GchrConfig(
 | 
	
		
			
				|  |  | +                first_month_of_financial_year="01",
 | 
	
		
			
				|  |  | +                data_dir=f"{self.base_dir}\\data",
 | 
	
		
			
				|  |  | +                gcstruct_dir=f"{self.base_dir}\\..\\GCStruct_Aufbereitung",
 | 
	
		
			
				|  |  | +                export_dir=f"{self.base_dir}\\Export",
 | 
	
		
			
				|  |  | +                export_format="SKR51",
 | 
	
		
			
				|  |  | +            )
 | 
	
		
			
				|  |  | +            with open(cfg_file, "w") as fwh:
 | 
	
		
			
				|  |  | +                json.dump(self.cfg.__dict__, fwh, indent=2)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        os.makedirs(self.cfg.data_dir, exist_ok=True)
 | 
	
		
			
				|  |  | +        os.makedirs(f"{self.cfg.export_dir}\\temp", exist_ok=True)
 | 
	
		
			
				|  |  | +        os.makedirs(f"{self.base_dir}\\logs", exist_ok=True)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        self.account_translation = f"{self.base_dir}/data/Kontenrahmen_uebersetzt.csv"
 | 
	
		
			
				|  |  | +        self.account_translation = f"{self.cfg.data_dir}\\Kontenrahmen_uebersetzt.csv"
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        self.first_month_of_financial_year = "10"
 | 
	
		
			
				|  |  | -        self.bookings = GchrBookings(self.base_dir, self.first_month_of_financial_year)
 | 
	
		
			
				|  |  | +        self.bookings = GchrBookings(self.base_dir, self.cfg.first_month_of_financial_year)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          self.timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -48,13 +54,13 @@ class GCHR:
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @property
 | 
	
		
			
				|  |  |      def debug_file(self) -> str:
 | 
	
		
			
				|  |  | -        return f"{self.logs_dir}/debug_{self.timestamp}.csv"
 | 
	
		
			
				|  |  | +        return f"{self.logs_dir}\\debug_{self.timestamp}.csv"
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @property
 | 
	
		
			
				|  |  |      def account_ignored(self) -> str:
 | 
	
		
			
				|  |  | -        return f"{self.export_info_dir}/ignoriert_{self.period}.csv"
 | 
	
		
			
				|  |  | +        return f"{self.export_info_dir}\\ignoriert_{self.bookings.period}.csv"
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    # self.account_invalid = f"{self.export_info_dir}/ungueltig_{self.period}.csv"
 | 
	
		
			
				|  |  | +    # self.account_invalid = f"{self.export_info_dir}\\ungueltig_{self.period}.csv"
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      def export_all_periods(self, overwrite=False, today=None) -> None:
 | 
	
		
			
				|  |  |          periods = GCHR.get_all_periods(today)
 | 
	
	
		
			
				|  | @@ -77,13 +83,13 @@ class GCHR:
 | 
	
		
			
				|  |  |          return periods
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      def export_period(self, year: str, month: str) -> str:
 | 
	
		
			
				|  |  | -        export_fn = get_export_fn(GchrExportFormat.SKR51)
 | 
	
		
			
				|  |  | +        export_fn = get_export_fn(self.cfg.export_format)
 | 
	
		
			
				|  |  |          # Kontensalden laden
 | 
	
		
			
				|  |  |          df_bookings = self.bookings.filter_bookings(year, month)
 | 
	
		
			
				|  |  |          all_periods = set(df_bookings["Bookkeep Period"].to_list())
 | 
	
		
			
				|  |  |          bookkeep_period_date = datetime(int(year), int(month), 28)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        if df_bookings.shape[0] == 0 or len(all_periods) <= 1 or self.booking_date < bookkeep_period_date:
 | 
	
		
			
				|  |  | +        if df_bookings.shape[0] == 0 or len(all_periods) <= 1 or self.bookings.booking_date < bookkeep_period_date:
 | 
	
		
			
				|  |  |              logging.error("ABBRUCH!!! Keine Daten vorhanden!")
 | 
	
		
			
				|  |  |              return False
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -92,7 +98,7 @@ class GCHR:
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          logging.info("df_bookings: " + str(df_bookings.shape))
 | 
	
		
			
				|  |  |          # Join auf Übersetzung
 | 
	
		
			
				|  |  | -        df_combined = df_bookings.merge(self._df_translate, how="inner", on="Konto_Nr_Händler")
 | 
	
		
			
				|  |  | +        df_combined = df_bookings.merge(self.df_translate, how="inner", on="Konto_Nr_Händler")
 | 
	
		
			
				|  |  |          logging.info(f"df_combined: {df_combined.shape}")
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          df_pivot = df_combined.pivot_table(
 | 
	
	
		
			
				|  | @@ -141,16 +147,17 @@ class GCHR:
 | 
	
		
			
				|  |  |              if i > 0:
 | 
	
		
			
				|  |  |                  filename = f"{filename[:-4]}_{main_site}.xml"
 | 
	
		
			
				|  |  |              export_cfg = GchrExportConfig(
 | 
	
		
			
				|  |  | -                main_site,
 | 
	
		
			
				|  |  | -                year,
 | 
	
		
			
				|  |  | -                month,
 | 
	
		
			
				|  |  | -                makes_used,
 | 
	
		
			
				|  |  | -                sites_used,
 | 
	
		
			
				|  |  | -                self.first_month_of_financial_year,
 | 
	
		
			
				|  |  | -                period_no,
 | 
	
		
			
				|  |  | -                self.bookings.bookkeep_filter,
 | 
	
		
			
				|  |  | -                filename,
 | 
	
		
			
				|  |  | -                df.to_dict(orient="records"),
 | 
	
		
			
				|  |  | +                main_site=main_site,
 | 
	
		
			
				|  |  | +                current_year=year,
 | 
	
		
			
				|  |  | +                current_month=month,
 | 
	
		
			
				|  |  | +                makes_used=makes_used,
 | 
	
		
			
				|  |  | +                sites_used=sites_used,
 | 
	
		
			
				|  |  | +                first_month=self.cfg.first_month_of_financial_year,
 | 
	
		
			
				|  |  | +                period_no=period_no,
 | 
	
		
			
				|  |  | +                bookkeep_filter=self.bookings.bookkeep_filter,
 | 
	
		
			
				|  |  | +                extraction_date=self.bookings.booking_date,
 | 
	
		
			
				|  |  | +                export_file=filename,
 | 
	
		
			
				|  |  | +                bookkeep_records=df.to_dict(orient="records"),
 | 
	
		
			
				|  |  |              )
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              export_fn(export_cfg)
 | 
	
	
		
			
				|  | @@ -180,25 +187,15 @@ class GCHR:
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @property
 | 
	
		
			
				|  |  |      def export_info_dir(self) -> str:
 | 
	
		
			
				|  |  | -        return f"{self.base_dir}/Export/{self.current_year}/info/"
 | 
	
		
			
				|  |  | +        return f"{self.cfg.export_dir}\\{self.bookings.current_year}\\info\\"
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @property
 | 
	
		
			
				|  |  |      def logs_dir(self) -> str:
 | 
	
		
			
				|  |  | -        return f"{self.base_dir}/Logs/"
 | 
	
		
			
				|  |  | +        return f"{self.base_dir}\\Logs\\"
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @property
 | 
	
		
			
				|  |  |      def export_invalid_filename(self) -> str:
 | 
	
		
			
				|  |  | -        return f"{self.base_dir}/Export/ungueltig.csv"
 | 
	
		
			
				|  |  | +        return f"{self.cfg.export_dir}\\ungueltig.csv"
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      def export_filename_for_period(self, year: str, month: str) -> str:
 | 
	
		
			
				|  |  | -        return f"{self.base_dir}/Export/{year}/export_{year}-{month}.xml"
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -def gchr_local() -> None:
 | 
	
		
			
				|  |  | -    base_dir = os.getcwd() + "/../GCHR2_Testdaten/Kunden"
 | 
	
		
			
				|  |  | -    for path in Path(base_dir).glob("*"):
 | 
	
		
			
				|  |  | -        if not path.is_dir():
 | 
	
		
			
				|  |  | -            continue
 | 
	
		
			
				|  |  | -        print(path.name)
 | 
	
		
			
				|  |  | -        gchr = GCHR(str(path))
 | 
	
		
			
				|  |  | -        gchr.export_all_periods()
 | 
	
		
			
				|  |  | +        return f"{self.cfg.export_dir}\\{year}\\export_{year}-{month}.xml"
 |