Files
HRM-System/app/Console/Commands/SyncLegacySalaries.php
2026-04-20 00:20:10 +08:00

67 lines
2.7 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use App\Models\User;
class SyncLegacySalaries extends Command
{
protected $signature = 'import:legacy-salaries';
protected $description = 'Sync legacy employee salaries to the local database to fix missing UI data';
public function handle()
{
$this->info("Syncing legacy employee salaries to local UI database...");
// Make sure legacy DB has the latest patches applied from SimulatePayslips
$legacySalaries = DB::connection('legacy')->table('employee_salary_structures')->get();
$syncedCount = 0;
foreach ($legacySalaries as $legacyBase) {
// Because legacy user IDs shifted during migration, we map them correctly via the exact User schema
// By default, try exactly porting email lookup if possible. If not, default to user_id match assumptions
// from the ImportLegacyRamesebData mapping rule.
// To mathematically guarantee Ana and Paul, we use the known mapping
// Rame legacy User ID -> Local system mapping:
// 2 -> likely 3 (Ana)
// 8 -> likely 9 (Paul)
// Actually, best to fetch legacy user email and map it perfectly
$legacyUser = DB::connection('legacy')->table('users')->where('id', $legacyBase->user_id)->first();
if (!$legacyUser) continue;
$localUser = User::where('email', $legacyUser->email)->first();
if (!$localUser) continue;
// Default components must be strictly null to allow Laravel array plucking internally without throwing 500 Array exceptions
$components = null;
// If the pay frequency is semi-monthly, the UI `basic_salary` is expected to be the full monthly or cut-off?
// Usually, basic_salary stores the monthly rate and payday math divides it.
$grossMonthly = $legacyBase->gross_salary;
$payFreq = $legacyBase->pay_frequency ?? 'monthly';
DB::table('employee_salaries')->updateOrInsert(
['employee_id' => $localUser->id],
[
'basic_salary' => $grossMonthly,
'pay_frequency' => $payFreq,
'components' => $components,
'is_active' => 1,
'calculation_status' => 'calculated',
'created_by' => 1,
'updated_at' => now(),
'created_at' => now()
]
);
$syncedCount++;
}
$this->info("Successfully synced $syncedCount employee salaries to the UI!");
}
}