sync_manager.py aktualisiert
This commit is contained in:
@@ -269,7 +269,11 @@ class SyncManager:
|
|||||||
self.logger.info("Archivierungs-Schritt wird übersprungen (Teil-Export angenommen).")
|
self.logger.info("Archivierungs-Schritt wird übersprungen (Teil-Export angenommen).")
|
||||||
existing_ids = d365_ids.intersection(gsheet_ids)
|
existing_ids = d365_ids.intersection(gsheet_ids)
|
||||||
|
|
||||||
self.logger.info(f"Sync-Analyse: {len(new_ids)} neue, {len(deleted_ids)} zu archivierende, {len(existing_ids)} bestehende Accounts.")
|
# Statistik befüllen
|
||||||
|
self.stats.new_accounts = len(new_ids)
|
||||||
|
self.stats.archived_accounts = len(deleted_ids)
|
||||||
|
self.stats.existing_accounts = len(existing_ids)
|
||||||
|
self.logger.info(f"Sync-Analyse: {self.stats.new_accounts} neue, {self.stats.archived_accounts} zu archivierende, {self.stats.existing_accounts} bestehende Accounts.")
|
||||||
|
|
||||||
updates_to_batch, rows_to_append = [], []
|
updates_to_batch, rows_to_append = [], []
|
||||||
|
|
||||||
@@ -285,8 +289,6 @@ class SyncManager:
|
|||||||
|
|
||||||
if existing_ids:
|
if existing_ids:
|
||||||
d365_indexed = self.d365_df.set_index('CRM ID')
|
d365_indexed = self.d365_df.set_index('CRM ID')
|
||||||
|
|
||||||
# --- KORREKTE DATENQUELLE VERWENDEN ---
|
|
||||||
gsheet_to_update_df = self.gsheet_df[self.gsheet_df['CRM ID'].isin(existing_ids)]
|
gsheet_to_update_df = self.gsheet_df[self.gsheet_df['CRM ID'].isin(existing_ids)]
|
||||||
|
|
||||||
for original_row_index, gsheet_row in gsheet_to_update_df.iterrows():
|
for original_row_index, gsheet_row in gsheet_to_update_df.iterrows():
|
||||||
@@ -299,54 +301,53 @@ class SyncManager:
|
|||||||
for gsheet_col in self.d365_wins_cols:
|
for gsheet_col in self.d365_wins_cols:
|
||||||
d365_val = str(d365_row[gsheet_col]).strip()
|
d365_val = str(d365_row[gsheet_col]).strip()
|
||||||
gsheet_val = str(gsheet_row[gsheet_col]).strip()
|
gsheet_val = str(gsheet_row[gsheet_col]).strip()
|
||||||
|
|
||||||
trigger_update = False
|
trigger_update = False
|
||||||
|
|
||||||
if gsheet_col == 'CRM Land':
|
if gsheet_col == 'CRM Land':
|
||||||
d365_code_lower = d365_val.lower()
|
d365_code_lower, gsheet_val_lower = d365_val.lower(), gsheet_val.lower()
|
||||||
gsheet_val_lower = gsheet_val.lower()
|
|
||||||
d365_translated_lower = Config.COUNTRY_CODE_MAP.get(d365_code_lower, d365_code_lower).lower()
|
d365_translated_lower = Config.COUNTRY_CODE_MAP.get(d365_code_lower, d365_code_lower).lower()
|
||||||
if gsheet_val_lower != d365_code_lower and gsheet_val_lower != d365_translated_lower:
|
if gsheet_val_lower != d365_code_lower and gsheet_val_lower != d365_translated_lower:
|
||||||
trigger_update = True
|
trigger_update = True
|
||||||
|
|
||||||
elif gsheet_col == 'CRM Anzahl Techniker':
|
elif gsheet_col == 'CRM Anzahl Techniker':
|
||||||
if (d365_val == '-1' or d365_val == '0') and gsheet_val == '': pass
|
if (d365_val == '-1' or d365_val == '0') and gsheet_val == '': pass
|
||||||
elif d365_val != gsheet_val: trigger_update = True
|
elif d365_val != gsheet_val: trigger_update = True
|
||||||
|
|
||||||
elif gsheet_col == 'CRM Branche':
|
elif gsheet_col == 'CRM Branche':
|
||||||
if gsheet_row['Chat Vorschlag Branche'] == '' and d365_val != gsheet_val:
|
if gsheet_row['Chat Vorschlag Branche'] == '' and d365_val != gsheet_val:
|
||||||
trigger_update = True
|
trigger_update = True
|
||||||
|
|
||||||
elif gsheet_col == 'CRM Umsatz':
|
elif gsheet_col == 'CRM Umsatz':
|
||||||
if gsheet_row['Wiki Umsatz'] == '' and d365_val != gsheet_val:
|
if gsheet_row['Wiki Umsatz'] == '' and d365_val != gsheet_val:
|
||||||
trigger_update = True
|
trigger_update = True
|
||||||
|
|
||||||
elif gsheet_col == 'CRM Anzahl Mitarbeiter':
|
elif gsheet_col == 'CRM Anzahl Mitarbeiter':
|
||||||
if gsheet_row['Wiki Mitarbeiter'] == '' and d365_val != gsheet_val:
|
if gsheet_row['Wiki Mitarbeiter'] == '' and d365_val != gsheet_val:
|
||||||
trigger_update = True
|
trigger_update = True
|
||||||
|
elif gsheet_col == 'CRM Beschreibung':
|
||||||
|
if gsheet_row['Website Zusammenfassung'] == '' and d365_val != gsheet_val:
|
||||||
|
trigger_update = True
|
||||||
else:
|
else:
|
||||||
if d365_val != gsheet_val: trigger_update = True
|
if d365_val != gsheet_val: trigger_update = True
|
||||||
|
|
||||||
if trigger_update:
|
if trigger_update:
|
||||||
row_updates[gsheet_col] = d365_val
|
row_updates[gsheet_col] = d365_val; needs_reeval = True
|
||||||
needs_reeval = True
|
self.logger.debug(f"Update für {crm_id} durch '{gsheet_col}': D365='{d365_val}' | GSheet='{gsheet_val}'")
|
||||||
self.logger.debug(f"ReEval für {crm_id} durch '{gsheet_col}': D365='{d365_val}' | GSheet='{gsheet_val}'")
|
|
||||||
|
|
||||||
for gsheet_col in self.smart_merge_cols:
|
for gsheet_col in self.smart_merge_cols:
|
||||||
d365_val = str(d365_row.get(gsheet_col, '')).strip()
|
d365_val = str(d365_row.get(gsheet_col, '')).strip()
|
||||||
gsheet_val = str(gsheet_row.get(gsheet_col, '')).strip()
|
gsheet_val = str(gsheet_row.get(gsheet_col, '')).strip()
|
||||||
|
|
||||||
if d365_val and not gsheet_val:
|
if d365_val and not gsheet_val:
|
||||||
row_updates[gsheet_col] = d365_val
|
row_updates[gsheet_col] = d365_val; needs_reeval = True
|
||||||
needs_reeval = True
|
|
||||||
elif d365_val and gsheet_val and d365_val != gsheet_val:
|
elif d365_val and gsheet_val and d365_val != gsheet_val:
|
||||||
conflict_messages.append(f"{gsheet_col}_CONFLICT: D365='{d365_val}' | GSHEET='{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 conflict_messages:
|
||||||
|
row_updates["SyncConflict"] = "; ".join(conflict_messages)
|
||||||
|
self.stats.conflict_accounts.add(crm_id)
|
||||||
|
for msg in conflict_messages: self.stats.field_conflicts[msg.split('_CONFLICT')[0]] += 1
|
||||||
|
|
||||||
if needs_reeval: row_updates["ReEval Flag"] = "x"
|
if needs_reeval: row_updates["ReEval Flag"] = "x"
|
||||||
|
|
||||||
if row_updates:
|
if row_updates:
|
||||||
|
self.stats.accounts_to_update.add(crm_id)
|
||||||
|
for field in row_updates.keys(): self.stats.field_updates[field] += 1
|
||||||
sheet_row_number = original_row_index + self.sheet_handler._header_rows + 1
|
sheet_row_number = original_row_index + self.sheet_handler._header_rows + 1
|
||||||
for col_name, value in row_updates.items():
|
for col_name, value in row_updates.items():
|
||||||
updates_to_batch.append({ "range": f"{COLUMN_MAP[col_name]['Titel']}{sheet_row_number}", "values": [[value]] })
|
updates_to_batch.append({ "range": f"{COLUMN_MAP[col_name]['Titel']}{sheet_row_number}", "values": [[value]] })
|
||||||
@@ -359,8 +360,11 @@ class SyncManager:
|
|||||||
self.logger.info(f"Sende {len(updates_to_batch)} Zell-Updates an das Google Sheet...")
|
self.logger.info(f"Sende {len(updates_to_batch)} Zell-Updates an das Google Sheet...")
|
||||||
self.sheet_handler.batch_update_cells(updates_to_batch)
|
self.sheet_handler.batch_update_cells(updates_to_batch)
|
||||||
|
|
||||||
if not rows_to_append and not updates_to_batch:
|
# --- WIEDERHERGESTELLTER STATISTIK-BLOCK ---
|
||||||
self.logger.info("Keine Änderungen festgestellt. Das Google Sheet ist bereits auf dem neuesten Stand.")
|
report = self.stats.generate_report()
|
||||||
|
self.logger.info(report)
|
||||||
|
print(report)
|
||||||
|
# --- ENDE STATISTIK-BLOCK ---
|
||||||
|
|
||||||
self.logger.info("Synchronisation erfolgreich abgeschlossen.")
|
self.logger.info("Synchronisation erfolgreich abgeschlossen.")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user