sync_manager.py aktualisiert
This commit is contained in:
@@ -34,8 +34,7 @@ class SyncManager:
|
||||
"No. Service Technicians": "CRM Anzahl Techniker",
|
||||
"Annual Revenue (Mio. €)": "CRM Umsatz",
|
||||
"Number of Employees": "CRM Anzahl Mitarbeiter",
|
||||
# <<< KORREKTUR: Auf "GUID" angepasst, wie von dir angegeben.
|
||||
"GUID": "CRM ID"
|
||||
"GUID": "CRM ID" # Deine korrekte Anpassung
|
||||
}
|
||||
|
||||
self.d365_wins_cols = ["CRM Name", "Parent Account Name", "CRM Ort", "CRM Land",
|
||||
@@ -48,14 +47,17 @@ class SyncManager:
|
||||
self.logger.info(f"Lade Daten aus D365-Export: '{self.d365_export_path}'...")
|
||||
try:
|
||||
self.d365_df = pd.read_excel(self.d365_export_path, dtype=str).fillna('')
|
||||
d365_guid_col = next((k for k, v in self.d365_to_gsheet_map.items() if v == "CRM ID"), None)
|
||||
|
||||
if d365_guid_col and d365_guid_col in self.d365_df.columns:
|
||||
self.d365_df.rename(columns={d365_guid_col: "CRM ID"}, inplace=True)
|
||||
else:
|
||||
self.logger.critical(f"FEHLER: Die erwartete GUID-Spalte '{d365_guid_col}' wurde in der D365-Exportdatei nicht gefunden.")
|
||||
raise ValueError(f"GUID-Spalte ('{d365_guid_col}') nicht in D365-Export gefunden.")
|
||||
# --- NEUE, ROBUSTERE LOGIK: Spalten direkt umbenennen ---
|
||||
# Prüfen, ob alle erwarteten Spalten aus D365 vorhanden sind
|
||||
for d365_col in self.d365_to_gsheet_map.keys():
|
||||
if d365_col not in self.d365_df.columns:
|
||||
raise ValueError(f"Erwartete Spalte '{d365_col}' nicht in der D365-Exportdatei gefunden.")
|
||||
|
||||
# Alle Spalten auf einmal umbenennen.
|
||||
self.d365_df.rename(columns=self.d365_to_gsheet_map, inplace=True)
|
||||
|
||||
# Bereinige CRM IDs und entferne Zeilen ohne gültige ID
|
||||
self.d365_df['CRM ID'] = self.d365_df['CRM ID'].str.strip()
|
||||
self.d365_df.dropna(subset=['CRM ID'], inplace=True)
|
||||
self.d365_df = self.d365_df[self.d365_df['CRM ID'] != '']
|
||||
@@ -75,10 +77,16 @@ class SyncManager:
|
||||
data_rows = all_data_with_headers[self.sheet_handler._header_rows:]
|
||||
|
||||
temp_df = pd.DataFrame(data_rows)
|
||||
# Stelle sicher, dass das DataFrame die richtige Anzahl Spalten hat
|
||||
if temp_df.shape[1] < len(actual_header):
|
||||
temp_df.reindex(columns=range(len(actual_header)), fill_value='')
|
||||
temp_df.columns = actual_header
|
||||
if temp_df.empty:
|
||||
temp_df = pd.DataFrame(columns=actual_header)
|
||||
else:
|
||||
# Stelle sicher, dass die Spaltenanzahl übereinstimmt, fülle ggf. auf
|
||||
if temp_df.shape[1] < len(actual_header):
|
||||
missing_cols = len(actual_header) - temp_df.shape[1]
|
||||
for i in range(missing_cols):
|
||||
temp_df[f'temp_{i}'] = ''
|
||||
temp_df.columns = actual_header
|
||||
|
||||
temp_df = temp_df.fillna('')
|
||||
|
||||
for col_name in COLUMN_ORDER:
|
||||
@@ -106,7 +114,6 @@ class SyncManager:
|
||||
self.logger.critical("Konnte Namen des Ziel-Sheets nicht ermitteln. Abbruch.")
|
||||
return
|
||||
|
||||
# Sicherstellen, dass die 'CRM ID' Spalten für den Vergleich sauber sind
|
||||
d365_ids = set(self.d365_df['CRM ID'].dropna())
|
||||
gsheet_ids = set(self.gsheet_df[self.gsheet_df['CRM ID'] != '']['CRM ID'].dropna())
|
||||
|
||||
@@ -119,16 +126,16 @@ class SyncManager:
|
||||
updates_to_batch = []
|
||||
rows_to_append = []
|
||||
|
||||
# --- KORRIGIERTE LOGIK FÜR NEUE ACCOUNTS ---
|
||||
if new_ids:
|
||||
new_accounts_df = self.d365_df[self.d365_df['CRM ID'].isin(new_ids)]
|
||||
for _, row in new_accounts_df.iterrows():
|
||||
new_row_data = [""] * len(COLUMN_ORDER)
|
||||
for d365_col, gsheet_col in self.d365_to_gsheet_map.items():
|
||||
# Wir mappen jetzt von den Original-Spaltennamen des D365-Exports
|
||||
original_d365_col_name = next((k for k, v in self.d365_to_gsheet_map.items() if v == gsheet_col), None)
|
||||
if original_d365_col_name in row:
|
||||
# Iteriere durch die Spaltennamen, die wir aus D365 kennen (jetzt schon umbenannt)
|
||||
for gsheet_col in self.d365_to_gsheet_map.values():
|
||||
if gsheet_col in row:
|
||||
col_idx = COLUMN_MAP[gsheet_col]['index']
|
||||
new_row_data[col_idx] = row[original_d365_col_name]
|
||||
new_row_data[col_idx] = row[gsheet_col]
|
||||
rows_to_append.append(new_row_data)
|
||||
|
||||
if deleted_ids:
|
||||
@@ -136,10 +143,7 @@ class SyncManager:
|
||||
row_indices = self.gsheet_df[self.gsheet_df['CRM ID'] == crm_id].index
|
||||
if not row_indices.empty:
|
||||
row_idx = row_indices[0]
|
||||
updates_to_batch.append({
|
||||
"range": f"{COLUMN_MAP['Archiviert']['Titel']}{row_idx + 2}",
|
||||
"values": [["TRUE"]]
|
||||
})
|
||||
updates_to_batch.append({ "range": f"{COLUMN_MAP['Archiviert']['Titel']}{row_idx + 2}", "values": [["TRUE"]] })
|
||||
|
||||
if existing_ids:
|
||||
d365_indexed = self.d365_df.set_index('CRM ID')
|
||||
@@ -153,15 +157,14 @@ class SyncManager:
|
||||
conflict_messages = []
|
||||
needs_reeval = False
|
||||
|
||||
# Da die Spaltennamen jetzt identisch sind, können wir direkt vergleichen
|
||||
for gsheet_col in self.d365_wins_cols:
|
||||
d365_col = next((k for k, v in self.d365_to_gsheet_map.items() if v == gsheet_col), None)
|
||||
if d365_col and d365_col in d365_row and str(d365_row[d365_col]) != str(gsheet_row[gsheet_col]):
|
||||
row_updates[gsheet_col] = str(d365_row[d365_col])
|
||||
if str(d365_row[gsheet_col]) != str(gsheet_row[gsheet_col]):
|
||||
row_updates[gsheet_col] = str(d365_row[gsheet_col])
|
||||
needs_reeval = True
|
||||
|
||||
for gsheet_col in self.smart_merge_cols:
|
||||
d365_col = next((k for k, v in self.d365_to_gsheet_map.items() if v == gsheet_col), None)
|
||||
d365_val = str(d365_row.get(d365_col, ''))
|
||||
d365_val = str(d365_row.get(gsheet_col, ''))
|
||||
gsheet_val = str(gsheet_row.get(gsheet_col, ''))
|
||||
|
||||
if d365_val and not gsheet_val:
|
||||
@@ -170,19 +173,13 @@ class SyncManager:
|
||||
elif d365_val and gsheet_val and d365_val != gsheet_val:
|
||||
conflict_messages.append(f"{gsheet_col}_CONFLICT: D365='{d365_val}' | GSHEET='{gsheet_val}'")
|
||||
|
||||
if conflict_messages:
|
||||
row_updates["SyncConflict"] = "; ".join(conflict_messages)
|
||||
|
||||
if needs_reeval:
|
||||
row_updates["ReEval Flag"] = "x"
|
||||
if conflict_messages: row_updates["SyncConflict"] = "; ".join(conflict_messages)
|
||||
if needs_reeval: row_updates["ReEval Flag"] = "x"
|
||||
|
||||
if row_updates:
|
||||
row_idx = gsheet_indexed.index.get_loc(crm_id)
|
||||
for col_name, value in row_updates.items():
|
||||
updates_to_batch.append({
|
||||
"range": f"{COLUMN_MAP[col_name]['Titel']}{row_idx + 2}",
|
||||
"values": [[value]]
|
||||
})
|
||||
updates_to_batch.append({ "range": f"{COLUMN_MAP[col_name]['Titel']}{row_idx + 2}", "values": [[value]] })
|
||||
|
||||
if rows_to_append:
|
||||
self.logger.info(f"Füge {len(rows_to_append)} neue Zeilen zum Google Sheet hinzu...")
|
||||
|
||||
Reference in New Issue
Block a user