Files
pds/app/Filament/Resources/ShiftResource.php
2025-03-28 16:52:40 +05:30

190 lines
6.6 KiB
PHP

<?php
namespace App\Filament\Resources;
use App\Filament\Imports\ShiftImporter;
use App\Filament\Resources\ShiftResource\Pages;
use App\Models\Shift;
use Carbon\Carbon;
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;
class ShiftResource extends Resource
{
protected static ?string $model = Shift::class;
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
protected static ?string $navigationGroup = 'Master Entries';
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('plant_id')
->relationship('plant', 'name')
->required()
->nullable()
->reactive()
->afterStateUpdated(fn ($set) => $set('block_id', null)), // Reset block_id when plant changes
Forms\Components\Select::make('block_id')
->relationship('block', 'name')
->required()
->nullable()
// ->options(fn (callable $get) =>
// \App\Models\Block::where('plant_id', $get('plant_id'))
// ->pluck('name', 'id')
// ->toArray() // Convert collection to array
// )
->options(function (callable $get) {
if (!$get('plant_id')) {
return [];
}
return \App\Models\Block::where('plant_id', $get('plant_id'))
->pluck('name', 'id')
->toArray();
})
->reactive(),
Forms\Components\TextInput::make('name')
->required(),
Forms\Components\TimePicker::make('start_time')
->required()
->live()
->afterStateUpdated(fn (callable $set, callable $get, $state) =>
$set('end_time', self::calculateEndTime($state, $get('duration')))
),
Forms\Components\TextInput::make('duration')
->required()
->inputMode('decimal')
->minValue(0.01) // Minimum valid duration
->lazy()
->afterStateUpdated(fn (callable $set, callable $get, $state) =>
$set('end_time', self::calculateEndTime($get('start_time'), $state))
),
Forms\Components\TimePicker::make('end_time')
->required(),
// ->native(false),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('id')
->label('ID')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('name')
->sortable(),
Tables\Columns\TextColumn::make('start_time')
->sortable(),
Tables\Columns\TextColumn::make('duration')
->sortable(),
Tables\Columns\TextColumn::make('end_time'),
Tables\Columns\TextColumn::make('block.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(ShiftImporter::class),
]);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListShifts::route('/'),
'create' => Pages\CreateShift::route('/create'),
'view' => Pages\ViewShift::route('/{record}'),
'edit' => Pages\EditShift::route('/{record}/edit'),
];
}
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}
protected static function calculateEndTime(?string $startTime, ?string $duration): ?string
{
if (!$startTime || !$duration) {
return null;
}
try {
// Convert start_time to Carbon instance
$startTimeCarbon = Carbon::createFromFormat('H:i:s', $startTime);
// Ensure duration is in a valid numeric format
$duration = str_replace(',', '.', $duration); // Handle decimal formats
if (!is_numeric($duration)) {
return null; // Invalid duration format
}
// Extract hours and minutes correctly
[$hours, $decimalMinutes] = explode('.', $duration) + [0, 0]; // Ensure two parts
$hours = (int) $hours; // Convert to integer hours
$minutes = (int) $decimalMinutes; // Directly use decimal part as minutes
// Calculate end time
$endTimeCarbon = $startTimeCarbon->addHours($hours)->addMinutes($minutes);
return $endTimeCarbon->format('H:i:s'); // Return formatted end time
} catch (\Exception $e) {
return null;
}
}
}