Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
Gemini PR Review / Gemini PR Review (pull_request) Has been cancelled
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Has been cancelled
Laravel Larastan / larastan (pull_request) Has been cancelled
Laravel Pint / pint (pull_request) Has been cancelled
348 lines
14 KiB
PHP
348 lines
14 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Imports;
|
|
|
|
use App\Models\Item;
|
|
use App\Models\Line;
|
|
use App\Models\Plant;
|
|
use App\Models\ProcessOrder;
|
|
use App\Models\User;
|
|
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
|
|
use Filament\Actions\Imports\ImportColumn;
|
|
use Filament\Actions\Imports\Importer;
|
|
use Filament\Actions\Imports\Models\Import;
|
|
use Filament\Facades\Filament;
|
|
use Str;
|
|
|
|
class ProcessOrderImporter extends Importer
|
|
{
|
|
protected static ?string $model = ProcessOrder::class;
|
|
|
|
public static function getColumns(): array
|
|
{
|
|
return [
|
|
ImportColumn::make('plant')
|
|
->requiredMapping()
|
|
->exampleHeader('PLANT CODE')
|
|
->example('1000')
|
|
->label('PLANT CODE')
|
|
->relationship(resolveUsing: 'code')
|
|
->rules(['required']),
|
|
ImportColumn::make('line')
|
|
->exampleHeader('LINE NAME')
|
|
->example(' Polywrapped line')
|
|
->label('LINE NAME')
|
|
->relationship(resolveUsing: 'name'),
|
|
ImportColumn::make('item')
|
|
->requiredMapping()
|
|
->exampleHeader('ITEM CODE')
|
|
->example('123456')
|
|
->label('ITEM CODE')
|
|
->relationship(resolveUsing: 'code')
|
|
->rules(['required']),
|
|
ImportColumn::make('process_order')
|
|
->requiredMapping()
|
|
->exampleHeader('PROCESS ORDER')
|
|
->example('202500123456')
|
|
->label('PROCESS ORDER')
|
|
->rules(['required']),
|
|
ImportColumn::make('coil_number')
|
|
->exampleHeader('COIL NUMBER')
|
|
->example('01')
|
|
->label('COIL NUMBER'),
|
|
ImportColumn::make('order_quantity')
|
|
->requiredMapping()
|
|
->exampleHeader('ORDER QUANTITY')
|
|
->example('100')
|
|
->label('ORDER QUANTITY')
|
|
->rules(['required']),
|
|
ImportColumn::make('received_quantity')
|
|
->exampleHeader('RECEIVED QUANTITY')
|
|
->example('01')
|
|
->label('RECEIVED QUANTITY'),
|
|
ImportColumn::make('sfg_number')
|
|
->exampleHeader('SFG NUMBER')
|
|
->example('200000220613-72')
|
|
->label('SFG NUMBER'),
|
|
ImportColumn::make('machine_name')
|
|
->exampleHeader('MACHINE NAME')
|
|
->example('WMIWRM13 - 2-L2')
|
|
->label('MACHINE NAME'),
|
|
ImportColumn::make('scrap_quantity')
|
|
->exampleHeader('SCRAP QUANTITY')
|
|
->example('0')
|
|
->label('SCRAP QUANTITY'),
|
|
ImportColumn::make('rework_status')
|
|
->exampleHeader('REWORK STATUS')
|
|
->example('0')
|
|
->label('REWORK STATUS'),
|
|
ImportColumn::make('created_at')
|
|
->exampleHeader('CREATED AT')
|
|
->example('2026-02-20 13:00:00')
|
|
->label('CREATED AT'),
|
|
ImportColumn::make('updated_at')
|
|
->exampleHeader('UPDATED AT')
|
|
->example('2026-02-20 13:00:00')
|
|
->label('UPDATED AT'),
|
|
ImportColumn::make('created_by')
|
|
->exampleHeader('CREATED BY')
|
|
->example('RAW01234')
|
|
->label('CREATED BY'),
|
|
ImportColumn::make('updated_by')
|
|
->exampleHeader('UPDATED BY')
|
|
->example('RAW01234')
|
|
->label('UPDATED BY'),
|
|
];
|
|
}
|
|
|
|
public function resolveRecord(): ?ProcessOrder
|
|
{
|
|
$warnMsg = [];
|
|
$plantCod = $this->data['plant'];
|
|
$plant = null;
|
|
$plantId = null;
|
|
$itemId = null;
|
|
$iCode = trim($this->data['item']);
|
|
$lineName = trim($this->data['line']);
|
|
$processOrder = trim($this->data['process_order'] ?? '');
|
|
$coilNo = trim($this->data['coil_number'] ?? '');
|
|
$sfgNo = trim($this->data['sfg_number'] ?? '');
|
|
$machineName = trim($this->data['machine_name'] ?? '');
|
|
$orderQuan = trim($this->data['order_quantity'] ?? '');
|
|
$scrapQuan = trim($this->data['scrap_quantity'] ?? '');
|
|
$reworkStatus = trim($this->data['rework_status'] ?? '');
|
|
$recQuan = trim($this->data['received_quantity'] ?? '');
|
|
$createdAt = trim($this->data['created_at'] ?? '');
|
|
$createdBy = trim($this->data['created_by'] ?? '');
|
|
$updatedAt = trim($this->data['updated_at'] ?? '');
|
|
$updatedBy = trim($this->data['updated_by'] ?? '');
|
|
// $user = Filament::auth()->user();
|
|
// $operatorName = $user->name;
|
|
|
|
if ($plantCod == null || $plantCod == '') {
|
|
$warnMsg[] = 'Plant code cannot be empty';
|
|
}
|
|
if ($iCode == null || $iCode == '') {
|
|
$warnMsg[] = 'Item code cannot be empty';
|
|
}
|
|
if ($processOrder == null || $processOrder == '') {
|
|
$warnMsg[] = 'Process order cannot be empty';
|
|
}
|
|
if ($orderQuan == null || $orderQuan == '') {
|
|
$warnMsg[] = 'Order quantity cannot be empty';
|
|
} elseif ($orderQuan == 0 || $orderQuan == '0') {
|
|
$warnMsg[] = 'Order quantity cannot be zero';
|
|
}
|
|
if ($coilNo == null || $coilNo == '') {
|
|
$coilNo = '0';
|
|
}
|
|
if ($scrapQuan == null || $scrapQuan == '') {
|
|
$scrapQuan = 0;
|
|
}
|
|
if ($recQuan == null || $recQuan == '') {
|
|
$recQuan = 0;
|
|
}
|
|
if ($reworkStatus == null || $reworkStatus = '') {
|
|
$reworkStatus = 0;
|
|
}
|
|
|
|
if ($createdBy == null || $createdBy == '') {
|
|
$createdBy = Filament::auth()->user()?->name;
|
|
$updatedBy = $createdBy;
|
|
}
|
|
|
|
if (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod)) {
|
|
$warnMsg[] = 'Invalid plant code found';
|
|
} else {
|
|
$plant = Plant::where('code', $plantCod)->first();
|
|
if (! $plant) {
|
|
$warnMsg[] = 'Plant not found';
|
|
} else {
|
|
$plantId = $plant->id;
|
|
}
|
|
}
|
|
|
|
if (Str::length($iCode) < 6 || ! ctype_alnum($iCode)) {
|
|
$warnMsg[] = 'Invalid item code found';
|
|
} else {
|
|
$itemCode = Item::where('code', $iCode)->first();
|
|
if (! $itemCode) {
|
|
$warnMsg[] = 'Item code not found';
|
|
} else {
|
|
if ($plantId) {
|
|
$itemCode = Item::where('code', $iCode)->where('plant_id', $plantId)->first();
|
|
if (! $itemCode) {
|
|
$warnMsg[] = 'Item code not found for the given plant';
|
|
} else {
|
|
$itemId = $itemCode->id;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($plant && $itemCode && $processOrder != '') {
|
|
|
|
$existingOrder = ProcessOrder::where('plant_id', $plantId)
|
|
->where('process_order', $processOrder)
|
|
->first();
|
|
|
|
if ($existingOrder && $existingOrder->item_id !== ($itemId ?? null)) {
|
|
$warnMsg[] = 'Same Process Order already exists for this Plant with a different Item Code';
|
|
}
|
|
}
|
|
|
|
if ($lineName != null && $lineName != '') {
|
|
$lineExists = Line::where('name', $lineName)->first();
|
|
if (! $lineExists) {
|
|
$warnMsg[] = 'Line name not found';
|
|
} else {
|
|
if ($plantId) {
|
|
$lineAgainstPlant = Line::where('name', $lineName)->where('plant_id', $plantId)->first();
|
|
if (! $lineAgainstPlant) {
|
|
$warnMsg[] = 'Line name not found for the given plant';
|
|
} else {
|
|
$lineId = $lineAgainstPlant->id;
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
$lineId = null;
|
|
}
|
|
|
|
if ($createdBy != null && $createdBy != '') {
|
|
if ($plantId) {
|
|
$user = User::where('name', $createdBy)->first();
|
|
|
|
$userPlant = User::where('name', $createdBy)->where('plant_id', $plantId)->first();
|
|
|
|
if (! $user) {
|
|
$warnMsg[] = 'Created By user name not found!';
|
|
} elseif (! $userPlant && ! $user->hasRole('Super Admin')) {
|
|
$warnMsg[] = "Created By user '{$createdBy}' not found for Plant '{$plantCod}'!";
|
|
} elseif (! $user->hasRole(['Super Admin', 'Process Quality Manager', 'Process Manager', 'Process Supervisor', 'Process Employee'])) {
|
|
$warnMsg[] = 'Created By user does not have rights!';
|
|
}
|
|
}
|
|
$updatedBy = Filament::auth()->user()?->name;
|
|
}
|
|
|
|
if (! empty($warnMsg)) {
|
|
throw new RowImportFailedException(implode(', ', $warnMsg));
|
|
}
|
|
|
|
if ($lineName != null && $lineName != '') {
|
|
$existingCoil = ProcessOrder::where('plant_id', $plantId)
|
|
->where('process_order', $processOrder)
|
|
->where('line_id', $lineId)
|
|
->where('coil_number', $coilNo)
|
|
->first();
|
|
|
|
if (! $existingCoil) {
|
|
ProcessOrder::Create(
|
|
[
|
|
'plant_id' => $plantId,
|
|
'line_id' => $lineId,
|
|
'process_order' => $processOrder,
|
|
'item_id' => $itemId,
|
|
'coil_number' => $coilNo,
|
|
'order_quantity' => $orderQuan,
|
|
'received_quantity' => $recQuan,
|
|
'scrap_quantity' => $scrapQuan,
|
|
'sfg_number' => $sfgNo,
|
|
'machine_name' => $machineName,
|
|
'rework_status' => $reworkStatus,
|
|
'created_at' => $createdAt,
|
|
'updated_at' => $updatedAt,
|
|
'created_by' => $createdBy,
|
|
'updated_by' => $updatedBy,
|
|
]
|
|
);
|
|
} else {
|
|
ProcessOrder::where('plant_id', $plantId)
|
|
->where('process_order', $processOrder)
|
|
->where('line_id', $lineId)
|
|
->where('coil_number', $coilNo)
|
|
->update([
|
|
// 'order_quantity' => $orderQty,
|
|
'received_quantity' => $recQuan,
|
|
'scrap_quantity' => $scrapQuan,
|
|
// 'sfg_number' => $sfgNo,
|
|
// 'machine_name' => $machineId,
|
|
'rework_status' => $reworkStatus,
|
|
'updated_by' => $updatedBy,
|
|
'updated_at' => $updatedAt,
|
|
]);
|
|
}
|
|
|
|
} elseif ($lineName == null || $lineName == '') {
|
|
$existing = ProcessOrder::where('plant_id', $plantId)
|
|
->where('process_order', $processOrder)
|
|
->where('coil_number', $coilNo)
|
|
->first();
|
|
|
|
if (! $existing && $coilNo == '0' || $coilNo == 0) {
|
|
ProcessOrder::create([
|
|
'plant_id' => $plantId,
|
|
'item_id' => $itemId,
|
|
'process_order' => $processOrder,
|
|
'coil_number' => '0',
|
|
'order_quantity' => $orderQuan,
|
|
'received_quantity' => 0,
|
|
'scrap_quantity' => 0,
|
|
'created_by' => $createdBy,
|
|
'updated_by' => $updatedBy,
|
|
]);
|
|
} elseif (! $existing) {
|
|
ProcessOrder::Create(
|
|
[
|
|
'plant_id' => $plantId,
|
|
'line_id' => $lineId,
|
|
'process_order' => $processOrder,
|
|
'item_id' => $itemId,
|
|
'coil_number' => $coilNo,
|
|
'order_quantity' => $orderQuan,
|
|
'received_quantity' => $recQuan,
|
|
'scrap_quantity' => $scrapQuan ?? 0,
|
|
'sfg_number' => $sfgNo,
|
|
'machine_name' => $machineName,
|
|
'rework_status' => $reworkStatus,
|
|
'created_at' => $createdAt,
|
|
'updated_at' => $updatedAt,
|
|
'created_by' => $createdBy,
|
|
'updated_by' => $updatedBy,
|
|
]
|
|
);
|
|
} else {// $coilNo = '0'
|
|
ProcessOrder::where('plant_id', $plantId)
|
|
->where('process_order', $processOrder)
|
|
->where('coil_number', $coilNo)
|
|
->update([
|
|
// 'order_quantity' => $orderQty,
|
|
'received_quantity' => $recQuan,
|
|
'scrap_quantity' => $scrapQuan,
|
|
// 'sfg_number' => $sfgNo,
|
|
// 'machine_name' => $machineId,
|
|
'rework_status' => $reworkStatus,
|
|
'updated_by' => $updatedBy,
|
|
'updated_at' => $updatedAt,
|
|
]);
|
|
}
|
|
}
|
|
|
|
return null;
|
|
|
|
// return new ProcessOrder();
|
|
}
|
|
|
|
public static function getCompletedNotificationBody(Import $import): string
|
|
{
|
|
$body = 'Your process order import has completed and '.number_format($import->successful_rows).' '.str('row')->plural($import->successful_rows).' imported.';
|
|
|
|
if ($failedRowsCount = $import->getFailedRowsCount()) {
|
|
$body .= ' '.number_format($failedRowsCount).' '.str('row')->plural($failedRowsCount).' failed to import.';
|
|
}
|
|
|
|
return $body;
|
|
}
|
|
}
|