diff --git a/app/Filament/Exports/ProductionCharacteristicExporter.php b/app/Filament/Exports/ProductionCharacteristicExporter.php new file mode 100644 index 0000000..2f723d1 --- /dev/null +++ b/app/Filament/Exports/ProductionCharacteristicExporter.php @@ -0,0 +1,48 @@ +label('ID'), + ExportColumn::make('plant.name'), + ExportColumn::make('line.name'), + ExportColumn::make('item.id'), + ExportColumn::make('machine.name'), + ExportColumn::make('production_order'), + ExportColumn::make('serial_number'), + ExportColumn::make('characteristic_name'), + ExportColumn::make('observed_value'), + ExportColumn::make('status'), + ExportColumn::make('inspection_status'), + ExportColumn::make('remark'), + ExportColumn::make('created_at'), + ExportColumn::make('updated_at'), + ExportColumn::make('created_by'), + ExportColumn::make('updated_by'), + ExportColumn::make('deleted_at'), + ]; + } + + public static function getCompletedNotificationBody(Export $export): string + { + $body = 'Your production characteristic export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.'; + + if ($failedRowsCount = $export->getFailedRowsCount()) { + $body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to export.'; + } + + return $body; + } +} diff --git a/app/Filament/Imports/ProductionCharacteristicImporter.php b/app/Filament/Imports/ProductionCharacteristicImporter.php new file mode 100644 index 0000000..6814510 --- /dev/null +++ b/app/Filament/Imports/ProductionCharacteristicImporter.php @@ -0,0 +1,65 @@ +requiredMapping() + ->relationship() + ->rules(['required']), + ImportColumn::make('line') + ->requiredMapping() + ->relationship() + ->rules(['required']), + ImportColumn::make('item') + ->requiredMapping() + ->relationship() + ->rules(['required']), + ImportColumn::make('machine') + ->requiredMapping() + ->relationship() + ->rules(['required']), + ImportColumn::make('production_order'), + ImportColumn::make('serial_number'), + ImportColumn::make('characteristic_name'), + ImportColumn::make('observed_value'), + ImportColumn::make('status'), + ImportColumn::make('inspection_status'), + ImportColumn::make('remark'), + ImportColumn::make('created_by'), + ImportColumn::make('updated_by'), + ]; + } + + public function resolveRecord(): ?ProductionCharacteristic + { + // return ProductionCharacteristic::firstOrNew([ + // // Update existing records, matching them by `$this->data['column_name']` + // 'email' => $this->data['email'], + // ]); + + return new ProductionCharacteristic(); + } + + public static function getCompletedNotificationBody(Import $import): string + { + $body = 'Your production characteristic import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.'; + + if ($failedRowsCount = $import->getFailedRowsCount()) { + $body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to import.'; + } + + return $body; + } +} diff --git a/app/Filament/Resources/ProductionCharacteristicResource.php b/app/Filament/Resources/ProductionCharacteristicResource.php new file mode 100644 index 0000000..a6890a7 --- /dev/null +++ b/app/Filament/Resources/ProductionCharacteristicResource.php @@ -0,0 +1,604 @@ +schema([ + Forms\Components\Select::make('plant_id') + ->label('Plant Name') + ->nullable() + ->searchable() + ->reactive() + ->relationship('plant', 'name') + ->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(ProductionCharacteristic::latest()->first())->plant_id; + }) + ->afterStateUpdated(function ($state, $set, callable $get) { + $plantId = $get('plant_id'); + $set('line_id', null); + $set('item_id', null); + $set('machine_id', null); + $set('production_order', null); + $set('serial_number', null); + $set('observed_value', null); + $set('status', 'NotOk'); + $set('updated_by', Filament::auth()->user()?->name); + if (! $plantId) { + $set('poPlantError', 'Please select a plant first.'); + } + }) + ->extraAttributes(fn ($get) => [ + 'class' => $get('poPlantError') ? 'border-red-500' : '', + ]) + ->hint(fn ($get) => $get('poPlantError') ? $get('poPlantError') : null) + ->hintColor('danger') + ->required(), + Forms\Components\Select::make('line_id') + ->label('Line Name') + ->nullable() + ->searchable() + ->reactive() + ->options(function (callable $get) { + if (! $get('plant_id')) { + return []; + } + + return Line::where('plant_id', $get('plant_id')) + ->pluck('name', 'id') + ->toArray(); + }) + ->disabled(fn (Get $get) => ! empty($get('id'))) + ->afterStateUpdated(function ($state, $set, callable $get) { + $plantId = $get('plant_id'); + $set('item_id', null); + $set('machine_id', null); + $set('production_order', null); + $set('serial_number', null); + $set('observed_value', null); + $set('status', 'NotOk'); + $set('updated_by', Filament::auth()->user()?->name); + if (! $plantId) { + $set('line_id', null); + $set('poPlantError', 'Please select a plant first.'); + } + }) + ->required(), + Forms\Components\Select::make('item_id') + ->label('Item Code') + ->nullable() + ->searchable() + ->reactive() + ->options(function (callable $get) { + if (! $get('plant_id') || ! $get('line_id')) { + return []; + } + + return Item::where('plant_id', $get('plant_id')) + ->pluck('code', 'id') + ->toArray(); + }) + ->disabled(fn (Get $get) => ! empty($get('id'))) + ->afterStateUpdated(function ($state, $set, callable $get) { + $plantId = $get('plant_id'); + $set('machine_id', null); + $set('production_order', null); + $set('serial_number', null); + $set('observed_value', null); + $set('status', 'NotOk'); + $set('updated_by', Filament::auth()->user()?->name); + if (! $plantId) { + $set('item_id', null); + $set('poPlantError', 'Please select a plant first.'); + } + }) + ->required(), + Forms\Components\Select::make('machine_id') + ->label('Work Center') + ->nullable() + ->searchable() + ->reactive() + ->options(function (callable $get) { + if (! $get('plant_id') || ! $get('line_id') || ! $get('item_id')) { + return []; + } + + return Machine::where('plant_id', $get('plant_id')) + ->where('line_id', $get('line_id')) + ->pluck('work_center', 'id') + ->toArray(); + }) + ->disabled(fn (Get $get) => ! empty($get('id'))) + ->afterStateUpdated(function ($state, $set, callable $get) { + $plantId = $get('plant_id'); + $set('production_order', null); + $set('serial_number', null); + $set('observed_value', null); + $set('status', 'NotOk'); + $set('updated_by', Filament::auth()->user()?->name); + if (! $plantId) { + $set('machine_id', null); + $set('poPlantError', 'Please select a plant first.'); + } + }) + ->required(), + Forms\Components\TextInput::make('production_order') + ->label('Production Order'), + Forms\Components\TextInput::make('serial_number') + ->label('Serial Number'), + Forms\Components\TextInput::make('inspection_lot_number') + ->label('Inspection Lot No'), + Forms\Components\TextInput::make('characteristic_name') + ->label('Characteristic Name'), + Forms\Components\TextInput::make('observed_value') + ->label('Observed Value'), + Forms\Components\Select::make('status') + ->label('Status') + ->options([ + 'Ok' => 'Ok', + 'NotOk' => 'Not Ok', + 'ConditionallyAccepted' => 'Conditionally Accepted', + ]) + ->reactive(), + Forms\Components\TextInput::make('inspection_status') + ->label('Inspection Status'), + Forms\Components\TextInput::make('remark') + ->label('Remark') + ->reactive() + ->required(fn ($get) => $get('status') == 'ConditionallyAccepted') + // ->visible(fn ($get) => $get('status') == 'ConditionallyAccepted'), + ->visible(fn ($get) => in_array($get('status'), [ + 'NotOk', + 'ConditionallyAccepted', + ])), + Forms\Components\Hidden::make('created_by') + ->label('Created By'), + Forms\Components\Hidden::make('updated_by') + ->label('Updated By'), + ]); + } + + 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') + ->searchable() + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('line.name') + ->label('Line Name') + ->searchable() + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('item.code') + ->label('Item Code') + ->searchable() + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('item.description') + ->label('Item Description') + ->searchable() + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('machine.work_center') + ->label('Work Center') + ->searchable() + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('production_order') + ->label('Production Order / Doc No') + ->searchable() + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('inspection_lot_number') + ->label('Inspection Lot No') + ->searchable() + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('serial_number') + ->label('Serial Number') + ->searchable() + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('characteristic_name') + ->label('Characteristic Name') + ->searchable() + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('machine.name') + ->label('Spec. Value') + // ->searchable() + ->formatStateUsing(function ($record) { + $specVal = ProductCharacteristicsMaster::where('plant_id', $record->plant_id)->where('item_id', $record->item_id)->where('line_id', $record->line_id)->where('machine_id', $record->machine_id)->where('name', $record->characteristic_name)->first(); + // return $record?->plant_id.'-'.$record?->item_id.'-'.$record->line_id.'-'.$record?->machine_id; + + return $specVal?->lower.' - '.$specVal?->upper; + }) + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('observed_value') + ->label('Observed Value') + ->searchable() + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('status') + ->label('Status') + ->searchable() + // ->formatStateUsing(function ($record) { + // return empty($record->status == 'Ok') ? 'Ok' : 'Not Ok'; + // }) + ->color(fn (string $state): string => match ($state) { + 'Ok' => 'success', + 'Not Ok' => 'danger', + 'NotOk' => 'danger', + 'ConditionallyAccepted' => 'success', + default => 'black', + }) + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('remark') + ->label('Remark') + ->searchable() + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('created_at') + ->label('Created At') + ->alignCenter() + ->dateTime() + ->sortable() + ->toggleable(isToggledHiddenByDefault: true), + Tables\Columns\TextColumn::make('updated_at') + ->label('Updated At') + ->alignCenter() + ->dateTime() + ->sortable() + ->toggleable(isToggledHiddenByDefault: true), + Tables\Columns\TextColumn::make('deleted_at') + ->label('Deleted At') + ->alignCenter() + ->dateTime() + ->sortable() + ->toggleable(isToggledHiddenByDefault: true), + ]) + ->filters([ + Tables\Filters\TrashedFilter::make(), + Filter::make('advanced_filters') + ->label('Advanced Filters') + ->form([ + Select::make('Plant') + ->label('Search by Plant Name') + ->nullable() + ->searchable() + ->reactive() + ->options(function (callable $get) { + $userHas = Filament::auth()->user()->plant_id; + + if ($userHas && strlen($userHas) > 0) { + return Plant::where('id', $userHas)->pluck('name', 'id')->toArray(); + } else { + return Plant::whereHas('productionCharacteristics', function ($query) { + $query->whereNotNull('id'); + })->orderBy('code')->pluck('name', 'id'); + } + + // return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); + }) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $set('Line', null); + $set('Item', null); + $set('Machine', null); + }), + Select::make('Line') + ->label('Search by Line Name') + ->nullable() + ->searchable() + ->reactive() + ->options(function (callable $get) { + $plantId = $get('Plant'); + + if (empty($plantId)) { + return []; + } + + return Line::whereHas('productionCharacteristics', function ($query) use ($plantId) { + if ($plantId) { + $query->where('plant_id', $plantId); + } + })->pluck('name', 'id'); + }) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $set('Machine', null); + }), + Select::make('Item') + ->label('Search by Item Code') + ->nullable() + ->searchable() + ->reactive() + ->options(function (callable $get) { + $plantId = $get('Plant'); + + if (empty($plantId)) { + return []; + } + + return Item::whereHas('productionCharacteristics', function ($query) use ($plantId) { + if ($plantId) { + $query->where('plant_id', $plantId); + } + })->pluck('code', 'id'); + }) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $set('production_order', null); + }), + Select::make('Machine') + ->label('Search by Work Center') + ->nullable() + ->searchable() + ->reactive() + ->options(function (callable $get) { + $plantId = $get('Plant'); + $lineId = $get('Line'); + + if (empty($plantId)) { + return []; + } + + return Machine::whereHas('productionCharacteristics', function ($query) use ($plantId, $lineId) { + if ($plantId) { + $query->where('plant_id', $plantId)->where('line_id', $lineId); + } + })->pluck('work_center', 'id'); + }) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $set('production_order', null); + }), + TextInput::make('production_order') + ->label('Production Order') + ->reactive() + ->placeholder('Enter Production Order') + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $set('serial_number', null); + }), + TextInput::make('serial_number') + ->label('Serial Number') + ->reactive() + ->placeholder('Enter Serial Number'), + Select::make('status') + ->label('Search by Status') + ->nullable() + ->searchable() + ->reactive() + ->options([ + 'Ok' => 'Ok', + 'NotOk' => 'Not Ok', + 'ConditionallyAccepted' => 'Conditionally Accepted', + ]), + DateTimePicker::make(name: 'created_from') + ->label('Created From') + ->placeholder('Select From DateTime') + ->reactive() + ->native(false), + DateTimePicker::make('created_to') + ->label('Created To') + ->placeholder('Select To DateTime') + ->reactive() + ->native(false), + ]) + ->query(function ($query, array $data) { + // Hide all records initially if no filters are applied + if (empty($data['Plant']) && empty($data['Line']) && empty($data['Item']) && empty($data['production_order']) && Str::length($data['serial_number']) <= 0 && ($data['status'] == null || $data['status'] == '') && empty($data['created_from']) && empty($data['created_to'])) { + return $query->whereRaw('1 = 0'); + } + + if (! empty($data['Plant'])) { + $query->where('plant_id', $data['Plant']); + } else { + $userHas = Filament::auth()->user()->plant_id; + + if ($userHas && strlen($userHas) > 0) { + return $query->whereRaw('1 = 0'); + } + } + + if (! empty($data['Line'])) { + $query->where('line_id', $data['Line']); + } + + if (! empty($data['Item'])) { + $query->where('item_id', $data['Item']); + } + + if (! empty($data['Machine'])) { + $query->where('machine_id', $data['Machine']); + } + + if (! empty($data['production_order'])) { + $query->where('production_order', 'like', '%'.$data['production_order'].'%'); + } + + if (Str::length($data['serial_number']) > 0) { + // $query->where('machine_name', $data['machine_name']); + $query->where('serial_number', 'like', '%'.$data['serial_number'].'%'); + } + + if ($data['status'] != null && $data['status'] != '') { + $query->where('status', $data['status']); + } + + if (! empty($data['created_from'])) { + $query->where('created_at', '>=', $data['created_from']); + } + + if (! empty($data['created_to'])) { + $query->where('created_at', '<=', $data['created_to']); + } + // $query->orderBy('created_at', 'asc'); + }) + ->indicateUsing(function (array $data) { + $indicators = []; + + if (! empty($data['Plant'])) { + $indicators[] = 'Plant Name: '.Plant::where('id', $data['Plant'])->value('name'); + } else { + $userHas = Filament::auth()->user()->plant_id; + + if ($userHas && strlen($userHas) > 0) { + return 'Plant Name: Choose plant to filter records.'; + } + } + + if (! empty($data['Line'])) { + $indicators[] = 'Line Name: '.Line::where('id', $data['Line'])->value('name'); + } + + if (! empty($data['Item'])) { + $indicators[] = 'Item Code: '.Item::where('id', $data['Item'])->value('code'); + } + + if (! empty($data['Machine'])) { + $indicators[] = 'Machine: '.Machine::where('id', $data['Machine'])->value('work_center'); + } + + if (! empty($data['production_order'])) { + $indicators[] = 'Production Order: '.$data['production_order']; + } + + if (Str::length($data['serial_number']) > 0) { + $indicators[] = 'Serial Number: '.$data['serial_number']; + } + + // if ($data['status'] != null && $data['status'] != '') { + // $indicators[] = ($data['status'] == 'Ok') ? 'Status: Ok' : 'Status: Not Ok'; + // } + + if ($data['status'] != null && $data['status'] != '') { + if ($data['status'] == 'Ok') { + $indicators[] = 'Status: Ok'; + } elseif ($data['status'] == 'NotOk') { + $indicators[] = 'Status: Not Ok'; + } elseif ($data['status'] == 'ConditionallyAccepted') { + $indicators[] = 'Status: Conditionally Accepted'; + } + } + + if (! empty($data['created_from'])) { + $indicators[] = 'From: '.$data['created_from']; + } + + if (! empty($data['created_to'])) { + $indicators[] = 'To: '.$data['created_to']; + } + + return $indicators; + }), + ]) + ->filtersFormMaxHeight('280px') + ->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 Characteristics') + ->color('warning') + ->importer(ProductionCharacteristicImporter::class) + ->visible(function () { + return Filament::auth()->user()->can('view import production characteristics'); + }), + ExportAction::make() + ->label('Export Production Characteristics') + ->color('warning') + ->exporter(ProductionCharacteristicExporter::class) + ->visible(function () { + return Filament::auth()->user()->can('view export production characteristics'); + }), + ]); + } + + public static function getRelations(): array + { + return [ + // + ]; + } + + public static function getPages(): array + { + return [ + 'index' => Pages\ListProductionCharacteristics::route('/'), + 'create' => Pages\CreateProductionCharacteristic::route('/create'), + 'view' => Pages\ViewProductionCharacteristic::route('/{record}'), + 'edit' => Pages\EditProductionCharacteristic::route('/{record}/edit'), + ]; + } + + public static function getEloquentQuery(): Builder + { + return parent::getEloquentQuery() + ->withoutGlobalScopes([ + SoftDeletingScope::class, + ]); + } +} diff --git a/app/Filament/Resources/ProductionCharacteristicResource/Pages/CreateProductionCharacteristic.php b/app/Filament/Resources/ProductionCharacteristicResource/Pages/CreateProductionCharacteristic.php new file mode 100644 index 0000000..90592b6 --- /dev/null +++ b/app/Filament/Resources/ProductionCharacteristicResource/Pages/CreateProductionCharacteristic.php @@ -0,0 +1,12 @@ +hasMany(ProductCharacteristicsMaster::class, 'plant_id', 'id'); } + + public function productionCharacteristics() + { + return $this->hasMany(ProductionCharacteristic::class, 'plant_id', 'id'); + } } diff --git a/app/Models/ProductionCharacteristic.php b/app/Models/ProductionCharacteristic.php new file mode 100644 index 0000000..d065af5 --- /dev/null +++ b/app/Models/ProductionCharacteristic.php @@ -0,0 +1,56 @@ + 'array', + ]; + + protected $fillable = [ + 'plant_id', + 'line_id', + 'item_id', + 'machine_id', + 'production_order', + 'serial_number', + 'observed_value', + 'characteristic_name', + 'status', + 'inspection_status', + 'inspection_lot_number', + 'remark', + 'result', + 'created_at', + 'updated_at', + 'created_by', + 'updated_by', + ]; + + public function plant(): BelongsTo + { + return $this->belongsTo(Plant::class); + } + + public function line(): BelongsTo + { + return $this->belongsTo(Line::class); + } + + public function item(): BelongsTo + { + return $this->belongsTo(Item::class); + } + + public function machine(): BelongsTo + { + return $this->belongsTo(Machine::class); + } +} diff --git a/database/migrations/2026_10_26_141214_create_production_characteristics_table.php b/database/migrations/2026_10_26_141214_create_production_characteristics_table.php new file mode 100644 index 0000000..0778b45 --- /dev/null +++ b/database/migrations/2026_10_26_141214_create_production_characteristics_table.php @@ -0,0 +1,55 @@ +