fix error sync google sheet
This commit is contained in:
91
app/Console/Commands/SyncGoogleSheetData.php
Normal file
91
app/Console/Commands/SyncGoogleSheetData.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -137,8 +137,13 @@ class ServiceGoogleSheet
|
|||||||
// Count occurrences of each no_registrasi
|
// Count occurrences of each no_registrasi
|
||||||
// Filter out null values before counting to avoid array_count_values error
|
// Filter out null values before counting to avoid array_count_values error
|
||||||
$registrationNumbers = array_filter(array_column($mapUpsert, 'no_registrasi'), function($value) {
|
$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);
|
$registrasiCounts = array_count_values($registrationNumbers);
|
||||||
|
|
||||||
// Filter duplicates (those appearing more than once)
|
// Filter duplicates (those appearing more than once)
|
||||||
@@ -220,7 +225,7 @@ class ServiceGoogleSheet
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DataSetting::updateOrInsert(
|
DataSetting::updateOrCreate(
|
||||||
["key" => $key], // Find by key
|
["key" => $key], // Find by key
|
||||||
["value" => $processedValue] // Update or insert value
|
["value" => $processedValue] // Update or insert value
|
||||||
);
|
);
|
||||||
@@ -315,7 +320,7 @@ class ServiceGoogleSheet
|
|||||||
|
|
||||||
foreach ($dataSettings as $key => $value) {
|
foreach ($dataSettings as $key => $value) {
|
||||||
// Ensure value is not null before saving to database
|
// Ensure value is not null before saving to database
|
||||||
$processedValue = null;
|
$processedValue = 0; // Default to 0 instead of null
|
||||||
if ($value !== null && $value !== '') {
|
if ($value !== null && $value !== '') {
|
||||||
// Try to convert to appropriate type based on key name
|
// Try to convert to appropriate type based on key name
|
||||||
if (strpos($key, '_COUNT') !== false) {
|
if (strpos($key, '_COUNT') !== false) {
|
||||||
@@ -323,11 +328,9 @@ class ServiceGoogleSheet
|
|||||||
} else {
|
} else {
|
||||||
$processedValue = $this->convertToDecimal($value) ?? 0;
|
$processedValue = $this->convertToDecimal($value) ?? 0;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
$processedValue = 0; // Default to 0 instead of null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DataSetting::updateOrInsert(
|
DataSetting::updateOrCreate(
|
||||||
['key' => $key],
|
['key' => $key],
|
||||||
['value' => $processedValue]
|
['value' => $processedValue]
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user