From 95624ea9ea57c41083ab2cd45f21cc2c208ec321 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Fri, 20 Nov 2026 11:52:57 +0530 Subject: [PATCH] Added item characteristics excel import functionality with validations --- .../Imports/ItemCharacteristicImporter.php | 4 +- .../Resources/ItemCharacteristicResource.php | 396 +++++++++--------- 2 files changed, 201 insertions(+), 199 deletions(-) diff --git a/app/Filament/Imports/ItemCharacteristicImporter.php b/app/Filament/Imports/ItemCharacteristicImporter.php index 8bf350dc..63e82379 100644 --- a/app/Filament/Imports/ItemCharacteristicImporter.php +++ b/app/Filament/Imports/ItemCharacteristicImporter.php @@ -744,8 +744,8 @@ class ItemCharacteristicImporter extends Importer $warnMsg[] = 'Invalid item code found'; } - if(!empty($iCode)){ - $item = Item::where('plant_id',$plant->id)->where('code', $iCode)->first(); + if (! empty($iCode)) { + $item = Item::where('plant_id', $plant->id)->where('code', $iCode)->first(); if (! $item) { $warnMsg[] = 'Item Code not found'; diff --git a/app/Filament/Resources/ItemCharacteristicResource.php b/app/Filament/Resources/ItemCharacteristicResource.php index 6eee694d..8fd0793d 100644 --- a/app/Filament/Resources/ItemCharacteristicResource.php +++ b/app/Filament/Resources/ItemCharacteristicResource.php @@ -27,6 +27,7 @@ use Filament\Tables\Table; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\SoftDeletingScope; use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Str; use Maatwebsite\Excel\Facades\Excel; class ItemCharacteristicResource extends Resource @@ -953,11 +954,6 @@ class ItemCharacteristicResource extends Resource ->alignCenter() ->sortable() ->toggleable(isToggledHiddenByDefault: true), - Tables\Columns\TextColumn::make('aufnr') - ->label('AUFNR') - ->alignCenter() - ->searchable() - ->sortable(), Tables\Columns\TextColumn::make('class') ->label('CLASS') ->alignCenter() @@ -1652,52 +1648,47 @@ class ItemCharacteristicResource extends Resource ]), ]) ->headerActions([ - Tables\Actions\Action::make('Import Serial Number') + Tables\Actions\Action::make('Import Item Characteristics (XLSX)') ->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) { + ->options(function () { $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(); + 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); + $set('item_characteristics', null); $plantId = $get('plant_id'); if (! $plantId) { - $set('invoice_serial_number', null); + $set('item_characteristics', null); return; } $plantCode = Plant::find($plantId)?->code ?? null; - $directory = "uploads/temp/{$plantCode}"; + $directory = "uploads/classcharacteristic/{$plantCode}"; if ($plantId && ! Storage::disk('local')->exists($directory)) { Storage::disk('local')->makeDirectory($directory); } }) ->reactive(), - FileUpload::make('invoice_serial_number') - ->label('Choose Serial Invoice') + FileUpload::make('item_characteristics') + ->label('Choose Item Characteristics') ->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) + ->rules(['mimes:xlsx', 'max:5120']) // Laravel validation: extension check + ->maxSize(5120) ->preserveFilenames() // <- this keeps the original filename ->reactive() ->storeFiles(false) // prevent auto-storing, we will store manually @@ -1707,13 +1698,13 @@ class ItemCharacteristicResource extends Resource $plant = Plant::find($get('plant_id')); $plantCode = $plant?->code ?? null; - return "uploads/temp/{$plantCode}"; + return "uploads/classcharacteristic/{$plantCode}"; }) ->uploadingMessage('Uploading...') ->helperText('Only .xlsx files are allowed (Excel files).'), ]) ->action(function (array $data) { - $uploadedFile = $data['invoice_serial_number']; + $uploadedFile = $data['item_characteristics']; $disk = Storage::disk('local'); @@ -1722,6 +1713,9 @@ class ItemCharacteristicResource extends Resource $plant = Plant::find($plantId); $plantCode = $plant?->code ?? null; + $userName = Filament::auth()->user()->name; + $created_by = $userName; + // Get original filename $originalName = $uploadedFile->getClientOriginalName(); // e.g. 3RA0018732.xlsx @@ -1735,77 +1729,10 @@ class ItemCharacteristicResource extends Resource $originalName = "{$originalNameOnly}.xlsx"; // Store manually using storeAs to keep original name - $path = $uploadedFile->storeAs("uploads/temp/{$plantCode}", $originalName, 'local'); // returns relative path + $path = $uploadedFile->storeAs("uploads/classcharacteristic/{$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'.
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]; @@ -1825,57 +1752,31 @@ class ItemCharacteristicResource extends Resource } $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)) { + // $plantCode = trim($row[0]); + $materialCode = trim($row[1]); + + if (empty($materialCode)) { + continue; + } else { 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; - } - } + $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') @@ -1886,44 +1787,11 @@ class ItemCharacteristicResource extends Resource $disk->delete($path); } - return; - } elseif (! empty($uniqueMissingSerials)) { - Notification::make() - ->title('Missing Serial Numbers') - ->body("The following item codes doesn't have valid serial number:
".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:
'.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:
'.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 + ->title('Invalid Item Characteristics Excel') + ->danger() ->body('Uploaded Excel sheet is empty or
contains no valid data.') ->send(); if ($disk->exists($path)) { @@ -1935,20 +1803,16 @@ class ItemCharacteristicResource extends Resource $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(); + $matchedItems = Item::where('plant_id', $plantId)->whereIn('code', $uniqueCodes)->get(); - $matchedCodes = $matchedItems->pluck('item.code')->toArray(); + $matchedCodes = $matchedItems->pluck('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:
'.implode(', ', $missingCodes); + $message = $missingCount > 10 ? "'$missingCount' item codes are not found in items table." : 'The following item codes are not found in items table:
'.implode(', ', $missingCodes); Notification::make() ->title('Unknown Item Codes') @@ -1963,43 +1827,181 @@ class ItemCharacteristicResource extends Resource 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); + foreach ($rows as $index => $row) { + if ($index == 0) { + continue; } - return; - } elseif (count($invalidCodes) > 0) { - Notification::make() - ->title('Invalid item codes found') - ->body('Material invoice Item Codes found : '.implode(', ', $invalidCodes)) - ->danger() - ->send(); + $iCode = trim($row[1]); - if ($disk->exists($path)) { - $disk->delete($path); + if (empty($materialCode)) { + continue; } - return; - } else { - session(['uploaded_invoice_path' => $fullPath]); - Notification::make() - ->title('Serial invoice imported successfully.') - ->success() - ->send(); + $item = Item::where('plant_id', $plantId)->where('code', $iCode)->first(); + + if ($item) { + $itemId = $item->id; + + $itemChar = ItemCharacteristic::where('plant_id', $plantId)->where('item_id', $itemId)->first(); + if ($itemChar) { + $created_by = $itemChar->created_by; + } + } else { + continue; + } + + ItemCharacteristic::updateOrCreate([ + 'plant_id' => $plantId, + 'item_id' => $itemId, + ], + [ + 'class' => trim($row[2]) ?? null, + 'arbid' => trim($row[3]) ?? null, + 'zz1_cn_bill_ord' => trim($row[4]) ?? null, + 'zmm_amps' => trim($row[5]) ?? null, + 'zmm_brand' => trim($row[6]) ?? null, + 'zmm_degreeofprotection' => trim($row[7]) ?? null, + 'zmm_delivery' => trim($row[8]) ?? null, + 'zmm_dir_rot' => trim($row[9]) ?? null, + 'zmm_discharge' => trim($row[10]) ?? null, + 'zmm_discharge_max' => trim($row[11]) ?? null, + 'zmm_discharge_min' => trim($row[12]) ?? null, + 'zmm_duty' => trim($row[13]) ?? null, + 'zmm_eff_motor' => trim($row[14]) ?? null, + 'zmm_eff_pump' => trim($row[15]) ?? null, + 'zmm_frequency' => trim($row[16]) ?? null, + 'zmm_head' => trim($row[17]) ?? null, + 'zmm_heading' => trim($row[18]) ?? null, + 'zmm_head_max' => trim($row[19]) ?? null, + 'zmm_head_minimum' => trim($row[20]) ?? null, + 'zmm_idx_eff_mtr' => trim($row[21]) ?? null, + 'zmm_idx_eff_pump' => trim($row[22]) ?? null, + 'zmm_kvacode' => trim($row[23]) ?? null, + 'zmm_maxambtemp' => trim($row[24]) ?? null, + 'zmm_mincoolingflow' => trim($row[25]) ?? null, + 'zmm_motorseries' => trim($row[26]) ?? null, + 'zmm_motor_model' => trim($row[27]) ?? null, + 'zmm_outlet' => trim($row[28]) ?? null, + 'zmm_phase' => trim($row[29]) ?? null, + 'zmm_pressure' => trim($row[30]) ?? null, + 'zmm_pumpflowtype' => trim($row[31]) ?? null, + 'zmm_pumpseries' => trim($row[32]) ?? null, + 'zmm_pump_model' => trim($row[33]) ?? null, + 'zmm_ratedpower' => trim($row[34]) ?? null, + 'zmm_region' => trim($row[35]) ?? null, + 'zmm_servicefactor' => trim($row[36]) ?? null, + 'zmm_servicefactormaximumamps' => trim($row[37]) ?? null, + 'zmm_speed' => trim($row[38]) ?? null, + 'zmm_suction' => trim($row[39]) ?? null, + 'zmm_suctionxdelivery' => trim($row[40]) ?? null, + 'zmm_supplysource' => trim($row[41]) ?? null, + 'zmm_temperature' => trim($row[42]) ?? null, + 'zmm_thrustload' => trim($row[43]) ?? null, + 'zmm_volts' => trim($row[44]) ?? null, + 'zmm_wire' => trim($row[45]) ?? null, + 'zmm_package' => trim($row[46]) ?? null, + 'zmm_pvarrayrating' => trim($row[47]) ?? null, + 'zmm_isi' => trim($row[48]) ?? null, + 'zmm_isimotor' => trim($row[49]) ?? null, + 'zmm_isipump' => trim($row[50]) ?? null, + 'zmm_isipumpset' => trim($row[51]) ?? null, + 'zmm_pumpset_model' => trim($row[52]) ?? null, + 'zmm_stages' => trim($row[53]) ?? null, + 'zmm_headrange' => trim($row[54]) ?? null, + 'zmm_overall_efficiency' => trim($row[55]) ?? null, + 'zmm_connection' => trim($row[56]) ?? null, + 'zmm_min_bore_size' => trim($row[57]) ?? null, + 'zmm_isireference' => trim($row[58]) ?? null, + 'zmm_category' => trim($row[59]) ?? null, + 'zmm_submergence' => trim($row[60]) ?? null, + 'zmm_capacitorstart' => trim($row[61]) ?? null, + 'zmm_capacitorrun' => trim($row[62]) ?? null, + 'zmm_inch' => trim($row[63]) ?? null, + 'zmm_motor_type' => trim($row[64]) ?? null, + 'zmm_dismantle_direction' => trim($row[65]) ?? null, + 'zmm_eff_ovrall' => trim($row[66]) ?? null, + 'zmm_bodymoc' => trim($row[67]) ?? null, + 'zmm_rotormoc' => trim($row[68]) ?? null, + 'zmm_dlwl' => trim($row[69]) ?? null, + 'zmm_inputpower' => trim($row[70]) ?? null, + 'zmm_imp_od' => trim($row[71]) ?? null, + 'zmm_ambtemp' => trim($row[72]) ?? null, + 'zmm_de' => trim($row[73]) ?? null, + 'zmm_dischargerange' => trim($row[74]) ?? null, + 'zmm_efficiency_class' => trim($row[75]) ?? null, + 'zmm_framesize' => trim($row[76]) ?? null, + 'zmm_impellerdiameter' => trim($row[77]) ?? null, + 'zmm_insulationclass' => trim($row[78]) ?? null, + 'zmm_maxflow' => trim($row[79]) ?? null, + 'zmm_minhead' => trim($row[80]) ?? null, + 'zmm_mtrlofconst' => trim($row[81]) ?? null, + 'zmm_nde' => trim($row[82]) ?? null, + 'zmm_powerfactor' => trim($row[83]) ?? null, + 'zmm_tagno' => trim($row[84]) ?? null, + 'zmm_year' => trim($row[85]) ?? null, + 'zmm_laser_name' => trim($row[86]) ?? null, + 'zmm_logo_cp' => trim($row[87]) ?? null, + 'zmm_logo_ce' => trim($row[88]) ?? null, + 'zmm_logo_nsf' => trim($row[89]) ?? null, + 'zmm_logo_eac' => trim($row[90]) ?? null, + 'zmm_beenote' => trim($row[91]) ?? null, + 'zmm_beenumber' => trim($row[92]) ?? null, + 'zmm_beestar' => trim($row[93]) ?? null, + 'zmm_codeclass' => trim($row[94]) ?? null, + 'zmm_colour' => trim($row[95]) ?? null, + 'zmm_grade' => trim($row[96]) ?? null, + 'zmm_grwt_pset' => trim($row[97]) ?? null, + 'zmm_grwt_cable' => trim($row[98]) ?? null, + 'zmm_grwt_motor' => trim($row[99]) ?? null, + 'zmm_grwt_pf' => trim($row[100]) ?? null, + 'zmm_grwt_pump' => trim($row[101]) ?? null, + 'zmm_isivalve' => trim($row[102]) ?? null, + 'zmm_isi_wc' => trim($row[103]) ?? null, + 'zmm_labelperiod' => trim($row[104]) ?? null, + 'zmm_length' => trim($row[105]) ?? null, + 'zmm_license_cml_no' => trim($row[106]) ?? null, + 'zmm_mfgmonyr' => trim($row[107]) ?? null, + 'zmm_modelyear' => trim($row[108]) ?? null, + 'zmm_motoridentification' => trim($row[109]) ?? null, + 'zmm_newt_pset' => trim($row[110]) ?? null, + 'zmm_newt_cable' => trim($row[111]) ?? null, + 'zmm_newt_motor' => trim($row[112]) ?? null, + 'zmm_newt_pf' => trim($row[113]) ?? null, + 'zmm_newt_pump' => trim($row[114]) ?? null, + 'zmm_packtype' => trim($row[115]) ?? null, + 'zmm_panel' => trim($row[116]) ?? null, + 'zmm_performance_factor' => trim($row[117]) ?? null, + 'zmm_pumpidentification' => trim($row[118]) ?? null, + 'zmm_psettype' => trim($row[119]) ?? null, + 'zmm_size' => trim($row[120]) ?? null, + 'zmm_eff_ttl' => trim($row[121]) ?? null, + 'zmm_type' => trim($row[122]) ?? null, + 'zmm_usp' => trim($row[123]) ?? null, + 'zmm_operating_range' => trim($row[124]) ?? null, + 'zmm_intake_air' => trim($row[125]) ?? null, + 'zmm_oxygen_transfer_rate' => trim($row[126]) ?? null, + 'zmm_air_inlet_pipesize' => trim($row[127]) ?? null, + 'zmm_sump_depth' => trim($row[128]) ?? null, + 'zmm_poles' => trim($row[129]) ?? null, + 'zmm_motor_heading' => trim($row[130]) ?? null, + 'zmm_motor_speed' => trim($row[131]) ?? null, + 'zqmm_qty' => trim($row[132]) ?? null, + 'zmm_operating_temperature' => trim($row[133]) ?? null, + 'zmm_axial_force' => trim($row[134]) ?? null, + 'zmm_mrp' => trim($row[135]) ?? null, + 'created_by' => $created_by ?? $userName, + 'updated_at' => now()->format('Y-m-d H:i:s'), + 'updated_by' => $userName, + ] + ); } + + Notification::make() + ->title('Import Successful') + ->success() + ->body('Item characteristics imported successfully.') + ->send(); } }) ->visible(function () {