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
|
|
|
|
38
|
|
|
|
39
|
|
|
if (!is_array($aXml)) { |
40
|
|
|
throw new \InvalidArgumentException('Os XML das MDFe devem ser passados em um array.'); |
41
|
|
|
} |
42
|
|
|
if ($indSinc == 1 && count($aXml) > 1) { |
43
|
|
|
throw new \InvalidArgumentException('Envio sincrono deve ser usado para enviar ' |
44
|
|
|
. 'uma UNICA mdfe por vez. Você está tentando enviar varias.'); |
45
|
|
|
} |
46
|
|
|
$servico = 'MDFeRecepcao'; |
47
|
|
|
if ($indSinc == 1) { |
48
|
|
|
$servico = 'MDFeRecepcaoSinc'; |
49
|
|
|
} |
50
|
|
|
$sxml = implode("", $aXml); |
51
|
|
|
$sxml = preg_replace("/<\?xml.*?\?>/", "", $sxml); |
52
|
|
|
$this->servico( |
53
|
|
|
$servico, |
54
|
|
|
$this->config->siglaUF, |
55
|
|
|
$this->tpAmb |
56
|
|
|
); |
57
|
|
|
$request = "<enviMDFe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
58
|
|
|
. "<idLote>$idLote</idLote>" |
59
|
|
|
. "$sxml" |
60
|
|
|
. "</enviMDFe>"; |
61
|
|
|
$this->isValid($this->urlVersion, $request, 'enviMDFe'); |
62
|
|
|
$this->lastRequest = $request; |
63
|
|
|
if ($indSinc == 1) { |
64
|
|
|
$request = base64_encode(gzencode($sxml)); |
65
|
|
|
} |
66
|
|
|
//montagem dos dados da mensagem SOAP |
67
|
|
|
$parameters = ['mdfeDadosMsg' => $request]; |
68
|
|
|
$body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>"; |
69
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
70
|
|
|
return $this->lastResponse; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @author Cleiton Perin |
75
|
|
|
* |
76
|
|
|
* @param string $recibo |
77
|
|
|
* @param string $tpAmb |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function sefazConsultaRecibo($recibo, $tpAmb = null) |
81
|
|
|
{ |
82
|
|
|
if (empty($tpAmb)) { |
83
|
|
|
$tpAmb = $this->tpAmb; |
84
|
|
|
} |
85
|
|
|
//carrega serviço |
86
|
|
|
$servico = 'MDFeRetRecepcao'; |
87
|
|
|
$this->servico( |
88
|
|
|
$servico, |
89
|
|
|
$this->config->siglaUF, |
90
|
|
|
$tpAmb |
91
|
|
|
); |
92
|
|
|
$request = "<consReciMDFe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
93
|
|
|
. "<tpAmb>$tpAmb</tpAmb>" |
94
|
|
|
. "<nRec>$recibo</nRec>" |
95
|
|
|
. "</consReciMDFe>"; |
96
|
|
|
$this->isValid($this->urlVersion, $request, 'consReciMDFe'); |
97
|
|
|
//montagem dos dados da mensagem SOAP |
98
|
|
|
$this->lastRequest = $request; |
99
|
|
|
$parameters = ['mdfeDadosMsg' => $request]; |
100
|
|
|
$body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>"; |
101
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
102
|
|
|
return $this->lastResponse; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @author Cleiton Perin |
107
|
|
|
* Consulta o status da MDFe pela chave de 44 digitos |
108
|
|
|
* |
109
|
|
|
* @param string $chave |
110
|
|
|
* @param string $tpAmb |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function sefazConsultaChave($chave, $tpAmb = null) |
114
|
|
|
{ |
115
|
|
|
if (empty($tpAmb)) { |
116
|
|
|
$tpAmb = $this->tpAmb; |
117
|
|
|
} |
118
|
|
|
//carrega serviço |
119
|
|
|
$servico = 'MDFeConsulta'; |
120
|
|
|
$this->servico( |
121
|
|
|
$servico, |
122
|
|
|
$this->config->siglaUF, |
123
|
|
|
$tpAmb |
124
|
|
|
); |
125
|
|
|
$request = "<consSitMDFe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
126
|
|
|
. "<tpAmb>$tpAmb</tpAmb>" |
127
|
|
|
. "<xServ>CONSULTAR</xServ>" |
128
|
|
|
. "<chMDFe>$chave</chMDFe>" |
129
|
|
|
. "</consSitMDFe>"; |
130
|
|
|
$this->isValid($this->urlVersion, $request, 'consSitMDFe'); |
131
|
|
|
$this->lastRequest = $request; |
132
|
|
|
$parameters = ['mdfeDadosMsg' => $request]; |
133
|
|
|
$body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>"; |
134
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
135
|
|
|
return $this->lastResponse; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @author Cleiton Perin |
140
|
|
|
* |
141
|
|
|
* @param string $uf sigla da unidade da Federação |
142
|
|
|
* @param string $tpAmb tipo de ambiente 1-produção e 2-homologação |
143
|
|
|
* @return mixed string XML do retorno do webservice, ou false se ocorreu algum erro |
144
|
|
|
*/ |
145
|
|
|
public function sefazStatus($uf = '', $tpAmb = null) |
146
|
|
|
{ |
147
|
|
|
if (empty($tpAmb)) { |
148
|
|
|
$tpAmb = $this->tpAmb; |
149
|
|
|
} |
150
|
|
|
if (empty($uf)) { |
151
|
|
|
$uf = $this->config->siglaUF; |
152
|
|
|
} |
153
|
|
|
//carrega serviço |
154
|
|
|
$servico = 'MDFeStatusServico'; |
155
|
|
|
$this->servico( |
156
|
|
|
$servico, |
157
|
|
|
$uf, |
158
|
|
|
$tpAmb |
159
|
|
|
); |
160
|
|
|
$request = "<consStatServMDFe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
161
|
|
|
. "<tpAmb>$tpAmb</tpAmb>" |
162
|
|
|
. "<xServ>STATUS</xServ></consStatServMDFe>"; |
163
|
|
|
$this->isValid($this->urlVersion, $request, 'consStatServMDFe'); |
164
|
|
|
$this->lastRequest = $request; |
165
|
|
|
$parameters = ['mdfeDadosMsg' => $request]; |
166
|
|
|
$body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>"; |
167
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
168
|
|
|
return $this->lastResponse; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @author Cleiton Perin |
173
|
|
|
* |
174
|
|
|
* @param string $chave |
175
|
|
|
* @param string $xJust |
176
|
|
|
* @param string $nProt |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
public function sefazCancela($chave, $xJust, $nProt) |
180
|
|
|
{ |
181
|
|
|
$xJust = Strings::replaceSpecialsChars( |
182
|
|
|
substr(trim($xJust), 0, 255) |
183
|
|
|
); |
184
|
|
|
$tpEvento = 110111; |
185
|
|
|
$nSeqEvento = 1; |
186
|
|
|
$tagAdic = "<evCancMDFe>" |
187
|
|
|
. "<descEvento>Cancelamento</descEvento>" |
188
|
|
|
. "<nProt>$nProt</nProt>" |
189
|
|
|
. "<xJust>$xJust</xJust>" |
190
|
|
|
. "</evCancMDFe>"; |
191
|
|
|
return $this->sefazEvento( |
192
|
|
|
$this->config->siglaUF, |
193
|
|
|
$chave, |
194
|
|
|
$tpEvento, |
195
|
|
|
$nSeqEvento, |
196
|
|
|
$tagAdic |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @author Cleiton Perin |
202
|
|
|
* |
203
|
|
|
* @param string $chave |
204
|
|
|
* @param string $nProt |
205
|
|
|
* @param string $cUF |
206
|
|
|
* @param string $cMun |
207
|
|
|
* @param string $dtEnc |
208
|
|
|
* @return string |
209
|
|
|
*/ |
210
|
|
|
public function sefazEncerra( |
211
|
|
|
$chave = '', |
212
|
|
|
$nProt = '', |
213
|
|
|
$cUF = '', |
214
|
|
|
$cMun = '', |
215
|
|
|
$dtEnc = '' |
216
|
|
|
) { |
217
|
|
|
|
218
|
|
|
|
219
|
|
|
|
220
|
|
|
$tpEvento = 110112; |
221
|
|
|
$nSeqEvento = 1; |
222
|
|
|
if ($dtEnc == '') { |
223
|
|
|
$dtEnc = date('Y-m-d'); |
224
|
|
|
} |
225
|
|
|
$tagAdic = "<evEncMDFe>" |
226
|
|
|
. "<descEvento>Encerramento</descEvento>" |
227
|
|
|
. "<nProt>$nProt</nProt>" |
228
|
|
|
. "<dtEnc>$dtEnc</dtEnc>" |
229
|
|
|
. "<cUF>$cUF</cUF>" |
230
|
|
|
. "<cMun>$cMun</cMun>" |
231
|
|
|
. "</evEncMDFe>"; |
232
|
|
|
return $this->sefazEvento( |
233
|
|
|
$this->config->siglaUF, |
234
|
|
|
$chave, |
235
|
|
|
$tpEvento, |
236
|
|
|
$nSeqEvento, |
237
|
|
|
$tagAdic |
238
|
|
|
); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @author Cleiton Perin |
243
|
|
|
* |
244
|
|
|
* @param string $chave |
245
|
|
|
* @param string $nSeqEvento |
246
|
|
|
* @param string $xNome |
247
|
|
|
* @param string $cpf |
248
|
|
|
* @return string |
249
|
|
|
*/ |
250
|
|
|
public function sefazIncluiCondutor( |
251
|
|
|
$chave = '', |
252
|
|
|
$nSeqEvento = '1', |
253
|
|
|
$xNome = '', |
254
|
|
|
$cpf = '' |
255
|
|
|
) { |
256
|
|
|
|
257
|
|
|
|
258
|
|
|
|
259
|
|
|
$tpEvento = 110114; |
260
|
|
|
$tagAdic = "<evIncCondutorMDFe>" |
261
|
|
|
. "<descEvento>Inclusao Condutor</descEvento>" |
262
|
|
|
. "<condutor>" |
263
|
|
|
. "<xNome>$xNome</xNome>" |
264
|
|
|
. "<CPF>$cpf</CPF>" |
265
|
|
|
. "</condutor>" |
266
|
|
|
. "</evIncCondutorMDFe>"; |
267
|
|
|
|
268
|
|
|
return $this->sefazEvento( |
269
|
|
|
$this->config->siglaUF, |
270
|
|
|
$chave, |
271
|
|
|
$tpEvento, |
272
|
|
|
$nSeqEvento, |
273
|
|
|
$tagAdic |
274
|
|
|
); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @author Cleiton Perin |
279
|
|
|
* |
280
|
|
|
* @return string |
281
|
|
|
*/ |
282
|
|
|
public function sefazConsultaNaoEncerrados() |
283
|
|
|
{ |
284
|
|
|
//carrega serviço |
285
|
|
|
$servico = 'MDFeConsNaoEnc'; |
286
|
|
|
$this->servico( |
287
|
|
|
$servico, |
288
|
|
|
$this->config->siglaUF, |
289
|
|
|
$this->tpAmb |
290
|
|
|
); |
291
|
|
|
$cnpj = $this->config->cnpj; |
292
|
|
|
$request = "<consMDFeNaoEnc xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
293
|
|
|
. "<tpAmb>$this->tpAmb</tpAmb>" |
294
|
|
|
. "<xServ>CONSULTAR NÃO ENCERRADOS</xServ>" |
295
|
|
|
. "<CNPJ>$cnpj</CNPJ>" |
296
|
|
|
. "</consMDFeNaoEnc>"; |
297
|
|
|
$this->lastRequest = $request; |
298
|
|
|
$parameters = ['mdfeDadosMsg' => $request]; |
299
|
|
|
$body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>"; |
300
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
301
|
|
|
return $this->lastResponse; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @author Cleiton Perin |
306
|
|
|
* |
307
|
|
|
* @param string $uf |
308
|
|
|
* @param string $chave |
309
|
|
|
* @param string $cOrgao |
|
|
|
|
310
|
|
|
* @param string $tpEvento |
311
|
|
|
* @param string $nSeqEvento |
312
|
|
|
* @param string $tagAdic |
313
|
|
|
* @return string |
314
|
|
|
*/ |
315
|
|
|
protected function sefazEvento( |
316
|
|
|
$uf, |
317
|
|
|
$chave, |
318
|
|
|
$tpEvento, |
319
|
|
|
$nSeqEvento = 1, |
320
|
|
|
$tagAdic = '' |
321
|
|
|
) { |
322
|
|
|
|
323
|
|
|
|
324
|
|
|
|
325
|
|
|
//carrega serviço |
326
|
|
|
$servico = 'MDFeRecepcaoEvento'; |
327
|
|
|
$this->servico( |
328
|
|
|
$servico, |
329
|
|
|
$uf, |
330
|
|
|
$this->tpAmb |
331
|
|
|
); |
332
|
|
|
$cnpj = $this->config->cnpj; |
333
|
|
|
$sigla = strlen($cnpj) == 11 ? 'CPF' : 'CNPJ'; |
334
|
|
|
$dt = new \DateTime(); |
335
|
|
|
$dhEvento = $dt->format('Y-m-d\TH:i:sP'); |
336
|
|
|
$sSeqEvento = str_pad($nSeqEvento, 2, "0", STR_PAD_LEFT); |
337
|
|
|
$eventId = "ID" . $tpEvento . $chave . $sSeqEvento; |
338
|
|
|
$cOrgao = UFList::getCodeByUF($uf); |
339
|
|
|
$request = "<eventoMDFe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
340
|
|
|
. "<infEvento Id=\"$eventId\">" |
341
|
|
|
. "<cOrgao>$cOrgao</cOrgao>" |
342
|
|
|
. "<tpAmb>$this->tpAmb</tpAmb>" |
343
|
|
|
. "<$sigla>$cnpj</$sigla>" |
344
|
|
|
. "<chMDFe>$chave</chMDFe>" |
345
|
|
|
. "<dhEvento>$dhEvento</dhEvento>" |
346
|
|
|
. "<tpEvento>$tpEvento</tpEvento>" |
347
|
|
|
. "<nSeqEvento>$nSeqEvento</nSeqEvento>" |
348
|
|
|
. "<detEvento versaoEvento=\"$this->urlVersion\">" |
349
|
|
|
. "$tagAdic" |
350
|
|
|
. "</detEvento>" |
351
|
|
|
. "</infEvento>" |
352
|
|
|
. "</eventoMDFe>"; |
353
|
|
|
//assinatura dos dados |
354
|
|
|
$request = Signer::sign( |
355
|
|
|
$this->certificate, |
356
|
|
|
$request, |
357
|
|
|
'infEvento', |
358
|
|
|
'Id', |
359
|
|
|
$this->algorithm, |
360
|
|
|
$this->canonical |
361
|
|
|
); |
362
|
|
|
$request = Strings::clearXmlString($request, true); |
363
|
|
|
$this->isValid($this->urlVersion, $request, 'eventoMDFe'); |
364
|
|
|
$this->lastRequest = $request; |
365
|
|
|
$parameters = ['mdfeDadosMsg' => $request]; |
366
|
|
|
$body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>"; |
367
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
368
|
|
|
return $this->lastResponse; |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
|
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.