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