-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathInit.php
More file actions
274 lines (240 loc) · 9.43 KB
/
Init.php
File metadata and controls
274 lines (240 loc) · 9.43 KB
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
<?php
/**
* This file is part of Informes plugin for FacturaScripts
* Copyright (C) 2022-2025 Carlos García Gómez <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\Core\Where;
use FacturaScripts\Core\Model\Role;
use FacturaScripts\Core\Model\RoleAccess;
use FacturaScripts\Dinamic\Model\BalanceAccount;
use FacturaScripts\Dinamic\Model\BalanceCode;
use FacturaScripts\Dinamic\Model\Empresa;
use ParseCsv\Csv;
final class Init extends InitClass
{
public function init(): void
{
$this->loadExtension(new Extension\Controller\EditAgente());
$this->loadExtension(new Extension\Controller\EditCliente());
$this->loadExtension(new Extension\Controller\EditCuenta());
$this->loadExtension(new Extension\Controller\EditProveedor());
$this->loadExtension(new Extension\Controller\EditUser());
$this->loadExtension(new Extension\Controller\Dashboard());
}
public function uninstall(): void
{
}
const ROLE_NAME = 'Informes';
public function update(): void
{
// inicializamos empresa para que aplique los cambios en la tabla
new Empresa();
// migramos los datos antiguos
$this->migrateOldBalances();
$this->migrateOldReports();
// crea el role y permisos del plugin
$this->createRoleForPlugin();
}
private function createRoleForPlugin(): void
{
// force table checks for Role and RoleAccess
new Role();
new RoleAccess();
$dataBase = new DataBase();
$dataBase->beginTransaction();
// creates the role if not exists
$role = new Role();
if (false === $role->load(self::ROLE_NAME)) {
$role->codrole = $role->descripcion = self::ROLE_NAME;
if (false === $role->save()) {
// rollback and exit on fail
$dataBase->rollback();
return;
}
}
// checks the role permissions
$nameControllers = [
'ReportTreasury','ReportTransport','ReportTaxes','ReportResult',
'ReportProducto','ReportClients','ReportContacts','ReportBreakdown','ReportBooks',
'ListReportAccounting','ListReport',
'EditReportLedger','EditReportBoard','EditReportBalance','EditReportAmount','EditReport','EditBalanceCode'
];
foreach ($nameControllers as $nameController) {
$roleAccess = new RoleAccess();
$where = [
Where::eq('codrole', self::ROLE_NAME),
Where::eq('pagename', $nameController)
];
if ($roleAccess->loadWhere($where)) {
// permission exists? Then skip
continue;
}
// creates the permission if not exists
$roleAccess->allowdelete = true;
$roleAccess->allowupdate = true;
$roleAccess->codrole = self::ROLE_NAME;
$roleAccess->pagename = $nameController;
$roleAccess->onlyownerdata = false;
if (false === $roleAccess->save()) {
// rollback and exit on fail
$dataBase->rollback();
return;
}
}
// without problems = Commit
$dataBase->commit();
}
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;
}
}
}
}