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
|
3 |
|
* @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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
|
|
256
|
|
|
return $this->sefazEvento( |
257
|
|
|
$this->config->siglaUF, |
258
|
|
|
$chave, |
259
|
|
|
$tpEvento, |
260
|
|
|
$nSeqEvento, |
261
|
|
|
$tagAdic |
262
|
|
|
); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @author Cleiton Perin |
267
|
|
|
* |
268
|
|
|
* @return string |
269
|
|
|
*/ |
270
|
|
|
public function sefazConsultaNaoEncerrados() |
271
|
|
|
{ |
272
|
|
|
//carrega serviço |
273
|
|
|
$servico = 'MDFeConsNaoEnc'; |
274
|
|
|
$this->servico( |
275
|
|
|
$servico, |
276
|
|
|
$this->config->siglaUF, |
277
|
|
|
$this->tpAmb |
278
|
|
|
); |
279
|
|
|
$cnpj = $this->config->cnpj; |
280
|
|
|
$request = "<consMDFeNaoEnc xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">" |
281
|
|
|
. "<tpAmb>$this->tpAmb</tpAmb>" |
282
|
|
|
. "<xServ>CONSULTAR NÃO ENCERRADOS</xServ>" |
283
|
|
|
. "<CNPJ>$cnpj</CNPJ>" |
284
|
|
|
. "</consMDFeNaoEnc>"; |
285
|
|
|
$this->lastRequest = $request; |
286
|
|
|
$parameters = ['mdfeDadosMsg' => $request]; |
287
|
|
|
$body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>"; |
288
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
289
|
|
|
return $this->lastResponse; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @author Cleiton Perin |
294
|
|
|
* |
295
|
|
|
* @param string $uf |
296
|
|
|
* @param string $chave |
297
|
|
|
* @param string $cOrgao |
|
|
|
|
298
|
|
|
* @param string $tpEvento |
299
|
|
|
* @param string $nSeqEvento |
300
|
|
|
* @param string $tagAdic |
301
|
|
|
* @return string |
302
|
|
|
*/ |
303
|
|
|
protected function sefazEvento( |
304
|
|
|
$uf, |
305
|
|
|
$chave, |
306
|
|
|
$tpEvento, |
307
|
|
|
$nSeqEvento = 1, |
308
|
|
|
$tagAdic = '' |
309
|
|
|
) { |
310
|
|
|
|
311
|
|
|
|
312
|
|
|
|
313
|
|
|
//carrega serviço |
314
|
|
|
$servico = 'MDFeRecepcaoEvento'; |
315
|
|
|
$this->servico( |
316
|
|
|
$servico, |
317
|
|
|
$uf, |
318
|
|
|
$this->tpAmb |
319
|
|
|
); |
320
|
|
|
$cnpj = $this->config->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
|
|
|
if ($this->typePerson === 'J') { |
331
|
|
|
$request .= "<CNPJ>$cnpj</CNPJ>"; |
332
|
|
|
} else { |
333
|
|
|
$request .= "<CPF>$cnpj</CPF>"; |
334
|
|
|
} |
335
|
|
|
$request .= "<chMDFe>$chave</chMDFe>" |
336
|
|
|
. "<dhEvento>$dhEvento</dhEvento>" |
337
|
|
|
. "<tpEvento>$tpEvento</tpEvento>" |
338
|
|
|
. "<nSeqEvento>$nSeqEvento</nSeqEvento>" |
339
|
|
|
. "<detEvento versaoEvento=\"$this->urlVersion\">" |
340
|
|
|
. "$tagAdic" |
341
|
|
|
. "</detEvento>" |
342
|
|
|
. "</infEvento>" |
343
|
|
|
. "</eventoMDFe>"; |
344
|
|
|
//assinatura dos dados |
345
|
|
|
$request = Signer::sign( |
346
|
|
|
$this->certificate, |
347
|
|
|
$request, |
348
|
|
|
'infEvento', |
349
|
|
|
'Id', |
350
|
|
|
$this->algorithm, |
351
|
|
|
$this->canonical |
352
|
|
|
); |
353
|
|
|
$request = Strings::clearXmlString($request, true); |
354
|
|
|
$this->isValid($this->urlVersion, $request, 'eventoMDFe'); |
355
|
|
|
$this->lastRequest = $request; |
356
|
|
|
$parameters = ['mdfeDadosMsg' => $request]; |
357
|
|
|
$body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>"; |
358
|
|
|
$this->lastResponse = $this->sendRequest($body, $parameters); |
359
|
|
|
return $this->lastResponse; |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.