1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NFePHP\NFe; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class responsible for communication with SEFAZ extends |
7
|
|
|
* NFePHP\NFe\Common\Tools |
8
|
|
|
* |
9
|
|
|
* @category NFePHP |
10
|
|
|
* @package NFePHP\NFe\Tools |
11
|
|
|
* @copyright NFePHP Copyright (c) 2008-2020 |
12
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPLv3+ |
13
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
14
|
|
|
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+ |
15
|
|
|
* @author Roberto L. Machado <linux.rlm at gmail dot com> |
16
|
|
|
* @link http://github.com/nfephp-org/sped-nfe for the canonical source repository |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use NFePHP\Common\Strings; |
20
|
|
|
use NFePHP\Common\Signer; |
21
|
|
|
use NFePHP\Common\UFList; |
22
|
|
|
use NFePHP\NFe\Common\Tools as ToolsCommon; |
23
|
|
|
use RuntimeException; |
24
|
|
|
use InvalidArgumentException; |
25
|
|
|
|
26
|
|
|
class Tools extends ToolsCommon |
27
|
|
|
{ |
28
|
|
|
const EVT_CONFIRMACAO = 210200; //only one per nfe seq=n |
29
|
|
|
const EVT_CIENCIA = 210210; //only one per nfe seq=1 |
30
|
|
|
const EVT_DESCONHECIMENTO = 210220; //only one per nfe seq=n |
31
|
|
|
const EVT_NAO_REALIZADA = 210240; //only one per nfe but seq=n |
32
|
|
|
const EVT_CCE = 110110; //many seq=n |
33
|
|
|
const EVT_CANCELA = 110111; //only seq=1 |
34
|
|
|
const EVT_CANCELASUBSTITUICAO = 110112; |
35
|
|
|
const EVT_EPEC = 110140; //only seq=1 |
36
|
|
|
const EVT_ATORINTERESSADO = 110150; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Request authorization to issue NFe in batch with one or more documents |
40
|
|
|
* @param array $aXml array of nfe's xml |
41
|
|
|
* @param string $idLote lote number |
42
|
|
|
* @param int $indSinc flag to use synchronous communication |
43
|
|
|
* @param bool $compactar flag to compress data with gzip |
44
|
|
|
* @param array $xmls array with xmls substitutes if contigency is on |
45
|
|
|
* @return string soap response xml |
46
|
|
|
* @throws InvalidArgumentException |
47
|
|
|
*/ |
48
|
|
|
public function sefazEnviaLote( |
49
|
|
|
$aXml, |
50
|
|
|
$idLote = '', |
51
|
|
|
$indSinc = 0, |
52
|
|
|
$compactar = false, |
53
|
|
|
&$xmls = [] |
54
|
|
|
) { |
55
|
|
|
if (!is_array($aXml)) { |
56
|
|
|
throw new InvalidArgumentException('Envia Lote: XMLs de NF-e deve ser um array!'); |
57
|
|
|
} |
58
|
|
|
if ($indSinc == 1 && count($aXml) > 1) { |
59
|
|
|
throw new InvalidArgumentException('Envio sincrono deve ser usado para enviar ' |
60
|
|
|
. 'uma UNICA nota por vez. Você está tentando enviar varias.'); |
61
|
|
|
} |
62
|
|
|
$servico = 'NfeAutorizacao'; |
63
|
|
|
$this->checkContingencyForWebServices($servico); |
64
|
|
|
if ($this->contingency->type != '') { |
65
|
|
|
// Em modo de contingencia esses XMLs deverão ser modificados e re-assinados e retornados |
66
|
|
|
// no parametro $xmls para serem armazenados pelo aplicativo pois serão alterados. |
67
|
|
|
foreach ($aXml as $doc) { |
68
|
|
|
//corrigir o xml para o tipo de contigência setado |
69
|
|
|
$xmls[] = $this->correctNFeForContingencyMode($doc); |
70
|
|
|
} |
71
|
|
|
$aXml = $xmls; |
72
|
|
|
} |
73
|
|
|
$ax = []; |
74
|
|
|
$check = true; |
|
|
|
|
75
|
|
|
foreach ($aXml as $xml) { |
76
|
|
|
//verifica se o modelo do XML é o mesmo setado na classe |
77
|
|
|
//gera um exception se não for |
78
|
|
|
$this->checkModelFromXml($xml); |
79
|
|
|
$ax[] = trim(preg_replace("/<\?xml.*?\?>/", "", $xml)); |
80
|
|
|
} |
81
|
|
|
$sxml = trim(implode("", $ax)); |
82
|
|
|
$this->servico($servico, $this->config->siglaUF, $this->tpAmb); |
83
|
|
|
$request = "<enviNFe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
84
|
|
|
. "<idLote>$idLote</idLote>" |
85
|
|
|
. "<indSinc>$indSinc</indSinc>" |
86
|
|
|
. "$sxml" |
87
|
|
|
. "</enviNFe>"; |
88
|
|
|
$this->isValid($this->urlVersion, $request, 'enviNFe'); |
89
|
|
|
$this->lastRequest = $request; |
90
|
|
|
//montagem dos dados da mensagem SOAP |
91
|
|
|
$parameters = ['nfeDadosMsg' => $request]; |
92
|
|
|
$body = "<nfeDadosMsg xmlns=\"$this->urlNamespace\">$request</nfeDadosMsg>"; |
93
|
|
|
if ($compactar) { |
94
|
|
|
$gzdata = base64_encode(gzencode($request, 9, FORCE_GZIP)); |
95
|
|
|
$parameters = ['nfeDadosMsgZip' => $gzdata]; |
96
|
|
|
$body = "<nfeDadosMsgZip xmlns=\"$this->urlNamespace\">$gzdata</nfeDadosMsgZip>"; |
97
|
|
|
} |
98
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
99
|
|
|
return $this->lastResponse; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Check status of Batch of NFe sent by receipt of this shipment |
104
|
|
|
* @param string $recibo |
105
|
|
|
* @param int $tpAmb |
106
|
|
|
* @return string |
107
|
|
|
* @throws InvalidArgumentException |
108
|
|
|
*/ |
109
|
1 |
|
public function sefazConsultaRecibo($recibo, $tpAmb = null) |
110
|
|
|
{ |
111
|
1 |
|
if (empty($recibo)) { |
112
|
1 |
|
throw new InvalidArgumentException('Consulta Recibo: numero do recibo vazio!'); |
113
|
|
|
} |
114
|
|
|
if (empty($tpAmb)) { |
115
|
|
|
$tpAmb = $this->tpAmb; |
116
|
|
|
} |
117
|
|
|
//carrega serviço |
118
|
|
|
$servico = 'NfeRetAutorizacao'; |
119
|
|
|
$this->checkContingencyForWebServices($servico); |
120
|
|
|
$this->servico($servico, $this->config->siglaUF, $tpAmb); |
121
|
|
|
if ($this->urlService == '') { |
122
|
|
|
$msg = "A consulta de NFe nao esta disponivel na SEFAZ {$this->config->siglaUF}!"; |
123
|
|
|
throw new RuntimeException($msg); |
124
|
|
|
} |
125
|
|
|
$request = "<consReciNFe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
126
|
|
|
. "<tpAmb>$tpAmb</tpAmb>" |
127
|
|
|
. "<nRec>$recibo</nRec>" |
128
|
|
|
. "</consReciNFe>"; |
129
|
|
|
$this->isValid($this->urlVersion, $request, 'consReciNFe'); |
130
|
|
|
$this->lastRequest = $request; |
131
|
|
|
$parameters = ['nfeDadosMsg' => $request]; |
132
|
|
|
$body = "<nfeDadosMsg xmlns=\"$this->urlNamespace\">$request</nfeDadosMsg>"; |
133
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
134
|
|
|
return $this->lastResponse; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Check the NFe status for the 44-digit key and retrieve the protocol |
139
|
|
|
* @param string $chave |
140
|
|
|
* @param int $tpAmb |
141
|
|
|
* @return string |
142
|
|
|
* @throws InvalidArgumentException |
143
|
|
|
*/ |
144
|
3 |
|
public function sefazConsultaChave($chave, $tpAmb = null) |
145
|
|
|
{ |
146
|
3 |
|
if (empty($chave)) { |
147
|
1 |
|
throw new InvalidArgumentException('Consulta chave: a chave esta vazia!'); |
148
|
|
|
} |
149
|
2 |
|
if (strlen($chave) != 44 || !is_numeric($chave)) { |
150
|
2 |
|
throw new InvalidArgumentException("Consulta chave: chave \"$chave\" invalida!"); |
151
|
|
|
} |
152
|
|
|
$uf = UFList::getUFByCode(substr($chave, 0, 2)); |
153
|
|
|
if (empty($tpAmb)) { |
154
|
|
|
$tpAmb = $this->tpAmb; |
155
|
|
|
} |
156
|
|
|
//carrega serviço |
157
|
|
|
$servico = 'NfeConsultaProtocolo'; |
158
|
|
|
$this->checkContingencyForWebServices($servico); |
159
|
|
|
$this->servico($servico, $uf, $tpAmb); |
160
|
|
|
$request = "<consSitNFe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
161
|
|
|
. "<tpAmb>$tpAmb</tpAmb>" |
162
|
|
|
. "<xServ>CONSULTAR</xServ>" |
163
|
|
|
. "<chNFe>$chave</chNFe>" |
164
|
|
|
. "</consSitNFe>"; |
165
|
|
|
$this->isValid($this->urlVersion, $request, 'consSitNFe'); |
166
|
|
|
$this->lastRequest = $request; |
167
|
|
|
$parameters = ['nfeDadosMsg' => $request]; |
168
|
|
|
$body = "<nfeDadosMsg xmlns=\"$this->urlNamespace\">$request</nfeDadosMsg>"; |
169
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
170
|
|
|
return $this->lastResponse; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Request to disable one or an NFe sequence of a given series |
175
|
|
|
* @param int $nSerie |
176
|
|
|
* @param int $nIni |
177
|
|
|
* @param int $nFin |
178
|
|
|
* @param string $xJust |
179
|
|
|
* @param int $tpAmb |
180
|
|
|
* @param string $ano |
181
|
|
|
* @return string |
182
|
|
|
* @throws InvalidArgumentException |
183
|
|
|
*/ |
184
|
|
|
public function sefazInutiliza($nSerie, $nIni, $nFin, $xJust, $tpAmb = null, $ano = null) |
185
|
|
|
{ |
186
|
|
|
if (empty($nIni) || empty($nFin) || empty($xJust)) { |
187
|
|
|
throw new InvalidArgumentException('Inutilizacao: parametros incompletos!'); |
188
|
|
|
} |
189
|
|
|
if (empty($tpAmb)) { |
190
|
|
|
$tpAmb = $this->tpAmb; |
191
|
|
|
} |
192
|
|
|
$xJust = Strings::replaceUnacceptableCharacters($xJust); |
193
|
|
|
$servico = 'NfeInutilizacao'; |
194
|
|
|
$this->checkContingencyForWebServices($servico); |
195
|
|
|
//carrega serviço |
196
|
|
|
$this->servico($servico, $this->config->siglaUF, $tpAmb); |
197
|
|
|
$cnpj = $this->config->cnpj; |
198
|
|
|
$strAno = $ano; |
199
|
|
|
if (empty($ano)) { |
200
|
|
|
$strAno = (string) date('y'); |
201
|
|
|
} |
202
|
|
|
$strSerie = str_pad($nSerie, 3, '0', STR_PAD_LEFT); |
203
|
|
|
$strInicio = str_pad($nIni, 9, '0', STR_PAD_LEFT); |
204
|
|
|
$strFinal = str_pad($nFin, 9, '0', STR_PAD_LEFT); |
205
|
|
|
$idInut = "ID" |
206
|
|
|
. $this->urlcUF |
207
|
|
|
. $strAno |
208
|
|
|
. $cnpj |
209
|
|
|
. $this->modelo |
210
|
|
|
. $strSerie |
211
|
|
|
. $strInicio |
212
|
|
|
. $strFinal; |
213
|
|
|
//limpa os caracteres indesejados da justificativa |
214
|
|
|
$xJust = Strings::replaceUnacceptableCharacters($xJust); |
215
|
|
|
//montagem do corpo da mensagem |
216
|
|
|
$msg = "<inutNFe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" . |
217
|
|
|
"<infInut Id=\"$idInut\">" . |
218
|
|
|
"<tpAmb>$tpAmb</tpAmb>" . |
219
|
|
|
"<xServ>INUTILIZAR</xServ>" . |
220
|
|
|
"<cUF>$this->urlcUF</cUF>" . |
221
|
|
|
"<ano>$strAno</ano>" . |
222
|
|
|
"<CNPJ>$cnpj</CNPJ>" . |
223
|
|
|
"<mod>$this->modelo</mod>" . |
224
|
|
|
"<serie>$nSerie</serie>" . |
225
|
|
|
"<nNFIni>$nIni</nNFIni>" . |
226
|
|
|
"<nNFFin>$nFin</nNFFin>" . |
227
|
|
|
"<xJust>$xJust</xJust>" . |
228
|
|
|
"</infInut></inutNFe>"; |
229
|
|
|
//assina a solicitação |
230
|
|
|
$request = Signer::sign( |
231
|
|
|
$this->certificate, |
232
|
|
|
$msg, |
233
|
|
|
'infInut', |
234
|
|
|
'Id', |
235
|
|
|
$this->algorithm, |
236
|
|
|
$this->canonical |
237
|
|
|
); |
238
|
|
|
$request = Strings::clearXmlString($request, true); |
239
|
|
|
$this->isValid($this->urlVersion, $request, 'inutNFe'); |
240
|
|
|
$this->lastRequest = $request; |
241
|
|
|
$parameters = ['nfeDadosMsg' => $request]; |
242
|
|
|
$body = "<nfeDadosMsg xmlns=\"$this->urlNamespace\">$request</nfeDadosMsg>"; |
243
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
244
|
|
|
return $this->lastResponse; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Search for the registration data of an NFe issuer, |
249
|
|
|
* if in contingency mode this service will cause a |
250
|
|
|
* Exception and remember not all Sefaz have this service available, |
251
|
|
|
* so it will not work in some cases. |
252
|
|
|
* @param string $uf federation unit (abbreviation) |
253
|
|
|
* @param string $cnpj CNPJ number (optional) |
254
|
|
|
* @param string $iest IE number (optional) |
255
|
|
|
* @param string $cpf CPF number (optional) |
256
|
|
|
* @return string xml soap response |
257
|
|
|
* @throws InvalidArgumentException |
258
|
|
|
*/ |
259
|
|
|
public function sefazCadastro($uf, $cnpj = '', $iest = '', $cpf = '') |
260
|
|
|
{ |
261
|
|
|
$filter = ''; |
262
|
|
|
if (!empty($cnpj)) { |
263
|
|
|
$filter = "<CNPJ>$cnpj</CNPJ>"; |
264
|
|
|
} elseif (!empty($iest)) { |
265
|
|
|
$filter = "<IE>$iest</IE>"; |
266
|
|
|
} elseif (!empty($cpf)) { |
267
|
|
|
$filter = "<CPF>$cpf</CPF>"; |
268
|
|
|
} |
269
|
|
|
if (empty($uf) || empty($filter)) { |
270
|
|
|
throw new InvalidArgumentException('Sigla UF esta vazia ou CNPJ+IE+CPF vazios!'); |
271
|
|
|
} |
272
|
|
|
//carrega serviço |
273
|
|
|
$servico = 'NfeConsultaCadastro'; |
274
|
|
|
$this->checkContingencyForWebServices($servico); |
275
|
|
|
$this->servico($servico, $uf, $this->tpAmb, true); |
276
|
|
|
$request = "<ConsCad xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
277
|
|
|
. "<infCons>" |
278
|
|
|
. "<xServ>CONS-CAD</xServ>" |
279
|
|
|
. "<UF>$uf</UF>" |
280
|
|
|
. "$filter" |
281
|
|
|
. "</infCons>" |
282
|
|
|
. "</ConsCad>"; |
283
|
|
|
if (strtoupper($uf) == 'MT') { |
284
|
|
|
$request = "<nfeDadosMsg>$request</nfeDadosMsg>" ; |
285
|
|
|
} |
286
|
|
|
$this->isValid($this->urlVersion, $request, 'consCad'); |
287
|
|
|
$this->lastRequest = $request; |
288
|
|
|
$parameters = ['nfeDadosMsg' => $request]; |
289
|
|
|
if ($this->urlVersion === '2.00') { |
290
|
|
|
$this->objHeader = new \SOAPHeader( |
291
|
|
|
$this->urlNamespace, |
292
|
|
|
'nfeCabecMsg', |
293
|
|
|
['cUF' => $this->urlcUF, 'versaoDados' => $this->urlVersion] |
294
|
|
|
); |
295
|
|
|
} |
296
|
|
|
$body = "<nfeDadosMsg xmlns=\"$this->urlNamespace\">$request</nfeDadosMsg>"; |
297
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
298
|
|
|
return $this->lastResponse; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Check services status SEFAZ/SVC |
303
|
|
|
* If $uf is empty use normal check with contingency |
304
|
|
|
* If $uf is NOT empty ignore contingency mode |
305
|
|
|
* @param string $uf initials of federation unit |
306
|
|
|
* @param int $tpAmb |
307
|
|
|
* @return string xml soap response |
308
|
|
|
*/ |
309
|
|
|
public function sefazStatus($uf = '', $tpAmb = null) |
310
|
|
|
{ |
311
|
|
|
if (empty($tpAmb)) { |
312
|
|
|
$tpAmb = $this->tpAmb; |
313
|
|
|
} |
314
|
|
|
$ignoreContingency = true; |
315
|
|
|
if (empty($uf)) { |
316
|
|
|
$uf = $this->config->siglaUF; |
317
|
|
|
$ignoreContingency = false; |
318
|
|
|
} |
319
|
|
|
$servico = 'NfeStatusServico'; |
320
|
|
|
$this->checkContingencyForWebServices($servico); |
321
|
|
|
$this->servico($servico, $uf, $tpAmb, $ignoreContingency); |
322
|
|
|
$request = "<consStatServ xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
323
|
|
|
. "<tpAmb>$tpAmb</tpAmb>" |
324
|
|
|
. "<cUF>$this->urlcUF</cUF>" |
325
|
|
|
. "<xServ>STATUS</xServ>" |
326
|
|
|
. "</consStatServ>"; |
327
|
|
|
$this->isValid($this->urlVersion, $request, 'consStatServ'); |
328
|
|
|
$this->lastRequest = $request; |
329
|
|
|
$parameters = ['nfeDadosMsg' => $request]; |
330
|
|
|
$body = "<nfeDadosMsg xmlns=\"$this->urlNamespace\">$request</nfeDadosMsg>"; |
331
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
332
|
|
|
return $this->lastResponse; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Service for the distribution of summary information and |
337
|
|
|
* electronic tax documents of interest to an actor. |
338
|
|
|
* @param integer $ultNSU last NSU number recived |
339
|
|
|
* @param integer $numNSU NSU number you wish to consult |
340
|
|
|
* @param string $fonte data source 'AN' and for some cases it may be 'RS' |
341
|
|
|
* @return string |
342
|
|
|
*/ |
343
|
|
|
public function sefazDistDFe($ultNSU = 0, $numNSU = 0, $fonte = 'AN') |
344
|
|
|
{ |
345
|
|
|
//carrega serviço |
346
|
|
|
$servico = 'NfeDistribuicaoDFe'; |
347
|
|
|
$this->checkContingencyForWebServices($servico); |
348
|
|
|
$this->servico($servico, $fonte, $this->tpAmb, true); |
349
|
|
|
$cUF = UFList::getCodeByUF($this->config->siglaUF); |
350
|
|
|
$cnpj = $this->config->cnpj; |
351
|
|
|
$ultNSU = str_pad($ultNSU, 15, '0', STR_PAD_LEFT); |
352
|
|
|
$tagNSU = "<distNSU><ultNSU>$ultNSU</ultNSU></distNSU>"; |
353
|
|
|
if ($numNSU != 0) { |
354
|
|
|
$numNSU = str_pad($numNSU, 15, '0', STR_PAD_LEFT); |
355
|
|
|
$tagNSU = "<consNSU><NSU>$numNSU</NSU></consNSU>"; |
356
|
|
|
} |
357
|
|
|
//monta a consulta |
358
|
|
|
$consulta = "<distDFeInt xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
359
|
|
|
. "<tpAmb>".$this->tpAmb."</tpAmb>" |
360
|
|
|
. "<cUFAutor>$cUF</cUFAutor>"; |
361
|
|
|
if ($this->typePerson === 'J') { |
362
|
|
|
$consulta .= "<CNPJ>$cnpj</CNPJ>"; |
363
|
|
|
} else { |
364
|
|
|
$consulta .= "<CPF>$cnpj</CPF>"; |
365
|
|
|
} |
366
|
|
|
$consulta .= "$tagNSU" |
367
|
|
|
. "</distDFeInt>"; |
368
|
|
|
//valida o xml da requisição |
369
|
|
|
$this->isValid($this->urlVersion, $consulta, 'distDFeInt'); |
370
|
|
|
$this->lastRequest = $consulta; |
371
|
|
|
//montagem dos dados da mensagem SOAP |
372
|
|
|
$request = "<nfeDadosMsg xmlns=\"$this->urlNamespace\">$consulta</nfeDadosMsg>"; |
373
|
|
|
$parameters = ['nfeDistDFeInteresse' => $request]; |
374
|
|
|
$body = "<nfeDistDFeInteresse xmlns=\"$this->urlNamespace\">" |
375
|
|
|
. $request |
376
|
|
|
. "</nfeDistDFeInteresse>"; |
377
|
|
|
//este webservice não requer cabeçalho |
378
|
|
|
$this->objHeader = null; |
379
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
380
|
|
|
return $this->lastResponse; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* Request authorization for Letter of Correction |
385
|
|
|
* @param string $chave |
386
|
|
|
* @param string $xCorrecao |
387
|
|
|
* @param int $nSeqEvento |
388
|
|
|
* @return string |
389
|
|
|
* @throws InvalidArgumentException |
390
|
|
|
*/ |
391
|
|
|
public function sefazCCe($chave, $xCorrecao, $nSeqEvento = 1) |
392
|
|
|
{ |
393
|
|
|
if (empty($chave) || empty($xCorrecao)) { |
394
|
|
|
throw new InvalidArgumentException('CC-e: chave ou motivo da correcao vazio!'); |
395
|
|
|
} |
396
|
|
|
$uf = $this->validKeyByUF($chave); |
397
|
|
|
$xCorrecao = Strings::replaceUnacceptableCharacters(substr(trim($xCorrecao), 0, 1000)); |
398
|
|
|
$xCondUso = 'A Carta de Correcao e disciplinada pelo paragrafo ' |
399
|
|
|
. '1o-A do art. 7o do Convenio S/N, de 15 de dezembro de 1970 ' |
400
|
|
|
. 'e pode ser utilizada para regularizacao de erro ocorrido ' |
401
|
|
|
. 'na emissao de documento fiscal, desde que o erro nao esteja ' |
402
|
|
|
. 'relacionado com: I - as variaveis que determinam o valor ' |
403
|
|
|
. 'do imposto tais como: base de calculo, aliquota, ' |
404
|
|
|
. 'diferenca de preco, quantidade, valor da operacao ou da ' |
405
|
|
|
. 'prestacao; II - a correcao de dados cadastrais que implique ' |
406
|
|
|
. 'mudanca do remetente ou do destinatario; III - a data de ' |
407
|
|
|
. 'emissao ou de saida.'; |
408
|
|
|
$tagAdic = "<xCorrecao>" |
409
|
|
|
. $xCorrecao |
410
|
|
|
. "</xCorrecao><xCondUso>$xCondUso</xCondUso>"; |
411
|
|
|
return $this->sefazEvento($uf, $chave, self::EVT_CCE, $nSeqEvento, $tagAdic); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* Evento do Ator Interessado |
416
|
|
|
* NOTA: NT2020.007 |
417
|
|
|
* @param string $chave |
418
|
|
|
* @param int $tpAutor |
419
|
|
|
* @param string $verAplic |
420
|
|
|
* @param int $nSeqEvento |
421
|
|
|
* @param array $interessados |
422
|
|
|
* @return string |
423
|
|
|
*/ |
424
|
|
|
public function sefazAtorInteressado( |
425
|
|
|
$chave, |
426
|
|
|
$tpAutor, |
427
|
|
|
$verAplic, |
428
|
|
|
$nSeqEvento = 1, |
429
|
|
|
$interessados = [] |
430
|
|
|
) { |
431
|
|
|
$xCondUso = 'O emitente ou destinatário da NF-e, declara que permite o ' |
432
|
|
|
. 'transportador declarado no campo CNPJ/CPF deste evento a ' |
433
|
|
|
. 'autorizar os transportadores subcontratados ou redespachados ' |
434
|
|
|
. 'a terem acesso ao download da NFe'; |
435
|
|
|
$tagAdic = "<cOrgaoAutor>{$this->urlcUF}</cOrgaoAutor>" |
436
|
|
|
. "<tpAutor>{$tpAutor}</tpAutor>" |
437
|
|
|
. "<verAplic>{$verAplic}</verAplic>"; |
438
|
|
|
foreach ($interessados as $aut) { |
439
|
|
|
$obj = (object) $aut; |
440
|
|
|
$tagAdic .= "<autXML>"; |
441
|
|
|
$tagAdic .= !empty($obj->CNPJ) |
442
|
|
|
? "<CNPJ>{$obj->CNPJ}</CNPJ>" |
443
|
|
|
: "<CPF>{$obj->CPF}</CPF>"; |
444
|
|
|
$tagAdic .= "<tpAutorizacao>{$obj->tpAutorizacao}</tpAutorizacao>" |
445
|
|
|
. "</autXML>"; |
446
|
|
|
} |
447
|
|
|
$tagAdic .= "<xCondUso>$xCondUso</xCondUso>"; |
448
|
|
|
return $this->sefazEvento($uf, $chave, self::EVT_ATORINTERESSADO, $nSeqEvento, $tagAdic); |
|
|
|
|
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* Request extension of the term of return of products of an NF-e of |
453
|
|
|
* consignment for industrialization to order with suspension of ICMS |
454
|
|
|
* in interstate operations |
455
|
|
|
* @param string $chNFe |
456
|
|
|
* @param string $nProt |
457
|
|
|
* @param integer $tipo 1-primerio prazo, 2-segundo prazo |
458
|
|
|
* @param array $itens |
459
|
|
|
* @param integer $nSeqEvento |
460
|
|
|
* @return string |
461
|
|
|
*/ |
462
|
|
|
public function sefazEPP( |
463
|
|
|
$chNFe, |
464
|
|
|
$nProt, |
465
|
|
|
$itens = array(), |
466
|
|
|
$tipo = 1, |
467
|
|
|
$nSeqEvento = 1 |
468
|
|
|
) { |
469
|
|
|
$uf = UFList::getUFByCode(substr($chNFe, 0, 2)); |
470
|
|
|
$tpEvento = 111500; |
471
|
|
|
if ($tipo == 2) { |
472
|
|
|
$tpEvento = 111501; |
473
|
|
|
} |
474
|
|
|
$tagAdic = "<nProt>$nProt</nProt>"; |
475
|
|
|
foreach ($itens as $item) { |
476
|
|
|
$tagAdic .= "<itemPedido numItem=\"" |
477
|
|
|
. $item[0] |
478
|
|
|
. "\"><qtdeItem>" |
479
|
|
|
. $item[1] |
480
|
|
|
."</qtdeItem></itemPedido>"; |
481
|
|
|
} |
482
|
|
|
return $this->sefazEvento( |
483
|
|
|
$uf, |
484
|
|
|
$chNFe, |
485
|
|
|
$tpEvento, |
486
|
|
|
$nSeqEvento, |
487
|
|
|
$tagAdic |
488
|
|
|
); |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/** |
492
|
|
|
* Request the cancellation of the request for an extension of the term |
493
|
|
|
* of return of products of an NF-e of consignment for industrialization |
494
|
|
|
* by order with suspension of ICMS in interstate operations |
495
|
|
|
* @param string $chave |
496
|
|
|
* @param string $nProt |
497
|
|
|
* @param integer $nSeqEvento |
498
|
|
|
* @return string |
499
|
|
|
* @throws InvalidArgumentException |
500
|
|
|
*/ |
501
|
|
|
public function sefazECPP($chave, $nProt, $nSeqEvento = 1) |
502
|
|
|
{ |
503
|
|
|
if (empty($chave) || empty($nProt)) { |
504
|
|
|
throw new InvalidArgumentException('A chave ou o numero do protocolo estao vazios!'); |
505
|
|
|
} |
506
|
|
|
$uf = UFList::getUFByCode(substr($chave, 0, 2)); |
507
|
|
|
$tpEvento = 111502; |
508
|
|
|
$origEvent = 111500; |
509
|
|
|
if ($nSeqEvento == 2) { |
510
|
|
|
$tpEvento = 111503; |
511
|
|
|
$origEvent = 111501; |
512
|
|
|
} |
513
|
|
|
$sSeqEvento = str_pad($nSeqEvento, 2, "0", STR_PAD_LEFT); |
514
|
|
|
$idPedidoCancelado = "ID$origEvent$chave$sSeqEvento"; |
515
|
|
|
$tagAdic = "<idPedidoCancelado>" |
516
|
|
|
. "$idPedidoCancelado" |
517
|
|
|
. "</idPedidoCancelado>" |
518
|
|
|
. "<nProt>$nProt</nProt>"; |
519
|
|
|
return $this->sefazEvento($uf, $chave, $tpEvento, $nSeqEvento, $tagAdic); |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
/** |
523
|
|
|
* Requires nfe cancellation |
524
|
|
|
* @param string $chave key of NFe |
525
|
|
|
* @param string $xJust justificative 255 characters max |
526
|
|
|
* @param string $nProt protocol number |
527
|
|
|
* @return string |
528
|
|
|
* @throws InvalidArgumentException |
529
|
|
|
*/ |
530
|
|
|
public function sefazCancela($chave, $xJust, $nProt) |
531
|
|
|
{ |
532
|
|
|
if (empty($chave) || empty($xJust) || empty($nProt)) { |
533
|
|
|
throw new InvalidArgumentException('Cancelamento: chave, just ou numprot vazio!'); |
534
|
|
|
} |
535
|
|
|
$uf = $this->validKeyByUF($chave); |
536
|
|
|
$xJust = Strings::replaceUnacceptableCharacters(substr(trim($xJust), 0, 255)); |
537
|
|
|
$nSeqEvento = 1; |
538
|
|
|
$tagAdic = "<nProt>$nProt</nProt><xJust>$xJust</xJust>"; |
539
|
|
|
return $this->sefazEvento($uf, $chave, self::EVT_CANCELA, $nSeqEvento, $tagAdic); |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
/** |
543
|
|
|
* Requires nfe cancellation by substitution |
544
|
|
|
* @param string $chave key of NFe |
545
|
|
|
* @param string $xJust justificative 255 characters max |
546
|
|
|
* @param string $nProt protocol number |
547
|
|
|
* @param string $chNFeRef key of New NFe |
548
|
|
|
* @param string $verAplic version of applicative |
549
|
|
|
* @return string |
550
|
|
|
* @throws InvalidArgumentException |
551
|
|
|
*/ |
552
|
|
|
public function sefazCancelaPorSubstituicao($chave, $xJust, $nProt, $chNFeRef, $verAplic) |
553
|
|
|
{ |
554
|
|
|
if ($this->modelo != 65) { |
555
|
|
|
throw new InvalidArgumentException( |
556
|
|
|
'Cancelamento pro Substituição deve ser usado apenas para ' |
557
|
|
|
. 'operações com modelo 65 NFCe' |
558
|
|
|
); |
559
|
|
|
} |
560
|
|
|
if (empty($chave) || empty($xJust) || empty($nProt) |
561
|
|
|
|| empty($chNFeRef) || empty($verAplic)) { |
562
|
|
|
throw new InvalidArgumentException( |
563
|
|
|
'CancelamentoPorSubs: chave da NFCe cancelada, justificativa, ' |
564
|
|
|
. 'protocolo, chave da NFCe substituta, ou versão do aplicativo ' |
565
|
|
|
. 'emissor não podem ser vazios!' |
566
|
|
|
); |
567
|
|
|
} |
568
|
|
|
$uf = $this->validKeyByUF($chave); |
569
|
|
|
$xJust = Strings::replaceUnacceptableCharacters(substr(trim($xJust), 0, 255)); |
570
|
|
|
$nSeqEvento = 1; |
571
|
|
|
$cOrgao = substr($chave, 0, 2); |
572
|
|
|
$tagAdic = "<cOrgaoAutor>$cOrgao</cOrgaoAutor>" |
573
|
|
|
. "<tpAutor>1</tpAutor>" |
574
|
|
|
. "<verAplic>$verAplic</verAplic>" |
575
|
|
|
. "<nProt>$nProt</nProt>" |
576
|
|
|
. "<xJust>$xJust</xJust>" |
577
|
|
|
. "<chNFeRef>$chNFeRef</chNFeRef>"; |
578
|
|
|
return $this->sefazEvento($uf, $chave, self::EVT_CANCELASUBSTITUICAO, $nSeqEvento, $tagAdic); |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
/** |
582
|
|
|
* Request the registration of the manifestation of recipient |
583
|
|
|
* @param string $chave |
584
|
|
|
* @param int $tpEvento |
585
|
|
|
* @param string $xJust Justification for not carrying out the operation |
586
|
|
|
* @param int $nSeqEvento |
587
|
|
|
* @return string |
588
|
|
|
* @throws InvalidArgumentException |
589
|
|
|
*/ |
590
|
|
|
public function sefazManifesta($chave, $tpEvento, $xJust = '', $nSeqEvento = 1) |
591
|
|
|
{ |
592
|
|
|
if (empty($chave) || empty($tpEvento)) { |
593
|
|
|
throw new InvalidArgumentException('Manifestacao: chave ou tipo de evento vazio!'); |
594
|
|
|
} |
595
|
|
|
$tagAdic = ''; |
596
|
|
|
if ($tpEvento == self::EVT_NAO_REALIZADA) { |
597
|
|
|
$xJust = Strings::replaceUnacceptableCharacters(substr(trim($xJust), 0, 255)); |
598
|
|
|
$tagAdic = "<xJust>$xJust</xJust>"; |
599
|
|
|
} |
600
|
|
|
return $this->sefazEvento('AN', $chave, $tpEvento, $nSeqEvento, $tagAdic); |
601
|
|
|
} |
602
|
|
|
|
603
|
|
|
/** |
604
|
|
|
* Request the registration of the manifestation of recipient in batch |
605
|
|
|
* @param \stdClass $std |
606
|
|
|
* @return string |
607
|
|
|
* @throws InvalidArgumentException |
608
|
|
|
* @throws RuntimeException |
609
|
|
|
*/ |
610
|
|
|
public function sefazManifestaLote(\stdClass $std) |
611
|
|
|
{ |
612
|
|
|
$allowed = [ |
613
|
|
|
self::EVT_CONFIRMACAO, |
614
|
|
|
self::EVT_CIENCIA, |
615
|
|
|
self::EVT_DESCONHECIMENTO, |
616
|
|
|
self::EVT_NAO_REALIZADA, |
617
|
|
|
]; |
618
|
|
|
if (empty($std) || empty($std->evento)) { |
619
|
|
|
throw new InvalidArgumentException('Manifestacao: parametro "std" ou evento estao vazios!'); |
620
|
|
|
} |
621
|
|
|
if (count($std->evento) > 20) { |
622
|
|
|
throw new RuntimeException('Manifestacao: o lote de eventos esta limitado a 20!'); |
623
|
|
|
} |
624
|
|
|
$evt = new \stdClass(); |
625
|
|
|
$i = 0; |
626
|
|
|
foreach ($std->evento as $s) { |
627
|
|
|
if (!in_array($s->tpEvento, $allowed)) { // se o evento não estiver entre os permitidos ignore |
628
|
|
|
continue; |
629
|
|
|
} |
630
|
|
|
$tagAdic = ''; |
631
|
|
|
if ($s->tpEvento == self::EVT_NAO_REALIZADA) { |
632
|
|
|
$xJust = Strings::replaceUnacceptableCharacters(substr(trim($s->xJust), 0, 255)); |
633
|
|
|
$tagAdic = "<xJust>$xJust</xJust>"; |
634
|
|
|
} |
635
|
|
|
$evt->evento[$i] = new \stdClass(); |
636
|
|
|
$evt->evento[$i]->chave = $s->chNFe; |
637
|
|
|
$evt->evento[$i]->tpEvento = $s->tpEvento; |
638
|
|
|
$evt->evento[$i]->nSeqEvento = $s->nSeqEvento; |
639
|
|
|
$evt->evento[$i]->tagAdic = $tagAdic; |
640
|
|
|
$i++; |
641
|
|
|
} |
642
|
|
|
return $this->sefazEventoLote('AN', $evt); |
643
|
|
|
} |
644
|
|
|
|
645
|
|
|
/** |
646
|
|
|
* Send event to SEFAZ in batch |
647
|
|
|
* @param string $uf |
648
|
|
|
* @param \stdClass $std |
649
|
|
|
* @return string |
650
|
|
|
* @throws RuntimeException |
651
|
|
|
* @throws InvalidArgumentException |
652
|
|
|
*/ |
653
|
|
|
public function sefazEventoLote($uf, \stdClass $std) |
654
|
|
|
{ |
655
|
|
|
if (empty($uf) || empty($std)) { |
656
|
|
|
throw new InvalidArgumentException('Evento Lote: UF ou parametro "std" vazio!'); |
657
|
|
|
} |
658
|
|
|
if (count($std->evento) > 20) { |
659
|
|
|
throw new RuntimeException('Evento Lote: o lote de eventos esta limitado a 20!'); |
660
|
|
|
} |
661
|
|
|
$servico = 'RecepcaoEvento'; |
662
|
|
|
$this->checkContingencyForWebServices($servico); |
663
|
|
|
$this->servico($servico, $uf, $this->tpAmb, false); |
664
|
|
|
$batchRequest = ''; |
665
|
|
|
foreach ($std->evento as $evt) { |
666
|
|
|
if ($evt->tpEvento == self::EVT_EPEC) { |
667
|
|
|
continue; //não é possivel enviar EPEC com outros eventos |
668
|
|
|
} |
669
|
|
|
$ev = $this->tpEv($evt->tpEvento); |
670
|
|
|
$descEvento = $ev->desc; |
671
|
|
|
$cnpj = $this->config->cnpj; |
672
|
|
|
$dt = new \DateTime('now', new \DateTimeZone($this->timezone)); |
673
|
|
|
$dhEvento = $dt->format('Y-m-d\TH:i:sP'); |
674
|
|
|
$sSeqEvento = str_pad($evt->nSeqEvento, 2, "0", STR_PAD_LEFT); |
675
|
|
|
$eventId = "ID".$evt->tpEvento.$evt->chave.$sSeqEvento; |
676
|
|
|
$cOrgao = UFList::getCodeByUF($uf); |
677
|
|
|
$request = "<evento xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
678
|
|
|
. "<infEvento Id=\"$eventId\">" |
679
|
|
|
. "<cOrgao>$cOrgao</cOrgao>" |
680
|
|
|
. "<tpAmb>$this->tpAmb</tpAmb>"; |
681
|
|
|
if ($this->typePerson === 'J') { |
682
|
|
|
$request .= "<CNPJ>$cnpj</CNPJ>"; |
683
|
|
|
} else { |
684
|
|
|
$request .= "<CPF>$cnpj</CPF>"; |
685
|
|
|
} |
686
|
|
|
$request .= "<chNFe>$evt->chave</chNFe>" |
687
|
|
|
. "<dhEvento>$dhEvento</dhEvento>" |
688
|
|
|
. "<tpEvento>$evt->tpEvento</tpEvento>" |
689
|
|
|
. "<nSeqEvento>$evt->nSeqEvento</nSeqEvento>" |
690
|
|
|
. "<verEvento>$this->urlVersion</verEvento>" |
691
|
|
|
. "<detEvento versao=\"$this->urlVersion\">" |
692
|
|
|
. "<descEvento>$descEvento</descEvento>" |
693
|
|
|
. "$evt->tagAdic" |
694
|
|
|
. "</detEvento>" |
695
|
|
|
. "</infEvento>" |
696
|
|
|
. "</evento>"; |
697
|
|
|
//assinatura dos dados |
698
|
|
|
$request = Signer::sign( |
699
|
|
|
$this->certificate, |
700
|
|
|
$request, |
701
|
|
|
'infEvento', |
702
|
|
|
'Id', |
703
|
|
|
$this->algorithm, |
704
|
|
|
$this->canonical |
705
|
|
|
); |
706
|
|
|
$batchRequest .= Strings::clearXmlString($request, true); |
707
|
|
|
} |
708
|
|
|
$dt = new \DateTime('now', new \DateTimeZone($this->timezone)); |
709
|
|
|
$lote = $dt->format('YmdHis') . rand(0, 9); |
710
|
|
|
$request = "<envEvento xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
711
|
|
|
. "<idLote>$lote</idLote>" |
712
|
|
|
. $batchRequest |
713
|
|
|
. "</envEvento>"; |
714
|
|
|
$this->isValid($this->urlVersion, $request, 'envEvento'); |
715
|
|
|
$this->lastRequest = $request; |
716
|
|
|
$parameters = ['nfeDadosMsg' => $request]; |
717
|
|
|
$body = "<nfeDadosMsg xmlns=\"$this->urlNamespace\">$request</nfeDadosMsg>"; |
718
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
719
|
|
|
return $this->lastResponse; |
720
|
|
|
} |
721
|
|
|
|
722
|
|
|
/** |
723
|
|
|
* Request authorization for issuance in contingency EPEC |
724
|
|
|
* @param string $xml |
725
|
|
|
* @return string |
726
|
|
|
* @throws InvalidArgumentException |
727
|
|
|
* @throws RuntimeException |
728
|
|
|
*/ |
729
|
|
|
public function sefazEPEC(&$xml) |
730
|
|
|
{ |
731
|
|
|
if (empty($xml)) { |
732
|
|
|
throw new InvalidArgumentException('EPEC: parametro xml esta vazio!'); |
733
|
|
|
} |
734
|
|
|
$nSeqEvento = 1; |
735
|
|
|
if ($this->contingency->type !== 'EPEC') { |
736
|
|
|
throw new RuntimeException('A contingencia EPEC deve estar ativada!'); |
737
|
|
|
} |
738
|
|
|
$xml = $this->correctNFeForContingencyMode($xml); |
739
|
|
|
$dom = new \DOMDocument('1.0', 'UTF-8'); |
740
|
|
|
$dom->preserveWhiteSpace = false; |
741
|
|
|
$dom->formatOutput = false; |
742
|
|
|
$dom->loadXML($xml); |
743
|
|
|
$infNFe = $dom->getElementsByTagName('infNFe')->item(0); |
744
|
|
|
$emit = $dom->getElementsByTagName('emit')->item(0); |
745
|
|
|
$dest = $dom->getElementsByTagName('dest')->item(0); |
746
|
|
|
$cOrgaoAutor = UFList::getCodeByUF($this->config->siglaUF); |
747
|
|
|
$chNFe = substr($infNFe->getAttribute('Id'), 3, 44); |
748
|
|
|
// EPEC |
749
|
|
|
$dhEmi = $dom->getElementsByTagName('dhEmi')->item(0)->nodeValue; |
750
|
|
|
$tpNF = $dom->getElementsByTagName('tpNF')->item(0)->nodeValue; |
751
|
|
|
$emitIE = $emit->getElementsByTagName('IE')->item(0)->nodeValue; |
752
|
|
|
$destUF = $dest->getElementsByTagName('UF')->item(0)->nodeValue; |
753
|
|
|
$total = $dom->getElementsByTagName('total')->item(0); |
754
|
|
|
$vNF = $total->getElementsByTagName('vNF')->item(0)->nodeValue; |
755
|
|
|
$vICMS = $total->getElementsByTagName('vICMS')->item(0)->nodeValue; |
756
|
|
|
$vST = $total->getElementsByTagName('vST')->item(0)->nodeValue; |
757
|
|
|
$dID = !empty($dest->getElementsByTagName('CNPJ')->item(0)) ? |
758
|
|
|
$dest->getElementsByTagName('CNPJ')->item(0)->nodeValue : null; |
759
|
|
|
if (!empty($dID)) { |
760
|
|
|
$destID = "<CNPJ>$dID</CNPJ>"; |
761
|
|
|
} else { |
762
|
|
|
$dID = $dest->getElementsByTagName('CPF')->item(0)->nodeValue; |
763
|
|
|
if (!empty($dID)) { |
764
|
|
|
$destID = "<CPF>$dID</CPF>"; |
765
|
|
|
} else { |
766
|
|
|
$dID = $dest->getElementsByTagName('idEstrangeiro') |
767
|
|
|
->item(0) |
768
|
|
|
->nodeValue; |
769
|
|
|
$destID = "<idEstrangeiro>$dID</idEstrangeiro>"; |
770
|
|
|
} |
771
|
|
|
} |
772
|
|
|
$dIE = !empty($dest->getElementsByTagName('IE')->item(0)->nodeValue) ? |
773
|
|
|
$dest->getElementsByTagName('IE')->item(0)->nodeValue : ''; |
774
|
|
|
$destIE = ''; |
775
|
|
|
if (!empty($dIE)) { |
776
|
|
|
$destIE = "<IE>$dIE</IE>"; |
777
|
|
|
} |
778
|
|
|
$tagAdic = "<cOrgaoAutor>$cOrgaoAutor</cOrgaoAutor>" |
779
|
|
|
. "<tpAutor>1</tpAutor>" |
780
|
|
|
. "<verAplic>$this->verAplic</verAplic>" |
781
|
|
|
. "<dhEmi>$dhEmi</dhEmi>" |
782
|
|
|
. "<tpNF>$tpNF</tpNF>" |
783
|
|
|
. "<IE>$emitIE</IE>" |
784
|
|
|
. "<dest>" |
785
|
|
|
. "<UF>$destUF</UF>" |
786
|
|
|
. $destID |
787
|
|
|
. $destIE |
788
|
|
|
. "<vNF>$vNF</vNF>" |
789
|
|
|
. "<vICMS>$vICMS</vICMS>" |
790
|
|
|
. "<vST>$vST</vST>" |
791
|
|
|
. "</dest>"; |
792
|
|
|
|
793
|
|
|
return $this->sefazEvento('AN', $chNFe, self::EVT_EPEC, $nSeqEvento, $tagAdic); |
794
|
|
|
} |
795
|
|
|
|
796
|
|
|
/** |
797
|
|
|
* Send event to SEFAZ |
798
|
|
|
* @param string $uf |
799
|
|
|
* @param string $chave |
800
|
|
|
* @param int $tpEvento |
801
|
|
|
* @param int $nSeqEvento |
802
|
|
|
* @param string $tagAdic |
803
|
|
|
* @return string |
804
|
|
|
*/ |
805
|
|
|
public function sefazEvento( |
806
|
|
|
$uf, |
807
|
|
|
$chave, |
808
|
|
|
$tpEvento, |
809
|
|
|
$nSeqEvento = 1, |
810
|
|
|
$tagAdic = '' |
811
|
|
|
) { |
812
|
|
|
$ignore = $tpEvento == self::EVT_EPEC; |
813
|
|
|
$servico = 'RecepcaoEvento'; |
814
|
|
|
$this->checkContingencyForWebServices($servico); |
815
|
|
|
$this->servico($servico, $uf, $this->tpAmb, $ignore); |
816
|
|
|
$ev = $this->tpEv($tpEvento); |
817
|
|
|
$descEvento = $ev->desc; |
818
|
|
|
$cnpj = isset($this->config->cnpj) ? $this->config->cnpj : ''; |
819
|
|
|
$dt = new \DateTime(date("Y-m-d H:i:sP"), new \DateTimeZone($this->timezone)); |
820
|
|
|
$dhEvento = $dt->format('Y-m-d\TH:i:sP'); |
821
|
|
|
$sSeqEvento = str_pad($nSeqEvento, 2, "0", STR_PAD_LEFT); |
822
|
|
|
$eventId = "ID".$tpEvento.$chave.$sSeqEvento; |
823
|
|
|
$cOrgao = UFList::getCodeByUF($uf); |
824
|
|
|
$request = "<evento xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
825
|
|
|
. "<infEvento Id=\"$eventId\">" |
826
|
|
|
. "<cOrgao>$cOrgao</cOrgao>" |
827
|
|
|
. "<tpAmb>$this->tpAmb</tpAmb>"; |
828
|
|
|
if ($this->typePerson === 'J') { |
829
|
|
|
$request .= "<CNPJ>$cnpj</CNPJ>"; |
830
|
|
|
} else { |
831
|
|
|
$request .= "<CPF>$cnpj</CPF>"; |
832
|
|
|
} |
833
|
|
|
$request .= "<chNFe>$chave</chNFe>" |
834
|
|
|
. "<dhEvento>$dhEvento</dhEvento>" |
835
|
|
|
. "<tpEvento>$tpEvento</tpEvento>" |
836
|
|
|
. "<nSeqEvento>$nSeqEvento</nSeqEvento>" |
837
|
|
|
. "<verEvento>$this->urlVersion</verEvento>" |
838
|
|
|
. "<detEvento versao=\"$this->urlVersion\">" |
839
|
|
|
. "<descEvento>$descEvento</descEvento>" |
840
|
|
|
. "$tagAdic" |
841
|
|
|
. "</detEvento>" |
842
|
|
|
. "</infEvento>" |
843
|
|
|
. "</evento>"; |
844
|
|
|
//assinatura dos dados |
845
|
|
|
$request = Signer::sign( |
846
|
|
|
$this->certificate, |
847
|
|
|
$request, |
848
|
|
|
'infEvento', |
849
|
|
|
'Id', |
850
|
|
|
$this->algorithm, |
851
|
|
|
$this->canonical |
852
|
|
|
); |
853
|
|
|
$request = Strings::clearXmlString($request, true); |
854
|
|
|
$lote = $dt->format('YmdHis').rand(0, 9); |
855
|
|
|
$request = "<envEvento xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
856
|
|
|
. "<idLote>$lote</idLote>" |
857
|
|
|
. $request |
858
|
|
|
. "</envEvento>"; |
859
|
|
|
$this->isValid($this->urlVersion, $request, 'envEvento'); |
860
|
|
|
$this->lastRequest = $request; |
861
|
|
|
//return $request; |
862
|
|
|
$parameters = ['nfeDadosMsg' => $request]; |
863
|
|
|
$body = "<nfeDadosMsg xmlns=\"$this->urlNamespace\">$request</nfeDadosMsg>"; |
864
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
865
|
|
|
return $this->lastResponse; |
866
|
|
|
} |
867
|
|
|
|
868
|
|
|
/** |
869
|
|
|
* Request the NFe download already manifested by its recipient, by the key |
870
|
|
|
* using new service in NfeDistribuicaoDFe |
871
|
|
|
* NOTA: NfeDownloadNF is deactivated |
872
|
|
|
* @param string $chave |
873
|
|
|
* @return string |
874
|
|
|
* @throws InvalidArgumentException |
875
|
|
|
*/ |
876
|
|
|
public function sefazDownload($chave) |
877
|
|
|
{ |
878
|
|
|
if (empty($chave)) { |
879
|
|
|
throw new InvalidArgumentException('Download: chave esta vazia!'); |
880
|
|
|
} |
881
|
|
|
//carrega serviço |
882
|
|
|
$servico = 'NfeDistribuicaoDFe'; |
883
|
|
|
$this->checkContingencyForWebServices($servico); |
884
|
|
|
$this->servico($servico, 'AN', $this->tpAmb, true); |
885
|
|
|
$cUF = UFList::getCodeByUF($this->config->siglaUF); |
886
|
|
|
$tagChave = "<consChNFe><chNFe>$chave</chNFe></consChNFe>"; |
887
|
|
|
$cnpj = $this->config->cnpj; |
888
|
|
|
//monta a consulta |
889
|
|
|
$request = "<distDFeInt xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
890
|
|
|
. "<tpAmb>".$this->tpAmb."</tpAmb>" |
891
|
|
|
. "<cUFAutor>$cUF</cUFAutor>"; |
892
|
|
|
if ($this->typePerson === 'J') { |
893
|
|
|
$request .= "<CNPJ>$cnpj</CNPJ>"; |
894
|
|
|
} else { |
895
|
|
|
$request .= "<CPF>$cnpj</CPF>"; |
896
|
|
|
} |
897
|
|
|
$request .= "$tagChave" |
898
|
|
|
. "</distDFeInt>"; |
899
|
|
|
//valida o xml da requisição |
900
|
|
|
$this->isValid($this->urlVersion, $request, 'distDFeInt'); |
901
|
|
|
$this->lastRequest = $request; |
902
|
|
|
//montagem dos dados da mensagem SOAP |
903
|
|
|
$request = "<nfeDadosMsg xmlns=\"$this->urlNamespace\">$request</nfeDadosMsg>"; |
904
|
|
|
$parameters = ['nfeDistDFeInteresse' => $request]; |
905
|
|
|
$body = "<nfeDistDFeInteresse xmlns=\"$this->urlNamespace\">" |
906
|
|
|
. $request |
907
|
|
|
. "</nfeDistDFeInteresse>"; |
908
|
|
|
//este webservice não requer cabeçalho |
909
|
|
|
$this->objHeader = null; |
910
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
911
|
|
|
return $this->lastResponse; |
912
|
|
|
} |
913
|
|
|
|
914
|
|
|
/** |
915
|
|
|
* Maintenance of the Taxpayer Security Code - CSC (Old Token) |
916
|
|
|
* @param int $indOp Identificador do tipo de operação: |
917
|
|
|
* 1 - Consulta CSC Ativos; |
918
|
|
|
* 2 - Solicita novo CSC; |
919
|
|
|
* 3 - Revoga CSC Ativo |
920
|
|
|
* @return string |
921
|
|
|
* @throws InvalidArgumentException |
922
|
|
|
* @throws RuntimeException |
923
|
|
|
*/ |
924
|
|
|
public function sefazCsc($indOp) |
925
|
|
|
{ |
926
|
|
|
if (empty($indOp) || $indOp < 1 || $indOp > 3) { |
927
|
|
|
throw new InvalidArgumentException('CSC: identificador operacao invalido!'); |
928
|
|
|
} |
929
|
|
|
if ($this->modelo != 65) { |
930
|
|
|
throw new RuntimeException('CSC: modelo diferente de 65!'); |
931
|
|
|
} |
932
|
|
|
$raizCNPJ = substr($this->config->cnpj, 0, -6); |
933
|
|
|
//carrega serviço |
934
|
|
|
$servico = 'CscNFCe'; |
935
|
|
|
$this->checkContingencyForWebServices($servico); |
936
|
|
|
$this->servico($servico, $this->config->siglaUF, $this->tpAmb); |
937
|
|
|
$request = "<admCscNFCe versao=\"$this->urlVersion\" xmlns=\"$this->urlPortal\">" |
938
|
|
|
. "<tpAmb>$this->tpAmb</tpAmb>" |
939
|
|
|
. "<indOp>$indOp</indOp>" |
940
|
|
|
. "<raizCNPJ>$raizCNPJ</raizCNPJ>" |
941
|
|
|
. "</admCscNFCe>"; |
942
|
|
|
if ($indOp == 3) { |
943
|
|
|
$request = "<admCscNFCe versao=\"$this->urlVersion\" xmlns=\"$this->urlPortal\">" |
944
|
|
|
. "<tpAmb>$this->tpAmb</tpAmb>" |
945
|
|
|
. "<indOp>$indOp</indOp>" |
946
|
|
|
. "<raizCNPJ>$raizCNPJ</raizCNPJ>" |
947
|
|
|
. "<dadosCsc>" |
948
|
|
|
. "<idCsc>".$this->config->CSCid."</idCsc>" |
949
|
|
|
. "<codigoCsc>".$this->config->CSC."</codigoCsc>" |
950
|
|
|
. "</dadosCsc>" |
951
|
|
|
. "</admCscNFCe>"; |
952
|
|
|
} |
953
|
|
|
//o xsd não está disponivel |
954
|
|
|
//$this->isValid($this->urlVersion, $request, 'cscNFCe'); |
955
|
|
|
$this->lastRequest = $request; |
956
|
|
|
$parameters = ['nfeDadosMsg' => $request]; |
957
|
|
|
$body = "<nfeDadosMsg xmlns=\"$this->urlNamespace\">$request</nfeDadosMsg>"; |
958
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
959
|
|
|
return $this->lastResponse; |
960
|
|
|
} |
961
|
|
|
|
962
|
|
|
/** |
963
|
|
|
* Checks the validity of an NFe, normally used for received NFe |
964
|
|
|
* @param string $nfe |
965
|
|
|
* @return bool |
966
|
|
|
* @throws InvalidArgumentException |
967
|
|
|
*/ |
968
|
|
|
public function sefazValidate($nfe) |
969
|
|
|
{ |
970
|
|
|
if (empty($nfe)) { |
971
|
|
|
throw new InvalidArgumentException('Validacao NF-e: a string da NF-e esta vazia!'); |
972
|
|
|
} |
973
|
|
|
//verifica a assinatura da NFe, exception caso de falha |
974
|
|
|
Signer::isSigned($nfe); |
975
|
|
|
$dom = new \DOMDocument('1.0', 'utf-8'); |
976
|
|
|
$dom->formatOutput = false; |
977
|
|
|
$dom->preserveWhiteSpace = false; |
978
|
|
|
$dom->loadXML($nfe); |
979
|
|
|
//verifica a validade no webservice da SEFAZ |
980
|
|
|
$tpAmb = $dom->getElementsByTagName('tpAmb')->item(0)->nodeValue; |
981
|
|
|
$infNFe = $dom->getElementsByTagName('infNFe')->item(0); |
982
|
|
|
$chNFe = preg_replace('/[^0-9]/', '', $infNFe->getAttribute("Id")); |
983
|
|
|
$protocol = $dom->getElementsByTagName('nProt')->item(0)->nodeValue; |
984
|
|
|
$digval = $dom->getElementsByTagName('DigestValue')->item(0)->nodeValue; |
985
|
|
|
//consulta a NFe |
986
|
|
|
$response = $this->sefazConsultaChave($chNFe, $tpAmb); |
987
|
|
|
$ret = new \DOMDocument('1.0', 'UTF-8'); |
988
|
|
|
$ret->preserveWhiteSpace = false; |
989
|
|
|
$ret->formatOutput = false; |
990
|
|
|
$ret->loadXML($response); |
991
|
|
|
$retProt = $ret->getElementsByTagName('protNFe')->item(0); |
992
|
|
|
if (!isset($retProt)) { |
993
|
|
|
$xMotivo = $ret->getElementsByTagName('xMotivo')->item(0); |
994
|
|
|
if (isset($xMotivo)) { |
995
|
|
|
throw new InvalidArgumentException('Validacao NF-e: ' . $xMotivo->nodeValue); |
996
|
|
|
} else { |
997
|
|
|
throw new InvalidArgumentException('O documento de resposta nao contem o node "protNFe".'); |
998
|
|
|
} |
999
|
|
|
} |
1000
|
|
|
$infProt = $ret->getElementsByTagName('infProt')->item(0); |
1001
|
|
|
$dig = $infProt->getElementsByTagName("digVal")->item(0); |
1002
|
|
|
$digProt = '000'; |
1003
|
|
|
if (isset($dig)) { |
1004
|
|
|
$digProt = $dig->nodeValue; |
1005
|
|
|
} |
1006
|
|
|
$chProt = $infProt->getElementsByTagName("chNFe")->item(0)->nodeValue; |
1007
|
|
|
$nProt = $infProt->getElementsByTagName("nProt")->item(0)->nodeValue; |
1008
|
|
|
if ($protocol == $nProt && $digval == $digProt && $chNFe == $chProt) { |
1009
|
|
|
return true; |
1010
|
|
|
} |
1011
|
|
|
return false; |
1012
|
|
|
} |
1013
|
|
|
|
1014
|
|
|
/** |
1015
|
|
|
* Returns alias and description event from event code. |
1016
|
|
|
* @param int $tpEvento |
1017
|
|
|
* @return \stdClass |
1018
|
|
|
* @throws \RuntimeException |
1019
|
|
|
*/ |
1020
|
|
|
private function tpEv($tpEvento) |
1021
|
|
|
{ |
1022
|
|
|
$std = new \stdClass(); |
1023
|
|
|
$std->alias = ''; |
1024
|
|
|
$std->desc = ''; |
1025
|
|
|
switch ($tpEvento) { |
1026
|
|
|
case self::EVT_CCE: |
1027
|
|
|
$std->alias = 'CCe'; |
1028
|
|
|
$std->desc = 'Carta de Correcao'; |
1029
|
|
|
break; |
1030
|
|
|
case self::EVT_CANCELA: |
1031
|
|
|
$std->alias = 'CancNFe'; |
1032
|
|
|
$std->desc = 'Cancelamento'; |
1033
|
|
|
break; |
1034
|
|
|
case self::EVT_CANCELASUBSTITUICAO: |
1035
|
|
|
$std->alias = 'CancNFe'; |
1036
|
|
|
$std->desc = 'Cancelamento por substituicao'; |
1037
|
|
|
break; |
1038
|
|
|
case self::EVT_EPEC: // Emissão em contingência EPEC |
1039
|
|
|
$std->alias = 'EPEC'; |
1040
|
|
|
$std->desc = 'EPEC'; |
1041
|
|
|
break; |
1042
|
|
|
case 111500: |
1043
|
|
|
case 111501: |
1044
|
|
|
//EPP |
1045
|
|
|
//Pedido de prorrogação |
1046
|
|
|
$std->alias = 'EPP'; |
1047
|
|
|
$std->desc = 'Pedido de Prorrogacao'; |
1048
|
|
|
break; |
1049
|
|
|
case 111502: |
1050
|
|
|
case 111503: |
1051
|
|
|
//ECPP |
1052
|
|
|
//Cancelamento do Pedido de prorrogação |
1053
|
|
|
$std->alias = 'ECPP'; |
1054
|
|
|
$std->desc = 'Cancelamento de Pedido de Prorrogacao'; |
1055
|
|
|
break; |
1056
|
|
|
case self::EVT_CONFIRMACAO: // Manifestação Confirmacao da Operação |
1057
|
|
|
$std->alias = 'EvConfirma'; |
1058
|
|
|
$std->desc = 'Confirmacao da Operacao'; |
1059
|
|
|
break; |
1060
|
|
|
case self::EVT_CIENCIA: // Manifestação Ciencia da Operação |
1061
|
|
|
$std->alias = 'EvCiencia'; |
1062
|
|
|
$std->desc = 'Ciencia da Operacao'; |
1063
|
|
|
$std->tpAutor = 2; |
1064
|
|
|
break; |
1065
|
|
|
case self::EVT_DESCONHECIMENTO: // Manifestação Desconhecimento da Operação |
1066
|
|
|
$std->alias = 'EvDesconh'; |
1067
|
|
|
$std->desc = 'Desconhecimento da Operacao'; |
1068
|
|
|
break; |
1069
|
|
|
case self::EVT_NAO_REALIZADA: // Manifestação Operacao não Realizada |
1070
|
|
|
$std->alias = 'EvNaoRealizada'; |
1071
|
|
|
$std->desc = 'Operacao nao Realizada'; |
1072
|
|
|
break; |
1073
|
|
|
case self::EVT_ATORINTERESSADO: //ator interessado |
1074
|
|
|
$std->alias = 'EvAtorInteressado'; |
1075
|
|
|
$std->desc = 'Ator interessado na NF-e'; |
1076
|
|
|
break; |
1077
|
|
|
default: |
1078
|
|
|
$msg = "O código do tipo de evento informado não corresponde a " |
1079
|
|
|
. "nenhum evento estabelecido."; |
1080
|
|
|
throw new RuntimeException($msg); |
1081
|
|
|
} |
1082
|
|
|
return $std; |
1083
|
|
|
} |
1084
|
|
|
} |
1085
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.