Added view rights against plant
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 10s

This commit is contained in:
dhanabalan
2026-01-13 16:00:31 +05:30
parent 47db04a36c
commit 7a988b7280

View File

@@ -5,7 +5,6 @@ namespace App\Filament\Resources;
use App\Filament\Exports\GuardPatrolEntryExporter; use App\Filament\Exports\GuardPatrolEntryExporter;
use App\Filament\Imports\GuardPatrolEntryImporter; use App\Filament\Imports\GuardPatrolEntryImporter;
use App\Filament\Resources\GuardPatrolEntryResource\Pages; use App\Filament\Resources\GuardPatrolEntryResource\Pages;
use App\Filament\Resources\GuardPatrolEntryResource\RelationManagers;
use App\Models\CheckPointName; use App\Models\CheckPointName;
use App\Models\Configuration; use App\Models\Configuration;
use App\Models\GuardName; use App\Models\GuardName;
@@ -17,7 +16,6 @@ use Filament\Forms;
use Filament\Forms\Components\DateTimePicker; use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\FileUpload; use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Select; use Filament\Forms\Components\Select;
use Filament\Forms\Components\Tabs\Tab;
use Filament\Forms\Components\TextInput; use Filament\Forms\Components\TextInput;
use Filament\Forms\Form; use Filament\Forms\Form;
use Filament\Forms\Get; use Filament\Forms\Get;
@@ -54,6 +52,7 @@ class GuardPatrolEntryResource extends Resource
->reactive() ->reactive()
->options(function (callable $get) { ->options(function (callable $get) {
$userHas = Filament::auth()->user()->plant_id; $userHas = Filament::auth()->user()->plant_id;
return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray(); return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray();
}) })
->default(function () { ->default(function () {
@@ -65,10 +64,9 @@ class GuardPatrolEntryResource extends Resource
if (! $plantId) { if (! $plantId) {
// $set('gPePlantError', 'Please select a plant first.'); // $set('gPePlantError', 'Please select a plant first.');
$set('gPePlantError', 'Please select a plant first.'); $set('gPePlantError', 'Please select a plant first.');
return; return;
} } else {
else
{
$set('patrol_time', now()->format('Y-m-d H:i:s')); $set('patrol_time', now()->format('Y-m-d H:i:s'));
$set('updated_by', Filament::auth()->user()?->name); $set('updated_by', Filament::auth()->user()?->name);
$set('gPePlantError', null); $set('gPePlantError', null);
@@ -102,10 +100,9 @@ class GuardPatrolEntryResource extends Resource
$guardName = $get('guard_name_id'); $guardName = $get('guard_name_id');
if (! $guardName) { if (! $guardName) {
$set('gPeGuardNameError', 'Please select a guard name first.'); $set('gPeGuardNameError', 'Please select a guard name first.');
return; return;
} } else {
else
{
$set('patrol_time', now()->format('Y-m-d H:i:s')); $set('patrol_time', now()->format('Y-m-d H:i:s'));
$set('updated_by', Filament::auth()->user()?->name); $set('updated_by', Filament::auth()->user()?->name);
$set('gPeGuardNameError', null); $set('gPeGuardNameError', null);
@@ -150,10 +147,9 @@ class GuardPatrolEntryResource extends Resource
if (! $checkPointName) { if (! $checkPointName) {
$set('check_point_name_id', null); $set('check_point_name_id', null);
$set('gPeCheckPointNameError', 'Please select a check point name first.'); $set('gPeCheckPointNameError', 'Please select a check point name first.');
return; return;
} } else {
else
{
$set('patrol_time', now()->format('Y-m-d H:i:s')); $set('patrol_time', now()->format('Y-m-d H:i:s'));
$set('updated_by', Filament::auth()->user()?->name); $set('updated_by', Filament::auth()->user()?->name);
$set('gPeCheckPointNameError', null); $set('gPeCheckPointNameError', null);
@@ -176,8 +172,7 @@ class GuardPatrolEntryResource extends Resource
->hidden(fn (Get $get) => ! $get('id')) ->hidden(fn (Get $get) => ! $get('id'))
->reactive() ->reactive()
->afterStateUpdated(function ($state, callable $set, callable $get) { ->afterStateUpdated(function ($state, callable $set, callable $get) {
if(!$get('id')) if (! $get('id')) {
{
$set('patrol_time', now()->format('Y-m-d H:i:s')); $set('patrol_time', now()->format('Y-m-d H:i:s'));
} }
$set('updated_by', Filament::auth()->user()?->name); $set('updated_by', Filament::auth()->user()?->name);
@@ -225,6 +220,7 @@ class GuardPatrolEntryResource extends Resource
$paginator = $livewire->getTableRecords(); $paginator = $livewire->getTableRecords();
$perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10; $perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10;
$currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1; $currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1;
return ($currentPage - 1) * $perPage + $rowLoop->iteration; return ($currentPage - 1) * $perPage + $rowLoop->iteration;
}), }),
// Tables\Columns\TextColumn::make('id') // Tables\Columns\TextColumn::make('id')
@@ -288,6 +284,7 @@ class GuardPatrolEntryResource extends Resource
// }) // })
->options(function (callable $get) { ->options(function (callable $get) {
$userHas = Filament::auth()->user()->plant_id; $userHas = Filament::auth()->user()->plant_id;
return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray(); return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray();
}) })
->reactive(), ->reactive(),
@@ -350,6 +347,12 @@ class GuardPatrolEntryResource extends Resource
if (! empty($data['Plant'])) { if (! empty($data['Plant'])) {
$query->where('plant_id', $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['Guard Name'])) { if (! empty($data['Guard Name'])) {
@@ -377,6 +380,12 @@ class GuardPatrolEntryResource extends Resource
if (! empty($data['Plant'])) { if (! empty($data['Plant'])) {
$indicators[] = 'Plant: '.Plant::where('id', $data['Plant'])->value('name'); $indicators[] = 'Plant: '.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['Guard Name'])) { if (! empty($data['Guard Name'])) {
@@ -400,7 +409,7 @@ class GuardPatrolEntryResource extends Resource
} }
return $indicators; return $indicators;
}) }),
]) ])
->filtersFormMaxHeight('280px') ->filtersFormMaxHeight('280px')
->actions([ ->actions([
@@ -422,6 +431,7 @@ class GuardPatrolEntryResource extends Resource
// ->options(Plant::pluck('name', 'id')->toArray()) // ->options(Plant::pluck('name', 'id')->toArray())
->options(function (callable $get) { ->options(function (callable $get) {
$userHas = Filament::auth()->user()->plant_id; $userHas = Filament::auth()->user()->plant_id;
return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray(); return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray();
}) })
->label('Select Plant') ->label('Select Plant')
@@ -435,10 +445,9 @@ class GuardPatrolEntryResource extends Resource
$set('guard_patrol_entry', null); $set('guard_patrol_entry', null);
if (! $plantId) { if (! $plantId) {
$set('gPeSelectPlantError', 'Please select a plant first.'); $set('gPeSelectPlantError', 'Please select a plant first.');
return; return;
} } else {
else
{
$set('gPeSelectPlantError', null); $set('gPeSelectPlantError', null);
} }
}) })
@@ -482,21 +491,20 @@ class GuardPatrolEntryResource extends Resource
$folderPath = Configuration::where('c_name', 'GUARD_PATROL_ENTRY_FOLDER_PATH')->where('plant_id', $plantId)->value('c_value'); $folderPath = Configuration::where('c_name', 'GUARD_PATROL_ENTRY_FOLDER_PATH')->where('plant_id', $plantId)->value('c_value');
if(!$folderPath) if (! $folderPath) {
{
Notification::make() Notification::make()
->title('Upload Folder Path Not Found!') ->title('Upload Folder Path Not Found!')
->body('Please set the folder path in configuration for Guard Patrol Entry.') ->body('Please set the folder path in configuration for Guard Patrol Entry.')
->danger() ->danger()
->send(); ->send();
return; return;
} }
$fullFolderPath = "uploads/$folderPath"; $fullFolderPath = "uploads/$folderPath";
// Check if the folder exists, if not, create it // Check if the folder exists, if not, create it
if (!Storage::disk('local')->exists($fullFolderPath)) if (! Storage::disk('local')->exists($fullFolderPath)) {
{
Storage::disk('local')->makeDirectory($fullFolderPath); Storage::disk('local')->makeDirectory($fullFolderPath);
} }
@@ -504,12 +512,10 @@ class GuardPatrolEntryResource extends Resource
$fullPath = Storage::disk('local')->path($path); $fullPath = Storage::disk('local')->path($path);
if ($fullPath && file_exists($fullPath)) if ($fullPath && file_exists($fullPath)) {
{
$rows = Excel::toArray(null, $fullPath)[0]; $rows = Excel::toArray(null, $fullPath)[0];
if((count($rows) - 1) <= 0) if ((count($rows) - 1) <= 0) {
{
Notification::make() Notification::make()
->title('Invalid Guard Patrol Entry Found') ->title('Invalid Guard Patrol Entry Found')
->body('Uploaded excel sheet is empty or<br>contains no valid data.') ->body('Uploaded excel sheet is empty or<br>contains no valid data.')
@@ -519,6 +525,7 @@ class GuardPatrolEntryResource extends Resource
if ($disk->exists($path)) { if ($disk->exists($path)) {
$disk->delete($path); $disk->delete($path);
} }
return; return;
} }
@@ -529,9 +536,10 @@ class GuardPatrolEntryResource extends Resource
$invalidPatrolTimes = []; $invalidPatrolTimes = [];
$validRowsFound = false; $validRowsFound = false;
foreach ($rows as $index => $row) foreach ($rows as $index => $row) {
{ if ($index === 0) {
if ($index === 0) continue; // Skip header continue;
} // Skip header
$rowNumber = trim($row[0]); $rowNumber = trim($row[0]);
$guardName = trim($row[1]); $guardName = trim($row[1]);
@@ -539,21 +547,20 @@ class GuardPatrolEntryResource extends Resource
$readerCode = trim($row[3]); $readerCode = trim($row[3]);
$patrolTime = trim($row[4]); $patrolTime = trim($row[4]);
if (empty($rowNumber)) { continue; } if (empty($rowNumber)) {
continue;
}
if (empty($guardName) || empty($checkPointName) || empty($readerCode) || empty($patrolTime)) { if (empty($guardName) || empty($checkPointName) || empty($readerCode) || empty($patrolTime)) {
$invalidRows[] = $rowNumber; $invalidRows[] = $rowNumber;
continue; continue;
} } else {
else if (Str::length($guardName) < 3 || Str::length($checkPointName) < 3 || Str::length($readerCode) < 3 || Str::length($patrolTime) < 3) {
{
if(Str::length($guardName) < 3 || Str::length($checkPointName) < 3 || Str::length($readerCode) < 3 || Str::length($patrolTime) < 3)
{
$invalidGuardCheckPoints[] = $rowNumber; $invalidGuardCheckPoints[] = $rowNumber;
continue; continue;
} } else {
else
{
$isValidRow = true; $isValidRow = true;
$guardNames = GuardName::where('plant_id', $plantId)->where('name', $guardName)->first(); $guardNames = GuardName::where('plant_id', $plantId)->where('name', $guardName)->first();
if (! $guardNames) { if (! $guardNames) {
@@ -602,6 +609,7 @@ class GuardPatrolEntryResource extends Resource
if ($disk->exists($path)) { if ($disk->exists($path)) {
$disk->delete($path); $disk->delete($path);
} }
return; return;
} }
@@ -616,6 +624,7 @@ class GuardPatrolEntryResource extends Resource
if ($disk->exists($path)) { if ($disk->exists($path)) {
$disk->delete($path); $disk->delete($path);
} }
return; return;
} }
@@ -672,15 +681,17 @@ class GuardPatrolEntryResource extends Resource
if ($disk->exists($path)) { if ($disk->exists($path)) {
$disk->delete($path); $disk->delete($path);
} }
return; return;
} }
$validCnt = 0; $validCnt = 0;
$dupCnt = 0; $dupCnt = 0;
$validRowsFound = false; $validRowsFound = false;
foreach ($rows as $index => $row) foreach ($rows as $index => $row) {
{ if ($index === 0) {
if ($index === 0) continue; // Skip header continue;
} // Skip header
$rowNumber = trim($row[0]); $rowNumber = trim($row[0]);
$guardName = trim($row[1]); $guardName = trim($row[1]);
@@ -688,7 +699,9 @@ class GuardPatrolEntryResource extends Resource
$readerCode = trim($row[3]); $readerCode = trim($row[3]);
$patrolTime = trim($row[4]); $patrolTime = trim($row[4]);
if (empty($rowNumber)) { continue; } if (empty($rowNumber)) {
continue;
}
if (empty($guardName) || empty($checkPointName) || empty($readerCode) || empty($patrolTime)) { if (empty($guardName) || empty($checkPointName) || empty($readerCode) || empty($patrolTime)) {
continue; continue;
@@ -720,21 +733,20 @@ class GuardPatrolEntryResource extends Resource
if ($guardEntryFound) { if ($guardEntryFound) {
// $warnMsg[] = "Duplicate guard patrol entry found"; // $warnMsg[] = "Duplicate guard patrol entry found";
$dupCnt++; $dupCnt++;
continue; continue;
} } else {
else
{
$validCnt++; $validCnt++;
GuardPatrolEntry::updateOrCreate([ GuardPatrolEntry::updateOrCreate([
'plant_id' => $plantId, 'plant_id' => $plantId,
'guard_name_id' => $guardNames->id, 'guard_name_id' => $guardNames->id,
'check_point_name_id' => $checkPointNames->id, 'check_point_name_id' => $checkPointNames->id,
'patrol_time' => $patrolDateTime->format('Y-m-d H:i:s') 'patrol_time' => $patrolDateTime->format('Y-m-d H:i:s'),
], ],
[ [
'reader_code' => $readerCode, 'reader_code' => $readerCode,
'created_by' => $user, 'created_by' => $user,
'updated_by' => $user 'updated_by' => $user,
] ]
); );
$validRowsFound = true; $validRowsFound = true;
@@ -748,25 +760,19 @@ class GuardPatrolEntryResource extends Resource
->body("Uploaded excel sheet contains '{$dupCnt}' duplicate entries!<br>Please check the uploaded file and try again.") ->body("Uploaded excel sheet contains '{$dupCnt}' duplicate entries!<br>Please check the uploaded file and try again.")
->danger() ->danger()
->send(); ->send();
if ($disk->exists($path)) if ($disk->exists($path)) {
{
$disk->delete($path); $disk->delete($path);
} }
} } elseif ($validRowsFound && $validCnt > 0) {
else if ($validRowsFound && $validCnt > 0)
{
// session(['guard_patrol_entry_path' => $fullPath]); // session(['guard_patrol_entry_path' => $fullPath]);
Notification::make() Notification::make()
->title("Success: '{$validCnt}' guard patrol entries imported successfully.") ->title("Success: '{$validCnt}' guard patrol entries imported successfully.")
->success() ->success()
->send(); ->send();
if ($disk->exists($path)) if ($disk->exists($path)) {
{
$disk->delete($path); $disk->delete($path);
} }
} } else {
else
{
Notification::make() Notification::make()
->title('Failed: Something went wrong while uploading guard patrol entries!') ->title('Failed: Something went wrong while uploading guard patrol entries!')
->danger() ->danger()