-
Notifications
You must be signed in to change notification settings - Fork 8
/
Init.php
154 lines (134 loc) · 5.08 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
<?php
/**
* This file is part of StockAvanzado plugin for FacturaScripts
* Copyright (C) 2020-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\StockAvanzado;
use FacturaScripts\Core\Base\DataBase;
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
use FacturaScripts\Core\Model\Role;
use FacturaScripts\Core\Model\RoleAccess;
use FacturaScripts\Core\Template\InitClass;
use FacturaScripts\Dinamic\Model\LineaTransferenciaStock;
use FacturaScripts\Dinamic\Model\MovimientoStock;
use FacturaScripts\Dinamic\Model\TransferenciaStock;
/**
* Description of Init
*
* @author Carlos Garcia Gomez <carlos@facturascripts.com>
*/
class Init extends InitClass
{
const ROLE_NAME = 'StockAvanzado';
public function init(): void
{
$this->loadExtension(new Extension\Controller\EditAlmacen());
$this->loadExtension(new Extension\Controller\EditProducto());
$this->loadExtension(new Extension\Controller\ListAlmacen());
$this->loadExtension(new Extension\Controller\ListProducto());
$this->loadExtension(new Extension\Controller\ReportProducto());
$this->loadExtension(new Extension\Model\Base\BusinessDocumentLine());
}
public function uninstall(): void
{
}
public function update(): void
{
new MovimientoStock();
$this->createRoleForPlugin();
$this->migrateData();
$this->unlinkUsers();
}
private function createRoleForPlugin(): void
{
$dataBase = new DataBase();
$dataBase->beginTransaction();
// creates the role if not exists
$role = new Role();
if (false === $role->loadFromCode(self::ROLE_NAME)) {
$role->codrole = $role->descripcion = self::ROLE_NAME;
if (false === $role->save()) {
// exit and rollback on fail
$dataBase->rollback();
return;
}
}
// check the role permissions
$controllerNames = ['EditConteoStock', 'EditTransferenciaStock', 'ReportStock'];
foreach ($controllerNames as $controllerName) {
$roleAccess = new RoleAccess();
$where = [
new DataBaseWhere('codrole', self::ROLE_NAME),
new DataBaseWhere('pagename', $controllerName)
];
if ($roleAccess->loadFromCode('', $where)) {
continue;
}
// creates the permission if not exists
$roleAccess->allowdelete = true;
$roleAccess->allowupdate = true;
$roleAccess->codrole = self::ROLE_NAME;
$roleAccess->pagename = $controllerName;
$roleAccess->onlyownerdata = false;
if (false === $roleAccess->save()) {
// exit and rollback on fail
$dataBase->rollback();
return;
}
}
// commit if there is no problem
$dataBase->commit();
}
private function migrateData(): void
{
$database = new DataBase();
if (false === $database->tableExists('transferenciasstock')) {
return;
}
foreach ($database->select('SELECT * FROM transferenciasstock') as $row) {
$trans = new TransferenciaStock($row);
if (false === $trans->save()) {
return;
}
}
if (false === $database->tableExists('lineastransferenciasstock')) {
$database->exec('DROP TABLE transferenciasstock;');
return;
}
LineaTransferenciaStock::setDisableUpdateStock(true);
foreach ($database->select('SELECT * FROM lineastransferenciasstock') as $row) {
$line = new LineaTransferenciaStock($row);
if (false === $line->save()) {
return;
}
}
$database->exec('DROP TABLE lineastransferenciasstock;');
$database->exec('DROP TABLE transferenciasstock;');
}
private function unlinkUsers(): void
{
$database = new DataBase();
$tables = ['stocks_conteos', 'stocks_lineasconteos', 'stocks_transferencias'];
foreach ($tables as $table) {
if (false === $database->tableExists($table)) {
continue;
}
$sqlUnlink = 'update ' . $table . ' set nick = null'
. ' where nick is not null and nick not in (select nick from users)';
$database->exec($sqlUnlink);
}
}
}