226 lines
9.6 KiB
PHP
226 lines
9.6 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources;
|
|
|
|
use App\Filament\Imports\PlantImporter;
|
|
use App\Filament\Resources\PlantResource\Pages;
|
|
use App\Models\Plant;
|
|
use Filament\Forms;
|
|
use Filament\Forms\Components\Section;
|
|
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;
|
|
|
|
class PlantResource extends Resource
|
|
{
|
|
protected static ?string $model = Plant::class;
|
|
|
|
protected static ?string $navigationIcon = 'heroicon-s-building-office-2';
|
|
|
|
protected static ?string $navigationGroup = 'Master Entries';
|
|
|
|
protected static ?int $navigationSort = 2;
|
|
|
|
public static function form(Form $form): Form
|
|
{
|
|
return $form
|
|
->schema([
|
|
Section::make('')
|
|
->schema([
|
|
Forms\Components\TextInput::make('code')
|
|
->required()
|
|
->unique(ignoreRecord: true)
|
|
->integer()
|
|
->reactive()
|
|
->placeholder('Scan the valid code')
|
|
->autofocus(true)
|
|
->minValue(1000)
|
|
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
|
$codeId = $get('code');
|
|
// Ensure `linestop_id` is not cleared
|
|
if (!$codeId) {
|
|
$set('pCodeError', 'Scan the valid code.');
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
if (strlen($codeId) < 4) {
|
|
$set('pCodeError', 'Code must be at least 4 digits.');
|
|
return;
|
|
}
|
|
// if (!preg_match('/^[1-9][0-9]{3,}$/', $codeId)) {
|
|
// $set('code',null);
|
|
// $set('pCodeError', 'Code should not begin with zero and must contain 4 digit number.');
|
|
// return;
|
|
// }
|
|
$set('pCodeError', null);
|
|
}
|
|
})
|
|
->extraAttributes(fn ($get) => [
|
|
'class' => $get('pCodeError') ? 'border-red-500' : '',
|
|
])
|
|
->hint(fn ($get) => $get('pCodeError') ? $get('pCodeError') : null)
|
|
->hintColor('danger'),
|
|
Forms\Components\Select::make('company_id')
|
|
// ->placeholder('Choose the valid company name')
|
|
->relationship('company', 'name')
|
|
->required()
|
|
->reactive()
|
|
->default(function () {
|
|
return optional(Plant::latest()->first())->company_id;
|
|
})
|
|
->disabled(fn (Get $get) => !empty($get('id')))
|
|
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
|
$companyId = $get('company_id');
|
|
// Ensure `linestop_id` is not cleared
|
|
if (!$companyId) {
|
|
$set('pCompanyError', 'Please select a company first.');
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
$set('pCompanyError', null);
|
|
}
|
|
})
|
|
->extraAttributes(fn ($get) => [
|
|
'class' => $get('pCompanyError') ? 'border-red-500' : '',
|
|
])
|
|
->hint(fn ($get) => $get('pCompanyError') ? $get('pCompanyError') : null)
|
|
->hintColor('danger'),
|
|
Forms\Components\TextInput::make('name')
|
|
->required()
|
|
->placeholder('Scan the valid name')
|
|
->unique(ignoreRecord: true)
|
|
->columnSpanFull()
|
|
->reactive()
|
|
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
|
$nameId = $get('name');
|
|
// Ensure `linestop_id` is not cleared
|
|
if (!$nameId) {
|
|
$set('pNameError', 'Scan the valid name.');
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
$set('pNameError', null);
|
|
}
|
|
})
|
|
->extraAttributes(fn ($get) => [
|
|
'class' => $get('pNameError') ? 'border-red-500' : '',
|
|
])
|
|
->hint(fn ($get) => $get('pNameError') ? $get('pNameError') : null)
|
|
->hintColor('danger'),
|
|
Forms\Components\Textarea::make('address')
|
|
->required()
|
|
->placeholder('Scan the valid address')
|
|
->unique(ignoreRecord: true)
|
|
->columnSpanFull()
|
|
->reactive()
|
|
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
|
$nameId = $get('address');
|
|
// Ensure `linestop_id` is not cleared
|
|
if (!$nameId) {
|
|
$set('pAddressError', 'Scan the valid address.');
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
$set('pAddressError', null);
|
|
}
|
|
})
|
|
->extraAttributes(fn ($get) => [
|
|
'class' => $get('pAddressError') ? 'border-red-500' : '',
|
|
])
|
|
->hint(fn ($get) => $get('pAddressError') ? $get('pAddressError') : null)
|
|
->hintColor('danger'),
|
|
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('name')
|
|
->sortable()
|
|
->searchable(),
|
|
Tables\Columns\TextColumn::make('address')
|
|
->sortable()
|
|
->searchable(),
|
|
Tables\Columns\TextColumn::make('company.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(PlantImporter::class),
|
|
]);
|
|
}
|
|
|
|
public static function getRelations(): array
|
|
{
|
|
return [
|
|
//
|
|
];
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => Pages\ListPlants::route('/'),
|
|
'create' => Pages\CreatePlant::route('/create'),
|
|
'view' => Pages\ViewPlant::route('/{record}'),
|
|
'edit' => Pages\EditPlant::route('/{record}/edit'),
|
|
];
|
|
}
|
|
|
|
public static function getEloquentQuery(): Builder
|
|
{
|
|
return parent::getEloquentQuery()
|
|
->withoutGlobalScopes([
|
|
SoftDeletingScope::class,
|
|
]);
|
|
}
|
|
}
|