Files
pds/app/Filament/Resources/ProductionPlanResource.php

297 lines
14 KiB
PHP

<?php
namespace App\Filament\Resources;
use App\Filament\Imports\ProductionPlanImporter;
use App\Filament\Resources\ProductionPlanResource\Pages;
use App\Filament\Resources\ProductionPlanResource\RelationManagers;
use App\Models\ProductionPlan;
use Filament\Forms;
use Filament\Forms\Form;
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 Illuminate\Support\Facades\Request;
class ProductionPlanResource extends Resource
{
protected static ?string $model = ProductionPlan::class;
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
// protected static ?string $navigationParentItem = 'Production Line Stops';
protected static ?string $navigationGroup = 'Production';
public static function form(Form $form): Form
{
return $form
->schema([
Section::make('')
->schema([
Forms\Components\Select::make('plant_id')
->relationship('plant', 'name')
->required()
// ->nullable()
->reactive()
// ->afterStateUpdated(fn ($set) => $set('block_name', null))
->afterStateUpdated(function ($state, callable $set, callable $get) {
$plantId = $get('plant_id');
$set('block_name', null);
if (!$plantId) {
$set('ppPlantError', 'Please select a plant first.');
return;
}
else
{
$set('ppPlantError', null);
}
})
->extraAttributes(fn ($get) => [
'class' => $get('ppPlantError') ? 'border-red-500' : '',
])
->hint(fn ($get) => $get('ppPlantError') ? $get('ppPlantError') : null)
->hintColor('danger'),
Forms\Components\Select::make('block_name')
->required()
// ->nullable()
->label('Block')
->options(function (callable $get) {
if (!$get('plant_id')) {
return [];
}
return \App\Models\Block::where('plant_id', $get('plant_id'))
->pluck('name', 'id')
->toArray();
})
->reactive()
//->afterStateUpdated(fn ($set) => $set('shift_id', null))
->afterStateUpdated(function ($state, callable $set, callable $get) {
$blockId = $get('block_name');
$set('shift_id', null);
if (!$blockId) {
$set('ppBlockError', 'Please select a block first.');
return;
}
else
{
$set('ppBlockError', null);
}
})
->extraAttributes(fn ($get) => [
'class' => $get('ppBlockError') ? 'border-red-500' : '',
])
->hint(fn ($get) => $get('ppBlockError') ? $get('ppBlockError') : null)
->hintColor('danger'),
Forms\Components\Select::make('shift_id')
->relationship('shift', 'name')
->required()
// ->nullable()
->options(function (callable $get) {
if (!$get('plant_id') || !$get('block_name')) {
return [];
}
return \App\Models\Shift::where('plant_id', $get('plant_id'))
->where('block_id', $get('block_name'))
->pluck('name', 'id')
->toArray();
})
->reactive()
// ->afterStateUpdated(fn ($set) => $set('line_id', null))
->afterStateUpdated(function ($state, callable $set, callable $get) {
$shiftId = $get('shift_id');
$set('line_id', null);
if (!$shiftId) {
$set('ppShiftError', 'Please select a shift first.');
return;
}
else
{
$set('ppShiftError', null);
}
})
->extraAttributes(fn ($get) => [
'class' => $get('ppShiftError') ? 'border-red-500' : '',
])
->hint(fn ($get) => $get('ppShiftError') ? $get('ppShiftError') : null)
->hintColor('danger'),
Forms\Components\Select::make('line_id')
->relationship('line', 'name')
->required()
// ->nullable()
// ->options(fn (callable $get) =>
// \App\Models\Line::where('plant_id', $get('plant_id'))
// ->pluck('name', 'id')
// ->toArray() // Convert collection to array
// )
->options(function (callable $get) {
if (!$get('plant_id') || !$get('block_name') || !$get('shift_id')) {
return [];
}
return \App\Models\Line::where('plant_id', $get('plant_id'))
->pluck('name', 'id')
->toArray();
})
->reactive()
->afterStateUpdated(function ($state, callable $set, callable $get) {
$lineId = $get('line_id');
if (!$lineId) {
$set('ppLineError', 'Please select a line first.');
return;
}
else
{
//SELECT COUNT(*) FROM plant_quantity_details WHERE DATE(created_at) = '{DateTime.Now.ToString("yyyy-MM-dd")}' AND shift_id = '{userShiftId}' AND plant_id = '{userPlantId}' AND line_id = '{userLineId}'
//Day shift 'Plant Production Quantity' already updated into database
// $currentPath = url()->current();
// $currentPath = $_SERVER['REQUEST_URI'];
// dd($currentPath);
$isUpdate = !empty($get('id'));
if (!$isUpdate)
{
$exists = ProductionPlan::where('plant_id', $get('plant_id'))
->where('shift_id', $get('shift_id'))
->where('line_id', $get('line_id'))
->whereDate('created_at', today())
->exists();
if ($exists)
{
$set('line_id', null);
$set('ppLineError', 'Production plan already updated.');
return;
}
}
$set('ppLineError', null);
}
})
->extraAttributes(fn ($get) => [
'class' => $get('ppLineError') ? 'border-red-500' : '',
])
->hint(fn ($get) => $get('ppLineError') ? $get('ppLineError') : null)
->hintColor('danger'),
Forms\Components\TextInput::make('plan_quantity')
->required()
->integer()
->label('Plan Quantity')
->placeholder('Scan the valid quantity')
->minValue(1)
->reactive()
->afterStateUpdated(function ($state, callable $set, callable $get) {
$planQuan = $get('plan_quantity');
if (!$planQuan) {
$set('ppPlanQuanError', 'Scan the valid plan quantity.');
return;
}
else
{
$set('ppPlanQuanError', null);
}
})
->extraAttributes(fn ($get) => [
'class' => $get('ppPlanQuanError') ? 'border-red-500' : '',
])
->hint(fn ($get) => $get('ppPlanQuanError') ? $get('ppPlanQuanError') : null)
->hintColor('danger'),
Forms\Components\TextInput::make('production_quantity')
->required()
->integer()
->label('Production Quantity')
->readOnly()
->default(0),
Forms\Components\TextInput::make('id')
->hidden()
->readOnly(),
])
->columns(2),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('id')
->label('ID')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('plan_quantity')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('production_quantity')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('line.name')
->sortable(),
Tables\Columns\TextColumn::make('shift.name')
->sortable(),
Tables\Columns\TextColumn::make('plant.name')
->sortable(),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('deleted_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
->filters([
Tables\Filters\TrashedFilter::make(),
])
->actions([
Tables\Actions\ViewAction::make(),
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
Tables\Actions\ForceDeleteBulkAction::make(),
Tables\Actions\RestoreBulkAction::make(),
]),
])
->headerActions([
ImportAction::make()
->importer(ProductionPlanImporter::class),
]);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListProductionPlans::route('/'),
'create' => Pages\CreateProductionPlan::route('/create'),
'view' => Pages\ViewProductionPlan::route('/{record}'),
'edit' => Pages\EditProductionPlan::route('/{record}/edit'),
];
}
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}
}