Files
pds/app/Filament/Imports/ProductCharacteristicsMasterImporter.php
dhanabalan 3b11781132
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
changed to plant code for product characteritics master importer and exporter
2026-01-03 13:02:44 +05:30

278 lines
11 KiB
PHP

<?php
namespace App\Filament\Imports;
use App\Models\Item;
use App\Models\Line;
use App\Models\Machine;
use App\Models\Plant;
use App\Models\ProductCharacteristicsMaster;
use App\Models\User;
use App\Models\WorkGroupMaster;
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import;
use Illuminate\Support\Facades\Auth;
use Str;
class ProductCharacteristicsMasterImporter extends Importer
{
protected static ?string $model = ProductCharacteristicsMaster::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('item')
->requiredMapping()
->exampleHeader('Item Code')
->example('630214')
->label('Item Code')
->relationship(resolveUsing: 'code')
->rules(['required']),
ImportColumn::make('line')
->requiredMapping()
->exampleHeader('Line Name')
->example('4 inch pump line')
->label('Line Name')
->relationship(resolveUsing: 'name')
->rules(['required']),
ImportColumn::make('work_group_master_id')
->label('Group Work Center')
->requiredMapping()
->exampleHeader('Group Work Center')
->example('RMGCGABC')
->relationship(resolveUsing: 'name')
->rules(['required']),
ImportColumn::make('machine')
->requiredMapping()
->exampleHeader('Work Center')
->example('RMGCE001')
->label('Work Center')
->relationship(resolveUsing: 'work_center')
->rules(['required']),
ImportColumn::make('characteristics_type')
->exampleHeader('Characteristics Type')
->example('Process or Product')
->label('Characteristics Type')
->rules(['required']),
ImportColumn::make('name')
->exampleHeader('Characteristics Name')
->example('Body')
->label('Characteristics Name')
->rules(['required']),
ImportColumn::make('inspection_type')
->exampleHeader('Inspection Type')
->example('Visual or Value')
->label('Inspection Type')
->rules(['required']),
ImportColumn::make('lower')
->exampleHeader('Lower')
->example('-0.2')
->label('Lower')
->rules(['numeric']),
ImportColumn::make('middle')
->exampleHeader('Middle')
->example('1')
->label('Middle')
->numeric()
->rules(['numeric']),
ImportColumn::make('upper')
->exampleHeader('Upper')
->example('0.2')
->label('Upper')
->rules(['numeric']),
ImportColumn::make('created_by')
->exampleHeader('Created By')
->example('Admin')
->label('Created By'),
// ImportColumn::make('updated_by'),
];
}
public function resolveRecord(): ?ProductCharacteristicsMaster
{
$warnMsg = [];
$plantCod = $this->data['plant'];
$plant = null;
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';
}
$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));
} 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');
// If record is new, fill all fields
if (! $record->exists) {
$record->name = $this->data['name'];
$record->characteristics_type = $this->data['characteristics_type'];
$record->inspection_type = $this->data['inspection_type'];
$record->upper = $this->data['upper'] ?? null;
$record->lower = $this->data['lower'] ?? null;
$record->middle = $this->data['middle'] ?? null;
$record->created_by = $currentUser;
} else {
// Record exists → update only updated_by and updated_at
$record->updated_by = $currentUser;
$record->touch();
}
$record->save();
return null;
}
// else
// {
// ProductCharacteristicsMaster::updateOrCreate(
// [
// 'plant_id' => $plant->id,
// 'item_id' => $itemId,
// 'line_id' => $LineId,
// 'work_group_master_id' => $workGroupMasterId,
// 'machine_id'=> $machineId,
// ],
// [
// '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;
// }
// return new WorkGroupMaster();
// return new ProductCharacteristicsMaster();
}
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.';
if ($failedRowsCount = $import->getFailedRowsCount()) {
$body .= ' '.number_format($failedRowsCount).' '.str('row')->plural($failedRowsCount).' failed to import.';
}
return $body;
}
}