This commit is contained in:
arifal
2025-07-03 15:28:54 +07:00
parent a7f578ca3d
commit a1e302a56d
4 changed files with 134 additions and 15 deletions

View File

@@ -20,7 +20,9 @@ class InjectSpatialPlanningsData extends Command
{--file=storage/app/public/templates/2025.xlsx : Path to Excel file}
{--sheet=0 : Sheet index to read from}
{--dry-run : Run without actually inserting data}
{--debug : Show Excel content for debugging}';
{--debug : Show Excel content for debugging}
{--truncate : Clear existing data before import}
{--no-truncate : Skip truncation (keep existing data)}';
/**
* The console command description.
@@ -39,6 +41,8 @@ class InjectSpatialPlanningsData extends Command
$sheetIndex = (int) $this->option('sheet');
$isDryRun = $this->option('dry-run');
$isDebug = $this->option('debug');
$shouldTruncate = $this->option('truncate');
$noTruncate = $this->option('no-truncate');
if (!file_exists($filePath)) {
$this->error("File not found: {$filePath}");
@@ -52,10 +56,86 @@ class InjectSpatialPlanningsData extends Command
$this->warn("DRY RUN MODE - No data will be inserted");
}
DB::statement('SET FOREIGN_KEY_CHECKS = 0');
DB::table('spatial_plannings')->truncate();
DB::statement('SET FOREIGN_KEY_CHECKS = 1');
$this->info('Spatial plannings table truncated successfully.');
// Check existing data
$existingCount = DB::table('spatial_plannings')->count();
if ($existingCount > 0) {
$this->info("Found {$existingCount} existing spatial planning records");
} else {
$this->info('No existing spatial planning data found');
}
// Handle truncation logic
$willTruncate = false;
if ($shouldTruncate) {
$willTruncate = true;
$this->info('Truncation requested via --truncate option');
} elseif ($noTruncate) {
$willTruncate = false;
$this->info('Truncation skipped via --no-truncate option');
} else {
// Default behavior: ask user if not in dry run mode
if (!$isDryRun) {
$willTruncate = $this->confirm('Do you want to clear existing spatial planning data before import?');
} else {
$willTruncate = false;
$this->info('DRY RUN MODE - Truncation will be skipped');
}
}
// Confirm truncation if not in dry run mode and truncation is requested
if ($willTruncate && !$isDryRun) {
if (!$this->confirm('This will delete all existing spatial planning data and related retribution calculations. Continue?')) {
$this->info('Operation cancelled.');
return 0;
}
}
// Truncate all related data properly
if ($willTruncate && !$isDryRun) {
$this->info('Truncating spatial planning data and related retribution calculations...');
try {
// Disable foreign key checks for safe truncation
DB::statement('SET FOREIGN_KEY_CHECKS = 0');
// 1. Delete calculable retributions for spatial plannings (polymorphic relationship)
$deletedCalculableRetributions = DB::table('calculable_retributions')
->where('calculable_type', 'App\\Models\\SpatialPlanning')
->count();
if ($deletedCalculableRetributions > 0) {
DB::table('calculable_retributions')
->where('calculable_type', 'App\\Models\\SpatialPlanning')
->delete();
$this->info("Deleted {$deletedCalculableRetributions} calculable retributions for spatial plannings.");
}
// 2. Truncate spatial plannings table
DB::table('spatial_plannings')->truncate();
$this->info('Spatial plannings table truncated successfully.');
// Re-enable foreign key checks
DB::statement('SET FOREIGN_KEY_CHECKS = 1');
$this->info('All spatial planning data and related retribution calculations cleared successfully.');
} catch (Exception $e) {
// Make sure to re-enable foreign key checks even on error
try {
DB::statement('SET FOREIGN_KEY_CHECKS = 1');
} catch (Exception $fkError) {
$this->error('Failed to re-enable foreign key checks: ' . $fkError->getMessage());
}
$this->error('Failed to truncate spatial planning data: ' . $e->getMessage());
return 1;
}
} elseif ($willTruncate && $isDryRun) {
$this->info('DRY RUN MODE - Would truncate spatial planning data and related retribution calculations');
} else {
$this->info('Keeping existing data (no truncation)');
}
$spreadsheet = IOFactory::load($filePath);
$worksheet = $spreadsheet->getSheet($sheetIndex);
@@ -97,8 +177,23 @@ class InjectSpatialPlanningsData extends Command
if (!$isDryRun) {
$this->info("Successfully inserted {$totalInserted} spatial planning records");
// Show summary of what was done
$finalCount = DB::table('spatial_plannings')->count();
$this->info("Final spatial planning records count: {$finalCount}");
if ($willTruncate) {
$this->info("✅ Data import completed with truncation");
} else {
$this->info("✅ Data import completed (existing data preserved)");
}
} else {
$this->info("Dry run completed. Total records that would be inserted: " . count($sections));
if ($willTruncate) {
$this->info("Would truncate existing data before import");
} else {
$this->info("Would preserve existing data during import");
}
}
return 0;
@@ -574,8 +669,6 @@ class InjectSpatialPlanningsData extends Command
strpos($activitiesLower, 'perdaganagan') !== false ||
strpos($activitiesLower, 'waterpark') !== false ||
strpos($activitiesLower, 'pasar') !== false ||
strpos($activitiesLower, 'perumahan') !== false ||
strpos($activitiesLower, 'perumhan') !== false ||
strpos($activitiesLower, 'kantor') !== false) {
$buildingFunction = 'Fungsi Usaha';
@@ -601,6 +694,8 @@ class InjectSpatialPlanningsData extends Command
// Determine housing type based on area and keywords
if (strpos($activitiesLower, 'mbr') !== false ||
strpos($activitiesLower, 'masyarakat berpenghasilan rendah') !== false ||
strpos($activitiesLower, 'perumahan') !== false ||
strpos($activitiesLower, 'perumhan') !== false ||
strpos($activitiesLower, 'sederhana') !== false ||
($landArea > 0 && $landArea < 2000)) { // Small area indicates MBR