Rapport-Laravel/database/seeders/InceptNebAllocationSeeder.php

63 lines
1.9 KiB
PHP

<?php
namespace Database\Seeders;
use App\Services\InceptNebAllocation;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use PhpOffice\PhpSpreadsheet\IOFactory;
class InceptNebAllocationSeeder extends Seeder
{
public function run()
{
$path = '/Volumes/Storage/Downloads/Nebulizers Allocation Data.xlsx';
if (!is_file($path)) {
throw new \RuntimeException('Allocation workbook not found at ' . $path);
}
$sheet = IOFactory::load($path)->getActiveSheet();
$rows = $sheet->toArray(null, true, true, true);
$service = new InceptNebAllocation();
$now = date('Y-m-d H:i:s');
$count = 0;
foreach ($rows as $index => $row) {
if ($index === 1) {
continue;
}
$empId = $service->normalizeEmpId($row['A'] ?? '');
if ($empId === '' || strcasecmp($empId, 'EMP ID') === 0) {
continue;
}
DB::table('incept_neb_allocations')->updateOrInsert(
['emp_id' => $empId],
[
'emp_name' => $this->clean($row['B'] ?? ''),
'team' => $this->clean($row['C'] ?? ''),
'designation' => $this->clean($row['D'] ?? ''),
'robust_qty' => max(0, (int) ($row['E'] ?? 0)),
'mini_qty' => max(0, (int) ($row['F'] ?? 0)),
'control_d_qty' => max(0, (int) ($row['G'] ?? 0)),
'updated_at' => $now,
'created_at' => $now,
]
);
$count++;
}
if (isset($this->command)) {
$this->command->info('Imported ' . $count . ' nebulizer allocations.');
}
}
private function clean($value)
{
$value = str_replace("\xc2\xa0", ' ', (string) $value);
$value = preg_replace('/\s+/', ' ', $value);
return trim($value);
}
}