From d5738a381df8216d630d3705bf3e6e91218babb4 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Mon, 24 Nov 2025 09:12:56 +0530 Subject: [PATCH] Added part validation mail trigger logic and image uploading --- .../Resources/QualityValidationResource.php | 828 +++++++++++++++--- 1 file changed, 707 insertions(+), 121 deletions(-) diff --git a/app/Filament/Resources/QualityValidationResource.php b/app/Filament/Resources/QualityValidationResource.php index 1f85792..da5e62d 100644 --- a/app/Filament/Resources/QualityValidationResource.php +++ b/app/Filament/Resources/QualityValidationResource.php @@ -6,6 +6,8 @@ use App\Filament\Exports\QualityValidationExporter; use App\Filament\Imports\QualityValidationImporter; use App\Filament\Resources\QualityValidationResource\Pages; use App\Filament\Resources\QualityValidationResource\RelationManagers; +use App\Mail\InvalidQualityMail; +use App\Models\AlertMailRule; use App\Models\Item; use App\Models\Line; use App\Models\Plant; @@ -32,7 +34,9 @@ use Illuminate\Database\Eloquent\SoftDeletingScope; use Filament\Tables\Filters\Filter; use Illuminate\Database\Eloquent\Model; use Illuminate\Validation\ValidationException; -use Notification; +use Filament\Notifications\Notification; +use Illuminate\Support\Facades\Mail; +use Storage; class QualityValidationResource extends Resource { @@ -42,10 +46,12 @@ class QualityValidationResource extends Resource protected static ?string $navigationGroup = 'Display'; public $isSubmitted = false; + public $data = []; public static function form(Form $form): Form { return $form + ->statePath('data') ->schema(components: [ Forms\Components\Select::make('plant_id') ->relationship('plant', 'name') @@ -59,12 +65,18 @@ class QualityValidationResource extends Resource $set('line_id', null); $set('production_order', null); + $set('part_validation_type_options', []); + $set('part_validation_type', null); + $set('show_validation_image', false); + $set('validation1_image_url', null); + $set('plant', $state); $pId = $get('plant_id'); if (!$pId) { $set('pqPlantError', 'Please select a plant first.'); - } else { + } + else { $set('pqPlantError', null); } @@ -167,21 +179,47 @@ class QualityValidationResource extends Resource ->default(Filament::auth()->user()->name), Forms\Components\Hidden::make('sap_msg_status') - ->default(null), + ->default(null), Forms\Components\Hidden::make('sap_msg_description') - ->default(null), + ->default(null), + + // Forms\Components\TextInput::make('name') + // ->required() + // ->autofocus() + // ->live(debounce: 600) + // ->afterStateUpdated(function ($state, callable $set, callable $get) { + // $set('email', $state . '@cripumps.com'); + // }) + // ->maxLength(255), + // Forms\Components\TextInput::make('email') + // // ->email() + // ->unique(ignoreRecord: true) + // ->required() + // ->readOnly() + // // ->rule(function (callable $get) { + // // return Rule::unique('users', 'email') + // // ->ignore($get('id')); // Ignore current record during updates + // // }) + // // ->dehydrated() + // //->prefix(fn ($get) => $get('name') ?? null) + // // ->suffix('@cripumps.com') + // ->maxLength(255), Forms\Components\TextInput::make('item_id') - ->label('Item Code') - ->placeholder('Scan the valid QR code') - ->reactive() - ->required() - ->autofocus() - ->live() - ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + ->label('Item Code') + ->placeholder('Scan the valid QR code') + ->reactive() + ->required() + ->autofocus() + ->live() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $pId = $get('line_id'); + $set('part_validation_type', null); + $set('show_validation_image', false); + $set('validation1_image_url', null); + if (!$pId) { $set('pqLineError', 'Please select a line.'); } else { @@ -193,6 +231,9 @@ class QualityValidationResource extends Resource { $set('prodOrdError', "Production Order can't be empty."); $set('production_order', null); + $set('part_validation_type', null); + $set('show_validation_image', false); + $set('validation1_image_url', null); } else { @@ -230,6 +271,10 @@ class QualityValidationResource extends Resource // // Check if QR format contains '|' if (strpos($state, '|') == false) { $set('validationError', 'Scan valid QR code. (Ex: Item_Code|Serial_Number)'); + $set('part_validation_type', null); + $set('show_validation_image', false); + $set('part_validation_type_options', []); + $set('validation1_image_url', null); return; } @@ -249,18 +294,34 @@ class QualityValidationResource extends Resource if (!ctype_alnum($iCode)) { $set('validationError', 'Item code must contain alpha-numeric values.'); + $set('part_validation_type', null); + $set('show_validation_image', false); + $set('part_validation_type_options', []); + $set('validation1_image_url', null); return; } else if (strlen($iCode) < 6) { $set('validationError', 'Item code must be at least 6 digits.'); + $set('part_validation_type', null); + $set('show_validation_image', false); + $set('part_validation_type_options', []); + $set('validation1_image_url', null); return; } else if (!ctype_alnum($sNumber)) { $set('validationError', 'Serial Number must contain alpha-numeric values.'); + $set('part_validation_type', null); + $set('show_validation_image', false); + $set('part_validation_type_options', []); + $set('validation1_image_url', null); return; } else if (strlen($sNumber) < 9) { $set('validationError', 'Serial Number must be at least 9 digits.'); + $set('part_validation_type', null); + $set('show_validation_image', false); + $set('part_validation_type_options', []); + $set('validation1_image_url', null); return; } // } @@ -284,11 +345,19 @@ class QualityValidationResource extends Resource if (strlen($itemCode) < 6) { $set('validationError', 'Item code must be at least 6 digits.'); + $set('part_validation_type', null); + $set('show_validation_image', false); + $set('part_validation_type_options', []); + $set('validation1_image_url', null); return; } else if (!ctype_alnum($itemCode)) { $set('validationError', 'Item Code should contain alpha-numeric values.'); $set('item_id', null); + $set('part_validation_type', null); + $set('show_validation_image', false); + $set('part_validation_type_options', []); + $set('validation1_image_url', null); return; } else if ($serialNumber == '') { @@ -297,11 +366,19 @@ class QualityValidationResource extends Resource } else if (strlen($serialNumber) < 9) { $set('validationError', 'Serial Number must be at least 9 digits.'); + $set('part_validation_type', null); + $set('show_validation_image', false); + $set('part_validation_type_options', []); + $set('validation1_image_url', null); return; } else if (!ctype_alnum($serialNumber)) { $set('validationError', 'Serial Number should contain alpha-numeric values.'); $set('item_id', null); + $set('part_validation_type', null); + $set('show_validation_image', false); + $set('part_validation_type_options', []); + $set('validation1_image_url', null); return; } else @@ -314,6 +391,10 @@ class QualityValidationResource extends Resource if (!$plantId) { $set('validationError', 'Please select a plant first.'); $set('item_id', null); + $set('part_validation_type', null); + $set('show_validation_image', false); + $set('part_validation_type_options', []); + $set('validation1_image_url', null); return; } // Check if the item exists for the selected plant @@ -327,6 +408,10 @@ class QualityValidationResource extends Resource $set('sticker_master_id', null); $set('uom', null); $set('serial_number', null); + $set('part_validation_type', null); + $set('show_validation_image', false); + $set('part_validation_type_options', []); + $set('validation1_image_url', null); return; } else @@ -336,6 +421,9 @@ class QualityValidationResource extends Resource $set('sticker_master_id', null); $set('uom', null); $set('serial_number', null); + $set('part_validation_type', null); + $set('show_validation_image', false); + $set('validation1_image_url', null); return; } else if(!is_numeric($get('production_order')) || !preg_match('/^[1-9][0-9]{6,13}$/', $get('production_order'))) @@ -345,6 +433,10 @@ class QualityValidationResource extends Resource $set('sticker_master_id', null); $set('uom', null); $set('serial_number', null); + $set('part_validation_type', null); + $set('show_validation_image', false); + $set('part_validation_type_options', []); + $set('validation1_image_url', null); return; } @@ -442,18 +534,260 @@ class QualityValidationResource extends Resource } $set('serial_number', $serialNumber); - }) + // Find item based on scanned code + $item = Item::where('code', $itemCode) + ->where('plant_id', $plantId)->first(); + if (!$item) { + $set('part_validation_type_options', []); + return; + } + + // Find sticker master + $sticker = StickerMaster::where('plant_id', $plantId) + ->where('item_id', $item->id) + ->first(); + + if (!$sticker) { + $set('part_validation_type_options', []); + return; + } + + // Build dynamic options + $options = []; + + if (!empty($sticker->part_validation1)) { + $options['part_validation1'] = 'Part Validation 1'; + } + if (!empty($sticker->part_validation2)) { + $options['part_validation2'] = 'Part Validation 2'; + } + if (!empty($sticker->part_validation3)) { + $options['part_validation3'] = 'Part Validation 3'; + } + if (!empty($sticker->part_validation4)) { + $options['part_validation4'] = 'Part Validation 4'; + } + + // Save options in hidden field + $set('part_validation_type_options', $options); + + }) ->extraAttributes(fn ($get) => [ 'class' => $get('validationError') ? 'border-red-500' : '', ]) ->extraAttributes([ 'onkeydown' => "if (event.key === 'Enter') { event.preventDefault(); return false; }", ]) - ->hint(fn ($get) => $get('validationError') ? $get('validationError') : null) ->hintColor('danger'), + // Forms\Components\Select::make('part_validation_type') + // ->reactive() + // ->label('Select Validation') + // ->options(function (callable $get) { + + // $plantId = $get('plant_id'); + // $itemId = $get('item_id'); + + // if (!$plantId || !$itemId) { + // return []; + // } + + // $sticker = StickerMaster::where('plant_id', $plantId) + // ->where('item_id', $itemId) + // ->first(); + + // if (!$sticker) { + // return []; + // } + + // $options = []; + + // if (!empty($sticker->part_validation1)) { + // $options['part_validation1'] = 'Part Validation 1'; + // } + + // if (!empty($sticker->part_validation2)) { + // $options['part_validation2'] = 'Part Validation 2'; + // } + + // if (!empty($sticker->part_validation3)) { + // $options['part_validation3'] = 'Part Validation 3'; + // } + + // if (!empty($sticker->part_validation4)) { + // $options['part_validation4'] = 'Part Validation 4'; + // } + + // // if (!empty($sticker->part_validation5)) { + // // $options['part_validation5'] = 'Part Validation 5'; + // // } + + // return $options; + // }) + // ->reactive() + // ->required(), + + Forms\Components\Hidden::make('part_validation_type_options') + ->default([]) + ->reactive() + ->dehydrated(false), + + Forms\Components\Hidden::make('show_validation_image') + ->reactive() + ->default(false), + + // Forms\Components\Select::make('part_validation_type') + // ->label('Select Validation') + // ->reactive() + // ->options(fn (callable $get) => $get('part_validation_type_options') ?? []) + // ->required() + // ->visible(fn ($get) => !empty($get('part_validation_type_options'))) + // ->afterStateUpdated(function ($state, callable $set) { + // // when user selects something new, hide the image + // $set('show_validation_image', false); + // $set('validation1_image_url', null); + // }), + + Forms\Components\Select::make('part_validation_type') + ->label('Select Validation') + ->reactive() + ->options(fn (callable $get) => $get('part_validation_type_options') ?? []) + ->required() + ->afterStateUpdated(function ($state, callable $set) { + // when user selects something new, hide the image + $set('show_validation_image', false); + $set('validation1_image_url', null); + }) + ->visible(fn ($get) => !empty($get('part_validation_type_options'))) + ->suffixAction( + Forms\Components\Actions\Action::make('toggleValidationImage') + ->icon(fn (callable $get) => $get('show_validation_image') ? 'heroicon-o-eye-slash' : 'heroicon-o-eye') + ->tooltip('View Validation Image') + ->action(function (callable $get, $set) { + + $currentState = $get('show_validation_image'); + $set('show_validation_image', !$currentState); + + if ($currentState == true) { + $set('validation1_image_url', null); + return; + } + + $partvalidationType = $get('part_validation_type'); + $plantId1 = $get('plant_id'); + + $plantCode1 = Plant::where('id', $plantId1)->value('code'); + + $itemState = $get('item_id'); + $parts = explode('|', $itemState); + $itemCode = trim($parts[0]); + + $itemId1 = Item::where('code', $itemCode) + ->where('plant_id', $plantId1) + ->first(); + + $itemId2 = $itemId1?->id; + + $sticker = StickerMaster::where('plant_id', $plantId1) + ->where('item_id', $itemId2) + ->first(); + + if (!$sticker) { + Notification::make() + ->title('Sticker Master Missing') + ->body('No Sticker Master found for selected Plant & Item.') + ->danger() + ->send(); + return; + } + + $value = $sticker->{$partvalidationType}; + $fileName = "{$value}.png"; + + $imageUrl = route('part.validation.image', [ + 'plant' => $plantCode1, + 'filename' => $fileName + ]); + + $set('validation1_image_url', $imageUrl); + }) + ), + + // Forms\Components\Actions::make([ + // Forms\Components\Actions\Action::make('openValidation') + // ->label('Open/Close') + // ->button() + // ->color('primary') + // ->action(function (callable $get, $set) { + + // $currentState = $get('show_validation_image'); + // $set('show_validation_image', !$currentState); + + // if ($currentState == true) { + // $set('validation1_image_url', null); + // return; + // } + + // $partvalidationType = $get('part_validation_type'); + // $plantId1 = $get('plant_id'); + + // $plantCode1 = Plant::where('id', $plantId1)->value('code'); + + + // $itemState = $get('item_id'); + + // $parts = explode('|', $itemState); + + // $itemCode = trim($parts[0]); + + // $itemId1 = Item::where('code', $itemCode) + // ->where('plant_id', $plantId1)->first(); + + // $itemId2 = $itemId1?->id; + + // $sticker = StickerMaster::where('plant_id', $plantId1) + // ->where('item_id', $itemId2) + // ->first(); + + // if (!$sticker) { + // Notification::make() + // ->title('Sticker Master Missing') + // ->body('No Sticker Master found for selected Plant & Item.') + // ->danger() + // ->send(); + // return; + // } + + // $value = $sticker->{$partvalidationType}; + + // $fileName = "{$value}.png"; + + // // Check existence + // $fullPath = storage_path("app/private/uploads/PartValidation/{$plantCode1}/{$fileName}"); + + // // Generate URL through custom route + // $imageUrl = route('part.validation.image', [ + // 'plant' => $plantCode1, + // 'filename' => $fileName + // ]); + + // // Pass to state (for modal/view) + // $set('validation1_image_url', $imageUrl); + + // }), + // ]) + // ->visible(fn ($get) => !empty($get('part_validation_type_options'))), + + Forms\Components\Hidden::make('validation1_image_url') + ->reactive(), + + Forms\Components\View::make('components.part-validation-error-icon') + ->statePath('validation1_image_url') // bind state directly + ->visible(fn ($get) => $get('show_validation_image') == true) + ->reactive(), + Forms\Components\Hidden::make('serial_number') ->required(), @@ -600,21 +934,21 @@ class QualityValidationResource extends Resource Forms\Components\Hidden::make('serial_number_pumpset'), Forms\Components\TextInput::make('serial_number_pumpset_qr') - ->label('Serial Number PumpSet') - ->reactive() - ->readOnly(fn (callable $get) => !$get('id')) - ->hidden(fn (callable $get) => !$get('serial_number_pumpset_qr_visible')) - ->default(''), + ->label('Serial Number PumpSet') + ->reactive() + ->readOnly(fn (callable $get) => !$get('id')) + ->hidden(fn (callable $get) => !$get('serial_number_pumpset_qr_visible')) + ->default(''), Forms\Components\Hidden::make('pack_slip_motor'), Forms\Components\TextInput::make('pack_slip_motor_qr') - ->label('Pack Slip Motor') - ->hidden(fn (callable $get) => !$get('pack_slip_motor_qr_visible')) - ->default('') - ->reactive() - ->required() - ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + ->label('Pack Slip Motor') + ->hidden(fn (callable $get) => !$get('pack_slip_motor_qr_visible')) + ->default('') + ->reactive() + ->required() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('pack_slip_motor_error', null); @@ -741,13 +1075,13 @@ class QualityValidationResource extends Resource Forms\Components\Hidden::make('pack_slip_pump'), - Forms\Components\TextInput::make('pack_slip_pump_qr') - ->label('Pack Slip Pump') - ->hidden(fn (callable $get) => !$get('pack_slip_pump_qr_visible')) - ->default('') - ->required() - ->reactive() - ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + Forms\Components\TextInput::make('pack_slip_pump_qr') + ->label('Pack Slip Pump') + ->hidden(fn (callable $get) => !$get('pack_slip_pump_qr_visible')) + ->default('') + ->required() + ->reactive() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('pack_slip_pump_error', null); @@ -871,12 +1205,12 @@ class QualityValidationResource extends Resource Forms\Components\Hidden::make('pack_slip_pumpset'), Forms\Components\TextInput::make('pack_slip_pumpset_qr') - ->label('Pack Slip PumpSet') - ->hidden(fn (callable $get) => !$get('pack_slip_pumpset_qr_visible')) - ->default('') - ->required() - ->reactive() - ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + ->label('Pack Slip PumpSet') + ->hidden(fn (callable $get) => !$get('pack_slip_pumpset_qr_visible')) + ->default('') + ->required() + ->reactive() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('pack_slip_pumpset_error', null); @@ -1002,12 +1336,12 @@ class QualityValidationResource extends Resource Forms\Components\Hidden::make('name_plate_motor'), Forms\Components\TextInput::make('name_plate_motor_qr') - ->label('Name Plate Motor') - ->hidden(fn (callable $get) => !$get('name_plate_motor_qr_visible')) - ->default('') - ->required() - ->reactive() - ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + ->label('Name Plate Motor') + ->hidden(fn (callable $get) => !$get('name_plate_motor_qr_visible')) + ->default('') + ->required() + ->reactive() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('name_plate_motor_error', null); @@ -1131,12 +1465,12 @@ class QualityValidationResource extends Resource Forms\Components\Hidden::make('name_plate_pump'), Forms\Components\TextInput::make('name_plate_pump_qr') - ->label('Name Plate Pump') - ->hidden(fn (callable $get) => !$get('name_plate_pump_qr_visible')) - ->default('') - ->required() - ->reactive() - ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + ->label('Name Plate Pump') + ->hidden(fn (callable $get) => !$get('name_plate_pump_qr_visible')) + ->default('') + ->required() + ->reactive() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('name_plate_pump_error', null); @@ -1260,12 +1594,12 @@ class QualityValidationResource extends Resource Forms\Components\Hidden::make('name_plate_pumpset'), Forms\Components\TextInput::make('name_plate_pumpset_qr') - ->label('Name Plate PumpSet') - ->hidden(fn (callable $get) => !$get('name_plate_pumpset_qr_visible')) - ->default('') - ->required() - ->reactive() - ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + ->label('Name Plate PumpSet') + ->hidden(fn (callable $get) => !$get('name_plate_pumpset_qr_visible')) + ->default('') + ->required() + ->reactive() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('name_plate_pumpset_error', null); @@ -1383,19 +1717,18 @@ class QualityValidationResource extends Resource ->extraAttributes(fn ($get) => [ 'class' => $get('name_plate_pumpset_error') ? 'border-red-500' : '', ]) - ->hint(fn ($get) => $get('name_plate_pumpset_error') ? $get('name_plate_pumpset_error') : null) - ->hintColor('danger'), + ->hint(fn ($get) => $get('name_plate_pumpset_error') ? $get('name_plate_pumpset_error') : null) + ->hintColor('danger'), Forms\Components\Hidden::make('tube_sticker_motor'), - Forms\Components\TextInput::make('tube_sticker_motor_qr') - ->label('Tube Sticker Motor') - ->hidden(fn (callable $get) => !$get('tube_sticker_motor_qr_visible')) - ->default('') - ->required() - ->reactive() - ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + ->label('Tube Sticker Motor') + ->hidden(fn (callable $get) => !$get('tube_sticker_motor_qr_visible')) + ->default('') + ->required() + ->reactive() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('tube_sticker_motor_error', null); @@ -1519,12 +1852,12 @@ class QualityValidationResource extends Resource Forms\Components\Hidden::make('tube_sticker_pump'), Forms\Components\TextInput::make('tube_sticker_pump_qr') - ->label('Tube Sticker Pump') - ->hidden(fn (callable $get) => !$get('tube_sticker_pump_qr_visible')) - ->default('') - ->required() - ->reactive() - ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + ->label('Tube Sticker Pump') + ->hidden(fn (callable $get) => !$get('tube_sticker_pump_qr_visible')) + ->default('') + ->required() + ->reactive() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('tube_sticker_pump_error', null); @@ -1648,12 +1981,12 @@ class QualityValidationResource extends Resource Forms\Components\Hidden::make('tube_sticker_pumpset'), Forms\Components\TextInput::make('tube_sticker_pumpset_qr') - ->label('Tube Sticker PumpSet') - ->hidden(fn (callable $get) => !$get('tube_sticker_pumpset_qr_visible')) - ->default('') - ->required() - ->reactive() - ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + ->label('Tube Sticker PumpSet') + ->hidden(fn (callable $get) => !$get('tube_sticker_pumpset_qr_visible')) + ->default('') + ->required() + ->reactive() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('tube_sticker_pumpset_error', null); @@ -1776,15 +2109,15 @@ class QualityValidationResource extends Resource ->hint(fn ($get) => $get('tube_sticker_pumpset_error') ? $get('tube_sticker_pumpset_error') : null) ->hintColor('danger'), - Forms\Components\Hidden::make('warranty_card'), + Forms\Components\Hidden::make('warranty_card'), - Forms\Components\TextInput::make('warranty_card_qr') - ->label('Warranty Card') - ->hidden(fn (callable $get) => !$get('warranty_card_qr_visible')) - ->default('') - ->required() - ->reactive() - ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + Forms\Components\TextInput::make('warranty_card_qr') + ->label('Warranty Card') + ->hidden(fn (callable $get) => !$get('warranty_card_qr_visible')) + ->default('') + ->required() + ->reactive() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('warranty_card_error', null); @@ -1906,12 +2239,19 @@ class QualityValidationResource extends Resource ->hint(fn ($get) => $get('warranty_card_error') ? $get('warranty_card_error') : null) ->hintColor('danger'), - Forms\Components\TextInput::make('part_validation1') - ->hidden(fn (callable $get) => !$get('part_validation1_visible')) - ->default('') - ->reactive() - ->required() - ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + Forms\Components\TextInput::make('part_validation1') + ->hidden(fn (callable $get) => !$get('part_validation1_visible')) + ->default('') + ->reactive() + ->required() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + + $mPorder = $get('production_order'); + + $mPlantId = $get('plant_id'); + + $plant = Plant::find($mPlantId); + $plantCodePart1 = $plant?->code; $stickerMasterId = $get('sticker_master_id'); if (!$stickerMasterId) { @@ -1937,7 +2277,37 @@ class QualityValidationResource extends Resource else { $set('part_validation1_error', "Invalid input for part validation 1."); + + //.Mail + + $mailData = \App\Filament\Resources\QualityValidationResource::getMailData($mPlantId); + + $mPlantName = $mailData['plant_name']; + $emails = $mailData['emails']; + + if (!empty($emails)) + { + //Mail::to($emails)->send(new InvalidSerialMail($serNo, $invoiceNumber, $mPlantName, $mInvoiceType)); + Mail::to($emails)->send( + new InvalidQualityMail($state, $mPorder, $mPlantName, 'InvalidPartNumber') + ); + } + else + { + \Log::warning("No recipients found for plant {$mPlantName}, module Serial, rule invalid_serial."); + } $set('part_validation1', null); + + $fileName = $expectedValue . ".png"; // or .jpg based on your file + $fullPath = storage_path("app/private/uploads/PartValidation/{$plantCodePart1}/{$fileName}"); + + // Generate URL through custom route + $imageUrl = route('part.validation.image', [ + 'plant' => $plantCodePart1, + 'filename' => $fileName + ]); + + $set('part_validation1_error_image', $imageUrl); return; } }) @@ -1947,12 +2317,27 @@ class QualityValidationResource extends Resource ->hint(fn ($get) => $get('part_validation1_error')) ->hintColor('danger'), - Forms\Components\TextInput::make('part_validation2') - ->hidden(fn (callable $get) => !$get('part_validation2_visible')) - ->default('') - ->required() - ->reactive() - ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + Forms\Components\Hidden::make('part_validation1_error_image') + ->default(null) + ->dehydrated(false), + + Forms\Components\View::make('components.part-validation1-error-icon') + ->visible(fn ($get) => $get('part_validation1_error') != null) + ->statePath('part_validation1_error_image') + ->reactive(), + + Forms\Components\TextInput::make('part_validation2') + ->hidden(fn (callable $get) => !$get('part_validation2_visible')) + ->default('') + ->required() + ->reactive() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + + $mPorder = $get('production_order'); + + $mPlantId = $get('plant_id'); + $plant = Plant::find($mPlantId); + $plantCodePart2 = $plant?->code; $stickerMasterId = $get('sticker_master_id'); if (!$stickerMasterId) { @@ -1978,30 +2363,82 @@ class QualityValidationResource extends Resource else { $set('part_validation2_error', "Invalid input for part validation 2."); + $mailData = \App\Filament\Resources\QualityValidationResource::getMailData($mPlantId); + + $mPlantName = $mailData['plant_name']; + $emails = $mailData['emails']; + + if (!empty($emails)) + { + //Mail::to($emails)->send(new InvalidSerialMail($serNo, $invoiceNumber, $mPlantName, $mInvoiceType)); + Mail::to($emails)->send( + new InvalidQualityMail($state, $mPorder, $mPlantName, 'InvalidPartNumber2') + ); + } + else + { + \Log::warning("No recipients found for plant {$mPlantName}, module Serial, rule invalid_serial."); + } $set('part_validation2', null); + + $fileName = $expectedValue . ".png"; // or .jpg based on your file + $fullPath = storage_path("app/private/uploads/PartValidation/{$plantCodePart2}/{$fileName}"); + + // if (!file_exists($fullPath)) { + // Notification::make() + // ->title('Image Not Found') + // ->body($fileName) + // ->danger() + // ->send(); + // return; + // } + + // Generate URL through custom route + $imageUrl = route('part.validation.image', [ + 'plant' => $plantCodePart2, + 'filename' => $fileName + ]); + + $set('part_validation2_error_image', $imageUrl); return; } }) - ->extraAttributes(fn ($get) => [ 'class' => $get('part_validation2_error') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('part_validation2_error')) ->hintColor('danger'), - Forms\Components\TextInput::make('part_validation3') - ->hidden(fn (callable $get) => !$get('part_validation3_visible')) - ->default('') - ->required() - ->reactive() - ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + Forms\Components\Hidden::make('part_validation2_error_image') + ->default(null) + ->dehydrated(false), + + Forms\Components\View::make('components.part-validation1-error-icon') + ->visible(fn ($get) => $get('part_validation2_error') != null) + ->statePath('part_validation2_error_image') + ->reactive(), + + Forms\Components\TextInput::make('part_validation3') + ->hidden(fn (callable $get) => !$get('part_validation3_visible')) + ->default('') + ->required() + ->reactive() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $stickerMasterId = $get('sticker_master_id'); + + $mPorder = $get('production_order'); + + $mPlantId = $get('plant_id'); + $plant = Plant::find($mPlantId); + $plantCodePart3 = $plant?->code; + if (!$stickerMasterId) { return; } $stickerMaster = StickerMaster::find($stickerMasterId); - if (!$stickerMaster) { + if (!$stickerMaster) + { return; } @@ -2019,24 +2456,73 @@ class QualityValidationResource extends Resource else { $set('part_validation3_error', "Invalid input for part validation 3."); + $mailData = \App\Filament\Resources\QualityValidationResource::getMailData($mPlantId); + + $mPlantName = $mailData['plant_name']; + $emails = $mailData['emails']; + + if (!empty($emails)) + { + //Mail::to($emails)->send(new InvalidSerialMail($serNo, $invoiceNumber, $mPlantName, $mInvoiceType)); + Mail::to($emails)->send( + new InvalidQualityMail($state, $mPorder, $mPlantName, 'InvalidPartNumber3') + ); + } + else + { + \Log::warning("No recipients found for plant {$mPlantName}, module Serial, rule invalid_serial."); + } $set('part_validation3', null); + $fileName = $expectedValue . ".png"; // or .jpg based on your file + //$fullPath = storage_path("app/private/uploads/PartValidation/{$plantCodePart3}/{$fileName}"); + + // if (!file_exists($fullPath)) { + // Notification::make() + // ->title('Image Not Found') + // ->body($fileName) + // ->danger() + // ->send(); + // return; + // } + + // Generate URL through custom route + $imageUrl = route('part.validation.image', [ + 'plant' => $plantCodePart3, + 'filename' => $fileName + ]); + + $set('part_validation2_error_image', $imageUrl); return; } }) - ->extraAttributes(fn ($get) => [ 'class' => $get('part_validation3_error') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('part_validation3_error')) ->hintColor('danger'), - Forms\Components\TextInput::make('part_validation4') - ->hidden(fn (callable $get) => !$get('part_validation4_visible')) - ->default('') - ->required() - ->reactive() - ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + Forms\Components\Hidden::make('part_validation3_error_image') + ->default(null) + ->dehydrated(false), + + Forms\Components\View::make('components.part-validation1-error-icon') + ->visible(fn ($get) => $get('part_validation3_error') != null) + ->statePath('part_validation3_error_image') + ->reactive(), + + Forms\Components\TextInput::make('part_validation4') + ->hidden(fn (callable $get) => !$get('part_validation4_visible')) + ->default('') + ->required() + ->reactive() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $stickerMasterId = $get('sticker_master_id'); + $mPorder = $get('production_order'); + + $mPlantId = $get('plant_id'); + $plant = Plant::find($mPlantId); + $plantCodePart4 = $plant?->code; + if (!$stickerMasterId) { return; } @@ -2061,25 +2547,63 @@ class QualityValidationResource extends Resource else { $set('part_validation4_error', "Invalid input for part validation 4."); + $mailData = \App\Filament\Resources\QualityValidationResource::getMailData($mPlantId); + + $mPlantName = $mailData['plant_name']; + $emails = $mailData['emails']; + + if (!empty($emails)) + { + //Mail::to($emails)->send(new InvalidSerialMail($serNo, $invoiceNumber, $mPlantName, $mInvoiceType)); + Mail::to($emails)->send( + new InvalidQualityMail($state, $mPorder, $mPlantName, 'InvalidPartNumber4') + ); + } + else + { + \Log::warning("No recipients found for plant {$mPlantName}, module Serial, rule invalid_serial."); + } $set('part_validation4', null); + $fileName = $expectedValue . ".png"; // or .jpg based on your file + $fullPath = storage_path("app/private/uploads/PartValidation/{$plantCodePart4}/{$fileName}"); + + // Generate URL through custom route + $imageUrl = route('part.validation.image', [ + 'plant' => $plantCodePart4, + 'filename' => $fileName + ]); + + $set('part_validation2_error_image', $imageUrl); + return; } - }) - ->extraAttributes(fn ($get) => [ 'class' => $get('part_validation4_error') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('part_validation4_error')) ->hintColor('danger'), - Forms\Components\TextInput::make('part_validation5') - ->hidden(fn (callable $get) => !$get('part_validation5_visible')) - ->default('') - ->required() - ->reactive() - ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + Forms\Components\Hidden::make('part_validation4_error_image') + ->default(null) + ->dehydrated(false), + + Forms\Components\View::make('components.part-validation1-error-icon') + ->visible(fn ($get) => $get('part_validation4_error') != null) + ->statePath('part_validation4_error_image') + ->reactive(), + + Forms\Components\TextInput::make('part_validation5') + ->hidden(fn (callable $get) => !$get('part_validation5_visible')) + ->default('') + ->required() + ->reactive() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $stickerMasterId = $get('sticker_master_id'); + + $mPorder = $get('production_order'); + $mPlantId = $get('plant_id'); + if (!$stickerMasterId) { return; } @@ -2146,24 +2670,86 @@ class QualityValidationResource extends Resource else { $set('part_validation5_error', "Invalid input for part validation 5."); + $mailData = \App\Filament\Resources\QualityValidationResource::getMailData($mPlantId); + + $mPlantName = $mailData['plant_name']; + $emails = $mailData['emails']; + + if (!empty($emails)) + { + //Mail::to($emails)->send(new InvalidSerialMail($serNo, $invoiceNumber, $mPlantName, $mInvoiceType)); + Mail::to($emails)->send( + new InvalidQualityMail($state, $mPorder, $mPlantName, 'InvalidPartNumber5') + ); + } + else + { + \Log::warning("No recipients found for plant {$mPlantName}, module Serial, rule invalid_serial."); + } $set('part_validation5', null); return; } }) - ->extraAttributes(fn ($get) => [ 'class' => $get('part_validation5_error') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('part_validation5_error')) ->hintColor('danger'), - Forms\Components\TextInput::make('id') + Forms\Components\TextInput::make('id') ->hidden() ->readOnly(), ]); } + public static function getMailData($plantId) + { + + $mPlantName = Plant::where('id', $plantId)->value('name'); + + $emails = AlertMailRule::where('plant', $plantId) + ->where('module', 'InvoiceValidation') + ->where('rule_name', 'InvoiceMail') + ->where(fn ($q) => $q->whereNull('schedule_type')->orWhere('schedule_type', '')) + ->pluck('email') + ->toArray(); + + return [ + 'plant_id' => $plantId, + 'plant_name' => $mPlantName, + 'emails' => $emails, + ]; + } + + + // public function getMail(){ + // //$plantId = $this->form->getState()['plant_id']; + // $plantId = $this->data['plant_id'] ?? null; + // //$this->plantId = $plantId; + // dd($plantId); + // $mInvoiceType = 'Serial'; + // $mPlantName = Plant::where('id', $plantId)->value('name'); + // $emails = AlertMailRule::where('plant', $plantId) + // //->where('plant', $plantName) + // ->where('module', 'InvoiceValidation') + // ->where('rule_name', 'InvoiceMail') + // ->where(function ($query) { + // $query->whereNull('schedule_type') + // ->orWhere('schedule_type', ''); + // }) + // ->pluck('email') + // ->toArray(); + + // return [ + // 'plant_id' => $plantId, + // 'plant_name' => $mPlantName, + // 'invoice_type' => $mInvoiceType, + // 'emails' => $emails, + // ]; + // } + + public static function table(Table $table): Table { return $table