Merge pull request 'ranjith-dev' (#879) from ranjith-dev into master
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 26s

Reviewed-on: #879
This commit was merged in pull request #879.
This commit is contained in:
2026-08-12 03:46:16 +00:00
8 changed files with 743 additions and 0 deletions

View File

@@ -0,0 +1,165 @@
<?php
namespace App\Filament\Resources;
use App\Filament\Resources\StickerValidationResource\Pages;
use App\Filament\Resources\StickerValidationResource\RelationManagers;
use App\Models\Machine;
use App\Models\StickerValidation;
use Filament\Facades\Filament;
use Filament\Forms;
use Filament\Forms\Components\Section;
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;
class StickerValidationResource extends Resource
{
protected static ?string $model = StickerValidation::class;
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
protected static ?string $navigationGroup = 'Customized Sticker Printing';
public static function form(Form $form): Form
{
return $form
->schema([
Section::make('')
->schema([
Forms\Components\Select::make('plant_id')
->label('Plant')
->reactive()
->relationship('plant', 'name')
->required(),
Forms\Components\Select::make('machine_id')
->label('Work Center')
->required()
->reactive()
->options(function (callable $get) {
$plantId = $get('plant_id');
if (empty($plantId)) {
return [];
}
return Machine::where('plant_id', $plantId)->pluck('work_center', 'id');
})
->searchable(),
Forms\Components\TextInput::make('production_order')
->label('Production Order')
->reactive()
->extraAttributes([
'id' => 'production_order_input',
'x-data' => '{ value: "" }',
'x-model' => 'value',
'wire:keydown.enter.prevent' => 'processProOrder(value)',
]),
Forms\Components\TextInput::make('serial_number')
->label('Serial Number')
->reactive()
->extraAttributes([
'id' => 'serial_number_input',
'x-data' => '{ value: "" }',
'x-model' => 'value',
'wire:keydown.enter.prevent' => 'processSno(value)',
]),
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),
])
->columns(4),
]);
}
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.code')
->label('Plant')
->alignCenter()
->sortable(),
Tables\Columns\TextColumn::make('machine.work_center')
->label('Work Center')
->alignCenter()
->sortable(),
Tables\Columns\TextColumn::make('production_order')
->label('Production Order')
->alignCenter()
->sortable(),
Tables\Columns\TextColumn::make('serial_number')
->label('Serial Number')
->alignCenter()
->sortable(),
Tables\Columns\TextColumn::make('status')
->label('Status')
->alignCenter()
->sortable(),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('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(),
]),
]);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListStickerValidations::route('/'),
'create' => Pages\CreateStickerValidation::route('/create'),
'view' => Pages\ViewStickerValidation::route('/{record}'),
'edit' => Pages\EditStickerValidation::route('/{record}/edit'),
];
}
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}
}

View File

