Passed
Push — master ( aabf39...695603 )
by Roberto
01:15
created

Tools::consultR209()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 20
cp 0
rs 9.584
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace NFePHP\EFDReinf;
4
5
/**
6
 * Classe Tools, performs communication with the EFDReinf webservice
7
 *
8
 * @category  API
9
 * @package   NFePHP\EFDReinf\Tools
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-efdreinf for the canonical source repository
16
 */
17
use NFePHP\Common\Certificate;
18
use NFePHP\Common\Validator;
19
use NFePHP\EFDReinf\Common\Tools as ToolsBase;
20
use NFePHP\EFDReinf\Common\FactoryInterface;
21
use NFePHP\EFDReinf\Common\Soap\SoapCurl;
22
use NFePHP\EFDReinf\Common\Soap\SoapInterface;
23
use NFePHP\EFDReinf\Exception\ProcessException;
24
use stdClass;
25
use NFePHP\EFDReinf\Common\Factory;
26
27
class Tools extends ToolsBase
28
{
29
    const CONSULTA_CONSOLIDADA = 1;
30
    const CONSULTA_R1000 = 2;
31
    const CONSULTA_R1070 = 3;
32
    const CONSULTA_R2010 = 4;
33
    const CONSULTA_R2020 = 5;
34
    const CONSULTA_R2030 = 6;
35
    const CONSULTA_R2040 = 7;
36
    const CONSULTA_R2050 = 8;
37
    const CONSULTA_R2060 = 9;
38
    const CONSULTA_R2098 = 10;
39
    const CONSULTA_R2099 = 11;
40
    const CONSULTA_R3010 = 12;
41
    
42
    /**
43
     * @var string
44
     */
45
    public $lastRequest;
46
    /**
47
     * @var string
48
     */
49
    public $lastResponse;
50
    /**
51
     * @var SoapInterface
52
     */
53
    public $soap;
54
    /**
55
     * @var array
56
     */
57
    protected $soapnamespaces = [
58
        'xmlns:soapenv' => "http://schemas.xmlsoap.org/soap/envelope/",
59
        'xmlns:sped'=> "http://sped.fazenda.gov.br/"
60
    ];
61
    /**
62
     * @var array
63
     */
64
    protected $uri = [
65
        '1' => 'https://reinf.receita.fazenda.gov.br/WsREINF/RecepcaoLoteReinf.svc',
66
        '2' => 'https://preprodefdreinf.receita.fazenda.gov.br/WsREINF/RecepcaoLoteReinf.svc'
67
        
68
    ];
69
    /**
70
     * @var array
71
     */
72
    protected $uriconsulta = [
73
        '1' => 'https://reinf.receita.fazenda.gov.br/WsREINF/ConsultasReinf.svc',
74
        '2' => 'https://preprodefdreinf.receita.fazenda.gov.br/WsREINF/ConsultasReinf.svc'
75
    ];
76
    /**
77
     * @var string
78
     */
79
    protected $action;
80
    /**
81
     * @var string
82
     */
83
    protected $method;
84
    
85
    /**
86
     * Constructor
87
     * @param string $config
88
     * @param Certificate $certificate
89
     */
90
    public function __construct($config, Certificate $certificate)
91
    {
92
        parent::__construct($config, $certificate);
93
    }
94
    
95
    /**
96
     * SOAP communication dependency injection
97
     * @param SoapInterface $soap
98
     */
99
    public function loadSoapClass(SoapInterface $soap)
100
    {
101
        $this->soap = $soap;
102
    }
103
    
104
    /**
105
     * Run EFD-REINF Query
106
     * @param integer $mod
107
     * @param stdClass $std
108
     * @throws ProcessException
109
     */
110
    public function consultar($mod, stdClass $std = null)
111
    {
112
        if (isset($std)) {
113
            //converte os nomes das propriedades do stdClass para caixa baixa
114
            $std = Factory::propertiesToLower($std);
115
        }
116
        switch ($mod) {
117
            case 1:
118
                $evt = 0;
119
                $request = $this->consultConsolidadas($evt, $std);
0 ignored issues
show
Bug introduced by
It seems like $std defined by parameter $std on line 110 can be null; however, NFePHP\EFDReinf\Tools::consultConsolidadas() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
120
                break;
121
            case 2:
122
                $evt = 1000;
123
                $request = $this->consultR1($evt);
124
                break;
125
            case 3:
126
                $evt = 1070;
127
                $request = $this->consultR1($evt);
128
                break;
129
            case 4:
130
                $evt = 2010;
131
                $request = $this->consultR2010($evt, $std);
0 ignored issues
show
Bug introduced by
It seems like $std defined by parameter $std on line 110 can be null; however, NFePHP\EFDReinf\Tools::consultR2010() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
132
                break;
133
            case 5:
134
                $evt = 2020;
135
                $request = $this->consultR2020($evt, $std);
0 ignored issues
show
Bug introduced by
It seems like $std defined by parameter $std on line 110 can be null; however, NFePHP\EFDReinf\Tools::consultR2020() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
136
                break;
137
            case 6:
138
                $evt = 2030;
139
                $request = $this->consultR20($evt, $std);
0 ignored issues
show
Bug introduced by
It seems like $std defined by parameter $std on line 110 can be null; however, NFePHP\EFDReinf\Tools::consultR20() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
140
                break;
141
            case 7:
142
                $evt = 2040;
143
                $request = $this->consultR20($evt, $std);
0 ignored issues
show
Bug introduced by
It seems like $std defined by parameter $std on line 110 can be null; however, NFePHP\EFDReinf\Tools::consultR20() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
144
                break;
145
            case 8:
146
                $evt = 2050;
147
                $request = $this->consultR20($evt, $std);
0 ignored issues
show
Bug introduced by
It seems like $std defined by parameter $std on line 110 can be null; however, NFePHP\EFDReinf\Tools::consultR20() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
148
                break;
149
            case 9:
150
                $evt = 2060;
151
                $request = $this->consultR2060($evt, $std);
0 ignored issues
show
Bug introduced by
It seems like $std defined by parameter $std on line 110 can be null; however, NFePHP\EFDReinf\Tools::consultR2060() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
152
                break;
153
            case 10:
154
                $evt = 2098;
155
                $request = $this->consultR209($evt, $std);
0 ignored issues
show
Bug introduced by
It seems like $std defined by parameter $std on line 110 can be null; however, NFePHP\EFDReinf\Tools::consultR209() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
156
                break;
157
            case 11:
158
                $evt = 2099;
159
                $request = $this->consultR209($evt, $std);
0 ignored issues
show
Bug introduced by
It seems like $std defined by parameter $std on line 110 can be null; however, NFePHP\EFDReinf\Tools::consultR209() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
160
                break;
161
            case 12:
162
                $evt = 3010;
163
                $request = $this->consultR3010($evt, $std);
0 ignored issues
show
Bug introduced by
It seems like $std defined by parameter $std on line 110 can be null; however, NFePHP\EFDReinf\Tools::consultR3010() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
164
                break;
165
            default:
166
                throw ProcessException::wrongArgument(2003, '');
167
        }
168
        $this->lastResponse = $this->sendRequest($request);
169
        return $this->lastResponse;
170
    }
171
    
172
    /**
173
     * Consultation of consolidated information
174
     * @param integer $evt
175
     * @param stdClass $std
176
     * @return string
177
     */
178
    public function consultConsolidadas($evt, stdClass $std)
0 ignored issues
show
Unused Code introduced by
The parameter $evt is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
179
    {
180
        $properties = [
181
            'tipoinscricaocontribuinte' => [
182
                'required' => true,
183
                'type' => 'integer',
184
                'min' => 1,
185
                'max' => 2
186
            ],
187
            'numeroinscricaocontribuinte' => [
188
                'required' => true,
189
                'type' => 'string',
190
                'regex' => '^[0-9]{11,14}$'
191
            ],
192
            'numeroprotocolofechamento' => [
193
                'required' => true,
194
                'type' => 'string',
195
                'regex' => ''
196
            ],
197
        ];
198
        $this->validInputParameters($properties, $std);
199
        
200
        $this->method = "ConsultaInformacoesConsolidadas";
201
        $this->action = "http://sped.fazenda.gov.br/ConsultasReinf/".$this->method;
202
        $request = "<sped:{$this->method}>"
203
            . "<sped:tipoInscricaoContribuinte>{$std->tipoinscricaocontribuinte}</sped:tipoInscricaoContribuinte>"
204
            . "<sped:numeroInscricaoContribuinte>{$std->numeroinscricaocontribuinte}</sped:numeroInscricaoContribuinte>"
205
            . "<sped:numeroProtocoloFechamento>{$std->numeroprotocolofechamento}</sped:numeroProtocoloFechamento>"
206
            . "</sped:{$this->method}>";
207
        return $request;
208
    }
209
    
210
    /**
211
     * Consultation R1000 and R1070
212
     * @param integer $evt
213
     * @return string
214
     */
215
    protected function consultR1($evt)
216
    {
217
        $this->method = "ConsultaReciboEvento{$evt}";
218
        $this->action = "http://sped.fazenda.gov.br/ConsultasReinf/".$this->method;
219
        $request = "<sped:{$this->method}>"
220
            . "<sped:tipoEvento>{$evt}</sped:tipoEvento>"
221
            . "<sped:tpInsc>{$this->tpInsc}</sped:tpInsc>"
222
            . "<sped:nrInsc>{$this->nrInsc}</sped:nrInsc>"
223
            . "</sped:{$this->method}>";
224
        return $request;
225
    }
226
    
227
    /**
228
     * Consultation R2010
229
     * @param integer $evt
230
     * @param stdClass $std
231
     * @return string
232
     */
233
    protected function consultR2010($evt, $std)
234
    {
235
        $properties = [
236
            'perapur' => [
237
                'required' => true,
238
                'type' => 'string',
239
                'regex' => '^(19[0-9][0-9]|2[0-9][0-9][0-9])[-](0?[1-9]|1[0-2])$'
240
            ],
241
            'tpinscestab' => [
242
                'required' => true,
243
                'type' => 'integer',
244
                'min' => 1,
245
                'max' => 2
246
            ],
247
            'nrinscestab' => [
248
                'required' => true,
249
                'type' => 'string',
250
                'regex' => '^[0-9]{11,14}$'
251
            ],
252
            'cnpjprestador' => [
253
                'required' => true,
254
                'type' => 'string',
255
                'regex' => '^[0-9]{14}$'
256
            ],
257
        ];
258
        $this->validInputParameters($properties, $std);
259
        
260
        $this->method = "ConsultaReciboEvento{$evt}";
261
        $this->action = "http://sped.fazenda.gov.br/ConsultasReinf/".$this->method;
262
        $request = "<sped:{$this->method}>"
263
            . "<sped:tipoEvento>{$evt}</sped:tipoEvento>"
264
            . "<sped:tpInsc>{$this->tpInsc}</sped:tpInsc>"
265
            . "<sped:nrInsc>{$this->nrInsc}</sped:nrInsc>"
266
            . "<sped:perApur>{$std->perapur}</sped:dtApur>"
267
            . "<sped:tpInscEstab>{$std->tpinscestab}</sped:tpInscEstab>"
268
            . "<sped:nrInscEstab>{$std->nrinscestab}</sped:nrInscEstab>"
269
            . "<sped:cnpjPrestador>{$std->cnpjprestador}</sped:cnpjPrestador>"
270
            . "</sped:{$this->method}>";
271
        return $request;
272
    }
273
    
274
    /**
275
     * Consultation R2020
276
     * @param integer $evt
277
     * @param stdClass $std
278
     * @return string
279
     */
280
    protected function consultR2020($evt, $std)
281
    {
282
        $properties = [
283
            'perapur' => [
284
                'required' => true,
285
                'type' => 'string',
286
                'regex' => '^(19[0-9][0-9]|2[0-9][0-9][0-9])[-](0?[1-9]|1[0-2])$'
287
             ],
288
            'nrinscestabprest' => [
289
                'required' => true,
290
                'type' => 'string',
291
                'regex' => '^[0-9]{11,14}$'
292
            ],
293
            'tpinsctomador' => [
294
                'required' => true,
295
                'type' => 'integer',
296
                'min' => 1,
297
                'max' => 2
298
            ],
299
            'nrinsctomador' => [
300
                'required' => true,
301
                'type' => 'string',
302
                'regex' => '^[0-9]{11,14}$'
303
            ],
304
        ];
305
        $this->validInputParameters($properties, $std);
306
        
307
        $this->method = "ConsultaReciboEvento{$evt}";
308
        $this->action = "http://sped.fazenda.gov.br/ConsultasReinf/".$this->method;
309
        $request = "<sped:{$this->method}>"
310
            . "<sped:tipoEvento>{$evt}</sped:tipoEvento>"
311
            . "<sped:tpInsc>{$this->tpInsc}</sped:tpInsc>"
312
            . "<sped:nrInsc>{$this->nrInsc}</sped:nrInsc>"
313
            . "<sped:perApur>{$std->perapur}</sped:dtApur>"
314
            . "<sped:nrInscEstabPrest>{$std->nrinscestabprest}</sped:nrInscEstabPrest>"
315
            . "<sped:tpInscTomador>{$std->tpinsctomador}</sped:tpInscTomador>"
316
            . "<sped:nrInscTomador>{$std->nrinsctomador}</sped:nrInscTomador>"
317
            . "</sped:{$this->method}>";
318
        return $request;
319
    }
320
    
321
    /**
322
     * Consultation R2030 and R2040 and R2050
323
     * @param integer $evt
324
     * @param stdClass $std
325
     * @return string
326
     */
327
    protected function consultR20($evt, $std)
328
    {
329
        $properties = [
330
            'perapur' => [
331
                'required' => true,
332
                'type' => 'string',
333
                'regex' => '^(19[0-9][0-9]|2[0-9][0-9][0-9])[-](0?[1-9]|1[0-2])$'
334
            ],
335
            'nrinscestab' => [
336
                'required' => true,
337
                'type' => 'string',
338
                'regex' => '^[0-9]{11,14}$'
339
            ],
340
        ];
341
        $this->validInputParameters($properties, $std);
342
        
343
        $this->method = "ConsultaReciboEvento{$evt}";
344
        $this->action = "http://sped.fazenda.gov.br/ConsultasReinf/".$this->method;
345
        $request = "<sped:{$this->method}>"
346
            . "<sped:tipoEvento>{$evt}</sped:tipoEvento>"
347
            . "<sped:tpInsc>{$this->tpInsc}</sped:tpInsc>"
348
            . "<sped:nrInsc>{$this->nrInsc}</sped:nrInsc>"
349
            . "<sped:perApur>{$std->perapur}</sped:dtApur>"
350
            . "<sped:nrInscEstab>{$std->nrinscestab}</sped:nrInscEstab>"
351
            . "</sped:{$this->method}>";
352
        return $request;
353
    }
354
    
355
    /**
356
     * Consultation R2060
357
     * @param integer $evt
358
     * @param stdClass $std
359
     * @return string
360
     */
361
    protected function consultR2060($evt, $std)
362
    {
363
        $properties = [
364
            'perapur' => [
365
                'required' => true,
366
                'type' => 'string',
367
                'regex' => '^(19[0-9][0-9]|2[0-9][0-9][0-9])[-](0?[1-9]|1[0-2])$'
368
            ],
369
            'nrinscestabprest' => [
370
                'required' => true,
371
                'type' => 'string',
372
                'regex' => '^[0-9]{11,14}$'
373
            ],
374
            'tpinscestab' => [
375
                'required' => true,
376
                'type' => 'integer',
377
                'min' => 1,
378
                'max' => 2
379
            ],
380
            'nrinscestab' => [
381
                'required' => true,
382
                'type' => 'string',
383
                'regex' => '^[0-9]{11,14}$'
384
            ],
385
        ];
386
        $this->validInputParameters($properties, $std);
387
        
388
        $this->method = "ConsultaReciboEvento{$evt}";
389
        $this->action = "http://sped.fazenda.gov.br/ConsultasReinf/".$this->method;
390
        $request = "<sped:{$this->method}>"
391
            . "<sped:tipoEvento>{$evt}</sped:tipoEvento>"
392
            . "<sped:tpInsc>{$this->tpInsc}</sped:tpInsc>"
393
            . "<sped:nrInsc>{$this->nrInsc}</sped:nrInsc>"
394
            . "<sped:perApur>{$std->perapur}</sped:dtApur>"
395
            . "<sped:tpInscEstab>{$std->tpinscestab}</sped:tpInscEstab>"
396
            . "<sped:nrInscEstab>{$std->nrinscestab}</sped:nrInscEstab>"
397
            . "</sped:{$this->method}>";
398
        return $request;
399
    }
400
    
401
    /**
402
     * Consultation R2098 and R2099
403
     * @param integer $evt
404
     * @param stdClass $std
405
     * @return string
406
     */
407
    protected function consultR209($evt, $std)
408
    {
409
        $properties = [
410
            'perapur' => [
411
                'required' => true,
412
                'type' => 'string',
413
                'regex' => '^(19[0-9][0-9]|2[0-9][0-9][0-9])[-](0?[1-9]|1[0-2])$'
414
            ],
415
        ];
416
        $this->validInputParameters($properties, $std);
417
        
418
        $this->method = "ConsultaReciboEvento{$evt}";
419
        $this->action = "http://sped.fazenda.gov.br/ConsultasReinf/".$this->method;
420
        $request = "<sped:{$this->method}>"
421
            . "<sped:tipoEvento>{$evt}</sped:tipoEvento>"
422
            . "<sped:tpInsc>{$this->tpInsc}</sped:tpInsc>"
423
            . "<sped:nrInsc>{$this->nrInsc}</sped:nrInsc>"
424
            . "<sped:perApur>{$std->perapur}</sped:dtApur>"
425
            . "</sped:{$this->method}>";
426
        return $request;
427
    }
428
   
429
    /**
430
     * Consultation R3010
431
     * @param integer $evt
432
     * @param stdClass $std
433
     * @return string
434
     */
435
    protected function consultR3010($evt, $std)
436
    {
437
        $properties = [
438
            'dtapur' => [
439
                'required' => true,
440
                'type' => 'string',
441
                'regex' => '^(19[0-9][0-9]|2[0-9][0-9][0-9])[-](0?[1-9]|1[0-2])[-](0?[1-9]|[1-2][0-9]|3[0-1])$'
442
            ],
443
            'nrinscestabelecimento' => [
444
                'required' => true,
445
                'type' => 'string',
446
                'regex' => '^[0-9]{11,14}$'
447
            ],
448
        ];
449
        $this->validInputParameters($properties, $std);
450
        
451
        $this->method = "ConsultaReciboEvento{$evt}";
452
        $this->action = "http://sped.fazenda.gov.br/ConsultasReinf/".$this->method;
453
        $request = "<sped:{$this->method}>"
454
            . "<sped:tipoEvento>{$evt}</sped:tipoEvento>"
455
            . "<sped:tpInsc>{$this->tpInsc}</sped:tpInsc>"
456
            . "<sped:nrInsc>{$this->nrInsc}</sped:nrInsc>"
457
            . "<sped:dtApur>{$std->dtapur}</sped:dtApur>"
458
            . "<sped:nrInscEstabelecimento>{$std->nrinscestabelecimento}</sped:nrInscEstabelecimento>"
459
            . "</sped:{$this->method}>";
460
        return $request;
461
    }
462
    
463
    
464
    /**
465
     * Send batch of events
466
     * @param  integer $grupo
467
     * @param array $eventos
468
     * @return string
469
     */
470
    public function enviarLoteEventos($grupo, $eventos = [])
471
    {
472
        if (empty($eventos)) {
473
            return '';
474
        }
475
        //check number of events
476
        $nEvt = count($eventos);
477
        if ($nEvt > 100) {
478
            throw ProcessException::wrongArgument(2000, $nEvt);
479
        }
480
        $this->method = "ReceberLoteEventos";
481
        $this->action = "http://sped.fazenda.gov.br/RecepcaoLoteReinf/ReceberLoteEventos";
482
        $xml = "";
483
        foreach ($eventos as $evt) {
484
            if (!is_a($evt, '\NFePHP\EFDReinf\Common\FactoryInterface')) {
485
                throw ProcessException::wrongArgument(2002, '');
486
            }
487
            //verifica se o evento pertence ao grupo indicado
488
            if (! in_array($evt->alias(), $this->grupos[$grupo])) {
489
                throw new \RuntimeException(
490
                    'O evento ' . $evt->alias() . ' não pertence a este grupo [ '
491
                    . $this->eventGroup[$grupo] . ' ].'
492
                );
493
            }
494
            $this->checkCertificate($evt);
495
            $xml .= "<evento id=\"".$evt->getId()."\">";
496
            $xml .= $evt->toXML();
497
            $xml .= "</evento>";
498
        }
499
        //build request
500
        $request = "<Reinf xmlns=\"http://www.reinf.esocial.gov.br/schemas/envioLoteEventos/v"
501
            . $this->serviceVersion."\" >"
502
            . "<loteEventos>"
503
            . $xml
504
            . "</loteEventos>"
505
            . "</Reinf>";
506
        //validate requisition with XSD
507
        $xsd = $this->path
508
            . "schemes/comunicacao/v$this->serviceVersion/"
509
            . $this->serviceXsd['EnvioLoteEventos']['name'];
510
        Validator::isValid($request, $xsd);
511
        //build soap body
512
        $body = "<sped:ReceberLoteEventos>"
513
            . "<sped:loteEventos>"
514
            . $request
515
            . "</sped:loteEventos>"
516
            . "</sped:ReceberLoteEventos>";
517
        $this->lastResponse = $this->sendRequest($body);
518
        return $this->lastResponse;
519
    }
520
    
521
    /**
522
     * Send request to webservice
523
     * @param string $request
524
     * @return string
525
     */
526
    protected function sendRequest($request)
527
    {
528
        if (empty($this->soap)) {
529
            $this->soap = new SoapCurl($this->certificate);
530
        }
531
        $envelope = "<soapenv:Envelope ";
532
        foreach ($this->soapnamespaces as $key => $xmlns) {
533
            $envelope .= "$key = \"$xmlns\" ";
534
        }
535
        $envelope .= ">"
536
            . "<soapenv:Header/>"
537
            . "<soapenv:Body>"
538
            . $request
539
            . "</soapenv:Body>"
540
            . "</soapenv:Envelope>";
541
        
542
        $msgSize = strlen($envelope);
543
        $parameters = [
544
            "Content-Type: text/xml;charset=UTF-8",
545
            "SOAPAction: \"$this->action\"",
546
            "Content-length: $msgSize"
547
        ];
548
        if ($this->method == 'ReceberLoteEventos') {
549
            $url = $this->uri[$this->tpAmb];
550
        } else {
551
            $url = $this->uriconsulta[$this->tpAmb];
552
        }
553
        $this->lastRequest = $envelope;
554
        return (string) $this->soap->send(
555
            $this->method,
556
            $url,
557
            $this->action,
558
            $envelope,
559
            $parameters
560
        );
561
    }
562
563
    /**
564
     * Verify the availability of a digital certificate.
565
     * If available, place it where it is needed
566
     * @param FactoryInterface $evento
567
     * @throws RuntimeException
568
     */
569
    protected function checkCertificate(FactoryInterface $evento)
570
    {
571
        //try to get certificate from event
572
        $certificate = $evento->getCertificate();
573
        if (empty($certificate)) {
574
            $evento->setCertificate($this->certificate);
575
        }
576
    }
577
    
578
    protected function validInputParameters($properties, $std)
579
    {
580
        foreach ($properties as $key => $rules) {
581
            $r = json_decode(json_encode($rules));
582
            if ($r->required) {
583
                if (!isset($std->$key)) {
584
                    throw new \Exception("$key não foi passado como parâmetro e é obrigatório.");
585
                }
586
                $value = $std->$key;
587
                if ($r->type === 'integer') {
588
                    if ($value < $r->min || $value > $r->max) {
589
                        throw new \Exception("$key contêm um valor invalido [$value].");
590
                    }
591
                }
592
                if ($r->type === 'string') {
593
                    if (!preg_match("/{$r->regex}/", $value)) {
594
                        throw new \Exception("$key contêm um valor invalido [$value].");
595
                    }
596
                }
597
            }
598
        }
599
    }
600
}
601