1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NFePHP\ECD; |
4
|
|
|
|
5
|
|
|
use NFePHP\ECD\Common\BlockInterface; |
6
|
|
|
|
7
|
|
|
class ECD |
8
|
|
|
{ |
9
|
|
|
protected $possibles = [ |
10
|
|
|
'block0' => ['class' => Blocks\Block0::class, 'order' => 1], //Abertura, Identificação e Referências |
11
|
|
|
'blockc' => ['class' => Blocks\BlockC::class, 'order' => 2], //Informações Recuperadas da ECD Anterior |
12
|
|
|
'blocki' => ['class' => Blocks\BlockI::class, 'order' => 3], //Lançamentos Contábeis |
13
|
|
|
'blockj' => ['class' => Blocks\BlockJ::class, 'order' => 4], //Demonstrações Contábeis |
14
|
|
|
'blockk' => ['class' => Blocks\BlockK::class, 'order' => 5], //Conglomerados Econômicos |
15
|
|
|
]; |
16
|
|
|
|
17
|
|
|
protected $qtd_lin = 0; |
18
|
|
|
|
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
//todo |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Add |
26
|
|
|
* @param BlockInterface $block |
27
|
|
|
*/ |
28
|
|
|
public function add(BlockInterface $block = null) |
29
|
|
|
{ |
30
|
|
|
if (empty($block)) { |
31
|
|
|
return; |
32
|
|
|
} |
33
|
|
|
$name = strtolower((new \ReflectionClass($block))->getShortName()); |
34
|
|
|
if (key_exists($name, $this->possibles)) { |
35
|
|
|
$this->{$name} = $block->get(); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create a ECD string |
41
|
|
|
*/ |
42
|
|
|
public function get() |
43
|
|
|
{ |
44
|
|
|
$ecd = ''; |
45
|
|
|
$keys = array_keys($this->possibles); |
46
|
|
|
foreach ($keys as $key) { |
47
|
|
|
if (isset($this->$key)) { |
48
|
|
|
$ecd .= $this->$key; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
$ecd .= $this->totalize($ecd); |
52
|
|
|
$ecd = $this->adjustI030J900($ecd); |
53
|
|
|
return $ecd; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Totals blocks contents |
58
|
|
|
* @param string $ecd |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
protected function totalize($ecd) |
62
|
|
|
{ |
63
|
|
|
$tot = ''; |
64
|
|
|
$keys = []; |
65
|
|
|
$aecd = explode("\n", $ecd); |
66
|
|
|
foreach ($aecd as $element) { |
67
|
|
|
$param = explode("|", $element); |
68
|
|
|
if (!empty($param[1])) { |
69
|
|
|
$key = $param[1]; |
70
|
|
|
if (!empty($keys[$key])) { |
71
|
|
|
$keys[$key] += 1; |
72
|
|
|
} else { |
73
|
|
|
$keys[$key] = 1; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
//Inicializa o bloco 9 |
78
|
|
|
$tot .= "|9001|0|\n"; |
79
|
|
|
$n = 0; |
80
|
|
|
foreach ($keys as $key => $value) { |
81
|
|
|
if (!empty($key)) { |
82
|
|
|
$tot .= "|9900|$key|$value|\n"; |
83
|
|
|
$n++; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
$n++; |
87
|
|
|
$tot .= "|9900|9001|1|\n"; |
88
|
|
|
$tot .= "|9900|9900|". ($n+3)."|\n"; |
89
|
|
|
$tot .= "|9900|9990|1|\n"; |
90
|
|
|
$tot .= "|9900|9999|1|\n"; |
91
|
|
|
$tot .= "|9990|". ($n+6) ."|\n"; |
92
|
|
|
$ecd .= $tot; |
93
|
|
|
$n = count(explode("\n", $ecd)); |
94
|
|
|
$tot .= "|9999|$n|\n"; |
95
|
|
|
$this->qtd_lin = $n; |
96
|
|
|
return $tot; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Ajusta os campos qtd_lin dos elementos I030 e J900 |
101
|
|
|
* |
102
|
|
|
* @param string $ecd |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
protected function adjustI030J900($ecd) |
107
|
|
|
{ |
108
|
|
|
$aecd = explode("\n", $ecd); |
109
|
|
|
$i = 0; |
110
|
|
|
foreach ($aecd as $element) { |
111
|
|
|
$param = explode("|", $element); |
112
|
|
|
if (!empty($param[1])) { |
113
|
|
|
if ($param[1] == 'I030') { |
114
|
|
|
$i030 = "|I030" |
115
|
|
|
. "|{$param[2]}" |
116
|
|
|
. "|{$param[3]}" |
117
|
|
|
. "|{$param[4]}" |
118
|
|
|
. "|{$this->qtd_lin}" |
119
|
|
|
. "|{$param[6]}" |
120
|
|
|
. "|{$param[7]}" |
121
|
|
|
. "|{$param[8]}" |
122
|
|
|
. "|{$param[9]}" |
123
|
|
|
. "|{$param[10]}" |
124
|
|
|
. "|{$param[11]}" |
125
|
|
|
. "|{$param[12]}|"; |
126
|
|
|
$aecd[$i] = $i030; |
127
|
|
|
} |
128
|
|
|
if ($param[1] == 'J900') { |
129
|
|
|
$j900 = "|J900" |
130
|
|
|
. "|{$param[2]}" |
131
|
|
|
. "|{$param[3]}" |
132
|
|
|
. "|{$param[4]}" |
133
|
|
|
. "|{$param[5]}" |
134
|
|
|
. "|{$this->qtd_lin}" |
135
|
|
|
. "|{$param[7]}" |
136
|
|
|
. "|{$param[8]}|"; |
137
|
|
|
$aecd[$i] = $j900; |
138
|
|
|
//ultimo campo a ser ajustado pode parar o loop |
139
|
|
|
break; |
140
|
|
|
} |
141
|
|
|
$i++; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
return implode("\n", $aecd); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|