@@ -0,0 +1,336 @@
<?php
namespace App\Filament\Resources\StickerValidationResource\Pages;
use App\Filament\Resources\StickerValidationResource;
use App\Models\Item;
use App\Models\ItemCharacteristic;
use App\Models\ProductionQuantity;
use App\Models\StickerDetail;
use App\Models\StickerMappingMaster;
use App\Models\StickerStructureDetail;
use App\Models\StickerValidation;
use App\Services\StickerPdfService;
use Filament\Actions;
use Filament\Facades\Filament;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\CreateRecord;
class CreateStickerValidation extends CreateRecord
{
protected static string $resource = StickerValidationResource::class;
protected static string $view = 'filament.resources.sticker-validation-resource.create-sticker-validation';
public $ref_number;
public $plantId;
public $workCenter;
public $serNo;
public function getFormActions(): array
{
return [
$this->getCancelFormAction(),
];
}
protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('create');
}
public function processProOrder($value)
{
$plantId = $this->form->getState()['plant_id'];
$this->plantId = $plantId;
$this->ref_number = $value;
$this->dispatch('refreshEmptySticker', $plantId, $value);
$this->dispatch('focus-serial-number');
}
public function processSno($serNo)
{
$plantId = $this->form->getState()['plant_id'];
$this->plantId = $plantId;
$workCenter = $this->form->getState()['machine_id'];
$this->workCenter = $workCenter;
$this->ref_number = $this->form->getState()['production_order'];
$this->serNo = $serNo;
$user = Filament::auth()->user();
$operatorName = $user->name;
if (! preg_match('/^([a-zA-Z0-9]{6,})\|([1-9][a-zA-Z0-9]{8,})\|?$/', $this->serNo, $matches)) {
Notification::make()
->danger()
->title('Invalid Serial QR Format')
->body('Scan valid Serial QR code proceed!<br>Sample formats are:<br>123456|1234567890123| OR 123456|1234567890123')
->seconds(3)
->send();
$this->dispatch('playWarnSound');
$this->form->fill([
'plant_id' => $this->plantId,
'machine_id' => $this->workCenter,
'production_order' => $this->ref_number,
'serial_number' => null,
]);
$this->dispatch('focus-serial-number');
return;
}
else {
$itemCode = $matches[1];
$serialNumber = $matches[2];
$recFound = ProductionQuantity::where('plant_id', $this->plantId)
->where('production_order', $this->ref_number)
->where('serial_number', $serialNumber)
->first();
if(!$recFound){
Notification::make()
->danger()
->title('Unknown Serial Number')
->body("Scanned serial number '$serialNumber' not found for the given plant and production order")
->seconds(3)
->send();
$this->form->fill([
'plant_id' => $this->plantId,
'machine_id' => $this->workCenter,
'production_order' => $this->ref_number,
'serial_number' => null,
]);
return;
}
$duplicate = StickerValidation::where('plant_id', $this->plantId)
->where('production_order', $this->ref_number)
->where('serial_number', $serialNumber)
->first();
if ($duplicate) {
Notification::make()
->danger()
->title('Duplicate Serial Number')
->body("Serial number $serialNumber already exists for this plant and production order!")
->seconds(3)
->send();
$this->form->fill([
'plant_id' => $this->plantId,
'machine_id' => $this->workCenter,
'production_order' => $this->ref_number,
'serial_number' => null,
]);
return;
}
$itemC = Item::where('code', $itemCode)
->where('plant_id',$this->plantId)
->first();
$itemId = $itemC->id;
$item = ItemCharacteristic::where('item_id', $itemId)
->where('plant_id',$this->plantId)
->first();
$itemI = $item->id;
$mapping = StickerMappingMaster::where('plant_id', $this->plantId)
->where('item_characteristic_id', $itemI)
->first();
if (!$mapping) {
Notification::make()
->danger()
->title('Sticker Mapping Not Found')
->body("No sticker mapping found for this item and plant.")
->send();
return;
}
$stickers = [];
for ($i = 1; $i <= 8; $i++) {
$machineColumn = "sticker{$i}_machine_id";
$ipColumn = "sticker{$i}_print_ip";
$stickerColumn = "sticker_structure{$i}_id";
$itemColumn = "item_characteristic_id";
if (
!empty($mapping->$machineColumn) &&
!empty($mapping->$stickerColumn)
) {
$stickers[] = [
'machine_id' => $mapping->$machineColumn,
'sticker_id' => $mapping->$stickerColumn,
'item_characteristic' => $mapping->$itemColumn,
'print_ip' => $mapping->$ipColumn,
];
}
}
if (empty($stickers)) {
Notification::make()
->danger()
->title('No Sticker Configuration Found')
->body('No sticker and machine mappings configured for this item and plant.')
->send();
return;
}
StickerValidation::create([
'plant_id' => $this->plantId,
'machine_id' => $this->workCenter,
'production_order' => $this->ref_number ?? null,
'serial_number' => $serialNumber,
'status' => 'Printed',
// 'sticker_id' => $matchedSticker,
'created_by' => $operatorName,
'created_at' => now(),
'updated_at' => now(),
]);
Notification::make()
->success()
->title('Sticker Recorded')
->body("Item: $itemCode, Serial: $serialNumber recorded successfully!")
->seconds(3)
->send();
$this->form->fill([
'plant_id' => $this->plantId,
'machine_id' => $this->workCenter,
'production_order' => $this->ref_number,
'serial_number' => null,
]);
$this->dispatch('refreshEmptySticker', $plantId, $this->ref_number);
foreach ($stickers as $sticker)
{
\Log::info("Looking up printer for IP: " . $sticker['print_ip']);
$printerName = $this->getCupsPrinterNameByIp($sticker['print_ip']);
\Log::info("Found printer: " . ($printerName ?? 'NULL'));
if (! $printerName) {
Notification::make()
->danger()
->title('Printer Not Found')
->body("No CUPS printer configured for IP: {$sticker['print_ip']}")
->send();
return;
}
$structure = StickerStructureDetail::findOrFail($sticker['sticker_id']);
$itemCharacteristic = ItemCharacteristic::where('plant_id', $this->plantId)
->where('id', $sticker['item_characteristic'])
->firstOrFail();
$dynamicElements = StickerDetail::where(
'sticker_structure_detail_id',
$structure->id
)->where('element_type', 'Dynamic')->get();
$pdfContent = (new StickerPdfService())->generatePdf1(
$structure->sticker_id,
$dynamicElements,
$itemCharacteristic,
$serialNumber,
$serNo
);
$tempPdfPath = storage_path('app/temp_sticker_' . uniqid() . '.pdf');
file_put_contents($tempPdfPath, $pdfContent);
exec(
"lp -d " . escapeshellarg($printerName) . " " . escapeshellarg($tempPdfPath),
$output,
$status
);
\Log::info("LP Output:", $output);
\Log::info("LP Status: " . $status);
if ($status != 0) {
Notification::make()
->danger()
->title('Print Failed')
->body("CUPS error while printing.")
->send();
if (file_exists($tempPdfPath)) {
unlink($tempPdfPath);
}
return;
}
if (file_exists($tempPdfPath)) {
unlink($tempPdfPath);
}
}
Notification::make()
->success()
->title('Sticker Printed')
->body("Sticker for Serial Number: $serialNumber printed successfully!")
->seconds(3)
->send();
// [$itemCode, $serialNumber] = explode('|', $serNo);
// $this->dispatch('open-sticker-pdf', [
// 'url' => url("/sticker/pdf/{$itemCode}/{$serialNumber}/$this->plantId/$this->ref_number")
// ]);
}
}
protected function getCupsPrinterNameByIp(string $ip): ?string
{
exec('lpstat -v 2>&1', $output, $status); //locak
// exec('lpstat -h cups:631 -v 2>&1', $output, $status);
if ($status != 0 || empty($output)) {
return null;
}
foreach ($output as $line){
$parts = explode(':', $line, 2);
if (count($parts) < 2) continue;
$printerName = trim(str_replace('device for', '', $parts[0]));
$deviceUri = trim($parts[1]);
if (str_contains($deviceUri, $ip)) {
return $printerName;
}
}
return null;
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace App\Filament\Resources\StickerValidationResource\Pages;
use App\Filament\Resources\StickerValidationResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
class EditStickerValidation extends EditRecord
{
protected static string $resource = StickerValidationResource::class;
protected function getHeaderActions(): array
{
return [
Actions\ViewAction::make(),
Actions\DeleteAction::make(),
Actions\ForceDeleteAction::make(),
Actions\RestoreAction::make(),
];
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Filament\Resources\StickerValidationResource\Pages;
use App\Filament\Resources\StickerValidationResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
class ListStickerValidations extends ListRecords
{
protected static string $resource = StickerValidationResource::class;
protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Filament\Resources\StickerValidationResource\Pages;
use App\Filament\Resources\StickerValidationResource;
use Filament\Actions;
use Filament\Resources\Pages\ViewRecord;
class ViewStickerValidation extends ViewRecord
{
protected static string $resource = StickerValidationResource::class;
protected function getHeaderActions(): array
{
return [
Actions\EditAction::make(),
];
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class StickerValidation extends Model
{
use SoftDeletes;
protected $fillable = [
'plant_id',
'machine_id',
'sticker_id',
'production_order',
'serial_number',
'status',
'created_by',
'updated_by',
'deleted_at',
];
public function plant()
{
return $this->belongsTo(Plant::class);
}
public function machine()
{
return $this->belongsTo(Machine::class);
}
}

View File

@@ -0,0 +1,106 @@
<?php
namespace App\Policies;
use Illuminate\Auth\Access\Response;
use App\Models\StickerValidation;
use App\Models\User;
class StickerValidationPolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
return $user->checkPermissionTo('view-any StickerValidation');
}
/**
* Determine whether the user can view the model.
*/
public function view(User $user, StickerValidation $stickervalidation): bool
{
return $user->checkPermissionTo('view StickerValidation');
}
/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
return $user->checkPermissionTo('create StickerValidation');
}
/**
* Determine whether the user can update the model.
*/
public function update(User $user, StickerValidation $stickervalidation): bool
{
return $user->checkPermissionTo('update StickerValidation');
}
/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, StickerValidation $stickervalidation): bool
{
return $user->checkPermissionTo('delete StickerValidation');
}
/**
* Determine whether the user can delete any models.
*/
public function deleteAny(User $user): bool
{
return $user->checkPermissionTo('delete-any StickerValidation');
}
/**
* Determine whether the user can restore the model.
*/
public function restore(User $user, StickerValidation $stickervalidation): bool
{
return $user->checkPermissionTo('restore StickerValidation');
}
/**
* Determine whether the user can restore any models.
*/
public function restoreAny(User $user): bool
{
return $user->checkPermissionTo('restore-any StickerValidation');
}
/**
* Determine whether the user can replicate the model.
*/
public function replicate(User $user, StickerValidation $stickervalidation): bool
{
return $user->checkPermissionTo('replicate StickerValidation');
}
/**
* Determine whether the user can reorder the models.
*/
public function reorder(User $user): bool
{
return $user->checkPermissionTo('reorder StickerValidation');
}
/**
* Determine whether the user can permanently delete the model.
*/
public function forceDelete(User $user, StickerValidation $stickervalidation): bool
{
return $user->checkPermissionTo('force-delete StickerValidation');
}
/**
* Determine whether the user can permanently delete any models.
*/
public function forceDeleteAny(User $user): bool
{
return $user->checkPermissionTo('force-delete-any StickerValidation');
}
}

View File

@@ -0,0 +1,43 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
$sql = <<<'SQL'
CREATE TABLE sticker_validations (
id BIGINT GENERATED always AS IDENTITY PRIMARY KEY,
plant_id BIGINT NOT NULL,
machine_id BIGINT NOT NULL,
production_order TEXT DEFAULT NULL,
serial_number TEXT DEFAULT NULL,
status TEXT DEFAULT NULL,
sticker_id TEXT DEFAULT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
created_by TEXT DEFAULT NULL,
updated_by TEXT DEFAULT NULL,
deleted_at TIMESTAMP,
FOREIGN KEY (plant_id) REFERENCES plants (id),
FOREIGN KEY (machine_id) REFERENCES machines (id)
);
SQL;
DB::statement($sql);
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('sticker_validations');
}
};