Test Setup Failed
Push — master ( 830d7c...657195 )
by Francimar
07:26
created

Pagamento::getNode()   B

Complexity

Conditions 9
Paths 19

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 9.0051

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 24
cts 25
cp 0.96
rs 8.0555
c 0
b 0
f 0
cc 9
nc 19
nop 1
crap 9.0051
1
<?php
2
/**
3
 * MIT License
4
 *
5
 * Copyright (c) 2016 MZ Desenvolvimento de Sistemas LTDA
6
 *
7
 * @author Francimar Alves <[email protected]>
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
namespace NFe\Entity;
29
30
use NFe\Common\Node;
31
use NFe\Common\Util;
32
33
class Pagamento implements Node
34
{
35
    /**
36
     * Indicador da forma de pagamento: 0 – pagamento à vista; 1 – pagamento à
37
     * prazo.
38
     */
39
    const INDICADOR_AVISTA = 'avista';
40
    const INDICADOR_APRAZO = 'aprazo';
41
42
    /**
43
     * Forma de Pagamento:01-Dinheiro;02-Cheque;03-Cartão de Crédito;04-Cartão
44
     * de Débito;05-Crédito Loja;10-Vale Alimentação;11-Vale Refeição;12-Vale
45
     * Presente;13-Vale Combustível;99 - Outros
46
     */
47
    const FORMA_DINHEIRO = 'dinheiro';
48
    const FORMA_CHEQUE = 'cheque';
49
    const FORMA_CREDITO = 'credito';
50
    const FORMA_DEBITO = 'debito';
51
    const FORMA_CREDIARIO = 'crediario';
52
    const FORMA_ALIMENTACAO = 'alimentacao';
53
    const FORMA_REFEICAO = 'refeicao';
54
    const FORMA_PRESENTE = 'presente';
55
    const FORMA_COMBUSTIVEL = 'combustivel';
56
    const FORMA_OUTROS = 'outros';
57
58
    /**
59
     * Bandeira da operadora de cartão de crédito/débito:01–Visa;
60
     * 02–Mastercard; 03–American Express; 04–Sorocred; 99–Outros
61
     */
62
    const BANDEIRA_VISA = 'visa';
63
    const BANDEIRA_MASTERCARD = 'mastercard';
64
    const BANDEIRA_AMEX = 'amex';
65
    const BANDEIRA_SOROCRED = 'sorocred';
66
    const BANDEIRA_OUTROS = 'outros';
67
68
    /**
69
     * Indicador da forma de pagamento: 0 – pagamento à vista; 1 – pagamento à
70
     * prazo.
71
     */
72
    private $indicador;
73
    private $forma;
74
    private $valor;
75
    private $integrado;
76
    private $credenciadora;
77
    private $autorizacao;
78
    private $bandeira;
79
80 42
    public function __construct($pagamento = [])
81
    {
82 42
        $this->fromArray($pagamento);
83 42
    }
84
85
    /**
86
     * Indicador da forma de pagamento: 0 – pagamento à vista; 1 – pagamento à
87
     * prazo.
88
     * @param boolean $normalize informa se o indicador deve estar no formato do XML
89
     * @return mixed indicador da Nota
90
     */
91 40
    public function getIndicador($normalize = false)
92
    {
93 40
        if (!$normalize) {
94 40
            return $this->indicador;
95
        }
96 1
        switch ($this->indicador) {
97 1
            case self::INDICADOR_AVISTA:
98 1
                return '0';
99 1
            case self::INDICADOR_APRAZO:
100 1
                return '1';
101
        }
102 1
        return $this->indicador;
103
    }
104
105
    /**
106
     * Altera o valor do Indicador para o informado no parâmetro
107
     * @param mixed $indicador novo valor para Indicador
108
     * @return Nota A própria instância da classe
109
     */
110 42
    public function setIndicador($indicador)
111
    {
112
        switch ($indicador) {
113 42
            case '0':
114 1
                $indicador = self::INDICADOR_AVISTA;
115 1
                break;
116 42
            case '1':
117 1
                $indicador = self::INDICADOR_APRAZO;
118 1
                break;
119
        }
120 42
        $this->indicador = $indicador;
121 42
        return $this;
122
    }
123
124
    /**
125
     * Forma de Pagamento:01-Dinheiro;02-Cheque;03-Cartão de Crédito;04-Cartão
126
     * de Débito;05-Crédito Loja;10-Vale Alimentação;11-Vale Refeição;12-Vale
127
     * Presente;13-Vale Combustível;99 - Outros
128
     */
