Added view rights against plant on view report and Updated alignments and filter query logic on resource
This commit is contained in:
@@ -6,8 +6,6 @@ use AlperenErsoy\FilamentExport\Actions\FilamentExportBulkAction as ActionsFilam
|
||||
use App\Filament\Exports\ProductionQuantityExporter;
|
||||
use App\Filament\Imports\ProductionQuantityImporter;
|
||||
use App\Filament\Resources\ProductionQuantityResource\Pages;
|
||||
use App\Filament\Resources\ProductionQuantityResource\RelationManagers;
|
||||
use App\Forms\Components\PlantSelect;
|
||||
use App\Models\Block;
|
||||
use App\Models\Item;
|
||||
use App\Models\Line;
|
||||
@@ -18,29 +16,25 @@ use Carbon\Carbon;
|
||||
use Filament\Facades\Filament;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Components\DateTimePicker;
|
||||
use Filament\Forms\Components\Hidden;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Forms\Get;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Actions\ImportAction;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||
use Filament\Forms\Components\Section;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Concerns\InteractsWithForms;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Forms\Get;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Actions\ExportAction;
|
||||
use Livewire\Livewire;
|
||||
// use Filament\Forms\Components\View;
|
||||
use Filament\Tables\Actions\FilamentExportBulkAction;
|
||||
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 Filament\Forms\Components\View;
|
||||
use Livewire\Livewire;
|
||||
|
||||
class ProductionQuantityResource extends Resource
|
||||
{
|
||||
|
||||
protected static ?string $model = ProductionQuantity::class;
|
||||
|
||||
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
|
||||
@@ -52,7 +46,6 @@ class ProductionQuantityResource extends Resource
|
||||
|
||||
protected static ?int $navigationSort = 1;
|
||||
|
||||
|
||||
// public $plant_id;
|
||||
public static function form(Form $form): Form
|
||||
{
|
||||
@@ -69,6 +62,7 @@ class ProductionQuantityResource extends Resource
|
||||
->columnSpan(2) // 1
|
||||
->options(function (callable $get) {
|
||||
$userHas = Filament::auth()->user()->plant_id;
|
||||
|
||||
return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray();
|
||||
})
|
||||
// ->default(fn () => optional(ProductionQuantity::latest()->first())->plant_id)
|
||||
@@ -81,13 +75,11 @@ class ProductionQuantityResource extends Resource
|
||||
->afterStateUpdated(function ($state, $set, callable $get, $livewire) {
|
||||
$plantId = $get('plant_id');
|
||||
$set('block_name', null);
|
||||
if (!$plantId)
|
||||
{
|
||||
if (! $plantId) {
|
||||
$set('pqPlantError', 'Please select a plant first.');
|
||||
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$set('validationError', null);
|
||||
$set('pqPlantError', null);
|
||||
}
|
||||
@@ -138,15 +130,14 @@ class ProductionQuantityResource extends Resource
|
||||
->reactive()
|
||||
->default(function () {
|
||||
$latestShiftId = optional(ProductionQuantity::latest()->first())->shift_id;
|
||||
|
||||
return optional(Shift::where('id', $latestShiftId)->first())->block_id;
|
||||
})
|
||||
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
||||
if($get('id'))
|
||||
{
|
||||
if ($get('id')) {
|
||||
$getShift = ProductionQuantity::where('id', $get('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('pqBlockError', null);
|
||||
}
|
||||
@@ -160,10 +151,9 @@ class ProductionQuantityResource extends Resource
|
||||
|
||||
if (! $blockId) {
|
||||
$set('pqBlockError', 'Please select a block first.');
|
||||
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$set('validationError', null);
|
||||
$set('pqBlockError', null);
|
||||
}
|
||||
@@ -194,11 +184,9 @@ class ProductionQuantityResource extends Resource
|
||||
})
|
||||
// ->afterStateUpdated(fn ($set) => $set('line_id', null))
|
||||
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
||||
if($get('id'))
|
||||
{
|
||||
if ($get('id')) {
|
||||
$getShift = ProductionQuantity::where('id', $get('id'))->first();
|
||||
if($getShift->shift_id)
|
||||
{
|
||||
if ($getShift->shift_id) {
|
||||
$set('shift_id', $getShift->shift_id);
|
||||
$set('pqShiftError', null);
|
||||
}
|
||||
@@ -209,10 +197,9 @@ class ProductionQuantityResource extends Resource
|
||||
|
||||
if (! $curShiftId) {
|
||||
$set('pqShiftError', 'Please select a shift first.');
|
||||
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$set('validationError', null);
|
||||
$set('pqShiftError', null);
|
||||
}
|
||||
@@ -246,11 +233,9 @@ class ProductionQuantityResource extends Resource
|
||||
return optional(ProductionQuantity::latest()->first())->line_id;
|
||||
})
|
||||
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
||||
if($get('id'))
|
||||
{
|
||||
if ($get('id')) {
|
||||
$getShift = ProductionQuantity::where('id', $get('id'))->first();
|
||||
if($getShift->line_id)
|
||||
{
|
||||
if ($getShift->line_id) {
|
||||
$set('line_id', $getShift->line_id);
|
||||
$set('pqLineError', null);
|
||||
}
|
||||
@@ -265,10 +250,9 @@ class ProductionQuantityResource extends Resource
|
||||
|
||||
if (! $lineId) {
|
||||
$set('pqLineError', 'Please select a line first.');
|
||||
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$set('validationError', null);
|
||||
$set('pqLineError', null);
|
||||
$set('item_id', null);
|
||||
@@ -296,7 +280,7 @@ class ProductionQuantityResource extends Resource
|
||||
// 'x-on:change' => "\$wire.dispatch('filtersUpdated')", // Dispatch Livewire event from Alpine.js
|
||||
// ])
|
||||
->extraAttributes([
|
||||
'x-on:change' => "\$wire.dispatch('filtersUpdated', { lineId: \$event.target.value })"
|
||||
'x-on:change' => "\$wire.dispatch('filtersUpdated', { lineId: \$event.target.value })",
|
||||
])
|
||||
->extraAttributes(fn ($get) => [
|
||||
'class' => $get('pqLineError') ? 'border-red-500' : '',
|
||||
@@ -328,21 +312,20 @@ class ProductionQuantityResource extends Resource
|
||||
// $latestProductionOrder = ProductionQuantity::latest()->first()->production_order;
|
||||
// return $latestProductionOrder ?? null;
|
||||
$latestProduction = ProductionQuantity::latest()->first();
|
||||
|
||||
return $latestProduction ? $latestProduction->production_order : null;
|
||||
})
|
||||
->afterStateUpdated(function ($state, callable $get, callable $set): void {
|
||||
if(!is_numeric($get('production_order')) || !preg_match('/^[1-9][0-9]{6,13}$/', $get('production_order')))
|
||||
{
|
||||
if (! is_numeric($get('production_order')) || ! preg_match('/^[1-9][0-9]{6,13}$/', $get('production_order'))) {
|
||||
$set('item_code', null);
|
||||
$set('item_id', null);
|
||||
$set('serial_number', null);
|
||||
$set('production_order', null);
|
||||
$set('prodOrdError', "Must be a numeric value with 7 to 14 digits.");
|
||||
$set('prodOrdError', 'Must be a numeric value with 7 to 14 digits.');
|
||||
$set('validationError', null);
|
||||
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$set('item_code', null);
|
||||
$set('item_id', null);
|
||||
// $set('item_description', null);
|
||||
@@ -350,6 +333,7 @@ class ProductionQuantityResource extends Resource
|
||||
$set('production_order', $state);
|
||||
$set('prodOrdError', null);
|
||||
$set('validationError', null);
|
||||
|
||||
return;
|
||||
// if (empty($state)) {
|
||||
// }
|
||||
@@ -971,6 +955,7 @@ class ProductionQuantityResource 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('production_order')
|
||||
@@ -1049,6 +1034,7 @@ class ProductionQuantityResource 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()
|
||||
@@ -1071,6 +1057,7 @@ class ProductionQuantityResource extends Resource
|
||||
if (! $plantId) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return Line::where('plant_id', $plantId)
|
||||
->pluck('name', 'id');
|
||||
})
|
||||
@@ -1089,6 +1076,7 @@ class ProductionQuantityResource extends Resource
|
||||
if (! $plantId) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return Block::where('plant_id', $get('Plant'))->pluck('name', 'id');
|
||||
})
|
||||
->reactive()
|
||||
@@ -1133,10 +1121,9 @@ class ProductionQuantityResource extends Resource
|
||||
$plantId = $get('Plant');
|
||||
|
||||
if (! $plantId) {
|
||||
return Item::distinct()->whereHas('productionQuantities')->pluck('code', 'id');
|
||||
}
|
||||
else {
|
||||
return Item::where('plant_id', $plantId)->whereHas('productionQuantities')->distinct()->pluck('code', 'id');
|
||||
return Item::whereHas('productionQuantities')->distinct()->pluck('code', 'id');
|
||||
} else {
|
||||
return Item::whereHas('productionQuantities')->where('plant_id', $plantId)->distinct()->pluck('code', 'id');
|
||||
}
|
||||
// return Item::whereHas('stickerMasters', function ($query) use ($pId) {
|
||||
// if ($pId) {
|
||||
@@ -1156,8 +1143,7 @@ class ProductionQuantityResource extends Resource
|
||||
|
||||
if (! $plantId) {
|
||||
return ProductionQuantity::whereNotNull('sap_msg_status')->select('sap_msg_status')->distinct()->pluck('sap_msg_status', 'sap_msg_status');
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return ProductionQuantity::where('plant_id', $plantId)->whereNotNull('sap_msg_status')->select('sap_msg_status')->distinct()->pluck('sap_msg_status', 'sap_msg_status');
|
||||
}
|
||||
})
|
||||
@@ -1171,24 +1157,15 @@ class ProductionQuantityResource extends Resource
|
||||
$plantId = $get('Plant');
|
||||
$lineId = $get('Line');
|
||||
$shiftId = $get('Shift');
|
||||
if (!$plantId && !$lineId && !$shiftId)
|
||||
{
|
||||
if (! $plantId && ! $lineId && ! $shiftId) {
|
||||
return ProductionQuantity::whereNotNull('operator_id')->select('operator_id')->distinct()->pluck('operator_id', 'operator_id');
|
||||
}
|
||||
else if ($plantId && !$lineId && !$shiftId)
|
||||
{
|
||||
} elseif ($plantId && ! $lineId && ! $shiftId) {
|
||||
return ProductionQuantity::where('plant_id', $plantId)->whereNotNull('operator_id')->select('operator_id')->distinct()->pluck('operator_id', 'operator_id');
|
||||
}
|
||||
else if ($plantId && $lineId && !$shiftId)
|
||||
{
|
||||
} elseif ($plantId && $lineId && ! $shiftId) {
|
||||
return ProductionQuantity::where('plant_id', $plantId)->where('line_id', $lineId)->whereNotNull('operator_id')->select('operator_id')->distinct()->pluck('operator_id', 'operator_id');
|
||||
}
|
||||
else if ($plantId && !$lineId && $shiftId)
|
||||
{
|
||||
} elseif ($plantId && ! $lineId && $shiftId) {
|
||||
return ProductionQuantity::where('plant_id', $plantId)->where('shift_id', $shiftId)->whereNotNull('operator_id')->select('operator_id')->distinct()->pluck('operator_id', 'operator_id');
|
||||
}
|
||||
else// if ($plantId && $lineId && $shiftId)
|
||||
{
|
||||
} else { // if ($plantId && $lineId && $shiftId)
|
||||
return ProductionQuantity::where('plant_id', $plantId)->where('line_id', $lineId)->where('shift_id', $shiftId)->whereNotNull('operator_id')->select('operator_id')->distinct()->pluck('operator_id', 'operator_id');
|
||||
}
|
||||
})
|
||||
@@ -1212,16 +1189,22 @@ class ProductionQuantityResource 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 (! empty($data['production_order'])) {
|
||||
@@ -1244,12 +1227,12 @@ class ProductionQuantityResource extends Resource
|
||||
$query->where('operator_id', $data['operator_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;
|
||||
@@ -1259,6 +1242,12 @@ class ProductionQuantityResource 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'])) {
|
||||
@@ -1298,7 +1287,7 @@ class ProductionQuantityResource extends Resource
|
||||
}
|
||||
|
||||
return $indicators;
|
||||
})
|
||||
}),
|
||||
])
|
||||
->filtersFormMaxHeight('280px')
|
||||
->actions([
|
||||
@@ -1370,7 +1359,6 @@ class ProductionQuantityResource extends Resource
|
||||
return 'Production Reports';
|
||||
}
|
||||
|
||||
|
||||
// public function triggerChartUpdate(): void
|
||||
// {
|
||||
// if (session()->has('select_plant') && session()->has('select_line')) {
|
||||
|
||||
Reference in New Issue
Block a user