From f5c6e87ae3d6562ea6ed60b979f653fe1d646257 Mon Sep 17 00:00:00 2001 From: Floke Date: Thu, 28 Aug 2025 19:02:12 +0000 Subject: [PATCH] sync_manager.py aktualisiert --- sync_manager.py | 56 ++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/sync_manager.py b/sync_manager.py index c92dbcd4..4676fd73 100644 --- a/sync_manager.py +++ b/sync_manager.py @@ -265,14 +265,18 @@ class SyncManager: gsheet_ids = set(self.gsheet_df['CRM ID'].dropna()) new_ids = d365_ids - gsheet_ids - deleted_ids = set() + deleted_ids = set() self.logger.info("Archivierungs-Schritt wird übersprungen (Teil-Export angenommen).") 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 = [], [] - + if new_ids: new_accounts_df = self.d365_df[self.d365_df['CRM ID'].isin(new_ids)] for _, row in new_accounts_df.iterrows(): @@ -285,8 +289,6 @@ class SyncManager: if existing_ids: 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)] for original_row_index, gsheet_row in gsheet_to_update_df.iterrows(): @@ -299,58 +301,57 @@ class SyncManager: for gsheet_col in self.d365_wins_cols: d365_val = str(d365_row[gsheet_col]).strip() gsheet_val = str(gsheet_row[gsheet_col]).strip() - trigger_update = False if gsheet_col == 'CRM Land': - d365_code_lower = d365_val.lower() - gsheet_val_lower = gsheet_val.lower() + d365_code_lower, gsheet_val_lower = d365_val.lower(), gsheet_val.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: trigger_update = True - elif gsheet_col == 'CRM Anzahl Techniker': if (d365_val == '-1' or d365_val == '0') and gsheet_val == '': pass elif d365_val != gsheet_val: trigger_update = True - elif gsheet_col == 'CRM Branche': if gsheet_row['Chat Vorschlag Branche'] == '' and d365_val != gsheet_val: trigger_update = True - elif gsheet_col == 'CRM Umsatz': if gsheet_row['Wiki Umsatz'] == '' and d365_val != gsheet_val: trigger_update = True - elif gsheet_col == 'CRM Anzahl Mitarbeiter': if gsheet_row['Wiki Mitarbeiter'] == '' and d365_val != gsheet_val: trigger_update = True - + elif gsheet_col == 'CRM Beschreibung': + if gsheet_row['Website Zusammenfassung'] == '' and d365_val != gsheet_val: + trigger_update = True else: if d365_val != gsheet_val: trigger_update = True - + if trigger_update: - row_updates[gsheet_col] = d365_val - needs_reeval = True - self.logger.debug(f"ReEval für {crm_id} durch '{gsheet_col}': D365='{d365_val}' | GSheet='{gsheet_val}'") - + row_updates[gsheet_col] = d365_val; needs_reeval = True + self.logger.debug(f"Update für {crm_id} durch '{gsheet_col}': D365='{d365_val}' | GSheet='{gsheet_val}'") + for gsheet_col in self.smart_merge_cols: d365_val = str(d365_row.get(gsheet_col, '')).strip() gsheet_val = str(gsheet_row.get(gsheet_col, '')).strip() - if d365_val and not gsheet_val: - row_updates[gsheet_col] = d365_val - needs_reeval = True + row_updates[gsheet_col] = d365_val; needs_reeval = True 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 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 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 for col_name, value in row_updates.items(): updates_to_batch.append({ "range": f"{COLUMN_MAP[col_name]['Titel']}{sheet_row_number}", "values": [[value]] }) - + if rows_to_append: self.logger.info(f"Füge {len(rows_to_append)} neue Zeilen zum Google Sheet hinzu...") self.sheet_handler.append_rows(sheet_name=self.target_sheet_name, values=rows_to_append) @@ -359,8 +360,11 @@ class SyncManager: self.logger.info(f"Sende {len(updates_to_batch)} Zell-Updates an das Google Sheet...") self.sheet_handler.batch_update_cells(updates_to_batch) - if not rows_to_append and not updates_to_batch: - self.logger.info("Keine Änderungen festgestellt. Das Google Sheet ist bereits auf dem neuesten Stand.") + # --- WIEDERHERGESTELLTER STATISTIK-BLOCK --- + report = self.stats.generate_report() + self.logger.info(report) + print(report) + # --- ENDE STATISTIK-BLOCK --- self.logger.info("Synchronisation erfolgreich abgeschlossen.")