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

DetailedListing::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
8
class DetailedListing extends Pdf
9
{
10
    /**
11
     * @var MailingList
12
     */
13
    private $mailingList;
14
15
    /**
16
     * @var mixed
17
     */
18
    private $postalServices;
19
20
    public function __construct(MailingList $mailingList, ConfigContract $config = null)
21
    {
22
        parent::__construct($config);
23
        $this->postalServices = json_decode(file_get_contents(CORREIOS_PHP_BASE . '/storage/postal_service.json'));
24
        $this->setMailingList($mailingList);
25
26
        $this->tcpdf = new \TCPDF('L', 'mm', 'A4', true, 'UTF-8');
27
        $this->tcpdf->setPrintHeader(false);
28
        $this->tcpdf->setPrintFooter(false);
29
        $this->tcpdf->SetMargins(0, 0, 0, true);
30
        $this->tcpdf->SetCreator('Eduardokum\\CorreiosPhp');
31
        $this->tcpdf->SetAuthor('Eduardokum\\CorreiosPhp');
32
        $this->tcpdf->SetTitle('DetailedListing');
33
        $this->tcpdf->SetSubject('DetailedListing');
34
        $this->tcpdf->SetKeywords('DetailedListing');
35
        $this->tcpdf->SetAutoPageBreak(false, 0);
36
        $this->tcpdf->SetFont($this->fontRegular, '', 9, '', false);
37
    }
38
39
    /**
40
     * @return MailingList
41
     */
42
    public function getMailingList()
43
    {
44
        return $this->mailingList;
45
    }
46
47
    /**
48
     * @param MailingList $mailingList
49
     *
50
     * @return DetailedListing
51
     */
52
    public function setMailingList(MailingList $mailingList)
53
    {
54
        $this->mailingList = $mailingList;
55
        return $this;
56
    }
57
58
    /**
59
     * @param string $output
60
     *
61
     * @return string
62
     */
63
    public function render($output = 'I')
64
    {
65
        $this->tcpdf->addPage();
66
        $x =  5;
67
        $y = 10;
68
69
        $width = $this->tcpdf->getPageWidth() - ($x * 2);
70
71
        $lastPosition = $this->logo($x, $y);
72
        $lastPosition = $this->title($x, $lastPosition[1], $width);
73
        $lastPosition = $this->info($x, $lastPosition[1], $width);
74
        $lastPosition = $this->tags($this->getMailingList()->toPrint(), $x, $lastPosition[1], $width);
75
        $this->signature($x, $lastPosition[1], $width);
76
77
        if ($lastPosition[1] + 35 + 10 > $this->tcpdf->getPageHeight()) {
78
            $this->tcpdf->addPage();
79
        }
80
81
        return $this->tcpdf->Output('detailed_listing.pdf', $output);
82
    }
83
84
    /**
85
     * @param $x
86
     * @param $y
87
     *
88
     * @return array
89
     */
90
    private function logo($x, $y)
91
    {
92
        $image = CORREIOS_PHP_BASE . '/resources/assets/images/correios.png';
93
        $this->tcpdf->Image($image, $x, $y - 1, null, 8, 'png', null, null, false, 300, null, false, false, 0, true);
94
95
        $this->tcpdf->SetXY($x + 55, $y);
96
        $this->tcpdf->SetFontSize(14);
97
        $this->writeBold(10, 'EMPRESA BRASILEITA DE CORREIOS E TELÉGRAFOS');
98
        $this->tcpdf->SetFontSize(9);
99
100
        $this->tcpdf->SetXY($x, $y + 10);
101
        return [$this->tcpdf->GetX(),$this->tcpdf->GetY()];
102
    }
103
104
    /**
105
     * @param $x
106
     * @param $y
107
     * @param $width
108
     *
109
     * @return array
110
     */
111
    private function title($x, $y, $width)
112
    {
113
        $this->tcpdf->SetXY($x, $y);
114
        $this->tcpdf->SetFont($this->fontBold, 'B', 14);
115
        $this->tcpdf->Cell($width, 10, 'LISTA DE POSTAGEM', 1, 0, 'C', false);
116
        $this->tcpdf->SetFont($this->fontRegular, null, 9);
117
118
        $this->tcpdf->SetXY($x, $y + 10);
119
        return [$this->tcpdf->GetX(), $this->tcpdf->GetY()];
120
    }
121
122
    /**
123
     * @param $x
124
     * @param $y
125
     * @param $width
126
     *
127
     * @return array
128
     */
129
    private function info($x, $y, $width)
130
    {
131
        $xDefault = $x;
132
        $yDefault = $y;
133
134
        $x += $this->padding;
135
        $y += $this->padding;
136
137
        $part = ($width - 10) / 3;
138
139
        $this->tcpdf->SetXY($x, $y);
140
        $this->writeBold(5, 'Nº da lista: ');
141
        $this->tcpdf->Write(5, $this->getMailingList()->getId());
142
        $y += 5;
143
144
        $this->tcpdf->SetXY($x, $y);
145
        $this->writeBold(5, 'Contrato: ');
146
        $this->tcpdf->Write(5, $this->getConfig()->getContract());
147
        $y += 5;
148
149
        $this->tcpdf->SetXY($x, $y);
150
        $this->writeBold(5, 'Remetente: ');
151
        $this->tcpdf->Write(5, $this->getConfig()->getSender()->getName());
152
        $y += 5;
153
154
        $this->tcpdf->SetXY($x, $y);
155
        $this->writeBold(5, 'Telefone de contato: ');
156
        $this->tcpdf->Write(5, $this->getConfig()->getSender()->getPhone());
157
        $y += 5;
158
159
        $this->tcpdf->SetXY($x, $y);
160
        $this->writeBold(5, 'Email de contato: ');
161
        $this->tcpdf->Write(5, $this->getConfig()->getSender()->getMail());
162
        $yEnd = $y + 5;
163
164
        $x = $part + $this->padding;
165
        $y = $yDefault + $this->padding;
166
167
        $this->tcpdf->SetXY($x, $y);
168
        $this->writeBold(5, 'Cliente: ');
169
        $this->tcpdf->Write(5, $this->getConfig()->getSender()->getName());
170
        $y += 5;
171
172
        $this->tcpdf->SetXY($x, $y);
173
        $this->writeBold(5, 'Cód. administrativo: ');
174
        $this->tcpdf->Write(5, $this->getConfig()->getAdministrativeCode());
175
        $y += 5;
176
177
        $this->tcpdf->SetXY($x, $y);
178
        $this->writeBold(5, 'Endereço: ');
179
        $this->tcpdf->Write(5, trim(vsprintf('%s, %s %s', [
180
            $this->getConfig()->getSender()->getStreet(),
181
            $this->getConfig()->getSender()->getNumber(),
182
            implode('-', str_split($this->getConfig()->getSender()->getCep(), 5)),
183
        ])));
184
        $y += 5;
185
        $this->tcpdf->SetXY($x, $y);
186
        $this->tcpdf->Write(5, trim(vsprintf('%s - %s - %s/%s', [
187
            $this->getConfig()->getSender()->getComplement(),
188
            $this->getConfig()->getSender()->getDistrict(),
189
            $this->getConfig()->getSender()->getCity(),
190
            $this->getConfig()->getSender()->getState(),
191
        ])));
192
193
        $x = ($part * 2) + 20;
194
        $y = $yDefault + $this->padding;
195
196
        $this->tcpdf->SetXY($x, $y);
197
        $this->writeBold(5, 'Cartão: ');
198
        $this->tcpdf->Write(5, $this->getConfig()->getPostCard());
199
200
        $this->tcpdf->Rect($xDefault, $yDefault, $width, $yEnd - $yDefault);
201
202
        $this->tcpdf->SetXY($xDefault, $yEnd);
203
        return [$this->tcpdf->GetX(), $this->tcpdf->GetY()];
204
    }
205
206
    private function tags(array $tags, $x, $y, $width)
207
    {
208
        $xDefault = $x;
209
        $yDefault = $y;
210
211
        $x += $this->padding;
212
        $y += $this->padding;
213
214
        $parts = $width / 100;
215
        $h = 6;
216
217
        $this->tcpdf->SetXY($x, $y);
218
        $this->tcpdf->SetFont($this->fontBold, 'B');
219
        $this->tcpdf->Cell($parts * 10, $h, 'Nº do objeto');
220
        $this->tcpdf->Cell($parts * 15, $h, 'Serviço:');
221
        $this->tcpdf->Cell($parts * 8, $h, 'CEP:');
222
        $this->tcpdf->Cell($parts * 8, $h, 'Peso(g):');
223
        $this->tcpdf->Cell($parts * 3, $h, 'AR:');
224
        $this->tcpdf->Cell($parts * 3, $h, 'MP:');
225
        $this->tcpdf->Cell($parts * 3, $h, 'VD:');
226
        $this->tcpdf->Cell($parts * 10, $h, 'Valor declarado:');
227
        $this->tcpdf->Cell($parts * 10, $h, 'Nota fiscal:');
228
        $this->tcpdf->Cell($parts * 6, $h, 'Volume:');
229
        $this->tcpdf->Cell($parts * 24, $h, 'Destinatário:');
230
        $this->tcpdf->SetFont($this->fontRegular, null);
231
        $y += $h;
232
233
        $fill = [
234
            0 => 210,
235
            1 => 255,
236
        ];
237
238
        $i = 0;
239
        foreach ($tags as $tag) {
240
            /** @var PostalObject $tag */
241
            $this->tcpdf->SetFillColor($fill[$i%2]);
242
            $name = $this->postalServices->{$tag->getService()}->name;
243
            $this->tcpdf->SetXY($x, $y);
244
            $this->tcpdf->Cell($parts * 10, $h, $tag->getTagDv(), 0, 0, '', true);
245
            $this->tcpdf->Cell($parts * 15, $h, $name, 0, 0, '', true);
246
            $this->tcpdf->Cell($parts * 8, $h, $tag->getRecipient()->getCep(), 0, 0, '', true);
247
            $this->tcpdf->Cell($parts * 8, $h, $tag->getWeight(), 0, 0, '', true);
248
            $this->tcpdf->Cell($parts * 3, $h, in_array('001', $tag->getAdditionalServices()) ? 'S' : 'N', 0, 0, '', true);
249
            $this->tcpdf->Cell($parts * 3, $h, in_array('002', $tag->getAdditionalServices()) ? 'S' : 'N', 0, 0, '', true);
250
            $this->tcpdf->Cell($parts * 3, $h, in_array('019', $tag->getAdditionalServices()) ? 'S' : 'N', 0, 0, '', true);
251
            $this->tcpdf->Cell($parts * 10, $h, $tag->getValueDeclared(), 0, 0, '', true);
252
            $this->tcpdf->Cell($parts * 10, $h, $tag->getInvoiceNumber(), 0, 0, '', true);
253
            $this->tcpdf->Cell($parts * 6, $h, '1/1', 0, 0, '', true);
254
            $this->tcpdf->Cell(($parts * 24) -1.5, $h, $tag->getRecipient()->getName(), 0, 0, '', true);
255
            $y += $h;
256
            $i++;
257
        }
258
        $this->tcpdf->SetFillColor(255);
259
260
        $y += 1;
261
        $this->tcpdf->SetXY($xDefault, $y);
262
        $this->tcpdf->Rect($xDefault, $yDefault, $width, $y - $yDefault);
263
264
        return [$this->tcpdf->GetX(), $this->tcpdf->GetY()];
265
    }
266
267
    private function signature($x, $y, $width)
268
    {
269
        $yDefault = $y;
270
271
        $this->tcpdf->Rect($x, $y, $width, 35);
272
        $x += 4;
273
        $y += 4;
274
275
        $this->tcpdf->SetXY($x, $y);
276
        $this->writeBold(5, 'APRESENTAR ESSA LISTA EM CASO DE PEDIDO DE INFORMAÇÕES');
277
        $y += 5 + 2;
278
279
        $this->tcpdf->SetXY($x, $y);
280
        $this->tcpdf->Write(5, 'Estou ciente do disposto na cláusula terceira do contrato de prestação de serviços');
281
        $y += 5 + 12;
282
        $x += 40;
283
284
        $this->tcpdf->SetXY($x, $y);
285
        $this->tcpdf->Line($x - 15, $y, $x + 65, $y);
286
        $this->tcpdf->Write(5, 'ASSINATURA DO REMETENTE');
287
288
        $x = $this->tcpdf->getPageWidth() - 100;
289
        $y = $yDefault + 4;
290
291
        $this->tcpdf->SetXY($x, $y);
292
        $this->tcpdf->Write(5, 'Carimbo e assinatura/matrícula dos correios');
293
    }
294
}
295