Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
12.70% covered (danger)
12.70%
16 / 126
14.29% covered (danger)
14.29%
3 / 21
CRAP
0.00% covered (danger)
0.00%
0 / 1
AddressPrintQrcodeService
12.70% covered (danger)
12.70%
16 / 126
14.29% covered (danger)
14.29%
3 / 21
713.34
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 mountQrcodeAddress
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 mountZplBlockMedium
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 mountZplBlockSmall
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 mountZplBlockLarge
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
2
 mountZplBlockMediumLarge
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
2
 printQrcodeMediumTag
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
 printQrcodeLargeTag
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 printQrcodeMediumLargeTag
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 printQrcodeSmallTag
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
 printSingleQrcodeSmall
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 printSingleQrcodeMedium
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 printSingleQrcodeLarge
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 printSingleQrcodeMediumLarge
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 printHierarchyQrcodeMedium
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 printHierarchyQrcodeSmall
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 printHierarchyQrcodeLarge
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 printHierarchyQrcodeMediumLarge
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 collectHierarchyRecursive
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 collectHierarchyItems
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 buildPreviewLabel
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Domain\Address\Services;
4
5use App\Models\Address\Address;
6use Exception;
7
8class AddressPrintQrcodeService
9{
10    private const TWO_COLUMN_PRINT_WIDTH = 812;
11    private const SECOND_COLUMN_OFFSET = 400;
12
13    private Address $address;
14
15    public function __construct($address)
16    {
17        $this->address = $address;
18    }
19
20
21    public function mountQrcodeAddress($item)
22    {
23        return [
24            'type' => 'address',
25            'id' => $item->id,
26        ];
27    }
28
29    /**
30     * @param $item
31     * @param $offset
32     * @return string
33     */
34    public function mountZplBlockMedium($item, $offset): string
35    {
36
37        $jsonContent = json_encode($this->mountQrcodeAddress($item));
38        $hierarchy = (new AddressHierarchyService())->get($item);
39        $description = $item->name;
40        $id = $item->id;
41
42        return "
43            ^FO" . (60 + $offset) . ",50^BQN,2,8
44            ^FDQA,$jsonContent^FS
45
46            ^FO" . (320 + $offset) . ",20^A0R,30,30^FB600,2,,L^FD$hierarchy^FS
47
48            ^FO" . (240 + $offset) . ",340^A0R,25,25^FDEndereço:^FS
49            ^FO" . (200 + $offset) . ",340^A0R,35,35^FD$description^FS
50
51            ^FO" . (140 + $offset) . ",340^A0R,25,25^FD#$id^FS
52        ";
53    }
54
55    /**
56     * @param $item
57     * @param $offset
58     * @return string
59     */
60    public function mountZplBlockSmall($item, $offset): string
61    {
62
63        $jsonContent = json_encode($this->mountQrcodeAddress($item));
64        $hierarchy = (new AddressHierarchyService())->get($item);
65        $description = $item->name;
66        $id = $item->id;
67
68        return "
69            ^FO" . (25 + $offset) . ",10^BQN,2,4
70            ^FDQA,$jsonContent^FS
71
72            ^FO" . (155 + $offset) . ",15^A0N,20,20^FB240,4,5,L^FD$hierarchy^FS
73
74            ^FO" . (155 + $offset) . ",65^A0N,25,25^FB240,2,,L^FD$description^FS
75            ^FO" . (155 + $offset) . ",120^A0N,20,20^FB240,1,,L^FD#$id^FS
76        ";
77    }
78
79    /**
80     * @param $item
81     * @return string
82     */
83    public function mountZplBlockLarge($item): string
84    {
85
86        $jsonContent = json_encode($this->mountQrcodeAddress($item));
87        $hierarchy = (new AddressHierarchyService())->get($item);
88        $description = $item->name;
89        $id = $item->id;
90
91        $jsonHex = str_replace('"', '_22', $jsonContent);
92        // O QB exige a contagem de caracteres original (antes da troca para hex)
93        // Formatado com 4 dígitos (ex: 0085)
94        $magnificacao = "13"; // 57 * 9 = 513 pontos (~6.4cm)
95
96        return "
97            ^XA
98            ^CI28
99            ^PW840
100            ^LL1184
101
102            ^FO180,50^BQN,2,$magnificacao
103            ^FH^FDQB,$jsonHex^FS
104
105            ^FO670,50^A0R,60,60^FB1000,2,,L^FD$hierarchy^FS
106
107            ^FO380,600^A0R,100,100^FD$description^FS
108
109            ^FO180,600^A0R,60,60^FD#$id^FS
110
111            ^XZ";
112    }
113
114    /**
115     * @param $item
116     * @return string
117     */
118    public function mountZplBlockMediumLarge($item): string
119    {
120        $jsonContent = json_encode($this->mountQrcodeAddress($item));
121        $hierarchy = (new AddressHierarchyService())->get($item);
122        $description = $item->name;
123        $id = $item->id;
124
125        $jsonHex = str_replace('"', '_22', $jsonContent);
126
127        $magnificacao = "9";
128
129        return "
130            ^XA
131            ^CI28
132            ^PW799
133            ^LL560
134
135            ^FO50,240^BQN,2,$magnificacao
136            ^FH^FDQ,$jsonHex^FS
137
138            ^FO50,90^A0,50,50^FB780,2,,L^FD$hierarchy^FS
139
140            ^FO400,240^A0,60,60^FB350,3,,L^FD$description^FS
141
142            ^FO400,470^A0,60,60^FD#$id^FS
143
144            ^XZ";
145    }
146
147    /**
148     * @param $items
149     * @return void
150     * @throws Exception
151     */
152    public function printQrcodeMediumTag($items): void
153    {
154        $zpl = '';
155        foreach (array_chunk($items, 2) as $item) {
156            $zpl .= "^XA^CI28^PW" . self::TWO_COLUMN_PRINT_WIDTH;
157            $zpl .= $this->mountZplBlockMedium($item[0], 0);
158
159            if (isset($item[1])) {
160                $zpl .= $this->mountZplBlockMedium($item[1], self::SECOND_COLUMN_OFFSET);
161            }
162
163            $zpl .= "^XZ";
164        }
165
166        $fp = fsockopen(env('WAREHOUSE_PRINTER_IP'), env('WAREHOUSE_PRINTER_PORT'), $errno, $errstr, 5);
167
168        if (!$fp) {
169            throw new Exception("Erro: $errstr ($errno)");
170        }
171
172        fwrite($fp, $zpl);
173        fclose($fp);
174    }
175
176    /**
177     * @param $items
178     * @return void
179     * @throws Exception
180     */
181    public function printQrcodeLargeTag($items): void
182    {
183        $zpl = '';
184        foreach ($items as $item) {
185            $zpl .= $this->mountZplBlockLarge($item);
186        }
187
188        $fp = fsockopen(env('WAREHOUSE_PRINTER_IP'), env('WAREHOUSE_PRINTER_PORT'), $errno, $errstr, 5);
189
190        if (!$fp) {
191            throw new Exception("Erro: $errstr ($errno)");
192        }
193        fwrite($fp, $zpl);
194        fclose($fp);
195    }
196
197    /**
198     * @param $items
199     * @return void
200     * @throws Exception
201     */
202    public function printQrcodeMediumLargeTag($items): void
203    {
204        $zpl = '';
205        foreach ($items as $item) {
206            $zpl .= $this->mountZplBlockMediumLarge($item);
207        }
208
209        $fp = fsockopen(env('WAREHOUSE_PRINTER_IP'), env('WAREHOUSE_PRINTER_PORT'), $errno, $errstr, 5);
210
211        if (!$fp) {
212            throw new Exception("Erro: $errstr ($errno)");
213        }
214        fwrite($fp, $zpl);
215        fclose($fp);
216    }
217
218    /**
219     * @param $items
220     * @return void
221     * @throws Exception
222     */
223    public function printQrcodeSmallTag($items): void
224    {
225        $zpl = '';
226        foreach (array_chunk($items, 2) as $item) {
227            $zpl .= "^XA^CI28^PW" . self::TWO_COLUMN_PRINT_WIDTH;
228            $zpl .= $this->mountZplBlockSmall($item[0], 0);
229
230            if (isset($item[1])) {
231                $zpl .= $this->mountZplBlockSmall($item[1], self::SECOND_COLUMN_OFFSET);
232            }
233
234            $zpl .= "^XZ";
235        }
236
237        $fp = fsockopen(env('WAREHOUSE_PRINTER_IP'), env('WAREHOUSE_PRINTER_PORT'), $errno, $errstr, 5);
238
239        if (!$fp) {
240            throw new Exception("Erro: $errstr ($errno)");
241        }
242
243        fwrite($fp, $zpl);
244        fclose($fp);
245    }
246
247    /**
248     * @return void
249     * @throws Exception
250     */
251    public function printSingleQrcodeSmall(): void
252    {
253        $this->printQrcodeSmallTag([$this->address]);
254    }
255
256    /**
257     * @return void
258     * @throws Exception
259     */
260    public function printSingleQrcodeMedium(): void
261    {
262        $this->printQrcodeMediumTag([$this->address]);
263    }
264
265    /**
266     * @return void
267     * @throws Exception
268     */
269    public function printSingleQrcodeLarge(): void
270    {
271        $this->printQrcodeLargeTag([$this->address]);
272    }
273
274    /**
275     * @return void
276     * @throws Exception
277     */
278    public function printSingleQrcodeMediumLarge(): void
279    {
280        $this->printQrcodeMediumLargeTag([$this->address]);
281    }
282
283    /**
284     * @return void
285     * @throws Exception
286     */
287    public function printHierarchyQrcodeMedium(): void
288    {
289        $items = [];
290        $this->collectHierarchyRecursive($this->address, $items);
291
292        $this->printQrcodeMediumTag($items);
293    }
294
295    /**
296     * @return void
297     * @throws Exception
298     */
299    public function printHierarchyQrcodeSmall(): void
300    {
301        $items = [];
302        $this->collectHierarchyRecursive($this->address, $items);
303
304        $this->printQrcodeSmallTag($items);
305    }
306
307    /**
308     * @return void
309     * @throws Exception
310     */
311    public function printHierarchyQrcodeLarge(): void
312    {
313        $items = [];
314        $this->collectHierarchyRecursive($this->address, $items);
315
316        $this->printQrcodeLargeTag($items);
317    }
318
319    /**
320     * @return void
321     * @throws Exception
322     */
323    public function printHierarchyQrcodeMediumLarge(): void
324    {
325        $items = [];
326        $this->collectHierarchyRecursive($this->address, $items);
327
328        $this->printQrcodeMediumLargeTag($items);
329    }
330
331    /**
332     * @param $address
333     * @param array $items
334     * @return void
335     */
336    private function collectHierarchyRecursive($address, array &$items): void
337    {
338        $items[] = $address;
339
340        foreach ($address->children as $child) {
341            $this->collectHierarchyRecursive($child, $items);
342        }
343    }
344
345    public function collectHierarchyItems(): array
346    {
347        $items = [];
348        $this->collectHierarchyRecursive($this->address, $items);
349
350        return $items;
351    }
352
353    public function buildPreviewLabel(Address $address): array
354    {
355        $hierarchy = (new AddressHierarchyService())->get($address);
356
357        return [
358            'title' => 'Etiqueta de Endereco',
359            'qrcode' => $this->mountQrcodeAddress($address),
360            'lines' => [
361                $hierarchy,
362                $address->name,
363                '#' . $address->id,
364            ],
365        ];
366    }
367}