-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathInit.php
More file actions
219 lines (191 loc) · 7.52 KB
/
Init.php
File metadata and controls
219 lines (191 loc) · 7.52 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
<?php
/**
* This file is part of StockAvanzado plugin for FacturaScripts
* Copyright (C) 2020-2026 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\Controller\ApiRoot;
use FacturaScripts\Core\Kernel;
use FacturaScripts\Core\Model\Role;
use FacturaScripts\Core\Model\RoleAccess;
use FacturaScripts\Core\Template\InitClass;
use FacturaScripts\Core\Where;
use FacturaScripts\Core\WorkQueue;
use FacturaScripts\Dinamic\Lib\StockMinMaxManager;
use FacturaScripts\Dinamic\Model\ConteoStock;
use FacturaScripts\Dinamic\Model\EmailNotification;
use FacturaScripts\Dinamic\Model\LineaConteoStock;
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';
/** @var DataBase */
private $db;
public function init(): void
{
// extensiones
$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\Lib\BusinessDocumentGenerator());
$this->loadExtension(new Extension\Model\Stock());
$this->loadExtension(new Extension\Model\Base\BusinessDocumentLine());
// API
Kernel::addRoute('/api/3/counting-execute', 'ApiCountingExecute', -1);
Kernel::addRoute('/api/3/transfer-execute', 'ApiTransferExecute', -1);
ApiRoot::addCustomResource('counting-execute');
ApiRoot::addCustomResource('transfer-execute');
// workers
WorkQueue::addWorker('RebuildStockMovements', 'Model.Producto.rebuildStockMovements');
WorkQueue::addWorker('UpdateStockMovements', 'Model.Producto.updateStockMovements');
}
public function uninstall(): void
{
}
public function update(): void
{
$this->unlinkUsers();
new MovimientoStock();
new ConteoStock();
new LineaConteoStock();
new TransferenciaStock();
new LineaTransferenciaStock();
$this->createRoleForPlugin();
$this->updateEmailNotifications();
$this->migrateData();
}
private function createRoleForPlugin(): void
{
// force table checks for Role and RoleAccess
new Role();
new RoleAccess();
$this->db()->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()) {
return;
}
}
// checks the role permissions (only plugin's own controllers)
$nameControllers = [
'ApiCountingExecute', 'ApiTransferExecute', 'EditConteoStock',
'EditTransferenciaStock', 'ReportStock'
];
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
$this->db()->rollback();
return;
}
}
$this->db()->commit();
}
protected function db(): DataBase
{
if ($this->db === null) {
$this->db = new DataBase();
$this->db->connect();
}
return $this->db;
}
/**
* Migración de datos de la versión 2017
* @return void
*/
private function migrateData(): void
{
if (false === $this->db()->tableExists('transferenciasstock')) {
return;
}
foreach ($this->db()->select('SELECT * FROM transferenciasstock') as $row) {
$trans = new TransferenciaStock($row);
$trans->completed = true;
if (false === $trans->save()) {
return;
}
}
if (false === $this->db()->tableExists('lineastransferenciasstock')) {
$this->db()->exec('DROP TABLE transferenciasstock;');
return;
}
foreach ($this->db()->select('SELECT * FROM lineastransferenciasstock') as $row) {
$line = new LineaTransferenciaStock($row);
if (false === $line->save()) {
return;
}
}
$this->db()->exec('DROP TABLE lineastransferenciasstock;');
$this->db()->exec('DROP TABLE transferenciasstock;');
}
private function unlinkUsers(): void
{
$tables = ['stocks_conteos', 'stocks_lineasconteos', 'stocks_transferencias'];
foreach ($tables as $table) {
if (false === $this->db()->tableExists($table)) {
continue;
}
$sqlUnlink = 'update ' . $table . ' set nick = null'
. ' where nick is not null and nick not in (select nick from users)';
$this->db()->exec($sqlUnlink);
}
}
private function updateEmailNotifications(): void
{
$notificationMax = new EmailNotification();
if (!$notificationMax->load(StockMinMaxManager::NOTIFICATION_STOCK_MAX)) {
$notificationMax->name = StockMinMaxManager::NOTIFICATION_STOCK_MAX;
$notificationMax->body = "Hola {nick}.\n\nSe ha alcanzado el stock máximo de los siguientes productos.";
$notificationMax->subject = 'Stock máximo alcanzado';
$notificationMax->enabled = true;
$notificationMax->save();
}
$notificationMin = new EmailNotification();
if (!$notificationMin->load(StockMinMaxManager::NOTIFICATION_STOCK_MIN)) {
$notificationMin->name = StockMinMaxManager::NOTIFICATION_STOCK_MIN;
$notificationMin->body = "Hola {nick}.\n\nSe ha alcanzado el stock mínimo de los siguientes productos.";
$notificationMin->subject = 'Stock mínimo alcanzado';
$notificationMin->enabled = true;
$notificationMin->save();
}
}
}