From 2e6f27824c20eee5e5ea93e4246023e9dca5ace3 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Fri, 23 Jan 2026 11:00:41 +0530 Subject: [PATCH] changed logic in invoice pending reason page --- app/Filament/Pages/InvoicePendingReason.php | 240 +++++++++++++++++- .../pages/invoice-pending-reason.blade.php | 30 ++- 2 files changed, 263 insertions(+), 7 deletions(-) diff --git a/app/Filament/Pages/InvoicePendingReason.php b/app/Filament/Pages/InvoicePendingReason.php index ce686c0..703c882 100644 --- a/app/Filament/Pages/InvoicePendingReason.php +++ b/app/Filament/Pages/InvoicePendingReason.php @@ -2,6 +2,7 @@ namespace App\Filament\Pages; +use App\Imports\InvoicePendingReasonImport; use App\Models\InvoiceDataValidation; use App\Models\InvoiceOutValidation; use App\Models\Plant; @@ -10,12 +11,15 @@ use Filament\Forms\Contracts\HasForms; use Filament\Forms\Concerns\InteractsWithForms; use Filament\Forms\Form; use Filament\Facades\Filament; +use Filament\Forms\Components\FileUpload; use Filament\Forms\Components\Hidden; use Filament\Forms\Components\Select; use Filament\Forms\Components\TextInput; use Filament\Notifications\Notification; use Filament\Pages\Dashboard\Concerns\HasFiltersForm; use Illuminate\Support\Facades\Auth; +use Maatwebsite\Excel\Facades\Excel; +use Storage; class InvoicePendingReason extends Page { @@ -26,6 +30,13 @@ class InvoicePendingReason extends Page protected static ?string $navigationGroup = 'Manufacturing SD'; + public ?string $file = null; + + public array $importErrors = []; + + public array $invoicePending = []; + + public function mount(): void { $this->filtersForm->fill([ @@ -40,7 +51,6 @@ class InvoicePendingReason extends Page return $form ->statePath('filters') ->schema([ - Select::make('plant_id') ->label('Plant') ->reactive() @@ -52,6 +62,14 @@ class InvoicePendingReason extends Page }) ->afterStateUpdated(function ($state, $set, callable $get,$livewire) { $plantId = $get('plant_id'); + + if($plantId){ + $this->dispatch('loadData' ,$plantId); + } + else{ + $this->dispatch('emptyData'); + } + $set('document_number', null); $set('customer_trade_name', null); $set('location', null); @@ -127,7 +145,7 @@ class InvoicePendingReason extends Page ->toArray(); }) ->afterStateUpdated(function ($state, callable $set, callable $get) { - $plantId = $get('plant_id'); + $plantId = $get('plant_id'); if (empty($plantId)) { return []; } @@ -171,6 +189,28 @@ class InvoicePendingReason extends Page ]) ->autofocus() ->required(), + FileUpload::make('file') + ->label('Upload Excel File') + ->required() + ->disk('local') + ->multiple(false) + ->directory('invoice-pending') + ->dehydrated(false) + //->preserveFilenames() + //->storeFiles() + //storeFileNamesIn('original_name') + // ->visibility('private') + ->acceptedFileTypes([ + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + 'application/vnd.ms-excel', + 'text/csv', + ]) + ->rules(['mimes:xlsx,xls,csv']) + ->afterStateUpdated(function ($state, callable $set) { + if ($state instanceof \Livewire\Features\SupportFileUploads\TemporaryUploadedFile) { + $set('file', $state->store('invoice-pending')); + } + }), ]) ->columns(3); } @@ -224,6 +264,202 @@ class InvoicePendingReason extends Page ->send(); } + // public function importPendingReason() + // { + + // $file = $this->filters['file'] ?? null; + + // $absolutePath = Storage::disk('local')->path($file); + + // Excel::import(new InvoicePendingReasonImport, $absolutePath); + + // $this->reset('filters.file'); + + // Notification::make() + // ->title('File processed and database updated successfully') + // ->success() + // ->send(); + // } + + public function importPendingReason() + { + $file = $this->filters['file'] ?? null; + + $plantId = $this->filters['plant_id'] ?? null; + + if (empty($file)) { + Notification::make() + ->title('Please upload a file') + ->danger() + ->send(); + return; + } + + // if ( + // empty($fileFilter) || + // !is_array($fileFilter) || + // empty($fileFilter[0]) + // ) { + // Notification::make() + // ->title('Please upload a file') + // ->danger() + // ->send(); + + // return; + // } + + + // $filePath = $fileFilter[0]; + + // if (!is_string($filePath)) { + // Notification::make() + // ->title('Invalid file upload') + // ->danger() + // ->send(); + + // return; + // } + + $absolutePath = Storage::disk('local')->path($file); + + $import = new InvoicePendingReasonImport(); + + try { + + Excel::import( + $import, + $absolutePath + ); + + if(!empty($import->plantCodeEmpty)) { + + Notification::make() + ->title('Import failed') + ->body("Plant code can't be empty") + ->danger() + ->send(); + $this->filtersForm->fill([ + 'file' => null, + ]); + return; + } + else if(!empty($import->docNoEmpty)) { + + Notification::make() + ->title('Import failed') + ->body("Document number can't be empty") + ->danger() + ->send(); + $this->filtersForm->fill([ + 'file' => null, + ]); + return; + } + // else if(!empty($import->remarkEmpty)) { + + // Notification::make() + // ->title('Import failed') + // ->body("Remark can't be empty") + // ->danger() + // ->send(); + // $this->filtersForm->fill([ + // 'file' => null, + // ]); + // return; + // } + else if (! empty($import->duplicateExcelDocs)) { + + $duplicates = collect($import->duplicateExcelDocs) + ->map(function ($rows, $key) { + [$plant, $doc] = explode('|', $key); + return "{$plant}-{$doc}"; + }) + ->implode(', '); + + Notification::make() + ->title('Import failed') + ->body("Duplicate Document Numbers found in Excel: {$duplicates}") + ->danger() + ->send(); + $this->filtersForm->fill([ + 'file' => null, + ]); + return; + } + else if(!empty($import->missingPlantCodes)) { + $codes = implode(', ', array_keys($import->missingPlantCodes)); + Notification::make() + ->title('Import failed') + ->body("Plant codes not found: {$codes}") + ->danger() + ->send(); + $this->filtersForm->fill([ + 'file' => null, + ]); + return; + } + else if(!empty($import->missingDocNo)) { + $docNo = implode(', ', array_keys($import->missingDocNo)); + Notification::make() + ->title('Import failed') + ->body("Document numbers not found: {$docNo}") + ->danger() + ->send(); + $this->filtersForm->fill([ + 'file' => null, + ]); + return; + } + + foreach ($import->validRows as $row) { + InvoiceDataValidation::where('plant_id', $row['plant_id']) + ->where('document_number', $row['document_number']) + ->update([ + 'remark' => $row['remark'], + 'updated_at' => now(), + ]); + } + + Notification::make() + ->title('Import successful') + ->body('All records updated successfully.') + ->success() + ->send(); + + $this->dispatch('loadData' ,$plantId); + + $this->filtersForm->fill([ + 'file' => null, + 'plant_id' => $plantId, + ]); + + } catch (\Throwable $e) { + + Notification::make() + ->title('Import error') + ->body($e->getMessage()) + ->danger() + ->send(); + } + } + + public function exportPendingReason() + { + $plantId = $this->filters['plant_id'] ?? null; + + if (! $plantId) { + Notification::make() + ->title('Plant') + ->body("please select plant to export data..!") + ->danger() + ->send(); + return; + } + + $this->dispatch('loadData1' ,$plantId); + + } + public static function canAccess(): bool { return Auth::check() && Auth::user()->can('view invoice pending reason'); diff --git a/resources/views/filament/pages/invoice-pending-reason.blade.php b/resources/views/filament/pages/invoice-pending-reason.blade.php index 5be04b3..444ac17 100644 --- a/resources/views/filament/pages/invoice-pending-reason.blade.php +++ b/resources/views/filament/pages/invoice-pending-reason.blade.php @@ -1,15 +1,35 @@
- {{-- {{ $this->filtersForm($this->form) }} --}} {{ $this->filtersForm($this->form) }}
- + + + + Import + + + Export + +
+ +