From a01b6f5611a3eb15745b8e9b81ab9aef924718e0 Mon Sep 17 00:00:00 2001 From: arifal Date: Tue, 24 Jun 2025 13:00:48 +0700 Subject: [PATCH] fix error sync google sheet --- app/Console/Commands/SyncGoogleSheetData.php | 91 ++++++++++++++++++++ app/Services/ServiceGoogleSheet.php | 15 ++-- 2 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 app/Console/Commands/SyncGoogleSheetData.php diff --git a/app/Console/Commands/SyncGoogleSheetData.php b/app/Console/Commands/SyncGoogleSheetData.php new file mode 100644 index 0000000..5d618fd --- /dev/null +++ b/app/Console/Commands/SyncGoogleSheetData.php @@ -0,0 +1,91 @@ +option('type'); + + $this->info('Starting Google Sheet data synchronization...'); + $this->info("Sync type: {$type}"); + + try { + $service = new ServiceGoogleSheet(); + + switch ($type) { + case 'google-sheet': + $this->info('Syncing Google Sheet data...'); + $service->sync_google_sheet_data(); + $this->info('✅ Google Sheet data synchronized successfully!'); + break; + + case 'big-data': + $this->info('Syncing Big Data...'); + $service->sync_big_data(); + $this->info('✅ Big Data synchronized successfully!'); + break; + + case 'leader': + $this->info('Syncing Leader data...'); + $result = $service->sync_leader_data(); + $this->info('✅ Leader data synchronized successfully!'); + $this->table(['Section', 'Total', 'Nominal'], collect($result)->map(function($item, $key) { + return [ + $key, + $item['total'] ?? 'N/A', + number_format($item['nominal'] ?? 0, 0, ',', '.') + ]; + })->toArray()); + break; + + case 'all': + default: + $this->info('Syncing all data (Google Sheet + Big Data)...'); + $service->run_service(); + $this->info('✅ All data synchronized successfully!'); + break; + } + + $this->newLine(); + $this->info('🚀 Synchronization completed successfully!'); + + } catch (Exception $e) { + $this->error('❌ Synchronization failed!'); + $this->error("Error: {$e->getMessage()}"); + + Log::error('Google Sheet sync command failed', [ + 'error' => $e->getMessage(), + 'type' => $type, + 'trace' => $e->getTraceAsString() + ]); + + return Command::FAILURE; + } + + return Command::SUCCESS; + } +} \ No newline at end of file diff --git a/app/Services/ServiceGoogleSheet.php b/app/Services/ServiceGoogleSheet.php index 039a44e..12a8a38 100644 --- a/app/Services/ServiceGoogleSheet.php +++ b/app/Services/ServiceGoogleSheet.php @@ -137,8 +137,13 @@ class ServiceGoogleSheet // Count occurrences of each no_registrasi // Filter out null values before counting to avoid array_count_values error $registrationNumbers = array_filter(array_column($mapUpsert, 'no_registrasi'), function($value) { - return $value !== null && $value !== ''; + // Ensure only string and integer values are counted + return $value !== null && $value !== '' && (is_string($value) || is_int($value)); }); + + // Additional safety check: convert all values to strings + $registrationNumbers = array_map('strval', $registrationNumbers); + $registrasiCounts = array_count_values($registrationNumbers); // Filter duplicates (those appearing more than once) @@ -220,7 +225,7 @@ class ServiceGoogleSheet } } - DataSetting::updateOrInsert( + DataSetting::updateOrCreate( ["key" => $key], // Find by key ["value" => $processedValue] // Update or insert value ); @@ -315,7 +320,7 @@ class ServiceGoogleSheet foreach ($dataSettings as $key => $value) { // Ensure value is not null before saving to database - $processedValue = null; + $processedValue = 0; // Default to 0 instead of null if ($value !== null && $value !== '') { // Try to convert to appropriate type based on key name if (strpos($key, '_COUNT') !== false) { @@ -323,11 +328,9 @@ class ServiceGoogleSheet } else { $processedValue = $this->convertToDecimal($value) ?? 0; } - } else { - $processedValue = 0; // Default to 0 instead of null } - DataSetting::updateOrInsert( + DataSetting::updateOrCreate( ['key' => $key], ['value' => $processedValue] );