fix error sync google sheet

This commit is contained in:
arifal
2025-06-24 13:00:48 +07:00
parent 2f43ebe97e
commit a01b6f5611
2 changed files with 100 additions and 6 deletions

View File

@@ -0,0 +1,91 @@
<?php
namespace App\Console\Commands;
use App\Services\ServiceGoogleSheet;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Exception;
class SyncGoogleSheetData extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sync:google-sheet {--type=all : Specify sync type (all, google-sheet, big-data, leader)}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Sync data from Google Sheets to database';
/**
* Execute the console command.
*/
public function handle()
{
$type = $this->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;
}
}

View File

@@ -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]
);