From eac712aaac8592da2933f1d87df63c90626e7431 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Fri, 28 Aug 2026 12:39:19 +0530 Subject: [PATCH 1/7] Added pump testing result migration file --- ...3801_create_pump_testing_results_table.php | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 database/migrations/2026_08_26_113801_create_pump_testing_results_table.php diff --git a/database/migrations/2026_08_26_113801_create_pump_testing_results_table.php b/database/migrations/2026_08_26_113801_create_pump_testing_results_table.php new file mode 100644 index 0000000..8e800d0 --- /dev/null +++ b/database/migrations/2026_08_26_113801_create_pump_testing_results_table.php @@ -0,0 +1,61 @@ + Date: Fri, 28 Aug 2026 12:42:59 +0530 Subject: [PATCH 2/7] Added pump testing result policies and model file --- app/Models/PumpTestingResult.php | 47 ++++++++++ app/Policies/PumpTestingResultPolicy.php | 105 +++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 app/Models/PumpTestingResult.php create mode 100644 app/Policies/PumpTestingResultPolicy.php diff --git a/app/Models/PumpTestingResult.php b/app/Models/PumpTestingResult.php new file mode 100644 index 0000000..06bcd51 --- /dev/null +++ b/app/Models/PumpTestingResult.php @@ -0,0 +1,47 @@ +belongsTo(Plant::class); + } + + public function pumpTestingMasters(): BelongsTo + { + return $this->belongsTo(PumpTestingMaster::class, 'pump_testing_master_id', 'id'); + } +} diff --git a/app/Policies/PumpTestingResultPolicy.php b/app/Policies/PumpTestingResultPolicy.php new file mode 100644 index 0000000..5583cf6 --- /dev/null +++ b/app/Policies/PumpTestingResultPolicy.php @@ -0,0 +1,105 @@ +checkPermissionTo('view-any PumpTestingResult'); + } + + /** + * Determine whether the user can view the model. + */ + public function view(User $user, PumpTestingResult $pumptestingresult): bool + { + return $user->checkPermissionTo('view PumpTestingResult'); + } + + /** + * Determine whether the user can create models. + */ + public function create(User $user): bool + { + return $user->checkPermissionTo('create PumpTestingResult'); + } + + /** + * Determine whether the user can update the model. + */ + public function update(User $user, PumpTestingResult $pumptestingresult): bool + { + return $user->checkPermissionTo('update PumpTestingResult'); + } + + /** + * Determine whether the user can delete the model. + */ + public function delete(User $user, PumpTestingResult $pumptestingresult): bool + { + return $user->checkPermissionTo('delete PumpTestingResult'); + } + + /** + * Determine whether the user can delete any models. + */ + public function deleteAny(User $user): bool + { + return $user->checkPermissionTo('delete-any PumpTestingResult'); + } + + /** + * Determine whether the user can restore the model. + */ + public function restore(User $user, PumpTestingResult $pumptestingresult): bool + { + return $user->checkPermissionTo('restore PumpTestingResult'); + } + + /** + * Determine whether the user can restore any models. + */ + public function restoreAny(User $user): bool + { + return $user->checkPermissionTo('restore-any PumpTestingResult'); + } + + /** + * Determine whether the user can replicate the model. + */ + public function replicate(User $user, PumpTestingResult $pumptestingresult): bool + { + return $user->checkPermissionTo('replicate PumpTestingResult'); + } + + /** + * Determine whether the user can reorder the models. + */ + public function reorder(User $user): bool + { + return $user->checkPermissionTo('reorder PumpTestingResult'); + } + + /** + * Determine whether the user can permanently delete the model. + */ + public function forceDelete(User $user, PumpTestingResult $pumptestingresult): bool + { + return $user->checkPermissionTo('force-delete PumpTestingResult'); + } + + /** + * Determine whether the user can permanently delete any models. + */ + public function forceDeleteAny(User $user): bool + { + return $user->checkPermissionTo('force-delete-any PumpTestingResult'); + } +} -- 2.49.1 From 5279b016d1f49f3c701b9065394a7801965896d3 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Fri, 28 Aug 2026 12:48:51 +0530 Subject: [PATCH 3/7] Added pump testing result model files for hasMany relations --- app/Models/Plant.php | 5 +++++ app/Models/PumpTestingMaster.php | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/app/Models/Plant.php b/app/Models/Plant.php index 7b3d18e..58a6ed0 100644 --- a/app/Models/Plant.php +++ b/app/Models/Plant.php @@ -94,6 +94,11 @@ class Plant extends Model return $this->hasMany(PumpTestingEntry::class, 'plant_id', 'id'); } + public function pumpTestingResults() + { + return $this->hasMany(PumpTestingResult::class, 'plant_id', 'id'); + } + public function guardNames() { return $this->hasMany(GuardName::class, 'plant_id', 'id'); diff --git a/app/Models/PumpTestingMaster.php b/app/Models/PumpTestingMaster.php index 271624f..f67f348 100644 --- a/app/Models/PumpTestingMaster.php +++ b/app/Models/PumpTestingMaster.php @@ -59,4 +59,9 @@ class PumpTestingMaster extends Model { return $this->hasMany(PumpTestingEntry::class, 'pump_testing_master_id', 'id'); } + + public function pumpTestingResults() + { + return $this->hasMany(PumpTestingResult::class, 'pump_testing_master_id', 'id'); + } } -- 2.49.1 From f4d416bea2692c284dedd1f2f08934ecc9afdf30 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Fri, 28 Aug 2026 12:54:53 +0530 Subject: [PATCH 4/7] 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 @@ + Date: Fri, 28 Aug 2026 13:01:24 +0530 Subject: [PATCH 5/7] Added pump testing entry / result post api function with validations and Updated pump testing master / entry / result get api function validations, --- .../Controllers/TestingPanelController.php | 1133 ++++++++++++++--- routes/api.php | 4 + 2 files changed, 958 insertions(+), 179 deletions(-) diff --git a/app/Http/Controllers/TestingPanelController.php b/app/Http/Controllers/TestingPanelController.php index ecbd571..4e59f66 100644 --- a/app/Http/Controllers/TestingPanelController.php +++ b/app/Http/Controllers/TestingPanelController.php @@ -10,7 +10,9 @@ use App\Models\MotorTestingMaster; use App\Models\Plant; use App\Models\PumpTestingEntry; use App\Models\PumpTestingMaster; +use App\Models\PumpTestingResult; use App\Models\TestingPanelReading; +use App\Models\User; use App\Models\WorkGroupMaster; use Carbon\Carbon; use Illuminate\Http\Request; @@ -452,7 +454,7 @@ class TestingPanelController extends Controller // return response("Plant not found.", 400)->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Plant code '{$plantCode}' not found!", + 'status_description' => "Plant code '{$plantCode}' not found in plant table!", ], 404); } @@ -552,7 +554,7 @@ class TestingPanelController extends Controller // return response("Plant not found.", 400)->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Plant code '{$plantCode}' not found!", + 'status_description' => "Plant code '{$plantCode}' not found in plant table!", 'status_response' => (object) [], ], 404); } @@ -938,18 +940,26 @@ class TestingPanelController extends Controller $itemCode = $request->header('item-code'); // $description = $item ? $item->description : ''; - if ($plantCode == null || $plantCode == '') { - // return response("ERROR: Plant Name can't be empty", 400) - // ->header('Content-Type', 'text/plain'); + if ($plantCode == null || $plantCode == '' || ! $plantCode) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Plant code can't be empty!", - ], 400); - } elseif (Str::length($plantCode) < 4 || ! is_numeric($plantCode) || ! preg_match('/^[1-9]\d{3,}$/', $plantCode)) { + ], 404); + } elseif (! is_numeric($plantCode)) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Invalid plant code found!', - ], 400); + 'status_description' => "Plant code '{$plantCode}' should contain only numeric values!", + ], 404); + } elseif (Str::length($plantCode) < 4 || Str::length($plantCode) > 7) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code '{$plantCode}' must be between 4 and 7 digits only!", + ], 404); + } elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCode)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Invalid plant code '{$plantCode}' found!", + ], 404); } elseif ($itemCode == null || $itemCode == '' || ! $itemCode) { return response()->json([ 'status_code' => 'ERROR', @@ -977,8 +987,8 @@ class TestingPanelController extends Controller if (! $plant) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Plant not found!', - ], 400); + 'status_description' => "Plant code '{$plantCode}' not found in plant table!", + ], 404); } $plantId = $plant->id; @@ -1079,22 +1089,41 @@ class TestingPanelController extends Controller ], 403); } + $userName = $request->header('user-name'); + + if (! $userName || $userName == null || $userName == '') { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "User name can't be empty!", + ], 404); + } elseif ($userName == 'jothi') { + $userName = 'Admin'; + } + $plantCode = $request->header('plant-code'); $pumpCode = $request->header('pump-code'); // $description = $item ? $item->description : ''; - if ($plantCode == null || $plantCode == '') { - // return response("ERROR: Plant Name can't be empty", 400) - // ->header('Content-Type', 'text/plain'); + if ($plantCode == null || $plantCode == '' || ! $plantCode) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Plant code can't be empty!", - ], 400); - } elseif (Str::length($plantCode) < 4 || ! is_numeric($plantCode) || ! preg_match('/^[1-9]\d{3,}$/', $plantCode)) { + ], 404); + } elseif (! is_numeric($plantCode)) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Invalid plant code found!', - ], 400); + 'status_description' => "Plant code '{$plantCode}' should contain only numeric values!", + ], 404); + } elseif (Str::length($plantCode) < 4 || Str::length($plantCode) > 7) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code '{$plantCode}' must be between 4 and 7 digits only!", + ], 404); + } elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCode)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Invalid plant code '{$plantCode}' found!", + ], 404); } elseif ($pumpCode == null || $pumpCode == '' || ! $pumpCode) { return response()->json([ 'status_code' => 'ERROR', @@ -1122,13 +1151,34 @@ class TestingPanelController extends Controller if (! $plant) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Plant not found!', - ], 400); + 'status_description' => "Plant code '{$plantCode}' not found in plant table!", + ], 404); } $plantId = $plant->id; $plantName = $plant->name; + $user = User::where('name', $userName)->first(); + + $userPlant = User::where('name', $userName)->where('plant_id', $plantId)->first(); + + if (! $user) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "User name '{$userName}' not found in users table!", + ], 403); + } elseif (! $userPlant && ! $user->hasRole('Super Admin')) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "User name '{$userName}' not found for the plant '{$plantName}' in users table!", + ], 403); + } elseif (! $user->hasRole(['Super Admin', 'Pump Testing Quality Manager', 'Pump Testing Quality Supervisor', 'Pump Testing Quality Technician'])) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "User '{$userName}' does not have rights!", + ], 403); + } + $item = Item::where('code', $pumpCode)->first(); if (! $item) { @@ -1219,23 +1269,42 @@ class TestingPanelController extends Controller ], 403); } + $userName = $request->header('user-name'); + + if (! $userName || $userName == null || $userName == '') { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "User name can't be empty!", + ], 404); + } elseif ($userName == 'jothi') { + $userName = 'Admin'; + } + $plantCode = $request->header('plant-code'); $pumpCode = $request->header('pump-code'); $pumpSerial = $request->header('pump-serial-number'); $testedType = $request->header('tested-type') ?? 'Pump Performance Test'; - if ($plantCode == null || $plantCode == '') { - // return response("ERROR: Plant Name can't be empty", 400) - // ->header('Content-Type', 'text/plain'); + if ($plantCode == null || $plantCode == '' || ! $plantCode) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Plant code can't be empty!", - ], 400); - } elseif (Str::length($plantCode) < 4 || ! is_numeric($plantCode) || ! preg_match('/^[1-9]\d{3,}$/', $plantCode)) { + ], 404); + } elseif (! is_numeric($plantCode)) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Invalid plant code found!', - ], 400); + 'status_description' => "Plant code '{$plantCode}' should contain only numeric values!", + ], 404); + } elseif (Str::length($plantCode) < 4 || Str::length($plantCode) > 7) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code '{$plantCode}' must be between 4 and 7 digits only!", + ], 404); + } elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCode)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Invalid plant code '{$plantCode}' found!", + ], 404); } elseif ($pumpCode == null || $pumpCode == '' || ! $pumpCode) { return response()->json([ 'status_code' => 'ERROR', @@ -1288,19 +1357,40 @@ class TestingPanelController extends Controller if (! $plant) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Plant code : '$plantCode' not found!", - ], 400); + 'status_description' => "Plant code '{$plantCode}' not found in plant table!", + ], 404); } $plantId = $plant->id; $plantName = $plant->name; + $user = User::where('name', $userName)->first(); + + $userPlant = User::where('name', $userName)->where('plant_id', $plantId)->first(); + + if (! $user) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "User name '{$userName}' not found in users table!", + ], 403); + } elseif (! $userPlant && ! $user->hasRole('Super Admin')) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "User name '{$userName}' not found for the plant '{$plantName}' in users table!", + ], 403); + } elseif (! $user->hasRole(['Super Admin', 'Pump Testing Quality Manager', 'Pump Testing Quality Supervisor', 'Pump Testing Quality Technician'])) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "User '{$userName}' does not have rights!", + ], 403); + } + $item = Item::where('code', $pumpCode)->first(); if (! $item) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Pump code : '$pumpCode' not found in item master table!", + 'status_description' => "Pump code '$pumpCode' not found in item master table!", ], 404); } @@ -1309,7 +1399,7 @@ class TestingPanelController extends Controller if (! $item) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Pump code : '$pumpCode' not found in item master table for the plant : '$plantName'!", + 'status_description' => "Pump code '$pumpCode' not found in item master table for the plant '$plantName'!", ], 404); } @@ -1322,7 +1412,7 @@ class TestingPanelController extends Controller if (! $pumpTestingMaster) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Pump code : '$pumpCode' not found in pump testing master table!", + 'status_description' => "Pump code '$pumpCode' not found in pump testing master table!", ], 404); } @@ -1331,7 +1421,7 @@ class TestingPanelController extends Controller if (! $pumpTestingMaster) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Pump code : '$pumpCode' not found in pump testing master table for the plant : '$plantName'!", + 'status_description' => "Pump code '$pumpCode' not found in pump testing master table for the plant '$plantName'!", ], 404); } @@ -1342,7 +1432,7 @@ class TestingPanelController extends Controller if (! $pumpTestingEntry) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Pump serial number : '$pumpSerial' not found in pump testing entry table!", + 'status_description' => "Pump serial number '$pumpSerial' not found in pump testing entry table!", ], 404); } @@ -1351,7 +1441,7 @@ class TestingPanelController extends Controller if (! $pumpTestingEntry) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Pump serial number : '$pumpSerial' not found in pump testing entry table for the plant : '$plantName'!", + 'status_description' => "Pump serial number '$pumpSerial' not found in pump testing entry table for the plant '$plantName'!", ], 404); } @@ -1360,7 +1450,7 @@ class TestingPanelController extends Controller if (! $pumpTestingEntry) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Pump serial number : '$pumpSerial' with Pump code : '$pumpCode' not found in pump testing entry table for the plant : '$plantName'!", + 'status_description' => "Pump serial number '$pumpSerial' with Pump code '$pumpCode' not found in pump testing entry table for the plant '$plantName'!", ], 404); } @@ -1369,27 +1459,27 @@ class TestingPanelController extends Controller if (! $pumpTestingEntry) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Pump serial number : '$pumpSerial' with Tested type : '$testedType' not found in pump testing entry table for the plant : '$plantName'!", + 'status_description' => "Pump serial number '$pumpSerial' with Tested type '$testedType' not found in pump testing entry table for the plant '$plantName'!", ], 404); } $pumpTestingEntries = PumpTestingEntry::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->where('tested_type', $testedType)->get()->toArray(); $output = [ - 'tested_date' => $pumpTestingEntry->tested_date ?? '', // $result['tested_date'] ?? '2026-07-16' - 'correction_head' => $pumpTestingEntry->correction_head ?? '', // $result['correction_head'] ?? '5.0', - 'pump_entries' => array_map(function ($entry) { + 'tested_date' => $pumpTestingEntry->tested_date ?? now('Asia/Kolkata')->toDateString(), // $result['tested_date'] ?? '2026-07-16' + 'correction_head' => $pumpTestingEntry->correction_head ?? '0', // $result['correction_head'] ?? '5.0', + 'pump_entries' => array_map(function ($entry) use ($userName) { return [ 'sl_no' => $entry['sl_no'] ?? '', - 'voltage' => $entry['voltage'] ?? '', - 'frequency' => $entry['frequency'] ?? '', - 'speed' => $entry['speed'] ?? '', - 'd_head' => $entry['head'] ?? '', - 'flow_reading' => $entry['flow_reading'] ?? '', - 'current' => $entry['current'] ?? '', - 'watt' => $entry['watt'] ?? '', - 'tested_by' => $entry['created_by'] ?? '', - 'created_datetime' => ! empty($entry['created_at']) ? Carbon::parse($entry['created_at'])->setTimezone('Asia/Kolkata')->format('Y-m-d H:i:s') : '', + 'voltage' => $entry['voltage'] ?? '0', + 'frequency' => $entry['frequency'] ?? '0', + 'speed' => $entry['speed'] ?? '0', + 'd_head' => $entry['head'] ?? '0', + 'flow_reading' => $entry['flow_reading'] ?? '0', + 'current' => $entry['current'] ?? '0', + 'watt' => $entry['watt'] ?? '0', + 'tested_by' => $entry['created_by'] ?? $userName, + 'created_datetime' => ! empty($entry['created_at']) ? Carbon::parse($entry['created_at'])->setTimezone('Asia/Kolkata')->format('Y-m-d H:i:s') : now('Asia/Kolkata')->format('Y-m-d H:i:s'), ]; }, $pumpTestingEntries ?? []), ]; @@ -1397,7 +1487,7 @@ class TestingPanelController extends Controller return response()->json($output, 200); } - public function get_pump_result(Request $request) + public function storePumpEntry(Request $request) { $expectedUser = env('API_AUTH_USER'); $expectedPw = env('API_AUTH_PW'); @@ -1412,22 +1502,49 @@ class TestingPanelController extends Controller ], 403); } + $userName = $request->header('user-name'); + + if (! $userName || $userName == null || $userName == '') { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "User name can't be empty!", + ], 404); + } elseif ($userName == 'jothi') { + $userName = 'Admin'; + } + $plantCode = $request->header('plant-code'); $pumpCode = $request->header('pump-code'); $pumpSerial = $request->header('pump-serial-number'); + $testedType = $request->header('tested-type') ?? 'Pump Performance Test'; - if ($plantCode == null || $plantCode == '') { - // return response("ERROR: Plant Name can't be empty", 400) - // ->header('Content-Type', 'text/plain'); + $json = $request->input('data'); + $data = json_decode($json, true); + + $testedDate = $data['tested_date'] ?? now('Asia/Kolkata')->toDateString(); + $correctionHead = $data['correction_head'] ?? '0'; + $entries = $data['pump_entries'] ?? []; + + if ($plantCode == null || $plantCode == '' || ! $plantCode) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Plant code can't be empty!", - ], 400); - } elseif (Str::length($plantCode) < 4 || ! is_numeric($plantCode) || ! preg_match('/^[1-9]\d{3,}$/', $plantCode)) { + ], 404); + } elseif (! is_numeric($plantCode)) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Invalid plant code found!', - ], 400); + 'status_description' => "Plant code '{$plantCode}' should contain only numeric values!", + ], 404); + } elseif (Str::length($plantCode) < 4 || Str::length($plantCode) > 7) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code '{$plantCode}' must be between 4 and 7 digits only!", + ], 404); + } elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCode)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Invalid plant code '{$plantCode}' found!", + ], 404); } elseif ($pumpCode == null || $pumpCode == '' || ! $pumpCode) { return response()->json([ 'status_code' => 'ERROR', @@ -1468,6 +1585,21 @@ class TestingPanelController extends Controller 'status_code' => 'ERROR', 'status_description' => "Pump serial number '{$pumpSerial}' should not begin with '0' or letter!", ], 404); + } elseif ($testedType == null || $testedType == '' || ! $testedType) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Tested type can't be empty!", + ], 404); + } elseif ($testedDate == null || $testedDate == '' || ! $testedDate) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Tested date can't be empty!", + ], 404); + } elseif ($correctionHead == null || $correctionHead == '' || ! $correctionHead) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Correction head can't be empty!", + ], 404); } $plant = Plant::where('code', $plantCode)->first(); @@ -1475,19 +1607,40 @@ class TestingPanelController extends Controller if (! $plant) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Plant not found!', - ], 400); + 'status_description' => "Plant code '{$plantCode}' not found in plant table!", + ], 404); } $plantId = $plant->id; $plantName = $plant->name; + $user = User::where('name', $userName)->first(); + + $userPlant = User::where('name', $userName)->where('plant_id', $plantId)->first(); + + if (! $user) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "User name '{$userName}' not found in users table!", + ], 403); + } elseif (! $userPlant && ! $user->hasRole('Super Admin')) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "User name '{$userName}' not found for the plant '{$plantName}' in users table!", + ], 403); + } elseif (! $user->hasRole(['Super Admin', 'Pump Testing Quality Manager', 'Pump Testing Quality Supervisor', 'Pump Testing Quality Technician'])) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "User '{$userName}' does not have rights!", + ], 403); + } + $item = Item::where('code', $pumpCode)->first(); if (! $item) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Pump code not found in item master table!', + 'status_description' => "Pump code '$pumpCode' not found in item master table!", ], 404); } @@ -1496,142 +1649,764 @@ class TestingPanelController extends Controller if (! $item) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Pump code not found in item master table for the plant : '$plantName'!", + 'status_description' => "Pump code '$pumpCode' not found in item master table for the plant '$plantName'!", ], 404); } $itemId = $item->id; - $description = $item ? $item->description : ''; + // $description = $item ? $item->description : ''; - $category = $item ? $item->category : ''; + $pumpTestingMaster = PumpTestingMaster::where('item_id', $itemId)->first(); - // $pumpTestingMaster = PumpTestingMaster::where('item_id', $itemId)->first(); - - // if (! $pumpTestingMaster) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => 'Pump code not found in pump testing master table!', - // ], 404); - // } - - // $pumpTestingMaster = PumpTestingMaster::where('plant_id', $plantId)->where('item_id', $itemId)->first(); - - // if (! $pumpTestingMaster) { - // return response()->json([ - // 'status_code' => 'ERROR', - // 'status_description' => "Pump code not found in pump testing master table for the plant : '$plantName'!", - // ], 404); - // } - - $entries = $result['pump_entries'] ?? []; - - if (empty($entries)) { - $entries = [ - [ - 'sl_no' => '1', - 'voltage' => '415', - 'frequency' => '50.02', - 'speed' => '2976', - 'head' => '5', - 'flow_reading' => '18.75', - 'current' => '61.8', - 'watt' => '40730', - ], - [ - 'sl_no' => '2', - 'voltage' => '415', - 'frequency' => '50.05', - 'speed' => '2976', - 'head' => '100', - 'flow_reading' => '18.71', - 'current' => '72.7', - 'watt' => '47100', - ], - [ - 'sl_no' => '3', - 'voltage' => '415', - 'frequency' => '50.04', - 'speed' => '2975', - 'head' => '150', - 'flow_reading' => '17.65', - 'current' => '76.3', - 'watt' => '48000', - ], - [ - 'sl_no' => '4', - 'voltage' => '415', - 'frequency' => '50.02', - 'speed' => '2974', - 'head' => '200', - 'flow_reading' => '15.65', - 'current' => '74', - 'watt' => '47760', - ], - [ - 'sl_no' => '5', - 'voltage' => '415', - 'frequency' => '50.04', - 'speed' => '2977', - 'head' => '250', - 'flow_reading' => '12.51', - 'current' => '68.2', - 'watt' => '45240', - ], - [ - 'sl_no' => '6', - 'voltage' => '415', - 'frequency' => '50', - 'speed' => '2979', - 'head' => '270', - 'flow_reading' => '11.53', - 'current' => '65.5', - 'watt' => '43050', - ], - [ - 'sl_no' => '7', - 'voltage' => '415', - 'frequency' => '50.05', - 'speed' => '2983', - 'head' => '300', - 'flow_reading' => '8.65', - 'current' => '57.4', - 'watt' => '37460', - ], - [ - 'sl_no' => '8', - 'voltage' => '415', - 'frequency' => '50.03', - 'speed' => '2990', - 'head' => '325', - 'flow_reading' => '0', - 'current' => '35.8', - 'watt' => '21300', - ], - ]; + if (! $pumpTestingMaster) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump code '$pumpCode' not found in pump testing master table!", + ], 404); } + $pumpTestingMaster = PumpTestingMaster::where('plant_id', $plantId)->where('item_id', $itemId)->first(); + + if (! $pumpTestingMaster) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump code '$pumpCode' not found in pump testing master table for the plant '$plantName'!", + ], 404); + } + + $pumpMasterId = $pumpTestingMaster->id; + + $pumpTestingEntry = PumpTestingEntry::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->first(); + + if ($pumpTestingEntry) { + $existPumpCode = $pumpTestingEntry?->pumpTestingMasters?->item?->code; + if ($existPumpCode != $pumpCode) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Duplicate pump code found! Pump serial number '{$pumpSerial}' is already assigned for pump code '{$existPumpCode}'!", + ], 404); + } + } + + $pumpTestingEntry = PumpTestingEntry::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->where('tested_type', $testedType)->first(); + + if ($pumpTestingEntry) { + // update logic + $cnt = 0; + $updatedRows = 0; + foreach ($entries as $entry) { + $sl_no = $entry['sl_no'] ?? null; + $voltage = $entry['voltage'] ?? '0'; + $frequency = $entry['frequency'] ?? '0'; + $speed = $entry['speed'] ?? '0'; + $head = $entry['head'] ?? '0'; + $flow_reading = $entry['flow_reading'] ?? '0'; + $current = $entry['current'] ?? '0'; + $watt = $entry['watt'] ?? '0'; + $tested_by = $entry['tested_by'] ?? $userName; + // $pendingExists = PumpTestingEntry::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->where('tested_type', $testedType)->where('sl_no', $sl_no)->first(); + + $cnt++; + if ($sl_no == 'Constant') { + for ($i = $cnt; $i <= 10; $i++) { + PumpTestingEntry::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->where('tested_type', $testedType)->where('sl_no', $i)->forceDelete(); + } + } + + $rowAffected = PumpTestingEntry::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->where('tested_type', $testedType)->where('sl_no', $sl_no) + ->update([ + 'tested_date' => $testedDate, + 'correction_head' => $correctionHead, + 'voltage' => $voltage, + 'frequency' => $frequency, + 'speed' => $speed, + 'head' => $head, + 'flow_reading' => $flow_reading, + 'current' => $current, + 'watt' => $watt, + 'created_by' => $tested_by, + 'updated_at' => now(), + 'updated_by' => $userName, + ]); + + if ($rowAffected) { + $updatedRows++; + } else { + $updatedRows++; + PumpTestingEntry::create([ + 'plant_id' => $plantId, + 'pump_testing_master_id' => $pumpMasterId, + 'tested_date' => $testedDate, + 'tested_type' => $testedType, + 'pump_serial_number' => $pumpSerial, + 'correction_head' => $correctionHead, + 'sl_no' => $sl_no, + 'voltage' => $voltage, + 'frequency' => $frequency, + 'speed' => $speed, + 'head' => $head, + 'flow_reading' => $flow_reading, + 'current' => $current, + 'watt' => $watt, + 'created_by' => $tested_by, + 'updated_at' => now(), + 'updated_by' => $userName, + ]); + } + } + + if ($updatedRows == 0) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Failed to update exist pump testing entries for the pump serial number '{$pumpSerial}'.", + ], 404); + } else { + return response()->json([ + 'status_code' => 'SUCCESS', + 'status_description' => "Successfully updated exist pump testing entries for the pump serial number '{$pumpSerial}'.", + ], 200); + } + } else { + // insert logic + $createdRows = 0; + foreach ($entries as $entry) { + $sl_no = $entry['sl_no'] ?? null; + $voltage = $entry['voltage'] ?? '0'; + $frequency = $entry['frequency'] ?? '0'; + $speed = $entry['speed'] ?? '0'; + $head = $entry['head'] ?? '0'; + $flow_reading = $entry['flow_reading'] ?? '0'; + $current = $entry['current'] ?? '0'; + $watt = $entry['watt'] ?? '0'; + $tested_by = $entry['tested_by'] ?? $userName; + + $createdRows++; + PumpTestingEntry::create([ + 'plant_id' => $plantId, + 'pump_testing_master_id' => $pumpMasterId, + 'tested_date' => $testedDate, + 'tested_type' => $testedType, + 'pump_serial_number' => $pumpSerial, + 'correction_head' => $correctionHead, + 'sl_no' => $sl_no, + 'voltage' => $voltage, + 'frequency' => $frequency, + 'speed' => $speed, + 'head' => $head, + 'flow_reading' => $flow_reading, + 'current' => $current, + 'watt' => $watt, + 'created_by' => $tested_by, + 'updated_at' => now(), + 'updated_by' => $userName, + ]); + } + + if ($createdRows == 0) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Failed to create new pump testing entries for the pump serial number '{$pumpSerial}'.", + ], 404); + } else { + return response()->json([ + 'status_code' => 'SUCCESS', + 'status_description' => "Successfully created new pump testing entries for the pump serial number '{$pumpSerial}'.", + ], 200); + } + } + } + + public function get_pump_result(Request $request) + { + $expectedUser = env('API_AUTH_USER'); + $expectedPw = env('API_AUTH_PW'); + $header_auth = $request->header('Authorization'); + $expectedToken = $expectedUser.':'.$expectedPw; + + // $data = $request->all(); + if ('Bearer '.$expectedToken != $header_auth) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid authorization token!', + ], 403); + } + + $userName = $request->header('user-name'); + + if (! $userName || $userName == null || $userName == '') { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "User name can't be empty!", + ], 404); + } elseif ($userName == 'jothi') { + $userName = 'Admin'; + } + + $plantCode = $request->header('plant-code'); + $pumpCode = $request->header('pump-code'); + $pumpSerial = $request->header('pump-serial-number'); + $testedType = $request->header('tested-type') ?? 'Pump Performance Test'; + + if ($plantCode == null || $plantCode == '' || ! $plantCode) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code can't be empty!", + ], 404); + } elseif (! is_numeric($plantCode)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code '{$plantCode}' should contain only numeric values!", + ], 404); + } elseif (Str::length($plantCode) < 4 || Str::length($plantCode) > 7) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code '{$plantCode}' must be between 4 and 7 digits only!", + ], 404); + } elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCode)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Invalid plant code '{$plantCode}' found!", + ], 404); + } elseif ($pumpCode == null || $pumpCode == '' || ! $pumpCode) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump code can't be empty!", + ], 404); + } elseif (Str::length($pumpCode) < 6) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump code '{$pumpCode}' should contain minimum 6 digits!", + ], 404); + } elseif (! ctype_alnum($pumpCode)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump code '{$pumpCode}' should contain only alpha-numeric values!", + ], 404); + } elseif (! preg_match('/^[a-zA-Z1-9][a-zA-Z0-9]{5,}$/', $pumpCode)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump code '{$pumpCode}' should not begin with '0'!", + ], 404); + } elseif ($pumpSerial == null || $pumpSerial == '' || ! $pumpSerial) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump serial number can't be empty!", + ], 404); + } elseif (Str::length($pumpSerial) < 9) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump serial number '{$pumpSerial}' should contain minimum 9 digits!", + ], 404); + } elseif (! ctype_alnum($pumpSerial)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump serial number '{$pumpSerial}' should contain only alpha-numeric values!", + ], 404); + } elseif (! preg_match('/^[1-9][a-zA-Z0-9]{8,}$/', $pumpSerial)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump serial number '{$pumpSerial}' should not begin with '0' or letter!", + ], 404); + } elseif ($testedType == null || $testedType == '' || ! $testedType) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Tested type can't be empty!", + ], 404); + } + + $plant = Plant::where('code', $plantCode)->first(); + + if (! $plant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code '{$plantCode}' not found in plant table!", + ], 404); + } + + $plantId = $plant->id; + $plantName = $plant->name; + + $user = User::where('name', $userName)->first(); + + $userPlant = User::where('name', $userName)->where('plant_id', $plantId)->first(); + + if (! $user) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "User name '{$userName}' not found in users table!", + ], 403); + } elseif (! $userPlant && ! $user->hasRole('Super Admin')) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "User name '{$userName}' not found for the plant '{$plantName}' in users table!", + ], 403); + } elseif (! $user->hasRole(['Super Admin', 'Pump Testing Quality Manager', 'Pump Testing Quality Supervisor', 'Pump Testing Quality Technician'])) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "User '{$userName}' does not have rights!", + ], 403); + } + + $item = Item::where('code', $pumpCode)->first(); + + if (! $item) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump code '$pumpCode' not found in item master table!", + ], 404); + } + + $item = Item::where('plant_id', $plantId)->where('code', $pumpCode)->first(); + + if (! $item) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump code '$pumpCode' not found in item master table for the plant '$plantName'!", + ], 404); + } + + $itemId = $item->id; + + // $description = $item ? $item->description : ''; + + $pumpTestingMaster = PumpTestingMaster::where('item_id', $itemId)->first(); + + if (! $pumpTestingMaster) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump code '$pumpCode' not found in pump testing master table!", + ], 404); + } + + $pumpTestingMaster = PumpTestingMaster::where('plant_id', $plantId)->where('item_id', $itemId)->first(); + + if (! $pumpTestingMaster) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump code '$pumpCode' not found in pump testing master table for the plant '$plantName'!", + ], 404); + } + + $pumpMasterId = $pumpTestingMaster->id; + + $pumpTestingResult = PumpTestingResult::where('pump_serial_number', $pumpSerial)->first(); + + if (! $pumpTestingResult) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump serial number '$pumpSerial' not found in pump testing result table!", + ], 404); + } + + $pumpTestingResult = PumpTestingResult::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->first(); + + if (! $pumpTestingResult) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump serial number '$pumpSerial' not found in pump testing result table for the plant '$plantName'!", + ], 404); + } + + $pumpTestingResult = PumpTestingResult::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->first(); + + if (! $pumpTestingResult) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump serial number '$pumpSerial' with Pump code '$pumpCode' not found in pump testing result table for the plant '$plantName'!", + ], 404); + } + + $pumpTestingResult = PumpTestingResult::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->where('tested_type', $testedType)->first(); + + if (! $pumpTestingResult) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump serial number '$pumpSerial' with Tested type '$testedType' not found in pump testing result table for the plant '$plantName'!", + ], 404); + } + + $pumpTestingResults = PumpTestingResult::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->where('tested_type', $testedType)->get()->toArray(); + $output = [ - 'tested_date' => $result['tested_date'] ?? '2026-07-16', - 'test_type' => $result['test-type'] ?? 'Pump Performance Test', - 'correction_head' => $result['correction_head'] ?? '5.0', - 'pump_entries' => array_map(function ($entry) { + 'tested_date' => $pumpTestingResult->tested_date ?? now('Asia/Kolkata')->toDateString(), // $result['tested_date'] ?? '2026-07-16' + 'correction_head' => $pumpTestingResult->correction_head ?? '0', // $result['correction_head'] ?? '5.0', + 'pump_results' => array_map(function ($result) use ($userName) { return [ - 'sl_no' => $entry['sl_no'] ?? '', - 'voltage' => $entry['voltage'] ?? '', - 'frequency' => $entry['frequency'] ?? '', - 'speed' => $entry['speed'] ?? '', - 'head' => $entry['head'] ?? '', - 'flow_reading' => $entry['flow_reading'] ?? '', - 'current' => $entry['current'] ?? '', - 'watt' => $entry['watt'] ?? '', + 'sl_no' => $result['sl_no'] ?? '', + 'voltage' => $result['voltage'] ?? '0', + 'frequency' => $result['frequency'] ?? '0', + 'speed' => $result['speed'] ?? '0', + 'd_head' => $result['head'] ?? '0', + 'flow_reading' => $result['flow_reading'] ?? '0', + 'current' => $result['current'] ?? '0', + 'watt' => $result['watt'] ?? '0', + 'tested_by' => $result['created_by'] ?? $userName, + 'created_datetime' => ! empty($result['created_at']) ? Carbon::parse($result['created_at'])->setTimezone('Asia/Kolkata')->format('Y-m-d H:i:s') : now('Asia/Kolkata')->format('Y-m-d H:i:s'), ]; - }, $entries ?? []), + }, $pumpTestingResults ?? []), ]; return response()->json($output, 200); } + public function storePumpResult(Request $request) + { + $expectedUser = env('API_AUTH_USER'); + $expectedPw = env('API_AUTH_PW'); + $header_auth = $request->header('Authorization'); + $expectedToken = $expectedUser.':'.$expectedPw; + + // $data = $request->all(); + if ('Bearer '.$expectedToken != $header_auth) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid authorization token!', + ], 403); + } + + $userName = $request->header('user-name'); + + if (! $userName || $userName == null || $userName == '') { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "User name can't be empty!", + ], 404); + } elseif ($userName == 'jothi') { + $userName = 'Admin'; + } + + $plantCode = $request->header('plant-code'); + $pumpCode = $request->header('pump-code'); + $pumpSerial = $request->header('pump-serial-number'); + $testedType = $request->header('tested-type') ?? 'Pump Performance Test'; + + $json = $request->input('data'); + $data = json_decode($json, true); + + $testedDate = $data['tested_date'] ?? now('Asia/Kolkata')->toDateString(); + $correctionHead = $data['correction_head'] ?? '0'; + $results = $data['pump_results'] ?? []; + + if ($plantCode == null || $plantCode == '' || ! $plantCode) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code can't be empty!", + ], 404); + } elseif (! is_numeric($plantCode)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code '{$plantCode}' should contain only numeric values!", + ], 404); + } elseif (Str::length($plantCode) < 4 || Str::length($plantCode) > 7) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code '{$plantCode}' must be between 4 and 7 digits only!", + ], 404); + } elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCode)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Invalid plant code '{$plantCode}' found!", + ], 404); + } elseif ($pumpCode == null || $pumpCode == '' || ! $pumpCode) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump code can't be empty!", + ], 404); + } elseif (Str::length($pumpCode) < 6) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump code '{$pumpCode}' should contain minimum 6 digits!", + ], 404); + } elseif (! ctype_alnum($pumpCode)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump code '{$pumpCode}' should contain only alpha-numeric values!", + ], 404); + } elseif (! preg_match('/^[a-zA-Z1-9][a-zA-Z0-9]{5,}$/', $pumpCode)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump code '{$pumpCode}' should not begin with '0'!", + ], 404); + } elseif ($pumpSerial == null || $pumpSerial == '' || ! $pumpSerial) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump serial number can't be empty!", + ], 404); + } elseif (Str::length($pumpSerial) < 9) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump serial number '{$pumpSerial}' should contain minimum 9 digits!", + ], 404); + } elseif (! ctype_alnum($pumpSerial)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump serial number '{$pumpSerial}' should contain only alpha-numeric values!", + ], 404); + } elseif (! preg_match('/^[1-9][a-zA-Z0-9]{8,}$/', $pumpSerial)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump serial number '{$pumpSerial}' should not begin with '0' or letter!", + ], 404); + } elseif ($testedType == null || $testedType == '' || ! $testedType) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Tested type can't be empty!", + ], 404); + } elseif ($testedDate == null || $testedDate == '' || ! $testedDate) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Tested date can't be empty!", + ], 404); + } elseif ($correctionHead == null || $correctionHead == '' || ! $correctionHead) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Correction head can't be empty!", + ], 404); + } + + $plant = Plant::where('code', $plantCode)->first(); + + if (! $plant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant code '{$plantCode}' not found in plant table!", + ], 404); + } + + $plantId = $plant->id; + $plantName = $plant->name; + + $user = User::where('name', $userName)->first(); + + $userPlant = User::where('name', $userName)->where('plant_id', $plantId)->first(); + + if (! $user) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "User name '{$userName}' not found in users table!", + ], 403); + } elseif (! $userPlant && ! $user->hasRole('Super Admin')) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "User name '{$userName}' not found for the plant '{$plantName}' in users table!", + ], 403); + } elseif (! $user->hasRole(['Super Admin', 'Pump Testing Quality Manager', 'Pump Testing Quality Supervisor', 'Pump Testing Quality Technician'])) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "User '{$userName}' does not have rights!", + ], 403); + } + + $item = Item::where('code', $pumpCode)->first(); + + if (! $item) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump code '$pumpCode' not found in item master table!", + ], 404); + } + + $item = Item::where('plant_id', $plantId)->where('code', $pumpCode)->first(); + + if (! $item) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump code '$pumpCode' not found in item master table for the plant '$plantName'!", + ], 404); + } + + $itemId = $item->id; + + // $description = $item ? $item->description : ''; + + $pumpTestingMaster = PumpTestingMaster::where('item_id', $itemId)->first(); + + if (! $pumpTestingMaster) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump code '$pumpCode' not found in pump testing master table!", + ], 404); + } + + $pumpTestingMaster = PumpTestingMaster::where('plant_id', $plantId)->where('item_id', $itemId)->first(); + + if (! $pumpTestingMaster) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Pump code '$pumpCode' not found in pump testing master table for the plant '$plantName'!", + ], 404); + } + + $pumpMasterId = $pumpTestingMaster->id; + + $pumpTestingResult = PumpTestingResult::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->first(); + + if ($pumpTestingResult) { + $existPumpCode = $pumpTestingResult?->pumpTestingMasters?->item?->code; + if ($existPumpCode != $pumpCode) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Duplicate pump code found! Pump serial number '{$pumpSerial}' is already assigned for pump code '{$existPumpCode}'!", + ], 404); + } + } + + $pumpTestingResult = PumpTestingResult::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->where('tested_type', $testedType)->first(); + + if ($pumpTestingResult) { + // update logic + $cnt = 0; + $updatedRows = 0; + foreach ($results as $result) { + if (! $result) { + continue; + } + + $sl_no = $result['sl_no'] ?? null; + $voltage = $result['voltage'] ?? '0'; + $current = $result['current'] ?? '0'; + $watt = $result['watt'] ?? '0'; + $frequency = $result['frequency'] ?? '0'; + $speed = $result['speed'] ?? '0'; + $discharge = $result['discharge'] ?? '0'; + $head = $result['head'] ?? '0'; + $pump_output = $result['pump_output'] ?? '0'; + $power_p2 = $result['power_p2'] ?? '0'; + $overall_efficiency = $result['overall_efficiency'] ?? '0'; + $pump_efficiency = $result['pump_efficiency'] ?? '0'; + $tested_by = $result['tested_by'] ?? $userName; + // $pendingExists = PumpTestingResult::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->where('tested_type', $testedType)->where('sl_no', $sl_no)->first(); + + $cnt++; + + $rowAffected = PumpTestingResult::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->where('tested_type', $testedType)->where('sl_no', $sl_no) + ->update([ + 'tested_date' => $testedDate, + 'correction_head' => $correctionHead, + 'voltage' => $voltage, + 'current' => $current, + 'watt' => $watt, + 'frequency' => $frequency, + 'speed' => $speed, + 'discharge' => $discharge, + 'head' => $head, + 'pump_output' => $pump_output, + 'power_p2' => $power_p2, + 'overall_efficiency' => $overall_efficiency, + 'pump_efficiency' => $pump_efficiency, + 'created_by' => $tested_by, + 'updated_at' => now(), + 'updated_by' => $userName, + ]); + + if ($rowAffected) { + $updatedRows++; + } else { + $updatedRows++; + PumpTestingResult::create([ + 'plant_id' => $plantId, + 'pump_testing_master_id' => $pumpMasterId, + 'tested_date' => $testedDate, + 'tested_type' => $testedType, + 'pump_serial_number' => $pumpSerial, + 'correction_head' => $correctionHead, + 'sl_no' => $sl_no, + 'voltage' => $voltage, + 'current' => $current, + 'watt' => $watt, + 'frequency' => $frequency, + 'speed' => $speed, + 'discharge' => $discharge, + 'head' => $head, + 'pump_output' => $pump_output, + 'power_p2' => $power_p2, + 'overall_efficiency' => $overall_efficiency, + 'pump_efficiency' => $pump_efficiency, + 'created_by' => $tested_by, + 'updated_at' => now(), + 'updated_by' => $userName, + ]); + } + } + + if ($updatedRows == 0) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Failed to update exist pump testing entries for the pump serial number '{$pumpSerial}'.", + ], 404); + } else { + for ($i = $cnt; $i <= 10; $i++) { + PumpTestingResult::where('plant_id', $plantId)->where('pump_serial_number', $pumpSerial)->where('pump_testing_master_id', $pumpMasterId)->where('tested_type', $testedType)->where('sl_no', $i)->forceDelete(); + } + + return response()->json([ + 'status_code' => 'SUCCESS', + 'status_description' => "Successfully updated exist pump testing entries for the pump serial number '{$pumpSerial}'.", + ], 200); + } + } else { + // insert logic + $createdRows = 0; + foreach ($results as $result) { + if (! $result) { + continue; + } + + $sl_no = $result['sl_no'] ?? null; + $voltage = $result['voltage'] ?? '0'; + $current = $result['current'] ?? '0'; + $watt = $result['watt'] ?? '0'; + $frequency = $result['frequency'] ?? '0'; + $speed = $result['speed'] ?? '0'; + $discharge = $result['discharge'] ?? '0'; + $head = $result['head'] ?? '0'; + $pump_output = $result['pump_output'] ?? '0'; + $power_p2 = $result['power_p2'] ?? '0'; + $overall_efficiency = $result['overall_efficiency'] ?? '0'; + $pump_efficiency = $result['pump_efficiency'] ?? '0'; + $tested_by = $result['tested_by'] ?? $userName; + + $createdRows++; + PumpTestingResult::create([ + 'plant_id' => $plantId, + 'pump_testing_master_id' => $pumpMasterId, + 'tested_date' => $testedDate, + 'tested_type' => $testedType, + 'pump_serial_number' => $pumpSerial, + 'correction_head' => $correctionHead, + 'sl_no' => $sl_no, + 'voltage' => $voltage, + 'current' => $current, + 'watt' => $watt, + 'frequency' => $frequency, + 'speed' => $speed, + 'discharge' => $discharge, + 'head' => $head, + 'pump_output' => $pump_output, + 'power_p2' => $power_p2, + 'overall_efficiency' => $overall_efficiency, + 'pump_efficiency' => $pump_efficiency, + 'created_by' => $tested_by, + 'updated_at' => now(), + 'updated_by' => $userName, + ]); + } + + if ($createdRows == 0) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Failed to create new pump testing entries for the pump serial number '{$pumpSerial}'.", + ], 404); + } else { + return response()->json([ + 'status_code' => 'SUCCESS', + 'status_description' => "Successfully created new pump testing entries for the pump serial number '{$pumpSerial}'.", + ], 200); + } + } + } + /** * Update the specified resource in storage. */ diff --git a/routes/api.php b/routes/api.php index 5f35861..8272e6b 100644 --- a/routes/api.php +++ b/routes/api.php @@ -158,8 +158,12 @@ Route::get('testing/pump-master/get', [TestingPanelController::class, 'get_pump_ Route::get('testing/pump-entry/get', [TestingPanelController::class, 'get_pump_entry']); +Route::post('testing/pump-entry/store-data', [TestingPanelController::class, 'storePumpEntry']); + Route::get('testing/pump-result/get', [TestingPanelController::class, 'get_pump_result']); +Route::post('testing/pump-result/store-data', [TestingPanelController::class, 'storePumpResult']); + // Testing panel Controller Route::get('testing/user/get-data', [UserController::class, 'get_user_data']); -- 2.49.1 From ce0bfa72ca5a6c1a8731952777a3bdcc84f4b829 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Fri, 28 Aug 2026 14:15:07 +0530 Subject: [PATCH 6/7] Updated unique query table name and exportAction label --- app/Filament/Resources/PumpTestingResultResource.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Filament/Resources/PumpTestingResultResource.php b/app/Filament/Resources/PumpTestingResultResource.php index 7a6a943..4ec61f0 100644 --- a/app/Filament/Resources/PumpTestingResultResource.php +++ b/app/Filament/Resources/PumpTestingResultResource.php @@ -197,7 +197,7 @@ class PumpTestingResultResource extends Resource ->label('Pump Serial Number') ->placeholder('Scan the pump serial number') ->rule(function (callable $get) { - return Rule::unique('pump_testing_entries', 'pump_serial_number') + return Rule::unique('pump_testing_results', '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')) @@ -765,7 +765,7 @@ class PumpTestingResultResource extends Resource ]) ->headerActions([ ExportAction::make() - ->label('Export Pump Testing Entries') + ->label('Export Pump Testing Results') ->color('warning') ->exporter(PumpTestingResultExporter::class) ->visible(function () { -- 2.49.1 From 02a435e93dafe876252a43df8ea029e6e53fdcb4 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Fri, 28 Aug 2026 14:18:54 +0530 Subject: [PATCH 7/7] Updated pump testing result post api function success and failure response description --- app/Http/Controllers/TestingPanelController.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/TestingPanelController.php b/app/Http/Controllers/TestingPanelController.php index 4e59f66..2f5bfd8 100644 --- a/app/Http/Controllers/TestingPanelController.php +++ b/app/Http/Controllers/TestingPanelController.php @@ -2333,7 +2333,7 @@ class TestingPanelController extends Controller if ($updatedRows == 0) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Failed to update exist pump testing entries for the pump serial number '{$pumpSerial}'.", + 'status_description' => "Failed to update exist pump testing results for the pump serial number '{$pumpSerial}'.", ], 404); } else { for ($i = $cnt; $i <= 10; $i++) { @@ -2342,7 +2342,7 @@ class TestingPanelController extends Controller return response()->json([ 'status_code' => 'SUCCESS', - 'status_description' => "Successfully updated exist pump testing entries for the pump serial number '{$pumpSerial}'.", + 'status_description' => "Successfully updated exist pump testing results for the pump serial number '{$pumpSerial}'.", ], 200); } } else { @@ -2396,12 +2396,12 @@ class TestingPanelController extends Controller if ($createdRows == 0) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Failed to create new pump testing entries for the pump serial number '{$pumpSerial}'.", + 'status_description' => "Failed to create new pump testing results for the pump serial number '{$pumpSerial}'.", ], 404); } else { return response()->json([ 'status_code' => 'SUCCESS', - 'status_description' => "Successfully created new pump testing entries for the pump serial number '{$pumpSerial}'.", + 'status_description' => "Successfully created new pump testing results for the pump serial number '{$pumpSerial}'.", ], 200); } } -- 2.49.1