From 4defe20dab0fbb750fa8fe20aca597f15037fdb0 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Fri, 18 Sep 2026 10:36:27 +0530 Subject: [PATCH] Added test certificate rules in sticker pdf service --- app/Services/StickerPdfService.php | 199 +++++++++++++++++++++++++++-- 1 file changed, 189 insertions(+), 10 deletions(-) diff --git a/app/Services/StickerPdfService.php b/app/Services/StickerPdfService.php index a00b584..50c470d 100644 --- a/app/Services/StickerPdfService.php +++ b/app/Services/StickerPdfService.php @@ -9,6 +9,7 @@ use App\Models\StickerDetail; use App\Models\StickerMappingMaster; use App\Models\StickerStructureDetail; use App\Models\StickerValidation; +use App\Models\TestApprovalMapping; use Illuminate\Http\Response; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Log; @@ -1042,7 +1043,7 @@ class StickerPdfService } } - public function generateTemplate(string $stickerId,Collection $dynamicElements,?ItemCharacteristic $itemCharacteristic,$itemCode,$plantId) + public function generateTemplate(string $stickerId,Collection $dynamicElements,?ItemCharacteristic $itemCharacteristic,$itemCode,$plantId, $machineId) { $dynamicValueMap = []; @@ -1070,6 +1071,14 @@ class StickerPdfService $elements = StickerDetail::where('sticker_structure_detail_id',$structure->id)->get(); + $machineCharacteristic = TestApprovalMapping::where('plant_id', $plantId)->where('machine_id', $machineId)->first(); + + $signOffNames = [ + 'tested_by' => $machineCharacteristic?->tested_by, + 'verified_by' => $machineCharacteristic?->verified_by, + 'approved_by' => $machineCharacteristic?->approved_by, + ]; + $templateElements = []; foreach ($elements as $row) { @@ -1143,6 +1152,27 @@ class StickerPdfService $imagePath = null; + $signOffTypes = ['tested_by', 'verified_by', 'approved_by']; + + if (in_array($row->characteristics_type, $signOffTypes)) { + + $column = $row->characteristics_type; + $name = null; + + if ($machineCharacteristic && $column && Schema::hasColumn('test_approval_mappings', $column)) { + $name = $machineCharacteristic->{$column}; + } + + if (!empty($name)) { + $imageName = strtolower($name) . '.png'; + $candidatePath = public_path('images/' . ltrim($imageName, '/')); + + if (file_exists($candidatePath)) { + $imagePath = $candidatePath; + } + } + } + if ($row->element_type == 'Dynamic' && isset($dynamicValueMap[$row->id]) && !empty($dynamicValueMap[$row->id])) { $imageName = strtolower($dynamicValueMap[$row->id]) . '.png'; @@ -1189,26 +1219,174 @@ class StickerPdfService } + //..BackUp Code + // public function generateTemplate(string $stickerId,Collection $dynamicElements,?ItemCharacteristic $itemCharacteristic,$itemCode,$plantId) + // { + // $dynamicValueMap = []; + + // $itemCode = $itemCharacteristic?->item?->code ?? $itemCode ?? ''; + + // $plantName = $itemCharacteristic?->plant?->name ?? ''; + // $plantAddress = $itemCharacteristic?->plant?->address ?? ''; + + + // foreach ($dynamicElements as $element) { + + // $column = $element->characteristics_type; + + // $value = ''; + + // if ($itemCharacteristic && $column && Schema::hasColumn('item_characteristics', $column)) + // { + // $value = $itemCharacteristic->{$column}; + // } + + // $dynamicValueMap[$element->id] = $value; + // } + + // $structure = StickerStructureDetail::where('sticker_id',$stickerId)->firstOrFail(); + + // $elements = StickerDetail::where('sticker_structure_detail_id',$structure->id)->get(); + + // $templateElements = []; + + // foreach ($elements as $row) { + + // if ($row->design_element_type == 'Text') { + + // $textValue = $row->string_value ?? ''; + + // if ($row->element_type == 'Dynamic' && isset($dynamicValueMap[$row->id]) && + // !in_array($row->characteristics_type, [ + // 'item_code', + // 'serial_number', + // 'plant_name', + // 'plant_address', + // 'current_date', + // ]) + // ) + // { + // $textValue = $dynamicValueMap[$row->id]; + // } + + // if ($row->characteristics_type == 'item_code') { + // $textValue = $itemCode; + // } + // if ($row->characteristics_type == 'serial_number') { + // $textValue = '__SERIAL_NUMBER__'; + // } + // if ($row->characteristics_type === 'plant_name') { + // $textValue = $plantName; + // } + // if ($row->characteristics_type === 'plant_address') { + // $textValue = $plantAddress; + // } + // if ($row->characteristics_type === 'current_date') { + // $textValue = date('M-y'); + // } + // $font = $row->string_font ?? 'helvetica'; + + // if (str_contains($row->string_value ?? '', '₹')) { + // $font = 'dejavusans'; + // } + + // $templateElements[] = + // [ + // 'type' => 'text', + // 'value' => $textValue, + // 'x' => (float) ($row->string_x_value ?? 0), + // 'y' => (float) ($row->string_y_value ?? 0), + // 'font' => $font, + // 'size' => (int) ($row->string_size ?? 10), + // 'color' => $row->element_colour ?? '#000000', + // ]; + // } + // elseif ($row->design_element_type == 'QR') { + + // if ($row->element_type == 'Dynamic') { + // $qrValue = '__DYNAMIC_QR__'; + // } else { + // $qrValue = $row->qr_value ?? ''; + // } + + // $templateElements[] = [ + // 'type' => 'qr', + // 'value' => $qrValue, + // 'x' => (float) ($row->qr_x_value ?? 0), + // 'y' => (float) ($row->qr_y_value ?? 0), + // 'size' => (float) ($row->qr_size ?? 10), + // ]; + // } + // elseif ($row->design_element_type == 'Image') { + + // $imagePath = null; + + // if ($row->element_type == 'Dynamic' && isset($dynamicValueMap[$row->id]) && !empty($dynamicValueMap[$row->id])) + // { + // $imageName = strtolower($dynamicValueMap[$row->id]) . '.png'; + + // $imagePath = public_path('images/' . ltrim($imageName, '/')); + // } + + // $templateElements[] = [ + // 'type' => 'image', + // 'path' => $imagePath, + // 'x' => (float) ($row->image_x ?? 0), + // 'y' => (float) ($row->image_y ?? 0), + // 'width' => (float) ($row->image_width ?? 0), + // 'height' => (float) ($row->image_height ?? 0), + // ]; + // } + // elseif ($row->design_element_type == 'Shape') { + + // $templateElements[] = [ + // 'type' => 'shape', + // 'name' => $row->shape_name, + // 'pen_size' => (float) ($row->shape_pen_size ?? ''), + // 'color' => $row->element_colour ?? '#000000', + // 'x1' => (float) ($row->shape_x1_value ?? 0), + // 'y1' => (float) ($row->shape_y1_value ?? 0), + // 'x2' => (float) ($row->shape_x2_value ?? 0), + // 'y2' => (float) ($row->shape_y2_value ?? 0), + // 'radius' => (float) ($row->curve_radius ?? ''), + // ]; + // } + // } + + // // Return prepared template. No PDF generation here. + // return [ + // 'width' => (float) $structure->sticker_width, + // 'height' => (float) $structure->sticker_height, + // 'lmargin' => (float) $structure->sticker_lmargin, + // 'tmargin' => (float) $structure->sticker_tmargin, + // 'rmargin' => (float) $structure->sticker_rmargin, + // 'bmargin' => (float) $structure->sticker_bmargin, + // 'itemCode' => $itemCode, + // 'elements' => $templateElements, + // ]; + // } + + public function generateBulkFromTemplate(array $template,$fromSerial,$toSerial,string $itemCode) { $stickerWidth = (float) $template['width']; $stickerHeight = (float) $template['height']; - $gap = 5.0; + // $gap = 5.0; - $count = ($toSerial - $fromSerial) + 1; + // $count = ($toSerial - $fromSerial) + 1; - $totalHeight = - ($stickerHeight * $count) - + - ($gap * ($count - 1)); + // $totalHeight = + // ($stickerHeight * $count) + // + + // ($gap * ($count - 1)); $pdf = new TCPDF( 'P', 'mm', [ $stickerWidth, - $totalHeight + $stickerHeight ], true, 'UTF-8', @@ -1227,7 +1405,7 @@ class StickerPdfService $pdf->SetAutoPageBreak(false, 0); - $pdf->AddPage(); + // $pdf->AddPage(); $currentY = 0; @@ -1235,6 +1413,7 @@ class StickerPdfService for ($serial = $fromSerial; $serial <= $toSerial; $serial++) { + $pdf->AddPage(); $qrContent = $itemCode . '|' . $serial; foreach ($template['elements'] as $element) { @@ -1357,7 +1536,7 @@ class StickerPdfService } } - $currentY += $stickerHeight + $gap; + // $currentY += $stickerHeight + $gap; } return $pdf->Output('', 'S');