Merge pull request 'changed the logic in product characteristic master importer' (#234) from ranjith-dev into master
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled

Reviewed-on: #234
This commit was merged in pull request #234.
This commit is contained in:
2026-09-20 09:35:16 +00:00

View File

@@ -9,12 +9,12 @@ use App\Models\Plant;
use App\Models\ProductCharacteristicsMaster; use App\Models\ProductCharacteristicsMaster;
use App\Models\User; use App\Models\User;
use App\Models\WorkGroupMaster; use App\Models\WorkGroupMaster;
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
use Filament\Actions\Imports\ImportColumn; use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer; use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import; use Filament\Actions\Imports\Models\Import;
use Filament\Facades\Filament;
use Str; use Str;
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
use Illuminate\Support\Facades\Auth;
class ProductCharacteristicsMasterImporter extends Importer class ProductCharacteristicsMasterImporter extends Importer
{ {
@@ -24,257 +24,305 @@ class ProductCharacteristicsMasterImporter extends Importer
{ {
return [ return [
ImportColumn::make('plant') ImportColumn::make('plant')
->label('PLANT CODE')
->requiredMapping() ->requiredMapping()
->exampleHeader('Plant Name') ->exampleHeader('PLANT CODE')
->example('Ransar Industries-I') ->example(['1000', '1000'])
->label('Plant Name') ->relationship(resolveUsing: 'code')
->relationship(resolveUsing:'name')
->rules(['required']), ->rules(['required']),
ImportColumn::make('item') ImportColumn::make('item')
->label('ITEM CODE')
->requiredMapping() ->requiredMapping()
->exampleHeader('Item Code') ->exampleHeader('ITEM CODE')
->example('630214') ->example(['123456', '123456'])
->label('Item Code') ->relationship(resolveUsing: 'code')
->relationship(resolveUsing:'code')
->rules(['required']), ->rules(['required']),
ImportColumn::make('line') ImportColumn::make('line')
->label('LINE NAME')
->requiredMapping() ->requiredMapping()
->exampleHeader('Line') ->exampleHeader('LINE NAME')
->example('4 inch pump line') ->example(['4 inch pump line', '4 inch pump line'])
->label('Line') ->relationship(resolveUsing: 'name')
->relationship(resolveUsing:'name')
->rules(['required']), ->rules(['required']),
ImportColumn::make('work_group_master_id') ImportColumn::make('work_group_master_id')
->label('Group Work Center') ->label('GROUP WORK CENTER')
->requiredMapping() ->requiredMapping()
->exampleHeader('Group Work Center') ->exampleHeader('GROUP WORK CENTER')
->example('RMGCGABC') ->example(['RMGSTR01', 'RMGSTR01'])
->relationship(resolveUsing:'name') ->relationship(resolveUsing: 'name')
->rules(['required']), ->rules(['required']),
ImportColumn::make('machine') ImportColumn::make('machine')
->label('WORK CENTER')
->requiredMapping() ->requiredMapping()
->exampleHeader('Work Center') ->exampleHeader('WORK CENTER')
->example('RMGCE001') ->example(['RMISTR01', 'RMISTR02'])
->label('Work Center') ->relationship(resolveUsing: 'work_center')
->relationship(resolveUsing:'work_center')
->rules(['required']), ->rules(['required']),
ImportColumn::make('characteristics_type') ImportColumn::make('characteristics_type')
->exampleHeader('Characteristics Type') ->label('CHARACTERISTICS TYPE')
->example('Process or Product') ->requiredMapping()
->label('Characteristics Type') ->exampleHeader('CHARACTERISTICS TYPE')
->example(['Product', 'Process'])
->rules(['required']), ->rules(['required']),
ImportColumn::make('name') ImportColumn::make('name')
->exampleHeader('Name') ->label('CHARACTERISTICS NAME')
->example('Body') ->requiredMapping()
->label('Name') ->exampleHeader('CHARACTERISTICS NAME')
->example(['TEST01', 'TEST02'])
->rules(['required']), ->rules(['required']),
ImportColumn::make('category')
->label('CATEGORY')
->requiredMapping()
->exampleHeader('CATEGORY')
->example(['Open Pump', 'Submersible Pump']),
ImportColumn::make('inspection_type') ImportColumn::make('inspection_type')
->exampleHeader('Inspection Type') ->label('INSPECTION TYPE')
->example('Visual or Value') ->requiredMapping()
->label('Inspection Type') ->exampleHeader('INSPECTION TYPE')
->example(['Value', 'Visual'])
->rules(['required']), ->rules(['required']),
ImportColumn::make('lower') ImportColumn::make('lower')
->exampleHeader('Lower') ->label('LOWER')
->example('-0.2') ->requiredMapping()
->label('Lower') ->exampleHeader('LOWER')
->rules(['numeric']), ->example(['5', '0'])
->rules(['required']),
ImportColumn::make('middle') ImportColumn::make('middle')
->exampleHeader('Middle') ->label('MIDDLE')
->example('1') ->requiredMapping()
->label('Middle') ->exampleHeader('MIDDLE')
->numeric() ->example(['10', '0'])
->rules(['numeric']), ->rules(['required']),
ImportColumn::make('upper') ImportColumn::make('upper')
->exampleHeader('Upper') ->label('UPPER')
->example('0.2') ->requiredMapping()
->label('Upper') ->exampleHeader('UPPER')
->rules(['numeric']), ->example(['15', '0'])
ImportColumn::make('created_by') ->rules(['required']),
->exampleHeader('Created By') // ImportColumn::make('created_by'),
->example('Admin') // ImportColumn::make('updated_by'),
->label('Created By'),
//ImportColumn::make('updated_by'),
]; ];
} }
public function resolveRecord(): ?ProductCharacteristicsMaster public function resolveRecord(): ?ProductCharacteristicsMaster
{ {
$warnMsg = []; $warnMsg = [];
$plant = Plant::where('name', $this->data['plant'])->first(); $plantCod = trim($this->data['plant']) ?? null;
if (!$plant) { $itemCod = trim($this->data['item']) ?? null;
$warnMsg[] = "Plant not found"; $lineNam = trim($this->data['line']) ?? null;
$groupWorkCenter = trim($this->data['work_group_master_id']) ?? null;
$workCenter = trim($this->data['machine']) ?? null;
$charTyp = trim($this->data['characteristics_type']) ?? null;
$charNam = trim($this->data['name']) ?? null;
$category = trim($this->data['category']) ?? null;
$inspectTyp = trim($this->data['inspection_type']) ?? null;
$lower = trim($this->data['lower']) ?? null;
$middle = trim($this->data['middle']) ?? null;
$upper = trim($this->data['upper']) ?? null;
$createdBy = Filament::auth()->user()->name;
$updatedBy = null;
$plantId = null;
$itemId = null;
$lineId = null;
$workGroupMasterId = null;
$machineId = null;
if ($plantCod == null || $plantCod == '') {
$warnMsg[] = "Plant code can't be empty!";
} elseif (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod)) {
$warnMsg[] = 'Invalid plant code found!';
}
if ($itemCod == null || $itemCod == '') {
$warnMsg[] = "Item code can't be empty!";
} elseif (Str::length($itemCod) < 6 || ! ctype_alnum($itemCod)) {
$warnMsg[] = 'Invalid item code found!';
}
if ($lineNam == null || $lineNam == '') {
$warnMsg[] = "Line name can't be empty!";
}
if ($groupWorkCenter == null || $groupWorkCenter == '') {
$warnMsg[] = "Group work center can't be empty!";
}
if ($workCenter == null || $workCenter == '') {
$warnMsg[] = "Work center can't be empty!";
}
if ($charTyp != 'Product' && $charTyp != 'Process') {
$warnMsg[] = "Characteristics type must be either 'Product' or 'Process'!";
}
if ($charNam == null || $charNam == '') {
$warnMsg[] = "Characteristics name can't be empty!";
}
if ($inspectTyp != 'Visual' && $inspectTyp != 'Value') {
$warnMsg[] = "Inspection type must be either 'Visual' or 'Value'!";
}
if ($lower == null || $lower == '' || $middle == null || $middle == '' || $upper == null || $upper == '' || $upper == 0 || $upper == '0') {
$lower = 0;
$middle = 0;
$upper = 0;
} }
$itemExists = Item::where('code', $this->data['item'])->first(); if (! empty($warnMsg)) {
if (!$itemExists) {
$warnMsg[] = "Item not found";
}
$itemAgainstPlant = Item::where('code', $this->data['item'])
->where('plant_id', $plant->id)
->first();
if (!$itemAgainstPlant) {
$warnMsg[] = "Item code not found for the given plant";
}
else {
$itemId = $itemAgainstPlant->id;
}
$lineExists = Line::where('name', $this->data['line'])->first();
if (!$lineExists) {
$warnMsg[] = "Line not found";
}
$lineAgainstPlant = Line::where('name', $this->data['line'])
->where('plant_id', $plant->id)
->first();
if (!$lineAgainstPlant) {
$warnMsg[] = "Line not found for the given plant";
}
else
{
$LineId = $lineAgainstPlant->id;
}
$WorkgroupMaster = WorkGroupMaster::where('name', $this->data['work_group_master_id'])->where('plant_id', $plant->id)->first();
if (!$WorkgroupMaster) {
$warnMsg[] = "Work Group Master value not found";
}
else {
$workGroupMasterId = $WorkgroupMaster->id;
// 2. Now check if this WorkGroupMaster id exists in ANY of the 10 columns
$existsInLine = Line::where('plant_id', $plant->id)
->where(function ($q) use ($workGroupMasterId) {
for ($i = 1; $i <= 10; $i++) {
$q->orWhere("work_group{$i}_id", $workGroupMasterId);
}
})
->exists();
if (!$existsInLine) {
$warnMsg[] = "Work Group Master '{$WorkgroupMaster->name}' is not mapped to any line in this plant";
}
else {
$workGroupMasterId = $WorkgroupMaster->id;
}
}
$machine = Machine::where('work_center', $this->data['machine'])->first();
if (!$machine) {
$warnMsg[] = "Work Center not found";
}
else {
$machineId = $machine->id;
}
$machineAgainstPlant = Machine::where('work_center', $this->data['machine'])
->where('plant_id', $plant->id)
->first();
if (!$machineAgainstPlant) {
$warnMsg[] = "Work Center not found for the given plant";
}
else
{
$machineId = $machineAgainstPlant->id;
}
$user = User::where('name', $this->data['created_by'])->first();
if (!$user) {
$warnMsg[] = "Operator ID not found";
}
if (($this->data['inspection_type'] ?? null) == 'Value') {
$upper = $this->data['upper'] ?? null;
$lower = $this->data['lower'] ?? null;
$middle = $this->data['middle'] ?? null;
if (is_null($upper) || is_null($lower) || is_null($middle)) {
$warnMsg[] = "For 'Value' inspection type, Upper, Lower, and Middle values are required.";
} elseif (!is_numeric($upper) || !is_numeric($lower) || !is_numeric($middle)) {
$warnMsg[] = "Upper, Lower, and Middle values must be numeric.";
} elseif (!($lower <= $middle && $middle <= $upper)) {
$warnMsg[] = "For 'Value' inspection type, values must satisfy: Lower ≤ Middle ≤ Upper.";
}
}
if (!empty($warnMsg)) {
throw new RowImportFailedException(implode(', ', $warnMsg)); throw new RowImportFailedException(implode(', ', $warnMsg));
} }
else
{
$record = ProductCharacteristicsMaster::firstOrNew([
'plant_id' => $plant->id,
'item_id' => $itemId,
'line_id' => $LineId,
'work_group_master_id' => $workGroupMasterId,
'machine_id' => $machineId,
]);
$currentUser = Auth::check() ? Auth::user()->name : ($this->data['created_by'] ?? 'System'); $plant = Plant::where('code', $plantCod)->first();
if (! $plant) {
// If record is new, fill all fields $warnMsg[] = 'Plant code not found!';
if (!$record->exists) { } else {
$record->name = $this->data['name']; $plantId = $plant->id;
$record->characteristics_type = $this->data['characteristics_type']; $itemExists = Item::where('code', $itemCod)->first();
$record->inspection_type = $this->data['inspection_type']; if (! $itemExists) {
$record->upper = $this->data['upper'] ?? null; $warnMsg[] = 'Item code not found!';
$record->lower = $this->data['lower'] ?? null;
$record->middle = $this->data['middle'] ?? null;
$record->created_by = $currentUser;
} else { } else {
// Record exists → update only updated_by and updated_at $itemAgainstPlant = Item::where('code', $itemCod)->where('plant_id', $plantId)->first();
$record->updated_by = $currentUser; if (! $itemAgainstPlant) {
$record->touch(); $warnMsg[] = 'Item code not found for the given plant!';
} else {
$itemId = $itemAgainstPlant->id;
}
} }
$record->save(); $lineExists = Line::where('name', $lineNam)->first();
if (! $lineExists) {
$warnMsg[] = 'Line name not found!';
} else {
$lineAgainstPlant = Line::where('name', $lineNam)->where('plant_id', $plantId)->first();
if (! $lineAgainstPlant) {
$warnMsg[] = 'Line name not found for the given plant!';
} else {
$lineId = $lineAgainstPlant->id;
return null; $WorkgroupMaster = WorkGroupMaster::where('name', $groupWorkCenter)->first();
if (! $WorkgroupMaster) {
$warnMsg[] = 'Group work center not found!';
} else {
$WorkgroupMaster = WorkGroupMaster::where('name', $groupWorkCenter)->where('plant_id', $plantId)->first();
if (! $WorkgroupMaster) {
$warnMsg[] = 'Group work center not found for the given plant!';
} else {
$workGroupMasterId = $WorkgroupMaster->id;
// 2. Now check if this WorkGroupMaster id exists in ANY of the 10 columns
$existsInLine = Line::where('plant_id', $plantId)->where('id', $lineId)
->where(function ($q) use ($workGroupMasterId) {
for ($i = 1; $i <= 10; $i++) {
$q->orWhere("work_group{$i}_id", $workGroupMasterId);
}
})
->exists();
if (! $existsInLine) {
$workGroupMasterId = null;
$warnMsg[] = "Group work center '{$WorkgroupMaster->name}' is not mapped for the given line!";
} else {
$workGroupMasterId = $WorkgroupMaster->id;
$machine = Machine::where('work_center', $workCenter)->first();
if (! $machine) {
$warnMsg[] = 'Work center not found!';
} else {
$machine = Machine::where('work_center', $workCenter)->where('plant_id', $plantId)->first();
if (! $machine) {
$warnMsg[] = 'Work center not found for the given plant!';
} else {
$machine = Machine::where('work_center', $workCenter)->where('plant_id', $plantId)->where('line_id', $lineId)->first();
if (! $machine) {
$warnMsg[] = "Work center '{$workCenter}' is not mapped for the given line!";
} else {
$machine = Machine::where('work_center', $workCenter)->where('plant_id', $plantId)->where('line_id', $lineId)->where('work_group_master_id', $workGroupMasterId)->first();
if (! $machine) {
$warnMsg[] = "Work center '{$workCenter}' is not mapped for the given group work center!";
} else {
$machineId = $machine->id;
}
}
}
}
}
}
}
}
}
$user = User::where('name', $createdBy)->first();
if (! $user) {
$warnMsg[] = 'Created by user not found!';
} else {
$updatedBy = $createdBy;
}
if ($inspectTyp == 'Value') {
if (is_null($upper) || is_null($lower) || is_null($middle)) {
$warnMsg[] = 'Upper, Lower, and Middle values are required.';
} elseif (! is_numeric($upper) || ! is_numeric($lower) || ! is_numeric($middle)) {
$warnMsg[] = 'Upper, Lower, and Middle values must be numeric.';
} else {
$lower = (float) $lower;
$middle = (float) $middle;
$upper = (float) $upper;
if ($lower == $upper) {
if ($lower != $middle) {
$warnMsg[] = "For 'Value' inspection type, values must satisfy: Lower = Middle = Upper.";
}
} elseif (! ($lower < $middle && $middle < $upper)) {
$warnMsg[] = "For 'Value' inspection type, values must satisfy: Lower < Middle < Upper.";
}
}
} else {
$lower = 0;
$middle = 0;
$upper = 0;
}
} }
// else
// {
// ProductCharacteristicsMaster::updateOrCreate(
// [
// 'plant_id' => $plant->id,
// 'item_id' => $itemId,
// 'line_id' => $LineId,
// 'work_group_master_id' => $workGroupMasterId,
// 'machine_id'=> $machineId,
// ], if (! empty($warnMsg)) {
// [ throw new RowImportFailedException(implode(', ', $warnMsg));
// 'name' => $this->data['name'], }
// 'characteristics_type' => $this->data['characteristics_type'],
// 'inspection_type' => $this->data['inspection_type'],
// 'upper' => $this->data['upper'] ?? null,
// 'lower' => $this->data['lower'] ?? null,
// 'middle' => $this->data['middle'] ?? null,
// //'created_by' => user ?? $this->data['created_by'],
// 'created_by' => Auth::check() ? Auth::user()->name :($this->data['created_by'] ?? null)
// ]
// );
// return null; if ($machineId) {
// } $record = ProductCharacteristicsMaster::where('plant_id', $plantId)->where('line_id', $lineId)->where('work_group_master_id', $workGroupMasterId)->where('machine_id', $machineId)->where('item_id', $itemId)->where('characteristics_type', $charTyp)->where('name', $charNam)->first();
if ($record) {
$createdBy = $record->created_by ?? $createdBy;
}
//return new WorkGroupMaster(); ProductCharacteristicsMaster::updateOrCreate(
[
'plant_id' => $plantId,
'item_id' => $itemId,
'line_id' => $lineId,
'work_group_master_id' => $workGroupMasterId,
'machine_id' => $machineId,
'characteristics_type' => $charTyp,
'name' => $charNam,
],
[
'category' => $category,
'inspection_type' => $inspectTyp,
'lower' => $lower,
'middle' => $middle,
'upper' => $upper,
'created_by' => $createdBy,
'updated_by' => $updatedBy,
]);
}
// return new ProductCharacteristicsMaster(); return null;
// return new WorkGroupMaster();
// return new ProductCharacteristicsMaster();
} }
public static function getCompletedNotificationBody(Import $import): string public static function getCompletedNotificationBody(Import $import): string
{ {
$body = 'Your product characteristics master import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.'; $body = 'Your product characteristics master import has completed and '.number_format($import->successful_rows).' '.str('row')->plural($import->successful_rows).' imported.';
if ($failedRowsCount = $import->getFailedRowsCount()) { if ($failedRowsCount = $import->getFailedRowsCount()) {
$body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to import.'; $body .= ' '.number_format($failedRowsCount).' '.str('row')->plural($failedRowsCount).' failed to import.';
} }
return $body; return $body;