-
Notifications
You must be signed in to change notification settings - Fork 7
/
Init.php
206 lines (180 loc) · 6.97 KB
/
Init.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
<?php
/**
* This file is part of Informes plugin for FacturaScripts
* Copyright (C) 2022-2024 Carlos Garcia Gomez <carlos@facturascripts.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace FacturaScripts\Plugins\Informes;
use FacturaScripts\Core\Base\DataBase;
use FacturaScripts\Core\Template\InitClass;
use FacturaScripts\Core\Tools;
use FacturaScripts\Dinamic\Model\Empresa;
use FacturaScripts\Plugins\Informes\Model\BalanceAccount;
use FacturaScripts\Plugins\Informes\Model\BalanceCode;
use ParseCsv\Csv;
final class Init extends InitClass
{
public function init(): void
{
$this->loadExtension(new Extension\Controller\EditCuenta());
}
public function uninstall(): void
{
}
public function update(): void
{
// inicializamos empresa para que aplique los cambios en la tabla
new Empresa();
// migramos los datos antiguos
$this->migrateOldBalances();
$this->migrateOldReports();
}
private function copyBalancePymes(): bool
{
// abrimos el csv balance_pymes.csv
$csv = new Csv();
$csv->auto(FS_FOLDER . '/Plugins/Informes/Data/Other/balance_pymes.csv');
if (empty($csv->data)) {
return false;
}
foreach ($csv->data as $row) {
$balanceCode = new BalanceCode();
$balanceCode->codbalance = $row['codbalance'];
$balanceCode->description1 = $row['description1'];
$balanceCode->description2 = $row['description2'];
$balanceCode->description3 = $row['description3'];
$balanceCode->description4 = $row['description4'];
$balanceCode->level1 = $row['level1'];
$balanceCode->level2 = $row['level2'];
$balanceCode->level3 = $row['level3'];
$balanceCode->level4 = $row['level4'];
$balanceCode->nature = $row['nature'];
$balanceCode->subtype = 'pymes';
if (false === $balanceCode->save()) {
return false;
}
// copiamos las cuentas
$accounts = explode(',', $row['accounts']);
foreach ($accounts as $account) {
$balanceAccount = new BalanceAccount();
$balanceAccount->idbalance = $balanceCode->id;
$balanceAccount->codcuenta = trim($account);
if (false === $balanceAccount->save()) {
return false;
}
}
}
return true;
}
private function migrateOldBalances(): void
{
$db = new DataBase();
if (false === $db->tableExists('balances')) {
//$this->copyOldBalances($db);
return;
}
// inicializamos los modelos para que se creen las tablas
new BalanceCode();
new BalanceAccount();
$db->beginTransaction();
// eliminamos datos de las tablas
$db->exec('DELETE FROM balance_accounts;');
$db->exec('DELETE FROM balance_codes;');
foreach ($db->select('SELECT * FROM balances;') as $row) {
// copiamos el balance normal
$balance = new BalanceCode();
$balance->codbalance = $row['codbalance'];
$balance->description1 = $row['descripcion1'];
$balance->description2 = $row['descripcion2'];
$balance->description3 = $row['descripcion3'];
$balance->description4 = $row['descripcion4'];
$balance->nature = $row['naturaleza'];
$balance->level1 = $row['nivel1'];
$balance->level2 = $row['nivel2'];
$balance->level3 = $row['nivel3'];
$balance->level4 = $row['nivel4'];
$balance->subtype = 'normal';
if (false === $balance->save()) {
Tools::log()->warning('balance-code-save-error');
$db->rollback();
return;
}
// copiamos las cuentas
if (false === $this->migrateOldBalanceAccounts($balance, $db, 'balancescuentas')) {
$db->rollback();
return;
}
// copiamos el balance abreviado
$balanceAbr = clone $balance;
$balanceAbr->id = null;
$balanceAbr->subtype = 'abbreviated';
if (false === $balanceAbr->save()) {
Tools::log()->warning('balance-code-save-error');
$db->rollback();
return;
}
// copiamos las cuentas
if (false === $this->migrateOldBalanceAccounts($balanceAbr, $db, 'balancescuentasabreviadas')) {
$db->rollback();
return;
}
}
if (false === $this->copyBalancePymes()) {
$db->rollback();
return;
}
$db->commit();
// eliminamos las tablas balances, balancescuentas y balancescuentasabreviadas
$db->exec('DROP TABLE balancescuentas;');
$db->exec('DROP TABLE balancescuentasabreviadas;');
$db->exec('DROP TABLE balances;');
}
private function migrateOldBalanceAccounts(BalanceCode $balanceCode, DataBase $db, string $tableName): bool
{
if (false === $db->tableExists($tableName)) {
return true;
}
$sql = 'SELECT * FROM ' . $tableName . ' WHERE codbalance = ' . $db->var2str($balanceCode->codbalance) . ';';
foreach ($db->select($sql) as $row) {
$balanceAccount = new BalanceAccount();
$balanceAccount->idbalance = $balanceCode->id;
$balanceAccount->codcuenta = $row['codcuenta'];
if (false === $balanceAccount->save()) {
return false;
}
}
return true;
}
private function migrateOldReports(): void
{
$db = new DataBase();
$tables = [
'reportsamounts' => 'reports_amounts',
'reportsbalance' => 'reports_balance',
'reportsledger' => 'reports_ledger',
];
foreach ($tables as $before => $after) {
if (false === $db->tableExists($before) || $db->tableExists($after)) {
continue;
}
// renombramos la tabla
$sql = 'ALTER TABLE ' . $before . ' RENAME TO ' . $after . ';';
if (false === $db->exec($sql)) {
Tools::log()->warning('rename-table-error');
return;
}
}
}
}