129 39
    public function getForma($normalize = false)
130
    {
131 39
        if (!$normalize) {
132 39
            return $this->forma;
133
        }
134 38
        switch ($this->forma) {
135 38
            case self::FORMA_DINHEIRO:
136 34
                return '01';
137 36
            case self::FORMA_CHEQUE:
138
                return '02';
139 36
            case self::FORMA_CREDITO:
140 34
                return '03';
141 2
            case self::FORMA_DEBITO:
142 2
                return '04';
143
            case self::FORMA_CREDIARIO:
144
                return '05';
145
            case self::FORMA_ALIMENTACAO:
146
                return '10';
147
            case self::FORMA_REFEICAO:
148
                return '11';
149
            case self::FORMA_PRESENTE:
150
                return '12';
151
            case self::FORMA_COMBUSTIVEL:
152
                return '13';
153
            case self::FORMA_OUTROS:
154
                return '99';
155
        }
156
        return $this->forma;
157
    }
158
159 42
    public function setForma($forma)
160
    {
161
        switch ($forma) {
162 42
            case '01':
163 29
                $forma = self::FORMA_DINHEIRO;
164 29
                break;
165 42
            case '02':
166
                $forma = self::FORMA_CHEQUE;
167
                break;
168 42
            case '03':
169 29
                $forma = self::FORMA_CREDITO;
170 29
                break;
171 42
            case '04':
172 1
                $forma = self::FORMA_DEBITO;
173 1
                break;
174 42
            case '05':
175
                $forma = self::FORMA_CREDIARIO;
176
                break;
177 42
            case '10':
178
                $forma = self::FORMA_ALIMENTACAO;
179
                break;
180 42
            case '11':
181
                $forma = self::FORMA_REFEICAO;
182
                break;
183 42
            case '12':
184
                $forma = self::FORMA_PRESENTE;
185
                break;
186 42
            case '13':
187
                $forma = self::FORMA_COMBUSTIVEL;
188
                break;
189 42
            case '99':
190
                $forma = self::FORMA_OUTROS;
191
                break;
192
        }
193 42
        $this->forma = $forma;
194 42
        return $this;
195
    }
196
197
    /**
198
     * Valor do Pagamento
199
     */
200 40
    public function getValor($normalize = false)
201
    {
202 40
        if (!$normalize) {
203 40
            return $this->valor;
204
        }
205 40
        return Util::toCurrency($this->valor);
206
    }
207
208 42
    public function setValor($valor)
209
    {
210 42
        $valor = floatval($valor);
211 42
        $this->valor = $valor;
212 42
        return $this;
213
    }
214
215
    /**
216
     * Tipo de Integração do processo de pagamento com o sistema de automação
217
     * da empresa/1=Pagamento integrado com o sistema de automação da empresa
218
     * Ex. equipamento TEF , Comercio Eletronico 2=Pagamento não integrado com
219
     * o sistema de automação da empresa Ex: equipamento POS
220
     */
221 38
    public function getIntegrado($normalize = false)
222
    {
223 38
        if (!$normalize) {
224 10
            return $this->integrado;
225
        }
226 36
        return $this->isIntegrado()?'1':'2';
227
    }
228
229
    /**
230
     * Tipo de Integração do processo de pagamento com o sistema de automação
231
     * da empresa/1=Pagamento integrado com o sistema de automação da empresa
232
     * Ex. equipamento TEF , Comercio Eletronico 2=Pagamento não integrado com
233
     * o sistema de automação da empresa Ex: equipamento POS
234
     */
235 36
    public function isIntegrado()
236
    {
237 36
        return $this->integrado == 'Y';
238
    }
239
240 42
    public function setIntegrado($integrado)
241
    {
242 42
        if (is_bool($integrado)) {
243
            $integrado = $integrado ? 'Y': 'N';
244
        }
245 42
        $this->integrado = in_array($integrado, ['Y', '1']) ? 'Y' : 'N';
246 42
        return $this;
247
    }
248
249
    /**
250
     * CNPJ da credenciadora de cartão de crédito/débito
251
     */
252 38
    public function getCredenciadora($normalize = false)
253
    {
254 38
        if (!$normalize) {
255 37
            return $this->credenciadora;
256
        }
257 2
        return $this->credenciadora;
258
    }
259
260 42
    public function setCredenciadora($credenciadora)
261
    {
262 42
        $this->credenciadora = $credenciadora;
263 42
        return $this;
264
    }
