From 46df0ef93aa77fa802491ef49553fadefb169aac Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Sun, 20 Sep 2026 11:20:33 +0530 Subject: [PATCH 1/5] Added characteristic value migration file --- ...212_create_characteristic_values_table.php | 48 +++++++++++++++++++ ..._value_column_to_characteristic_values.php | 29 +++++++++++ 2 files changed, 77 insertions(+) create mode 100644 database/migrations/2026_09_20_111212_create_characteristic_values_table.php create mode 100644 database/migrations/2026_09_20_111529_add_observed_value_column_to_characteristic_values.php diff --git a/database/migrations/2026_09_20_111212_create_characteristic_values_table.php b/database/migrations/2026_09_20_111212_create_characteristic_values_table.php new file mode 100644 index 0000000..f8d9b5f --- /dev/null +++ b/database/migrations/2026_09_20_111212_create_characteristic_values_table.php @@ -0,0 +1,48 @@ + Date: Sun, 20 Sep 2026 11:21:47 +0530 Subject: [PATCH 2/5] Added characteristic value model file --- app/Models/CharacteristicValue.php | 47 ++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 app/Models/CharacteristicValue.php diff --git a/app/Models/CharacteristicValue.php b/app/Models/CharacteristicValue.php new file mode 100644 index 0000000..abad9d0 --- /dev/null +++ b/app/Models/CharacteristicValue.php @@ -0,0 +1,47 @@ +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); + } +} -- 2.49.1 From 8f3432b57a56e8409d4051f1e52e0796dffde677 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Sun, 20 Sep 2026 11:24:25 +0530 Subject: [PATCH 3/5] Added characteristics values resource pages --- .../Resources/CharacteristicValueResource.php | 703 ++++++++++++++++++ .../Pages/CreateCharacteristicValue.php | 12 + .../Pages/EditCharacteristicValue.php | 22 + .../Pages/ListCharacteristicValues.php | 19 + .../Pages/ViewCharacteristicValue.php | 19 + 5 files changed, 775 insertions(+) create mode 100644 app/Filament/Resources/CharacteristicValueResource.php create mode 100644 app/Filament/Resources/CharacteristicValueResource/Pages/CreateCharacteristicValue.php create mode 100644 app/Filament/Resources/CharacteristicValueResource/Pages/EditCharacteristicValue.php create mode 100644 app/Filament/Resources/CharacteristicValueResource/Pages/ListCharacteristicValues.php create mode 100644 app/Filament/Resources/CharacteristicValueResource/Pages/ViewCharacteristicValue.php diff --git a/app/Filament/Resources/CharacteristicValueResource.php b/app/Filament/Resources/CharacteristicValueResource.php new file mode 100644 index 0000000..c3a90c4 --- /dev/null +++ b/app/Filament/Resources/CharacteristicValueResource.php @@ -0,0 +1,703 @@ +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(CharacteristicValue::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('process_order', null); + $set('coil_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('process_order', null); + $set('coil_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('process_order', null); + $set('coil_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('process_order', null); + $set('coil_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('process_order') + ->label('Process Order') + ->reactive() + ->afterStateUpdated(function ($state, $set, callable $get) { + $plantId = $get('plant_id'); + $set('coil_number', null); + $set('observed_value', null); + $set('status', 'NotOk'); + $set('updated_by', Filament::auth()->user()?->name); + if (! $plantId) { + $set('process_order', null); + $set('poPlantError', 'Please select a plant first.'); + } + }) + ->required(), + Forms\Components\TextInput::make('coil_number') + ->label('Coil Number') + // ->reactive() + // ->afterStateUpdated(function ($state, $set, callable $get) { + // $plantId = $get('plant_id'); + // $set('status', 'NotOk'); + // if (! $plantId) { + // $set('poPlantError', 'Please select a plant first.'); + // } + // }) + // ->required(), + ->label('Coil Number') + ->default('0') + ->numeric() + ->reactive() + ->afterStateUpdated(function ($state, $set, callable $get, $livewire) { + $plantId = $get('plant_id'); + $processOrder = $get('process_order'); + // $coilNo = $get('coil_number'); + $set('poPlantError', null); + $set('observed_value', null); + $set('status', 'NotOk'); + $set('coilNumberError', null); + $set('updated_by', Filament::auth()->user()?->name); + if (! $plantId) { + $set('coil_number', null); + $set('poPlantError', 'Please select a plant first.'); + } elseif (! $processOrder) { + $set('coil_number', null); + } + // elseif ($coilNo || $coilNo == '0') { + // $existing = CharacteristicValue::where('plant_id', $plantId) + // ->where('process_order', $processOrder) + // ->where('coil_number', $coilNo) + // ->first(); + + // if ($existing && ! $get('id')) { + // // $set('coil_number', null); + // $set('coilNumberError', "Duplicate Coil : '{$coilNo}' found!"); + // } else { + // $set('coilNumberError', null); + // } + // } + }) + ->extraAttributes(fn ($get) => [ + 'class' => $get('coilNumberError') ? 'border-red-500' : '', + ]) + ->hint(fn ($get) => $get('coilNumberError') ? $get('coilNumberError') : null) + ->hintColor('danger') + ->rule(function (callable $get) { + return Rule::unique('characteristic_values', 'coil_number') + ->where('plant_id', $get('plant_id')) + ->where('process_order', $get('process_order')) + ->ignore($get('id')); // Ignore current record during updates + }) + ->required(), + Forms\Components\TextInput::make('observed_value') + ->label('Observed Value') + ->reactive() + ->afterStateUpdated(function ($state, $set, callable $get) { + $plantId = $get('plant_id'); + $itemId = $get('item_id'); + $lineId = $get('line_id'); + $machineId = $get('machine_id'); + $set('updated_by', Filament::auth()->user()?->name); + + if (! $plantId || ! $itemId || ! $lineId || ! $machineId) { + $set('observed_value', null); + $set('status', 'NotOk'); + + return; + } + + if (Str::length($state) <= 0 || ! is_numeric($state) || ! preg_match('/^\d+(\.\d+)?$/', $state) + ) { + $set('status', 'NotOk'); + + return; + } + + $specVal = ProductCharacteristicsMaster::where('plant_id', $plantId)->where('item_id', $itemId)->where('line_id', $lineId)->where('machine_id', $machineId)->first(); + if (! $specVal) { + $set('status', 'NotOk'); + + return; + } + + $lowLimit = $specVal?->lower ?? 0; + $uppLimit = $specVal?->upper ?? 0; + + if (Str::length($lowLimit) <= 0 || ! is_numeric($lowLimit) || ! preg_match('/^\d+(\.\d+)?$/', $lowLimit) + ) { + $set('status', 'NotOk'); + + return; + } elseif (Str::length($uppLimit) <= 0 || ! is_numeric($uppLimit) || ! preg_match('/^\d+(\.\d+)?$/', $uppLimit) + ) { + $set('status', 'NotOk'); + + return; + } + + if (($lowLimit == 0 && $uppLimit == 0) || ($uppLimit == 0)) { + $set('status', 'NotOk'); + + return; + } + + if ($lowLimit > $state || $uppLimit < $state) { + $set('status', 'NotOk'); + + return; + } + + $set('status', 'Ok'); + }) + ->required(), + Forms\Components\TextInput::make('status')// Select + ->label('Status') + // ->options([ + // 'Ok' => 'OK', + // 'NotOk' => 'Not Ok', + // ]) + ->reactive() + ->default('NotOk') + ->readOnly() + ->required(), + 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\TextInput::make('id') + ->hidden() + ->readOnly(), + ]); + } + + 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('process_order') + ->label('Process Order') + ->searchable() + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('coil_number') + ->label('Coil Number') + ->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)->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', + default => 'gray', + }) + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('created_at') + ->label('Created At') + ->alignCenter() + ->dateTime() + ->sortable(), + Tables\Columns\TextColumn::make('created_by') + ->label('Created By') + ->searchable() + ->alignCenter() + ->sortable(), + Tables\Columns\TextColumn::make('updated_at') + ->label('Updated At') + ->alignCenter() + ->dateTime() + ->sortable() + ->toggleable(isToggledHiddenByDefault: true), + Tables\Columns\TextColumn::make('updated_by') + ->label('Updated By') + ->alignCenter() + ->searchable() + ->sortable() + ->toggleable(isToggledHiddenByDefault: true), + Tables\Columns\TextColumn::make('deleted_at') + ->label('Deleted At') + ->alignCenter() + ->dateTime() + ->sortable() + ->toggleable(isToggledHiddenByDefault: true), + ]) + // ->filters([ + // Tables\Filters\TrashedFilter::make(), + // ]) + ->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('characteristicValues', 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('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('characteristicValues', function ($query) use ($plantId) { + if ($plantId) { + $query->where('plant_id', $plantId); + } + })->pluck('name', 'id'); + // return $plantId ? Item::where('plant_id', $plantId)->pluck('code', 'id') : []; + }) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $set('Item', null); + $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('characteristicValues', function ($query) use ($plantId) { + if ($plantId) { + $query->where('plant_id', $plantId); + } + })->pluck('code', 'id'); + // return $plantId ? Item::where('plant_id', $plantId)->pluck('code', 'id') : []; + }) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $set('process_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) || empty($lineId)) { + return []; + } + + return Machine::whereHas('characteristicValues', function ($query) use ($plantId, $lineId) { + if ($plantId && $lineId) { + $query->where('plant_id', $plantId)->where('line_id', $lineId); + } + })->pluck('work_center', 'id'); + // return $plantId ? Item::where('plant_id', $plantId)->pluck('code', 'id') : []; + }) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $set('process_order', null); + }), + TextInput::make('process_order') + ->label('Process Order') + ->placeholder('Enter Process Order'), + TextInput::make('coil_number') + ->label('Coil Number') + ->placeholder(placeholder: 'Enter Coil Number'), + Select::make('status') + ->label('Status') + ->options([ + 'Ok' => 'OK', + 'NotOk' => 'Not Ok', + ]), + DateTimePicker::make(name: 'created_from') + ->label('Created From') + ->placeholder(placeholder: 'Select From DateTime') + ->reactive() + ->native(false), + DateTimePicker::make('created_to') + ->label('Created To') + ->placeholder(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['Machine']) && empty($data['process_order']) && empty($data['coil_number']) && empty($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['process_order'])) { + $query->where('process_order', 'like', '%'.$data['process_order'].'%'); + } + + if (! empty($data['coil_number'])) { + $query->where('coil_number', 'like', '%'.$data['coil_number'].'%'); + } + + if (! empty($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: 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[] = 'Work Center: '.Machine::where('id', $data['Machine'])->value('work_center'); + } + + if (! empty($data['process_order'])) { + $indicators[] = 'Process Order: '.$data['process_order']; + } + + if (! empty($data['coil_number'])) { + $indicators[] = 'Coil Number: '.$data['coil_number']; + } + + if (! empty($data['status'])) { + $indicators[] = 'Status: '.$data['status']; + } + + 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() + ->importer(CharacteristicValueImporter::class) + ->label('Import Characteristic Value') + ->color('warning') + ->visible(function () { + return Filament::auth()->user()->can('view import characteristic value'); + }), + ExportAction::make() + ->exporter(CharacteristicValueExporter::class) + ->label('Export Characteristic Value') + ->color('warning') + ->visible(function () { + return Filament::auth()->user()->can('view export characteristic value'); + }), + ]); + } + + public static function getRelations(): array + { + return [ + // + ]; + } + + public static function getPages(): array + { + return [ + 'index' => Pages\ListCharacteristicValues::route('/'), + 'create' => Pages\CreateCharacteristicValue::route('/create'), + 'view' => Pages\ViewCharacteristicValue::route('/{record}'), + 'edit' => Pages\EditCharacteristicValue::route('/{record}/edit'), + ]; + } + + public static function getEloquentQuery(): Builder + { + return parent::getEloquentQuery() + ->withoutGlobalScopes([ + SoftDeletingScope::class, + ]); + } +} diff --git a/app/Filament/Resources/CharacteristicValueResource/Pages/CreateCharacteristicValue.php b/app/Filament/Resources/CharacteristicValueResource/Pages/CreateCharacteristicValue.php new file mode 100644 index 0000000..4ac8770 --- /dev/null +++ b/app/Filament/Resources/CharacteristicValueResource/Pages/CreateCharacteristicValue.php @@ -0,0 +1,12 @@ + Date: Sun, 20 Sep 2026 11:25:00 +0530 Subject: [PATCH 4/5] Added characteristic value importer and exporter --- .../Exports/CharacteristicValueExporter.php | 81 ++++ .../Imports/CharacteristicValueImporter.php | 372 ++++++++++++++++++ 2 files changed, 453 insertions(+) create mode 100644 app/Filament/Exports/CharacteristicValueExporter.php create mode 100644 app/Filament/Imports/CharacteristicValueImporter.php diff --git a/app/Filament/Exports/CharacteristicValueExporter.php b/app/Filament/Exports/CharacteristicValueExporter.php new file mode 100644 index 0000000..6a8779c --- /dev/null +++ b/app/Filament/Exports/CharacteristicValueExporter.php @@ -0,0 +1,81 @@ +label('NO') + ->state(function ($record) use (&$rowNumber) { + // Increment and return the row number + return ++$rowNumber; + }), + ExportColumn::make('plant.code') + ->label('PLANT CODE'), + ExportColumn::make('line.name') + ->label('LINE NAME'), + ExportColumn::make('item.code') + ->label('ITEM CODE'), + ExportColumn::make('item.description') + ->label('DESCRIPTION'), + ExportColumn::make('machine.work_center') + ->label('WORK CENTER'), + ExportColumn::make('process_order') + ->label('PROCESS ORDER'), + ExportColumn::make('coil_number') + ->label('COIL NUMBER'), + ExportColumn::make('status') + ->label('STATUS'), + ExportColumn::make('spec_value') + ->label('Spec. Value') + ->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) + ->first(); + + return $specVal?->lower . ' - ' . $specVal?->upper; + }), + ExportColumn::make('observed_value') + ->label('OBSERVED VALUE'), + ExportColumn::make('created_at') + ->label('CREATED AT'), + ExportColumn::make('created_by') + ->label('CREATED BY'), + ExportColumn::make('updated_at') + ->label('UPDATED AT'), + ExportColumn::make('updated_by') + ->label('UPDATED BY'), + ExportColumn::make('deleted_at') + ->enabledByDefault(false) + ->label('DELETED AT'), + + ]; + } + + public static function getCompletedNotificationBody(Export $export): string + { + $body = 'Your characteristic value 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/CharacteristicValueImporter.php b/app/Filament/Imports/CharacteristicValueImporter.php new file mode 100644 index 0000000..19cb4d0 --- /dev/null +++ b/app/Filament/Imports/CharacteristicValueImporter.php @@ -0,0 +1,372 @@ +requiredMapping() + ->exampleHeader('Plant Code') + ->example('1000') + ->label('Plant Code') + ->relationship(resolveUsing: 'code') + ->rules(['required']), + ImportColumn::make('line') + ->requiredMapping() + ->exampleHeader('Line Name') + ->example('4 inch pump line') + ->label('Line Name') + ->relationship(resolveUsing: 'name') + ->rules(['required']), + ImportColumn::make('item') + ->requiredMapping() + ->exampleHeader('Item Code') + ->example('123456') + ->label('Item Code') + ->relationship(resolveUsing: 'code') + ->rules(['required']), + ImportColumn::make('machine') + ->requiredMapping() + ->exampleHeader('Work Center') + ->example('RMGS09745') + ->label('Work Center') + ->relationship(resolveUsing: 'work_center') + ->rules(['required']), + ImportColumn::make('process_order') + ->requiredMapping() + ->exampleHeader('Process Order') + ->example('23455256352') + ->label('Process Order'), + ImportColumn::make('coil_number') + ->requiredMapping() + ->exampleHeader('Coil Number') + ->example('0') + ->label('Coil Number'), + ImportColumn::make('status') + ->requiredMapping() + ->exampleHeader('Status') + ->example('Ok') + ->label('Status'), + ImportColumn::make('observed_value') + ->requiredMapping() + ->exampleHeader('Observed Value') + ->example('RAW01234') + ->label('Observed Value'), + ImportColumn::make('created_by') + ->requiredMapping() + ->exampleHeader('Created By') + ->example('RAW01234') + ->label('Created By'), + ImportColumn::make('created_at') + ->requiredMapping() + ->exampleHeader('Created DateTime') + ->example('01-01-2025 08:00:00') + ->label('Created DateTime') + ->rules(['required']), + ImportColumn::make('updated_at') + ->requiredMapping() + ->exampleHeader('Updated DateTime') + ->example('01-01-2025 08:00:00') + ->label('Updated DateTime') + ->rules(['required']), + ]; + } + + public function resolveRecord(): ?CharacteristicValue + { + // return CharacteristicValue::firstOrNew([ + // // Update existing records, matching them by `$this->data['column_name']` + // 'email' => $this->data['email'], + // ]); + + $warnMsg = []; + $plantId = null; + $itemId = null; + $lineId = null; + $machineId = null; + // $itemAgainstPlant = null; + + $plantCode = $this->data['plant']; + $processOrder = trim($this->data['process_order'] ?? ''); + $iCode = trim($this->data['item']); + $workCenter = trim($this->data['machine']); + $lineName = trim($this->data['line']); + $coilNo = trim($this->data['coil_number']); + $obserVal = trim($this->data['observed_value']); + $status = trim($this->data['status']); + $createdBy = trim($this->data['created_by']); + + if ($plantCode == null || $plantCode == '') { + $warnMsg[] = 'Plant code cannot be empty'; + } elseif ($iCode == null || $iCode == '') {// process_order + $warnMsg[] = 'Item code cannot be empty'; + } elseif ($processOrder == null || $processOrder == '') {// + $warnMsg[] = 'Process Order cannot be empty'; + } elseif ($workCenter == null || $workCenter == '') { + $warnMsg[] = 'Work center cannot be empty'; + } elseif ($lineName == null || $lineName == '') { + $warnMsg[] = 'Line name cannot be empty'; + } elseif ($coilNo == null || $coilNo == '') { + $warnMsg[] = 'Coil number cannot be empty'; + } elseif ($obserVal == null || $obserVal == '') { + $warnMsg[] = 'Observed value cannot be empty'; + } elseif ($status == null || $status == '') { + $warnMsg[] = 'Status cannot be empty'; + } + + if (Str::length($plantCode) > 0 && (Str::length($plantCode) < 4 || ! is_numeric($plantCode) || ! preg_match('/^[1-9]\d{3,}$/', $plantCode))) { + $warnMsg[] = 'Invalid plant code found'; + } else { + $plant = Plant::where('code', $plantCode)->first(); + if (! $plant) { + $warnMsg[] = 'Plant not found'; + } else { + $plantId = $plant->id; + } + } + + if (Str::length($iCode) > 0 && (Str::length($iCode) < 6 || ! ctype_alnum($iCode))) { + $warnMsg[] = 'Invalid item code found'; + } else { + $itemCode = Item::where('code', $iCode)->first(); + if (! $itemCode) { + $warnMsg[] = 'Item code not found'; + } else { + if ($plantId) { + $itemCode = Item::where('code', $iCode)->where('plant_id', $plantId)->first(); + if (! $itemCode) { + $warnMsg[] = 'Item code not found for the given plant'; + } else { + $itemId = $itemCode->id; + } + } + } + } + + $lineExists = Line::where('name', $lineName)->first(); + if (! $lineExists) { + $warnMsg[] = 'Line name not found'; + } else { + if ($plantId) { + $lineAgainstPlant = Line::where('name', $lineName)->where('plant_id', $plantId)->first(); + if (! $lineAgainstPlant) { + $warnMsg[] = 'Line name not found for the given plant'; + } else { + $lineId = $lineAgainstPlant->id; + } + } + } + + $workCenterExist = Machine::where('work_center', $workCenter)->first(); + if (! $workCenterExist) { + $warnMsg[] = 'Work Center not found'; + } + + // $workCenterAgainstPlant = Machine::where('work_center', $workCenter) + // ->where('plant_id', $plantId) + // ->first(); + + // if (!$workCenterAgainstPlant) { + // $warnMsg[] = 'Work center not found for the given plant'; + // } else { + // $MachineId = $workCenterAgainstPlant->id; + // } + + if ($plantId != null && $lineId != null) { + $machineAgaPlantLine = Machine::where('plant_id', $plantId) + ->where('line_id', $lineId) + ->where('work_center', $workCenter) + ->first(); + + if (! $machineAgaPlantLine) { + $warnMsg[] = 'Work center not found for the given plant and line'; + } else { + $machineId = $machineAgaPlantLine->id; + } + } + + if (Str::length($coilNo) > 0 && ! is_numeric($coilNo)) { + $warnMsg[] = 'Coil number should contain only numeric values!'; + } + + if (Str::length($obserVal) > 0 && ! is_numeric($obserVal)) { + $warnMsg[] = 'Observed value should contain only numeric values!'; + } + + if (Str::length($status) > 0 && ! in_array($status, ['Ok', 'NotOk'], true)) { + $warnMsg[] = "Status must be either 'Ok' or 'NotOk'!"; + } + // else { + // if (Str::length($status) <= 0 || ! is_numeric($status) || ! preg_match('/^\d+(\.\d+)?$/', $status) + // ) { + // $status = 'NotOk'; + // } else { + // $specVal = ProductCharacteristicsMaster::where('plant_id', $plantId)->where('item_id', $itemId)->where('line_id', $lineId)->where('machine_id', $machineId)->first(); + // if (! $specVal) { + // $status = 'NotOk'; + // } + + // $lowLimit = $specVal?->lower ?? 0; + // $uppLimit = $specVal?->upper ?? 0; + + // if (Str::length($lowLimit) <= 0 || ! is_numeric($lowLimit) || ! preg_match('/^\d+(\.\d+)?$/', $lowLimit) + // ) { + // $status = 'NotOk'; + // } elseif (Str::length($uppLimit) <= 0 || ! is_numeric($uppLimit) || ! preg_match('/^\d+(\.\d+)?$/', $uppLimit) + // ) { + // $status = 'NotOk'; + // } + + // if (($lowLimit == 0 && $uppLimit == 0) || ($uppLimit == 0)) { + // $status = 'NotOk'; + // } + + // if ($lowLimit > $obserVal || $uppLimit < $obserVal) { + // $status = 'NotOk'; + // } + // $status = 'Ok'; + // } + // } + + if ($createdBy == null || $createdBy == '' || ! $createdBy) { + $warnMsg[] = 'Created By cannot be empty'; + } + + if ($plantId) { + $user = User::where('name', $createdBy)->first(); + + $userPlant = User::where('name', $createdBy)->where('plant_id', $plantId)->first(); + + if (! $user) { + $warnMsg[] = 'Created By user name not found!'; + } elseif (! $userPlant && ! $user->hasRole('Super Admin')) { + $warnMsg[] = "Created By user '{$createdBy}' not found for Plant '{$plantCode}'!"; + } elseif (! $user->hasRole(['Super Admin', 'Process Quality Manager', 'Process Manager', 'Process Supervisor', 'Process Employee'])) { + $warnMsg[] = 'Created By user does not have rights!'; + } + } + + if ($plantId && $processOrder) { + $existing = CharacteristicValue::where('plant_id', $plantId) + ->where('process_order', $processOrder) + ->where('coil_number', $coilNo) + ->first(); + + if ($existing) { + $warnMsg[] = "Coil number '{$coilNo}' already exists for Plant '{$plantCode}' and Process Order '{$processOrder}'."; + } + } + + if ($plant && $itemCode && $processOrder != '' && $processOrder != null) { + + $existingOrder = ProcessOrder::where('plant_id', $plantId) + ->where('process_order', $processOrder) + ->first(); + + if ($existingOrder && $existingOrder->item_id !== ($itemId ?? null)) { + $warnMsg[] = 'Same Process Order already exists for this Plant with a different Item Code'; + } + } + + $updatedBy = Filament::auth()->user()->name; // ?? 'Admin' + if (! $updatedBy) { + $warnMsg[] = 'Invalid updated by user name found'; + } + + $fromDate = $this->data['created_at']; + $toDate = $this->data['updated_at']; + + $formats = ['d-m-Y H:i', 'd-m-Y H:i:s']; // '07-05-2025 08:00' or '07-05-2025 08:00:00' + + $fdateTime = null; + $tdateTime = null; + // Try parsing with multiple formats + foreach ($formats as $format) { + try { + $fdateTime = Carbon::createFromFormat($format, $fromDate); + break; + } catch (\Exception $e) { + // $warnMsg[] = "Date format mismatch with format: $format"; + } + } + + foreach ($formats as $format) { + try { + $tdateTime = Carbon::createFromFormat($format, $toDate); + break; + } catch (\Exception $e) { + // $warnMsg[] = "Date format mismatch with format: $format"; + } + } + + if (! isset($fdateTime)) { + $warnMsg[] = "Invalid 'Created DateTime' format. Expected DD-MM-YYYY HH:MM:SS"; + } + if (! isset($tdateTime)) { + $warnMsg[] = "Invalid 'Updated DateTime' format. Expected DD-MM-YYYY HH:MM:SS"; + } + + if (isset($fdateTime) && isset($tdateTime)) { + if ($fdateTime->greaterThan($tdateTime)) { + $warnMsg[] = "'Created DataTime' is greater than 'Updated DateTime'."; + } + } + + if (! empty($warnMsg)) { + throw new RowImportFailedException(implode(', ', $warnMsg)); + } + + return CharacteristicValue::updateOrCreate([ + 'plant_id' => $plantId, + 'process_order' => $processOrder, + 'coil_number' => $coilNo, + ], + [ + 'item_id' => $itemId, + 'line_id' => $lineId, + 'machine_id' => $machineId, + 'status' => $status, + 'observed_value' => $obserVal, + 'created_by' => $createdBy, + 'updated_by' => $updatedBy, + 'created_at' => $fdateTime->format('Y-m-d H:i:s'), + 'updated_at' => $tdateTime->format('Y-m-d H:i:s'), + ]); + + // return null; + + // return new CharacteristicValue; + } + + public static function getCompletedNotificationBody(Import $import): string + { + $body = 'Your characteristic value 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; + } +} -- 2.49.1 From 4ccdf4de734e011f0de1923f341c6f2a811f08f8 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Sun, 20 Sep 2026 11:25:33 +0530 Subject: [PATCH 5/5] Added characteristic value policy file --- app/Policies/CharacteristicValuePolicy.php | 106 +++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 app/Policies/CharacteristicValuePolicy.php diff --git a/app/Policies/CharacteristicValuePolicy.php b/app/Policies/CharacteristicValuePolicy.php new file mode 100644 index 0000000..ce32f66 --- /dev/null +++ b/app/Policies/CharacteristicValuePolicy.php @@ -0,0 +1,106 @@ +checkPermissionTo('view-any CharacteristicValue'); + } + + /** + * Determine whether the user can view the model. + */ + public function view(User $user, CharacteristicValue $characteristicvalue): bool + { + return $user->checkPermissionTo('view CharacteristicValue'); + } + + /** + * Determine whether the user can create models. + */ + public function create(User $user): bool + { + return $user->checkPermissionTo('create CharacteristicValue'); + } + + /** + * Determine whether the user can update the model. + */ + public function update(User $user, CharacteristicValue $characteristicvalue): bool + { + return $user->checkPermissionTo('update CharacteristicValue'); + } + + /** + * Determine whether the user can delete the model. + */ + public function delete(User $user, CharacteristicValue $characteristicvalue): bool + { + return $user->checkPermissionTo('delete CharacteristicValue'); + } + + /** + * Determine whether the user can delete any models. + */ + public function deleteAny(User $user): bool + { + return $user->checkPermissionTo('delete-any CharacteristicValue'); + } + + /** + * Determine whether the user can restore the model. + */ + public function restore(User $user, CharacteristicValue $characteristicvalue): bool + { + return $user->checkPermissionTo('restore CharacteristicValue'); + } + + /** + * Determine whether the user can restore any models. + */ + public function restoreAny(User $user): bool + { + return $user->checkPermissionTo('restore-any CharacteristicValue'); + } + + /** + * Determine whether the user can replicate the model. + */ + public function replicate(User $user, CharacteristicValue $characteristicvalue): bool + { + return $user->checkPermissionTo('replicate CharacteristicValue'); + } + + /** + * Determine whether the user can reorder the models. + */ + public function reorder(User $user): bool + { + return $user->checkPermissionTo('reorder CharacteristicValue'); + } + + /** + * Determine whether the user can permanently delete the model. + */ + public function forceDelete(User $user, CharacteristicValue $characteristicvalue): bool + { + return $user->checkPermissionTo('force-delete CharacteristicValue'); + } + + /** + * Determine whether the user can permanently delete any models. + */ + public function forceDeleteAny(User $user): bool + { + return $user->checkPermissionTo('force-delete-any CharacteristicValue'); + } +} -- 2.49.1