-
Notifications
You must be signed in to change notification settings - Fork 11
/
Init.php
210 lines (184 loc) · 6.87 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
207
208
209
210
<?php
/**
* This file is part of Servicios plugin for FacturaScripts
* Copyright (C) 2020-2024 Carlos Garcia Gomez <[email protected]>
*
* 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\Servicios;
use FacturaScripts\Core\Base\AjaxForms\SalesHeaderHTML;
use FacturaScripts\Core\Base\DataBase;
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
use FacturaScripts\Core\Model\Role;
use FacturaScripts\Core\Model\RoleAccess;
use FacturaScripts\Core\Plugins;
use FacturaScripts\Core\Template\InitClass;
use FacturaScripts\Core\Tools;
use FacturaScripts\Dinamic\Controller\SendTicket;
use FacturaScripts\Dinamic\Lib\ExportManager;
use FacturaScripts\Dinamic\Lib\StockMovementManager;
use FacturaScripts\Dinamic\Lib\Tickets\Service;
use FacturaScripts\Dinamic\Model\AlbaranCliente;
use FacturaScripts\Dinamic\Model\EmailNotification;
use FacturaScripts\Dinamic\Model\FacturaCliente;
use FacturaScripts\Dinamic\Model\PresupuestoCliente;
/**
* Description of Init
*
* @author Carlos Garcia Gomez <[email protected]>
*/
final class Init extends InitClass
{
const ROLE_NAME = 'Servicios';
public function init(): void
{
// extensions
$this->loadExtension(new Extension\Controller\CopyModel());
$this->loadExtension(new Extension\Controller\EditCliente());
// tickets
if (Plugins::isEnabled('Tickets')) {
SendTicket::addFormat(Service::class, 'ServicioAT', 'service');
}
if (Plugins::isEnabled('Randomizer')) {
$this->loadExtension(new Extension\Controller\Randomizer());
}
// export manager
if (Plugins::isEnabled('PlantillasPDF')) {
ExportManager::addOptionModel('PlantillasPDFserviciosExport', 'PDF', 'ServicioAT');
} else {
ExportManager::addOptionModel('PDFserviciosExport', 'PDF', 'ServicioAT');
}
// mod para los documentos de venta
SalesHeaderHTML::addMod(new Mod\SalesHeaderHTMLMod());
// mod y extensión para StockAvanzado
if (Plugins::isEnabled('StockAvanzado')) {
StockMovementManager::addMod(new Mod\StockMovementMod());
$this->loadExtension(new Extension\Model\TrabajoAT());
}
}
public function uninstall(): void
{
}
public function update(): void
{
$this->fixMissingAgents();
$this->fixMissingCustomers();
new Model\EstadoAT();
new Model\MaquinaAT();
new Model\PrioridadAT();
new Model\TipoAT();
new Model\ServicioAT();
new PresupuestoCliente();
new AlbaranCliente();
new FacturaCliente();
$this->setupSettings();
$this->createRoleForPlugin();
$this->updateEmailNotifications();
}
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()) {
// rollback and exit on fail
$dataBase->rollback();
return;
}
}
// checks the role permissions
$nameControllers = ['EditMaquinaAT', 'EditServicioAT', 'ListServicioAT', 'NewServicioAT'];
foreach ($nameControllers as $nameController) {
$roleAccess = new RoleAccess();
$where = [
new DataBaseWhere('codrole', self::ROLE_NAME),
new DataBaseWhere('pagename', $nameController)
];
if ($roleAccess->loadFromCode('', $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 fixMissingAgents(): void
{
// si no existe la tabla, no hacemos nada
$db = new DataBase();
foreach (['serviciosat', 'serviciosat_trabajos'] as $table) {
if (false === $db->tableExists($table)) {
break;
}
$sql = 'UPDATE ' . $table . ' SET codagente = NULL WHERE codagente IS NOT NULL AND codagente NOT IN (SELECT codagente FROM agentes);';
$db->exec($sql);
}
}
private function fixMissingCustomers(): void
{
// si no existe la tabla, no hacemos nada
$db = new DataBase();
if (false === $db->tableExists('serviciosat')) {
return;
}
$db = new DataBase();
$sql = 'UPDATE serviciosat SET codcliente = NULL WHERE codcliente IS NOT NULL AND codcliente NOT IN (SELECT codcliente FROM clientes);';
$db->exec($sql);
}
private function setupSettings(): void
{
$defaults = [
'footertext' => '',
'longnumero' => 6,
'patron' => 'SER{ANYO}-{NUM}',
'workstatus' => 1
];
foreach ($defaults as $key => $value) {
Tools::settings('servicios', $key, $value);
}
Tools::settingsSave();
}
private function updateEmailNotifications(): void
{
$notificationModel = new EmailNotification();
$keys = [
'new-service-assignee', 'new-service-agent', 'new-service-customer',
'new-service-status', 'new-service-user'
];
foreach ($keys as $key) {
if ($notificationModel->loadFromCode($key)) {
continue;
}
$notificationModel->name = $key;
$notificationModel->body = Tools::lang()->trans($key . '-body');
$notificationModel->subject = Tools::lang()->trans($key);
$notificationModel->enabled = false;
$notificationModel->save();
}
}
}