265
266
    /**
267
     * Número de autorização da operação cartão de crédito/débito
268
     */
269 11
    public function getAutorizacao($normalize = false)
270
    {
271 11
        if (!$normalize) {
272 10
            return $this->autorizacao;
273
        }
274 2
        return $this->autorizacao;
275
    }
276
277 42
    public function setAutorizacao($autorizacao)
278
    {
279 42
        $this->autorizacao = $autorizacao;
280 42
        return $this;
281
    }
282
283
    /**
284
     * Bandeira da operadora de cartão de crédito/débito:01–Visa;
285
     * 02–Mastercard; 03–American Express; 04–Sorocred; 99–Outros
286
     */
287 38
    public function getBandeira($normalize = false)
288
    {
289 38
        if (!$normalize) {
290 38
            return $this->bandeira;
291
        }
292 36
        switch ($this->bandeira) {
293 36
            case self::BANDEIRA_VISA:
294 2
                return '01';
295 34
            case self::BANDEIRA_MASTERCARD:
296 34
                return '02';
297
            case self::BANDEIRA_AMEX:
298
                return '03';
299
            case self::BANDEIRA_SOROCRED:
300
                return '04';
301
            case self::BANDEIRA_OUTROS:
302
                return '99';
303
        }
304
        return $this->bandeira;
305
    }
306
307 42
    public function setBandeira($bandeira)
308
    {
309
        switch ($bandeira) {
310 42
            case '01':
311 1
                $bandeira = self::BANDEIRA_VISA;
312 1
                break;
313 42
            case '02':
314 29
                $bandeira = self::BANDEIRA_MASTERCARD;
315 29
                break;
316 42
            case '03':
317
                $bandeira = self::BANDEIRA_AMEX;
318
                break;
319 42
            case '04':
320
                $bandeira = self::BANDEIRA_SOROCRED;
321
                break;
322 42
            case '99':
323
                $bandeira = self::BANDEIRA_OUTROS;
324
                break;
325
        }
326 42
        $this->bandeira = $bandeira;
327 42
        return $this;
328
    }
329
330
    /**
331
     * Informa se o pagamento é em cartão
332
     */
333 38
    public function isCartao()
334
    {
335 38
        return in_array($this->getForma(), [self::FORMA_CREDITO, self::FORMA_DEBITO]);
336
    }
337
338 10
    public function toArray($recursive = false)
0 ignored issues
show
Unused Code introduced by
The parameter $recursive is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
339
    {
340 10
        $pagamento = [];
341 10
        $pagamento['indicador'] = $this->getIndicador();
342 10
        $pagamento['forma'] = $this->getForma();
343 10
        $pagamento['valor'] = $this->getValor();
344 10
        $pagamento['integrado'] = $this->getIntegrado();
345 10
        $pagamento['credenciadora'] = $this->getCredenciadora();
346 10
        $pagamento['autorizacao'] = $this->getAutorizacao();
347 10
        $pagamento['bandeira'] = $this->getBandeira();
348 10
        return $pagamento;
349
    }
350
351 42
    public function fromArray($pagamento = [])
352
    {
353 42
        if ($pagamento instanceof Pagamento) {
354 9
            $pagamento = $pagamento->toArray();
355 42
        } elseif (!is_array($pagamento)) {
356 9
            return $this;
357
        }
358 42
        if (isset($pagamento['indicador'])) {
359
            $this->setIndicador($pagamento['indicador']);
360
        } else {
361 42
            $this->setIndicador(null);
362
        }
363 42
        if (isset($pagamento['forma'])) {
364 9
            $this->setForma($pagamento['forma']);
365
        } else {
366 42
            $this->setForma(null);
367
        }
368 42
        if (isset($pagamento['valor'])) {
369 9
            $this->setValor($pagamento['valor']);
370
        } else {
371 42
            $this->setValor(null);
372
        }
373 42
        if (!isset($pagamento['integrado'])) {
374 42
            $this->setIntegrado('N');
375
        } else {
376 9
            $this->setIntegrado($pagamento['integrado']);
377
        }
378 42
        if (isset($pagamento['credenciadora'])) {
379 1
            $this->setCredenciadora($pagamento['credenciadora']);
380
        } else {
381 42
            $this->setCredenciadora(null);
382
        }
383 42
        if (isset($pagamento['autorizacao'])) {
384 1
            $this->setAutorizacao($pagamento['autorizacao']);
385
        } else {
386 42
            $this->setAutorizacao(null);
387
        }
388 42
        if (isset($pagamento['bandeira'])) {
389 7
            $this->setBandeira($pagamento['bandeira']);
390
        } else {
391 42
            $this->setBandeira(null);
392
        }
393 42
        return $this;
394
    }
