Test Setup Failed
Push — master ( aa40a2...69297c )
by Eduardo
06:24
created

Voucher::setMailingList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
namespace Eduardokum\CorreiosPhp\Render;
3
4
use Eduardokum\CorreiosPhp\Contracts\Config\Config as ConfigContract;
5
use Eduardokum\CorreiosPhp\Entities\MailingList;
6
use Eduardokum\CorreiosPhp\Entities\PostalObject;
7
use Eduardokum\CorreiosPhp\Exception\InvalidArgumentException;
8
9
class Voucher extends Pdf
10
{
11
    /**
12
     * @var MailingList
13
     */
14
    private $mailingList;
15
16
    /**
17
     * @var int
18
     */
19
    private $docCount = 0;
20
21
    /**
22
     * @var mixed
23
     */
24
    private $postalServices;
25
26
    public function __construct(MailingList $mailingList, ConfigContract $config = null)
27
    {
28
        parent::__construct($config);
29
        $this->postalServices = json_decode(file_get_contents(CORREIOS_PHP_BASE . '/storage/postal_service.json'));
30
        $this->setMailingList($mailingList);
31
32
        $this->tcpdf = new \TCPDF(null, 'mm', 'A4', true, 'UTF-8');
33
        $this->tcpdf->setPrintHeader(false);
34
        $this->tcpdf->setPrintFooter(false);
35
        $this->tcpdf->SetMargins(0, 0, 0, true);
36
        $this->tcpdf->SetCreator('Eduardokum\\CorreiosPhp');
37
        $this->tcpdf->SetAuthor('Eduardokum\\CorreiosPhp');
38
        $this->tcpdf->SetTitle('Voucher');
39
        $this->tcpdf->SetSubject('Voucher');
40
        $this->tcpdf->SetKeywords('Voucher');
41
        $this->tcpdf->SetAutoPageBreak(false, 0);
42
        $this->tcpdf->SetFont($this->fontRegular, '', 9, '', false);
43
    }
44
45
    /**
46
     * @return MailingList
47
     */
48
    public function getMailingList()
49
    {
50
        return $this->mailingList;
51
    }
52
53
    /**
54
     * @param MailingList $mailingList
55
     *
56
     * @return Voucher
57
     */
58
    public function setMailingList(MailingList $mailingList)
59
    {
60
        $this->mailingList = $mailingList;
61
        return $this;
62
    }
63
64
    /**
65
     * @param string $output
66
     *
67
     * @return string
68
     */
69
    public function render($output = 'I')
70
    {
71
        $services = $this->filterTags($this->mailingList->toPrint());
72
        if (count($services) == 0) {
73
            throw new InvalidArgumentException('No tags available for printing');
74
        }
75
76
        $this->tcpdf->addPage();
77
        $lastPosition = $this->voucher($services, 5, 10);
78
        $this->voucher($services, $lastPosition[0], $lastPosition[1]);
79
80
        return $this->tcpdf->Output('voucher.pdf', $output);
81
    }
82
83
    /**
84
     * @param array $tags
85
     *
86
     * @return array
87
     */
88
    private function filterTags(array $tags)
89
    {
90
        $mailingListParsed = [];
91
        foreach ($tags as $postalObject) {
92
            /** @var PostalObject $postalObject */
93
            $mailingListParsed[$postalObject->getService()][] = $postalObject;
94
        }
95
        return $mailingListParsed;
96
    }
97
98
    /**
99
     * @param array $services
100
     * @param       $x
101
     * @param       $y
102
     *
103
     * @return array
104
     */
105
    private function voucher(array $services, $x, $y)
106
    {
107
        $this->docCount++;
108
        $width = $this->tcpdf->getPageWidth() - ($x * 2);
109
110
        $lastPosition = $this->logo($x, $y);
111
        $lastPosition = $this->title($x, $lastPosition[1], $width);
112
        $lastPosition = $this->info($x, $lastPosition[1], $width);
113
        $this->barcode($x, $lastPosition[1]);
114
        $lastPosition = $this->services($services, $x, $lastPosition[1], $width);
115
        return $this->dash($x, $lastPosition[1], $width);
116
    }
117
118
    /**
119
     * @param $x
120
     * @param $y
121
     *
122
     * @return array
123
     */
124
    private function logo($x, $y)
125
    {
126
        $image = CORREIOS_PHP_BASE . '/resources/assets/images/correios.png';
127
        $this->tcpdf->Image($image, $x, $y - 1, null, 8, 'png', null, null, false, 300, null, false, false, 0, true);
128
129
        $this->tcpdf->SetXY($x + 55, $y);
130
        $this->tcpdf->SetFontSize(14);
131
        $this->writeBold(10, 'EMPRESA BRASILEITA DE CORREIOS E TELÉGRAFOS');
132
        $this->tcpdf->SetFontSize(9);
133
134
        $this->tcpdf->SetXY($x, $y + 10);
135
        return [$this->tcpdf->GetX(),$this->tcpdf->GetY()];
136
    }
137
138
    /**
139
     * @param $x
140
     * @param $y
141
     * @param $width
142
     *
143
     * @return array
144
     */
145
    private function title($x, $y, $width)
146
    {
147
        $this->tcpdf->SetXY($x, $y);
148
        $this->tcpdf->SetFont($this->fontBold, 'B', 14);
149
        $this->tcpdf->Cell($width, 10, 'PRÉ - LISTA DE POSTAGEM - PLP - SIGEP WEB', 1, 0, 'C', false);
150
        $this->tcpdf->SetFont($this->fontRegular, null, 9);
151
152
        $this->tcpdf->SetXY($x, $y + 10);
153
        return [$this->tcpdf->GetX(), $this->tcpdf->GetY()];
154
    }
155
156
    /**
157
     * @param $x
158
     * @param $y
159
     * @param $width
160
     *
161
     * @return array
162
     */
163
    private function info($x, $y, $width)
164
    {
165
        $xDefault = $x;
166
        $yDefault = $y;
167
168
        $x += $this->padding;
169
        $y += $this->padding;
170
171
        $this->tcpdf->SetXY($x, $y);
172
        $this->writeBold(5, 'SIGEP WEB - Gerenciador de postagens dos correios');
173
        $y += 5;
174
175
        $this->tcpdf->SetXY($x, $y);
176
        $this->writeBold(5, 'Contrato: ');
177
        $this->tcpdf->Write(5, $this->getConfig()->getContract());
178
        $y += 5;
179
180
        $this->tcpdf->SetXY($x, $y);
181
        $this->writeBold(5, 'Cliente: ');
182
        $this->tcpdf->Write(5, $this->getConfig()->getSender()->getName());
183
        $y += 5;
184
185
        $this->tcpdf->SetXY($x, $y);
186
        $this->writeBold(5, 'Telefone de contato: ');
187
        $this->tcpdf->Write(5, $this->getConfig()->getSender()->getPhone());
188
        $y += 5;
189
190
        $this->tcpdf->SetXY($x, $y);
191
        $this->writeBold(5, 'Email de contato: ');
192
        $this->tcpdf->Write(5, $this->getConfig()->getSender()->getMail());
193
        $y += 5;
194
195
        $this->tcpdf->Rect($xDefault, $yDefault, $width, $y - $yDefault);
196
197
        $this->tcpdf->SetXY($x, $y);
198
        return [$this->tcpdf->GetX(), $this->tcpdf->GetY()];
199
    }
200
201
    /**
202
     * @param $x
203
     * @param $y
204
     */
205
    private function barcode($x, $y)
206
    {
207
        $style = [
208
            'position' => '',
209
            'align'    => 'C',
210
            'fitwidth' => false,
211
            'border'   => 0,
212
            'hpadding' => 0,
213
            'vpadding' => 0,
214
            'fgcolor'  => [0, 0, 0],
215
            'bgcolor'  => [255, 255, 255],
216
            'text'     => false,
217
        ];
218
219
        $x += 140;
220
        $y -= 25;
221
222
        $this->tcpdf->SetXY($x + 12, $y);
223
        $this->writeBold(5, sprintf('Nº PLP: %d', $this->getMailingList()->getId()));
224
        $y += 5;
225
226
        $this->tcpdf->write1DBarcode($this->getMailingList()->getId(), 'C128', $x, $y, 50, null, null, $style);
227
    }
228
229
    /**
230
     * @param $services
231
     * @param $x
232
     * @param $y
233
     * @param $width
234
     *
235
     * @return array
236
     */
237
    private function services($services, $x, $y, $width)
238
    {
239
        $xDefault = $x;
240
        $yDefault = $y;
241
242
        $x += $this->padding;
243
        $y += $this->padding;
244
245
        $parts = $width / 10;
246
        $h = 7;
247
248
        $this->tcpdf->SetXY($x, $y);
249
        $this->tcpdf->SetFont($this->fontBold, 'B');
250
        $this->tcpdf->Cell($parts * 2, $h, 'Cód. Serviço:');
251
        $this->tcpdf->Cell($parts * 2, $h, 'Quantidade:');
252
        $this->tcpdf->Cell($parts * 3, $h, 'Serviço:');
253
        $this->tcpdf->SetFont($this->fontRegular, null);
254
        $y += $h;
255
256
        $sTot = 0;
257
        foreach ($services as $service => $tags) {
258
            $name = $this->postalServices->$service->name;
259
            $this->tcpdf->SetXY($x, $y);
260
            $this->tcpdf->Cell($parts * 2, $h, $service);
261
            $this->tcpdf->Cell($parts * 2, $h, $sTot = count($tags));
262
            $this->tcpdf->Cell($parts * 3, $h, $name);
263
            $y += $h;
264
        }
265
266
        $this->tcpdf->SetXY($x, $y);
267
        $this->tcpdf->Cell($parts * 2, $h, 'Total');
268
        $this->tcpdf->Cell($parts * 2, $h, $sTot);
269
        $this->tcpdf->Cell($parts * 3, $h, '');
270
        $yEnd = $y + $h + $this->padding;
271
272
        $x = $this->tcpdf->GetX();
273
        $y = $yDefault + 3;
274
        $this->tcpdf->SetXY($x, $y);
275
        $this->writeBold(5, 'Data de entraga: ____/____/______');
276
        $y += 5 + 5;
277
278
        $this->tcpdf->SetXY($x, $y);
279
        $this->tcpdf->Line($x, $y, $x + ($parts * 3) - 5, $y);
280
        $this->tcpdf->Write(4, 'Assinatura/Matrícula dos correios');
281
        $y += 4;
282
        
283
        $this->tcpdf->SetXY($x, $y);
284
        $this->tcpdf->Write(4, sprintf('%dº via - %s', $this->docCount, ($this->docCount == 1 ? 'correios' : 'cliente')));
285
286
        $this->tcpdf->SetXY($xDefault, $yEnd);
287
        $this->tcpdf->Rect($xDefault, $yDefault, $width, $yEnd - $yDefault);
288
289
        return [$this->tcpdf->GetX(), $yEnd];
290
    }
291
292
    /**
293
     * @param $x
294
     * @param $y
295
     * @param $width
296
     *
297
     * @return array
298
     */
299
    private function dash($x, $y, $width)
300
    {
301
        if ($this->docCount > 1) {
302
            return null;
303
        }
304
305
        $y += 3;
306
        $this->tcpdf->SetLineStyle(['dash' => '4,2']);
307
        $this->tcpdf->Line($x, $y, $x+$width, $y, []);
308
        $this->tcpdf->SetLineStyle(['dash' => 0]);
309
        $y += 5;
310
311
        $this->tcpdf->SetXY($x, $y);
312
        return [$this->tcpdf->GetX(), $this->tcpdf->GetY()];
313
    }
314
}