Import Fun Completed and Sticker Master

This commit is contained in:
dhanabalan
2025-03-28 16:52:40 +05:30
parent ef4504155a
commit e46f290fd1
47 changed files with 1317 additions and 335 deletions

View File

@@ -2,12 +2,15 @@
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;
@@ -26,23 +29,53 @@ class ShiftResource extends Resource
->schema([
Forms\Components\Select::make('plant_id')
->relationship('plant', 'name')
->required(),
->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(),
->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(),
// ->native(false),
->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(1),
->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
@@ -90,6 +123,10 @@ class ShiftResource extends Resource
Tables\Actions\ForceDeleteBulkAction::make(),
Tables\Actions\RestoreBulkAction::make(),
]),
])
->headerActions([
ImportAction::make()
->importer(ShiftImporter::class),
]);
}
@@ -117,4 +154,36 @@ class ShiftResource extends Resource
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;
}
}
}