395
396 40
    public function getNode($name = null)
397
    {
398 40
        $dom = new \DOMDocument('1.0', 'UTF-8');
399 40
        if ($this->getValor() < 0) {
400 4
            $element = $dom->createElement(is_null($name)?'vTroco':$name);
401 4
            $this->setValor(-$this->getValor());
402 4
            $element->appendChild($dom->createTextNode($this->getValor(true)));
403 4
            $this->setValor(-$this->getValor());
404 4
            return $element;
405
        }
406 38
        $element = $dom->createElement(is_null($name)?'detPag':$name);
407 38
        if (!is_null($this->getIndicador())) {
408
            Util::appendNode($element, 'indPag', $this->getIndicador(true));
409
        }
410 38
        Util::appendNode($element, 'tPag', $this->getForma(true));
411 38
        Util::appendNode($element, 'vPag', $this->getValor(true));
412 38
        if (!$this->isCartao()) {
413 34
            return $element;
414
        }
415 36
        $cartao = $dom->createElement('card');
416 36
        Util::appendNode($cartao, 'tpIntegra', $this->getIntegrado(true));
417 36
        if ($this->isIntegrado()) {
418 2
            Util::appendNode($cartao, 'CNPJ', $this->getCredenciadora(true));
419
        }
420 36
        if (!is_null($this->getBandeira())) {
421 36
            Util::appendNode($cartao, 'tBand', $this->getBandeira(true));
422
        }
423 36
        if ($this->isIntegrado()) {
424 2
            Util::appendNode($cartao, 'cAut', $this->getAutorizacao(true));
425
        }
426 36
        $element->appendChild($cartao);
427 36
        return $element;
428
    }
429
430 32
    public function loadNode($element, $name = null)
431
    {
432 32
        $name = is_null($name)?'detPag':$name;
433 32
        if ($name == 'vTroco') {
434 3
            $this->setValor(
435 3
                '-'.Util::loadNode(
436 3
                    $element,
437 3
                    'vTroco',
438 3
                    'Tag "vTroco" do campo "Valor" não encontrada'
439
                )
440
            );
441 3
            return $element;
442
        }
443 31
        if ($element->nodeName != $name) {
444
            $_fields = $element->getElementsByTagName($name);
445
            if ($_fields->length == 0) {
446
                throw new \Exception('Tag "'.$name.'" não encontrada', 404);
447
            }
448
            $element = $_fields->item(0);
449
        }
450 31
        $this->setIndicador(
451 31
            Util::loadNode(
452 31
                $element,
453 31
                'indPag'
454
            )
455
        );
456 31
        $this->setForma(
457 31
            Util::loadNode(
458 31
                $element,
459 31
                'tPag',
460 31
                'Tag "tPag" do campo "Forma" não encontrada'
461
            )
462
        );
463 31
        $this->setValor(
464 31
            Util::loadNode(
465 31
                $element,
466 31
                'vPag',
467 31
                'Tag "vPag" do campo "Valor" não encontrada'
468
            )
469
        );
470 31
        $integrado = Util::loadNode($element, 'tpIntegra');
471 31
        if (is_null($integrado) && $this->isCartao()) {
472
            throw new \Exception('Tag "tpIntegra" do campo "Integrado" não encontrada', 404);
473
        }
474 31
        $this->setIntegrado($integrado);
475 31
        $this->setCredenciadora(Util::loadNode($element, 'CNPJ'));
476 31
        $autorizacao = Util::loadNode($element, 'cAut');
477 31
        if (is_null($autorizacao) && $this->isCartao() && is_numeric($this->getCredenciadora())) {
478
            throw new \Exception('Tag "cAut" do campo "Autorizacao" não encontrada', 404);
479
        }
480 31
        $this->setAutorizacao($autorizacao);
481 31
        $bandeira = Util::loadNode($element, 'tBand');
482 31
        if (is_null($bandeira) && $this->isCartao() && is_numeric($this->getCredenciadora())) {
483
            throw new \Exception('Tag "tBand" do campo "Bandeira" não encontrada', 404);
484
        }
485 31
        $this->setBandeira($bandeira);
486 31
        return $element;
487
    }
488
}
489