|
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\Core; |
|
29
|
|
|
|
|
30
|
|
|
use NFe\Common\Util; |
|
31
|
|
|
use NFe\Entity\Imposto; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Classe para validação da nota fiscal eletrônica do consumidor |
|
35
|
|
|
*/ |
|
36
|
|
|
class NFCe extends Nota |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
const QRCODE_VERSAO = '100'; |
|
40
|
|
|
|
|
41
|
|
|
private $consulta_url; |
|
42
|
|
|
|
|
43
|
7 |
|
public function __construct($nfce = array()) |
|
44
|
|
|
{ |
|
45
|
7 |
|
parent::__construct($nfce); |
|
46
|
7 |
|
$this->setModelo(self::MODELO_NFCE); |
|
47
|
7 |
|
$this->setFormato(self::FORMATO_CONSUMIDOR); |
|
48
|
7 |
|
} |
|
49
|
|
|
|
|
50
|
4 |
|
public function getConsultaURL($normalize = false) |
|
51
|
|
|
{ |
|
52
|
4 |
|
if (!$normalize) { |
|
53
|
2 |
|
return $this->consulta_url; |
|
54
|
|
|
} |
|
55
|
3 |
|
return $this->consulta_url; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
7 |
|
public function setConsultaURL($consulta_url) |
|
59
|
|
|
{ |
|
60
|
7 |
|
$this->consulta_url = $consulta_url; |
|
61
|
7 |
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
public function toArray() |
|
65
|
|
|
{ |
|
66
|
2 |
|
$nfce = parent::toArray(); |
|
67
|
2 |
|
$nfce['consulta_url'] = $this->getConsultaURL(); |
|
68
|
2 |
|
return $nfce; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
7 |
|
public function fromArray($nfce = array()) |
|
72
|
|
|
{ |
|
73
|
7 |
|
if ($nfce instanceof NFCe) { |
|
74
|
2 |
|
$nfce = $nfce->toArray(); |
|
75
|
7 |
|
} elseif (!is_array($nfce)) { |
|
76
|
2 |
|
return $this; |
|
77
|
|
|
} |
|
78
|
7 |
|
parent::fromArray($nfce); |
|
79
|
7 |
|
if (isset($nfce['consulta_url'])) { |
|
80
|
|
|
$this->setConsultaURL($nfce['consulta_url']); |
|
81
|
|
|
} else { |
|
82
|
7 |
|
$this->setConsultaURL(null); |
|
83
|
|
|
} |
|
84
|
7 |
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
3 |
|
private function gerarQRCodeInfo(&$dom) |
|
88
|
|
|
{ |
|
89
|
3 |
|
$config = SEFAZ::getInstance()->getConfiguracao(); |
|
90
|
3 |
|
$totais = $this->getTotais(); |
|
91
|
3 |
|
$digest = $dom->getElementsByTagName('DigestValue')->item(0); |
|
92
|
|
|
// if($this->getEmissao() == self::EMISSAO_NORMAL) |
|
93
|
3 |
|
$dig_val = $digest->nodeValue; |
|
94
|
|
|
// else |
|
95
|
|
|
// $dig_val = base64_encode(sha1($dom->saveXML(), true)); |
|
96
|
|
|
$params = array( |
|
97
|
3 |
|
'chNFe' => $this->getID(), |
|
98
|
3 |
|
'nVersao' => self::QRCODE_VERSAO, |
|
99
|
3 |
|
'tpAmb' => $this->getAmbiente(true), |
|
100
|
3 |
|
'cDest' => null, |
|
101
|
3 |
|
'dhEmi' => Util::toHex($this->getDataEmissao(true)), |
|
102
|
3 |
|
'vNF' => Util::toCurrency($totais['nota']), |
|
103
|
3 |
|
'vICMS' => Util::toCurrency($totais[Imposto::GRUPO_ICMS]), |
|
104
|
3 |
|
'digVal' => Util::toHex($dig_val), |
|
105
|
3 |
|
'cIdToken' => Util::padDigit($config->getToken(), 6), |
|
106
|
|
|
'cHashQRCode' => null |
|
107
|
3 |
|
); |
|
108
|
3 |
|
if (!is_null($this->getDestinatario())) { |
|
109
|
3 |
|
$params['cDest'] = $this->getDestinatario()->getID(true); |
|
110
|
3 |
|
} else { |
|
111
|
|
|
unset($params['cDest']); |
|
112
|
|
|
} |
|
113
|
3 |
|
$_params = $params; |
|
114
|
3 |
|
unset($_params['cHashQRCode']); |
|
115
|
3 |
|
$query = http_build_query($_params); |
|
116
|
3 |
|
$params['cHashQRCode'] = sha1($query.$config->getCSC()); |
|
117
|
3 |
|
return $params; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
3 |
|
private function checkQRCode(&$dom) |
|
121
|
|
|
{ |
|
122
|
3 |
|
$estado = $this->getEmitente()->getEndereco()->getMunicipio()->getEstado(); |
|
123
|
3 |
|
$db = SEFAZ::getInstance()->getConfiguracao()->getBanco(); |
|
|
|
|
|
|
124
|
3 |
|
$params = $this->gerarQRCodeInfo($dom); |
|
125
|
3 |
|
$query = http_build_query($params); |
|
126
|
3 |
|
$info = $db->getInformacaoServico($this->getEmissao(), $estado->getUF(), 'nfce', $this->getAmbiente()); |
|
127
|
3 |
|
if (!isset($info['qrcode'])) { |
|
128
|
|
|
throw new \Exception('Não existe URL de consulta de QRCode para o estado "'.$estado->getUF().'"', 404); |
|
129
|
|
|
} |
|
130
|
3 |
|
$url = $info['qrcode']; |
|
131
|
3 |
|
$url .= (strpos($url, '?') === false?'?':'&').$query; |
|
132
|
3 |
|
$this->setConsultaURL($url); |
|
133
|
3 |
|
} |
|
134
|
|
|
|
|
135
|
3 |
|
private function getNodeSuplementar(&$dom) |
|
136
|
|
|
{ |
|
137
|
3 |
|
$this->checkQRCode($dom); |
|
138
|
3 |
|
$element = $dom->createElement('infNFeSupl'); |
|
139
|
3 |
|
$qrcode = $dom->createElement('qrCode'); |
|
140
|
3 |
|
$data = $dom->createCDATASection($this->getConsultaURL(true)); |
|
141
|
3 |
|
$qrcode->appendChild($data); |
|
142
|
3 |
|
$element->appendChild($qrcode); |
|
143
|
3 |
|
return $element; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
3 |
|
public function loadNode($element, $name = null) |
|
147
|
|
|
{ |
|
148
|
3 |
|
$element = parent::loadNode($element, $name); |
|
149
|
3 |
|
$_fields = $element->getElementsByTagName('qrCode'); |
|
150
|
3 |
|
$_sig_fields = $element->getElementsByTagName('Signature'); |
|
151
|
3 |
|
$consulta_url = null; |
|
152
|
3 |
|
if ($_fields->length > 0) { |
|
153
|
2 |
|
$consulta_url = $_fields->item(0)->nodeValue; |
|
154
|
3 |
|
} elseif ($_sig_fields->length > 0) { |
|
155
|
|
|
throw new \Exception('Tag "qrCode" não encontrada', 404); |
|
156
|
|
|
} |
|
157
|
3 |
|
$this->setConsultaURL($consulta_url); |
|
158
|
3 |
|
return $element; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Assina e adiciona informações suplementares da nota |
|
163
|
|
|
*/ |
|
164
|
3 |
|
public function assinar($dom = null) |
|
165
|
|
|
{ |
|
166
|
3 |
|
$dom = parent::assinar($dom); |
|
167
|
3 |
|
$suplementar = $this->getNodeSuplementar($dom); |
|
168
|
3 |
|
$signature = $dom->getElementsByTagName('Signature')->item(0); |
|
169
|
3 |
|
$signature->parentNode->insertBefore($suplementar, $signature); |
|
170
|
3 |
|
return $dom; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.