import pandas as pd import numpy as np import xml.etree.ElementTree as ET import csv from xml.dom import minidom from datetime import datetime base_dir = '/home/robert/projekte/python/gcstruct/Siebrecht/' account_translation = base_dir + 'Kontenrahmen_kombiniert.csv' account_bookings = base_dir + 'GuV_Bilanz_Salden.csv' first_month_of_financial_year = '01' current_year = '2021' current_month = '12' account_ignored = base_dir + f'ignoriert_{current_year}-{current_month}.csv' account_invalid = base_dir + f'ungueltig_{current_year}-{current_month}.csv' last_year = str(int(current_year) - 1) next_year = str(int(current_year) + 1) def header(makes, sites): return { 'Country': 'DE', 'MainBmCode': sites[0]['Marke_HBV'], 'Month': current_month, 'Year': current_year, 'Currency': 'EUR', 'NumberOfMakes': len(makes), 'NumberOfSites': len(sites), 'ExtractionDate': datetime.now().strftime('%d.%m.%Y'), 'ExtractionTime': datetime.now().strftime('%H:%M:%S'), 'BeginFiscalYear': first_month_of_financial_year } def bookkeep_filter(): period = [current_year + str(i).zfill(2) for i in range(1, 13)] if first_month_of_financial_year != '01': if first_month_of_financial_year > current_month: period = [last_year + str(i).zfill(2) for i in range(1, 13)] + period else: period = period + [next_year + str(i).zfill(2) for i in range(1, 13)] fm = int(first_month_of_financial_year) period = period[fm - 1:fm + 12] rename_to = ['Period' + str(i).zfill(2) for i in range(1, 13)] return dict(zip(period, rename_to)) def export_month(): # Übersetzungstabelle laden df_translate = pd.read_csv(account_translation, decimal=',', sep=';', encoding='latin-1', converters={i: str for i in range(0, 200)}, index_col='Konto_Nr_Händler') # Kontensalden laden df_bookings = pd.read_csv(account_bookings, decimal=',', sep=';', encoding='latin-1', converters={0: str, 1: str}) # Kontensalden auf gegebenen Monat filtern filter_from = current_year + first_month_of_financial_year if first_month_of_financial_year > current_month: filter_from = last_year + first_month_of_financial_year filter_to = current_year + current_month df_bookings = df_bookings[(df_bookings['Bookkeep Period'] >= filter_from) & (df_bookings['Bookkeep Period'] <= filter_to)] bk_filter = bookkeep_filter() period_no = list(bk_filter.keys()).index(filter_to) + 1 # Spalten konvertieren df_bookings['period'] = df_bookings['Bookkeep Period'].apply(lambda x: bk_filter[x]) df_bookings['amount'] = df_bookings['Debit Amount'] + df_bookings['Credit Amount'] df_bookings['quantity'] = df_bookings['Debit Quantity'] + df_bookings['Credit Quantity'] # Join auf Übersetzung df_combined = df_bookings.merge(df_translate, how='inner', on='Konto_Nr_Händler') # df_combined.to_csv(account_invalid, decimal=',', sep=';', encoding='latin-1', index=False) # Gruppieren # df_grouped = df_combined.groupby(['Konto_Nr_SKR51', 'period']).sum() df_pivot = df_combined.pivot_table(index=['Konto_Nr_SKR51'], columns=['period'], values='amount', aggfunc=np.sum, margins=True, margins_name='CumulatedYear') # Infos ergänzen df_pivot['Decimals'] = 2 df_pivot['OpeningBalance'] = 0.0 df = df_translate.reset_index(drop=True).drop(columns=['Kostenträger_Ebene']).drop_duplicates() df = df.merge(df_pivot, how='inner', on='Konto_Nr_SKR51') # Bereinigung GW-Kostenträger df['GW_Verkauf_1'] = (df['Konto_Nr'].str.match(r'^[78]0')) & (df['Kostenstelle'].str.match(r'^[^1]\d')) df['Kostenstelle'] = np.where(df['GW_Verkauf_1'] == True, '11', df['Kostenstelle']) df['GW_Verkauf_2'] = (df['Konto_Nr'].str.match(r'^[78]1')) & (df['Kostenstelle'].str.match(r'^[^2]\d')) df['Kostenstelle'] = np.where(df['GW_Verkauf_2'] == True, '21', df['Kostenstelle']) df['GW_Verkauf_3'] = (df['Konto_Nr'].str.match(r'^[78]3')) & (df['Kostenstelle'].str.match(r'^[^3]\d')) df['Kostenstelle'] = np.where(df['GW_Verkauf_3'] == True, '31', df['Kostenstelle']) df['GW_Verkauf_4'] = (df['Konto_Nr'].str.match(r'^[78]4')) & (df['Kostenstelle'].str.match(r'^[^4]\d')) df['Kostenstelle'] = np.where(df['GW_Verkauf_4'] == True, '41', df['Kostenstelle']) df['GW_Verkauf_5'] = (df['Konto_Nr'].str.match(r'^[78]5')) & (df['Kostenstelle'].str.match(r'^[^5]\d')) df['Kostenstelle'] = np.where(df['GW_Verkauf_5'] == True, '51', df['Kostenstelle']) df['GW_Verkauf_50'] = (df['Konto_Nr'].str.match(r'^[78]')) & (df['Kostenstelle'].str.match(r'^2')) df['Kostenträger'] = np.where(df['GW_Verkauf_50'] == True, '52', df['Kostenträger']) df['Kostenträger'] = np.where((df['GW_Verkauf_50'] == True) & (df['Marke'] == '01'), '50', df['Kostenträger']) df['Bilanz'] = (df['Konto_Nr'].str.match(r'^[013]')) df['Kostenträger'] = np.where(df['Bilanz'] == True, '00', df['Kostenträger']) df['Konto_5er'] = (df['Konto_Nr'].str.match('^5')) df['Absatzkanal'] = np.where(df['Konto_5er'] == True, '99', df['Absatzkanal']) from_label = ['Marke', 'Standort', 'Konto_Nr', 'Kostenstelle', 'Absatzkanal', 'Kostenträger'] to_label = ['Make', 'Site', 'Account', 'Origin', 'SalesChannel', 'CostCarrier'] df = df.rename(columns=dict(zip(from_label, to_label))) df['CostAccountingString'] = df['Make'] + df['Site'] + df['Origin'] + \ df['SalesChannel'] + df['CostCarrier'] df['IsNumeric'] = df['CostAccountingString'].str.isdigit() df_invalid = df[df['IsNumeric'] == False] df_invalid.to_csv(account_invalid + '.2.csv', decimal=',', sep=';', encoding='latin-1', index=False) export_csv = base_dir + 'export_' + current_year + '-' + current_month + '.csv' df.to_csv(export_csv, decimal=',', sep=';', encoding='latin-1', index=False) export_file = export_xml(df, bk_filter, period_no) # Join auf Übersetzung - nicht zugeordnet df_ignored = df_bookings.merge(df_translate, how='left', on='Konto_Nr_Händler') df_ignored = df_ignored[df_ignored['Konto_Nr_SKR51'].isna()] # [['Konto_Nr_Händler', 'Bookkeep Period', 'amount', 'quantity']] if not df_ignored.empty: df_ignored = df_ignored.pivot_table(index=['Konto_Nr_Händler'], columns=['period'], values='amount', aggfunc=np.sum, margins=True, margins_name='CumulatedYear') df_ignored.to_csv(account_ignored, decimal=',', sep=';', encoding='latin-1') return export_file def export_xml(df: pd.DataFrame, bk_filter, period_no): df = df[df['IsNumeric']] makes = df[['Make', 'Marke_HBV']].drop_duplicates().sort_values(by=['Make']).to_dict(orient='records') sites = df[['Make', 'Site', 'Marke_HBV']].drop_duplicates().sort_values(by=['Make', 'Site']).to_dict(orient='records') res = df.to_dict(orient='records') record_elements = ['Account', 'Make', 'Site', 'Origin', 'SalesChannel', 'CostCarrier', 'CostAccountingString', 'Decimals', 'OpeningBalance'] + list(bk_filter.values())[:period_no] + ['CumulatedYear'] root = ET.Element('HbvData') h = ET.SubElement(root, 'Header') for k, v in header(makes, sites).items(): ET.SubElement(h, k).text = str(v) make_list = ET.SubElement(root, 'MakeList') for m in makes: e = ET.SubElement(make_list, 'MakeListEntry') ET.SubElement(e, 'Make').text = m['Make'] ET.SubElement(e, 'MakeCode').text = m['Marke_HBV'] bm_code_list = ET.SubElement(root, 'BmCodeList') for s in sites: e = ET.SubElement(bm_code_list, 'BmCodeEntry') ET.SubElement(e, 'Make').text = s['Make'] ET.SubElement(e, 'Site').text = s['Site'] ET.SubElement(e, 'BmCode').text = s['Marke_HBV'] record_list = ET.SubElement(root, 'RecordList') for row in res: record = ET.SubElement(record_list, 'Record') for e in record_elements: child = ET.SubElement(record, e) field = row.get(e, 0.0) if str(field) == 'nan': field = '0' elif type(field) is float: field = '{:.0f}'.format(field * 100) child.text = str(field) export_file = base_dir + 'export_' + current_year + '-' + current_month + '.xml' with open(export_file, 'w', encoding='utf-8') as f: f.write(minidom.parseString(ET.tostring(root)).toprettyxml(indent=' ')) return export_file def convert_to_row(node): return [child.text for child in node] def convert_xml_to_csv(xmlfile, csvfile): record_list = ET.parse(xmlfile).getroot().find('RecordList') header = [child.tag for child in record_list.find('Record')] bookings = [convert_to_row(node) for node in record_list.findall('Record')] with open(csvfile, 'w') as fwh: cwh = csv.writer(fwh, delimiter=';') cwh.writerow(header) cwh.writerows(bookings) return True if __name__ == '__main__': print(bookkeep_filter()) export_file = export_month() # convert_xml_to_csv(export_file, export_file[:-4] + '.csv') # convert_xml_to_csv('DE2119_2105_150621_080944.xml', 'DE2119_2105_150621_080944.csv')