Added production order resource pages
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
This commit is contained in:
327
app/Filament/Resources/ProductionOrderResource.php
Normal file
327
app/Filament/Resources/ProductionOrderResource.php
Normal file
@@ -0,0 +1,327 @@
|
||||
<?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 (Get $get) => ! empty($get('id')))
|
||||
->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())->plant_id : optional(ProductionOrder::latest()->first())->plant_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))
|
||||
->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'),
|
||||
Forms\Components\DateTimePicker::make('end_date')
|
||||
->label('End Date'),
|
||||
Forms\Components\TextInput::make('production_order')
|
||||
->label('Production Order')
|
||||
->readOnly()
|
||||
->visible(fn ($get) => $get('show_extra_fields')),
|
||||
Forms\Components\TextInput::make('from_serial_number')
|
||||
->label('From Serial Number')
|
||||
->readOnly()
|
||||
->visible(fn ($get) => $get('show_extra_fields')),
|
||||
Forms\Components\TextInput::make('to_serial_number')
|
||||
->label('To Serial Number')
|
||||
->readOnly()
|
||||
->visible(fn ($get) => $get('show_extra_fields')),
|
||||
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.')
|
||||
->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()
|
||||
->sortable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
Tables\Columns\TextColumn::make('updated_at')
|
||||
->label('Updated At')
|
||||
->dateTime()
|
||||
->sortable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
Tables\Columns\TextColumn::make('deleted_at')
|
||||
->label('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()
|
||||
->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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user