Added filter in item characteristic resource page and ordered zmm columns #1476

Merged
jothi merged 1 commits from ranjith-dev into master 2026-11-19 05:50:53 +00:00
Showing only changes of commit c8b0fb7d25 - Show all commits

View File

@@ -10,15 +10,24 @@ use App\Models\ItemCharacteristic;
use App\Models\Plant; use App\Models\Plant;
use Filament\Facades\Filament; use Filament\Facades\Filament;
use Filament\Forms; use Filament\Forms;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Section; use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form; use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Notifications\Notification;
use Filament\Resources\Resource; use Filament\Resources\Resource;
use Filament\Tables; use Filament\Tables;
use Filament\Tables\Actions\ExportAction; use Filament\Tables\Actions\ExportAction;
use Filament\Tables\Actions\ImportAction; use Filament\Tables\Actions\ImportAction;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Table; use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope; use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel;
class ItemCharacteristicResource extends Resource class ItemCharacteristicResource extends Resource
{ {
@@ -64,6 +73,12 @@ class ItemCharacteristicResource extends Resource
->afterStateUpdated(function (callable $set) { ->afterStateUpdated(function (callable $set) {
$set('updated_by', Filament::auth()->user()?->name); $set('updated_by', Filament::auth()->user()?->name);
}), }),
Forms\Components\TextInput::make('arbid')
->label('ARBID')
->reactive()
->afterStateUpdated(function (callable $set) {
$set('updated_by', Filament::auth()->user()?->name);
}),
Forms\Components\TextInput::make('zz1_cn_bill_ord') Forms\Components\TextInput::make('zz1_cn_bill_ord')
->label('ZZ1 CN BILL ORD') ->label('ZZ1 CN BILL ORD')
->reactive() ->reactive()
@@ -871,12 +886,9 @@ class ItemCharacteristicResource extends Resource
{ {
return $table return $table
->columns([ ->columns([
// Tables\Columns\TextColumn::make('id')
// ->label('ID')
// ->numeric()
// ->sortable(),
Tables\Columns\TextColumn::make('No.') Tables\Columns\TextColumn::make('No.')
->label('No.') ->label('NO')
->alignCenter()
->getStateUsing(function ($record, $livewire, $column, $rowLoop) { ->getStateUsing(function ($record, $livewire, $column, $rowLoop) {
$paginator = $livewire->getTableRecords(); $paginator = $livewire->getTableRecords();
$perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10; $perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10;
@@ -885,17 +897,49 @@ class ItemCharacteristicResource extends Resource
return ($currentPage - 1) * $perPage + $rowLoop->iteration; return ($currentPage - 1) * $perPage + $rowLoop->iteration;
}), }),
Tables\Columns\TextColumn::make('plant.name') Tables\Columns\TextColumn::make('plant.name')
->label('PLANT') ->label('PLANT NAME')
->alignCenter() ->alignCenter()
->searchable()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('item.code') Tables\Columns\TextColumn::make('item.code')
->label('ITEM CODE') ->label('ITEM CODE')
->alignCenter() ->alignCenter()
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('item.description')
->label('DESCRIPTION')
->default('-')
->searchable()
->alignCenter()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('item.category')
->label('CATEGORY')
->default('-')
->searchable()
->alignCenter()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('item.uom')
->label('UNIT OF MEASURE')
->default('-')
->searchable()
->alignCenter()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('aufnr')
->label('AUFNR')
->alignCenter()
->searchable()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('class') Tables\Columns\TextColumn::make('class')
->label('CLASS') ->label('CLASS')
->alignCenter() ->alignCenter()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('arbid')
->label('ARBID')
->alignCenter()
->sortable(),
Tables\Columns\TextColumn::make('zz1_cn_bill_ord') Tables\Columns\TextColumn::make('zz1_cn_bill_ord')
->label('ZZ1 CN BILL ORD') ->label('ZZ1 CN BILL ORD')
->alignCenter() ->alignCenter()
@@ -917,7 +961,7 @@ class ItemCharacteristicResource extends Resource
->alignCenter() ->alignCenter()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('zmm_dir_rot') Tables\Columns\TextColumn::make('zmm_dir_rot')
->label('ZMM DIRECTION ROTATION') ->label('ZMM DIR ROT')
->alignCenter() ->alignCenter()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('zmm_discharge') Tables\Columns\TextColumn::make('zmm_discharge')
@@ -1018,7 +1062,6 @@ class ItemCharacteristicResource extends Resource
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('zmm_ratedpower') Tables\Columns\TextColumn::make('zmm_ratedpower')
->label('ZMM RATEDPOWER') ->label('ZMM RATEDPOWER')
->alignCenter()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('zmm_region') Tables\Columns\TextColumn::make('zmm_region')
->label('ZMM REGION') ->label('ZMM REGION')
@@ -1145,7 +1188,7 @@ class ItemCharacteristicResource extends Resource
->alignCenter() ->alignCenter()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('zmm_eff_ovrall') Tables\Columns\TextColumn::make('zmm_eff_ovrall')
->label('ZMM OVERALL EFFICIENCY') ->label('ZMM EFF OVRALL')
->alignCenter() ->alignCenter()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('zmm_bodymoc') Tables\Columns\TextColumn::make('zmm_bodymoc')
@@ -1169,7 +1212,7 @@ class ItemCharacteristicResource extends Resource
->alignCenter() ->alignCenter()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('zmm_ambtemp') Tables\Columns\TextColumn::make('zmm_ambtemp')
->label('ZMM AMBIENT TEMPERATURE') ->label('ZMM AMBTEMP')
->alignCenter() ->alignCenter()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('zmm_de') Tables\Columns\TextColumn::make('zmm_de')
@@ -1189,7 +1232,7 @@ class ItemCharacteristicResource extends Resource
->alignCenter() ->alignCenter()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('zmm_impellerdiameter') Tables\Columns\TextColumn::make('zmm_impellerdiameter')
->label('ZMM IMPPELLER DIAMETER') ->label('ZMM IMPELLERDIAMETER')
->alignCenter() ->alignCenter()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('zmm_insulationclass') Tables\Columns\TextColumn::make('zmm_insulationclass')
@@ -1205,7 +1248,7 @@ class ItemCharacteristicResource extends Resource
->alignCenter() ->alignCenter()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('zmm_mtrlofconst') Tables\Columns\TextColumn::make('zmm_mtrlofconst')
->label('ZMM MOTOR LOAD CONSTANT') ->label('ZMM MTRLOFCONST')
->alignCenter() ->alignCenter()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('zmm_nde') Tables\Columns\TextColumn::make('zmm_nde')
@@ -1217,7 +1260,7 @@ class ItemCharacteristicResource extends Resource
->alignCenter() ->alignCenter()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('zmm_tagno') Tables\Columns\TextColumn::make('zmm_tagno')
->label('ZMM TAGNO') ->label('ZMM TANGO')
->alignCenter() ->alignCenter()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('zmm_year') Tables\Columns\TextColumn::make('zmm_year')
@@ -1228,6 +1271,22 @@ class ItemCharacteristicResource extends Resource
->label('ZMM LASER NAME') ->label('ZMM LASER NAME')
->alignCenter() ->alignCenter()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('zmm_logo_cp')
->label('ZMM LOGO CP')
->alignCenter()
->sortable(),
Tables\Columns\TextColumn::make('zmm_logo_ce')
->label('ZMM LOGO CE')
->alignCenter()
->sortable(),
Tables\Columns\TextColumn::make('zmm_logo_nsf')
->label('ZMM LOGO NSF')
->alignCenter()
->sortable(),
Tables\Columns\TextColumn::make('zmm_logo_eac')
->label('ZMM LOGO EAC')
->alignCenter()
->sortable(),
Tables\Columns\TextColumn::make('zmm_beenote') Tables\Columns\TextColumn::make('zmm_beenote')
->label('ZMM BEENOTE') ->label('ZMM BEENOTE')
->alignCenter() ->alignCenter()
@@ -1240,10 +1299,6 @@ class ItemCharacteristicResource extends Resource
->label('ZMM BEESTAR') ->label('ZMM BEESTAR')
->alignCenter() ->alignCenter()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('zmm_logo_ce')
->label('ZMM LOGO CE')
->alignCenter()
->sortable(),
Tables\Columns\TextColumn::make('zmm_codeclass') Tables\Columns\TextColumn::make('zmm_codeclass')
->label('ZMM CODECLASS') ->label('ZMM CODECLASS')
->alignCenter() ->alignCenter()
@@ -1252,10 +1307,6 @@ class ItemCharacteristicResource extends Resource
->label('ZMM COLOUR') ->label('ZMM COLOUR')
->alignCenter() ->alignCenter()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('zmm_logo_cp')
->label('ZMM LOGO CP')
->alignCenter()
->sortable(),
Tables\Columns\TextColumn::make('zmm_grade') Tables\Columns\TextColumn::make('zmm_grade')
->label('ZMM GRADE') ->label('ZMM GRADE')
->alignCenter() ->alignCenter()
@@ -1332,14 +1383,6 @@ class ItemCharacteristicResource extends Resource
->label('ZMM NEWT PUMP') ->label('ZMM NEWT PUMP')
->alignCenter() ->alignCenter()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('zmm_logo_nsf')
->label('ZMM LOGO NSF')
->alignCenter()
->sortable(),
Tables\Columns\TextColumn::make('zmm_logo_eac')
->label('ZMM LOGO EAC')
->alignCenter()
->sortable(),
Tables\Columns\TextColumn::make('zmm_packtype') Tables\Columns\TextColumn::make('zmm_packtype')
->label('ZMM PACKTYPE') ->label('ZMM PACKTYPE')
->alignCenter() ->alignCenter()
@@ -1409,7 +1452,7 @@ class ItemCharacteristicResource extends Resource
->alignCenter() ->alignCenter()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('zqmm_qty') Tables\Columns\TextColumn::make('zqmm_qty')
->label('ZMM QTY') ->label('ZQMM QTY')
->alignCenter() ->alignCenter()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('zmm_operating_temperature') Tables\Columns\TextColumn::make('zmm_operating_temperature')
@@ -1424,31 +1467,153 @@ class ItemCharacteristicResource extends Resource
->label('ZMM MRP') ->label('ZMM MRP')
->alignCenter() ->alignCenter()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('created_at')
->label('CREATED AT')
->alignCenter()
->dateTime()
->sortable(),
Tables\Columns\TextColumn::make('created_by') Tables\Columns\TextColumn::make('created_by')
->label('CREATED BY') ->label('CREATED BY')
->alignCenter() ->alignCenter(),
->sortable(),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_at') Tables\Columns\TextColumn::make('updated_at')
->label('UPDATED AT')
->alignCenter()
->dateTime() ->dateTime()
->sortable() ->sortable(),
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_by') Tables\Columns\TextColumn::make('updated_by')
->label('UPDATED BY') ->label('UPDATED BY')
->alignCenter() ->alignCenter(),
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('deleted_at') Tables\Columns\TextColumn::make('deleted_at')
->label('DELETED AT')
->alignCenter()
->dateTime() ->dateTime()
->sortable() ->sortable()
->toggleable(isToggledHiddenByDefault: true), ->toggleable(isToggledHiddenByDefault: true),
]) ])
->filters([ ->filters([
Tables\Filters\TrashedFilter::make(), Tables\Filters\TrashedFilter::make(),
Filter::make('advanced_filters')
->label('Advanced Filters')
->form([
Select::make('Plant')
->label('Search by Plant Name')
->nullable()
->searchable()
->reactive()
->options(function (callable $get) {
$userHas = Filament::auth()->user()->plant_id;
if ($userHas && strlen($userHas) > 0) {
return Plant::where('id', $userHas)->pluck('name', 'id')->toArray();
} else {
return Plant::whereHas('ItemCharacteristics', function ($query) {
$query->whereNotNull('id');
})->orderBy('code')->pluck('name', 'id');
}
// return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray();
})
->afterStateUpdated(function ($state, callable $set, callable $get): void {
$set('item_id', null);
}),
Select::make('item_id')
->label('Search by Item Code')
->nullable()
->searchable()
->reactive()
->options(function (callable $get) {
$plantId = $get('Plant');
if (empty($plantId)) {
return [];
}
return Item::whereHas('ItemCharacteristics', function ($query) use ($plantId) {
if ($plantId) {
$query->where('plant_id', $plantId);
}
})->orderBy('code')->pluck('code', 'id');
}),
TextInput::make('zmm_heading')
->label('Heading')
->placeholder('Enter Heading'),
DateTimePicker::make('created_from')
->label('Created From')
->placeholder('Select From DateTime')
->reactive()
->native(false),
DateTimePicker::make('created_to')
->label('Created To')
->placeholder('Select To DateTime')
->reactive()
->native(false),
]) ])
->query(function ($query, array $data) {
// Hide all records initially if no filters are applied
if (empty($data['Plant']) && empty($data['item_id']) && empty($data['zmm_heading']) && empty($data['created_from']) && empty($data['created_to'])) {
return $query->whereRaw('1 = 0');
}
if (! empty($data['Plant'])) { // $plant = $data['Plant'] ?? null
$query->where('plant_id', $data['Plant']);
} else {
$userHas = Filament::auth()->user()->plant_id;
if ($userHas && strlen($userHas) > 0) {
return $query->whereRaw('1 = 0');
}
}
if (! empty($data['item_id'])) {
$query->where('item_id', $data['item_id']);
}
if (! empty($data['zmm_heading'])) {
$query->where('zmm_heading', 'like', '%'.$data['zmm_heading'].'%');
}
if (! empty($data['created_from'])) {
$query->where('created_at', '>=', $data['created_from']);
}
if (! empty($data['created_to'])) {
$query->where('created_at', '<=', $data['created_to']);
}
})
->indicateUsing(function (array $data) {
$indicators = [];
if (! empty($data['Plant'])) {
$indicators[] = 'Plant Name: '.Plant::where('id', $data['Plant'])->value('name');
} else {
$userHas = Filament::auth()->user()->plant_id;
if ($userHas && strlen($userHas) > 0) {
return 'Plant: Choose plant to filter records.';
}
}
if (! empty($data['item_id'])) {
$indicators[] = 'Item Code: '.Item::where('id', $data['item_id'])->value('code');
}
if (! empty($data['zmm_heading'])) {
$indicators[] = 'Heading: '.$data['zmm_heading'];
}
if (! empty($data['created_from'])) {
$indicators[] = 'Created From: '.$data['created_from'];
}
if (! empty($data['created_to'])) {
$indicators[] = 'Created To: '.$data['created_to'];
}
return $indicators;
}),
])
->filtersFormMaxHeight('280px')
->actions([ ->actions([
Tables\Actions\ViewAction::make(), Tables\Actions\ViewAction::make(),
Tables\Actions\EditAction::make(), Tables\Actions\EditAction::make(),
@@ -1461,8 +1626,361 @@ class ItemCharacteristicResource extends Resource
]), ]),
]) ])
->headerActions([ ->headerActions([
Tables\Actions\Action::make('Import Serial Number')
->label('Import Item Characteristics (XLSX)')
->form([
Select::make('plant_id')
// ->options(Plant::pluck('name', 'id')->toArray()) // Fetch plant names and IDs
->options(function (callable $get) {
$userHas = Filament::auth()->user()->plant_id;
return null;
// return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray();
})
->searchable()
->label('Search by Plant Name')
->required()
->default(function () {
return null;
return optional(ItemCharacteristic::latest()->first())->plant_id;
})
->afterStateUpdated(function ($state, callable $set, callable $get) {
$set('invoice_serial_number', null);
$plantId = $get('plant_id');
if (! $plantId) {
$set('invoice_serial_number', null);
return;
}
$plantCode = Plant::find($plantId)?->code ?? null;
$directory = "uploads/temp/{$plantCode}";
if ($plantId && ! Storage::disk('local')->exists($directory)) {
Storage::disk('local')->makeDirectory($directory);
}
})
->reactive(),
FileUpload::make('invoice_serial_number')
->label('Choose Serial Invoice')
->required()
->acceptedFileTypes([
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
// 'application/vnd.ms-excel', // Legacy .xls fallback if needed
])
->rules(['mimes:xlsx', 'max:50']) // Laravel validation: extension check
->maxSize(50)
->preserveFilenames() // <- this keeps the original filename
->reactive()
->storeFiles(false) // prevent auto-storing, we will store manually
->disk('local') // 'local' refers to the local storage disk defined in config/filesystems.php, typically pointing to storage/app.
->visible(fn (Get $get) => ! empty($get('plant_id')))
->directory(function (callable $get) {
$plant = Plant::find($get('plant_id'));
$plantCode = $plant?->code ?? null;
return "uploads/temp/{$plantCode}";
})
->uploadingMessage('Uploading...')
->helperText('Only .xlsx files are allowed (Excel files).'),
])
->action(function (array $data) {
$uploadedFile = $data['invoice_serial_number'];
$disk = Storage::disk('local');
$plantId = $data['plant_id'];
$plant = Plant::find($plantId);
$plantCode = $plant?->code ?? null;
// Get original filename
$originalName = $uploadedFile->getClientOriginalName(); // e.g. 3RA0018732.xlsx
$extension = strtolower(pathinfo($originalName, PATHINFO_EXTENSION));
if ($extension != 'xlsx') {
throw new \Exception('Only .xlsx files allowed.');
}
$originalNameOnly = strtoupper(pathinfo($originalName, PATHINFO_FILENAME));
$originalName = "{$originalNameOnly}.xlsx";
// Store manually using storeAs to keep original name
$path = $uploadedFile->storeAs("uploads/temp/{$plantCode}", $originalName, 'local'); // returns relative path
// uploads/temp/{$plantCode}/3RA0018735.xlsx
if (strlen($originalNameOnly) < 8 || ! ctype_alnum($originalNameOnly)) {
Notification::make()
->title("Serial invoice number : '$originalNameOnly' should contain minimum 8 digit alpha numeric values!")
->danger()
->send();
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
}
$fullPath = Storage::disk('local')->path($path);
// /home/iot-dev/projects/pds/storage/app/private/uploads/temp/{$plantCode}/3RA0018735.xlsx
$totQuan = ItemCharacteristic::where('invoice_number', $originalNameOnly)->count();
if ($totQuan > 0) {
$scanSQuan = ItemCharacteristic::where('invoice_number', $originalNameOnly)->where('scanned_status', 'Scanned')->count();
if ($totQuan == $scanSQuan) {
$invoiceFirst = ItemCharacteristic::with('plant')->where('invoice_number', $originalNameOnly)->first();
$plantName = $invoiceFirst ? (string) $invoiceFirst->plant->name : null;
Notification::make()
->title("Serial invoice number : '$originalNameOnly' already completed the scanning process for plant : '$plantName'.")
->danger()
->send();
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
} else {
$invoiceFirst = ItemCharacteristic::with('plant')->where('invoice_number', $originalNameOnly)->first();
// $plantCode = $invoiceFirst ? (String)$invoiceFirst->plant->code : null;
$plantName = $invoiceFirst ? (string) $invoiceFirst->plant->name : null;
$invoicePlantId = $invoiceFirst->plant_id;
if ($plantId != $invoicePlantId) {
Notification::make()
->title("Serial invoice number : '$originalNameOnly' already exists for plant : '$plantName'.<br>Choose the valid 'Plant' to proceed!")
->danger()
->send();
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
}
}
}
$totQuan = ItemCharacteristic::where('invoice_number', $originalNameOnly)->where('plant_id', $plantId)->count();
$scanSQuan = ItemCharacteristic::where('invoice_number', $originalNameOnly)->where('scanned_status', 'Scanned')->where('plant_id', $plantId)->count();
if ($totQuan > 0 && $totQuan == $scanSQuan) {
Notification::make()
->title('Serial invoice already completed the scanning process for selected plant.')
->danger()
->send();
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
}
if ($fullPath && file_exists($fullPath)) {
$rows = Excel::toArray(null, $fullPath)[0];
if ((count($rows) - 1) <= 0) {
Notification::make()
->title('Records Not Found')
->body("Import the valid 'Item Characteristics Excel' file to proceed..!")
->danger()
->send();
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
}
$invalidMatCodes = [];
$invalidSerialCodes = [];
$materialCodes = [];
$missingSerials = [];
$duplicateSerials = [];
$seenSerialNumbers = [];
$validRowsFound = false;
foreach ($rows as $index => $row) {
if ($index == 0) {
continue;
} // Skip header
$materialCode = trim($row[0]);
$serialNumber = trim($row[1]);
if (empty($materialCode) && empty($serialNumber)) {
continue;
}
if (! empty($materialCode)) {
if (Str::length($materialCode) < 6 || ! ctype_alnum($materialCode)) {
$invalidMatCodes[] = $materialCode;
} else {
if (empty($serialNumber)) {
$missingSerials[] = $materialCode;
} elseif (Str::length($serialNumber) < 9 || ! ctype_alnum($serialNumber)) {
$invalidSerialCodes[] = $serialNumber;
} else {
if (in_array($serialNumber, $seenSerialNumbers)) {
$duplicateSerials[] = $serialNumber;
} else {
$seenSerialNumbers[] = $serialNumber;
$materialCodes[] = $materialCode;
$validRowsFound = true;
}
}
}
} else {
continue;
}
}
$uniqueInvalidCodes = array_unique($invalidMatCodes);
$uniqueMissingSerials = array_unique($missingSerials);
$uniqueSerialCodes = array_unique($invalidSerialCodes);
$duplicateSerialCodes = array_unique($duplicateSerials);
if (! empty($uniqueInvalidCodes)) {
Notification::make()
->title('Invalid Item Codes')
->body('The following item codes should contain minimum 6 digit alpha numeric values:<br>'.implode(', ', $uniqueInvalidCodes))
->danger()
->send();
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
} elseif (! empty($uniqueMissingSerials)) {
Notification::make()
->title('Missing Serial Numbers')
->body("The following item codes doesn't have valid serial number:<br>".implode(', ', $uniqueMissingSerials))
->danger()
->send();
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
} elseif (! empty($uniqueSerialCodes)) {
Notification::make()
->title('Invalid Serial Number')
->body('The following serial numbers should contain minimum 9 digit alpha numeric values:<br>'.implode(', ', $uniqueSerialCodes))
->danger()
->send();
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
} elseif (! empty($duplicateSerialCodes)) {
Notification::make()
->title('Duplicate Serial Numbers')
->body('The following serial numbers are already exist in imported excel:<br>'.implode(', ', $duplicateSerialCodes))
->danger()
->send();
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
} elseif (! $validRowsFound) {
Notification::make()
->title('Invalid Serial Invoice')
->danger() // This makes the notification red to indicate an error
->body('Uploaded Excel sheet is empty or<br>contains no valid data.')
->send();
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
}
$uniqueCodes = array_unique($materialCodes);
$matchedItems = StickerMaster::with('item')->where('plant_id', $plantId)
->whereHas('item', function ($query) use ($uniqueCodes, $plantId) {
$query->whereIn('code', $uniqueCodes)->where('plant_id', $plantId);
})
->get();
$matchedCodes = $matchedItems->pluck('item.code')->toArray();
$missingCodes = array_diff($uniqueCodes, $matchedCodes);
if (! empty($missingCodes)) {
$missingCount = count($missingCodes);
$message = $missingCount > 10 ? "'$missingCount' item codes are not found in sticker master." : 'The following item codes are not found in sticker master:<br>'.implode(', ', $missingCodes);
Notification::make()
->title('Unknown Item Codes')
->body($message)
->danger()
->send();
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
}
// Check which codes have a material_type set (not null)
$invalidCodes = $matchedItems
->filter(fn ($sticker) => ! empty($sticker->material_type)) // filter invalid
->pluck('item.code')
->toArray();
if (count($invalidCodes) > 10) {
Notification::make()
->title('Invalid item codes found')
->body(''.count($invalidCodes).' item codes have material type.')
->danger()
->send();
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
} elseif (count($invalidCodes) > 0) {
Notification::make()
->title('Invalid item codes found')
->body('Material invoice Item Codes found : '.implode(', ', $invalidCodes))
->danger()
->send();
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
} else {
session(['uploaded_invoice_path' => $fullPath]);
Notification::make()
->title('Serial invoice imported successfully.')
->success()
->send();
}
}
})
->visible(function () {
return Filament::auth()->user()->can('view import item characteristics excel');
}),
ImportAction::make() ImportAction::make()
->label('Import Item Characteristics') ->label('Import Item Characteristics (CSV)')
->color('warning') ->color('warning')
->importer(ItemCharacteristicImporter::class) ->importer(ItemCharacteristicImporter::class)
->visible(function () { ->visible(function () {
@@ -1476,7 +1994,6 @@ class ItemCharacteristicResource extends Resource
return Filament::auth()->user()->can('view export item characteristics'); return Filament::auth()->user()->can('view export item characteristics');
}), }),
]); ]);
} }
public static function getRelations(): array public static function getRelations(): array