This repository has been archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoCSV.php
290 lines (247 loc) · 10.4 KB
/
toCSV.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/**
* Converte so txt para SQLite
*/
require_once 'vendor/autoload.php';
require 'config.php';
use PTK\FS\Path;
use PTK\FS\Directory;
use PTK\Log\Logger\Logger;
use PTK\DataFrame\DataFrame;
use PTK\Log\Writer\CLImateWriter;
use PTK\DataFrame\Reader\CSVReader;
use PTK\Log\Formatter\CLImateFormatter;
use IDDRS\SIAPC\PAD\Converter\Writer\CSVWriter;
use IDDRS\SIAPC\PAD\Converter\Reader\InputReader;
use PTK\DataFrame\Writer\CSVWriter as CSVWriter2;
use IDDRS\SIAPC\PAD\Converter\Processor\Processor;
use IDDRS\SIAPC\PAD\Converter\Parser\ParserFactory;
use IDDRS\SIAPC\PAD\Converter\Exception\ErrorException;
use IDDRS\SIAPC\PAD\Converter\Assembler\BalRecAltAssembler;
use IDDRS\SIAPC\PAD\Converter\Assembler\PagamentoAssembler;
use IDDRS\SIAPC\PAD\Converter\Assembler\RecAntAltAssembler;
use IDDRS\SIAPC\PAD\Converter\Assembler\BrecAntAltAssembler;
use IDDRS\SIAPC\PAD\Converter\Assembler\LiquidacaoAssembler;
use IDDRS\SIAPC\PAD\Converter\Assembler\ReceitaAltAssembler;
use IDDRS\SIAPC\PAD\Converter\Assembler\RestosAPagarAssembler;
try {
$defaultWriter = new CLImateWriter();
$defaultFormatter = new CLImateFormatter();
$logger = new Logger($defaultWriter, $defaultFormatter);
} catch (Exception $ex) {
echo $ex->getMessage();
exit($ex->getCode());
}
try {
if (file_exists($outputCSV)) {
$logger->info('Apagando conteúdo original...');
$oCSV = new Directory($outputCSV);
$oCSV->recursive()->delete();
}
} catch (Exception $ex) {
$logger->notice($ex->getTraceAsString());
}
try {
$reader = new InputReader(...$inputDir);
$writer = new CSVWriter($outputCSV);
$parserFactory = new ParserFactory();
$processor = new Processor($reader, $writer, $parserFactory, $logger);
$processor->convert();
} catch (Exception $ex) {
$logger->emergency($ex->getTraceAsString());
exit($ex->getCode());
}
try {
$logger->info('Gerando arquivo LIQUIDACAO...');
$empenhoPath = new Path($outputCSV, 'EMPENHO.csv');
$empenhoHandle = fopen($empenhoPath->getRealPath(), 'r');
if ($empenhoHandle === false) {
throw new ErrorException("Falha ao abrir {$empenhoPath->getPath()}");
}
$empenho = new DataFrame(new CSVReader($empenhoHandle, ';', true));
$liquidacPath = new Path($outputCSV, 'LIQUIDAC.csv');
$liquidacHandle = fopen($liquidacPath->getRealPath(), 'r');
if ($liquidacHandle === false) {
throw new ErrorException("Falha ao abrir {$liquidacPath->getPath()}");
}
$liquidac = new DataFrame(new CSVReader($liquidacHandle, ';', true));
$liquidacaoAssembler = new LiquidacaoAssembler($liquidac, $empenho);
$dfLiquidacao = $liquidacaoAssembler->assemble();
$liquidacaoOutput = new Path($outputCSV, 'LIQUIDACAO.csv');
$liquidacaoHandle = fopen($liquidacaoOutput->getPath(), 'w');
if ($liquidacaoHandle === false) {
throw new ErrorException("Falha ao abrir {$liquidacaoOutput->getPath()}");
}
$liquidacaoWriter = new CSVWriter2($dfLiquidacao, $liquidacaoHandle, ';', true);
$liquidacaoWriter->write();
} catch (Exception $ex) {
$logger->emergency($ex->getTraceAsString());
exit($ex->getCode());
}
try {
$logger->info('Gerando arquivo PAGAMENTO...');
$empenhoPath = new Path($outputCSV, 'EMPENHO.csv');
$empenhoHandle = fopen($empenhoPath->getRealPath(), 'r');
if ($empenhoHandle === false) {
throw new ErrorException("Falha ao abrir {$empenhoPath->getPath()}");
}
$empenho = new DataFrame(new CSVReader($empenhoHandle, ';', true));
$pagamentPath = new Path($outputCSV, 'PAGAMENT.csv');
$pagamentHandle = fopen($pagamentPath->getRealPath(), 'r');
if ($pagamentHandle === false) {
throw new ErrorException("Falha ao abrir {$pagamentPath->getPath()}");
}
$pagament = new DataFrame(new CSVReader($pagamentHandle, ';', true));
$pagamentoAssembler = new PagamentoAssembler($pagament, $empenho);
$dfPagamento = $pagamentoAssembler->assemble();
$pagamentoOutput = new Path($outputCSV, 'PAGAMENTO.csv');
$pagamentoHandle = fopen($pagamentoOutput->getPath(), 'w');
if ($pagamentoHandle === false) {
throw new ErrorException("Falha ao abrir {$pagamentoOutput->getPath()}");
}
$pagamentoWriter = new CSVWriter2($dfPagamento, $pagamentoHandle, ';', true);
$pagamentoWriter->write();
} catch (Exception $ex) {
$logger->emergency($ex->getTraceAsString());
exit($ex->getCode());
}
try {
$logger->info('Gerando arquivo RESTOS_PAGAR...');
$empenhoPath = new Path($outputCSV, 'EMPENHO.csv');
$empenhoHandle = fopen($empenhoPath->getRealPath(), 'r');
if ($empenhoHandle === false) {
throw new ErrorException("Falha ao abrir {$empenhoPath->getPath()}");
}
$empenho = new DataFrame(new CSVReader($empenhoHandle, ';', true));
$liquidacPath = new Path($outputCSV, 'LIQUIDAC.csv');
$liquidacHandle = fopen($liquidacPath->getRealPath(), 'r');
if ($liquidacHandle === false) {
throw new ErrorException("Falha ao abrir {$liquidacPath->getPath()}");
}
$liquidac = new DataFrame(new CSVReader($liquidacHandle, ';', true));
$pagamentPath = new Path($outputCSV, 'PAGAMENT.csv');
$pagamentHandle = fopen($pagamentPath->getRealPath(), 'r');
if ($pagamentHandle === false) {
throw new ErrorException("Falha ao abrir {$pagamentPath->getPath()}");
}
$pagament = new DataFrame(new CSVReader($pagamentHandle, ';', true));
$rpAssembler = new RestosAPagarAssembler($empenho, $liquidac, $pagament);
$dfRP = $rpAssembler->assemble();
$colTypes = $dfRP->getColTypes();
foreach($colTypes as $colName => $type){
switch($type){
case 'double':
case 'float':
$dfRP->applyOnCols($colName, function($cell){
return number_format($cell, 2, ',', '.');
});
break;
}
}
$rpOutput = new Path($outputCSV, 'RESTOS_PAGAR.csv');
$rpHandle = fopen($rpOutput->getPath(), 'w');
if ($rpHandle === false) {
throw new ErrorException("Falha ao abrir {$rpOutput->getPath()}");
}
$rpWriter = new CSVWriter2($dfRP, $rpHandle, ';', true);
$rpWriter->write();
} catch (Exception $ex) {
$logger->emergency($ex->getTraceAsString());
exit($ex->getCode());
}
try {
$logger->info('Gerando arquivo BAL_REC_ALT...');
$balRecPath = new Path($outputCSV, 'BAL_REC.csv');
$balRecHandle = fopen($balRecPath->getRealPath(), 'r');
if ($balRecHandle === false) {
throw new ErrorException("Falha ao abrir {$balRecPath->getPath()}");
}
$balRec = new DataFrame(new CSVReader($balRecHandle, ';', true));
$balRecAltAssembler = new BalRecAltAssembler($balRec);
$dfBalRecAlt = $balRecAltAssembler->assemble();
$balRecAltOutput = new Path($outputCSV, 'BAL_REC_ALT.csv');
$balRecAltHandle = fopen($balRecAltOutput->getPath(), 'w');
if ($balRecAltHandle === false) {
throw new ErrorException("Falha ao abrir {$balRecAltOutput->getPath()}");
}
$balRecAltWriter = new CSVWriter2($dfBalRecAlt, $balRecAltHandle, ';', true);
$balRecAltWriter->write();
} catch (Exception $ex) {
$logger->emergency($ex->getTraceAsString());
exit($ex->getCode());
}
try {
$logger->info('Gerando arquivo BREC_ANT_ALT...');
$brecAntPath = new Path($outputCSV, 'BREC_ANT.csv');
$brecAntHandle = fopen($brecAntPath->getRealPath(), 'r');
if ($brecAntHandle === false) {
throw new ErrorException("Falha ao abrir {$brecAntPath->getPath()}");
}
$brecAnt = new DataFrame(new CSVReader($brecAntHandle, ';', true));
$brecAntAltAssembler = new BrecAntAltAssembler($brecAnt);
$dfBrecAntAlt = $brecAntAltAssembler->assemble();
$brecAntAltOutput = new Path($outputCSV, 'BREC_ANT_ALT.csv');
$brecAntAltHandle = fopen($brecAntAltOutput->getPath(), 'w');
if ($brecAntAltHandle === false) {
throw new ErrorException("Falha ao abrir {$brecAntAltOutput->getPath()}");
}
$brecAntAltWriter = new CSVWriter2($dfBrecAntAlt, $brecAntAltHandle, ';', true);
$brecAntAltWriter->write();
} catch (Exception $ex) {
$logger->emergency($ex->getTraceAsString());
exit($ex->getCode());
}
try {
$logger->info('Gerando arquivo RECEITA_ALT...');
$receitaPath = new Path($outputCSV, 'RECEITA.csv');
$receitaHandle = fopen($receitaPath->getRealPath(), 'r');
if ($receitaHandle === false) {
throw new ErrorException("Falha ao abrir {$receitaPath->getPath()}");
}
$receita = new DataFrame(new CSVReader($receitaHandle, ';', true));
$receitaAltAssembler = new ReceitaAltAssembler($receita);
$dfReceitaAlt = $receitaAltAssembler->assemble();
$receitaAltOutput = new Path($outputCSV, 'RECEITA_ALT.csv');
$receitaAltHandle = fopen($receitaAltOutput->getPath(), 'w');
if ($receitaAltHandle === false) {
throw new ErrorException("Falha ao abrir {$receitaAltOutput->getPath()}");
}
$receitaAltWriter = new CSVWriter2($dfReceitaAlt, $receitaAltHandle, ';', true);
$receitaAltWriter->write();
} catch (Exception $ex) {
$logger->emergency($ex->getTraceAsString());
exit($ex->getCode());
}
try {
$logger->info('Gerando arquivo REC_ANT_ALT...');
$recAntPath = new Path($outputCSV, 'REC_ANT.csv');
$recAntHandle = fopen($recAntPath->getRealPath(), 'r');
if ($recAntHandle === false) {
throw new ErrorException("Falha ao abrir {$recAntPath->getPath()}");
}
$recAnt = new DataFrame(new CSVReader($recAntHandle, ';', true));
$recAntAltAssembler = new RecAntAltAssembler($recAnt);
$dfRecAntAlt = $recAntAltAssembler->assemble();
$recAntAltOutput = new Path($outputCSV, 'REC_ANT_ALT.csv');
$recAntAltHandle = fopen($recAntAltOutput->getPath(), 'w');
if ($recAntAltHandle === false) {
throw new ErrorException("Falha ao abrir {$recAntAltOutput->getPath()}");
}
$recAntAltWriter = new CSVWriter2($dfRecAntAlt, $recAntAltHandle, ';', true);
$recAntAltWriter->write();
} catch (Exception $ex) {
$logger->emergency($ex->getTraceAsString());
exit($ex->getCode());
}
try {
$logger->info('Apagando conteúdo de latest...');
$latestPath = new Path(dirname($outputCSV), 'latest');
$latestDir = new Directory($latestPath->getPath());
$latestDir->delete();
$logger->info('Copiando conteúdo para latest...');
$output = new Directory($outputCSV);
$output->copy($latestPath->getPath());
} catch (Exception $ex) {
$logger->error($ex->getTraceAsString());
exit($ex->getCode());
}