import pandas as pd
from functools import reduce

# source_csv = 'data\\Kontenrahmen_Import.csv'
# target_csv = 'data\\Kontenrahmen.csv'
# result_csv = 'data\\Kontenrahmen_Ergebnis.csv'
# source_csv = 'P:\\SKR51_GCStruct\\Kontenrahmen_Vorlage.csv'
# target_csv = 'P:\\SKR51_GCStruct\\GCStruct_Portal\\Kontenrahmen\\Kontenrahmen.csv'
# result_csv = 'P:\\SKR51_GCStruct\\GCStruct_Portal\\Kontenrahmen\\Kontenrahmen_neu.csv'
base_dir = 'V:\\Kunden\\Luchtenberg\\1 Umstellung SKR51\\GCStruct_Modell\\Kontenrahmen\\'
source_csv = base_dir + 'Kontenrahmen_Vorlage.csv'
target_csv = base_dir + 'Kontenrahmen.csv'
result_csv = base_dir + 'Kontenrahmen_neu.csv'

debug = False
cols_pkey = ['Konto_Nr']


def update(d, other):
    d.update(dict(dict(other)))
    return d


def get_dict(cols, type):
    return dict(dict(zip(cols, [type] * len(cols))))

# cols_dict = reduce(update, (get_dict(cols_pkey, np.str), get_dict(cols_str, np.str), get_dict(cols_float, np.float)), {})


df_source = pd.read_csv(source_csv, decimal=',', sep=';', encoding='latin-1', converters={i: str for i in range(0, 200)})
df_source['pkey'] = reduce(lambda x, y: x + '_' + df_source[y], cols_pkey, '')
df_source = df_source.set_index('pkey')

df_target = pd.read_csv(target_csv, decimal=',', sep=';', encoding='latin-1', converters={i: str for i in range(0, 200)})
target_columns = df_target.columns
df_target['pkey'] = reduce(lambda x, y: x + '_' + df_target[y], cols_pkey, '')
df_target = df_target.set_index('pkey')

df_join = df_source.join(df_target, how='left', rsuffix='_other')[target_columns]
df_remain = df_target.join(df_source, how='left', rsuffix='_other')
df_remain = df_remain[pd.isna(df_remain[cols_pkey[0] + '_other'])][target_columns]
# df_result = df_join[(df_join['Gesamt'] != 0) & (df_join['Serviceberater'] != "")]

# veraltete Konten nicht löschen
df_join = df_join.append(df_remain).sort_index()

df_join.to_csv(result_csv, decimal=',', sep=';', encoding='latin-1', index=None)
