diff --git a/app/Filament/Resources/InvoiceValidationResource.php b/app/Filament/Resources/InvoiceValidationResource.php index 0a62e86..ab2a6c8 100644 --- a/app/Filament/Resources/InvoiceValidationResource.php +++ b/app/Filament/Resources/InvoiceValidationResource.php @@ -67,7 +67,7 @@ class InvoiceValidationResource extends Resource ->options(function (callable $get) { $userHas = Filament::auth()->user()->plant_id; - return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray(); + return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); }) ->default(function () { return optional(InvoiceValidation::latest()->first())->plant_id; @@ -332,7 +332,7 @@ class InvoiceValidationResource extends Resource ->options(function (callable $get) { $userHas = Filament::auth()->user()->plant_id; - return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray(); + return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); }) ->label('Select Plant') ->required() @@ -341,6 +341,13 @@ class InvoiceValidationResource extends Resource }) ->afterStateUpdated(function ($state, callable $set, callable $get) { $set('invoice_serial_number', null); + $plantId = $get('plant_id'); + $plantCode = Plant::find($plantId)?->code ?? null; + + $directory = "uploads/temp/{$plantCode}"; + if ($plantId && ! Storage::disk('local')->exists($directory)) { + Storage::disk('local')->makeDirectory($directory); + } }) ->reactive(), @@ -353,7 +360,12 @@ class InvoiceValidationResource extends Resource ->required() ->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('uploads/temp'), + ->directory(function (callable $get) { + $plant = Plant::find($get('plant_id')); + $plantCode = $plant?->code ?? null; + + return "uploads/temp/{$plantCode}"; + }), ]) ->action(function (array $data) { $uploadedFile = $data['invoice_serial_number']; @@ -362,17 +374,33 @@ class InvoiceValidationResource extends Resource $plantId = $data['plant_id']; + $plant = Plant::find($plantId); + $plantCode = $plant?->code ?? null; + // Get original filename $originalName = $uploadedFile->getClientOriginalName(); // e.g. 3RA0018732.xlsx $originalNameOnly = pathinfo($originalName, PATHINFO_FILENAME); // Store manually using storeAs to keep original name - $path = $uploadedFile->storeAs('uploads/temp', $originalName, 'local'); // returns relative path - // uploads/temp/3RA0018735.xlsx + $path = $uploadedFile->storeAs("uploads/temp/{$plantCode}", $originalName, 'local'); // returns relative path + // uploads/temp/{$plantCode}/3RA0018735.xlsx + + if ($originalNameOnly && strlen($originalNameOnly) < 8) { + Notification::make() + ->title("Invoice number : '$originalNameOnly' should be greater than or equal to 8 characters!") + ->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/3RA0018735.xlsx + // /home/iot-dev/projects/pds/storage/app/private/uploads/temp/{$plantCode}/3RA0018735.xlsx $totQuan = InvoiceValidation::where('invoice_number', $originalNameOnly)->count(); if ($totQuan > 0) { @@ -637,7 +665,7 @@ class InvoiceValidationResource extends Resource ->options(function (callable $get) { $userHas = Filament::auth()->user()->plant_id; - return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray(); + return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); }) ->label('Select Plant') ->required() @@ -646,6 +674,13 @@ class InvoiceValidationResource extends Resource }) ->afterStateUpdated(function ($state, callable $set, callable $get) { $set('invoice_material', null); + $plantId = $get('plant_id'); + $plantCode = Plant::find($plantId)?->code ?? null; + + $directory = "uploads/temp/{$plantCode}"; + if ($plantId && ! Storage::disk('local')->exists($directory)) { + Storage::disk('local')->makeDirectory($directory); + } }) ->reactive(), @@ -657,13 +692,21 @@ class InvoiceValidationResource extends Resource ->storeFiles(false) // prevent auto-storing ->disk('local') ->visible(fn (Get $get) => ! empty($get('plant_id'))) - ->directory('uploads/temp'), + ->directory(function (callable $get) { + $plant = Plant::find($get('plant_id')); + $plantCode = $plant?->code ?? null; + + return "uploads/temp/{$plantCode}"; + }), ]) ->action(function (array $data) { $uploadedFile = $data['invoice_material']; $plantId = $data['plant_id']; // Access the selected plant_id + $plant = Plant::find($plantId); + $plantCode = $plant?->code ?? null; + $disk = Storage::disk('local'); // Get original filename @@ -671,7 +714,20 @@ class InvoiceValidationResource extends Resource $originalNameOnly = pathinfo($originalName, PATHINFO_FILENAME); - $path = $uploadedFile->storeAs('uploads/temp', $originalName, 'local'); + $path = $uploadedFile->storeAs("uploads/temp/{$plantCode}", $originalName, 'local'); + + if ($originalNameOnly && strlen($originalNameOnly) < 8) { + Notification::make() + ->title("Invoice number : '$originalNameOnly' should be greater than or equal to 8 characters!") + ->danger() + ->send(); + + if ($disk->exists($path)) { + $disk->delete($path); + } + + return; + } $fullPath = Storage::disk('local')->path($path); @@ -1073,7 +1129,7 @@ class InvoiceValidationResource extends Resource ->options(function (callable $get) { $userHas = Filament::auth()->user()->plant_id; - return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray(); + return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); }) ->reactive() ->afterStateUpdated(function ($state, callable $set, callable $get): void { diff --git a/app/Filament/Resources/InvoiceValidationResource/Pages/CreateInvoiceValidation.php b/app/Filament/Resources/InvoiceValidationResource/Pages/CreateInvoiceValidation.php index e2c8887..3a411a2 100644 --- a/app/Filament/Resources/InvoiceValidationResource/Pages/CreateInvoiceValidation.php +++ b/app/Filament/Resources/InvoiceValidationResource/Pages/CreateInvoiceValidation.php @@ -117,8 +117,8 @@ class CreateInvoiceValidation extends CreateRecord $this->plantId = $plantId; - // $plant = Plant::find($plantId); - // $plantCode = $plant ? $plant->code : null; + $plant = Plant::find($plantId); + $plantCode = $plant?->code ?? null; // ..GET SERIAL INVOICE API @@ -233,7 +233,7 @@ class CreateInvoiceValidation extends CreateRecord $this->dispatch('playNotificationSound'); $filename = $invoiceNumber.'.xlsx'; - $directory = 'uploads/temp'; + $directory = "uploads/temp/{$plantCode}"; $disk = Storage::disk('local'); $filePath = $directory.'/'.$filename; // $fullPath = null; @@ -266,7 +266,7 @@ class CreateInvoiceValidation extends CreateRecord if ($updateStatus == '1') { // 'Material invoice update in progress...'; $filename = $invoiceNumber.'.xlsx'; - $directory = 'uploads/temp'; + $directory = "uploads/temp/{$plantCode}"; $disk = Storage::disk('local'); $filePath = $directory.'/'.$filename; $fullPath = $disk->path($filePath); @@ -877,7 +877,7 @@ class CreateInvoiceValidation extends CreateRecord $this->dispatch('playNotificationSound'); $filename = $invoiceNumber.'.xlsx'; - $directory = 'uploads/temp'; + $directory = "uploads/temp/{$plantCode}"; $disk = Storage::disk('local'); $filePath = $directory.'/'.$filename; // $fullPath = null; @@ -907,16 +907,15 @@ class CreateInvoiceValidation extends CreateRecord if ($updateStatus == '1') { $filename = $invoiceNumber.'.xlsx'; - $directory = 'uploads/temp'; + $directory = "uploads/temp/{$plantCode}"; $disk = Storage::disk('local'); $filePath = $directory.'/'.$filename; $fullPath = $disk->path($filePath); // Check if file exists //if ($disk->exists($filePath)) if ($fullPath && file_exists($fullPath)) { - // /home/iot-dev/projects/pds/storage/app/private/uploads/temp/3RA0018735.xlsx + // /home/iot-dev/projects/pds/storage/app/private/uploads/temp/{$plantCode}/3RA0018735.xlsx // 'Serial invoice update in progress...' - // Now you can read/process the file here $rows = Excel::toArray(null, $fullPath)[0]; @@ -1337,7 +1336,7 @@ class CreateInvoiceValidation extends CreateRecord } $filename = $invoiceNumber.'.xlsx'; - $directory = 'uploads/temp'; + $directory = "uploads/temp/{$plantCode}"; $disk = Storage::disk('local'); $filePath = $directory.'/'.$filename; $fullPath = null; // $fullPath = $disk->path($filePath); @@ -2358,6 +2357,9 @@ class CreateInvoiceValidation extends CreateRecord $invoiceNumber = $this->form->getState()['invoice_number']; $this->invoiceNumber = $invoiceNumber; + $plant = Plant::find($plantId); + $plantCode = $plant?->code ?? null; + $totQuan = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); $scanSQuan = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('scanned_status', 'Scanned')->where('plant_id', $plantId)->count(); $totMQuan = InvoiceValidation::where('invoice_number', $invoiceNumber)->whereNotNull('quantity')->where('plant_id', $plantId)->count(); // ->where('quantity', '!=', '') @@ -2395,7 +2397,7 @@ class CreateInvoiceValidation extends CreateRecord $this->dispatch('playNotificationSound'); $filename = $invoiceNumber.'.xlsx'; - $directory = 'uploads/temp'; + $directory = "uploads/temp/{$plantCode}"; $disk = Storage::disk('local'); $filePath = $directory.'/'.$filename; // $fullPath = null; @@ -3055,7 +3057,7 @@ class CreateInvoiceValidation extends CreateRecord $this->dispatch('playNotificationSound'); $filename = $invoiceNumber.'.xlsx'; - $directory = 'uploads/temp'; + $directory = "uploads/temp/{$plantCode}"; $disk = Storage::disk('local'); $filePath = $directory.'/'.$filename; // $fullPath = null; @@ -3099,7 +3101,7 @@ class CreateInvoiceValidation extends CreateRecord $this->dispatch('playNotificationSound'); $filename = $invoiceNumber.'.xlsx'; - $directory = 'uploads/temp'; + $directory = "uploads/temp/{$plantCode}"; $disk = Storage::disk('local'); $filePath = $directory.'/'.$filename; // $fullPath = null; @@ -3427,7 +3429,7 @@ class CreateInvoiceValidation extends CreateRecord $this->dispatch('playNotificationSound'); $filename = $invoiceNumber.'.xlsx'; - $directory = 'uploads/temp'; + $directory = "uploads/temp/{$plantCode}"; $disk = Storage::disk('local'); $filePath = $directory.'/'.$filename; // $fullPath = null; @@ -3544,7 +3546,7 @@ class CreateInvoiceValidation extends CreateRecord $this->dispatch('playNotificationSound'); $filename = $invoiceNumber.'.xlsx'; - $directory = 'uploads/temp'; + $directory = "uploads/temp/{$plantCode}"; $disk = Storage::disk('local'); $filePath = $directory.'/'.$filename; // $fullPath = null; @@ -3740,7 +3742,7 @@ class CreateInvoiceValidation extends CreateRecord // } $filename = $invoiceNumber.'.xlsx'; - $directory = 'uploads/temp'; + $directory = "uploads/temp/{$plantCode}"; $disk = Storage::disk('local'); $filePath = $directory.'/'.$filename; // $fullPath = null;