Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
Gemini PR Review / Gemini PR Review (pull_request) Has been cancelled
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Has been cancelled
Laravel Larastan / larastan (pull_request) Has been cancelled
Laravel Pint / pint (pull_request) Has been cancelled
362 lines
17 KiB
PHP
362 lines
17 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources;
|
|
|
|
use App\Filament\Exports\ProductionOrderExporter;
|
|
use App\Filament\Imports\ProductionOrderImporter;
|
|
use App\Filament\Resources\ProductionOrderResource\Pages;
|
|
use App\Models\Item;
|
|
use App\Models\Plant;
|
|
use App\Models\ProductionOrder;
|
|
use Carbon\Carbon;
|
|
use Filament\Facades\Filament;
|
|
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\ExportAction;
|
|
use Filament\Tables\Actions\ImportAction;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
|
|
|
class ProductionOrderResource extends Resource
|
|
{
|
|
protected static ?string $model = ProductionOrder::class;
|
|
|
|
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
|
|
|
|
protected static ?string $navigationGroup = 'Production Order';
|
|
|
|
public static function form(Form $form): Form
|
|
{
|
|
return $form
|
|
->schema([
|
|
Section::make('')
|
|
->schema([
|
|
Forms\Components\Select::make('plant_id')
|
|
->label('Plant Name')
|
|
->relationship('plant', 'name')
|
|
->searchable()
|
|
->options(function (callable $get) {
|
|
$userHas = Filament::auth()->user()->plant_id;
|
|
|
|
return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray();
|
|
})
|
|
->disabled(fn (string $context): bool => $context == 'edit')
|
|
->dehydrated(true)
|
|
->default(function () {
|
|
$userHas = Filament::auth()->user()->plant_id;
|
|
|
|
return ($userHas && strlen($userHas) > 0) ? $userHas : optional(ProductionOrder::latest()->first())->plant_id;
|
|
})
|
|
->reactive()
|
|
->extraAttributes(fn ($get) => [
|
|
'class' => $get('poPlantError') ? 'border-red-500' : '',
|
|
])
|
|
->hint(fn ($get) => $get('poPlantError') ? $get('poPlantError') : null)
|
|
->hintColor('danger')
|
|
->required()
|
|
->afterStateUpdated(function ($state, callable $set) {
|
|
$set('item_id', null);
|
|
$set('quantity', null);
|
|
$set('show_extra_fields', false);
|
|
$set('start_date', null);
|
|
$set('end_date', null);
|
|
$set('production_order', null);
|
|
$set('from_serial_number', null);
|
|
$set('to_serial_number', null);
|
|
}),
|
|
Forms\Components\Select::make('item_id')
|
|
->label('Item Code')
|
|
->searchable()
|
|
->reactive()
|
|
->options(function (callable $get) {
|
|
$plantId = $get('plant_id');
|
|
if (empty($plantId)) {
|
|
return [];
|
|
}
|
|
|
|
return Item::where('plant_id', $plantId)->pluck('code', 'id');
|
|
})
|
|
->disabled(fn (Get $get) => ! empty($get('id')))
|
|
->default(function () {
|
|
$userHas = Filament::auth()->user()->plant_id;
|
|
|
|
return ($userHas && strlen($userHas) > 0) ? optional(ProductionOrder::where('plant_id', $userHas)->latest()->first())->item_id : optional(ProductionOrder::latest()->first())->item_id;
|
|
})
|
|
->required()
|
|
->afterStateUpdated(function ($state, callable $set) {
|
|
$set('quantity', null);
|
|
$set('show_extra_fields', false);
|
|
$set('start_date', null);
|
|
$set('end_date', null);
|
|
$set('production_order', null);
|
|
$set('from_serial_number', null);
|
|
$set('to_serial_number', null);
|
|
}),
|
|
Forms\Components\TextInput::make('quantity')
|
|
->label('Quantity')
|
|
->reactive()
|
|
->integer()
|
|
->required()
|
|
->readOnly(fn ($get) => ($get('plant_id') == null || $get('item_id') == null))
|
|
->disabled(fn (string $context): bool => $context === 'edit')
|
|
->afterStateUpdated(function ($state, callable $set, $get) {
|
|
if (! empty($state) && $state > 0) {
|
|
$set('show_extra_fields', true);
|
|
$now = Carbon::now();
|
|
$plantId = $get('plant_id');
|
|
$quantity = $get('quantity');
|
|
$set('start_date', null);
|
|
$set('end_date', null);
|
|
$plantCode = Plant::find($plantId)->code;
|
|
|
|
$year = $now->format('y'); // Year (last 2 digits)
|
|
$month = $now->format('m');
|
|
|
|
$monthText = strtoupper($now->format('M')); // APR
|
|
$monthNumber = '';
|
|
|
|
foreach (str_split($monthText) as $char) {
|
|
$curSeq = ord($char) - 64;
|
|
$monthNumber .= str_pad($curSeq, 2, '0', STR_PAD_LEFT);
|
|
}
|
|
|
|
$prefix = $year.$monthNumber;
|
|
|
|
$last = ProductionOrder::where('production_order', 'like', "{$prefix}%")->orderByDesc('production_order')->first(); // ProductionOrder::where('production_order', 'like', $prefix.'%')->orderBy('production_order', 'desc')->first();
|
|
|
|
if ($last) {
|
|
$lastSeq = substr($last->production_order, -4);
|
|
$nextSeq = str_pad(((int) $lastSeq + 1), 4, '0', STR_PAD_LEFT);
|
|
} else {
|
|
$nextSeq = '0001';
|
|
}
|
|
|
|
$productionOrder = $prefix.$nextSeq;
|
|
|
|
$set('production_order', $productionOrder);
|
|
|
|
$prefixSerial = $plantCode.$year.$month;
|
|
|
|
$last = ProductionOrder::where('to_serial_number', 'like', "{$prefixSerial}%")->orderByDesc('to_serial_number')->first();
|
|
|
|
if ($last) {
|
|
$lastSeq = (int) substr($last->to_serial_number, -6);
|
|
$startSeq = $lastSeq + 1;
|
|
} else {
|
|
$startSeq = 1;
|
|
}
|
|
|
|
$endSeq = $startSeq + $quantity - 1;
|
|
|
|
$fromSerial = $prefixSerial.str_pad($startSeq, 6, '0', STR_PAD_LEFT);
|
|
$toSerial = $prefixSerial.str_pad($endSeq, 6, '0', STR_PAD_LEFT);
|
|
|
|
$set('from_serial_number', $fromSerial);
|
|
$set('to_serial_number', $toSerial);
|
|
|
|
} else {
|
|
$set('show_extra_fields', false);
|
|
|
|
$set('production_order', null);
|
|
$set('start_date', null);
|
|
$set('end_date', null);
|
|
$set('from_serial_number', null);
|
|
$set('to_serial_number', null);
|
|
}
|
|
}),
|
|
Forms\Components\Hidden::make('show_extra_fields')
|
|
->default(false),
|
|
Forms\Components\DateTimePicker::make('start_date')
|
|
->label('Start Date')
|
|
->reactive()
|
|
// ->minDate(function () {
|
|
// return Carbon::now()->addMinute();
|
|
// })
|
|
->afterStateUpdated(function ($state, callable $set) {
|
|
$set('end_date', null);
|
|
}),
|
|
Forms\Components\DateTimePicker::make('end_date')
|
|
->label('End Date')
|
|
->reactive(),
|
|
// ->rule('after:start_date')
|
|
// ->minDate(function (callable $get) {
|
|
// $startDate = $get('start_date');
|
|
// if ($startDate) {
|
|
// return Carbon::parse($startDate)->addMinute();
|
|
// }
|
|
|
|
// return Carbon::now()->addMinute();
|
|
// }),
|
|
Forms\Components\TextInput::make('production_order')
|
|
->label('Production Order')
|
|
->readOnly()
|
|
->visible(fn ($get) => $get('quantity') > 0),
|
|
// ->visible(fn ($get) => $get('show_extra_fields')),
|
|
Forms\Components\TextInput::make('from_serial_number')
|
|
->label('From Serial Number')
|
|
->readOnly()
|
|
->visible(fn ($get) => $get('quantity') > 0),
|
|
Forms\Components\TextInput::make('to_serial_number')
|
|
->label('To Serial Number')
|
|
->readOnly()
|
|
->visible(fn ($get) => $get('quantity') > 0),
|
|
Forms\Components\Hidden::make('created_by')
|
|
->label('Created By')
|
|
->default(Filament::auth()->user()?->name),
|
|
Forms\Components\Hidden::make('updated_by')
|
|
->label('Updated By')
|
|
->default(Filament::auth()->user()?->name),
|
|
Forms\Components\View::make('forms.components.save-production-order-button'),
|
|
])
|
|
->columns(5),
|
|
]);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->columns([
|
|
Tables\Columns\TextColumn::make('No.')
|
|
->label('No.')
|
|
->alignCenter()
|
|
->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.name')
|
|
->label('Plant Name')
|
|
->alignCenter()
|
|
->searchable()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('item.code')
|
|
->label('Item Code')
|
|
->searchable()
|
|
->alignCenter()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('quantity')
|
|
->label('Quantity')
|
|
->searchable()
|
|
->alignCenter()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('start_date')
|
|
->label('Start Date')
|
|
->searchable()
|
|
->alignCenter()
|
|
->dateTime()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('end_date')
|
|
->label('End Date')
|
|
->searchable()
|
|
->alignCenter()
|
|
->dateTime()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('production_order')
|
|
->label('Production Order')
|
|
->searchable()
|
|
->alignCenter()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('from_serial_number')
|
|
->label('From Serial Number')
|
|
->searchable()
|
|
->alignCenter()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('to_serial_number')
|
|
->label('To Serial Number')
|
|
->searchable()
|
|
->alignCenter()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('created_at')
|
|
->label('Created At')
|
|
->dateTime()
|
|
->alignCenter()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('created_by')
|
|
->label('Created By')
|
|
->searchable()
|
|
->alignCenter()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('updated_at')
|
|
->label('Updated At')
|
|
->dateTime()
|
|
->alignCenter()
|
|
->sortable()
|
|
->toggleable(isToggledHiddenByDefault: true),
|
|
Tables\Columns\TextColumn::make('updated_by')
|
|
->label('Updated By')
|
|
->searchable()
|
|
->alignCenter()
|
|
->sortable()
|
|
->toggleable(isToggledHiddenByDefault: true),
|
|
Tables\Columns\TextColumn::make('deleted_at')
|
|
->label('Deleted At')
|
|
->dateTime()
|
|
->alignCenter()
|
|
->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()
|
|
->label('Import Production Orders')
|
|
->color('warning')
|
|
->importer(ProductionOrderImporter::class)
|
|
->visible(function () {
|
|
return Filament::auth()->user()->can('view import production orders');
|
|
}),
|
|
ExportAction::make()
|
|
->label('Export Production Orders')
|
|
->color('warning')
|
|
->exporter(ProductionOrderExporter::class)
|
|
->visible(function () {
|
|
return Filament::auth()->user()->can('view export production orders');
|
|
}),
|
|
]);
|
|
}
|
|
|
|
public static function getRelations(): array
|
|
{
|
|
return [
|
|
//
|
|
];
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => Pages\ListProductionOrders::route('/'),
|
|
'create' => Pages\CreateProductionOrder::route('/create'),
|
|
'view' => Pages\ViewProductionOrder::route('/{record}'),
|
|
'edit' => Pages\EditProductionOrder::route('/{record}/edit'),
|
|
];
|
|
}
|
|
|
|
public static function getEloquentQuery(): Builder
|
|
{
|
|
return parent::getEloquentQuery()
|
|
->withoutGlobalScopes([
|
|
SoftDeletingScope::class,
|
|
]);
|
|
}
|
|
}
|