NoticeReceipt::getPositionXY()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
1
<?php
2
namespace Eduardokum\CorreiosPhp\Render;
3
4
use Eduardokum\CorreiosPhp\Contracts\Render\Printable as PrintableContract;
5
use Eduardokum\CorreiosPhp\Contracts\Config\Config as ConfigContract;
6
use Eduardokum\CorreiosPhp\Entities\PostalObject;
7
use Eduardokum\CorreiosPhp\Exception\InvalidArgumentException;
8
9
class NoticeReceipt extends Pdf
10
{
11
12
    /**
13
     * @var PrintableContract
14
     */
15
    private $printable;
16
17
    /**
18
     * @var int
19
     */
20
    private $perPage = 3;
21
22
    /**
23
     * @var array
24
     */
25
    private $positions = [
26
        3 => [
27
            1 => [2.5, 2.5],
28
            2 => [2.5, 97 + 3.5],
29
            3 => [2.5, (97 * 2) + (2.5 * 2)],
30
        ],
31
    ];
32
33
    /**
34
     * @var array
35
     */
36
    private $noticeReceiptSize = [205, 95];
37
38
    public function __construct(PrintableContract $printable, ConfigContract $config = null)
39
    {
40
        parent::__construct($config);
41
        $this->setPrintable($printable);
42
43
        $this->tcpdf = new \TCPDF(null, 'mm', 'A4', true, 'UTF-8');
44
        $this->tcpdf->setPrintHeader(false);
45
        $this->tcpdf->setPrintFooter(false);
46
        $this->tcpdf->SetMargins(0, 0, 0, true);
47
        $this->tcpdf->SetCreator('Eduardokum\\CorreiosPhp');
48
        $this->tcpdf->SetAuthor('Eduardokum\\CorreiosPhp');
49
        $this->tcpdf->SetTitle('AR');
50
        $this->tcpdf->SetSubject('AR');
51
        $this->tcpdf->SetKeywords('AR');
52
        $this->tcpdf->SetAutoPageBreak(false, 0);
53
        $this->tcpdf->SetFont($this->fontRegular, '', 9, '', false);
54
    }
55
56
    /**
57
     * @return int
58
     */
59
    public function getPerPage()
60
    {
61
        return $this->perPage;
62
    }
63
64
    /**
65
     * @return PrintableContract
66
     */
67
    public function getPrintable()
68
    {
69
        return $this->printable;
70
    }
71
72
    /**
73
     * @param PrintableContract $printable
74
     *
75
     * @return NoticeReceipt
76
     */
77
    public function setPrintable(PrintableContract $printable)
78
    {
79
        $this->printable = $printable;
80
        return $this;
81
    }
82
83
    /**
84
     * @param string $output
85
     *
86
     * @return string
87
     */
88
    public function render($output = 'I')
89
    {
90
        $tags = $this->filterTags($this->getPrintable()->toPrint());
91
92
        if (count($tags) == 0) {
93
            throw new InvalidArgumentException('No tags available for printing');
94
        }
95
96
        $noticeCount = 0;
97
        foreach ($tags as $tag) {
98
            $noticeCount++;
99
            $position = $rest = $noticeCount % $this->getPerPage();
100
            $position = $position == 0 ? $this->getPerPage() : $position;
101
            if ($rest === 1) {
102
                $this->tcpdf->addPage();
103
            }
104
            $this->noticeReceipt($tag, $position);
105
        }
106
107
        return $this->tcpdf->Output('notice_receipt.pdf', $output);
108
    }
109
110
    /**
111
     * @param array $tags
112
     *
113
     * @return array
114
     */
115
    private function filterTags(array $tags)
116
    {
117
        return array_filter($tags, function ($tag) {
118
            /** @var PostalObject $tag */
119
            return in_array('001', $tag->getAdditionalServices());
120
        });
121
    }
122
123
    /**
124
     * @param $position
125
     *
126
     * @return array
127
     */
128
    private function getPositionXY($position)
129
    {
130
        return $this->positions[$this->getPerPage()][$position];
131
    }
132
133
    /**
134
     * @return int
135
     */
136
    private function getNoticeReceiptWidth()
137
    {
138
        return $this->noticeReceiptSize[0];
139
    }
140
141
    /**
142
     * @return int
143
     */
144
    private function getNoticeReceiptHeight()
145
    {
146
        return $this->noticeReceiptSize[1];
147
    }
148
149
    /**
150
     * @param $tag
151
     * @param $position
152
     */
153
    private function noticeReceipt(PostalObject $tag, $position)
154
    {
155
        list($x, $y) = $this->getPositionXY($position);
156
        $this->frame($x, $y);
157
158
        $x += 7;
159
        $this->tcpdf->SetXY($x, $y);
160
        $lastPosition = $topPosition = $this->logo($x, $y);
161
        $lastPosition = $this->recipient($tag, $x, $lastPosition[1]);
162
        $lastPosition = $this->barcode($tag, $x, $lastPosition[1]);
163
        $lastPosition = $this->sender($x, $lastPosition[1]);
164
        $this->signatures($x, $lastPosition[1]);
165
        $lastPosition = $this->retry($x, $topPosition[1]);
166
        $this->stamp($lastPosition[0], $topPosition[1]);
167
    }
168
169
    /**
170
     * @param $x
171
     * @param $y
172
     */
173
    private function frame($x, $y)
174
    {
175
        $this->tcpdf->Rect($x + 7, $y, $this->getNoticeReceiptWidth() - 7, $this->getNoticeReceiptHeight());
176
        $this->tcpdf->StartTransform();
177
        $this->tcpdf->SetXY($x, $y + $this->getNoticeReceiptHeight());
178
        $this->tcpdf->Rotate(90);
179
        $border_dashed = ['LRTB' => ['dash' => '3,2']];
180
        $this->tcpdf->Cell($this->getNoticeReceiptHeight(), 6, sprintf('Cole aqui %40s Cole aqui', ''), $border_dashed, 0, 'C');
181
        $this->tcpdf->StopTransform();
182
        $this->tcpdf->SetLineStyle(['dash' => 0]);
183
    }
184
185
    /**
186
     * @param $x
187
     * @param $y
188
     *
189
     * @return array
190
     */
191
    private function logo($x, $y)
192
    {
193
        $xDefault = $x;
194
        $image = CORREIOS_PHP_BASE . '/resources/assets/images/correios.png';
195
        $this->tcpdf->Image($image, $x + $this->padding, $y + $this->padding, null, 8, 'png', null, null, false, 300, null, false, false, 0, true);
196
197
        $this->tcpdf->SetXY($x + 55, $y);
198
        $this->tcpdf->SetFontSize(18);
199
        $this->writeBold(10, 'SIGEP');
200
        $this->tcpdf->SetFontSize(9);
201
202
        $x = $this->tcpdf->GetX() + 2;
203
        $this->tcpdf->SetXY($x, $y + $this->padding);
204
        $this->writeBold(3, 'AVISO DE');
205
        $this->tcpdf->SetXY($x, $y + 5);
206
        $this->writeBold(3, 'RECEBIMENTO');
207
208
        $x = $this->tcpdf->GetX() + 15;
209
        $this->tcpdf->SetXY($x, $y);
210
        $this->tcpdf->Write(10, sprintf('CONTRATO %s', $this->getConfig()->getContract()));
211
        $y += 10;
212
        $this->tcpdf->SetXY($x, $y);
213
        $this->tcpdf->Line($xDefault, $y, $this->tcpdf->getPageWidth() - 2.5, $y);
214
        return [$this->tcpdf->GetX(),$this->tcpdf->GetY()];
215
    }
216
217
    /**
218
     * @param PostalObject $tag
219
     * @param              $x
220
     * @param              $y
221
     *
222
     * @return array
223
     */
224
    private function recipient(PostalObject $tag, $x, $y)
225
    {
226
        $x += 2;
227
        $y += 1;
228
        $this->tcpdf->SetXY($x, $y);
229
230
        $this->writeBold(5, 'DESTINATÁRIO: ');
231
        $y += 6;
232
233
        $this->tcpdf->SetXY($x, $y);
234
        $this->tcpdf->Write(4, $tag->getRecipient()->getName());
235
        $y += 4;
236
237
        $this->tcpdf->SetXY($x, $y);
238
        $this->tcpdf->Write(4, sprintf('%s, %s', $tag->getRecipient()->getStreet(), $tag->getRecipient()->getNumber()));
239
        $y += 4;
240
241
        $this->tcpdf->SetXY($x, $y);
242
        $this->tcpdf->Write(4, trim(sprintf('%s %s', $tag->getRecipient()->getComplement(), $tag->getRecipient()->getDistrict())));
243
        $y += 4;
244
245
        $this->tcpdf->SetXY($x, $y);
246
        $this->writeBold(4, implode('-', str_split($tag->getRecipient()->getCep(), 5)));
247
        $this->tcpdf->Write(4, sprintf(' %s/%s', $tag->getRecipient()->getCity(), $tag->getRecipient()->getState()));
248
        $y += 4;
249
250
        $this->tcpdf->SetXY($x, $y);
251
        return [$this->tcpdf->GetX(), $this->tcpdf->GetY()];
252
    }
253
254
    /**
255
     * @param PostalObject $tag
256
     * @param              $x
257
     * @param              $y
258
     *
259
     * @return array
260
     */
261
    private function barcode(PostalObject $tag, $x, $y)
262
    {
263
        $x += 10;
264
        $y += 2;
265
266
        $code = implode(' ', array_merge(
267
            [substr($tag->getTagDv(), 0, 2)],
268
            str_split(preg_replace('/[^0-9]/', '', $tag->getTagDv()), 3),
269
            [substr($tag->getTagDv(), - 2)]
270
        ));
271
272
        $this->tcpdf->SetXY($x + 20, $y);
273
        $this->tcpdf->SetFontSize(11);
274
        $this->writeBold(5, $code);
275
        $this->tcpdf->SetFontSize(9);
276
277
        $style = [
278
            'position' => '',
279
            'align'    => 'C',
280
            'fitwidth' => false,
281
            'border'   => 0,
282
            'hpadding' => 0,
283
            'vpadding' => 0,
284
            'fgcolor'  => [0, 0, 0],
285
            'bgcolor'  => [255, 255, 255],
286
            'text'     => false,
287
        ];
288
289
        $this->tcpdf->write1DBarcode($tag->getTagDv(), 'C128', $x + 5, $y + 5, 65, 15, null, $style);
290
291
        $y += 5 + 18;
292
        $this->tcpdf->SetXY($x, $y);
293
        return [$this->tcpdf->GetX(), $this->tcpdf->GetY()];
294
    }
295
296
    /**
297
     * @param        $x
298
     * @param        $y
299
     *
300
     * @return array
301
     */
302
    private function sender($x, $y)
303
    {
304
        $x += 2;
305
        $y += $this->padding;
306
307
        $this->tcpdf->SetXY($x, $y);
308
        $this->writeBold(4, 'REMETENTE: ');
309
        $this->tcpdf->Write(4, $this->getConfig()->getSender()->getName());
310
        $y += 4;
311
312
        $this->tcpdf->SetXY($x, $y);
313
        $this->writeBold(4, 'ENDEREÇO PARA DEVOLUÇÃO DO OBJETO: ');
314
        $y += 4;
315
316
        $this->tcpdf->SetXY($x, $y);
317
        $this->tcpdf->Write(4, sprintf('%s, %s ', $this->getConfig()->getSender()->getStreet(), $this->getConfig()->getSender()->getNumber()));
318
        $this->tcpdf->Write(4, trim(sprintf('%s %s', $this->getConfig()->getSender()->getComplement(), $this->getConfig()->getSender()->getDistrict())));
319
        $y += 4;
320
321
        $this->tcpdf->SetXY($x, $y);
322
        $this->writeBold(4, implode('-', str_split($this->getConfig()->getSender()->getCep(), 5)));
323
        $this->tcpdf->Write(4, sprintf(' %s/%s', $this->getConfig()->getSender()->getCity(), $this->getConfig()->getSender()->getState()));
324
        $y += 4;
325
        $this->tcpdf->SetXY($x, $y);
326
327
        return [$this->tcpdf->GetX(), $this->tcpdf->GetY()];
328
    }
329
330
    /**
331
     * @param $x
332
     * @param $y
333
     *
334
     * @return array
335
     */
336
    private function signatures($x, $y)
337
    {
338
        $y += 2.5;
339
        $this->tcpdf->Line($x, $y - 0.5, $this->tcpdf->getPageWidth() - 2.5, $y - 0.5);
340
        $this->tcpdf->SetXY($x, $y);
341
342
        $this->tcpdf->SetFontSize(5);
343
        $this->tcpdf->Write(2, 'DECLARAÇÂO DE CONTEÚDO');
344
        $y += 6;
345
346
        $x2Colum = $this->tcpdf->getPageWidth() - 82;
347
348
        $this->tcpdf->SetXY($x, $y);
349
        $this->tcpdf->Line($x, $y - 0.5, $this->tcpdf->getPageWidth() - 2.5, $y - 0.5);
350
        $this->tcpdf->Write(2, 'ASSINATURA DO RECEBEDOR');
351
        $this->tcpdf->SetX($x2Colum);
352
        $this->tcpdf->Write(2, 'DATA DE ENTREGA');
353
        $this->tcpdf->Line($x2Colum - $this->padding, $y - 0.5, $x2Colum - $this->padding, $y + 5.5);
354
        $y += 6;
355
356
        $this->tcpdf->SetXY($x, $y);
357
        $this->tcpdf->Line($x, $y - 0.5, $this->tcpdf->getPageWidth() - 2.5, $y - 0.5);
358
        $this->tcpdf->Write(2, 'NOME LEGIVÉL DO RECEBEDOR');
359
        $this->tcpdf->SetX($x2Colum);
360
        $this->tcpdf->Write(2, 'N DOC DE IDENTIDADE');
361
        $this->tcpdf->Line($x2Colum - $this->padding, $y - 0.5, $x2Colum - $this->padding, $y + 5.5);
362
        $y += 6;
363
364
        $this->tcpdf->SetXY($x, $y);
365
        $this->tcpdf->SetFontSize(9);
366
        return [$this->tcpdf->GetX(), $this->tcpdf->GetY()];
367
    }
368
369
    /**
370
     * @param $x
371
     * @param $y
372
     *
373
     * @return array
374
     */
375
    private function retry($x, $y)
376
    {
377
        $x += 95;
378
        $xDefault  = $x;
379
        $this->tcpdf->Rect($x, $y, 65, $this->getNoticeReceiptHeight() - 22, 'DF', [], [255, 255, 255]);
380
381
        $x += 2;
382
        $y += 1;
383
        $this->tcpdf->SetXY($x, $y);
384
        $this->writeBold(5, 'TENTATIVAS DE ENTREGA:');
385
        $y += 8;
386
387
        $this->tcpdf->SetXY($x, $y);
388
        $this->writeBold(6, '1º');
389
        $this->tcpdf->Write(6, ' ___/___/_____        _____:_____h');
390
        $y += 6;
391
392
        $this->tcpdf->SetXY($x, $y);
393
        $this->writeBold(6, '2º');
394
        $this->tcpdf->Write(6, ' ___/___/_____        _____:_____h');
395
        $y += 6;
396
397
        $this->tcpdf->SetXY($x, $y);
398
        $this->writeBold(6, '3º');
399
        $this->tcpdf->Write(6, ' ___/___/_____        _____:_____h');
400
        $y += 16;
401
402
        $this->tcpdf->SetXY($x, $y);
403
        $this->tcpdf->Write(5, 'MOTIVO DE DEVOLUÇÃO:');
404
        $y += 7;
405
406
        $this->tcpdf->SetFontSize(6);
407
408
        $yTop = $y;
409
        $this->tcpdf->SetXY($x, $y);
410
        $this->tcpdf->Cell(6, 4, '1', 1, 0, 'C');
411
        $this->tcpdf->Write(4, 'Mudou-se');
412
        $y += 5;
413
414
        $this->tcpdf->SetXY($x, $y);
415
        $this->tcpdf->Cell(6, 4, '2', 1, 0, 'C');
416
        $this->tcpdf->Write(4, 'Endereço insulficiente');
417
        $y += 5;
418
419
        $this->tcpdf->SetXY($x, $y);
420
        $this->tcpdf->Cell(6, 4, '3', 1, 0, 'C');
421
        $this->tcpdf->Write(4, 'Não existe o número');
422
        $y += 5;
423
424
        $this->tcpdf->SetXY($x, $y);
425
        $this->tcpdf->Cell(6, 4, '4', 1, 0, 'C');
426
        $this->tcpdf->Write(4, 'Desconhecido');
427
        $y += 5;
428
429
        $this->tcpdf->SetXY($x, $y);
430
        $this->tcpdf->Cell(6, 4, '9', 1, 0, 'C');
431
        $this->tcpdf->Write(4, 'Outros: ____________________________________');
432
        $y = $yTop;
433
        $x += 32;
434
435
        $this->tcpdf->SetXY($x, $y);
436
        $this->tcpdf->Cell(6, 4, '5', 1, 0, 'C');
437
        $this->tcpdf->Write(4, 'Recusado');
438
        $y += 5;
439
440
        $this->tcpdf->SetXY($x, $y);
441
        $this->tcpdf->Cell(6, 4, '6', 1, 0, 'C');
442
        $this->tcpdf->Write(4, 'Não procurado');
443
        $y += 5;
444
445
        $this->tcpdf->SetXY($x, $y);
446
        $this->tcpdf->Cell(6, 4, '7', 1, 0, 'C');
447
        $this->tcpdf->Write(4, 'Ausente');
448
        $y += 5;
449
450
        $this->tcpdf->SetXY($x, $y);
451
        $this->tcpdf->Cell(6, 4, '8', 1, 0, 'C');
452
        $this->tcpdf->Write(4, 'Falecido');
453
        $y += 5;
454
455
        $this->tcpdf->SetFontSize(9);
456
        $this->tcpdf->SetXY($xDefault + 65, $y);
457
        return [$this->tcpdf->GetX(), $this->tcpdf->GetY()];
458
    }
459
460
    /**
461
     * @param $x
462
     * @param $y
463
     */
464
    private function stamp($x, $y)
465
    {
466
        $this->tcpdf->SetFontSize(6);
467
        $allSize = $this->getNoticeReceiptHeight() - 10;
468
469
        $first = ($allSize/10)*6;
470
        $last = ($allSize/10)*4;
471
472
        $this->tcpdf->SetXY($x + 4, $y + $this->padding);
473
        $this->tcpdf->Rect($x, $y, 38, $first, 'DF', [], [255, 255, 255]);
474
        $this->tcpdf->MultiCell(30, 7, 'CARIMBO UNIDADE DE ENTREGA', 0, 'C');
475
        $y += $first;
476
477
        $this->tcpdf->SetXY($x + 4, $y + $this->padding);
478
        $this->tcpdf->Rect($x, $y, 38, $last, 'DF', [], [255, 255, 255]);
479
        $this->tcpdf->MultiCell(30, 7, 'RUBRICA E MATRÍCULA DO CARTEIRO', 0, 'C');
480
        $this->tcpdf->SetFontSize(9);
481
    }
482
}
483