|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NFePHP\eSocial; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Classe Tools, performs communication with the e-Social webservice |
|
7
|
|
|
* |
|
8
|
|
|
* @category library |
|
9
|
|
|
* @package NFePHP\eSocial |
|
10
|
|
|
* @copyright Copyright (c) 2017 |
|
11
|
|
|
* @license https://www.gnu.org/licenses/lgpl-3.0.txt LGPLv3 |
|
12
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.txt GPLv3 |
|
13
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT |
|
14
|
|
|
* @author Roberto L. Machado <linux.rlm at gmail dot com> |
|
15
|
|
|
* @link http://github.com/nfephp-org/sped-esocial for the canonical source repository |
|
16
|
|
|
*/ |
|
17
|
|
|
use InvalidArgumentException; |
|
18
|
|
|
use NFePHP\Common\Certificate; |
|
19
|
|
|
use NFePHP\Common\Validator; |
|
20
|
|
|
use NFePHP\eSocial\Common\FactoryInterface; |
|
21
|
|
|
use NFePHP\eSocial\Common\Soap\SoapCurl; |
|
22
|
|
|
use NFePHP\eSocial\Common\Soap\SoapInterface; |
|
23
|
|
|
use NFePHP\eSocial\Common\Tools as ToolsBase; |
|
24
|
|
|
use RuntimeException; |
|
25
|
|
|
|
|
26
|
|
|
class Tools extends ToolsBase |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
public $lastRequest; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
public $lastResponse; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var \NFePHP\Common\Soap\SoapInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $soap; |
|
40
|
|
|
/** |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $soapnamespaces = [ |
|
44
|
|
|
'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance", |
|
45
|
|
|
'xmlns:xsd' => "http://www.w3.org/2001/XMLSchema", |
|
46
|
|
|
'xmlns:soap' => "http://www.w3.org/2003/05/soap-envelope", |
|
47
|
|
|
]; |
|
48
|
|
|
/** |
|
49
|
|
|
* @var \SoapHeader |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $objHeader; |
|
52
|
|
|
/** |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $xmlns; |
|
56
|
|
|
/** |
|
57
|
|
|
* @var string |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $uri; |
|
60
|
|
|
/** |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $action; |
|
64
|
|
|
/** |
|
65
|
|
|
* @var string |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $method; |
|
68
|
|
|
/** |
|
69
|
|
|
* @var array |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $parameters; |
|
72
|
|
|
/** |
|
73
|
|
|
* @var array |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $envelopeXmlns; |
|
76
|
|
|
/** |
|
77
|
|
|
* @var array |
|
78
|
|
|
*/ |
|
79
|
|
|
protected $urlbase; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Constructor |
|
83
|
|
|
* @param string $config |
|
84
|
|
|
* @param Certificate $certificate |
|
85
|
|
|
*/ |
|
86
|
|
|
public function __construct($config, Certificate $certificate) |
|
87
|
|
|
{ |
|
88
|
|
|
parent::__construct($config, $certificate); |
|
89
|
|
|
//define o ambiente a ser usado |
|
90
|
|
|
$this->urlbase = [ |
|
91
|
|
|
'consulta' => 'https://webservices.producaorestrita.esocial.gov.br/' |
|
92
|
|
|
. 'servicos/empregador/consultarloteeventos/WsConsultarLoteEventos.svc', |
|
93
|
|
|
'envio' => 'https://webservices.producaorestrita.esocial.gov.br/' |
|
94
|
|
|
. 'servicos/empregador/enviarloteeventos/WsEnviarLoteEventos.svc', |
|
95
|
|
|
'identificadores' => 'https://webservices.producaorestrita.esocial.gov.br/' |
|
96
|
|
|
. 'servicos/empregador/dwlcirurgico/WsConsultarIdentificadoresEventos.svc', |
|
97
|
|
|
'download' => 'https://webservices.producaorestrita.esocial.gov.br/' |
|
98
|
|
|
. 'servicos/empregador/dwlcirurgico/WsSolicitarDownloadEventos.svc' |
|
99
|
|
|
]; |
|
100
|
|
|
if ($this->tpAmb == 1) { |
|
101
|
|
|
$this->urlbase = [ |
|
102
|
|
|
'consulta' => 'https://webservices.consulta.esocial.gov.br/' |
|
103
|
|
|
. 'servicos/empregador/consultarloteeventos/WsConsultarLoteEventos.svc', |
|
104
|
|
|
'envio' => 'https://webservices.envio.esocial.gov.br/' |
|
105
|
|
|
. 'servicos/empregador/enviarloteeventos/WsEnviarLoteEventos.svc', |
|
106
|
|
|
'identificadores' => 'https://webservices.download.esocial.gov.br/' |
|
107
|
|
|
. 'servicos/empregador/dwlcirurgico/WsConsultarIdentificadoresEventos.svc', |
|
108
|
|
|
'download' => 'https://webservices.download.esocial.gov.br/' |
|
109
|
|
|
. 'servicos/empregador/dwlcirurgico/WsSolicitarDownloadEventos.svc' |
|
110
|
|
|
]; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* SOAP communication dependency injection |
|
116
|
|
|
* @param SoapInterface $soap |
|
117
|
|
|
*/ |
|
118
|
|
|
public function loadSoapClass(SoapInterface $soap) |
|
119
|
|
|
{ |
|
120
|
|
|
$this->soap = $soap; |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Event batch query |
|
125
|
|
|
* @param string $protocolo |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
|
|
public function consultarLoteEventos($protocolo) |
|
129
|
|
|
{ |
|
130
|
|
|
$operationVersion = $this->serviceXsd['ConsultaLoteEventos']['version']; |
|
131
|
|
|
if (empty($operationVersion)) { |
|
132
|
|
|
throw new \InvalidArgumentException( |
|
133
|
|
|
'Schemas não localizados, verifique de passou as versões ' |
|
134
|
|
|
. 'corretamente no config.' |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
$verWsdl = $this->serviceXsd['WsConsultarLoteEventos']['version']; |
|
138
|
|
|
$this->action = "http://www.esocial.gov.br/servicos/empregador/lote" |
|
139
|
|
|
."/eventos/envio/consulta/retornoProcessamento/$verWsdl" |
|
140
|
|
|
."/ServicoConsultarLoteEventos/ConsultarLoteEventos"; |
|
141
|
|
|
$this->method = "ConsultarLoteEventos"; |
|
142
|
|
|
$this->uri = $this->urlbase['consulta']; |
|
143
|
|
|
$this->envelopeXmlns = [ |
|
144
|
|
|
'xmlns:soapenv' => "http://schemas.xmlsoap.org/soap/envelope/", |
|
145
|
|
|
'xmlns:v1' => "http://www.esocial.gov.br/servicos/empregador/lote" |
|
146
|
|
|
."/eventos/envio/consulta/retornoProcessamento/$verWsdl", |
|
147
|
|
|
]; |
|
148
|
|
|
$request = "<eSocial xmlns=\"http://www.esocial.gov.br/schema/lote" |
|
149
|
|
|
."/eventos/envio/consulta/retornoProcessamento/" |
|
150
|
|
|
. $operationVersion . "\" " |
|
151
|
|
|
."xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" |
|
152
|
|
|
."<consultaLoteEventos>" |
|
153
|
|
|
."<protocoloEnvio>$protocolo</protocoloEnvio>" |
|
154
|
|
|
."</consultaLoteEventos>" |
|
155
|
|
|
."</eSocial>"; |
|
156
|
|
|
//validar a requisição conforme o seu respectivo XSD |
|
157
|
|
|
Validator::isValid( |
|
158
|
|
|
$request, |
|
159
|
|
|
$this->path |
|
160
|
|
|
."schemes/comunicacao/$this->serviceStr/" |
|
161
|
|
|
."ConsultaLoteEventos-$operationVersion.xsd" |
|
162
|
|
|
); |
|
163
|
|
|
$body = "<v1:ConsultarLoteEventos>" |
|
164
|
|
|
."<v1:consulta>" |
|
165
|
|
|
.$request |
|
166
|
|
|
."</v1:consulta>" |
|
167
|
|
|
."</v1:ConsultarLoteEventos>"; |
|
168
|
|
|
$this->lastRequest = $body; |
|
169
|
|
|
$this->lastResponse = $this->sendRequest($body); |
|
170
|
|
|
return $this->lastResponse; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function consultarEventosEmpregador() |
|
174
|
|
|
{ |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function consultarEventosTabela() |
|
178
|
|
|
{ |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function consultarEventosTrabalhador() |
|
182
|
|
|
{ |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public function downloadEventosPorId() |
|
186
|
|
|
{ |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
public function downloadEventosPorNrRecibo() |
|
190
|
|
|
{ |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Send request to webservice |
|
195
|
|
|
* @param string $request |
|
196
|
|
|
* @return string |
|
197
|
|
|
*/ |
|
198
|
|
|
protected function sendRequest($request) |
|
199
|
|
|
{ |
|
200
|
|
|
if (empty($this->soap)) { |
|
201
|
|
|
$this->soap = new SoapCurl($this->certificate); |
|
|
|
|
|
|
202
|
|
|
} |
|
203
|
|
|
$envelope = "<soapenv:Envelope "; |
|
204
|
|
|
foreach ($this->envelopeXmlns as $key => $xmlns) { |
|
205
|
|
|
$envelope .= "$key = \"$xmlns\" "; |
|
206
|
|
|
} |
|
207
|
|
|
$envelope .= ">" |
|
208
|
|
|
."<soapenv:Header/>" |
|
209
|
|
|
."<soapenv:Body>" |
|
210
|
|
|
.$request |
|
211
|
|
|
."</soapenv:Body>" |
|
212
|
|
|
."</soapenv:Envelope>"; |
|
213
|
|
|
$msgSize = strlen($envelope); |
|
214
|
|
|
$parameters = [ |
|
215
|
|
|
"Content-Type: text/xml;charset=UTF-8", |
|
216
|
|
|
"SOAPAction: \"$this->action\"", |
|
217
|
|
|
"Content-length: $msgSize", |
|
218
|
|
|
]; |
|
219
|
|
|
return (string) $this->soap->send( |
|
220
|
|
|
$this->method, |
|
221
|
|
|
$this->uri, |
|
222
|
|
|
$this->action, |
|
223
|
|
|
$envelope, |
|
224
|
|
|
$parameters |
|
225
|
|
|
); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Send batch of events |
|
230
|
|
|
* @param integer $grupo |
|
231
|
|
|
* @param array $eventos |
|
232
|
|
|
* @return string |
|
233
|
|
|
*/ |
|
234
|
|
|
public function enviarLoteEventos($grupo, $eventos = []) |
|
235
|
|
|
{ |
|
236
|
|
|
if (empty($eventos)) { |
|
237
|
|
|
return ''; |
|
238
|
|
|
} |
|
239
|
|
|
$xml = ""; |
|
240
|
|
|
$nEvt = count($eventos); |
|
241
|
|
|
if ($nEvt > 50) { |
|
242
|
|
|
throw new InvalidArgumentException( |
|
243
|
|
|
"O numero máximo de eventos em um lote é 50, " |
|
244
|
|
|
."você está tentando enviar $nEvt eventos !" |
|
245
|
|
|
); |
|
246
|
|
|
} |
|
247
|
|
|
foreach ($eventos as $evt) { |
|
248
|
|
|
//verifica se o evento pertence ao grupo indicado |
|
249
|
|
|
if (! in_array($evt->alias(), $this->grupos[$grupo])) { |
|
250
|
|
|
throw new RuntimeException( |
|
251
|
|
|
'O evento ' . $evt->alias() . ' não pertence a este grupo [ ' |
|
252
|
|
|
. $this->eventGroup[$grupo] . ' ].' |
|
253
|
|
|
); |
|
254
|
|
|
} |
|
255
|
|
|
$this->checkCertificate($evt); |
|
256
|
|
|
$xml .= "<evento Id=\"$evt->evtid\">"; |
|
257
|
|
|
$xml .= $evt->toXML(); |
|
258
|
|
|
$xml .= "</evento>"; |
|
259
|
|
|
} |
|
260
|
|
|
$operationVersion = $this->serviceXsd['EnvioLoteEventos']['version']; |
|
261
|
|
|
if (empty($operationVersion)) { |
|
262
|
|
|
throw new \InvalidArgumentException( |
|
263
|
|
|
'Schemas não localizados, verifique de passou as versões ' |
|
264
|
|
|
. 'corretamente no config.' |
|
265
|
|
|
); |
|
266
|
|
|
} |
|
267
|
|
|
$verWsdl = $this->serviceXsd['WsEnviarLoteEventos']['version']; |
|
268
|
|
|
$this->method = "EnviarLoteEventos"; |
|
269
|
|
|
$this->action = "http://www.esocial.gov.br/servicos/empregador/lote" |
|
270
|
|
|
. "/eventos/envio/" |
|
271
|
|
|
. $verWsdl |
|
272
|
|
|
. "/ServicoEnviarLoteEventos" |
|
273
|
|
|
. "/EnviarLoteEventos"; |
|
274
|
|
|
$this->uri = $this->urlbase['envio']; |
|
275
|
|
|
$this->envelopeXmlns = [ |
|
276
|
|
|
'xmlns:soapenv' => "http://schemas.xmlsoap.org/soap/envelope/", |
|
277
|
|
|
'xmlns:v1' => "http://www.esocial.gov.br/servicos/empregador" |
|
278
|
|
|
. "/lote/eventos/envio/$verWsdl", |
|
279
|
|
|
]; |
|
280
|
|
|
$request = "<eSocial xmlns=\"http://www.esocial.gov.br/schema/lote" |
|
281
|
|
|
. "/eventos/envio/$operationVersion\" " |
|
282
|
|
|
. "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" |
|
283
|
|
|
. "<envioLoteEventos grupo=\"$grupo\">" |
|
284
|
|
|
. "<ideEmpregador>" |
|
285
|
|
|
. "<tpInsc>$this->tpInsc</tpInsc>" |
|
286
|
|
|
. "<nrInsc>$this->nrInsc</nrInsc>" |
|
287
|
|
|
. "</ideEmpregador>" |
|
288
|
|
|
. "<ideTransmissor>" |
|
289
|
|
|
. "<tpInsc>$this->transmissortpInsc</tpInsc>" |
|
290
|
|
|
. "<nrInsc>$this->transmissornrInsc</nrInsc>" |
|
291
|
|
|
. "</ideTransmissor>" |
|
292
|
|
|
. "<eventos>" |
|
293
|
|
|
. "$xml" |
|
294
|
|
|
. "</eventos>" |
|
295
|
|
|
. "</envioLoteEventos>" |
|
296
|
|
|
. "</eSocial>"; |
|
297
|
|
|
//validar a requisição conforme o seu respectivo XSD |
|
298
|
|
|
Validator::isValid( |
|
299
|
|
|
$request, |
|
300
|
|
|
$this->path |
|
301
|
|
|
. "schemes/comunicacao/$this->serviceStr/" |
|
302
|
|
|
. "EnvioLoteEventos-$operationVersion.xsd" |
|
303
|
|
|
); |
|
304
|
|
|
$body = "<v1:EnviarLoteEventos>" |
|
305
|
|
|
. "<v1:loteEventos>" |
|
306
|
|
|
. $request |
|
307
|
|
|
. "</v1:loteEventos>" |
|
308
|
|
|
. "</v1:EnviarLoteEventos>"; |
|
309
|
|
|
$this->lastRequest = $body; |
|
310
|
|
|
$this->lastResponse = $this->sendRequest($body); |
|
311
|
|
|
return $this->lastResponse; |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
* Verify the availability of a digital certificate. |
|
316
|
|
|
* If available, place it where it is needed |
|
317
|
|
|
* @param FactoryInterface $evento |
|
318
|
|
|
* @throws RuntimeException |
|
319
|
|
|
*/ |
|
320
|
|
|
protected function checkCertificate(FactoryInterface $evento) |
|
321
|
|
|
{ |
|
322
|
|
|
if (empty($this->certificate)) { |
|
323
|
|
|
//try to get certificate from event |
|
324
|
|
|
$certificate = $evento->getCertificate(); |
|
325
|
|
|
if (empty($certificate)) { |
|
326
|
|
|
//oops no certificate avaiable |
|
327
|
|
|
throw new \RuntimeException("Não temos um certificado disponível!"); |
|
328
|
|
|
} |
|
329
|
|
|
$this->certificate = $certificate; |
|
330
|
|
|
} else { |
|
331
|
|
|
$certificate = $evento->getCertificate(); |
|
332
|
|
|
if (empty($certificate)) { |
|
333
|
|
|
$evento->setCertificate($this->certificate); |
|
334
|
|
|
} |
|
335
|
|
|
} |
|
336
|
|
|
} |
|
337
|
|
|
} |
|
338
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..