2 Commits

Author SHA1 Message Date
18f60ec336 Merge pull request 'changed to plant code for product characteritics master importer and exporter' (#145) from ranjith-dev into master
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 12s
Reviewed-on: #145
2026-01-03 07:32:56 +00:00
dhanabalan
3b11781132 changed to plant code for product characteritics master importer and exporter
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 10s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 10s
Gemini PR Review / review (pull_request) Failing after 30s
Laravel Pint / pint (pull_request) Successful in 2m30s
Laravel Larastan / larastan (pull_request) Failing after 3m9s
2026-01-03 13:02:44 +05:30
2 changed files with 127 additions and 131 deletions

View File

@@ -14,6 +14,7 @@ class ProductCharacteristicsMasterExporter extends Exporter
public static function getColumns(): array public static function getColumns(): array
{ {
static $rowNumber = 0; static $rowNumber = 0;
return [ return [
ExportColumn::make('no') ExportColumn::make('no')
->label('NO') ->label('NO')
@@ -21,12 +22,12 @@ class ProductCharacteristicsMasterExporter extends Exporter
// Increment and return the row number // Increment and return the row number
return ++$rowNumber; return ++$rowNumber;
}), }),
ExportColumn::make('plant.name') ExportColumn::make('plant.code')
->label('PLANT'), ->label('PLANT CODE'),
ExportColumn::make('item.code') ExportColumn::make('item.code')
->label('ITEM CODE'), ->label('ITEM CODE'),
ExportColumn::make('line.name') ExportColumn::make('line.name')
->label('LINE NAME'), //machine.workGroupMaster.name ->label('LINE NAME'), // machine.workGroupMaster.name
ExportColumn::make('machine.workGroupMaster.name') ExportColumn::make('machine.workGroupMaster.name')
->label('WORK GROUP MASTER'), ->label('WORK GROUP MASTER'),
ExportColumn::make('machine.work_center') ExportColumn::make('machine.work_center')
@@ -34,15 +35,15 @@ class ProductCharacteristicsMasterExporter extends Exporter
ExportColumn::make('characteristics_type') ExportColumn::make('characteristics_type')
->label('CHARACTERISTICS TYPE'), ->label('CHARACTERISTICS TYPE'),
ExportColumn::make('name') ExportColumn::make('name')
->label('NAME'), ->label('CHARACTERISTICS NAME'),
ExportColumn::make('inspection_type') ExportColumn::make('inspection_type')
->label('INSPECTION TYPE'), ->label('INSPECTION TYPE'),
ExportColumn::make('upper')
->label('UPPER'),
ExportColumn::make('lower') ExportColumn::make('lower')
->label('LOWER'), ->label('LOWER'),
ExportColumn::make('middle') ExportColumn::make('middle')
->label('MIDDLE'), ->label('MIDDLE'),
ExportColumn::make('upper')
->label('UPPER'),
ExportColumn::make('created_at') ExportColumn::make('created_at')
->label('CREATED AT'), ->label('CREATED AT'),
ExportColumn::make('updated_at') ExportColumn::make('updated_at')
@@ -59,10 +60,10 @@ class ProductCharacteristicsMasterExporter extends Exporter
public static function getCompletedNotificationBody(Export $export): string public static function getCompletedNotificationBody(Export $export): string
{ {
$body = 'Your product characteristics master export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.'; $body = 'Your product characteristics master export has completed and '.number_format($export->successful_rows).' '.str('row')->plural($export->successful_rows).' exported.';
if ($failedRowsCount = $export->getFailedRowsCount()) { if ($failedRowsCount = $export->getFailedRowsCount()) {
$body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to export.'; $body .= ' '.number_format($failedRowsCount).' '.str('row')->plural($failedRowsCount).' failed to export.';
} }
return $body; return $body;

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 Str;
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Str;
class ProductCharacteristicsMasterImporter extends Importer class ProductCharacteristicsMasterImporter extends Importer
{ {
@@ -25,38 +25,38 @@ class ProductCharacteristicsMasterImporter extends Importer
return [ return [
ImportColumn::make('plant') ImportColumn::make('plant')
->requiredMapping() ->requiredMapping()
->exampleHeader('Plant Name') ->exampleHeader('Plant Code')
->example('Ransar Industries-I') ->example('1000')
->label('Plant Name') ->label('Plant Code')
->relationship(resolveUsing:'name') ->relationship(resolveUsing: 'code')
->rules(['required']), ->rules(['required']),
ImportColumn::make('item') ImportColumn::make('item')
->requiredMapping() ->requiredMapping()
->exampleHeader('Item Code') ->exampleHeader('Item Code')
->example('630214') ->example('630214')
->label('Item Code') ->label('Item Code')
->relationship(resolveUsing:'code') ->relationship(resolveUsing: 'code')
->rules(['required']), ->rules(['required']),
ImportColumn::make('line') ImportColumn::make('line')
->requiredMapping() ->requiredMapping()
->exampleHeader('Line') ->exampleHeader('Line Name')
->example('4 inch pump line') ->example('4 inch pump line')
->label('Line') ->label('Line Name')
->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('RMGCGABC')
->relationship(resolveUsing:'name') ->relationship(resolveUsing: 'name')
->rules(['required']), ->rules(['required']),
ImportColumn::make('machine') ImportColumn::make('machine')
->requiredMapping() ->requiredMapping()
->exampleHeader('Work Center') ->exampleHeader('Work Center')
->example('RMGCE001') ->example('RMGCE001')
->label('Work Center') ->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') ->exampleHeader('Characteristics Type')
@@ -64,9 +64,9 @@ class ProductCharacteristicsMasterImporter extends Importer
->label('Characteristics Type') ->label('Characteristics Type')
->rules(['required']), ->rules(['required']),
ImportColumn::make('name') ImportColumn::make('name')
->exampleHeader('Name') ->exampleHeader('Characteristics Name')
->example('Body') ->example('Body')
->label('Name') ->label('Characteristics Name')
->rules(['required']), ->rules(['required']),
ImportColumn::make('inspection_type') ImportColumn::make('inspection_type')
->exampleHeader('Inspection Type') ->exampleHeader('Inspection Type')
@@ -93,7 +93,7 @@ class ProductCharacteristicsMasterImporter extends Importer
->exampleHeader('Created By') ->exampleHeader('Created By')
->example('Admin') ->example('Admin')
->label('Created By'), ->label('Created By'),
//ImportColumn::make('updated_by'), // ImportColumn::make('updated_by'),
]; ];
} }
@@ -102,132 +102,127 @@ class ProductCharacteristicsMasterImporter extends Importer
{ {
$warnMsg = []; $warnMsg = [];
$plant = Plant::where('name', $this->data['plant'])->first(); $plantCod = $this->data['plant'];
if (!$plant) { $plant = null;
$warnMsg[] = "Plant not found"; 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 {
$itemExists = Item::where('code', $this->data['item'])->first();
if (! $itemExists) {
$warnMsg[] = 'Item not found';
}
$itemExists = Item::where('code', $this->data['item'])->first(); $itemAgainstPlant = Item::where('code', $this->data['item'])
if (!$itemExists) { ->where('plant_id', $plant->id)
$warnMsg[] = "Item not found"; ->first();
} if (! $itemAgainstPlant) {
$warnMsg[] = 'Item code not found for the given plant';
} else {
$itemId = $itemAgainstPlant->id;
}
$itemAgainstPlant = Item::where('code', $this->data['item']) $lineExists = Line::where('name', $this->data['line'])->first();
->where('plant_id', $plant->id) if (! $lineExists) {
->first(); $warnMsg[] = 'Line not found';
if (!$itemAgainstPlant) { }
$warnMsg[] = "Item code not found for the given plant";
}
else {
$itemId = $itemAgainstPlant->id;
}
$lineExists = Line::where('name', $this->data['line'])->first(); $lineAgainstPlant = Line::where('name', $this->data['line'])
if (!$lineExists) { ->where('plant_id', $plant->id)
$warnMsg[] = "Line not found"; ->first();
}
$lineAgainstPlant = Line::where('name', $this->data['line']) if (! $lineAgainstPlant) {
->where('plant_id', $plant->id) $warnMsg[] = 'Line not found for the given plant';
->first(); } else {
$LineId = $lineAgainstPlant->id;
}
if (!$lineAgainstPlant) { $WorkgroupMaster = WorkGroupMaster::where('name', $this->data['work_group_master_id'])->where('plant_id', $plant->id)->first();
$warnMsg[] = "Line not found for the given plant"; if (! $WorkgroupMaster) {
} $warnMsg[] = 'Work Group Master value not found';
else } else {
{
$LineId = $lineAgainstPlant->id;
}
$WorkgroupMaster = WorkGroupMaster::where('name', $this->data['work_group_master_id'])->where('plant_id', $plant->id)->first(); $workGroupMasterId = $WorkgroupMaster->id;
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();
// 2. Now check if this WorkGroupMaster id exists in ANY of the 10 columns if (! $existsInLine) {
$existsInLine = Line::where('plant_id', $plant->id) $warnMsg[] = "Work Group Master '{$WorkgroupMaster->name}' is not mapped to any line in this plant";
->where(function ($q) use ($workGroupMasterId) { } else {
for ($i = 1; $i <= 10; $i++) { $workGroupMasterId = $WorkgroupMaster->id;
$q->orWhere("work_group{$i}_id", $workGroupMasterId);
} }
}) }
->exists();
if (!$existsInLine) { $machine = Machine::where('work_center', $this->data['machine'])->first();
$warnMsg[] = "Work Group Master '{$WorkgroupMaster->name}' is not mapped to any line in this plant"; if (! $machine) {
} $warnMsg[] = 'Work Center not found';
else { } else {
$workGroupMasterId = $WorkgroupMaster->id; $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)) {
$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 {
else
{
$record = ProductCharacteristicsMaster::firstOrNew([ $record = ProductCharacteristicsMaster::firstOrNew([
'plant_id' => $plant->id, 'plant_id' => $plant->id,
'item_id' => $itemId, 'item_id' => $itemId,
'line_id' => $LineId, 'line_id' => $LineId,
'work_group_master_id' => $workGroupMasterId, 'work_group_master_id' => $workGroupMasterId,
'machine_id' => $machineId, 'machine_id' => $machineId,
]); ]);
$currentUser = Auth::check() ? Auth::user()->name : ($this->data['created_by'] ?? 'System'); $currentUser = Auth::check() ? Auth::user()->name : ($this->data['created_by'] ?? 'System');
// If record is new, fill all fields // If record is new, fill all fields
if (!$record->exists) { if (! $record->exists) {
$record->name = $this->data['name']; $record->name = $this->data['name'];
$record->characteristics_type = $this->data['characteristics_type']; $record->characteristics_type = $this->data['characteristics_type'];
$record->inspection_type = $this->data['inspection_type']; $record->inspection_type = $this->data['inspection_type'];
$record->upper = $this->data['upper'] ?? null; $record->upper = $this->data['upper'] ?? null;
$record->lower = $this->data['lower'] ?? null; $record->lower = $this->data['lower'] ?? null;
$record->middle = $this->data['middle'] ?? null; $record->middle = $this->data['middle'] ?? null;
$record->created_by = $currentUser; $record->created_by = $currentUser;
} else { } else {
// Record exists → update only updated_by and updated_at // Record exists → update only updated_by and updated_at
$record->updated_by = $currentUser; $record->updated_by = $currentUser;
@@ -264,17 +259,17 @@ class ProductCharacteristicsMasterImporter extends Importer
// return null; // return null;
// } // }
//return new WorkGroupMaster(); // return new WorkGroupMaster();
// return new ProductCharacteristicsMaster(); // 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;