1 | <?php |
||
36 | class NFCe extends Nota |
||
37 | { |
||
38 | |||
39 | /** |
||
40 | * Versão do QRCode |
||
41 | */ |
||
42 | const QRCODE_VERSAO = '100'; |
||
43 | |||
44 | /** |
||
45 | * Texto com o QR-Code impresso no DANFE NFC-e |
||
46 | */ |
||
47 | private $qrcode_url; |
||
48 | |||
49 | /** |
||
50 | * Constroi uma instância de NFCe vazia |
||
51 | * @param array $nfce Array contendo dados do NFCe |
||
52 | */ |
||
53 | 29 | public function __construct($nfce = array()) |
|
59 | |||
60 | /** |
||
61 | * Texto com o QR-Code impresso no DANFE NFC-e |
||
62 | * @param boolean $normalize informa se a qrcode_url deve estar no formato do XML |
||
63 | * @return mixed qrcode_url do NFCe |
||
64 | */ |
||
65 | 26 | public function getQRCodeURL($normalize = false) |
|
72 | |||
73 | /** |
||
74 | * Altera o valor da QrcodeURL para o informado no parâmetro |
||
75 | * @param mixed $qrcode_url novo valor para QrcodeURL |
||
76 | * @return NFCe A própria instância da classe |
||
77 | */ |
||
78 | 29 | public function setQRCodeURL($qrcode_url) |
|
83 | |||
84 | /** |
||
85 | * URL da página de consulta da nota fiscal |
||
86 | * @param boolean $normalize informa se a URL de consulta deve estar no formato do XML |
||
87 | * @return string URL de consulta da NFCe |
||
88 | */ |
||
89 | public function getConsultaURL($normalize = false) |
||
|
|||
90 | { |
||
91 | $estado = $this->getEmitente()->getEndereco()->getMunicipio()->getEstado(); |
||
92 | $db = SEFAZ::getInstance()->getConfiguracao()->getBanco(); |
||
93 | $info = $db->getInformacaoServico( |
||
94 | $this->getEmissao(), |
||
95 | $estado->getUF(), |
||
96 | $this->getModelo(), |
||
97 | $this->getAmbiente() |
||
98 | ); |
||
99 | if (!isset($info['consulta'])) { |
||
100 | throw new \Exception('Não existe URL de consulta da nota para o estado "'.$estado->getUF().'"', 404); |
||
101 | } |
||
102 | $url = $info['consulta']; |
||
103 | if (is_array($url)) { |
||
104 | $url = $url['url']; |
||
105 | } |
||
106 | return $url; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Converte a instância da classe para um array de campos com valores |
||
111 | * @return array Array contendo todos os campos e valores da instância |
||
112 | */ |
||
113 | 3 | public function toArray($recursive = false) |
|
119 | |||
120 | /** |
||
121 | * Atribui os valores do array para a instância atual |
||
122 | * @param mixed $nfce Array ou instância de NFCe, para copiar os valores |
||
123 | * @return NFCe A própria instância da classe |
||
124 | */ |
||
125 | 29 | public function fromArray($nfce = array()) |
|
126 | { |
||
127 | 29 | if ($nfce instanceof NFCe) { |
|
128 | 2 | $nfce = $nfce->toArray(); |
|
129 | 29 | } elseif (!is_array($nfce)) { |
|
130 | 2 | return $this; |
|
131 | } |
||
132 | 29 | parent::fromArray($nfce); |
|
133 | 29 | if (!isset($nfce['qrcode_url'])) { |
|
134 | 29 | $this->setQRCodeURL(null); |
|
135 | } else { |
||
136 | $this->setQRCodeURL($nfce['qrcode_url']); |
||
137 | } |
||
138 | 29 | return $this; |
|
139 | } |
||
140 | |||
141 | 25 | private function gerarQRCodeInfo(&$dom) |
|
142 | { |
||
143 | 25 | $config = SEFAZ::getInstance()->getConfiguracao(); |
|
144 | 25 | $totais = $this->getTotais(); |
|
145 | // if ($this->getEmissao() == self::EMISSAO_NORMAL) { |
||
146 | 25 | $dig_val = Util::loadNode($dom, 'DigestValue', 'Tag "DigestValue" não encontrada na NFCe'); |
|
147 | // } else { |
||
148 | // $dig_val = base64_encode(sha1($dom->saveXML(), true)); |
||
149 | // } |
||
150 | $params = array( |
||
151 | 25 | 'chNFe' => $this->getID(), |
|
152 | 25 | 'nVersao' => self::QRCODE_VERSAO, |
|
153 | 25 | 'tpAmb' => $this->getAmbiente(true), |
|
154 | 'cDest' => null, |
||
155 | 25 | 'dhEmi' => Util::toHex($this->getDataEmissao(true)), |
|
156 | 25 | 'vNF' => Util::toCurrency($totais['nota']), |
|
157 | 25 | 'vICMS' => Util::toCurrency($totais[Imposto::GRUPO_ICMS]), |
|
158 | 25 | 'digVal' => Util::toHex($dig_val), |
|
159 | 25 | 'cIdToken' => Util::padDigit($config->getToken(), 6), |
|
160 | 'cHashQRCode' => null |
||
161 | ); |
||
162 | 25 | if (!is_null($this->getDestinatario())) { |
|
163 | 25 | $params['cDest'] = $this->getDestinatario()->getID(true); |
|
164 | } else { |
||
165 | unset($params['cDest']); |
||
166 | } |
||
167 | 25 | $_params = $params; |
|
168 | 25 | unset($_params['cHashQRCode']); |
|
169 | 25 | $query = http_build_query($_params); |
|
170 | 25 | $params['cHashQRCode'] = sha1($query.$config->getCSC()); |
|
171 | 25 | return $params; |
|
172 | } |
||
173 | |||
174 | 25 | private function checkQRCode(&$dom) |
|
175 | { |
||
176 | 25 | $estado = $this->getEmitente()->getEndereco()->getMunicipio()->getEstado(); |
|
177 | 25 | $db = SEFAZ::getInstance()->getConfiguracao()->getBanco(); |
|
178 | 25 | $params = $this->gerarQRCodeInfo($dom); |
|
179 | 25 | $query = http_build_query($params); |
|
180 | 25 | $info = $db->getInformacaoServico( |
|
181 | 25 | $this->getEmissao(), |
|
182 | 25 | $estado->getUF(), |
|
183 | 25 | $this->getModelo(), |
|
184 | 25 | $this->getAmbiente() |
|
185 | ); |
||
186 | 25 | if (!isset($info['qrcode'])) { |
|
187 | throw new \Exception('Não existe URL de consulta de QRCode para o estado "'.$estado->getUF().'"', 404); |
||
188 | } |
||
189 | 25 | $url = $info['qrcode']; |
|
190 | 25 | if (is_array($url)) { |
|
191 | $url = $url['url']; |
||
192 | } |
||
193 | 25 | $url .= (strpos($url, '?') === false?'?':'&').$query; |
|
194 | 25 | $this->setQRCodeURL($url); |
|
195 | 25 | } |
|
196 | |||
197 | 25 | private function getNodeSuplementar(&$dom) |
|
207 | |||
208 | /** |
||
209 | * Carrega as informações do nó e preenche a instância da classe |
||
210 | * @param DOMElement $element Nó do xml com todos as tags dos campos |
||
211 | * @param string $name Nome do nó que será carregado |
||
212 | * @return DOMElement Instância do nó que foi carregado |
||
213 | */ |
||
214 | 25 | public function loadNode($element, $name = null) |
|
224 | |||
225 | /** |
||
226 | * Assina e adiciona informações suplementares da nota |
||
227 | */ |
||
228 | 25 | public function assinar($dom = null) |
|
236 | } |
||
237 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.