diff --git a/app/Filament/Exports/InvoiceValidationExporter.php b/app/Filament/Exports/InvoiceValidationExporter.php index a5d5504..ae13217 100644 --- a/app/Filament/Exports/InvoiceValidationExporter.php +++ b/app/Filament/Exports/InvoiceValidationExporter.php @@ -24,14 +24,16 @@ class InvoiceValidationExporter extends Exporter // Increment and return the row number return ++$rowNumber; }), - ExportColumn::make('plant.name') - ->label('PLANT'), + ExportColumn::make('plant.code') + ->label('PLANT CODE'), ExportColumn::make('invoice_number') ->label('INVOICE NUMBER'), ExportColumn::make('serial_number') ->label('SERIAL NUMBER'), ExportColumn::make('stickerMaster.item.code') ->label('ITEM CODE'), + ExportColumn::make('stickerMaster.item.description') + ->label('ITEM DESCRIPTION'), ExportColumn::make('motor_scanned_status') ->label('MOTOR SCANNED STATUS'), ExportColumn::make('pump_scanned_status') @@ -58,23 +60,23 @@ class InvoiceValidationExporter extends Exporter ->label('OPERATOR ID'), ExportColumn::make('created_at') ->label('CREATED AT'), - //->dateTimeFormat('d-m-Y H:i:s'), + // ->dateTimeFormat('d-m-Y H:i:s'), ExportColumn::make('updated_at') ->label('UPDATED AT'), - //->dateTimeFormat('d-m-Y H:i:s'), + // ->dateTimeFormat('d-m-Y H:i:s'), ExportColumn::make('deleted_at') ->enabledByDefault(false) ->label('DELETED AT'), - //->dateTimeFormat('d-m-Y H:i:s'), + // ->dateTimeFormat('d-m-Y H:i:s'), ]; } public static function getCompletedNotificationBody(Export $export): string { - $body = 'Your invoice validation export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.'; + $body = 'Your invoice validation export has completed and '.number_format($export->successful_rows).' '.str('row')->plural($export->successful_rows).' exported.'; 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; diff --git a/app/Filament/Resources/InvoiceValidationResource.php b/app/Filament/Resources/InvoiceValidationResource.php index f83da66..ce941cc 100644 --- a/app/Filament/Resources/InvoiceValidationResource.php +++ b/app/Filament/Resources/InvoiceValidationResource.php @@ -10,12 +10,8 @@ use App\Models\InvoiceValidation; use App\Models\Item; use App\Models\Plant; use App\Models\StickerMaster; -use Auth; -use Filament\Actions\Action as FilamentActionsAction; -use Filament\Actions\CreateAction; use Filament\Facades\Filament; use Filament\Forms; -use Filament\Forms\Components\Actions\Action as ActionsAction; use Filament\Forms\Components\DateTimePicker; use Filament\Forms\Components\FileUpload; use Filament\Forms\Components\Radio; @@ -23,25 +19,23 @@ use Filament\Forms\Components\Section; use Filament\Forms\Components\Select; use Filament\Forms\Components\TextInput; use Filament\Forms\Components\ToggleButtons; +use Filament\Forms\Components\View; use Filament\Forms\Form; use Filament\Forms\Get; +use Filament\Notifications\Notification; use Filament\Resources\Resource; use Filament\Tables; -use Filament\Tables\Table; -use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\SoftDeletingScope; -use Filament\Notifications\Notification; use Filament\Tables\Actions\Action; use Filament\Tables\Actions\ExportAction; use Filament\Tables\Filters\Filter; -use Illuminate\Support\Facades\Storage; -use Maatwebsite\Excel\Facades\Excel; -use Livewire\Livewire; -use Str; +use Filament\Tables\Table; +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\SoftDeletingScope; use Illuminate\Support\Facades\Session; -use Illuminate\Support\Facades\Mail; -use Filament\Forms\Components\View; - +use Illuminate\Support\Facades\Storage; +use Livewire\Livewire; +use Maatwebsite\Excel\Facades\Excel; +use Str; class InvoiceValidationResource extends Resource { @@ -51,9 +45,7 @@ class InvoiceValidationResource extends Resource protected static ?string $navigationGroup = 'Invoice'; - public $invoiceNumber; - - + public $invoiceNumber; public static function form(Form $form): Form { @@ -63,168 +55,163 @@ class InvoiceValidationResource extends Resource // //->relationship('stickerMaster', 'id') // ->required(), - Section::make('') - ->schema([ - Forms\Components\Select::make('plant_id') - ->relationship('plant', 'name') - ->required() - // ->preload() - // ->nullable(), - ->reactive() - ->columnSpan(1) - ->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(); - }) - ->default(function () { - return optional(InvoiceValidation::latest()->first())->plant_id; - }) - ->disabled(fn (Get $get) => !empty($get('id'))) - // ->afterStateUpdated(fn ($set) => $set('block_id', null) & $set('name', null) & $set('start_time', null) & $set('duration', null) & $set('end_time', null)) - ->afterStateUpdated(function ($state, callable $set, callable $get) { - $plantId = $get('plant_id'); - $set('update_invoice', null); - // Ensure `linestop_id` is not cleared - if (!$plantId) { - $set('invoice_number', null); - $set('serial_number', null); - $set('total_quantity', null); - $set('scanned_quantity', null); - $set('ivPlantError', 'Please select a plant first.'); - return; - } - else - { - $set('ivPlantError', null); - } - }) - ->extraAttributes(fn ($get) => [ - 'class' => $get('ivPlantError') ? 'border-red-500' : '', - ]) - ->hint(fn ($get) => $get('ivPlantError') ? $get('ivPlantError') : null) - ->hintColor('danger'), + Section::make('') + ->schema([ + Forms\Components\Select::make('plant_id') + ->relationship('plant', 'name') + ->required() + // ->preload() + // ->nullable(), + ->reactive() + ->columnSpan(1) + ->options(function (callable $get) { + $userHas = Filament::auth()->user()->plant_id; - Forms\Components\TextInput::make('invoice_number') - ->label('Invoice Number') - ->required() - ->reactive() - ->columnSpan(1) - ->readOnly(fn (callable $get) => !empty($get('serial_number'))) - //->disabled(fn (Get $get) => !empty($get('serial_number'))) - ->extraAttributes([ - 'id' => 'invoice_number_input', - 'x-data' => '{ value: "" }', - 'x-model' => 'value', - 'wire:keydown.enter.prevent' => 'processInvoice(value)', - //'x-on:keydown.enter.prevent' => '$wire.processInvoice(value)', - ]) - // ->afterStateHydrated(function (TextInput $component, string $state) { - // $component->state(ucwords($state)); - // }) - ->afterStateUpdated(function ($state, callable $set, callable $get) { - $invNo = $get('invoice_number'); - $set('serial_number', null); - $set('update_invoice', null); - //Session::put('invoice_number', $state); - session(['invoice_number' => $state]); + return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray(); + }) + ->default(function () { + return optional(InvoiceValidation::latest()->first())->plant_id; + }) + ->disabled(fn (Get $get) => ! empty($get('id'))) + // ->afterStateUpdated(fn ($set) => $set('block_id', null) & $set('name', null) & $set('start_time', null) & $set('duration', null) & $set('end_time', null)) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $plantId = $get('plant_id'); + $set('update_invoice', null); + // Ensure `linestop_id` is not cleared + if (! $plantId) { + $set('invoice_number', null); + $set('serial_number', null); + $set('total_quantity', null); + $set('scanned_quantity', null); + $set('ivPlantError', 'Please select a plant first.'); - // if (!$invNo) { return; } else { } - }), + return; + } else { + $set('ivPlantError', null); + } + }) + ->extraAttributes(fn ($get) => [ + 'class' => $get('ivPlantError') ? 'border-red-500' : '', + ]) + ->hint(fn ($get) => $get('ivPlantError') ? $get('ivPlantError') : null) + ->hintColor('danger'), - Forms\Components\TextInput::make('serial_number') - ->label('Serial Number') - ->reactive() - ->readOnly(fn (callable $get) => empty($get('invoice_number'))) - ->disabled(fn (Get $get) => empty($get('invoice_number'))) - // ->extraAttributes([ - // 'id' => 'serial_number_input', - // 'x-data' => '{ value: "" }', - // 'x-model' => 'value', - // 'wire:keydown.enter.prevent' => 'processSerial(value)', // Using wire:keydown - // ]) - ->dehydrated(false) // Do not trigger Livewire syncing - ->extraAttributes([ - 'id' => 'serial_number_input', - 'x-on:keydown.enter.prevent' => " + Forms\Components\TextInput::make('invoice_number') + ->label('Invoice Number') + ->required() + ->reactive() + ->columnSpan(1) + ->readOnly(fn (callable $get) => ! empty($get('serial_number'))) + // ->disabled(fn (Get $get) => !empty($get('serial_number'))) + ->extraAttributes([ + 'id' => 'invoice_number_input', + 'x-data' => '{ value: "" }', + 'x-model' => 'value', + 'wire:keydown.enter.prevent' => 'processInvoice(value)', + // 'x-on:keydown.enter.prevent' => '$wire.processInvoice(value)', + ]) + // ->afterStateHydrated(function (TextInput $component, string $state) { + // $component->state(ucwords($state)); + // }) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $invNo = $get('invoice_number'); + $set('serial_number', null); + $set('update_invoice', null); + // Session::put('invoice_number', $state); + session(['invoice_number' => $state]); + + // if (!$invNo) { return; } else { } + }), + + Forms\Components\TextInput::make('serial_number') + ->label('Serial Number') + ->reactive() + ->readOnly(fn (callable $get) => empty($get('invoice_number'))) + ->disabled(fn (Get $get) => empty($get('invoice_number'))) + // ->extraAttributes([ + // 'id' => 'serial_number_input', + // 'x-data' => '{ value: "" }', + // 'x-model' => 'value', + // 'wire:keydown.enter.prevent' => 'processSerial(value)', // Using wire:keydown + // ]) + ->dehydrated(false) // Do not trigger Livewire syncing + ->extraAttributes([ + 'id' => 'serial_number_input', + 'x-on:keydown.enter.prevent' => " let serial = \$event.target.value; if (serial.trim() != '') { \$wire.dispatch('process-scan', serial); \$event.target.value = ''; } ", + ]) + ->afterStateUpdated(function ($state, callable $set, callable $get, callable $livewire) { + $set('update_invoice', 0); + // $this->dispatch('focus-serial-number'); + // if (!$invNo) { return; } else { } + // if ($state) { + // $livewire('process-scan', $state); + // $set('serial_number', null); + // } + }) + ->columnSpan(1), + Forms\Components\TextInput::make('total_quantity') + ->label('Total Quantity') + ->readOnly(true) + ->columnSpan(1), + Forms\Components\TextInput::make('scanned_quantity') + ->label('Scanned Quantity') + ->readOnly(true) + ->columnSpan(1), + ToggleButtons::make('update_invoice') + ->label('Update Invoice?') + ->boolean() + ->grouped() + ->reactive() + ->hidden(fn (callable $get) => ($get('invoice_number') == null || $get('update_invoice') == '0') || ! empty($get('serial_number'))) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + if (! $get('plant_id')) { + $set('update_invoice', null); + + return; + } + + if ($get('update_invoice') == '1') { + $totQuan = InvoiceValidation::where('invoice_number', $get('invoice_number'))->where('plant_id', $get('plant_id'))->count(); + if ($totQuan <= 0) { + $set('update_invoice', null); + + return; + } + + $totMQuan = InvoiceValidation::where('invoice_number', $get('invoice_number'))->whereNotNull('quantity')->where('plant_id', $get('plant_id'))->count(); + $scanMQuan = InvoiceValidation::where('invoice_number', $get('invoice_number'))->whereNotNull('serial_number')->where('serial_number', '!=', '')->where('plant_id', $get('plant_id'))->count(); + $scanSQuan = InvoiceValidation::where('invoice_number', $get('invoice_number'))->where('scanned_status', 'Scanned')->where('plant_id', $get('plant_id'))->count(); + + if ($totMQuan > 0) { + if ($totQuan == $scanMQuan) { + $set('update_invoice', null); + + return; + } + } else { + if ($totQuan == $scanSQuan) { + $set('update_invoice', null); + + return; + } + } + } + }) + ->disabled(fn (Get $get) => empty($get('invoice_number'))), + Forms\Components\TextInput::make('id') + ->hidden() + ->readOnly(true), ]) - ->afterStateUpdated(function ($state, callable $set, callable $get, callable $livewire) { - $set('update_invoice', 0); - // $this->dispatch('focus-serial-number'); - // if (!$invNo) { return; } else { } - // if ($state) { - // $livewire('process-scan', $state); - // $set('serial_number', null); - // } - }) - ->columnSpan(1), - Forms\Components\TextInput::make('total_quantity') - ->label('Total Quantity') - ->readOnly(true) - ->columnSpan(1), - Forms\Components\TextInput::make('scanned_quantity') - ->label('Scanned Quantity') - ->readOnly(true) - ->columnSpan(1), - ToggleButtons::make('update_invoice') - ->label('Update Invoice?') - ->boolean() - ->grouped() - ->reactive() - ->hidden(fn (callable $get) => ($get('invoice_number') == null || $get('update_invoice') == '0') || !empty($get('serial_number'))) - ->afterStateUpdated(function ($state, callable $set, callable $get) { - if (!$get('plant_id')) - { - $set('update_invoice', null); - return; - } - - if ($get('update_invoice') == "1") - { - $totQuan = InvoiceValidation::where('invoice_number', $get('invoice_number'))->where('plant_id', $get('plant_id'))->count(); - if ($totQuan <= 0) - { - $set('update_invoice', null); - return; - } - - $totMQuan = InvoiceValidation::where('invoice_number', $get('invoice_number'))->whereNotNull('quantity')->where('plant_id', $get('plant_id'))->count(); - $scanMQuan = InvoiceValidation::where('invoice_number', $get('invoice_number'))->whereNotNull('serial_number')->where('serial_number', '!=', '')->where('plant_id', $get('plant_id'))->count(); - $scanSQuan = InvoiceValidation::where('invoice_number', $get('invoice_number'))->where('scanned_status', 'Scanned')->where('plant_id', $get('plant_id'))->count(); - - if ($totMQuan > 0) - { - if ($totQuan == $scanMQuan) - { - $set('update_invoice', null); - return; - } - } - else - { - if ($totQuan == $scanSQuan) - { - $set('update_invoice', null); - return; - } - } - } - }) - ->disabled(fn (Get $get) => empty($get('invoice_number'))), - Forms\Components\TextInput::make('id') - ->hidden() - ->readOnly(true), - ]) - ->columns(5), + ->columns(5), ]); } - public static function table(Table $table): Table { return $table @@ -240,20 +227,26 @@ class InvoiceValidationResource extends Resource $paginator = $livewire->getTableRecords(); $perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10; $currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1; + return ($currentPage - 1) * $perPage + $rowLoop->iteration; }), Tables\Columns\TextColumn::make('invoice_number') ->label('Invoice Number') ->alignCenter() - ->sortable(), //->searchable() + ->sortable(), // ->searchable() Tables\Columns\TextColumn::make('stickerMaster.item.code') ->label('Item Code') ->alignCenter() - ->sortable(), //->searchable() + ->sortable(), // ->searchable() + Tables\Columns\TextColumn::make('stickerMaster.item.description') + ->label('Item Description') + ->alignCenter() + ->sortable() // ->searchable() + ->toggleable(isToggledHiddenByDefault: true), Tables\Columns\TextColumn::make('serial_number') ->label('Serial Number') ->alignCenter() - ->sortable(), //->searchable() + ->sortable(), // ->searchable() Tables\Columns\TextColumn::make('motor_scanned_status') ->label('Motor Scanned Status') ->alignCenter(), @@ -269,6 +262,9 @@ class InvoiceValidationResource extends Resource Tables\Columns\TextColumn::make('scanned_status') ->label('Scanned Status') ->alignCenter(), + // Tables\Columns\TextColumn::make('stickerMaster.panel_box_code') + // ->label('Panel Box Code') + // ->alignCenter(), Tables\Columns\TextColumn::make('panel_box_supplier') ->label('Panel Box Supplier') ->alignCenter(), @@ -335,6 +331,7 @@ class InvoiceValidationResource extends Resource // ->options(Plant::pluck('name', 'id')->toArray()) // Fetch plant names and IDs ->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(); }) ->label('Select Plant') @@ -354,8 +351,8 @@ class InvoiceValidationResource extends Resource ->storeFiles(false) // prevent auto-storing, we will store manually ->reactive() ->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'))) + ->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'), ]) ->action(function (array $data) { @@ -378,42 +375,37 @@ class InvoiceValidationResource extends Resource // /home/iot-dev/projects/pds/storage/app/private/uploads/temp/3RA0018735.xlsx $totQuan = InvoiceValidation::where('invoice_number', $originalNameOnly)->count(); - if ($totQuan > 0) - { + if ($totQuan > 0) { $scanSQuan = InvoiceValidation::where('invoice_number', $originalNameOnly)->where('scanned_status', 'Scanned')->count(); - if ($totQuan == $scanSQuan) - { + if ($totQuan == $scanSQuan) { $invoiceFirst = InvoiceValidation::with('plant')->where('invoice_number', $originalNameOnly)->first(); - $plantName = $invoiceFirst ? (String)$invoiceFirst->plant->name : null; + $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)) - { + if ($disk->exists($path)) { $disk->delete($path); } + return; - } - else - { + } else { $invoiceFirst = InvoiceValidation::with('plant')->where('invoice_number', $originalNameOnly)->first(); // $plantCode = $invoiceFirst ? (String)$invoiceFirst->plant->code : null; - $plantName = $invoiceFirst ? (String)$invoiceFirst->plant->name : null; + $plantName = $invoiceFirst ? (string) $invoiceFirst->plant->name : null; $invoicePlantId = $invoiceFirst->plant_id; - if ($plantId != $invoicePlantId) - { + 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)) - { + if ($disk->exists($path)) { $disk->delete($path); } + return; } } @@ -422,26 +414,23 @@ class InvoiceValidationResource extends Resource $totQuan = InvoiceValidation::where('invoice_number', $originalNameOnly)->where('plant_id', $plantId)->count(); $scanSQuan = InvoiceValidation::where('invoice_number', $originalNameOnly)->where('scanned_status', 'Scanned')->where('plant_id', $plantId)->count(); - if ($totQuan > 0 && $totQuan == $scanSQuan) - { + if ($totQuan > 0 && $totQuan == $scanSQuan) { Notification::make() ->title('Serial invoice already completed the scanning process for selected plant.') ->danger() ->send(); - if ($disk->exists($path)) - { + if ($disk->exists($path)) { $disk->delete($path); } + return; } - if ($fullPath && file_exists($fullPath)) - { + if ($fullPath && file_exists($fullPath)) { $rows = Excel::toArray(null, $fullPath)[0]; - if ((count($rows) - 1) <= 0) - { + if ((count($rows) - 1) <= 0) { Notification::make() ->title('Records Not Found') ->body("Import the valid 'Serial Invoice' file to proceed..!") @@ -451,6 +440,7 @@ class InvoiceValidationResource extends Resource if ($disk->exists($path)) { $disk->delete($path); } + return; } @@ -462,9 +452,10 @@ class InvoiceValidationResource extends Resource $seenSerialNumbers = []; $validRowsFound = false; - foreach ($rows as $index => $row) - { - if ($index == 0) continue; // Skip header + foreach ($rows as $index => $row) { + if ($index == 0) { + continue; + } // Skip header $materialCode = trim($row[0]); $serialNumber = trim($row[1]); @@ -473,24 +464,16 @@ class InvoiceValidationResource extends Resource continue; } - if (!empty($materialCode)) - { - if (Str::length($materialCode) < 6 || !ctype_alnum($materialCode)) - { + if (! empty($materialCode)) { + if (Str::length($materialCode) < 6 || ! ctype_alnum($materialCode)) { $invalidMatCodes[] = $materialCode; - } - else - { + } else { if (empty($serialNumber)) { $missingSerials[] = $materialCode; - } - else if (Str::length($serialNumber) < 9 || !ctype_alnum($serialNumber)) - { + } elseif (Str::length($serialNumber) < 9 || ! ctype_alnum($serialNumber)) { $invalidSerialCodes[] = $serialNumber; - } - else - { + } else { if (in_array($serialNumber, $seenSerialNumbers)) { $duplicateSerials[] = $serialNumber; } else { @@ -500,9 +483,7 @@ class InvoiceValidationResource extends Resource } } } - } - else - { + } else { continue; } } @@ -515,52 +496,53 @@ class InvoiceValidationResource extends Resource $duplicateSerialCodes = array_unique($duplicateSerials); - if (!empty($uniqueInvalidCodes)) { + if (! empty($uniqueInvalidCodes)) { Notification::make() ->title('Invalid Item Codes') - ->body('The following item codes should contain minimum 6 digit alpha numeric values:
' . implode(', ', $uniqueInvalidCodes)) + ->body('The following item codes should contain minimum 6 digit alpha numeric values:
'.implode(', ', $uniqueInvalidCodes)) ->danger() ->send(); if ($disk->exists($path)) { $disk->delete($path); } + return; - } - else if (!empty($uniqueMissingSerials)) { + } elseif (! empty($uniqueMissingSerials)) { Notification::make() ->title('Missing Serial Numbers') - ->body("The following item codes doesn't have valid serial number:
" . implode(', ', $uniqueMissingSerials)) + ->body("The following item codes doesn't have valid serial number:
".implode(', ', $uniqueMissingSerials)) ->danger() ->send(); if ($disk->exists($path)) { $disk->delete($path); } + return; - } - else if (!empty($uniqueSerialCodes)) { + } elseif (! empty($uniqueSerialCodes)) { Notification::make() ->title('Invalid Serial Number') - ->body('The following serial numbers should contain minimum 9 digit alpha numeric values:
' . implode(', ', $uniqueSerialCodes)) + ->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; - } - else if (!empty($duplicateSerialCodes)) { + } elseif (! empty($duplicateSerialCodes)) { Notification::make() ->title('Duplicate Serial Numbers') - ->body('The following serial numbers are already exist in imported excel:
' . implode(', ', $duplicateSerialCodes)) + ->body('The following serial numbers are already exist in imported excel:
'.implode(', ', $duplicateSerialCodes)) ->danger() ->send(); if ($disk->exists($path)) { $disk->delete($path); } + return; } - if (!$validRowsFound) { + if (! $validRowsFound) { Notification::make() ->title('Invalid Serial Invoice') ->danger() // This makes the notification red to indicate an error @@ -569,26 +551,26 @@ class InvoiceValidationResource extends Resource if ($disk->exists($path)) { $disk->delete($path); } + return; } $uniqueCodes = array_unique($materialCodes); $matchedItems = StickerMaster::with('item') - ->whereHas('item', function ($query) use ($uniqueCodes) { - $query->whereIn('code', $uniqueCodes); - }) - ->get(); + ->whereHas('item', function ($query) use ($uniqueCodes) { + $query->whereIn('code', $uniqueCodes); + }) + ->get(); $matchedCodes = $matchedItems->pluck('item.code')->toArray(); $missingCodes = array_diff($uniqueCodes, $matchedCodes); - if (!empty($missingCodes)) - { + if (! empty($missingCodes)) { $missingCount = count($missingCodes); - $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database." : 'The following item codes are not found in database:
' . implode(', ', $missingCodes); + $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database." : 'The following item codes are not found in database:
'.implode(', ', $missingCodes); Notification::make() ->title('Unknown Item Codes') @@ -599,43 +581,41 @@ class InvoiceValidationResource extends Resource 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 + ->filter(fn ($sticker) => ! empty($sticker->material_type)) // filter invalid ->pluck('item.code') ->toArray(); - if (count($invalidCodes) > 10) - { + if (count($invalidCodes) > 10) { Notification::make() ->title('Invalid item codes found') - ->body('' . count($invalidCodes) . 'item codes found have material type.') + ->body(''.count($invalidCodes).'item codes found have material type.') ->danger() ->send(); if ($disk->exists($path)) { $disk->delete($path); } + return; - } - else if (count($invalidCodes) > 0) - { + } elseif (count($invalidCodes) > 0) { Notification::make() ->title('Invalid item codes found') - ->body('Material invoice Item Codes found : ' . implode(', ', $invalidCodes)) + ->body('Material invoice Item Codes found : '.implode(', ', $invalidCodes)) ->danger() ->send(); if ($disk->exists($path)) { $disk->delete($path); } + return; - } - else - { + } else { // Save full file path to session session(['uploaded_invoice_path' => $fullPath]); Notification::make() @@ -645,7 +625,7 @@ class InvoiceValidationResource extends Resource } } }) - ->visible(function() { + ->visible(function () { return Filament::auth()->user()->can('view import serial invoice'); }), Tables\Actions\Action::make('Import Invoice Material') @@ -654,19 +634,20 @@ class InvoiceValidationResource extends Resource 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 ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray(); - }) - ->label('Select Plant') - ->required() - ->default(function () { - return optional(InvoiceValidation::latest()->first())->plant_id; - }) - ->afterStateUpdated(function ($state, callable $set, callable $get) { - $set('invoice_material', null); - }) - ->reactive(), + ->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(); + }) + ->label('Select Plant') + ->required() + ->default(function () { + return optional(InvoiceValidation::latest()->first())->plant_id; + }) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $set('invoice_material', null); + }) + ->reactive(), FileUpload::make('invoice_material') ->label('Invoice Material') @@ -675,7 +656,7 @@ class InvoiceValidationResource extends Resource ->reactive() // <- this keeps the original filename ->storeFiles(false) // prevent auto-storing ->disk('local') - ->visible(fn (Get $get) => !empty($get('plant_id'))) + ->visible(fn (Get $get) => ! empty($get('plant_id'))) ->directory('uploads/temp'), ]) ->action(function (array $data) { @@ -695,42 +676,37 @@ class InvoiceValidationResource extends Resource $fullPath = Storage::disk('local')->path($path); $totQuan = InvoiceValidation::where('invoice_number', $originalNameOnly)->count(); - if ($totQuan > 0) - { + if ($totQuan > 0) { $scanMQuan = InvoiceValidation::where('invoice_number', $originalNameOnly)->whereNotNull('serial_number')->where('serial_number', '!=', '')->count(); - if ($totQuan == $scanMQuan) - { + if ($totQuan == $scanMQuan) { $invoiceFirst = InvoiceValidation::with('plant')->where('invoice_number', $originalNameOnly)->first(); - $plantName = $invoiceFirst ? (String)$invoiceFirst->plant->name : null; + $plantName = $invoiceFirst ? (string) $invoiceFirst->plant->name : null; Notification::make() ->title("Material invoice number : '$originalNameOnly' already completed the scanning process for plant : '$plantName'.") ->danger() ->send(); - if ($disk->exists($path)) - { + if ($disk->exists($path)) { $disk->delete($path); } + return; - } - else - { + } else { $invoiceFirst = InvoiceValidation::with('plant')->where('invoice_number', $originalNameOnly)->first(); // $plantCode = $invoiceFirst ? (String)$invoiceFirst->plant->code : null; - $plantName = $invoiceFirst ? (String)$invoiceFirst->plant->name : null; + $plantName = $invoiceFirst ? (string) $invoiceFirst->plant->name : null; $invoicePlantId = $invoiceFirst->plant_id; - if ($plantId != $invoicePlantId) - { + if ($plantId != $invoicePlantId) { Notification::make() ->title("Material invoice number : '$originalNameOnly' already exists for plant : '$plantName'.
Choose the valid 'Plant' to proceed!") ->danger() ->send(); - if ($disk->exists($path)) - { + if ($disk->exists($path)) { $disk->delete($path); } + return; } } @@ -739,8 +715,7 @@ class InvoiceValidationResource extends Resource $totQuan = InvoiceValidation::where('invoice_number', $originalNameOnly)->where('plant_id', $plantId)->count(); $scanMQuan = InvoiceValidation::where('invoice_number', $originalNameOnly)->whereNotNull('serial_number')->where('serial_number', '!=', '')->where('plant_id', $plantId)->count(); - if ($totQuan > 0 && $totQuan == $scanMQuan) - { + if ($totQuan > 0 && $totQuan == $scanMQuan) { Notification::make() ->title('Material invoice already completed the scanning process for selected plant.') ->danger() @@ -748,15 +723,14 @@ class InvoiceValidationResource extends Resource if ($disk->exists($path)) { $disk->delete($path); } + return; } - if ($fullPath && file_exists($fullPath)) - { + if ($fullPath && file_exists($fullPath)) { $rows = Excel::toArray(null, $fullPath)[0]; - if ((count($rows) - 1) <= 0) - { + if ((count($rows) - 1) <= 0) { Notification::make() ->title('Records Not Found') ->body("Import the valid 'Material Invoice' file to proceed..!") @@ -766,6 +740,7 @@ class InvoiceValidationResource extends Resource if ($disk->exists($path)) { $disk->delete($path); } + return; } @@ -776,9 +751,10 @@ class InvoiceValidationResource extends Resource $invalidMaterialQuan = []; $validRowsFound = false; - foreach ($rows as $index => $row) - { - if ($index == 0) continue; // Skip header + foreach ($rows as $index => $row) { + if ($index == 0) { + continue; + } // Skip header $materialCode = trim($row[0]); $materialQuantity = trim($row[1]); @@ -787,34 +763,22 @@ class InvoiceValidationResource extends Resource continue; } - if (!empty($materialCode)) { - if (Str::length($materialCode) < 6 || !ctype_alnum($materialCode)) - { + if (! empty($materialCode)) { + if (Str::length($materialCode) < 6 || ! ctype_alnum($materialCode)) { $invalidMatCodes[] = $materialCode; - } - else - { - if ($materialQuantity == 0) - { + } else { + if ($materialQuantity == 0) { $invalidMaterialQuan[] = $materialCode; - } - else if (empty($materialQuantity)) - { + } elseif (empty($materialQuantity)) { $missingQuantities[] = $materialCode; - } - else if (!is_numeric($materialQuantity)) - { + } elseif (! is_numeric($materialQuantity)) { $invalidMatQuan[] = $materialCode; - } - else - { + } else { $materialCodes[] = $materialCode; $validRowsFound = true; } } - } - else - { + } else { continue; } } @@ -824,59 +788,63 @@ class InvoiceValidationResource extends Resource $uniqueZeroMat = array_unique($invalidMaterialQuan); $uniqueEmptyMat = array_unique($missingQuantities); - if (!empty($uniqueInvalidCodes)) { + if (! empty($uniqueInvalidCodes)) { Notification::make() ->title('Invalid Item Codes') - ->body('The following item codes should contain minimum 6 digit alpha numeric values:
' . implode(', ', $uniqueInvalidCodes)) + ->body('The following item codes should contain minimum 6 digit alpha numeric values:
'.implode(', ', $uniqueInvalidCodes)) ->danger() ->send(); if ($disk->exists($path)) { $disk->delete($path); } + return; } - if (!empty($uniqueaplhaMat)) { + if (! empty($uniqueaplhaMat)) { Notification::make() ->title('Invalid Material Quantity') - ->body("The following item codes material quantity must be a numeric values :
" . implode(', ', $uniqueaplhaMat)) + ->body('The following item codes material quantity must be a numeric values :
'.implode(', ', $uniqueaplhaMat)) ->danger() ->send(); if ($disk->exists($path)) { $disk->delete($path); } + return; } - if (!empty($uniqueZeroMat)) { + if (! empty($uniqueZeroMat)) { Notification::make() ->title('Invalid Material Quantity') - ->body("The following item codes material quantity should be greater than 0:
" . implode(', ', $uniqueZeroMat)) + ->body('The following item codes material quantity should be greater than 0:
'.implode(', ', $uniqueZeroMat)) ->danger() ->send(); if ($disk->exists($path)) { $disk->delete($path); } + return; } - if (!empty($uniqueEmptyMat)) { + if (! empty($uniqueEmptyMat)) { Notification::make() ->title('Missing Material Quantity') - ->body("The following item codes doesn't have valid material quantity:
" . implode(', ', $uniqueEmptyMat)) + ->body("The following item codes doesn't have valid material quantity:
".implode(', ', $uniqueEmptyMat)) ->danger() ->send(); if ($disk->exists($path)) { $disk->delete($path); } + return; } - if (!$validRowsFound) { + if (! $validRowsFound) { Notification::make() ->title('Invalid Material Invoice') ->danger() // This makes the notification red to indicate an error @@ -886,6 +854,7 @@ class InvoiceValidationResource extends Resource if ($disk->exists($path)) { $disk->delete($path); } + return; } @@ -901,13 +870,12 @@ class InvoiceValidationResource extends Resource $missingCodes = array_diff($uniqueCodes, $matchedCodes); - if (!empty($missingCodes)) - { + if (! empty($missingCodes)) { $missingCount = count($missingCodes); $message = $missingCount > 10 ? "'$missingCount' Item Codes are not found in sticker master." - : 'Item Codes are not found in sticker master:
' . implode(', ', $missingCodes); + : 'Item Codes are not found in sticker master:
'.implode(', ', $missingCodes); Notification::make() ->title('Unknown Item Codes') @@ -918,40 +886,40 @@ class InvoiceValidationResource extends Resource if ($disk->exists($path)) { $disk->delete($path); } + return; } $invalidCodes = $matchedItems - ->filter(fn ($sticker) => empty($sticker->material_type)) //filter invalid + ->filter(fn ($sticker) => empty($sticker->material_type)) // filter invalid ->pluck('item.code') ->toArray(); - if (count($invalidCodes) > 10) - { + if (count($invalidCodes) > 10) { $invalidCodes = array_unique($invalidCodes); Notification::make() ->title('Invalid item codes found') - ->body('' . count($invalidCodes) . 'invalid item codes found have serial number.') + ->body(''.count($invalidCodes).'invalid item codes found have serial number.') ->danger() ->send(); if ($disk->exists($path)) { $disk->delete($path); } + return; - } - else if (count($invalidCodes) > 0) - { + } elseif (count($invalidCodes) > 0) { $invalidCodes = array_unique($invalidCodes); Notification::make() ->title('Invalid item codes found') - ->body('Serial invoice Item Codes found : ' . implode(', ', $invalidCodes)) + ->body('Serial invoice Item Codes found : '.implode(', ', $invalidCodes)) ->danger() ->send(); if ($disk->exists($path)) { $disk->delete($path); } + return; } @@ -959,24 +927,22 @@ class InvoiceValidationResource extends Resource $zeroQtyCodes = []; $notDivisibleCodes = []; - foreach ($matchedItems as $sticker) - { + foreach ($matchedItems as $sticker) { $code = $sticker->item->code; $materialType = $sticker->material_type; - if ($materialType == 2) - { + if ($materialType == 2) { $bundleQty = $sticker->bundle_quantity ?? 0; $totalExcelQty = 0; - foreach ($rows as $index => $row) - { - if ($index == 0) continue; // Skip header + foreach ($rows as $index => $row) { + if ($index == 0) { + continue; + } // Skip header $excelCode = trim($row[0]); $excelMatQty = trim($row[1]); - if ($excelCode == $code && is_numeric($excelMatQty)) { $totalExcelQty += $excelMatQty; // Sum up the quantities } @@ -984,7 +950,7 @@ class InvoiceValidationResource extends Resource if ($totalExcelQty == 0) { $zeroQtyCodes[] = $code; - } elseif (!is_numeric($totalExcelQty)) { + } elseif (! is_numeric($totalExcelQty)) { $nonNumericQtyCodes[] = $code; // Here you may want to check divisibility condition too } elseif ($bundleQty != 0 && $totalExcelQty % $bundleQty != 0) { $notDivisibleCodes[] = $code; @@ -992,8 +958,10 @@ class InvoiceValidationResource extends Resource } } - $showValidationNotification = function(array $codes, string $message) { - if (count($codes) == 0) return; + $showValidationNotification = function (array $codes, string $message) { + if (count($codes) == 0) { + return; + } $uniqueCodes = array_unique($codes); $codeList = implode(', ', $uniqueCodes); @@ -1009,20 +977,18 @@ class InvoiceValidationResource extends Resource $zeroQtyCodes = array_unique($zeroQtyCodes); $notDivisibleCodes = array_unique($notDivisibleCodes); - $showValidationNotification($nonNumericQtyCodes, "The following item codes contains invalid bundle quantity:"); + $showValidationNotification($nonNumericQtyCodes, 'The following item codes contains invalid bundle quantity:'); $showValidationNotification($zeroQtyCodes, "The following item codes quantity should be greater than '0':"); - $showValidationNotification($notDivisibleCodes, "The following item codes quantity is not divisible by bundle quantity."); + $showValidationNotification($notDivisibleCodes, 'The following item codes quantity is not divisible by bundle quantity.'); if ($nonNumericQtyCodes || $zeroQtyCodes || $notDivisibleCodes) { - if ($disk->exists($path)) - { + if ($disk->exists($path)) { $disk->delete($path); } + return; - } - else - { - // Save full file path to session + } else { + // Save full file path to session session(['uploaded_material_invoice' => $fullPath]); Notification::make() ->title('Material invoice imported successfully.') @@ -1032,14 +998,14 @@ class InvoiceValidationResource extends Resource } }) - ->visible(function() { + ->visible(function () { return Filament::auth()->user()->can('view import material invoice'); }), ExportAction::make() ->label('Export Invoices') ->color('warning') ->exporter(InvoiceValidationExporter::class) - ->visible(function() { + ->visible(function () { return Filament::auth()->user()->can('view export invoice'); }), ]) @@ -1047,206 +1013,244 @@ class InvoiceValidationResource extends Resource ->filters([ Tables\Filters\TrashedFilter::make(), Filter::make('advanced_filters') - ->label('Advanced Filters') - ->form([ - Radio::make('invoice_type') - ->label('Type ?') - ->boolean() - ->options([ - 'Serial' => 'Serial', - 'Material' => 'Material' - ]) - ->default('Serial') - ->inlineLabel(false) - ->inline(), - Select::make('Plant') - ->label('Select Plant') - ->nullable() - // ->options(function () { - // return Plant::pluck('name', 'id'); - // }) - ->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(); - }) - ->reactive() - ->afterStateUpdated(function ($state, callable $set, callable $get): void { - $set('sticker_master_id', null); - $set('operator_id', null); - }), - TextInput::make('invoice_number') - ->label('Invoice Number') - ->placeholder(placeholder: 'Enter Invoice Number'), - TextInput::make('serial_number') - ->label('Serial Number') - ->placeholder(placeholder: 'Enter Serial Number'), - Select::make('sticker_master_id') - ->label('Search by Item Code') - ->nullable() - ->options(function (callable $get) { - $pId = $get('Plant'); - // if (empty($pId)) { - // return []; - // } - return Item::whereHas('stickerMasters', function ($query) use ($pId) { - if ($pId) { - $query->where('plant_id', $pId); + ->label('Advanced Filters') + ->form([ + Radio::make('invoice_type') + ->label('Type ?') + ->boolean() + // ->options([ + // 'Serial' => 'Serial', + // 'Material' => 'Material', + // ]) + ->options(function () { + $userRights = Filament::auth()->user(); + $hasBoth = ($userRights->hasRole(['Super Admin', 'Sales Employee', 'Sales Supervisor Hub', 'Sales Manager']) ?? null); + + $hasSerial = ($userRights->hasRole(['Sales Supervisor 1', 'Sales Supervisor Inventory', 'Sales Employee Inventory']) ?? null); + + $hasMaterial = $userRights->hasRole('Sales Supervisor 2') ?? null; + + $retRes = [ + 'Serial' => 'Serial', + 'Material' => 'Material', + ]; + + if ($hasBoth) { + return $retRes; + } elseif ($hasSerial) { + return ['Serial' => 'Serial']; + } elseif ($hasMaterial) { + return ['Material' => 'Material']; + } else { + return ['Serial' => 'Serial']; // return []; } - $query->whereHas('invoiceValidations'); - })->pluck('code', 'id'); - }) - ->searchable() - ->reactive(), - Select::make('scanned_status') - ->label('Scanned Status') - ->nullable() - ->options([ - 'Scanned' => 'Scanned', - 'Pending' => 'Pending', - ]) - ->searchable() - ->reactive(), - Select::make('operator_id') - ->label('Created By') - ->nullable() - ->options(function (callable $get) { - $plantId = $get('Plant'); - if (!$plantId) - { - return InvoiceValidation::whereNotNull('operator_id')->select('operator_id')->distinct()->pluck('operator_id', 'operator_id'); + }) + // ->default('Serial') + ->default(function () { + $userRights = Filament::auth()->user(); + $hasBoth = ($userRights->hasRole(['Super Admin', 'Sales Employee', 'Sales Supervisor Hub', 'Sales Manager']) ?? null); + + $hasSerial = ($userRights->hasRole(['Sales Supervisor 1', 'Sales Supervisor Inventory', 'Sales Employee Inventory']) ?? null); + + $hasMaterial = $userRights->hasRole('Sales Supervisor 2') ?? null; + + if ($hasBoth || $hasSerial) { + return 'Serial'; + } elseif ($hasMaterial) { + return 'Material'; + } else { + return 'Serial'; // return []; + } + }) + ->inlineLabel(false) + ->inline(), + Select::make('Plant') + ->label('Select Plant') + ->nullable() + // ->options(function () { + // return Plant::pluck('name', 'id'); + // }) + ->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(); + }) + ->reactive() + ->afterStateUpdated(function ($state, callable $set, callable $get): void { + $set('sticker_master_id', null); + $set('operator_id', null); + }), + TextInput::make('invoice_number') + ->label('Invoice Number') + ->placeholder(placeholder: 'Enter Invoice Number'), + TextInput::make('serial_number') + ->label('Serial Number') + ->placeholder(placeholder: 'Enter Serial Number'), + Select::make('sticker_master_id') + ->label('Search by Item Code') + ->nullable() + ->options(function (callable $get) { + $pId = $get('Plant'); + + // if (empty($pId)) { + // return []; + // } + return Item::whereHas('stickerMasters', function ($query) use ($pId) { + if ($pId) { + $query->where('plant_id', $pId); + } + $query->whereHas('invoiceValidations'); + })->pluck('code', 'id'); + }) + ->searchable() + ->reactive(), + Select::make('scanned_status') + ->label('Scanned Status') + ->nullable() + ->options([ + 'Scanned' => 'Scanned', + 'Pending' => 'Pending', + ]) + ->searchable() + ->reactive(), + Select::make('operator_id') + ->label('Created By') + ->nullable() + ->options(function (callable $get) { + $plantId = $get('Plant'); + if (! $plantId) { + return InvoiceValidation::whereNotNull('operator_id')->select('operator_id')->distinct()->pluck('operator_id', 'operator_id'); + } else { + return InvoiceValidation::where('plant_id', $plantId)->whereNotNull('operator_id')->select('operator_id')->distinct()->pluck('operator_id', 'operator_id'); + } + }) + ->searchable() + ->reactive(), + DateTimePicker::make(name: 'created_from') + ->label('Created From') + ->placeholder(placeholder: 'Select From DateTime') + ->reactive() + ->native(false), + DateTimePicker::make('created_to') + ->label('Created To') + ->placeholder(placeholder: 'Select To DateTime') + ->reactive() + ->native(false), + ]) + ->query(function ($query, array $data) { + // Hide all records initially if no filters are applied + if (empty($data['invoice_type']) || (empty($data['Plant']) && empty($data['invoice_number']) && empty($data['serial_number']) && empty($data['created_from']) && empty($data['created_to']) && empty($data['operator_id']) && empty($data['scanned_status']) && empty($data['sticker_master_id']))) { + if (empty($data['invoice_type'])) { + Notification::make() + ->title('Please, choose invoice type to filter.') + ->danger() + ->send(); } - else - { - return InvoiceValidation::where('plant_id', $plantId)->whereNotNull('operator_id')->select('operator_id')->distinct()->pluck('operator_id', 'operator_id'); - } - }) - ->searchable() - ->reactive(), - DateTimePicker::make(name: 'created_from') - ->label('Created From') - ->placeholder(placeholder: 'Select From DateTime') - ->reactive() - ->native(false), - DateTimePicker::make('created_to') - ->label('Created To') - ->placeholder(placeholder: 'Select To DateTime') - ->reactive() - ->native(false), - ]) - ->query(function ($query, array $data) { - // Hide all records initially if no filters are applied - if (empty($data['invoice_type']) || (empty($data['Plant']) && empty($data['invoice_number']) && empty($data['serial_number']) && empty($data['created_from']) && empty($data['created_to']) && empty($data['operator_id']) && empty($data['scanned_status']) && empty($data['sticker_master_id']))) { - if (empty($data['invoice_type'])) - { - Notification::make() - ->title('Please, choose invoice type to filter.') - ->danger() - ->send(); + + return $query->whereRaw('1 = 0'); } - return $query->whereRaw('1 = 0'); - } - if ($data['invoice_type'] == 'Serial') { - $query->whereNull('quantity'); + if ($data['invoice_type'] == 'Serial') { + $query->whereNull('quantity'); - if (!empty($data['scanned_status'])) { - if ($data['scanned_status'] == 'Scanned') { - $query->whereNotNull('scanned_status')->where('scanned_status', '!=', ''); - } elseif ($data['scanned_status'] == 'Pending') { - //$query->whereNull('scanned_status')->orWhere('scanned_status', ''); - $query->where(function ($query) use ($data) { - // if (empty($data['scanned_status']) || $data['scanned_status'] == 'Pending') { - $query->whereNull('scanned_status')->orWhere('scanned_status', '!=', 'Scanned'); - // } - }); + if (! empty($data['scanned_status'])) { + if ($data['scanned_status'] == 'Scanned') { + $query->whereNotNull('scanned_status')->where('scanned_status', '!=', ''); + } elseif ($data['scanned_status'] == 'Pending') { + // $query->whereNull('scanned_status')->orWhere('scanned_status', ''); + $query->where(function ($query) { + // if (empty($data['scanned_status']) || $data['scanned_status'] == 'Pending') { + $query->whereNull('scanned_status')->orWhere('scanned_status', '!=', 'Scanned'); + // } + }); + } + } + } elseif ($data['invoice_type'] == 'Material') { + $query->whereNotNull('quantity'); // ->where('quantity', '>', 0) + + if (! empty($data['scanned_status'])) { + if ($data['scanned_status'] == 'Scanned') { + $query->whereNotNull('serial_number')->where('serial_number', '!=', ''); + } elseif ($data['scanned_status'] == 'Pending') { + $query->where(function ($query) { + $query->whereNull('serial_number')->orWhere('serial_number', '=', ''); + }); + } } } - } elseif ($data['invoice_type'] == 'Material') { - $query->whereNotNull('quantity');//->where('quantity', '>', 0) - if (!empty($data['scanned_status'])) { - if ($data['scanned_status'] == 'Scanned') { - $query->whereNotNull('serial_number')->where('serial_number', '!=', ''); - } elseif ($data['scanned_status'] == 'Pending') { - $query->where(function ($query) use ($data) { - $query->whereNull('serial_number')->orWhere('serial_number', '=', ''); - }); + if (! empty($data['Plant'])) { // $plant = $data['Plant'] ?? null + $query->where('plant_id', $data['Plant']); + } + + if (! empty($data['invoice_number'])) { + $query->where('invoice_number', 'like', '%'.$data['invoice_number'].'%'); + } + + if (! empty($data['serial_number'])) { + $query->where('serial_number', 'like', '%'.$data['serial_number'].'%'); + } + + if (! empty($data['created_from'])) { + $query->where('created_at', '>=', $data['created_from']); + } + + if (! empty($data['created_to'])) { + $query->where('created_at', '<=', $data['created_to']); + } + + if (! empty($data['operator_id'])) { + $query->where('operator_id', $data['operator_id']); + } + + if (! empty($data['sticker_master_id'])) { + $stickerMasterIds = StickerMaster::where('item_id', $data['sticker_master_id']) + ->pluck('id') + ->toArray(); + + if (! empty($stickerMasterIds)) { + $query->whereIn('sticker_master_id', $stickerMasterIds); } } - } + }) + ->indicateUsing(function (array $data) { + $indicators = []; - if (!empty($data['Plant'])) { //$plant = $data['Plant'] ?? null - $query->where('plant_id', $data['Plant']); - } - - if (!empty($data['invoice_number'])) { - $query->where('invoice_number', 'like', '%' . $data['invoice_number'] . '%'); - } - - if (!empty($data['serial_number'])) { - $query->where('serial_number', 'like', '%' . $data['serial_number'] . '%'); - } - - if (!empty($data['created_from'])) { - $query->where('created_at', '>=', $data['created_from']); - } - - if (!empty($data['created_to'])) { - $query->where('created_at', '<=', $data['created_to']); - } - - if (!empty($data['operator_id'])) { - $query->where('operator_id', $data['operator_id']); - } - - if (!empty($data['sticker_master_id'])) { - $stickerMasterIds = StickerMaster::where('item_id', $data['sticker_master_id']) - ->pluck('id') - ->toArray(); - - if (!empty($stickerMasterIds)) { - $query->whereIn('sticker_master_id', $stickerMasterIds); + if (! empty($data['Plant'])) { + $indicators[] = 'Plant: '.Plant::where('id', $data['Plant'])->value('name'); } - } - }) - ->indicateUsing(function (array $data) { - $indicators = []; - if (!empty($data['Plant'])) { - $indicators[] = 'Plant: ' . Plant::where('id', $data['Plant'])->value('name'); - } + if (! empty($data['invoice_number'])) { + $indicators[] = 'Invoice Number: '.$data['invoice_number']; + } - if (!empty($data['invoice_number'])) { - $indicators[] = 'Invoice Number: ' . $data['invoice_number']; - } + if (! empty($data['serial_number'])) { + $indicators[] = 'Serial Number: '.$data['serial_number']; + } - if (!empty($data['serial_number'])) { - $indicators[] = 'Serial Number: ' . $data['serial_number']; - } + if (! empty($data['sticker_master_id'])) { + $itemCode = Item::find($data['sticker_master_id'])->code ?? 'Unknown'; + $indicators[] = 'Item Code: '.$itemCode; + } - if (!empty($data['sticker_master_id'])) { - $itemCode = Item::find($data['sticker_master_id'])->code ?? 'Unknown'; - $indicators[] = 'Item Code: ' . $itemCode; - } + if (! empty($data['operator_id'])) { + $indicators[] = 'Created By: '.$data['operator_id']; + } - if (!empty($data['operator_id'])) { - $indicators[] = 'Created By: ' . $data['operator_id']; - } + if (! empty($data['created_from'])) { + $indicators[] = 'From: '.$data['created_from']; + } - if (!empty($data['created_from'])) { - $indicators[] = 'From: ' . $data['created_from']; - } + if (! empty($data['created_to'])) { + $indicators[] = 'To: '.$data['created_to']; + } - if (!empty($data['created_to'])) { - $indicators[] = 'To: ' . $data['created_to']; - } + if (! empty($data['scanned_status'])) { + $indicators[] = 'Scanned Status: '.$data['scanned_status']; + } - if (!empty($data['scanned_status'])) { - $indicators[] = 'Scanned Status: ' . $data['scanned_status']; - } - - return $indicators; - }) + return $indicators; + }), ]) ->filtersFormMaxHeight('280px') ->actions([ @@ -1258,7 +1262,7 @@ class InvoiceValidationResource extends Resource Tables\Actions\DeleteBulkAction::make(), Tables\Actions\ForceDeleteBulkAction::make(), Tables\Actions\RestoreBulkAction::make(), - FilamentExportBulkAction::make('export') + FilamentExportBulkAction::make('export'), ]), ]); }