Repository navigation
Expand file tree
/
Copy pathInit.php
More file actions
112 lines (99 loc) · 3.93 KB
/
Copy pathInit.php
File metadata and controls
112 lines (99 loc) · 3.93 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
<?php
/**
* This file is part of Modelo347 plugin for FacturaScripts
* Copyright (C) 2017-2026 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\Modelo347;
use FacturaScripts\Core\Controller\ApiRoot;
use FacturaScripts\Core\Kernel;
use FacturaScripts\Core\Lib\AjaxForms\PurchasesHeaderHTML;
use FacturaScripts\Core\Lib\AjaxForms\SalesHeaderHTML;
use FacturaScripts\Core\Template\InitClass;
use FacturaScripts\Core\Tools;
use FacturaScripts\Core\WorkQueue;
use FacturaScripts\Dinamic\Model\EmailNotification;
use FacturaScripts\Dinamic\Model\FacturaCliente;
use FacturaScripts\Dinamic\Model\FacturaProveedor;
/**
* Description of Init
*
* @author Jose Antonio Cuello Principal <[email protected]>
*/
final class Init extends InitClass
{
/**
* Carga las extensiones, mods y workers del plugin.
*/
public function init(): void
{
// extensiones
$this->loadExtension(new Extension\Lib\BusinessDocumentGenerator());
$this->loadExtension(new Extension\Model\Cliente());
$this->loadExtension(new Extension\Model\FacturaCliente());
$this->loadExtension(new Extension\Model\FacturaProveedor());
$this->loadExtension(new Extension\Model\Proveedor());
// mods
PurchasesHeaderHTML::addMod(new Mod\PurchasesHeaderHTMLMod());
SalesHeaderHTML::addMod(new Mod\SalesHeaderHTMLMod());
// workers
// un único worker envía los emails a clientes y a proveedores, un destinatario por evento
WorkQueue::addWorker('Modelo347EmailWorker', 'Modelo347.SendCustomersEmails');
WorkQueue::addWorker('Modelo347EmailWorker', 'Modelo347.SendSuppliersEmails');
// endpoint de solo lectura de la API con los datos del modelo
Kernel::addRoute('/api/3/modelo347', 'ApiModelo347', -1);
ApiRoot::addCustomResource('modelo347');
}
public function uninstall(): void
{
}
/**
* Crea las columnas del plugin y las notificaciones por email.
*/
public function update(): void
{
new FacturaCliente();
new FacturaProveedor();
$this->createEmailNotifications();
}
/**
* Crea las notificaciones de la carta del modelo 347 para clientes y proveedores.
* Si ya existen, solo reescribe el asunto o el cuerpo vacíos o iguales a su clave de traducción.
*/
private function createEmailNotifications(): void
{
foreach (['modelo347-customers', 'modelo347-suppliers'] as $name) {
$subjectKey = $name . '-subject';
$bodyKey = $name . '-body';
$notification = new EmailNotification();
if (false === $notification->load($name)) {
$notification->name = $name;
$notification->enabled = true;
}
$changed = false;
if (empty($notification->subject) || $notification->subject === $subjectKey) {
$notification->subject = Tools::trans($subjectKey);
$changed = true;
}
if (empty($notification->body) || $notification->body === $bodyKey) {
$notification->body = Tools::trans($bodyKey);
$changed = true;
}
if ($changed) {
$notification->save();
}
}
}
}