1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NFePHP\CTe; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Classe principal para a comunicação com a SEFAZ |
7
|
|
|
* |
8
|
|
|
* @category Library |
9
|
|
|
* @package nfephp-org/sped-cte |
10
|
|
|
* @copyright 2009-2016 NFePHP |
11
|
|
|
* @license http://www.gnu.org/licenses/lesser.html LGPL v3 |
12
|
|
|
* @link http://github.com/nfephp-org/sped-cte for the canonical source repository |
13
|
|
|
* @author Roberto L. Machado <linux.rlm at gmail dot com> |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
use NFePHP\Common\Base\BaseTools; |
17
|
|
|
use NFePHP\Common\LotNumber\LotNumber; |
18
|
|
|
use NFePHP\Common\Strings\Strings; |
19
|
|
|
use NFePHP\Common\Files; |
20
|
|
|
use NFePHP\Common\Exception; |
21
|
|
|
use NFePHP\CTe\Auxiliar\Response; |
22
|
|
|
use NFePHP\CTe\Auxiliar\IdentifyCTe; |
23
|
|
|
use NFePHP\Common\Dom\ValidXsd; |
24
|
|
|
|
25
|
|
|
if (!defined('NFEPHP_ROOT')) { |
26
|
|
|
define('NFEPHP_ROOT', dirname(dirname(__FILE__))); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
class Tools extends BaseTools |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* urlPortal |
33
|
|
|
* Instância do WebService |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $urlPortal = 'http://www.portalfiscal.inf.br/cte'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* errrors |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
public $erros = array(); |
43
|
|
|
|
44
|
|
|
protected $modelo = '65'; |
45
|
|
|
|
46
|
|
|
public function printCTe() |
47
|
|
|
{ |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function mailCTe() |
51
|
|
|
{ |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* assina |
56
|
|
|
* @param string $xml |
57
|
|
|
* @param boolean $saveFile |
58
|
|
|
* @return string |
59
|
|
|
* @throws Exception\RuntimeException |
60
|
|
|
*/ |
61
|
|
|
public function assina($xml = '', $saveFile = false) |
62
|
|
|
{ |
63
|
|
|
return $this->assinaDoc($xml, 'cte', 'infCte', $saveFile); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function sefazEnvia( |
67
|
|
|
$aXml, |
68
|
|
|
$tpAmb = '2', |
69
|
|
|
$idLote = '', |
70
|
|
|
&$aRetorno = array(), |
71
|
|
|
$indSinc = 0, |
|
|
|
|
72
|
|
|
$compactarZip = false |
|
|
|
|
73
|
|
|
) { |
74
|
|
|
$this->modelo = '65'; |
75
|
|
|
$sxml = $aXml; |
76
|
|
|
if (empty($aXml)) { |
77
|
|
|
$msg = "Pelo menos uma NFe deve ser informada."; |
78
|
|
|
throw new Exception\InvalidArgumentException($msg); |
79
|
|
|
} |
80
|
|
|
if (is_array($aXml)) { |
81
|
|
|
if (count($aXml) > 1) { |
82
|
|
|
//multiplas cte, não pode ser sincrono |
83
|
|
|
$indSinc = 0; |
|
|
|
|
84
|
|
|
} |
85
|
|
|
$sxml = implode("", $sxml); |
86
|
|
|
} |
87
|
|
|
$sxml = preg_replace("/<\?xml.*\?>/", "", $sxml); |
88
|
|
|
$siglaUF = $this->aConfig['siglaUF']; |
89
|
|
|
|
90
|
|
|
if ($tpAmb == '') { |
91
|
|
|
$tpAmb = $this->aConfig['tpAmb']; |
92
|
|
|
} |
93
|
|
|
if ($idLote == '') { |
94
|
|
|
$idLote = LotNumber::geraNumLote(15); |
95
|
|
|
} |
96
|
|
|
//carrega serviço |
97
|
|
|
$servico = 'CteRecepcao'; |
98
|
|
|
$this->zLoadServico( |
99
|
|
|
'cte', |
100
|
|
|
$servico, |
101
|
|
|
$siglaUF, |
102
|
|
|
$tpAmb |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
if ($this->urlService == '') { |
106
|
|
|
$msg = "O envio de lote não está disponível na SEFAZ $siglaUF!!!"; |
107
|
|
|
throw new Exception\RuntimeException($msg); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// Montagem dos dados da mensagem SOAP |
111
|
|
|
$dados = "<cteDadosMsg xmlns=\"$this->urlNamespace\">" |
112
|
|
|
. "<enviCTe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
113
|
|
|
. "<idLote>$idLote</idLote>" |
114
|
|
|
. "$sxml" |
115
|
|
|
. "</enviCTe>" |
116
|
|
|
. "</cteDadosMsg>"; |
117
|
|
|
|
118
|
|
|
// Envia dados via SOAP |
119
|
|
|
$retorno = $this->oSoap->send( |
120
|
|
|
$this->urlService, |
121
|
|
|
$this->urlNamespace, |
122
|
|
|
$this->urlHeader, |
123
|
|
|
$dados, |
124
|
|
|
$this->urlMethod |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
// if ($compactarZip) { |
|
|
|
|
128
|
|
|
// $gzdata = base64_encode(gzencode($cons, 9, FORCE_GZIP)); |
129
|
|
|
// $body = "<cteDadosMsgZip xmlns=\"$this->urlNamespace\">$gzdata</cteDadosMsgZip>"; |
130
|
|
|
// $method = $this->urlMethod."Zip"; |
131
|
|
|
// } |
132
|
|
|
|
133
|
|
|
$lastMsg = $this->oSoap->lastMsg; |
134
|
|
|
$this->soapDebug = $this->oSoap->soapDebug; |
|
|
|
|
135
|
|
|
//salva mensagens |
136
|
|
|
$filename = "$idLote-enviCTe.xml"; |
137
|
|
|
$this->zGravaFile('cte', $tpAmb, $filename, $lastMsg); |
138
|
|
|
$filename = "$idLote-retEnviCTe.xml"; |
139
|
|
|
$this->zGravaFile('cte', $tpAmb, $filename, $retorno); |
140
|
|
|
//tratar dados de retorno |
141
|
|
|
|
142
|
|
|
$aRetorno = Response::readReturnSefaz($servico, $retorno); |
143
|
|
|
//caso o envio seja recebido com sucesso mover a NFe da pasta |
144
|
|
|
//das assinadas para a pasta das enviadas |
145
|
|
|
return (string) $retorno; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function sefazConsultaRecibo($recibo = '', $tpAmb = '2', &$aRetorno = array()) |
149
|
|
|
{ |
150
|
|
|
if ($recibo == '') { |
151
|
|
|
$msg = "Deve ser informado um recibo."; |
152
|
|
|
throw new Exception\InvalidArgumentException($msg); |
153
|
|
|
} |
154
|
|
|
if ($tpAmb == '') { |
155
|
|
|
$tpAmb = $this->aConfig['tpAmb']; |
156
|
|
|
} |
157
|
|
|
$siglaUF = $this->aConfig['siglaUF']; |
158
|
|
|
//carrega serviço |
159
|
|
|
$servico = 'CteRetRecepcao'; |
160
|
|
|
$this->zLoadServico( |
161
|
|
|
'cte', |
162
|
|
|
$servico, |
163
|
|
|
$siglaUF, |
164
|
|
|
$tpAmb |
165
|
|
|
); |
166
|
|
|
if ($this->urlService == '') { |
167
|
|
|
$msg = "A consulta de NFe não está disponível na SEFAZ $siglaUF!!!"; |
168
|
|
|
throw new Exception\RuntimeException($msg); |
169
|
|
|
} |
170
|
|
|
$cons = "<consReciCTe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
171
|
|
|
. "<tpAmb>$tpAmb</tpAmb>" |
172
|
|
|
. "<nRec>$recibo</nRec>" |
173
|
|
|
. "</consReciCTe>"; |
174
|
|
|
//validar mensagem com xsd |
175
|
|
|
//if (! $this->validarXml($cons)) { |
|
|
|
|
176
|
|
|
// $msg = 'Falha na validação. '.$this->error; |
|
|
|
|
177
|
|
|
// throw new Exception\RuntimeException($msg); |
|
|
|
|
178
|
|
|
//} |
179
|
|
|
//montagem dos dados da mensagem SOAP |
180
|
|
|
$body = "<cteDadosMsg xmlns=\"$this->urlNamespace\">$cons</cteDadosMsg>"; |
181
|
|
|
//envia a solicitação via SOAP |
182
|
|
|
$retorno = $this->oSoap->send( |
183
|
|
|
$this->urlService, |
184
|
|
|
$this->urlNamespace, |
185
|
|
|
$this->urlHeader, |
186
|
|
|
$body, |
187
|
|
|
$this->urlMethod |
188
|
|
|
); |
189
|
|
|
$lastMsg = $this->oSoap->lastMsg; |
190
|
|
|
$this->soapDebug = $this->oSoap->soapDebug; |
191
|
|
|
//salva mensagens |
192
|
|
|
$filename = "$recibo-consReciCTe.xml"; |
193
|
|
|
$this->zGravaFile('cte', $tpAmb, $filename, $lastMsg); |
194
|
|
|
$filename = "$recibo-retConsReciCTe.xml"; |
195
|
|
|
$this->zGravaFile('cte', $tpAmb, $filename, $retorno); |
196
|
|
|
//tratar dados de retorno |
197
|
|
|
$aRetorno = Response::readReturnSefaz($servico, $retorno); |
198
|
|
|
//podem ser retornados nenhum, um ou vários protocolos |
199
|
|
|
//caso existam protocolos protocolar as NFe e movelas-las para a |
200
|
|
|
//pasta enviadas/aprovadas/anomes |
201
|
|
|
return (string) $retorno; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function sefazConsultaChave($chave = '', $tpAmb = '2', &$aRetorno = array()) |
205
|
|
|
{ |
206
|
|
|
$chNFe = preg_replace('/[^0-9]/', '', $chave); |
207
|
|
|
if (strlen($chNFe) != 44) { |
208
|
|
|
$msg = "Uma chave de 44 dígitos da NFe deve ser passada."; |
209
|
|
|
throw new Exception\InvalidArgumentException($msg); |
210
|
|
|
} |
211
|
|
|
if ($tpAmb == '') { |
212
|
|
|
$tpAmb = $this->aConfig['tpAmb']; |
213
|
|
|
} |
214
|
|
|
$cUF = substr($chNFe, 0, 2); |
215
|
|
|
$siglaUF = $this->zGetSigla($cUF); |
216
|
|
|
//carrega serviço |
217
|
|
|
$servico = 'CteConsultaProtocolo'; |
218
|
|
|
$this->zLoadServico( |
219
|
|
|
'cte', |
220
|
|
|
$servico, |
221
|
|
|
$siglaUF, |
222
|
|
|
$tpAmb |
223
|
|
|
); |
224
|
|
|
if ($this->urlService == '') { |
225
|
|
|
$msg = "A consulta de NFe não está disponível na SEFAZ $siglaUF!!!"; |
226
|
|
|
throw new Exception\RuntimeException($msg); |
227
|
|
|
} |
228
|
|
|
$cons = "<consSitCTe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
229
|
|
|
. "<tpAmb>$tpAmb</tpAmb>" |
230
|
|
|
. "<xServ>CONSULTAR</xServ>" |
231
|
|
|
. "<chCTe>$chNFe</chCTe>" |
232
|
|
|
. "</consSitCTe>"; |
233
|
|
|
//validar mensagem com xsd |
234
|
|
|
//if (! $this->validarXml($cons)) { |
|
|
|
|
235
|
|
|
// $msg = 'Falha na validação. '.$this->error; |
|
|
|
|
236
|
|
|
// throw new Exception\RuntimeException($msg); |
|
|
|
|
237
|
|
|
//} |
238
|
|
|
//montagem dos dados da mensagem SOAP |
239
|
|
|
$body = "<cteDadosMsg xmlns=\"$this->urlNamespace\">$cons</cteDadosMsg>"; |
240
|
|
|
//envia a solicitação via SOAP |
241
|
|
|
$retorno = $this->oSoap->send( |
242
|
|
|
$this->urlService, |
243
|
|
|
$this->urlNamespace, |
244
|
|
|
$this->urlHeader, |
245
|
|
|
$body, |
246
|
|
|
$this->urlMethod |
247
|
|
|
); |
248
|
|
|
$lastMsg = $this->oSoap->lastMsg; |
249
|
|
|
$this->soapDebug = $this->oSoap->soapDebug; |
250
|
|
|
//salva mensagens |
251
|
|
|
$filename = "$chNFe-consSitCTe.xml"; |
252
|
|
|
$this->zGravaFile('cte', $tpAmb, $filename, $lastMsg); |
253
|
|
|
$filename = "$chNFe-retConsSitNFe.xml"; |
254
|
|
|
$this->zGravaFile('cte', $tpAmb, $filename, $retorno); |
255
|
|
|
//tratar dados de retorno |
256
|
|
|
$aRetorno = Response::readReturnSefaz($servico, $retorno); |
257
|
|
|
return (string) $retorno; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
public function sefazStatus($siglaUF = '', $tpAmb = '2', &$aRetorno = array()) |
261
|
|
|
{ |
262
|
|
|
if ($tpAmb == '') { |
263
|
|
|
$tpAmb = $this->aConfig['tpAmb']; |
264
|
|
|
} |
265
|
|
|
if ($siglaUF == '') { |
266
|
|
|
$siglaUF = $this->aConfig['siglaUF']; |
267
|
|
|
} |
268
|
|
|
//carrega serviço |
269
|
|
|
$servico = 'CteStatusServico'; |
270
|
|
|
$this->zLoadServico( |
271
|
|
|
'cte', |
272
|
|
|
$servico, |
273
|
|
|
$siglaUF, |
274
|
|
|
$tpAmb |
275
|
|
|
); |
276
|
|
|
if ($this->urlService == '') { |
277
|
|
|
$msg = "O status não está disponível na SEFAZ $siglaUF!!!"; |
278
|
|
|
throw new Exception\RuntimeException($msg); |
279
|
|
|
} |
280
|
|
|
$cons = "<consStatServCte xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
281
|
|
|
. "<tpAmb>$tpAmb</tpAmb>" |
282
|
|
|
. "<xServ>STATUS</xServ></consStatServCte>"; |
283
|
|
|
//valida mensagem com xsd |
284
|
|
|
//validar mensagem com xsd |
285
|
|
|
//if (! $this->validarXml($cons)) { |
|
|
|
|
286
|
|
|
// $msg = 'Falha na validação. '.$this->error; |
|
|
|
|
287
|
|
|
// throw new Exception\RuntimeException($msg); |
|
|
|
|
288
|
|
|
//} |
289
|
|
|
//montagem dos dados da mensagem SOAP |
290
|
|
|
$body = "<cteDadosMsg xmlns=\"$this->urlNamespace\">$cons</cteDadosMsg>"; |
291
|
|
|
//consome o webservice e verifica o retorno do SOAP |
292
|
|
|
$retorno = $this->oSoap->send( |
293
|
|
|
$this->urlService, |
294
|
|
|
$this->urlNamespace, |
295
|
|
|
$this->urlHeader, |
296
|
|
|
$body, |
297
|
|
|
$this->urlMethod |
298
|
|
|
); |
299
|
|
|
$lastMsg = $this->oSoap->lastMsg; |
300
|
|
|
$this->soapDebug = $this->oSoap->soapDebug; |
301
|
|
|
$datahora = date('Ymd_His'); |
302
|
|
|
$filename = $siglaUF."_"."$datahora-consStatServCte.xml"; |
303
|
|
|
$this->zGravaFile('cte', $tpAmb, $filename, $lastMsg); |
304
|
|
|
$filename = $siglaUF."_"."$datahora-retConsStatServCte.xml"; |
305
|
|
|
$this->zGravaFile('cte', $tpAmb, $filename, $retorno); |
306
|
|
|
//tratar dados de retorno |
307
|
|
|
$aRetorno = Response::readReturnSefaz($servico, $retorno); |
308
|
|
|
return (string) $retorno; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
public function sefazInutiliza( |
312
|
|
|
$nSerie = '1', |
313
|
|
|
$nIni = '', |
314
|
|
|
$nFin = '', |
315
|
|
|
$xJust = '', |
316
|
|
|
$tpAmb = '2', |
317
|
|
|
&$aRetorno = array(), |
318
|
|
|
$salvarMensagens = true |
319
|
|
|
) { |
320
|
|
|
$nSerie = (integer) $nSerie; |
321
|
|
|
$nIni = (integer) $nIni; |
322
|
|
|
$nFin = (integer) $nFin; |
323
|
|
|
$xJust = Strings::cleanString($xJust); |
324
|
|
|
$this->zValidParamInut($xJust, $nSerie, $nIni, $nFin); |
325
|
|
|
if ($tpAmb == '') { |
326
|
|
|
$tpAmb = $this->aConfig['tpAmb']; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
// Identificação do serviço |
330
|
|
|
$servico = 'CteInutilizacao'; |
|
|
|
|
331
|
|
|
//monta serviço |
332
|
|
|
$siglaUF = $this->aConfig['siglaUF']; |
333
|
|
|
//carrega serviço |
334
|
|
|
$servico = 'CteInutilizacao'; |
335
|
|
|
$this->zLoadServico( |
336
|
|
|
'cte', |
337
|
|
|
$servico, |
338
|
|
|
$siglaUF, |
339
|
|
|
$tpAmb |
340
|
|
|
); |
341
|
|
|
|
342
|
|
|
if ($this->urlService == '') { |
343
|
|
|
$msg = "A inutilização não está disponível na SEFAZ $siglaUF!!!"; |
344
|
|
|
throw new Exception\RuntimeException($msg); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
//montagem dos dados da mensagem SOAP |
348
|
|
|
$cnpj = $this->aConfig['cnpj']; |
349
|
|
|
$sAno = (string) date('y'); |
350
|
|
|
$sSerie = str_pad($nSerie, 3, '0', STR_PAD_LEFT); |
351
|
|
|
$sInicio = str_pad($nIni, 9, '0', STR_PAD_LEFT); |
352
|
|
|
$sFinal = str_pad($nFin, 9, '0', STR_PAD_LEFT); |
353
|
|
|
|
354
|
|
|
//limpa os caracteres indesejados da justificativa |
355
|
|
|
$xJust = Strings::cleanString($xJust); |
356
|
|
|
|
357
|
|
|
// Identificador da TAG a ser assinada formada com Código da UF + |
358
|
|
|
// precedida do literal “ID” |
359
|
|
|
// 41 posições |
360
|
|
|
$id = 'ID'.$this->urlcUF.$cnpj.'57'.$sSerie.$sInicio.$sFinal; |
361
|
|
|
|
362
|
|
|
// Montagem do corpo da mensagem |
363
|
|
|
$dXML = "<inutCTe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
364
|
|
|
."<infInut Id=\"$id\">" |
365
|
|
|
."<tpAmb>$tpAmb</tpAmb>" |
366
|
|
|
."<xServ>INUTILIZAR</xServ>" |
367
|
|
|
."<cUF>$this->urlcUF</cUF>" |
368
|
|
|
."<ano>$sAno</ano>" |
369
|
|
|
."<CNPJ>$cnpj</CNPJ>" |
370
|
|
|
."<mod>57</mod>" |
371
|
|
|
."<serie>$nSerie</serie>" |
372
|
|
|
."<nCTIni>$nIni</nCTIni>" |
373
|
|
|
."<nCTFin>$nFin</nCTFin>" |
374
|
|
|
."<xJust>$xJust</xJust>" |
375
|
|
|
."</infInut></inutCTe>"; |
376
|
|
|
|
377
|
|
|
//assina a solicitação de inutilização |
378
|
|
|
$signedMsg = $this->oCertificate->signXML($dXML, 'infInut'); |
379
|
|
|
$signedMsg = Strings::clearXml($signedMsg, true); |
380
|
|
|
|
381
|
|
|
$body = "<cteDadosMsg xmlns=\"$this->urlNamespace\">$signedMsg</cteDadosMsg>"; |
382
|
|
|
|
383
|
|
|
//envia a solicitação via SOAP |
384
|
|
|
$retorno = $this->oSoap->send( |
385
|
|
|
$this->urlService, |
386
|
|
|
$this->urlNamespace, |
387
|
|
|
$this->urlHeader, |
388
|
|
|
$body, |
389
|
|
|
$this->urlMethod |
390
|
|
|
); |
391
|
|
|
$lastMsg = $this->oSoap->lastMsg; |
392
|
|
|
$this->soapDebug = $this->oSoap->soapDebug; |
393
|
|
|
|
394
|
|
|
//salva mensagens |
395
|
|
|
if ($salvarMensagens) { |
396
|
|
|
$filename = "$sAno-$this->modelo-$sSerie-".$sInicio."_".$sFinal."-inutCTe.xml"; |
397
|
|
|
$this->zGravaFile('cte', $tpAmb, $filename, $lastMsg); |
398
|
|
|
$filename = "$sAno-$this->modelo-$sSerie-".$sInicio."_".$sFinal."-retInutCTe.xml"; |
399
|
|
|
$this->zGravaFile('cte', $tpAmb, $filename, $retorno); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
//tratar dados de retorno |
403
|
|
|
$aRetorno = Response::readReturnSefaz($servico, $retorno); |
404
|
|
|
if ($aRetorno['cStat'] == '102') { |
405
|
|
|
$retorno = $this->zAddProtMsg('ProcInutCTe', 'inutCTe', $signedMsg, 'retInutCTe', $retorno); |
406
|
|
|
if ($salvarMensagens) { |
407
|
|
|
$filename = "$sAno-$this->modelo-$sSerie-".$sInicio."_".$sFinal."-procInutCTe.xml"; |
408
|
|
|
$this->zGravaFile('cte', $tpAmb, $filename, $retorno, 'inutilizadas'); |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
return (string) $retorno; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
public function sefazCancela($chCTe = '', $tpAmb = '2', $xJust = '', $nProt = '', &$aRetorno = array()) |
416
|
|
|
{ |
417
|
|
|
$chCTe = preg_replace('/[^0-9]/', '', $chCTe); |
418
|
|
|
$nProt = preg_replace('/[^0-9]/', '', $nProt); |
419
|
|
|
$xJust = Strings::cleanString($xJust); |
420
|
|
|
//validação dos dados de entrada |
421
|
|
|
if (strlen($chCTe) != 44) { |
422
|
|
|
$msg = "Uma chave de CTe válida não foi passada como parâmetro $chCTe."; |
423
|
|
|
throw new Exception\InvalidArgumentException($msg); |
424
|
|
|
} |
425
|
|
|
if ($nProt == '') { |
426
|
|
|
$msg = "Não foi passado o numero do protocolo!!"; |
427
|
|
|
throw new Exception\InvalidArgumentException($msg); |
428
|
|
|
} |
429
|
|
|
if (strlen($xJust) < 15 || strlen($xJust) > 255) { |
430
|
|
|
$msg = "A justificativa deve ter pelo menos 15 digitos e no máximo 255!!"; |
431
|
|
|
throw new Exception\InvalidArgumentException($msg); |
432
|
|
|
} |
433
|
|
|
$siglaUF = $this->zGetSigla(substr($chCTe, 0, 2)); |
434
|
|
|
|
435
|
|
|
//estabelece o codigo do tipo de evento CANCELAMENTO |
436
|
|
|
$tpEvento = '110111'; |
437
|
|
|
$descEvento = 'Cancelamento'; |
438
|
|
|
$nSeqEvento = 1; |
439
|
|
|
//monta mensagem |
440
|
|
|
$tagAdic = "<evCancCTe>" |
441
|
|
|
. "<descEvento>$descEvento</descEvento>" |
442
|
|
|
. "<nProt>$nProt</nProt>" |
443
|
|
|
. "<xJust>$xJust</xJust>" |
444
|
|
|
. "</evCancCTe>"; |
445
|
|
|
$retorno = $this->zSefazEvento($siglaUF, $chCTe, $tpAmb, $tpEvento, $nSeqEvento, $tagAdic); |
446
|
|
|
$aRetorno = $this->aLastRetEvent; |
|
|
|
|
447
|
|
|
return $retorno; |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
public function enviaMail($pathXml = '', $aMails = array(), $templateFile = '', $comPdf = false, $pathPdf = '') |
|
|
|
|
451
|
|
|
{ |
452
|
|
|
$mail = new Mail($this->aMailConf); |
453
|
|
|
// Se não for informado o caminho do PDF, monta um através do XML |
454
|
|
|
/* |
|
|
|
|
455
|
|
|
if ($comPdf && $this->modelo == '55' && $pathPdf == '') { |
456
|
|
|
$docxml = Files\FilesFolders::readFile($pathXml); |
457
|
|
|
$danfe = new Extras\Danfe($docxml, 'P', 'A4', $this->aDocFormat['pathLogoFile'], 'I', ''); |
458
|
|
|
$id = $danfe->montaDANFE(); |
459
|
|
|
$pathPdf = $this->aConfig['pathNFeFiles'] |
460
|
|
|
. DIRECTORY_SEPARATOR |
461
|
|
|
. $this->ambiente |
462
|
|
|
. DIRECTORY_SEPARATOR |
463
|
|
|
. 'pdf' |
464
|
|
|
. DIRECTORY_SEPARATOR |
465
|
|
|
. $id . '-danfe.pdf'; |
466
|
|
|
$pdf = $danfe->printDANFE($pathPdf, 'F'); |
467
|
|
|
} |
468
|
|
|
* |
469
|
|
|
*/ |
470
|
|
|
if ($mail->envia($pathXml, $aMails, $comPdf, $pathPdf) === false) { |
471
|
|
|
throw new Exception\RuntimeException('Email não enviado. '.$mail->error); |
472
|
|
|
} |
473
|
|
|
return true; |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* zSefazEvento |
478
|
|
|
* @param string $siglaUF |
479
|
|
|
* @param string $chCTe |
480
|
|
|
* @param string $tpAmb |
481
|
|
|
* @param string $tpEvento |
482
|
|
|
* @param string $nSeqEvento |
483
|
|
|
* @param string $tagAdic |
484
|
|
|
* @return string |
485
|
|
|
* @throws Exception\RuntimeException |
486
|
|
|
* @internal function zLoadServico (Common\Base\BaseTools) |
487
|
|
|
*/ |
488
|
|
|
protected function zSefazEvento( |
489
|
|
|
$siglaUF = '', |
490
|
|
|
$chCTe = '', |
491
|
|
|
$tpAmb = '2', |
492
|
|
|
$tpEvento = '', |
493
|
|
|
$nSeqEvento = '1', |
494
|
|
|
$tagAdic = '' |
495
|
|
|
) { |
496
|
|
|
if ($tpAmb == '') { |
497
|
|
|
$tpAmb = $this->aConfig['tpAmb']; |
498
|
|
|
} |
499
|
|
|
//carrega serviço |
500
|
|
|
$servico = 'CteRecepcaoEvento'; |
501
|
|
|
$this->zLoadServico( |
502
|
|
|
'cte', |
503
|
|
|
$servico, |
504
|
|
|
$siglaUF, |
505
|
|
|
$tpAmb |
506
|
|
|
); |
507
|
|
|
if ($this->urlService == '') { |
508
|
|
|
$msg = "A recepção de eventos não está disponível na SEFAZ $siglaUF!!!"; |
509
|
|
|
throw new Exception\RuntimeException($msg); |
510
|
|
|
} |
511
|
|
|
$aRet = $this->zTpEv($tpEvento); |
512
|
|
|
$aliasEvento = $aRet['alias']; |
513
|
|
|
$descEvento = $aRet['desc']; |
|
|
|
|
514
|
|
|
$cnpj = $this->aConfig['cnpj']; |
515
|
|
|
$dhEvento = (string) str_replace(' ', 'T', date('Y-m-d H:i:s')); |
516
|
|
|
// $dhEvento = (string) str_replace(' ', 'T', date('Y-m-d H:i:sP')); |
|
|
|
|
517
|
|
|
$sSeqEvento = str_pad($nSeqEvento, 2, "0", STR_PAD_LEFT); |
518
|
|
|
$eventId = "ID".$tpEvento.$chCTe.$sSeqEvento; |
519
|
|
|
$cOrgao = $this->urlcUF; |
520
|
|
|
if ($siglaUF == 'AN') { |
521
|
|
|
$cOrgao = '91'; |
522
|
|
|
} |
523
|
|
|
$mensagem = "<infEvento Id=\"$eventId\">" |
524
|
|
|
. "<cOrgao>$cOrgao</cOrgao>" |
525
|
|
|
. "<tpAmb>$tpAmb</tpAmb>" |
526
|
|
|
. "<CNPJ>$cnpj</CNPJ>" |
527
|
|
|
. "<chCTe>$chCTe</chCTe>" |
528
|
|
|
. "<dhEvento>$dhEvento</dhEvento>" |
529
|
|
|
. "<tpEvento>$tpEvento</tpEvento>" |
530
|
|
|
. "<nSeqEvento>$nSeqEvento</nSeqEvento>" |
531
|
|
|
//. "<nSeqEvento>$sSeqEvento</nSeqEvento>" |
532
|
|
|
. "<detEvento versaoEvento=\"$this->urlVersion\">" |
533
|
|
|
. "$tagAdic" |
534
|
|
|
. "</detEvento>" |
535
|
|
|
. "</infEvento>"; |
536
|
|
|
|
537
|
|
|
$cons = "<eventoCTe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
538
|
|
|
. "$mensagem" |
539
|
|
|
. "</eventoCTe>"; |
540
|
|
|
|
541
|
|
|
$signedMsg = $this->oCertificate->signXML($cons, 'infEvento'); |
542
|
|
|
$signedMsg = preg_replace("/<\?xml.*\?>/", "", $signedMsg); |
543
|
|
|
|
544
|
|
|
//$signedMsg = Strings::clearXml($signedMsg, true); |
|
|
|
|
545
|
|
|
|
546
|
|
|
// if (! $this->zValidMessage($signedMsg, 'cte', 'envEvento', $this->urlVersion)) { |
|
|
|
|
547
|
|
|
// $msg = 'Falha na validação. '.$this->error; |
548
|
|
|
// throw new Exception\RuntimeException($msg); |
549
|
|
|
// } |
550
|
|
|
|
551
|
|
|
|
552
|
|
|
// $filename = "../cancelamento.xml"; |
|
|
|
|
553
|
|
|
// $xml = file_get_contents($filename); |
554
|
|
|
|
555
|
|
|
|
556
|
|
|
//$body = "<cteDadosMsg xmlns=\"$this->urlNamespace\">$xml</cteDadosMsg>"; |
|
|
|
|
557
|
|
|
$body = "<cteDadosMsg xmlns=\"$this->urlNamespace\">$signedMsg</cteDadosMsg>"; |
558
|
|
|
|
559
|
|
|
$retorno = $this->oSoap->send( |
560
|
|
|
$this->urlService, |
561
|
|
|
$this->urlNamespace, |
562
|
|
|
$this->urlHeader, |
563
|
|
|
$body, |
564
|
|
|
$this->urlMethod |
565
|
|
|
); |
566
|
|
|
$lastMsg = $this->oSoap->lastMsg; |
567
|
|
|
$this->soapDebug = $this->oSoap->soapDebug; |
568
|
|
|
//salva mensagens |
569
|
|
|
$filename = "$chCTe-$aliasEvento-envEvento.xml"; |
570
|
|
|
$this->zGravaFile('cte', $tpAmb, $filename, $lastMsg); |
571
|
|
|
$filename = "$chCTe-$aliasEvento-retEnvEvento.xml"; |
572
|
|
|
$this->zGravaFile('cte', $tpAmb, $filename, $retorno); |
573
|
|
|
//tratar dados de retorno |
574
|
|
|
$this->aLastRetEvent = Response::readReturnSefaz($servico, $retorno); |
575
|
|
|
if ($this->aLastRetEvent['cStat'] == '128') { |
576
|
|
|
if ($this->aLastRetEvent['evento'][0]['cStat'] == '135' || |
577
|
|
|
$this->aLastRetEvent['evento'][0]['cStat'] == '136' || |
578
|
|
|
$this->aLastRetEvent['evento'][0]['cStat'] == '155' |
579
|
|
|
) { |
580
|
|
|
$pasta = 'eventos'; //default |
581
|
|
|
if ($aliasEvento == 'CanCTe') { |
582
|
|
|
$pasta = 'canceladas'; |
583
|
|
|
$filename = "$chCTe-$aliasEvento-procEvento.xml"; |
584
|
|
|
} elseif ($aliasEvento == 'CCe') { |
585
|
|
|
$pasta = 'cartacorrecao'; |
586
|
|
|
$filename = "$chCTe-$aliasEvento-$nSeqEvento-procEvento.xml"; |
587
|
|
|
} |
588
|
|
|
$retorno = $this->zAddProtMsg('procEventoCTe', 'evento', $signedMsg, 'retEvento', $retorno); |
589
|
|
|
$this->zGravaFile('cte', $tpAmb, $filename, $retorno, $pasta); |
590
|
|
|
} |
591
|
|
|
} |
592
|
|
|
return (string) $retorno; |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
/** |
596
|
|
|
* zAddProtMsg |
597
|
|
|
* |
598
|
|
|
* @param string $tagproc |
599
|
|
|
* @param string $tagmsg |
600
|
|
|
* @param string $xmlmsg |
601
|
|
|
* @param string $tagretorno |
602
|
|
|
* @param string $xmlretorno |
603
|
|
|
* @return string |
604
|
|
|
*/ |
605
|
|
|
protected function zAddProtMsg($tagproc, $tagmsg, $xmlmsg, $tagretorno, $xmlretorno) |
606
|
|
|
{ |
607
|
|
|
$doc = new Dom(); |
608
|
|
|
$doc->loadXMLString($xmlmsg); |
609
|
|
|
$nodedoc = $doc->getNode($tagmsg, 0); |
610
|
|
|
$procver = $nodedoc->getAttribute("versao"); |
611
|
|
|
$procns = $nodedoc->getAttribute("xmlns"); |
612
|
|
|
|
613
|
|
|
$doc1 = new Dom(); |
614
|
|
|
$doc1->loadXMLString($xmlretorno); |
615
|
|
|
$nodedoc1 = $doc1->getNode($tagretorno, 0); |
616
|
|
|
|
617
|
|
|
$proc = new \DOMDocument('1.0', 'utf-8'); |
618
|
|
|
$proc->formatOutput = false; |
619
|
|
|
$proc->preserveWhiteSpace = false; |
620
|
|
|
//cria a tag nfeProc |
621
|
|
|
$procNode = $proc->createElement($tagproc); |
622
|
|
|
$proc->appendChild($procNode); |
623
|
|
|
//estabele o atributo de versão |
624
|
|
|
$procNodeAtt1 = $procNode->appendChild($proc->createAttribute('versao')); |
625
|
|
|
$procNodeAtt1->appendChild($proc->createTextNode($procver)); |
626
|
|
|
//estabelece o atributo xmlns |
627
|
|
|
$procNodeAtt2 = $procNode->appendChild($proc->createAttribute('xmlns')); |
628
|
|
|
$procNodeAtt2->appendChild($proc->createTextNode($procns)); |
629
|
|
|
//inclui a tag inutNFe |
630
|
|
|
$node = $proc->importNode($nodedoc, true); |
631
|
|
|
$procNode->appendChild($node); |
632
|
|
|
//inclui a tag retInutNFe |
633
|
|
|
$node = $proc->importNode($nodedoc1, true); |
634
|
|
|
$procNode->appendChild($node); |
635
|
|
|
//salva o xml como string em uma variável |
636
|
|
|
$procXML = $proc->saveXML(); |
637
|
|
|
//remove as informações indesejadas |
638
|
|
|
$procXML = Strings::clearProt($procXML); |
639
|
|
|
return $procXML; |
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
/** |
643
|
|
|
* zTpEv |
644
|
|
|
* @param string $tpEvento |
645
|
|
|
* @return array |
646
|
|
|
* @throws Exception\RuntimeException |
647
|
|
|
*/ |
648
|
|
|
private function zTpEv($tpEvento = '') |
649
|
|
|
{ |
650
|
|
|
//montagem dos dados da mensagem SOAP |
651
|
|
|
switch ($tpEvento) { |
652
|
|
|
case '110110': |
653
|
|
|
//CCe |
654
|
|
|
$aliasEvento = 'CCe'; |
655
|
|
|
$descEvento = 'Carta de Correcao'; |
656
|
|
|
break; |
657
|
|
|
case '110111': |
658
|
|
|
//cancelamento |
659
|
|
|
$aliasEvento = 'CancNFe'; |
660
|
|
|
$descEvento = 'Cancelamento'; |
661
|
|
|
break; |
662
|
|
|
case '110140': |
663
|
|
|
//EPEC |
664
|
|
|
//emissão em contingência EPEC |
665
|
|
|
$aliasEvento = 'EPEC'; |
666
|
|
|
$descEvento = 'EPEC'; |
667
|
|
|
break; |
668
|
|
|
case '111500': |
669
|
|
|
case '111501': |
670
|
|
|
//EPP |
671
|
|
|
//Pedido de prorrogação |
672
|
|
|
$aliasEvento = 'EPP'; |
673
|
|
|
$descEvento = 'Pedido de Prorrogacao'; |
674
|
|
|
break; |
675
|
|
|
case '111502': |
676
|
|
|
case '111503': |
677
|
|
|
//ECPP |
678
|
|
|
//Cancelamento do Pedido de prorrogação |
679
|
|
|
$aliasEvento = 'ECPP'; |
680
|
|
|
$descEvento = 'Cancelamento de Pedido de Prorrogacao'; |
681
|
|
|
break; |
682
|
|
|
case '210200': |
683
|
|
|
//Confirmacao da Operacao |
684
|
|
|
$aliasEvento = 'EvConfirma'; |
685
|
|
|
$descEvento = 'Confirmacao da Operacao'; |
686
|
|
|
break; |
687
|
|
|
case '210210': |
688
|
|
|
//Ciencia da Operacao |
689
|
|
|
$aliasEvento = 'EvCiencia'; |
690
|
|
|
$descEvento = 'Ciencia da Operacao'; |
691
|
|
|
break; |
692
|
|
|
case '210220': |
693
|
|
|
//Desconhecimento da Operacao |
694
|
|
|
$aliasEvento = 'EvDesconh'; |
695
|
|
|
$descEvento = 'Desconhecimento da Operacao'; |
696
|
|
|
break; |
697
|
|
|
case '210240': |
698
|
|
|
//Operacao não Realizada |
699
|
|
|
$aliasEvento = 'EvNaoRealizada'; |
700
|
|
|
$descEvento = 'Operacao nao Realizada'; |
701
|
|
|
break; |
702
|
|
|
default: |
703
|
|
|
$msg = "O código do tipo de evento informado não corresponde a " |
704
|
|
|
. "nenhum evento estabelecido."; |
705
|
|
|
throw new Exception\RuntimeException($msg); |
706
|
|
|
} |
707
|
|
|
return array('alias' => $aliasEvento, 'desc' => $descEvento); |
708
|
|
|
} |
709
|
|
|
|
710
|
|
|
private function zValidParamInut($xJust, $nSerie, $nIni, $nFin) |
711
|
|
|
{ |
712
|
|
|
$msg = ''; |
713
|
|
|
$nL = strlen($xJust); |
714
|
|
|
|
715
|
|
|
// Valida dos dados de entrada |
716
|
|
|
if ($nIni == '' || $nFin == '' || $xJust == '') { |
717
|
|
|
$msg = "Não foi passado algum dos parametos necessários" |
718
|
|
|
. "inicio=$nIni fim=$nFin justificativa=$xJust."; |
719
|
|
|
} elseif (strlen($nSerie) == 0 || strlen($nSerie) > 3) { |
720
|
|
|
$msg = "O campo serie está errado: $nSerie. Corrija e refaça o processo!!"; |
721
|
|
|
} elseif (strlen($nIni) < 1 || strlen($nIni) > 9) { |
722
|
|
|
$msg = "O campo numero inicial está errado: $nIni. Corrija e refaça o processo!!"; |
723
|
|
|
} elseif (strlen($nFin) < 1 || strlen($nFin) > 9) { |
724
|
|
|
$msg = "O campo numero final está errado: $nFin. Corrija e refaça o processo!!"; |
725
|
|
|
} elseif ($nL < 15 || $nL > 255) { |
726
|
|
|
$msg = "A justificativa tem que ter entre 15 e 255 caracteres, encontrado $nL. " |
727
|
|
|
. "Corrija e refaça o processo!!"; |
728
|
|
|
} |
729
|
|
|
|
730
|
|
|
if ($msg != '') { |
731
|
|
|
throw new Exception\InvalidArgumentException($msg); |
732
|
|
|
} |
733
|
|
|
} |
734
|
|
|
|
735
|
|
|
/** |
736
|
|
|
* validarXml |
737
|
|
|
* Valida qualquer xml do sistema NFe com seu xsd |
738
|
|
|
* NOTA: caso não exista um arquivo xsd apropriado retorna false |
739
|
|
|
* @param string $xml path ou conteudo do xml |
740
|
|
|
* @return boolean |
741
|
|
|
*/ |
742
|
|
|
public function validarXml($xml = '') |
743
|
|
|
{ |
744
|
|
|
$aResp = array(); |
745
|
|
|
$schem = IdentifyCTe::identificar($xml, $aResp); |
746
|
|
|
if ($schem == '') { |
747
|
|
|
return true; |
748
|
|
|
} |
749
|
|
|
$xsdFile = $aResp['Id'].'_v'.$aResp['versao'].'.xsd'; |
750
|
|
|
$xsdPath = NFEPHP_ROOT.DIRECTORY_SEPARATOR . |
751
|
|
|
'schemas' . |
752
|
|
|
DIRECTORY_SEPARATOR . |
753
|
|
|
$this->aConfig['schemasCTe'] . |
754
|
|
|
DIRECTORY_SEPARATOR . |
755
|
|
|
$xsdFile; |
756
|
|
|
if (! is_file($xsdPath)) { |
757
|
|
|
$this->erros[] = "O arquivo XSD $xsdFile não foi localizado."; |
758
|
|
|
return false; |
759
|
|
|
} |
760
|
|
|
if (! ValidXsd::validar($aResp['xml'], $xsdPath)) { |
761
|
|
|
$this->erros[] = ValidXsd::$errors; |
762
|
|
|
return false; |
763
|
|
|
} |
764
|
|
|
return true; |
765
|
|
|
} |
766
|
|
|
} |
767
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.