1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NFePHP\MDFe; |
4
|
|
|
|
5
|
|
|
use NFePHP\Common\Signer; |
6
|
|
|
use NFePHP\Common\Strings; |
7
|
|
|
use NFePHP\Common\UFList; |
8
|
|
|
use NFePHP\MDFe\Common\Tools as ToolsCommon; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Classe principal para a comunicação com a SEFAZ |
12
|
|
|
* |
13
|
|
|
* @author Cleiton Perin <cperin20 at gmail dot com> |
14
|
|
|
* @package nfephp-org/sped-mdfe |
15
|
|
|
* @copyright 2008-2019 NFePHP |
16
|
|
|
* @license http://www.gnu.org/licenses/lesser.html LGPL v3 |
17
|
|
|
* @link http://github.com/nfephp-org/sped-mdfe for the canonical source repository |
18
|
|
|
* @category Library |
19
|
|
|
*/ |
20
|
|
|
class Tools extends ToolsCommon |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Cleiton Perin |
25
|
|
|
* |
26
|
|
|
* @param array $aXml |
27
|
|
|
* @param string $idLote |
28
|
|
|
* @param int $indSinc flag to use synchronous communication |
29
|
|
|
* @return string |
30
|
|
|
* @throws Exception\InvalidArgumentException |
31
|
|
|
*/ |
32
|
|
|
public function sefazEnviaLote( |
33
|
|
|
$aXml, |
34
|
|
|
$idLote = '', |
35
|
|
|
$indSinc = 0 |
36
|
|
|
) { |
37
|
|
|
if (!is_array($aXml)) { |
38
|
|
|
throw new \InvalidArgumentException('Os XML das MDFe devem ser passados em um array.'); |
39
|
|
|
} |
40
|
|
|
if ($indSinc == 1 && count($aXml) > 1) { |
41
|
|
|
throw new \InvalidArgumentException('Envio sincrono deve ser usado para enviar ' |
42
|
|
|
. 'uma UNICA mdfe por vez. Você está tentando enviar varias.'); |
43
|
|
|
} |
44
|
|
|
$servico = 'MDFeRecepcao'; |
45
|
|
|
if ($indSinc == 1) { |
46
|
|
|
$servico = 'MDFeRecepcaoSinc'; |
47
|
|
|
} |
48
|
|
|
$sxml = implode("", $aXml); |
49
|
|
|
$sxml = preg_replace("/<\?xml.*?\?>/", "", $sxml); |
50
|
|
|
$this->servico( |
51
|
|
|
$servico, |
52
|
|
|
$this->config->siglaUF, |
53
|
|
|
$this->tpAmb |
54
|
|
|
); |
55
|
|
|
$request = "<enviMDFe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
56
|
|
|
. "<idLote>$idLote</idLote>" |
57
|
|
|
. "$sxml" |
58
|
|
|
. "</enviMDFe>"; |
59
|
|
|
$this->isValid($this->urlVersion, $request, 'enviMDFe'); |
60
|
|
|
$this->lastRequest = $request; |
61
|
|
|
if ($indSinc == 1) { |
62
|
|
|
$request = base64_encode(gzencode($sxml)); |
63
|
|
|
} |
64
|
|
|
//montagem dos dados da mensagem SOAP |
65
|
|
|
$parameters = ['mdfeDadosMsg' => $request]; |
66
|
|
|
$body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>"; |
67
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
68
|
|
|
return $this->lastResponse; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @author Cleiton Perin |
73
|
|
|
* |
74
|
|
|
* @param string $recibo |
75
|
|
|
* @param string $tpAmb |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function sefazConsultaRecibo($recibo, $tpAmb = null) |
79
|
|
|
{ |
80
|
|
|
if (empty($tpAmb)) { |
81
|
|
|
$tpAmb = $this->tpAmb; |
82
|
|
|
} |
83
|
|
|
//carrega serviço |
84
|
|
|
$servico = 'MDFeRetRecepcao'; |
85
|
|
|
$this->servico( |
86
|
|
|
$servico, |
87
|
|
|
$this->config->siglaUF, |
88
|
|
|
$tpAmb |
89
|
|
|
); |
90
|
|
|
$request = "<consReciMDFe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
91
|
|
|
. "<tpAmb>$tpAmb</tpAmb>" |
92
|
|
|
. "<nRec>$recibo</nRec>" |
93
|
|
|
. "</consReciMDFe>"; |
94
|
|
|
$this->isValid($this->urlVersion, $request, 'consReciMDFe'); |
95
|
|
|
//montagem dos dados da mensagem SOAP |
96
|
|
|
$this->lastRequest = $request; |
97
|
|
|
$parameters = ['mdfeDadosMsg' => $request]; |
98
|
|
|
$body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>"; |
99
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
100
|
|
|
return $this->lastResponse; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @author Cleiton Perin |
105
|
|
|
* Consulta o status da MDFe pela chave de 44 digitos |
106
|
|
|
* |
107
|
|
|
* @param string $chave |
108
|
|
|
* @param string $tpAmb |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function sefazConsultaChave($chave, $tpAmb = null) |
112
|
|
|
{ |
113
|
|
|
if (empty($tpAmb)) { |
114
|
|
|
$tpAmb = $this->tpAmb; |
115
|
|
|
} |
116
|
|
|
//carrega serviço |
117
|
|
|
$servico = 'MDFeConsulta'; |
118
|
|
|
$this->servico( |
119
|
|
|
$servico, |
120
|
|
|
$this->config->siglaUF, |
121
|
|
|
$tpAmb |
122
|
|
|
); |
123
|
|
|
$request = "<consSitMDFe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
124
|
|
|
. "<tpAmb>$tpAmb</tpAmb>" |
125
|
|
|
. "<xServ>CONSULTAR</xServ>" |
126
|
|
|
. "<chMDFe>$chave</chMDFe>" |
127
|
|
|
. "</consSitMDFe>"; |
128
|
|
|
$this->isValid($this->urlVersion, $request, 'consSitMDFe'); |
129
|
|
|
$this->lastRequest = $request; |
130
|
|
|
$parameters = ['mdfeDadosMsg' => $request]; |
131
|
|
|
$body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>"; |
132
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
133
|
|
|
return $this->lastResponse; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @author Cleiton Perin |
138
|
|
|
* |
139
|
|
|
* @param string $uf sigla da unidade da Federação |
140
|
|
|
* @param string $tpAmb tipo de ambiente 1-produção e 2-homologação |
141
|
|
|
* @return mixed string XML do retorno do webservice, ou false se ocorreu algum erro |
142
|
|
|
*/ |
143
|
|
|
public function sefazStatus($uf = '', $tpAmb = null) |
144
|
|
|
{ |
145
|
|
|
if (empty($tpAmb)) { |
146
|
|
|
$tpAmb = $this->tpAmb; |
147
|
|
|
} |
148
|
|
|
if (empty($uf)) { |
149
|
|
|
$uf = $this->config->siglaUF; |
150
|
|
|
} |
151
|
|
|
//carrega serviço |
152
|
|
|
$servico = 'MDFeStatusServico'; |
153
|
|
|
$this->servico( |
154
|
|
|
$servico, |
155
|
|
|
$uf, |
156
|
|
|
$tpAmb |
157
|
|
|
); |
158
|
|
|
$request = "<consStatServMDFe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
159
|
|
|
. "<tpAmb>$tpAmb</tpAmb>" |
160
|
|
|
. "<xServ>STATUS</xServ></consStatServMDFe>"; |
161
|
|
|
$this->isValid($this->urlVersion, $request, 'consStatServMDFe'); |
162
|
|
|
$this->lastRequest = $request; |
163
|
|
|
$parameters = ['mdfeDadosMsg' => $request]; |
164
|
|
|
$body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>"; |
165
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
166
|
|
|
return $this->lastResponse; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @author Cleiton Perin |
171
|
|
|
* |
172
|
|
|
* @param string $chave |
173
|
|
|
* @param string $xJust |
174
|
|
|
* @param string $nProt |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
public function sefazCancela($chave, $xJust, $nProt) |
178
|
|
|
{ |
179
|
|
|
$xJust = Strings::replaceSpecialsChars( |
180
|
|
|
substr(trim($xJust), 0, 255) |
181
|
|
|
); |
182
|
|
|
$tpEvento = 110111; |
183
|
|
|
$nSeqEvento = 1; |
184
|
|
|
$tagAdic = "<evCancMDFe>" |
185
|
|
|
. "<descEvento>Cancelamento</descEvento>" |
186
|
|
|
. "<nProt>$nProt</nProt>" |
187
|
|
|
. "<xJust>$xJust</xJust>" |
188
|
|
|
. "</evCancMDFe>"; |
189
|
|
|
return $this->sefazEvento( |
190
|
|
|
$this->config->siglaUF, |
191
|
|
|
$chave, |
192
|
|
|
$tpEvento, |
193
|
|
|
$nSeqEvento, |
194
|
|
|
$tagAdic |
195
|
|
|
); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @author Cleiton Perin |
200
|
|
|
* |
201
|
|
|
* @param string $chave |
202
|
|
|
* @param string $nProt |
203
|
|
|
* @param string $cUF |
204
|
|
|
* @param string $cMun |
205
|
|
|
* @param string $dtEnc |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
|
|
public function sefazEncerra( |
209
|
|
|
$chave = '', |
210
|
|
|
$nProt = '', |
211
|
|
|
$cUF = '', |
212
|
|
|
$cMun = '', |
213
|
|
|
$dtEnc = '' |
214
|
|
|
) { |
215
|
|
|
$tpEvento = 110112; |
216
|
|
|
$nSeqEvento = 1; |
217
|
|
|
if ($dtEnc == '') { |
218
|
|
|
$dtEnc = date('Y-m-d'); |
219
|
|
|
} |
220
|
|
|
$tagAdic = "<evEncMDFe>" |
221
|
|
|
. "<descEvento>Encerramento</descEvento>" |
222
|
|
|
. "<nProt>$nProt</nProt>" |
223
|
|
|
. "<dtEnc>$dtEnc</dtEnc>" |
224
|
|
|
. "<cUF>$cUF</cUF>" |
225
|
|
|
. "<cMun>$cMun</cMun>" |
226
|
|
|
. "</evEncMDFe>"; |
227
|
|
|
return $this->sefazEvento( |
228
|
|
|
$this->config->siglaUF, |
229
|
|
|
$chave, |
230
|
|
|
$tpEvento, |
231
|
|
|
$nSeqEvento, |
232
|
|
|
$tagAdic |
233
|
|
|
); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @author Cleiton Perin |
238
|
|
|
* |
239
|
|
|
* @param string $chave |
240
|
|
|
* @param string $nSeqEvento |
241
|
|
|
* @param string $xNome |
242
|
|
|
* @param string $cpf |
243
|
|
|
* @return string |
244
|
|
|
*/ |
245
|
|
|
public function sefazIncluiCondutor( |
246
|
|
|
$chave = '', |
247
|
|
|
$nSeqEvento = '1', |
248
|
|
|
$xNome = '', |
249
|
|
|
$cpf = '' |
250
|
|
|
) { |
251
|
|
|
$tpEvento = 110114; |
252
|
|
|
$tagAdic = "<evIncCondutorMDFe>" |
253
|
|
|
. "<descEvento>Inclusao Condutor</descEvento>" |
254
|
|
|
. "<condutor>" |
255
|
|
|
. "<xNome>$xNome</xNome>" |
256
|
|
|
. "<CPF>$cpf</CPF>" |
257
|
|
|
. "</condutor>" |
258
|
|
|
. "</evIncCondutorMDFe>"; |
259
|
|
|
|
260
|
|
|
return $this->sefazEvento( |
261
|
|
|
$this->config->siglaUF, |
262
|
|
|
$chave, |
263
|
|
|
$tpEvento, |
264
|
|
|
$nSeqEvento, |
265
|
|
|
$tagAdic |
266
|
|
|
); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @author João Gabriel |
271
|
|
|
* |
272
|
|
|
* @param string $chave |
273
|
|
|
* @param string $nProt |
274
|
|
|
* @param string $cMunCarrega |
275
|
|
|
* @param string $xMunCarrega |
276
|
|
|
* @param string $cMunDescarga |
277
|
|
|
* @param string $xMunDescarga |
278
|
|
|
* @param string $chNFe |
279
|
|
|
* @param int $nSeqEvento |
280
|
|
|
* @return string |
281
|
|
|
*/ |
282
|
|
|
public function sefazIncluiDFe( |
283
|
|
|
$chave = '', |
284
|
|
|
$nProt = '', |
285
|
|
|
$cMunCarrega = '', |
286
|
|
|
$xMunCarrega = '', |
287
|
|
|
$cMunDescarga = '', |
288
|
|
|
$xMunDescarga = '', |
289
|
|
|
$chNFe = '', |
290
|
|
|
$nSeqEvento = '1' |
291
|
|
|
) { |
292
|
|
|
$tpEvento = 110115; |
293
|
|
|
$tagAdic = "<evIncDFeMDFe>" |
294
|
|
|
. "<descEvento>Inclusao DF-e</descEvento>" |
295
|
|
|
. "<nProt>$nProt</nProt>" |
296
|
|
|
. "<cMunCarrega>$cMunCarrega</cMunCarrega>" |
297
|
|
|
. "<xMunCarrega>$xMunCarrega</xMunCarrega>" |
298
|
|
|
. "<infDoc>" |
299
|
|
|
. "<cMunDescarga>$cMunDescarga</cMunDescarga>" |
300
|
|
|
. "<xMunDescarga>$xMunDescarga</xMunDescarga>" |
301
|
|
|
. "<chNFe>$chNFe</chNFe>" |
302
|
|
|
. "</infDoc>" |
303
|
|
|
. "</evIncDFeMDFe>"; |
304
|
|
|
|
305
|
|
|
return $this->sefazEvento( |
306
|
|
|
$this->config->siglaUF, |
307
|
|
|
$chave, |
308
|
|
|
$tpEvento, |
309
|
|
|
$nSeqEvento, |
310
|
|
|
$tagAdic |
311
|
|
|
); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @author Cleiton Perin |
316
|
|
|
* |
317
|
|
|
* @return string |
318
|
|
|
*/ |
319
|
|
|
public function sefazConsultaNaoEncerrados() |
320
|
|
|
{ |
321
|
|
|
//carrega serviço |
322
|
|
|
$servico = 'MDFeConsNaoEnc'; |
323
|
|
|
$this->servico( |
324
|
|
|
$servico, |
325
|
|
|
$this->config->siglaUF, |
326
|
|
|
$this->tpAmb |
327
|
|
|
); |
328
|
|
|
$cnpj = $this->config->cnpj; |
329
|
|
|
$request = "<consMDFeNaoEnc xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
330
|
|
|
. "<tpAmb>$this->tpAmb</tpAmb>" |
331
|
|
|
. "<xServ>CONSULTAR NÃO ENCERRADOS</xServ>" |
332
|
|
|
. "<CNPJ>$cnpj</CNPJ>" |
333
|
|
|
. "</consMDFeNaoEnc>"; |
334
|
|
|
$this->lastRequest = $request; |
335
|
|
|
$parameters = ['mdfeDadosMsg' => $request]; |
336
|
|
|
$body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>"; |
337
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
338
|
|
|
return $this->lastResponse; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* @author Cleiton Perin |
343
|
|
|
* |
344
|
|
|
* @param string $uf |
345
|
|
|
* @param string $chave |
346
|
|
|
* @param string $cOrgao |
|
|
|
|
347
|
|
|
* @param string $tpEvento |
348
|
|
|
* @param string $nSeqEvento |
349
|
|
|
* @param string $tagAdic |
350
|
|
|
* @return string |
351
|
|
|
*/ |
352
|
|
|
protected function sefazEvento( |
353
|
|
|
$uf, |
354
|
|
|
$chave, |
355
|
|
|
$tpEvento, |
356
|
|
|
$nSeqEvento = 1, |
357
|
|
|
$tagAdic = '' |
358
|
|
|
) { |
359
|
|
|
//carrega serviço |
360
|
|
|
$servico = 'MDFeRecepcaoEvento'; |
361
|
|
|
$this->servico( |
362
|
|
|
$servico, |
363
|
|
|
$uf, |
364
|
|
|
$this->tpAmb |
365
|
|
|
); |
366
|
|
|
$cnpj = $this->config->cnpj; |
367
|
|
|
$sigla = strlen($cnpj) == 11 ? 'CPF' : 'CNPJ'; |
368
|
|
|
$dt = new \DateTime(); |
369
|
|
|
$dhEvento = $dt->format('Y-m-d\TH:i:sP'); |
370
|
|
|
$sSeqEvento = str_pad($nSeqEvento, 2, "0", STR_PAD_LEFT); |
371
|
|
|
$eventId = "ID" . $tpEvento . $chave . $sSeqEvento; |
372
|
|
|
$cOrgao = UFList::getCodeByUF($uf); |
373
|
|
|
$request = "<eventoMDFe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
374
|
|
|
. "<infEvento Id=\"$eventId\">" |
375
|
|
|
. "<cOrgao>$cOrgao</cOrgao>" |
376
|
|
|
. "<tpAmb>$this->tpAmb</tpAmb>" |
377
|
|
|
. "<$sigla>$cnpj</$sigla>" |
378
|
|
|
. "<chMDFe>$chave</chMDFe>" |
379
|
|
|
. "<dhEvento>$dhEvento</dhEvento>" |
380
|
|
|
. "<tpEvento>$tpEvento</tpEvento>" |
381
|
|
|
. "<nSeqEvento>$nSeqEvento</nSeqEvento>" |
382
|
|
|
. "<detEvento versaoEvento=\"$this->urlVersion\">" |
383
|
|
|
. "$tagAdic" |
384
|
|
|
. "</detEvento>" |
385
|
|
|
. "</infEvento>" |
386
|
|
|
. "</eventoMDFe>"; |
387
|
|
|
//assinatura dos dados |
388
|
|
|
$request = Signer::sign( |
389
|
|
|
$this->certificate, |
390
|
|
|
$request, |
391
|
|
|
'infEvento', |
392
|
|
|
'Id', |
393
|
|
|
$this->algorithm, |
394
|
|
|
$this->canonical |
395
|
|
|
); |
396
|
|
|
$request = Strings::clearXmlString($request, true); |
397
|
|
|
$this->isValid($this->urlVersion, $request, 'eventoMDFe'); |
398
|
|
|
$this->lastRequest = $request; |
399
|
|
|
$parameters = ['mdfeDadosMsg' => $request]; |
400
|
|
|
$body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>"; |
401
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
402
|
|
|
return $this->lastResponse; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* Service for the distribution of summary information and |
407
|
|
|
* electronic tax documents of interest to an actor. |
408
|
|
|
* @param integer $ultNSU last NSU number recived |
409
|
|
|
* @param integer $numNSU NSU number you wish to consult |
410
|
|
|
* @return string |
411
|
|
|
*/ |
412
|
|
|
public function sefazDistDFe( |
413
|
|
|
$ultNSU = 0, |
414
|
|
|
$numNSU = 0 |
415
|
|
|
) { |
416
|
|
|
//carrega serviço |
417
|
|
|
$servico = 'MDFeDistribuicaoDFe'; |
418
|
|
|
$this->servico( |
419
|
|
|
$servico, |
420
|
|
|
$this->config->siglaUF, |
421
|
|
|
$this->tpAmb |
422
|
|
|
); |
423
|
|
|
$ultNSU = str_pad($ultNSU, 15, '0', STR_PAD_LEFT); |
424
|
|
|
$tagNSU = "<distNSU><ultNSU>$ultNSU</ultNSU></distNSU>"; |
425
|
|
|
if ($numNSU != 0) { |
426
|
|
|
$numNSU = str_pad($numNSU, 15, '0', STR_PAD_LEFT); |
427
|
|
|
$tagNSU = "<consNSU><NSU>$numNSU</NSU></consNSU>"; |
428
|
|
|
} |
429
|
|
|
//monta a consulta |
430
|
|
|
$request = "<distDFeInt xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
431
|
|
|
. "<tpAmb>".$this->tpAmb."</tpAmb>" |
432
|
|
|
. ((strlen($this->config->cnpj)==14) ? |
433
|
|
|
"<CNPJ>".$this->config->cnpj."</CNPJ>" : |
434
|
|
|
"<CPF>".$this->config->cnpj."</CPF>" |
435
|
|
|
) |
436
|
|
|
. $tagNSU."</distDFeInt>"; |
437
|
|
|
//valida o xml da requisição |
438
|
|
|
$this->isValid($this->urlVersion, $request, 'distDFeInt'); |
439
|
|
|
$this->lastRequest = $request; |
440
|
|
|
//montagem dos dados da mensagem SOAP |
441
|
|
|
$body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>"; |
442
|
|
|
$parameters = ['mdfeDadosMsg' => $request]; |
443
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
444
|
|
|
return $this->lastResponse; |
445
|
|
|
} |
446
|
|
|
} |
447
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.