Added view rights against plant on view report and Updated alignments and filter query logic on resource
This commit is contained in:
@@ -6,30 +6,28 @@ use AlperenErsoy\FilamentExport\Actions\FilamentExportBulkAction;
|
||||
use App\Filament\Exports\ProductionLineStopExporter;
|
||||
use App\Filament\Imports\ProductionLineStopImporter;
|
||||
use App\Filament\Resources\ProductionLineStopResource\Pages;
|
||||
use App\Filament\Resources\ProductionLineStopResource\RelationManagers;
|
||||
use App\Models\Block;
|
||||
use App\Models\Line;
|
||||
use App\Models\LineStop;
|
||||
use App\Models\Plant;
|
||||
use App\Models\ProductionLineStop;
|
||||
use App\Models\Shift;
|
||||
use Carbon\Carbon;
|
||||
use Filament\Facades\Filament;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Components\DateTimePicker;
|
||||
use Filament\Forms\Components\Section;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Forms\Get;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Actions\ExportAction;
|
||||
use Filament\Tables\Actions\ImportAction;
|
||||
use Filament\Tables\Filters\Filter;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||
use Carbon\Carbon;
|
||||
use Filament\Facades\Filament;
|
||||
use Filament\Forms\Components\DateTimePicker;
|
||||
use Filament\Forms\Components\Section;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Tables\Actions\ExportAction;
|
||||
use Filament\Tables\Filters\Filter;
|
||||
|
||||
class ProductionLineStopResource extends Resource
|
||||
{
|
||||
@@ -56,6 +54,7 @@ class ProductionLineStopResource extends Resource
|
||||
->reactive()
|
||||
->options(function (callable $get) {
|
||||
$userHas = Filament::auth()->user()->plant_id;
|
||||
|
||||
return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray();
|
||||
})
|
||||
->default(function () {
|
||||
@@ -68,10 +67,9 @@ class ProductionLineStopResource extends Resource
|
||||
$set('block_name', null);
|
||||
if (! $plantId) {
|
||||
$set('plsPlantError', 'Please select a plant first.');
|
||||
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$set('plsPlantError', null);
|
||||
}
|
||||
})
|
||||
@@ -97,17 +95,16 @@ class ProductionLineStopResource extends Resource
|
||||
->reactive()
|
||||
->default(function () {
|
||||
$latestShiftId = optional(ProductionLineStop::latest()->first())->shift_id;
|
||||
|
||||
return optional(Shift::where('id', $latestShiftId)->first())->block_id;
|
||||
})
|
||||
// ->afterStateUpdated(fn ($set) => $set('shift_id', null))
|
||||
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
||||
if($get('id'))
|
||||
{
|
||||
if ($get('id')) {
|
||||
$getShift = ProductionLineStop::where('id', $get('id'))->first();
|
||||
// $getBlock = \App\Models\Shift::where('id', $getShift->shift_id)->first();
|
||||
$getBlock = Shift::where('id', $getShift->shift_id)->first();
|
||||
if($getBlock->block_id)
|
||||
{
|
||||
if ($getBlock->block_id) {
|
||||
$set('block_name', $getBlock->block_id);
|
||||
$set('plsBlockError', null);
|
||||
}
|
||||
@@ -118,10 +115,9 @@ class ProductionLineStopResource extends Resource
|
||||
|
||||
if (! $blockId) {
|
||||
$set('plsBlockError', 'Please select a block first.');
|
||||
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$set('plsBlockError', null);
|
||||
}
|
||||
})
|
||||
@@ -150,11 +146,9 @@ class ProductionLineStopResource extends Resource
|
||||
})
|
||||
// ->afterStateUpdated(fn ($set) => $set('line_id', null))
|
||||
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
||||
if($get('id'))
|
||||
{
|
||||
if ($get('id')) {
|
||||
$getShift = ProductionLineStop::where('id', $get('id'))->first();
|
||||
if($getShift->shift_id)
|
||||
{
|
||||
if ($getShift->shift_id) {
|
||||
$set('shift_id', $getShift->shift_id);
|
||||
$set('plsShiftError', null);
|
||||
}
|
||||
@@ -165,10 +159,9 @@ class ProductionLineStopResource extends Resource
|
||||
|
||||
if (! $shiftId) {
|
||||
$set('plsShiftError', 'Please select a shift first.');
|
||||
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$set('plsShiftError', null);
|
||||
}
|
||||
})
|
||||
@@ -196,11 +189,9 @@ class ProductionLineStopResource extends Resource
|
||||
return optional(ProductionLineStop::latest()->first())->line_id;
|
||||
})
|
||||
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
||||
if($get('id'))
|
||||
{
|
||||
if ($get('id')) {
|
||||
$getShift = ProductionLineStop::where('id', $get('id'))->first();
|
||||
if($getShift->line_id)
|
||||
{
|
||||
if ($getShift->line_id) {
|
||||
$set('line_id', $getShift->line_id);
|
||||
$set('plsLineError', null);
|
||||
}
|
||||
@@ -212,10 +203,9 @@ class ProductionLineStopResource extends Resource
|
||||
|
||||
if (! $lineId) {
|
||||
$set('plsLineError', 'Please select a line first.');
|
||||
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$set('plsLineError', null);
|
||||
}
|
||||
})
|
||||
@@ -253,6 +243,7 @@ class ProductionLineStopResource extends Resource
|
||||
// Ensure `linestop_id` is not cleared
|
||||
if (! $lineStopId) {
|
||||
$set('lineStop_reason', null);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -275,16 +266,14 @@ class ProductionLineStopResource extends Resource
|
||||
->before('to_datetime')
|
||||
->reactive()
|
||||
// ->closeOnDateSelection()
|
||||
->afterStateUpdated(fn ($state, callable $set, callable $get) =>
|
||||
self::updateStopDuration($get, $set)
|
||||
->afterStateUpdated(fn ($state, callable $set, callable $get) => self::updateStopDuration($get, $set)
|
||||
),
|
||||
Forms\Components\DateTimePicker::make('to_datetime')
|
||||
->label('To DateTime')
|
||||
->required()
|
||||
->after('from_datetime')
|
||||
->reactive()
|
||||
->afterStateUpdated(fn ($state, callable $set, callable $get) =>
|
||||
self::updateStopDuration($get, $set) //self means it calling the function within the class
|
||||
->afterStateUpdated(fn ($state, callable $set, callable $get) => self::updateStopDuration($get, $set) // self means it calling the function within the class
|
||||
)
|
||||
->extraAttributes(fn ($get) => [
|
||||
'class' => $get('plsToDateError') ? 'border-red-500' : '',
|
||||
@@ -328,14 +317,11 @@ class ProductionLineStopResource extends Resource
|
||||
$diffInMinutes = $fromTime->diffInMinutes($toTime);
|
||||
// $set('stop_hour', floor($diffInMinutes / 60));
|
||||
// $set('stop_min', $diffInMinutes % 60);
|
||||
if((floor($diffInMinutes / 60) === 0.0) && ($diffInMinutes % 60 === 0))
|
||||
{
|
||||
if ((floor($diffInMinutes / 60) === 0.0) && ($diffInMinutes % 60 === 0)) {
|
||||
$set('stop_hour', null);
|
||||
$set('stop_min', null);
|
||||
$set('plsToDateError', 'Time difference must be atlease a minute.');
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$set('stop_hour', floor($diffInMinutes / 60));
|
||||
$set('stop_min', $diffInMinutes % 60);
|
||||
$set('plsToDateError', null);
|
||||
@@ -363,6 +349,7 @@ class ProductionLineStopResource extends Resource
|
||||
$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('linestop.code')
|
||||
@@ -433,7 +420,6 @@ class ProductionLineStopResource extends Resource
|
||||
Filter::make('advanced_filters')
|
||||
->label('Advanced Filters')
|
||||
->form([
|
||||
|
||||
// plant
|
||||
Select::make('Plant')
|
||||
->label('Select Plant')
|
||||
@@ -443,6 +429,7 @@ class ProductionLineStopResource extends Resource
|
||||
// })
|
||||
->options(function (callable $get) {
|
||||
$userHas = Filament::auth()->user()->plant_id;
|
||||
|
||||
return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray();
|
||||
})
|
||||
->reactive()
|
||||
@@ -463,6 +450,7 @@ class ProductionLineStopResource extends Resource
|
||||
if (! $plantId) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return Line::where('plant_id', $plantId)
|
||||
->pluck('name', 'id');
|
||||
})
|
||||
@@ -478,6 +466,7 @@ class ProductionLineStopResource extends Resource
|
||||
if (! $plantId) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return Block::where('plant_id', $get('Plant'))->pluck('name', 'id');
|
||||
})
|
||||
->reactive()
|
||||
@@ -509,6 +498,7 @@ class ProductionLineStopResource extends Resource
|
||||
// ->options(fn () => LineStop::orderBy('code')->whereHas('productionLineStops')->pluck('code', 'id'))
|
||||
->options(function (callable $get) {
|
||||
$pId = $get('Plant');
|
||||
|
||||
return LineStop::orderBy('code')->whereHas('productionLineStops', function ($query) use ($pId) {
|
||||
if ($pId) {
|
||||
$query->where('plant_id', $pId);
|
||||
@@ -535,19 +525,25 @@ class ProductionLineStopResource extends Resource
|
||||
return $query->whereRaw('1 = 0');
|
||||
}
|
||||
|
||||
if ($plant = $data['Plant'] ?? null) {
|
||||
$query->where('plant_id', $plant);
|
||||
if (! empty($data['Plant'])) {// if ($plant = $data['Plant'] ?? null) {
|
||||
$query->where('plant_id', $data['Plant']);
|
||||
} else {
|
||||
$userHas = Filament::auth()->user()->plant_id;
|
||||
|
||||
if ($userHas && strlen($userHas) > 0) {
|
||||
return $query->whereRaw('1 = 0');
|
||||
}
|
||||
}
|
||||
|
||||
if ($shift = $data['Shift'] ?? null) {
|
||||
$query->where('shift_id', $shift);
|
||||
if (! empty($data['Shift'])) {// if ($shift = $data['Shift'] ?? null) {
|
||||
$query->where('shift_id', $data['Shift']);
|
||||
}
|
||||
|
||||
if ($line = $data['Line'] ?? null) {
|
||||
$query->where('line_id', $line);
|
||||
if (! empty($data['Line'])) {// if ($line = $data['Line'] ?? null) {
|
||||
$query->where('line_id', $data['Line']);
|
||||
}
|
||||
|
||||
if ($code = $data['line_stop_id'] ?? null) {
|
||||
if (! empty($data['line_stop_id'])) { // if ($code = $data['line_stop_id'] ?? null) {
|
||||
// Find the linestop_id by code entered
|
||||
// $lineStop = \App\Models\LineStop::where('code', 'like', "%{$code}%")->first();
|
||||
|
||||
@@ -558,15 +554,15 @@ class ProductionLineStopResource extends Resource
|
||||
// // If no match found, you can either handle it as an error or return no results
|
||||
// $query->where('linestop_id', null); // This will return no results if no match
|
||||
// }
|
||||
$query->where('linestop_id', $code);
|
||||
$query->where('linestop_id', $data['line_stop_id']);
|
||||
}
|
||||
|
||||
if ($from = $data['created_from'] ?? null) {
|
||||
$query->where('created_at', '>=', $from);
|
||||
if (! empty($data['created_from'])) {// if ($from = $data['created_from'] ?? null) {
|
||||
$query->where('created_at', '>=', $data['created_from']);
|
||||
}
|
||||
|
||||
if ($to = $data['created_to'] ?? null) {
|
||||
$query->where('created_at', '<=', $to);
|
||||
if (! empty($data['created_to'])) {// if ($to = $data['created_to'] ?? null) {
|
||||
$query->where('created_at', '<=', $data['created_to']);
|
||||
}
|
||||
// return $query;
|
||||
})
|
||||
@@ -575,6 +571,12 @@ class ProductionLineStopResource extends Resource
|
||||
|
||||
if (! empty($data['Plant'])) {
|
||||
$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['Shift'])) {
|
||||
@@ -599,7 +601,7 @@ class ProductionLineStopResource extends Resource
|
||||
}
|
||||
|
||||
return $indicators;
|
||||
})
|
||||
}),
|
||||
])
|
||||
->filtersFormMaxHeight('280px')
|
||||
->actions([
|
||||
@@ -611,7 +613,7 @@ class ProductionLineStopResource extends Resource
|
||||
Tables\Actions\DeleteBulkAction::make(),
|
||||
Tables\Actions\ForceDeleteBulkAction::make(),
|
||||
Tables\Actions\RestoreBulkAction::make(),
|
||||
FilamentExportBulkAction::make('export')
|
||||
FilamentExportBulkAction::make('export'),
|
||||
]),
|
||||
])
|
||||
->headerActions([
|
||||
|
||||
Reference in New Issue
Block a user