From f4d416bea2692c284dedd1f2f08934ecc9afdf30 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Fri, 28 Aug 2026 12:54:53 +0530 Subject: [PATCH] Added pump testing result resource and exporter files and Updated pump testing entry report filter functionality --- .../Exports/PumpTestingResultExporter.php | 93 ++ .../Resources/PumpTestingEntryResource.php | 19 +- .../Resources/PumpTestingResultResource.php | 808 ++++++++++++++++++ .../Pages/CreatePumpTestingResult.php | 12 + .../Pages/EditPumpTestingResult.php | 22 + .../Pages/ListPumpTestingResults.php | 19 + .../Pages/ViewPumpTestingResult.php | 19 + 7 files changed, 986 insertions(+), 6 deletions(-) create mode 100644 app/Filament/Exports/PumpTestingResultExporter.php create mode 100644 app/Filament/Resources/PumpTestingResultResource.php create mode 100644 app/Filament/Resources/PumpTestingResultResource/Pages/CreatePumpTestingResult.php create mode 100644 app/Filament/Resources/PumpTestingResultResource/Pages/EditPumpTestingResult.php create mode 100644 app/Filament/Resources/PumpTestingResultResource/Pages/ListPumpTestingResults.php create mode 100644 app/Filament/Resources/PumpTestingResultResource/Pages/ViewPumpTestingResult.php diff --git a/app/Filament/Exports/PumpTestingResultExporter.php b/app/Filament/Exports/PumpTestingResultExporter.php new file mode 100644 index 0000000..8631bb3 --- /dev/null +++ b/app/Filament/Exports/PumpTestingResultExporter.php @@ -0,0 +1,93 @@ +label('NO') + ->state(function ($record) use (&$rowNumber) { + // Increment and return the row number + return ++$rowNumber; + }), + ExportColumn::make('plant.code') + ->label('PLANT CODE'), + ExportColumn::make('tested_date') + ->label('TESTED DATE'), + ExportColumn::make('tested_type') + ->label('TESTED TYPE'), + ExportColumn::make('pumpTestingMaster.item.code') + ->label('PUMP CODE'), + ExportColumn::make('pumpTestingMaster.item.category') + ->label('PUMP CATEGORY') + ->enabledByDefault(false), + ExportColumn::make('pumpTestingMaster.item.description') + ->label('PUMP TYPE'), + ExportColumn::make('pumpTestingMaster.item.uom') + ->label('UNIT OF MEASURE') + ->enabledByDefault(false), + ExportColumn::make('pump_serial_number') + ->label('PUMP SERIAL NUMBER'), + ExportColumn::make('correction_head') + ->label('CORRECTION HEAD'), + ExportColumn::make('sl_no') + ->label('SL. NO.'), + ExportColumn::make('voltage') + ->label('VOLTAGE'), + ExportColumn::make('current') + ->label('CURRENT'), + ExportColumn::make('watt') + ->label('WATT'), + ExportColumn::make('frequency') + ->label('FREQUENCY'), + ExportColumn::make('speed') + ->label('SPEED'), + ExportColumn::make('discharge') + ->label('FLOW DISCHARGE'), + ExportColumn::make('head') + ->label('HEAD'), + ExportColumn::make('pump_output') + ->label('PUMP OUTPUT'), + ExportColumn::make('power_p2') + ->label('PUMP P2'), + ExportColumn::make('overall_efficiency') + ->label('OVERALL EFFICIENCY'), + ExportColumn::make('pump_efficiency') + ->label('PUMP EFFICIENCY'), + ExportColumn::make('created_at') + ->label('CREATED AT'), + ExportColumn::make('created_by') + ->label('CREATED BY'), + ExportColumn::make('updated_at') + ->label('UPDATED AT'), + ExportColumn::make('updated_by') + ->label('UPDATED BY'), + ExportColumn::make('deleted_at') + ->enabledByDefault(false) + ->label('DELETED AT'), + ]; + } + + public static function getCompletedNotificationBody(Export $export): string + { + $body = 'Your pump testing result 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.'; + } + + return $body; + } +} diff --git a/app/Filament/Resources/PumpTestingEntryResource.php b/app/Filament/Resources/PumpTestingEntryResource.php index 2905f71..b5ce8a1 100644 --- a/app/Filament/Resources/PumpTestingEntryResource.php +++ b/app/Filament/Resources/PumpTestingEntryResource.php @@ -464,7 +464,7 @@ class PumpTestingEntryResource extends Resource }) ->afterStateUpdated(function ($state, callable $set, callable $get) { $set('Item', null); - $set('isi_type', null); + $set('tested_type', null); $set('created_by', null); $set('updated_by', null); }), @@ -479,7 +479,7 @@ class PumpTestingEntryResource extends Resource ->reactive() ->native(false), Select::make('tested_type') - ->label('Tested Type') + ->label('Search by Tested Type') ->nullable() ->options(function (callable $get) { $plantId = $get('Plant'); @@ -592,13 +592,13 @@ class PumpTestingEntryResource extends Resource } if (! empty($data['Item']) && ! empty($data['Plant'])) { - $itemIds = Item::where('id', $data['Item']) + $pumpMasterIds = PumpTestingMaster::where('item_id', $data['Item']) ->where('plant_id', $data['Plant']) ->pluck('id') ->toArray(); - if (! empty($itemIds)) { - $query->whereIn('item_id', $itemIds); + if (! empty($pumpMasterIds)) { + $query->whereIn('pump_testing_master_id', $pumpMasterIds); } } @@ -611,7 +611,14 @@ class PumpTestingEntryResource extends Resource })->pluck('id')->toArray(); if (! empty($descIds)) { - $query->whereIn('item_id', $descIds); + $pumpMasterIds = PumpTestingMaster::whereIn('item_id', $descIds) + ->where('plant_id', $data['Plant']) + ->pluck('id') + ->toArray(); + + if (! empty($pumpMasterIds)) { + $query->whereIn('pump_testing_master_id', $pumpMasterIds); + } } } diff --git a/app/Filament/Resources/PumpTestingResultResource.php b/app/Filament/Resources/PumpTestingResultResource.php new file mode 100644 index 0000000..7a6a943 --- /dev/null +++ b/app/Filament/Resources/PumpTestingResultResource.php @@ -0,0 +1,808 @@ +schema([ + Section::make('') + ->schema([ + Forms\Components\Select::make('plant_id') + ->label('Plant Name') + ->relationship('plant', 'name') + ->options(function (callable $get) { + $userHas = Filament::auth()->user()->plant_id; + + return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); + }) + ->columnSpan(1) // (['default' => 1, 'sm' => 2]) + ->required() + ->searchable() + ->reactive() + ->default(function () { + $userHas = Filament::auth()->user()->plant_id; + + return ($userHas && strlen($userHas) > 0) ? $userHas : optional(PumpTestingResult::latest()->first())->plant_id ?? null; + }) + ->disabled(fn (Get $get) => ! empty($get('id'))) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $plantId = $get('plant_id'); + $set('pump_testing_master_id', null); + $set('pump_master_id', null); + // $set('pump_type', null); + + if (! $plantId) { + $set('pTeError', 'Please select a plant first.'); + + return; + } else { + $set('pTeError', null); + } + + $set('updated_by', Filament::auth()->user()?->name); + }) + ->extraAttributes(fn ($get) => [ + 'class' => $get('pTeError') ? 'border-red-500' : '', + ]) + ->hint(fn ($get) => $get('pTeError') ? $get('pTeError') : null) + ->hintColor('danger'), + Forms\Components\Select::make('tested_type') + ->label('Tested Type') + ->selectablePlaceholder(false) + ->options(function (callable $get) { + $plantId = $get('plant_id'); + if ($plantId) { + return Configuration::where('plant_id', $plantId) + ->where('c_name', 'PUMP_DISPLAY_TEST_TYPE') + ->whereNot('c_value', 'Routine Test') + ->orderBy('created_at') + ->pluck('c_value', 'c_value') + ->toArray(); + } else { + return Configuration::where('c_name', 'PUMP_DISPLAY_TEST_TYPE') + ->whereNot('c_value', 'Routine Test') + ->orderBy('created_at') + ->pluck('c_value', 'c_value') + ->toArray(); + } + }) + ->afterStateUpdated(function ($state, callable $set) { + $set('updated_by', Filament::auth()->user()?->name); + }) + ->columnSpan(1) + ->required() + ->default('Pump Performance Test') + ->reactive(), + Forms\Components\DatePicker::make('tested_date') + ->label('Tested Date') + ->afterStateUpdated(function ($state, callable $set) { + $set('updated_by', Filament::auth()->user()?->name); + }) + ->columnSpan(1) + ->default(now()) + ->required() + ->reactive(), + Forms\Components\TextInput::make('correction_head') + ->label('Correction Head') + ->placeholder('Scan the correction head') + ->afterStateUpdated(function ($state, callable $set) { + $set('updated_by', Filament::auth()->user()?->name); + }) + ->columnSpan(1) + ->required() + ->reactive(), + Forms\Components\Hidden::make('pump_testing_master_id') + // ->relationship('pumpTestingMasters', 'id') + ->required(), + Forms\Components\Select::make('pump_master_id') + ->label('Pump Code') + // ->relationship('pumpTestingMasters', 'id') + ->options(function (callable $get) { + $plantId = $get('plant_id'); + if (! $plantId) { + return []; + } + + return Item::where('plant_id', $plantId)->whereHas('pumpTestingMasters')->orderBy('code')->pluck('code', 'code'); + }) + ->columnSpan(1) + ->required() + ->searchable() + ->reactive() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + $plantId = $get('plant_id'); // Get selected plant + $set('pump_testing_master_id', null); + // $set('pump_type', null); + if (! $plantId) { + $set('pump_master_id', null); + } elseif ($state) { + // $pumpMaster = PumpTestingMaster::where('plant_id', $plantId)->where('item_id', $state)->first(); + $pumpMaster = PumpTestingMaster::where('plant_id', $plantId)->whereHas('item', function ($query) use ($state) { + $query->where('code', $state); + })->first(); + + if (! $pumpMaster) { + $set('pump_master_id', null); + } else { + $set('pump_testing_master_id', $pumpMaster->id); + // $set('pump_type', $pumpMaster->item->description ?? null); + } + } + + $set('updated_by', Filament::auth()->user()?->name); + }) + ->afterStateHydrated(function ($component, $state, Get $get, Set $set) { + if ($get('id')) { + $itemId = PumpTestingMaster::where('id', $get('pump_testing_master_id'))->first()?->item_id; + if ($itemId) { + $item = Item::where('id', $itemId)->first()?->code; + if ($item) { + $set('pump_master_id', $item); + } else { + $set('pump_master_id', null); + } + } else { + $set('pump_master_id', null); + } + } + }), + // Forms\Components\TextInput::make('pump_type') + // ->label('Pump Type]') + // ->placeholder('Choose the pump code first') + // ->readOnly() + // ->afterStateUpdated(function ($state, callable $set) { + // $set('updated_by', Filament::auth()->user()?->name); + // }) + // ->columnSpan(1) + // ->required() + // ->reactive(), + Forms\Components\TextInput::make('pump_serial_number') + ->label('Pump Serial Number') + ->placeholder('Scan the pump serial number') + ->rule(function (callable $get) { + return Rule::unique('pump_testing_entries', 'pump_serial_number') + ->where('plant_id', $get('plant_id')) + ->where('pump_testing_master_id', $get('pump_testing_master_id')) + ->where('tested_type', $get('tested_type')) + ->where('sl_no', $get('sl_no')) + ->ignore($get('id')); + }) + ->afterStateUpdated(function ($state, callable $set) { + + if (strpos($state, '|') != false) { + $parts = explode('|', $state); + $set('pump_serial_number', $parts[1]); + } + + $set('updated_by', Filament::auth()->user()?->name); + }) + ->columnSpan(1) + ->required() + ->reactive(), + Forms\Components\TextInput::make('sl_no') + ->label('Sl. No.') + ->placeholder('Scan the sl. no.') + ->afterStateUpdated(function ($state, callable $set) { + $set('updated_by', Filament::auth()->user()?->name); + }) + ->columnSpan(1) + ->required() + ->reactive(), + Forms\Components\TextInput::make('voltage') + ->label('Voltage') + ->placeholder('Scan the voltage') + ->afterStateUpdated(function ($state, callable $set) { + $set('updated_by', Filament::auth()->user()?->name); + }) + ->columnSpan(1) + ->required() + ->reactive(), + Forms\Components\TextInput::make('current') + ->label('Current') + ->placeholder('Scan the current') + ->afterStateUpdated(function ($state, callable $set) { + $set('updated_by', Filament::auth()->user()?->name); + }) + ->columnSpan(1) + ->required() + ->reactive(), + Forms\Components\TextInput::make('watt') + ->label('Watt') + ->placeholder('Scan the watt') + ->afterStateUpdated(function ($state, callable $set) { + $set('updated_by', Filament::auth()->user()?->name); + }) + ->columnSpan(1) + ->required() + ->reactive(), + Forms\Components\TextInput::make('frequency') + ->label('Frequency') + ->placeholder('Scan the frequency') + ->afterStateUpdated(function ($state, callable $set) { + $set('updated_by', Filament::auth()->user()?->name); + }) + ->columnSpan(1) + ->required() + ->reactive(), + Forms\Components\TextInput::make('speed') + ->label('Speed') + ->placeholder('Scan the speed') + ->afterStateUpdated(function ($state, callable $set) { + $set('updated_by', Filament::auth()->user()?->name); + }) + ->columnSpan(1) + ->required() + ->reactive(), + Forms\Components\TextInput::make('discharge') + ->label('Discharge') + ->placeholder('Scan the discharge') + ->afterStateUpdated(function ($state, callable $set) { + $set('updated_by', Filament::auth()->user()?->name); + }) + ->columnSpan(1) + ->required() + ->reactive(), + Forms\Components\TextInput::make('head') + ->label('Head') + ->placeholder('Scan the head') + ->afterStateUpdated(function ($state, callable $set) { + $set('updated_by', Filament::auth()->user()?->name); + }) + ->columnSpan(1) + ->required() + ->reactive(), + Forms\Components\TextInput::make('pump_output') + ->label('Pump Output') + ->placeholder('Scan the pump output') + ->afterStateUpdated(function ($state, callable $set) { + $set('updated_by', Filament::auth()->user()?->name); + }) + ->columnSpan(1) + ->required() + ->reactive(), + Forms\Components\TextInput::make('power_p2') + ->label('Power P2') + ->placeholder('Scan the power p2') + ->afterStateUpdated(function ($state, callable $set) { + $set('updated_by', Filament::auth()->user()?->name); + }) + ->columnSpan(1) + ->required() + ->reactive(), + Forms\Components\TextInput::make('overall_efficiency') + ->label('Overall Efficiency') + ->placeholder('Scan the overall efficiency') + ->afterStateUpdated(function ($state, callable $set) { + $set('updated_by', Filament::auth()->user()?->name); + }) + ->columnSpan(1) + ->required() + ->reactive(), + Forms\Components\TextInput::make('pump_efficiency') + ->label('Pump Efficiency') + ->placeholder('Scan the pump efficiency') + ->afterStateUpdated(function ($state, callable $set) { + $set('updated_by', Filament::auth()->user()?->name); + }) + ->columnSpan(1) + ->required() + ->reactive(), + Forms\Components\Hidden::make('created_by') + ->default(fn () => Filament::auth()->user()?->name) + ->columnSpan(1) + ->required(), + Forms\Components\Hidden::make('updated_by') + ->default(fn () => Filament::auth()->user()?->name) + ->columnSpan(1) + ->required(), + Forms\Components\TextInput::make('id') + ->hidden() + ->columnSpan(1) + ->readOnly(), + ]) + ->columns(['default' => 1, 'sm' => 2]), + ]); + } + + public static function table(Table $table): Table + { + return $table + ->columns([ + // Tables\Columns\TextColumn::make('id') + // ->label('ID') + // ->numeric() + // ->sortable(), + Tables\Columns\TextColumn::make('No.') + ->label('No.') + ->getStateUsing(function ($record, $livewire, $column, $rowLoop) { + $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('plant.name') + ->label('Plant Name') + ->searchable() + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('tested_date') + ->label('Tested Date') + ->date() // 'F d, Y' + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('tested_type') + ->label('Tested Type') + ->searchable() + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('pumpTestingMasters.item.code') + ->label('Pump Code') + ->searchable() + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('pumpTestingMasters.item.category') + ->label('Pump Category') + ->default('-') + ->searchable() + ->alignCenter() + ->sortable() + ->toggleable(isToggledHiddenByDefault: true), + Tables\Columns\TextColumn::make('pumpTestingMasters.item.description') + ->label('Pump Type') + ->searchable() + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('pumpTestingMasters.item.uom') + ->label('Unit Of Measure') + ->default('-') + ->searchable() + ->alignCenter() + ->sortable() + ->toggleable(isToggledHiddenByDefault: true), + Tables\Columns\TextColumn::make('pump_serial_number') + ->label('Pump Serial Number') + ->searchable() + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('correction_head') + ->label('Correction Head') + ->searchable() + ->alignCenter(), + Tables\Columns\TextColumn::make('sl_no') + ->label('Sl. No.') + ->searchable() + ->alignCenter(), + Tables\Columns\TextColumn::make('voltage') + ->label('Voltage') + ->searchable() + ->alignCenter(), + Tables\Columns\TextColumn::make('current') + ->label('Current') + ->searchable() + ->alignCenter(), + Tables\Columns\TextColumn::make('watt') + ->label('Watt') + ->searchable() + ->alignCenter(), + Tables\Columns\TextColumn::make('frequency') + ->label('Frequency') + ->searchable() + ->alignCenter(), + Tables\Columns\TextColumn::make('speed') + ->label('Speed') + ->searchable() + ->alignCenter(), + Tables\Columns\TextColumn::make('discharge') + ->label('Discharge') + ->searchable() + ->alignCenter(), + Tables\Columns\TextColumn::make('head') + ->label('Head') + ->searchable() + ->alignCenter(), + Tables\Columns\TextColumn::make('pump_output') + ->label('Pump Output') + ->searchable() + ->alignCenter(), + Tables\Columns\TextColumn::make('power_p2') + ->label('Power P2') + ->searchable() + ->alignCenter(), + Tables\Columns\TextColumn::make('overall_efficiency') + ->label('Overall Efficiency') + ->searchable() + ->alignCenter(), + Tables\Columns\TextColumn::make('pump_efficiency') + ->label('Pump Efficiency') + ->searchable() + ->alignCenter(), + Tables\Columns\TextColumn::make('created_by') + ->label('Created By') + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('created_at') + ->label('Created At') + ->dateTime() + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('updated_by') + ->label('Updated By') + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('updated_at') + ->label('Updated At') + ->dateTime() + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('deleted_at') + ->label('Deleted At') + ->dateTime() + ->alignCenter() + ->sortable() + ->toggleable(isToggledHiddenByDefault: true), + ]) + ->filters([ + Tables\Filters\TrashedFilter::make(), + Filter::make('advanced_filters') + ->label('Advanced Filters') + ->form([ + Select::make('Plant') + ->label('Search by Plant Name') + ->nullable() + ->searchable() + // ->options(function () { + // return Plant::pluck('name', 'id'); + // }) + ->reactive() + ->options(function (callable $get) { + $userHas = Filament::auth()->user()->plant_id; + + if ($userHas && strlen($userHas) > 0) { + return Plant::where('id', $userHas)->pluck('name', 'id')->toArray(); + } else { + return Plant::whereHas('pumpTestingResults', function ($query) { + $query->whereNotNull('id'); + })->orderBy('code')->pluck('name', 'id'); + } + + // return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); + }) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $set('Item', null); + $set('tested_type', null); + $set('created_by', null); + $set('updated_by', null); + }), + DatePicker::make('tested_from') + ->label('Tested From') + ->placeholder('Select From Date') + ->reactive() + ->native(false), + DatePicker::make('tested_to') + ->label('Tested To') + ->placeholder('Select To Date') + ->reactive() + ->native(false), + Select::make('tested_type') + ->label('Search by Tested Type') + ->nullable() + ->options(function (callable $get) { + $plantId = $get('Plant'); + if (! $plantId) { + return PumpTestingResult::whereNotNull('tested_type')->select('tested_type')->distinct()->pluck('tested_type', 'tested_type'); + } else { + return PumpTestingResult::where('plant_id', $plantId)->whereNotNull('tested_type')->select('tested_type')->distinct()->pluck('tested_type', 'tested_type'); + } + }) + ->searchable() + ->reactive(), + Select::make('Item') + ->label('Search by Pump Code') + ->nullable() + ->options(function (callable $get) { + $pId = $get('Plant'); + + if (! $pId) { + return []; + } else { + return Item::whereHas('pumpTestingMasters', function ($query) use ($pId) { + if ($pId) { + $query->where('plant_id', $pId); + } + $query->whereHas('pumpTestingResults'); + })->pluck('code', 'id'); + } + }) + ->searchable() + ->reactive(), + TextInput::make('description') + ->label('Pump Type') + ->placeholder('Enter the Pump Type'), + TextInput::make('pump_serial_number') + ->label('Pump Serial Number') + ->placeholder('Enter the Pump Serial Number'), + Select::make('created_by') + ->label('Search by Created By') + ->nullable() + ->options(function (callable $get) { + $plantId = $get('Plant'); + if (! $plantId) { + return PumpTestingResult::whereNotNull('created_by')->select('created_by')->distinct()->pluck('created_by', 'created_by'); + } else { + return PumpTestingResult::where('plant_id', $plantId)->whereNotNull('created_by')->select('created_by')->distinct()->pluck('created_by', 'created_by'); + } + }) + ->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), + Select::make('updated_by') + ->label('Search by Updated By') + ->nullable() + ->options(function (callable $get) { + $plantId = $get('Plant'); + if (! $plantId) { + return PumpTestingResult::whereNotNull('updated_by')->select('updated_by')->distinct()->pluck('updated_by', 'updated_by'); + } else { + return PumpTestingResult::where('plant_id', $plantId)->whereNotNull('updated_by')->select('updated_by')->distinct()->pluck('updated_by', 'updated_by'); + } + }) + ->searchable() + ->reactive(), + DateTimePicker::make(name: 'updated_from') + ->label('Updated From') + ->placeholder(placeholder: 'Select From DateTime') + ->reactive() + ->native(false), + DateTimePicker::make('updated_to') + ->label('Updated 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['Plant']) && empty($data['tested_from']) && empty($data['tested_to']) && empty($data['tested_type']) && empty($data['Item']) && empty($data['description']) && empty($data['pump_serial_number']) && empty($data['created_by']) && empty($data['created_from']) && empty($data['created_to']) && empty($data['updated_by']) && empty($data['updated_from']) && empty($data['updated_to'])) { + return $query->whereRaw('1 = 0'); + } + + if (! empty($data['Plant'])) { + $query->where('plant_id', $data['Plant']); + } else { + $userHas = Filament::auth()->user()->plant_id; + + if ($userHas && strlen($userHas) > 0) { + return $query->whereRaw('1 = 0'); + } + } + + if (! empty($data['tested_from'])) { + $query->where('tested_date', '>=', $data['tested_from']); + } + + if (! empty($data['tested_to'])) { + $query->where('tested_date', '<=', $data['tested_to']); + } + + if (! empty($data['tested_type'])) { + $query->where('tested_type', $data['tested_type']); + } + + if (! empty($data['Item']) && ! empty($data['Plant'])) { + $pumpMasterIds = PumpTestingMaster::where('item_id', $data['Item']) + ->where('plant_id', $data['Plant']) + ->pluck('id') + ->toArray(); + + if (! empty($pumpMasterIds)) { + $query->whereIn('pump_testing_master_id', $pumpMasterIds); + } + } + + if (! empty($data['description'])) { + $pId = $data['Plant'] ?? null; + $descIds = Item::where('description', 'like', '%'.$data['description'].'%')->whereHas('pumpTestingMasters', function ($query) use ($pId) { + if ($pId) { + $query->where('plant_id', $pId); + } + })->pluck('id')->toArray(); + + if (! empty($descIds)) { + $pumpMasterIds = PumpTestingMaster::whereIn('item_id', $descIds) + ->where('plant_id', $data['Plant']) + ->pluck('id') + ->toArray(); + + if (! empty($pumpMasterIds)) { + $query->whereIn('pump_testing_master_id', $pumpMasterIds); + } + } + } + + if (! empty($data['pump_serial_number'])) { + $query->where('pump_serial_number', 'like', '%'.$data['pump_serial_number'].'%'); + } + + if (! empty($data['created_by'])) { + $query->where('created_by', $data['created_by']); + } + + 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['updated_by'])) { + $query->where('updated_by', $data['updated_by']); + } + + if (! empty($data['updated_from'])) { + $query->where('updated_at', '>=', $data['updated_from']); + } + + if (! empty($data['updated_to'])) { + $query->where('updated_at', '<=', $data['updated_to']); + } + }) + ->indicateUsing(function (array $data) { + $indicators = []; + + if (! empty($data['Plant'])) { + $indicators[] = 'Plant Name: '.Plant::where('id', $data['Plant'])->value('name'); + } else { + $userHas = Filament::auth()->user()->plant_id; + + if ($userHas && strlen($userHas) > 0) { + return 'Plant: Choose plant to filter records.'; + } + } + + if (! empty($data['tested_from'])) { + $indicators[] = 'Tested From: '.$data['tested_from']; + } + + if (! empty($data['tested_to'])) { + $indicators[] = 'Tested To: '.$data['tested_to']; + } + + if (! empty($data['tested_type'])) { + $indicators[] = 'Tested Type: '.$data['tested_type']; + } + + if (! empty($data['Item']) && ! empty($data['Plant'])) { + $itemCode = Item::find($data['Item'])->code ?? 'Unknown'; + $indicators[] = 'Pump Code: '.$itemCode; + } + + if (! empty($data['description'])) { + $indicators[] = 'Pump Type: '.$data['description']; + } + + if (! empty($data['pump_serial_number'])) { + $indicators[] = 'Pump Serial Number: '.$data['pump_serial_number']; + } + + if (! empty($data['created_by'])) { + $indicators[] = 'Created By: '.$data['created_by']; + } + + if (! empty($data['created_from'])) { + $indicators[] = 'Created From: '.$data['created_from']; + } + + if (! empty($data['created_to'])) { + $indicators[] = 'Created To: '.$data['created_to']; + } + + if (! empty($data['updated_by'])) { + $indicators[] = 'Updated By: '.$data['updated_by']; + } + + if (! empty($data['updated_from'])) { + $indicators[] = 'Updated From: '.$data['updated_from']; + } + + if (! empty($data['updated_to'])) { + $indicators[] = 'Updated To: '.$data['updated_to']; + } + + return $indicators; + }), + ]) + ->filtersFormMaxHeight('280px') + ->actions([ + Tables\Actions\ViewAction::make(), + Tables\Actions\EditAction::make(), + ]) + ->headerActions([ + ExportAction::make() + ->label('Export Pump Testing Entries') + ->color('warning') + ->exporter(PumpTestingResultExporter::class) + ->visible(function () { + return Filament::auth()->user()->can('view export pump testing results'); + }), + ]) + ->bulkActions([ + Tables\Actions\BulkActionGroup::make([ + Tables\Actions\DeleteBulkAction::make(), + Tables\Actions\ForceDeleteBulkAction::make(), + Tables\Actions\RestoreBulkAction::make(), + ]), + ]); + } + + public static function getRelations(): array + { + return [ + // + ]; + } + + public static function getPages(): array + { + return [ + 'index' => Pages\ListPumpTestingResults::route('/'), + 'create' => Pages\CreatePumpTestingResult::route('/create'), + 'view' => Pages\ViewPumpTestingResult::route('/{record}'), + 'edit' => Pages\EditPumpTestingResult::route('/{record}/edit'), + ]; + } + + public static function getEloquentQuery(): Builder + { + return parent::getEloquentQuery() + ->withoutGlobalScopes([ + SoftDeletingScope::class, + ]); + } +} diff --git a/app/Filament/Resources/PumpTestingResultResource/Pages/CreatePumpTestingResult.php b/app/Filament/Resources/PumpTestingResultResource/Pages/CreatePumpTestingResult.php new file mode 100644 index 0000000..2f2d81f --- /dev/null +++ b/app/Filament/Resources/PumpTestingResultResource/Pages/CreatePumpTestingResult.php @@ -0,0 +1,12 @@ +