215 lines
8.4 KiB
PHP
215 lines
8.4 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources;
|
|
|
|
use App\Filament\Resources\AlertMailRuleResource\Pages;
|
|
use App\Filament\Resources\AlertMailRuleResource\RelationManagers;
|
|
use App\Models\AlertMailRule;
|
|
use App\Models\Plant;
|
|
use Dotenv\Exception\ValidationException;
|
|
use Filament\Facades\Filament;
|
|
use Filament\Forms;
|
|
use Filament\Forms\Components\Checkbox;
|
|
use Filament\Forms\Form;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Tables;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
|
use Filament\Forms\Components\Section;
|
|
use Illuminate\Validation\ValidationException as ValidationValidationException;
|
|
|
|
class AlertMailRuleResource extends Resource
|
|
{
|
|
protected static ?string $model = AlertMailRule::class;
|
|
|
|
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
|
|
|
|
protected static ?string $navigationGroup = 'Alert Mail';
|
|
|
|
public static function form(Form $form): Form
|
|
{
|
|
return $form
|
|
->schema([
|
|
Section::make('')
|
|
->schema([
|
|
Forms\Components\Select::make('plant')
|
|
->label('Plant')
|
|
->reactive()
|
|
->options(function (callable $get) {
|
|
$userHas = Filament::auth()->user()->plant_id;
|
|
return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray();
|
|
})
|
|
->required(fn ($get) => ! $get('is_active'))
|
|
->afterStateUpdated(fn ($state, callable $set) => $state ? $set('is_active', false) : null),
|
|
// ->options(fn () => Plant::pluck('id', 'name')->toArray()),
|
|
Forms\Components\Select::make('module')
|
|
->label('Module')
|
|
->required()
|
|
->options([
|
|
'InvoiceValidation' => 'InvoiceValidation',
|
|
'InvoiceDataReport' => 'InvoiceDataReport',
|
|
'ProductionQuantities' => 'ProductionQuantities',
|
|
//'Calibration' => 'Calibration',
|
|
]),
|
|
Forms\Components\Select::make('rule_name')
|
|
->label('Rule Name')
|
|
->options([
|
|
'InvoiceMail' => 'Invoice Mail',
|
|
'SerialInvoiceMail' => 'Serial Invoice Mail',
|
|
'MaterialInvoiceMail' => 'Material Invoice Mail',
|
|
'ProductionMail' => 'Production Mail',
|
|
'InvoiceDataMail' => 'Invoice Data Mail',
|
|
//'CalibrationMail' => 'Calibration Mail',
|
|
])
|
|
->required(),
|
|
Forms\Components\TextInput::make('email')
|
|
->label('Email')
|
|
->required(),
|
|
Forms\Components\Textarea::make('cc_emails')
|
|
->label('CC Emails'),
|
|
Forms\Components\Select::make('schedule_type')
|
|
->label('Schedule Type')
|
|
->required()
|
|
->options([
|
|
'Live' => 'Live',
|
|
'Hourly' => 'Hourly',
|
|
'Daily' => 'Daily',
|
|
]),
|
|
Checkbox::make('is_active')
|
|
->label('All Plants Reports')
|
|
->afterStateUpdated(fn ($state, callable $set) => $state ? $set('plant', null) : null)
|
|
->reactive(),
|
|
Forms\Components\Hidden::make('created_by')
|
|
->default(fn () => Filament::auth()->user()?->name),
|
|
Forms\Components\Hidden::make('updated_by')
|
|
->default(fn () => Filament::auth()->user()?->name),
|
|
])
|
|
->columns(6),
|
|
]);
|
|
}
|
|
|
|
// Optionally, also override for update/editing
|
|
// public static function mutateFormDataBeforeSave(array $data): array
|
|
// {
|
|
// dd('test');
|
|
// if ($data['is_active']) {
|
|
// $data['plant'] = 'All Plants';
|
|
// }
|
|
// return $data;
|
|
// }
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->columns([
|
|
Tables\Columns\TextColumn::make('No.')
|
|
->label('No.')
|
|
->getStateUsing(function ($record, $livewire, $column, $rowLoop) {
|
|
$paginator = $livewire->getTableRecords();
|
|
$perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10;
|
|
$currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1;
|
|
return ($currentPage - 1) * $perPage + $rowLoop->iteration;
|
|
}),
|
|
Tables\Columns\TextColumn::make('plant')
|
|
->label('Plant')
|
|
->alignCenter()
|
|
->searchable()
|
|
->sortable()
|
|
->formatStateUsing(function ($state) {
|
|
static $plants;
|
|
if (! $plants) {
|
|
$plants = Plant::pluck('name', 'id')->toArray();
|
|
}
|
|
return $plants[$state] ?? 'All Plants';
|
|
}),
|
|
Tables\Columns\TextColumn::make('module')
|
|
->label('Module')
|
|
->alignCenter()
|
|
->searchable()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('rule_name')
|
|
->label('Rule Name')
|
|
->alignCenter()
|
|
->searchable()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('email')
|
|
->label('Email')
|
|
->searchable()
|
|
->alignCenter()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('cc_emails')
|
|
->label('CC Emails')
|
|
->searchable()
|
|
->alignCenter()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('schedule_type')
|
|
->label('Schedule Type')
|
|
->searchable()
|
|
->alignCenter()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('created_at')
|
|
->alignCenter()
|
|
->dateTime()
|
|
->sortable()
|
|
->toggleable(isToggledHiddenByDefault: true),
|
|
Tables\Columns\TextColumn::make('created_by')
|
|
->label('Created By')
|
|
->alignCenter()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('updated_at')
|
|
->alignCenter()
|
|
->dateTime()
|
|
->sortable()
|
|
->toggleable(isToggledHiddenByDefault: true),
|
|
Tables\Columns\TextColumn::make('updated_by')
|
|
->label('Updated By')
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('deleted_at')
|
|
->alignCenter()
|
|
->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(),
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public static function getRelations(): array
|
|
{
|
|
return [
|
|
//
|
|
];
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => Pages\ListAlertMailRules::route('/'),
|
|
'create' => Pages\CreateAlertMailRule::route('/create'),
|
|
'view' => Pages\ViewAlertMailRule::route('/{record}'),
|
|
'edit' => Pages\EditAlertMailRule::route('/{record}/edit'),
|
|
];
|
|
}
|
|
|
|
public static function getEloquentQuery(): Builder
|
|
{
|
|
return parent::getEloquentQuery()
|
|
->withoutGlobalScopes([
|
|
SoftDeletingScope::class,
|
|
]);
|
|
}
|
|
}
|