From 6731618356649d8a837b5cfcf5a35d6a87979d22 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Thu, 10 Sep 2026 17:46:01 +0530 Subject: [PATCH] Added sticker template service for bulk sticker printing --- app/Services/StickerPdfService.php | 323 ++++++++++++++++++++++++++++- 1 file changed, 322 insertions(+), 1 deletion(-) diff --git a/app/Services/StickerPdfService.php b/app/Services/StickerPdfService.php index 17f8ffb..a00b584 100644 --- a/app/Services/StickerPdfService.php +++ b/app/Services/StickerPdfService.php @@ -1042,6 +1042,327 @@ class StickerPdfService } } + 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; + + $count = ($toSerial - $fromSerial) + 1; + + $totalHeight = + ($stickerHeight * $count) + + + ($gap * ($count - 1)); + + $pdf = new TCPDF( + 'P', + 'mm', + [ + $stickerWidth, + $totalHeight + ], + true, + 'UTF-8', + false + ); + + $pdf->setPrintHeader(false); + $pdf->setPrintFooter(false); + + $pdf->SetMargins( + 0, + 0, + 0, + 0 + ); + + $pdf->SetAutoPageBreak(false, 0); + + $pdf->AddPage(); + + $currentY = 0; + + // Generate every serial + + for ($serial = $fromSerial; $serial <= $toSerial; $serial++) + { + $qrContent = $itemCode . '|' . $serial; + + foreach ($template['elements'] as $element) { + + if ($element['type'] == 'text') { + + $textValue = $element['value']; + + if ($textValue == '__SERIAL_NUMBER__') { + $textValue = (string) $serial; + } + + $pdf->SetFont($element['font'],'',$element['size']); + + $rgb = $this->hexToRgb($element['color']); + + $pdf->SetTextColor( + $rgb[0], + $rgb[1], + $rgb[2] + ); + + $pdf->Text( + $element['x'], + $element['y'] + $currentY, + (string) $textValue + ); + } + elseif ($element['type'] == 'qr') { + + $qrValue = $element['value']; + + if ($qrValue == '__DYNAMIC_QR__') { + $qrValue = $qrContent; + } + + $pdf->write2DBarcode( + $qrValue, + 'QRCODE,H', + $element['x'], + $element['y'] + $currentY, + $element['size'], + $element['size'] + ); + } + elseif ($element['type'] == 'image') { + + if (!empty($element['path']) && file_exists($element['path'])) + { + + $pdf->Image( + $element['path'], + $element['x'], + $element['y'] + $currentY, + $element['width'], + $element['height'] + ); + } + } + elseif ($element['type'] == 'shape') { + + $rgb = $this->hexToRgb( + $element['color'] + ); + + $pdf->SetDrawColor( + $rgb[0], + $rgb[1], + $rgb[2] + ); + + $pdf->SetLineWidth( + $element['pen_size'] + ); + if ($element['name'] == 'Line') { + + $pdf->Line( + $element['x1'], + $element['y1'] + $currentY, + $element['x2'], + $element['y2'] + $currentY + ); + } + elseif ($element['name'] == 'Rectangle') { + + $x = min($element['x1'],$element['x2']); + $y = min($element['y1'],$element['y2']); + + $width = abs($element['x2'] - $element['x1']); + + $height = abs($element['y2'] - $element['y1']); + + $pdf->Rect( + $x, + $y + $currentY, + $width, + $height, + 'D' + ); + } + elseif ($element['name'] == 'CurvedRectangle') + { + $x = min($element['x1'],$element['x2']); + $y = min($element['y1'],$element['y2']); + + $width = abs($element['x2'] - $element['x1']); + + $height = abs($element['y2'] - $element['y1']); + + $pdf->RoundedRect( + $x, + $y + $currentY, + $width, + $height, + $element['radius'], + '1111', + 'D' + ); + } + } + } + + $currentY += $stickerHeight + $gap; + } + + return $pdf->Output('', 'S'); + } + public function generatePdfBySerial($item, $serNo, $plantId, $refNumber) { $recFound = ProductionQuantity::where('plant_id', $plantId) @@ -1146,7 +1467,7 @@ class StickerPdfService private function hexToRgb($hex) { - if (! is_string($hex) || ($hex = trim($hex)) === '') { + if (! is_string($hex) || ($hex = trim($hex)) == '') { return [0, 0, 0]; } $hex = ltrim($hex, '#');