Passed
Push — master ( 16e739...240667 )
by Roberto
50s queued 10s
created

EvtCAT::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 2
1
<?php
2
3
namespace NFePHP\eSocial\Factories;
4
5
/**
6
 * Class eSocial EvtCAT Event S-2210 constructor
7
 * Read for 2.4.2 layout
8
 * Read for 2.5.0 layout
9
 *
10
 * @category  library
11
 * @package   NFePHP\eSocial
12
 * @copyright NFePHP Copyright (c) 2017
13
 * @license   http://www.gnu.org/licenses/lgpl.txt LGPLv3+
14
 * @license   https://opensource.org/licenses/MIT MIT
15
 * @license   http://www.gnu.org/licenses/gpl.txt GPLv3+
16
 * @author    Roberto L. Machado <linux.rlm at gmail dot com>
17
 * @link      http://github.com/nfephp-org/sped-esocial for the canonical source repository
18
 */
19
20
use NFePHP\Common\Certificate;
21
use NFePHP\eSocial\Common\Factory;
22
use NFePHP\eSocial\Common\FactoryId;
23
use NFePHP\eSocial\Common\FactoryInterface;
24
use stdClass;
25
26
class EvtCAT extends Factory implements FactoryInterface
27
{
28
    /**
29
     * @var string
30
     */
31
    protected $evtName = 'evtCAT';
32
    /**
33
     * @var string
34
     */
35
    protected $evtAlias = 'S-2210';
36
    /**
37
     * Parameters patterns
38
     *
39
     * @var array
40
     */
41
    protected $parameters = [];
42
43
    /**
44
     * Constructor
45
     *
46
     * @param string $config
47
     * @param stdClass $std
48
     * @param Certificate $certificate | null
49
     * @param string $date
50
     */
51
    public function __construct(
52
        $config,
53
        stdClass $std,
54
        Certificate $certificate = null,
55
        $date = ''
56
    ) {
57
        parent::__construct($config, $std, $certificate, $date);
58
    }
59
60
    /**
61
     * Node constructor
62
     */
63
    protected function toNode()
64
    {
65
        $ideEmpregador = $this->node->getElementsByTagName('ideEmpregador')->item(0);
66
        $ideEvento = $this->dom->createElement("ideEvento");
67
        $this->dom->addChild(
68
            $ideEvento,
69
            "indRetif",
70
            $this->std->indretif,
71
            true
72
        );
73
        $this->dom->addChild(
74
            $ideEvento,
75
            "nrRecibo",
76
            ! empty($this->std->nrrecibo) ? $this->std->nrrecibo : null,
77
            false
78
        );
79
        $this->dom->addChild(
80
            $ideEvento,
81
            "tpAmb",
82
            $this->tpAmb,
83
            true
84
        );
85
        $this->dom->addChild(
86
            $ideEvento,
87
            "procEmi",
88
            $this->procEmi,
89
            true
90
        );
91
        $this->dom->addChild(
92
            $ideEvento,
93
            "verProc",
94
            $this->verProc,
95
            true
96
        );
97
        $this->node->insertBefore($ideEvento, $ideEmpregador);
98
99
        $this->tagVinculo();
100
101
        $cat = $this->tagCAT();
102
103
        $localAcidente = $this->tagLocalAcidente($cat);
104
105
        if (!empty($this->std->idelocalacid)) {
106
            $this->tagIdeLocalAcid($localAcidente);
107
        }
108
109
110
111
        foreach ($this->std->parteatingida as $pa) {
112
            $parteAtingida = $this->dom->createElement("parteAtingida");
113
            $this->dom->addChild(
114
                $parteAtingida,
115
                "codParteAting",
116
                $pa->codparteating,
117
                true
118
            );
119
            $this->dom->addChild(
120
                $parteAtingida,
121
                "lateralidade",
122
                $pa->lateralidade,
123
                true
124
            );
125
            $cat->appendChild($parteAtingida);
126
        }
127
128
        foreach ($this->std->agentecausador as $pa) {
129
            $agenteCausador = $this->dom->createElement("agenteCausador");
130
            $this->dom->addChild(
131
                $agenteCausador,
132
                "codAgntCausador",
133
                $pa->codagntcausador,
134
                true
135
            );
136
            $cat->appendChild($agenteCausador);
137
        }
138
        if (!empty($this->std->atestado)) {
139
            $pa = $this->std->atestado;
140
            $atestado = $this->dom->createElement("atestado");
141
            $this->dom->addChild(
142
                $atestado,
143
                "codCNES",
144
                !empty($pa->codcnes) ? $pa->codcnes : null,
145
                false
146
            );
147
            $this->dom->addChild(
148
                $atestado,
149
                "dtAtendimento",
150
                $pa->dtatendimento,
151
                true
152
            );
153
            $this->dom->addChild(
154
                $atestado,
155
                "hrAtendimento",
156
                $pa->hratendimento,
157
                true
158
            );
159
            $this->dom->addChild(
160
                $atestado,
161
                "indInternacao",
162
                $pa->indinternacao,
163
                true
164
            );
165
            $this->dom->addChild(
166
                $atestado,
167
                "durTrat",
168
                $pa->durtrat,
169
                true
170
            );
171
            $this->dom->addChild(
172
                $atestado,
173
                "indAfast",
174
                $pa->indafast,
175
                true
176
            );
177
            $this->dom->addChild(
178
                $atestado,
179
                "dscLesao",
180
                !empty($pa->dsclesao) ? $pa->dsclesao : null,
181
                false
182
            );
183
            $this->dom->addChild(
184
                $atestado,
185
                "dscCompLesao",
186
                !empty($pa->dsccomplesao) ? $pa->dsccomplesao : null,
187
                false
188
            );
189
            $this->dom->addChild(
190
                $atestado,
191
                "diagProvavel",
192
                !empty($pa->diagprovavel) ? $pa->diagprovavel : null,
193
                false
194
            );
195
            $this->dom->addChild(
196
                $atestado,
197
                "codCID",
198
                $pa->codcid,
199
                true
200
            );
201
            $this->dom->addChild(
202
                $atestado,
203
                "observacao",
204
                !empty($pa->observacao) ? $pa->observacao : null,
205
                false
206
            );
207
            $emitente = $this->dom->createElement("emitente");
208
            $this->dom->addChild(
209
                $emitente,
210
                "nmEmit",
211
                $pa->nmemit,
212
                true
213
            );
214
            $this->dom->addChild(
215
                $emitente,
216
                "ideOC",
217
                $pa->ideoc,
218
                true
219
            );
220
            if ($this->layoutStr == 'v02_05_00') {
221
                $this->dom->addChild(
222
                    $emitente,
223
                    "nrOC",
224
                    $pa->nroc,
225
                    true
226
                );
227
            } else {
228
                $this->dom->addChild(
229
                    $emitente,
230
                    "nrOc",
231
                    $pa->nroc,
232
                    true
233
                );
234
            }
235
            $this->dom->addChild(
236
                $emitente,
237
                "ufOC",
238
                !empty($pa->ufoc) ? $pa->ufoc : null,
239
                true
240
            );
241
            $atestado->appendChild($emitente);
242
            $cat->appendChild($atestado);
243
        }
244
        if (!empty($this->std->catorigem)) {
245
            $pa = $this->std->catorigem;
246
            $catOrigem = $this->dom->createElement("catOrigem");
247
            $this->dom->addChild(
248
                $catOrigem,
249
                "dtCatOrig",
250
                !empty($pa->dtcatorig) ? $pa->dtcatorig : null,
251
                false
252
            );
253
            if ($this->layoutStr != 'v02_05_00') {
254
                $this->dom->addChild(
255
                    $catOrigem,
256
                    "nrCatOrig",
257
                    !empty($pa->nrcatorig) ? $pa->nrcatorig : null,
258
                    false
259
                );
260
            } else {
261
                $this->dom->addChild(
262
                    $catOrigem,
263
                    "nrRecCatOrig",
264
                    !empty($pa->nrreccatorig) ? $pa->nrreccatorig : null,
265
                    false
266
                );
267
            }
268
            $cat->appendChild($catOrigem);
269
        }
270
        $this->node->appendChild($cat);
271
        $this->eSocial->appendChild($this->node);
272
        //$this->xml = $this->dom->saveXML($this->eSocial);
273
        $this->sign();
274
    }
275
276
    protected function tagRegistrador(\DOMElement $ideEmpregador)
277
    {
278
        $ideRegistrador = $this->dom->createElement("ideRegistrador");
279
        $this->dom->addChild(
280
            $ideRegistrador,
281
            "tpRegistrador",
282
            $this->std->tpregistrador,
283
            true
284
        );
285
        $this->dom->addChild(
286
            $ideRegistrador,
287
            "tpInsc",
288
            !empty($this->std->tpinsc) ? $this->std->tpinsc : null,
289
            false
290
        );
291
        $this->dom->addChild(
292
            $ideRegistrador,
293
            "nrInsc",
294
            !empty($this->std->nrinsc) ? $this->std->nrinsc : null,
295
            false
296
        );
297
        $this->node->insertBefore($ideRegistrador, $ideEmpregador);
298
    }
299
300
    protected function tagTrabalhador()
301
    {
302
        $ideTrabalhador = $this->dom->createElement("ideTrabalhador");
303
        $this->dom->addChild(
304
            $ideTrabalhador,
305
            "cpfTrab",
306
            $this->std->cpftrab,
307
            true
308
        );
309
        $this->dom->addChild(
310
            $ideTrabalhador,
311
            "nisTrab",
312
            !empty($this->std->nistrab) ? $this->std->nistrab : null,
313
            false
314
        );
315
        $this->node->appendChild($ideTrabalhador);
316
    }
317
318
    protected function tagVinculo()
319
    {
320
        $ideVinculo = $this->dom->createElement("ideVinculo");
321
        $this->dom->addChild(
322
            $ideVinculo,
323
            "cpfTrab",
324
            $this->std->cpftrab,
325
            true
326
        );
327
        $this->dom->addChild(
328
            $ideVinculo,
329
            "nisTrab",
330
            !empty($this->std->nistrab) ? $this->std->nistrab : null,
331
            false
332
        );
333
        $this->dom->addChild(
334
            $ideVinculo,
335
            "matricula",
336
            !empty($this->std->matricula) ? $this->std->matricula : null,
337
            false
338
        );
339
        $this->dom->addChild(
340
            $ideVinculo,
341
            "codCateg",
342
            !empty($this->std->codcateg) ? $this->std->codcateg : null,
343
            false
344
        );
345
        $this->node->appendChild($ideVinculo);
346
    }
347
348
    protected function tagCAT()
349
    {
350
        $cat = $this->dom->createElement("cat");
351
        $this->dom->addChild(
352
            $cat,
353
            "dtAcid",
354
            $this->std->dtacid,
355
            true
356
        );
357
        $this->dom->addChild(
358
            $cat,
359
            "tpAcid",
360
            $this->std->tpacid,
361
            true
362
        );
363
        $this->dom->addChild(
364
            $cat,
365
            "hrAcid",
366
            $this->std->hracid,
367
            true
368
        );
369
        $this->dom->addChild(
370
            $cat,
371
            "hrsTrabAntesAcid",
372
            $this->std->hrstrabantesacid,
373
            true
374
        );
375
        $this->dom->addChild(
376
            $cat,
377
            "tpCat",
378
            $this->std->tpcat,
379
            true
380
        );
381
        $this->dom->addChild(
382
            $cat,
383
            "indCatObito",
384
            $this->std->indcatobito,
385
            true
386
        );
387
        $this->dom->addChild(
388
            $cat,
389
            "dtObito",
390
            !empty($this->std->dtobito) ? $this->std->dtobito : null,
391
            false
392
        );
393
        $this->dom->addChild(
394
            $cat,
395
            "indComunPolicia",
396
            $this->std->indcomunpolicia,
397
            true
398
        );
399
        $this->dom->addChild(
400
            $cat,
401
            "codSitGeradora",
402
            !empty($this->std->codsitgeradora) ? $this->std->codsitgeradora : null,
403
            false
404
        );
405
        $this->dom->addChild(
406
            $cat,
407
            "iniciatCAT",
408
            $this->std->iniciatcat,
409
            true
410
        );
411
        if ($this->layoutStr !== 'v02_04_02') {
412
            $this->dom->addChild(
413
                $cat,
414
                "obsCAT",
415
                !empty($this->std->obscat) ? $this->std->obscat : null,
416
                false
417
            );
418
        } else {
419
            $this->dom->addChild(
420
                $cat,
421
                "observacao",
422
                !empty($this->std->observacao) ? $this->std->observacao : null,
423
                false
424
            );
425
        }
426
        return $cat;
427
    }
428
429
    protected function tagLocalAcidente(\DOMElement &$cat)
430
    {
431
        $localAcidente = $this->dom->createElement("localAcidente");
432
        $this->dom->addChild(
433
            $localAcidente,
434
            "tpLocal",
435
            $this->std->tplocal,
436
            true
437
        );
438
        $this->dom->addChild(
439
            $localAcidente,
440
            "dscLocal",
441
            !empty($this->std->dsclocal) ? $this->std->dsclocal : null,
442
            false
443
        );
444
        $this->dom->addChild(
445
            $localAcidente,
446
            "codAmb",
447
            !empty($this->std->codamb) ? $this->std->codamb : null,
448
            false
449
        );
450
        $this->dom->addChild(
451
            $localAcidente,
452
            "tpLograd",
453
            !empty($this->std->tplograd) ? $this->std->tplograd : null,
454
            false
455
        );
456
        $this->dom->addChild(
457
            $localAcidente,
458
            "dscLograd",
459
            !empty($this->std->dsclograd) ? $this->std->dsclograd : null,
460
            false
461
        );
462
        $this->dom->addChild(
463
            $localAcidente,
464
            "nrLograd",
465
            !empty($this->std->nrlograd) ? $this->std->nrlograd : null,
466
            false
467
        );
468
        $this->dom->addChild(
469
            $localAcidente,
470
            "complemento",
471
            !empty($this->std->complemento) ? $this->std->complemento : null,
472
            false
473
        );
474
        $this->dom->addChild(
475
            $localAcidente,
476
            "bairro",
477
            !empty($this->std->bairro) ? $this->std->bairro : null,
478
            false
479
        );
480
        $this->dom->addChild(
481
            $localAcidente,
482
            "cep",
483
            !empty($this->std->cep) ? $this->std->cep : null,
484
            false
485
        );
486
        $this->dom->addChild(
487
            $localAcidente,
488
            "codMunic",
489
            !empty($this->std->codmunic) ? $this->std->codmunic : null,
490
            false
491
        );
492
        $this->dom->addChild(
493
            $localAcidente,
494
            "uf",
495
            !empty($this->std->uf) ? $this->std->uf : null,
496
            false
497
        );
498
        $this->dom->addChild(
499
            $localAcidente,
500
            "cnpjLocalAcid",
501
            !empty($this->std->cnpjlocalacid) ? $this->std->cnpjlocalacid : null,
502
            false
503
        );
504
        $this->dom->addChild(
505
            $localAcidente,
506
            "pais",
507
            !empty($this->std->pais) ? $this->std->pais : null,
508
            false
509
        );
510
        $this->dom->addChild(
511
            $localAcidente,
512
            "codPostal",
513
            !empty($this->std->codpostal) ? $this->std->codpostal : null,
514
            false
515
        );
516
        $cat->appendChild($localAcidente);
517
518
        return $localAcidente;
519
    }
520
521
    protected function tagIdeLocalAcid(\DOMElement &$cat)
522
    {
523
        $ideLocalAcid = $this->dom->createElement("ideLocalAcid");
524
        $this->dom->addChild(
525
            $ideLocalAcid,
526
            "tpInsc",
527
            $this->std->idelocalacid->tpinsc,
528
            true
529
        );
530
        $this->dom->addChild(
531
            $ideLocalAcid,
532
            "nrInsc",
533
            $this->std->idelocalacid->nrinsc,
534
            true
535
        );
536
        $cat->appendChild($ideLocalAcid);
537
    }
538
}
539