parfum/app/Console/Commands/Migrations/MigrateEmployeesFromOldData.php
Yoga Pangestu 3a05ee2617 feat: implement comprehensive migration commands for legacy data
- Added migration commands for users, employees, outlets, categories, products, bottles, and perfumes to transfer data from the old database to the new structure.
- Each command includes options for forced migration and recreation of existing records.
- Implemented progress feedback and error handling during migration processes.
- Updated database configuration to include connection settings for the old database.
- Enhanced DatabaseSeeder to automatically trigger the migration process after seeding essential data.
2026-01-10 19:04:31 +07:00

114 lines
3.8 KiB
PHP

<?php
namespace App\Console\Commands\Migrations;
use App\Enums\EmploymentStatus;
use App\Enums\Gender;
use App\Models\Employee;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class MigrateEmployeesFromOldData extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'migrate:employees {--force : Force migration without confirmation}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Migrate employees data from old database to new structure';
/**
* Execute the console command.
*/
public function handle()
{
if (! $this->option('force') && ! $this->confirm('Are you sure you want to migrate employees data? This will add new employees to your database.')) {
$this->info('Migration cancelled.');
return;
}
$this->info('Starting employees migration...');
try {
$oldBiographical = DB::connection('old_database')->table('biographical')->whereNull('deleted_at')->get();
$this->info("Found {$oldBiographical->count()} employees to migrate.");
$bar = $this->output->createProgressBar($oldBiographical->count());
$migrated = 0;
$skipped = 0;
foreach ($oldBiographical as $oldBio) {
$user = User::find($oldBio->user_id);
if (! $user) {
$this->warn("User ID {$oldBio->user_id} not found, skipping employee {$oldBio->name}...");
$skipped++;
$bar->advance();
continue;
}
$existingEmployee = Employee::where('user_id', $oldBio->user_id)->first();
if ($existingEmployee) {
$this->warn("Employee for user ID {$oldBio->user_id} already exists, skipping...");
$skipped++;
$bar->advance();
continue;
}
// Get user data for additional info
$oldUser = DB::connection('old_database')->table('users')->where('id', $oldBio->user_id)->first();
Employee::create([
'user_id' => $oldBio->user_id,
'full_name' => $oldBio->name,
'code' => $this->generateEmployeeCode($oldBio->user_id),
'phone_number' => $oldBio->phone_number,
'base_salary' => $oldUser ? $oldUser->salary : 0,
'gender' => $oldBio->gender == '1' ? Gender::MALE : Gender::FEMALE,
'address' => $oldBio->pob, // Using place of birth as address
'birthdate' => $oldBio->dob,
'hire_date' => $oldUser ? $oldUser->entry_date : now()->toDateString(),
'resign_date' => $oldUser && $oldUser->out_date ? $oldUser->out_date : null,
'status' => EmploymentStatus::PERMANENT,
'created_at' => $oldBio->created_at,
'updated_at' => $oldBio->updated_at,
]);
$migrated++;
$bar->advance();
}
$bar->finish();
$this->newLine(2);
$this->info('✅ Employees migration completed!');
$this->info("📊 Migrated: {$migrated} employees");
$this->info("⏭️ Skipped: {$skipped} employees (user not found or already exist)");
} catch (\Exception $e) {
$this->error("❌ Migration failed: {$e->getMessage()}");
return 1;
}
return 0;
}
private function generateEmployeeCode(int $userId): string
{
return 'EMP'.str_pad($userId, 4, '0', STR_PAD_LEFT);
}
}