-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathInit.php
More file actions
185 lines (167 loc) · 7 KB
/
Init.php
File metadata and controls
185 lines (167 loc) · 7 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
<?php
/**
* This file is part of Proyectos 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\Proyectos;
use FacturaScripts\Core\Lib\AjaxForms\PurchasesHeaderHTML;
use FacturaScripts\Core\Lib\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\Template\InitClass;
use FacturaScripts\Core\Tools;
use FacturaScripts\Core\Where;
use FacturaScripts\Dinamic\Model\AlbaranCliente;
use FacturaScripts\Dinamic\Model\AlbaranProveedor;
use FacturaScripts\Dinamic\Model\EmailNotification;
use FacturaScripts\Dinamic\Model\FacturaCliente;
use FacturaScripts\Dinamic\Model\FacturaProveedor;
use FacturaScripts\Dinamic\Model\PedidoCliente;
use FacturaScripts\Dinamic\Model\PedidoProveedor;
use FacturaScripts\Dinamic\Model\PresupuestoCliente;
use FacturaScripts\Dinamic\Model\PresupuestoProveedor;
use FacturaScripts\Plugins\Proyectos\Model\Proyecto;
use FacturaScripts\Plugins\Proyectos\Model\UserProyecto;
/**
* Description of Init
*
* @author Carlos Garcia Gomez <carlos@facturascripts.com>
*/
final class Init extends InitClass
{
const ROLE_NAME = 'Proyectos';
public function init(): void
{
$this->loadExtension(new Extension\Controller\DocumentStitcher());
$this->loadExtension(new Extension\Controller\EditAlbaranCliente());
$this->loadExtension(new Extension\Controller\EditAlbaranProveedor());
$this->loadExtension(new Extension\Controller\EditCliente());
$this->loadExtension(new Extension\Controller\EditFacturaCliente());
$this->loadExtension(new Extension\Controller\EditFacturaProveedor());
$this->loadExtension(new Extension\Controller\EditPedidoCliente());
$this->loadExtension(new Extension\Controller\EditPedidoProveedor());
$this->loadExtension(new Extension\Controller\EditPresupuestoCliente());
$this->loadExtension(new Extension\Controller\EditPresupuestoProveedor());
$this->loadExtension(new Extension\Controller\EditProducto());
$this->loadExtension(new Extension\Controller\ListAlbaranCliente());
$this->loadExtension(new Extension\Controller\ListAlbaranProveedor());
$this->loadExtension(new Extension\Controller\ListFacturaCliente());
$this->loadExtension(new Extension\Controller\ListFacturaProveedor());
$this->loadExtension(new Extension\Controller\ListPedidoCliente());
$this->loadExtension(new Extension\Controller\ListPedidoProveedor());
$this->loadExtension(new Extension\Controller\ListPresupuestoCliente());
$this->loadExtension(new Extension\Controller\ListPresupuestoProveedor());
$this->loadExtension(new Extension\Controller\NewServicioAT());
$this->loadExtension(new Extension\Model\Base\BusinessDocument());
$this->loadExtension(new Extension\Model\Stock());
$this->loadExtension(new Extension\Model\FacturaProgramada());
PurchasesHeaderHTML::addMod(new Mod\PurchasesHeaderHTMLMod());
SalesHeaderHTML::addMod(new Mod\SalesHeaderHTMLMod());
if (class_exists('FacturaScripts\\Dinamic\\Controller\\Randomizer')) {
$this->loadExtension(new Extension\Controller\Randomizer());
}
}
public function uninstall(): void
{
}
public function update(): void
{
// init models
new UserProyecto();
new Proyecto();
new AlbaranCliente();
new AlbaranProveedor();
new FacturaCliente();
new FacturaProveedor();
new PedidoCliente();
new PedidoProveedor();
new PresupuestoCliente();
new PresupuestoProveedor();
$this->setupSettings();
$this->createRoleForPlugin();
$this->updateEmailNotifications();
}
private function createRoleForPlugin(): void
{
// init models before transaction
$role = new Role();
new RoleAccess();
$db = new DataBase();
$db->beginTransaction();
// creates the role if not exists
if (false === $role->load(self::ROLE_NAME)) {
$role->codrole = $role->descripcion = self::ROLE_NAME;
if (false === $role->save()) {
// exit and rollback on fail
$db->rollback();
return;
}
}
// check the role permissions
$controllerNames = [
'AdminProyectos', 'EditNotaProyecto', 'EditProyecto', 'EditTareaProyecto', 'ListProyecto',
'ListTareaProyecto'
];
foreach ($controllerNames as $controllerName) {
$roleAccess = new RoleAccess();
$where = [
Where::eq('codrole', self::ROLE_NAME),
Where::eq('pagename', $controllerName)
];
if ($roleAccess->loadWhere($where)) {
// permission exists? the skip
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
$db->rollback();
return;
}
}
// without problems = Commit
$db->commit();
}
private function setupSettings(): void
{
Tools::settings('proyectos', 'patron', 'PR-{ANYO}-{NUM}');
Tools::settings('proyectos', 'longnumero', 6);
Tools::settings('proyectos', 'reiniciar_patron_anualmente', 0);
Tools::settingsSave();
}
private function updateEmailNotifications(): void
{
$notificationModel = new EmailNotification();
$keys = ['new-task-user', 'new-task-status'];
foreach ($keys as $key) {
if ($notificationModel->load($key)) {
continue;
}
$notificationModel->name = $key;
$notificationModel->body = Tools::trans($key . '-body');
$notificationModel->subject = Tools::trans($key);
$notificationModel->enabled = false;
$notificationModel->save();
}
}
}