238 lines
9.9 KiB
PHP
238 lines
9.9 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources;
|
|
|
|
use App\Filament\Exports\ItemExporter;
|
|
use App\Filament\Imports\ItemImporter;
|
|
use App\Filament\Resources\ItemResource\Pages;
|
|
use App\Models\Item;
|
|
use Filament\Actions\Exports\Enums\ExportFormat;
|
|
use Filament\Forms;
|
|
use Filament\Forms\Components\TextInput;
|
|
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\Table;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
|
use Filament\Forms\Components\Section;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class ItemResource extends Resource
|
|
{
|
|
protected static ?string $model = Item::class;
|
|
|
|
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
|
|
|
|
protected static ?string $navigationGroup = 'Master Entries';
|
|
|
|
protected static ?int $navigationSort = 6;
|
|
|
|
public static function form(Form $form): Form
|
|
{
|
|
return $form
|
|
->schema([
|
|
Section::make('')
|
|
->schema([
|
|
Forms\Components\Select::make('plant_id')
|
|
->relationship('plant', 'name')
|
|
->required()
|
|
// ->preload()
|
|
// ->nullable(),
|
|
->reactive()
|
|
->default(function () {
|
|
return optional(Item::latest()->first())->plant_id;
|
|
})
|
|
->disabled(fn (Get $get) => !empty($get('id')))
|
|
// ->afterStateUpdated(fn ($set) => $set('block_id', null) & $set('name', null) & $set('start_time', null) & $set('duration', null) & $set('end_time', null))
|
|
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
|
$plantId = $get('plant_id');
|
|
// Ensure `linestop_id` is not cleared
|
|
if (!$plantId) {
|
|
$set('iPlantError', 'Please select a plant first.');
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
$set('iPlantError', null);
|
|
}
|
|
})
|
|
->extraAttributes(fn ($get) => [
|
|
'class' => $get('iPlantError') ? 'border-red-500' : '',
|
|
])
|
|
->hint(fn ($get) => $get('iPlantError') ? $get('iPlantError') : null)
|
|
->hintColor('danger'),
|
|
Forms\Components\TextInput::make('code')
|
|
->required()
|
|
->placeholder('Scan the valid code')
|
|
->autofocus(true)
|
|
// ->unique(ignoreRecord: true)
|
|
->alphaNum()
|
|
->minLength(6)
|
|
// ->autocapitalize('characters')
|
|
->reactive()
|
|
->disabled(fn (Get $get) => !empty($get('id')))
|
|
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
|
$code = $get('code');
|
|
// Ensure `linestop_id` is not cleared
|
|
if (!$code) {
|
|
$set('iCodeError', 'Scan the valid code.');
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
if (strlen($code) < 6) {
|
|
$set('iCodeError', 'Item code must be at least 6 digits.');
|
|
return;
|
|
}
|
|
else if (!preg_match('/^[a-zA-Z0-9]{6,}$/', $code)) {
|
|
$set('code',null);
|
|
$set('iCodeError', 'Item code must contain only alpha-numeric characters.');
|
|
return;
|
|
}
|
|
$set('iCodeError', null);
|
|
}
|
|
})
|
|
->extraAttributes(fn ($get) => [
|
|
'class' => $get('iCodeError') ? 'border-red-500' : '',
|
|
])
|
|
->hint(fn ($get) => $get('iCodeError') ? $get('iCodeError') : null)
|
|
->hintColor('danger')
|
|
->rule(function (callable $get) {
|
|
return Rule::unique('items', 'code')
|
|
->where('plant_id', $get('plant_id'))
|
|
->ignore($get('id')); // Ignore current record during updates
|
|
}),
|
|
Forms\Components\TextInput::make('hourly_quantity')
|
|
->required()
|
|
->label('Hourly Quantity')
|
|
->placeholder('Scan the valid quantity')
|
|
->integer()
|
|
->minValue(1)
|
|
->reactive()
|
|
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
|
$hourQuan = $get('hourly_quantity');
|
|
// Ensure `linestop_id` is not cleared
|
|
if (!$hourQuan) {
|
|
$set('iHourQuanError', 'Scan the valid hourly quantity.');
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
if (!preg_match('/^[0-9]{1,}$/', $hourQuan)) {
|
|
$set('hourly_quantity',null);
|
|
$set('iHourQuanError', 'Quantity must be integer value.');
|
|
return;
|
|
}
|
|
$set('iHourQuanError', null);
|
|
}
|
|
})
|
|
->extraAttributes(fn ($get) => [
|
|
'class' => $get('iHourQuanError') ? 'border-red-500' : '',
|
|
])
|
|
->hint(fn ($get) => $get('iHourQuanError') ? $get('iHourQuanError') : null)
|
|
->hintColor('danger'),
|
|
Forms\Components\Textarea::make('description')
|
|
->required()
|
|
->placeholder('Scan the valid description'),
|
|
// ->columnSpanFull(),
|
|
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('code')
|
|
->sortable()
|
|
->searchable(),
|
|
Tables\Columns\TextColumn::make('description')
|
|
->sortable()
|
|
->searchable(),
|
|
Tables\Columns\TextColumn::make('hourly_quantity')
|
|
->numeric()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('plant.name')
|
|
->sortable()
|
|
->searchable(),
|
|
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(ItemImporter::class),
|
|
// ->maxRows(100000),
|
|
ExportAction::make()
|
|
// ->columnMapping(true)
|
|
// ->label('Export')
|
|
// ->fileName("items" . date('Y-m-d') . ".xlsx")
|
|
->exporter(ItemExporter::class),
|
|
// ->formats([
|
|
// ExportFormat::Xlsx,
|
|
// ExportFormat::Csv,
|
|
// ]),
|
|
]);
|
|
}
|
|
|
|
public static function getRelations(): array
|
|
{
|
|
return [
|
|
//
|
|
];
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => Pages\ListItems::route('/'),
|
|
'create' => Pages\CreateItem::route('/create'),
|
|
'view' => Pages\ViewItem::route('/{record}'),
|
|
'edit' => Pages\EditItem::route('/{record}/edit'),
|
|
];
|
|
}
|
|
|
|
public static function getEloquentQuery(): Builder
|
|
{
|
|
return parent::getEloquentQuery()
|
|
->withoutGlobalScopes([
|
|
SoftDeletingScope::class,
|
|
]);
|
|
}
|
|
}
|