Added invoice pending reason page #195

Merged
jothi merged 1 commits from ranjith-dev into master 2026-01-20 04:53:20 +00:00
2 changed files with 185 additions and 0 deletions

View File

@@ -0,0 +1,179 @@
<?php
namespace App\Filament\Pages;
use App\Models\InvoiceDataValidation;
use App\Models\InvoiceOutValidation;
use App\Models\Plant;
use Filament\Pages\Page;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Form;
use Filament\Facades\Filament;
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;
class InvoicePendingReason extends Page
{
use HasFiltersForm;
protected static ?string $navigationIcon = 'heroicon-o-document-text';
protected static string $view = 'filament.pages.invoice-pending-reason';
protected static ?string $navigationGroup = 'Manufacturing SD';
public function mount(): void
{
$this->filtersForm->fill([
'plant_id' => null,
'document_number' => null,
'remark' => null,
]);
}
public function filtersForm(Form $form): Form
{
return $form
->statePath('filters')
->schema([
Select::make('plant_id')
->label('Plant')
->reactive()
->required()
->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();
})
->afterStateUpdated(function ($state, $set, callable $get,$livewire) {
$plantId = $get('plant_id');
$set('document_number', null);
})
->hint(fn ($get) => $get('pqPlantError') ? $get('pqPlantError') : null)
->hintColor('danger'),
Select::make('document_number')
->label('Document Number')
->required()
->reactive()
->columnSpan(1)
->options(function (callable $get) {
$plantId = $get('plant_id');
if (empty($plantId)) {
return [];
}
$distributions = InvoiceDataValidation::whereNotNull('distribution_channel_desc')
->distinct()
->pluck('distribution_channel_desc')
->filter(fn ($v) => trim($v) !== '')
->values()
->toArray();
$distributions[] = '';
$pendingInvoices = collect();
foreach ($distributions as $distribution) {
$invoices = InvoiceDataValidation::where('plant_id', $plantId)
->where('distribution_channel_desc', $distribution)
->select('id', 'document_number')
->get()
->unique('document_number')
->filter(fn ($inv) =>
! empty($inv->document_number) &&
! str_contains($inv->document_number, '-')
);
if (trim($distribution) == '') {
$invoices = $invoices->filter(fn ($inv) =>
str_starts_with($inv->document_number, '7')
);
}
if ($invoices->isEmpty()) {
continue;
}
$invoiceNumbers = $invoices->pluck('document_number')
->map(fn ($n) => preg_replace('/\s+/', '', strtoupper($n)))
->toArray();
$wentOut = InvoiceOutValidation::whereIn('qr_code', $invoiceNumbers)
->distinct()
->pluck('qr_code')
->map(fn ($n) => preg_replace('/\s+/', '', strtoupper($n)))
->toArray();
$pending = $invoices->filter(function ($inv) use ($wentOut) {
$doc = preg_replace('/\s+/', '', strtoupper($inv->document_number));
return ! in_array($doc, $wentOut, true);
});
$pendingInvoices = $pendingInvoices->merge($pending);
}
return $pendingInvoices
->unique('document_number')
->pluck('document_number', 'document_number')
->toArray();
})
->afterStateUpdated(function ($state, callable $set, callable $get) {
})
->extraAttributes(fn ($get) => [
'class' => $get('pqBlockError') ? 'border-red-500' : '',
])
->hint(fn ($get) => $get('pqBlockError') ? $get('pqBlockError') : null)
->hintColor('danger'),
TextInput::make('remark')
->label('Remark')
->reactive()
->extraAttributes([
'wire:keydown.enter.prevent' => 'addRemark($event.target.value)',
])
->autofocus()
->required(),
])
->columns(3);
}
public function addRemark($value){
$plantId = $this->filters['plant_id'] ?? null;
$documentNumber = $this->filters['document_number'] ?? null;
$remark = $value;
if (! $plantId || ! $documentNumber || $remark == '') {
return;
}
InvoiceDataValidation::where('plant_id', $plantId)
->where('document_number', $documentNumber)
->update([
'remark' => $remark,
'updated_at' => now(),
]);
$this->filtersForm->fill([
'plant_id' => $plantId,
'document_number' => $documentNumber,
'remark' => null,
]);
Notification::make()
->title('Remark updated successfully')
->success()
->send();
}
public static function canAccess(): bool
{
return Auth::check() && Auth::user()->can('view invoice pending reason');
}
}

View File

@@ -0,0 +1,6 @@
<x-filament-panels::page>
<div class="space-y-4">
{{-- {{ $this->filtersForm($this->form) }} --}}
{{ $this->filtersForm($this->form) }}
</div>
</x-filament-panels::page>