diff --git a/app/Filament/Exports/CharacteristicValueExporter.php b/app/Filament/Exports/CharacteristicValueExporter.php index 9d05fb0..3d9e54d 100644 --- a/app/Filament/Exports/CharacteristicValueExporter.php +++ b/app/Filament/Exports/CharacteristicValueExporter.php @@ -36,6 +36,8 @@ class CharacteristicValueExporter extends Exporter ->label('COIL NUMBER'), ExportColumn::make('status') ->label('STATUS'), + ExportColumn::make('observed_value') + ->label('OBSERVED VALUE'), ExportColumn::make('created_at') ->label('CREATED AT'), ExportColumn::make('updated_at') diff --git a/app/Filament/Imports/CharacteristicValueImporter.php b/app/Filament/Imports/CharacteristicValueImporter.php index 583552b..90bf542 100644 --- a/app/Filament/Imports/CharacteristicValueImporter.php +++ b/app/Filament/Imports/CharacteristicValueImporter.php @@ -8,10 +8,14 @@ use App\Models\Line; use App\Models\Machine; use App\Models\Plant; use App\Models\ProcessOrder; +use App\Models\ProductCharacteristicsMaster; +use App\Models\User; +use Carbon\Carbon; 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 CharacteristicValueImporter extends Importer @@ -64,11 +68,28 @@ class CharacteristicValueImporter extends Importer ->exampleHeader('Status') ->example('Ok') ->label('Status'), + ImportColumn::make('observed_value') + ->requiredMapping() + ->exampleHeader('Observed Value') + ->example('RAW01234') + ->label('Observed Value'), ImportColumn::make('created_by') ->requiredMapping() ->exampleHeader('Created By') ->example('RAW01234') ->label('Created By'), + ImportColumn::make('created_at') + ->requiredMapping() + ->exampleHeader('Created DateTime') + ->example('01-01-2025 08:00:00') + ->label('Created DateTime') + ->rules(['required']), + ImportColumn::make('updated_at') + ->requiredMapping() + ->exampleHeader('Updated DateTime') + ->example('01-01-2025 08:00:00') + ->label('Updated DateTime') + ->rules(['required']), ]; } @@ -82,9 +103,9 @@ class CharacteristicValueImporter extends Importer $warnMsg = []; $plantId = null; $itemId = null; - $LineId = null; + $lineId = null; $machineId = null; - $itemAgainstPlant = null; + // $itemAgainstPlant = null; $plantCode = $this->data['plant']; $processOrder = trim($this->data['process_order'] ?? ''); @@ -94,6 +115,7 @@ class CharacteristicValueImporter extends Importer $status = trim($this->data['status']); $createdBy = trim($this->data['created_by']); $coilNo = trim($this->data['coil_number']); + $obserVal = trim($this->data['observed_value']); if ($plantCode == null || $plantCode == '') { $warnMsg[] = 'Plant code cannot be empty'; @@ -124,11 +146,11 @@ class CharacteristicValueImporter extends Importer $warnMsg[] = 'Item code not found'; } else { if ($plantId) { - $itemAgainstPlant = Item::where('code', $iCode)->where('plant_id', $plantId)->first(); - if (! $itemAgainstPlant) { + $itemCode = Item::where('code', $iCode)->where('plant_id', $plantId)->first(); + if (! $itemCode) { $warnMsg[] = 'Item code not found for the given plant'; } else { - $itemId = $itemAgainstPlant->id; + $itemId = $itemCode->id; } } } @@ -143,7 +165,7 @@ class CharacteristicValueImporter extends Importer if (! $lineAgainstPlant) { $warnMsg[] = 'Line name not found for the given plant'; } else { - $LineId = $lineAgainstPlant->id; + $lineId = $lineAgainstPlant->id; } } } @@ -163,9 +185,9 @@ class CharacteristicValueImporter extends Importer // $MachineId = $workCenterAgainstPlant->id; // } - if ($plantId != null && $LineId != null) { + if ($plantId != null && $lineId != null) { $machineAgaPlantLine = Machine::where('plant_id', $plantId) - ->where('line_id', $LineId) + ->where('line_id', $lineId) ->where('work_center', $workCenter) ->first(); @@ -186,24 +208,66 @@ class CharacteristicValueImporter extends Importer $warnMsg[] = 'Coil number should contain only numeric values!'; } + if ($obserVal == null || $obserVal == '') { + $warnMsg[] = 'Observed value cannot be empty'; + } elseif (! is_numeric($obserVal)) { + $warnMsg[] = 'Observed value should contain only numeric values!'; + } + if ($status == null || $status == '' || ! $status) { $warnMsg[] = 'Status cannot be empty'; } elseif (! in_array($status, ['Ok', 'NotOk'], true)) { $warnMsg[] = "Status must be either 'Ok' or 'NotOk'!"; } + // else { + // if (Str::length($status) <= 0 || ! is_numeric($status) || ! preg_match('/^\d+(\.\d+)?$/', $status) + // ) { + // $status = 'NotOk'; + // } else { + // $specVal = ProductCharacteristicsMaster::where('plant_id', $plantId)->where('item_id', $itemId)->where('line_id', $lineId)->where('machine_id', $machineId)->first(); + // if (! $specVal) { + // $status = 'NotOk'; + // } + + // $lowLimit = $specVal?->lower ?? 0; + // $uppLimit = $specVal?->upper ?? 0; + + // if (Str::length($lowLimit) <= 0 || ! is_numeric($lowLimit) || ! preg_match('/^\d+(\.\d+)?$/', $lowLimit) + // ) { + // $status = 'NotOk'; + // } elseif (Str::length($uppLimit) <= 0 || ! is_numeric($uppLimit) || ! preg_match('/^\d+(\.\d+)?$/', $uppLimit) + // ) { + // $status = 'NotOk'; + // } + + // if (($lowLimit == 0 && $uppLimit == 0) || ($uppLimit == 0)) { + // $status = 'NotOk'; + // } + + // if ($lowLimit > $obserVal || $uppLimit < $obserVal) { + // $status = 'NotOk'; + // } + // $status = 'Ok'; + // } + // } if ($createdBy == null || $createdBy == '' || ! $createdBy) { $warnMsg[] = 'Created By cannot be empty'; } - // $existing = CharacteristicValue::where('plant_id', $plantId) - // ->where('process_order', $processOrder) - // ->where('coil_number', $coilNo) - // ->first(); + if ($plantId != null) { + $user = User::where('name', $createdBy)->first(); - // if ($existing) { - // $warnMsg[] = "Process order '{$processOrder}' with coil number '{$coilNo}' already exist for the plant code '{$plantCode}'!"; - // } + $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 '{$plantCode}'!"; + } elseif (! $user->hasRole(['Super Admin', 'Process Quality Manager', 'Process Manager', 'Process Supervisor', 'Process Employee'])) { + $warnMsg[] = 'Created By user does not have rights!'; + } + } if ($plantId && $processOrder) { $existing = CharacteristicValue::where('plant_id', $plantId) @@ -218,29 +282,79 @@ class CharacteristicValueImporter extends Importer if ($plant && $itemCode && $processOrder != '') { - $existingOrder = ProcessOrder::where('plant_id', $plant->id) + $existingOrder = ProcessOrder::where('plant_id', $plantId) ->where('process_order', $processOrder) ->first(); - if ($existingOrder && $existingOrder->item_id !== ($itemCode->id ?? null)) { + if ($existingOrder && $existingOrder->item_id !== ($itemId ?? null)) { $warnMsg[] = 'Same Process Order already exists for this Plant with a different Item Code'; } } + $updatedBy = Filament::auth()->user()->name; // ?? 'Admin' + if (! $updatedBy) { + $warnMsg[] = 'Invalid updated by user name found'; + } + + $fromDate = $this->data['created_at']; + $toDate = $this->data['updated_at']; + + $formats = ['d-m-Y H:i', 'd-m-Y H:i:s']; // '07-05-2025 08:00' or '07-05-2025 08:00:00' + + $fdateTime = null; + $tdateTime = null; + // Try parsing with multiple formats + foreach ($formats as $format) { + try { + $fdateTime = Carbon::createFromFormat($format, $fromDate); + break; + } catch (\Exception $e) { + // $warnMsg[] = "Date format mismatch with format: $format"; + } + } + + foreach ($formats as $format) { + try { + $tdateTime = Carbon::createFromFormat($format, $toDate); + break; + } catch (\Exception $e) { + // $warnMsg[] = "Date format mismatch with format: $format"; + } + } + + if (! isset($fdateTime)) { + $warnMsg[] = "Invalid 'Created DateTime' format. Expected DD-MM-YYYY HH:MM:SS"; + } + if (! isset($tdateTime)) { + $warnMsg[] = "Invalid 'Updated DateTime' format. Expected DD-MM-YYYY HH:MM:SS"; + } + + if (isset($fdateTime) && isset($tdateTime)) { + if ($fdateTime->greaterThan($tdateTime)) { + $warnMsg[] = "'Created DataTime' is greater than 'Updated DateTime'."; + } + } + if (! empty($warnMsg)) { throw new RowImportFailedException(implode(', ', $warnMsg)); } - return CharacteristicValue::create([ + return CharacteristicValue::updateOrCreate([ 'plant_id' => $plantId, - 'item_id' => $itemId, - 'line_id' => $LineId, - 'machine_id' => $machineId, - 'process_order' => trim($this->data['process_order']), - 'coil_number' => trim($this->data['coil_number']), - 'status' => trim($this->data['status']), - 'created_by' => trim($this->data['created_by']), - ]); + 'process_order' => $processOrder, + 'coil_number' => $coilNo, + ], + [ + 'item_id' => $itemId, + 'line_id' => $lineId, + 'machine_id' => $machineId, + 'status' => $status, + 'observed_value' => $obserVal, + 'created_by' => $createdBy, + 'updated_by' => $updatedBy, + 'created_at' => $fdateTime->format('Y-m-d H:i:s'), + 'updated_at' => $tdateTime->format('Y-m-d H:i:s'), + ]); // return null;