1
0
mirror of https://github.com/devcode-it/openstamanager.git synced 2024-12-23 05:54:03 +01:00
This commit is contained in:
Luca 2019-08-27 16:00:59 +02:00
commit f096a53608
22 changed files with 316 additions and 150 deletions

View File

@ -208,7 +208,7 @@ elseif (filter('op') == 'sort_checks') {
elseif (post('op') == 'send-email') {
$template = MailTemplate::find(post('template'));
$mail = \Models\Mail::build($template, $id_record);
$mail = \Models\Mail::build($user, $template, $id_record);
// Rimozione allegati predefiniti
$mail->resetAttachments();

View File

@ -108,17 +108,12 @@ switch (get('op')) {
$hook_id = filter('id');
$hook = Hook::find($hook_id);
$response = $hook->execute();
echo json_encode($response);
break;
case 'prepare-hook':
$hook_id = filter('id');
$hook = Hook::find($hook_id);
$response = $hook->prepare();
$init = filter('init');
if (!empty($init)) {
$response = $hook->prepare();
} else {
$response = $hook->execute();
}
echo json_encode($response);

View File

@ -8,7 +8,7 @@ function startHooks() {
data: {
op: "hooks",
},
success: function(data) {
success: function (data) {
hooks = JSON.parse(data);
$("#hooks-header").text(globals.translations.hooksExecuting);
@ -20,89 +20,66 @@ function startHooks() {
$("#hooks-header").text(globals.translations.hookNone);
}
hooks.forEach(function(item, index){
startHook(item);
hooks.forEach(function (item, index) {
renderHook(item, {
show: true,
message: globals.translations.hookExecuting.replace('_NAME_', item.name)
});
executeHook(item, true);
});
},
});
}
/**
*
* @param hook
*/
function startHook(hook){
var element_id = "hook-" + hook.id;
$("#hooks").append('<li class="hook-element" id="' + element_id + '"><a href="#">' + globals.translations.hookExecuting.replace('_NAME_', hook.name) + '</a></li>');
element_id = "#" + element_id;
$.ajax({
url: globals.rootdir + "/ajax.php",
type: "get",
data: {
op: "prepare-hook",
id: hook.id,
},
success: function(data) {
var result = JSON.parse(data);
addHookCount("#hooks-counter");
if (result){
renderHook(element_id, result);
if (result.execute){
addHookCount("#hooks-notified");
executeHook(hook, element_id, true)
} else {
$(element_id).remove();
}
} else {
executeHook(hook, element_id)
}
},
});
}
/**
*
* @param hook
* @param element_id
*/
function executeHook(hook, element_id, is_background){
function executeHook(hook, init) {
$.ajax({
url: globals.rootdir + "/ajax.php",
type: "get",
data: {
op: "hook",
id: hook.id,
init: init,
},
success: function(data) {
success: function (data) {
var result = JSON.parse(data);
renderHook(element_id, result);
renderHook(hook, result);
if (!is_background) {
if (result.notify) {
addHookCount("#hooks-notified");
} else {
$(element_id).remove();
}
var timeout;
if (result.execute) {
timeout = 1;
} else {
timeout = 30;
}
setTimeout(function () {
executeHook(hook);
}, timeout * 1000);
if (init) {
hookCount("#hooks-counter");
}
// Rimozione eventuale della rotella di caricamento
var counter = $("#hooks-counter").text();
var number = $("#hooks-notified").text();
if(counter == $("#hooks-number").text()) {
var number = $("#hooks > li").length;
$("#hooks-notified").text(number);
if (counter == $("#hooks-number").text()) {
$("#hooks-loading").hide();
if (number > 1){
var hookMessage;
if (number > 1) {
hookMessage = globals.translations.hookMultiple.replace('_NUM_', number);
}else if(number == 1){
} else if (number == 1) {
hookMessage = globals.translations.hookSingle;
}else {
} else {
hookMessage = globals.translations.hookNone;
}
@ -115,22 +92,57 @@ function executeHook(hook, element_id, is_background){
/**
* Aggiunta dell'hook al numero totale.
*/
function addHookCount(id) {
var hooks_number = $(id);
var number = parseInt(hooks_number.text());
function hookCount(id, value) {
value = value ? value : 1;
var element = $(id);
var number = parseInt(element.text());
number = isNaN(number) ? 0 : number;
number++;
hooks_number.text(number);
number += value;
element.text(number);
return number;
}
/**
* Genera l'HTML per la visualizzazione degli hook.
*
* @param element_id
* @param result
*/
function renderHook(element_id, result) {
$(element_id).html('<a href="' + (result.link ? result.link : "#") + '"><i class="' + result.icon + '"></i><span class="small" > ' + result.message + '</span></a>');
function renderHook(hook, result) {
if (result.length == 0) return;
var element_id = "hook-" + hook.id;
// Inizializzazione
var element = $("#" + element_id);
if (element.length == 0) {
$("#hooks").append('<li class="hook-element" id="' + element_id + '"></li>');
element = $("#" + element_id);
}
// Rimozione
if (!result.show) {
element.remove();
return;
}
// Contenuto
var content = '<a href="' + (result.link ? result.link : "#") + '"><i class="' + result.icon + '"></i><span class="small"> ' + result.message + '</span>';
if (result.progress) {
var current = result.progress.current;
var total = result.progress.total;
var percentage = total == 0 ? current / total * 100 : 100;
content += '<div class="progress" style="margin-bottom: 0px;"><div class="progress-bar" role="progressbar" aria-valuenow="' + percentage + '" aria-valuemin="0" aria-valuemax="100" style="width:' + percentage + '%">' + percentage + '% (' + current + '/' + total + ')</div></div>';
}
content += '</a>';
element.html(content);
}

View File

@ -5,6 +5,7 @@ return [
'modules/aggiornamenti' => 'Modules\Aggiornamenti',
'modules/anagrafiche' => 'Modules\Anagrafiche',
'modules/backups' => 'Modules\Backups',
'modules/emails' => 'Modules\Emails',
'modules/articoli' => 'Modules\Articoli',
'modules/checklists' => 'Modules\Checklists',
'modules/ritenute' => 'Modules\Ritenute',

View File

@ -11,7 +11,7 @@ class UpdateHook extends CachedManager
{
protected static $client = null;
public function execute()
public function data()
{
$result = self::isAvailable();
@ -31,7 +31,7 @@ class UpdateHook extends CachedManager
'icon' => 'fa fa-download text-info',
'link' => $link,
'message' => $message,
'notify' => !empty($update),
'show' => !empty($update),
];
}

View File

@ -7,25 +7,25 @@ use Hooks\Manager;
class BackupHook extends Manager
{
public function manage()
public function execute()
{
$result = Backup::daily();
return $result;
}
public function response($update)
public function response($data)
{
return [
'icon' => 'fa fa-file-o text-info',
'message' => tr('Backup completato!'),
'notify' => true,
'show' => true,
];
}
public function prepare()
{
$result = setting('Backup automatico') && !Backup::isDailyComplete() && self::getHook()->processing == 0;
$result = setting('Backup automatico') && !Backup::isDailyComplete();
return [
'icon' => 'fa fa-file-o text-danger',

View File

@ -0,0 +1,81 @@
<?php
namespace Modules\Emails;
use Hooks\Manager;
use Models\Mail;
use Models\MailAccount;
use Notifications\EmailNotification;
class EmailHook extends Manager
{
public function execute()
{
$accounts = MailAccount::all();
$diff = date('Y-m-d', strtotime('-4 hours'));
$list = [];
foreach ($accounts as $account) {
$mail = Mail::whereNull('sent_at')
->where('id_account', $account->id)
->whereNull('failed_at')
->orWhereDate('failed_at', '<', $diff)
->orderBy('created_at')
->first();
if (!empty($mail)) {
$list[] = $mail;
}
}
foreach ($list as $mail) {
$email = EmailNotification::build($mail);
try {
// Invio mail
$email->send();
} catch (PHPMailer\PHPMailer\Exception $e) {
}
}
return count($list);
}
public function response($data)
{
return $this->prepare();
}
public function prepare()
{
$yesterday = date('Y-m-d', strtotime('-1 days'));
$diff = date('Y-m-d', strtotime('-4 hours'));
$user = auth()->getUser();
$remaining = Mail::whereNull('sent_at')
->where('created_by', $user->id)
->count();
$total = Mail::whereDate('sent_at', '>', $yesterday)
->orWhereNull('sent_at')
->where('created_by', $user->id)
->count();
$current = $total - $remaining;
$total_remaining = Mail::whereNull('sent_at')
->whereDate('failed_at', '<', $diff)
->count();
$message = !empty($remaining) ? tr('Invio email in corso...') : tr('Invio email completato!');
return [
'icon' => 'fa fa-envelope text-info',
'message' => $message,
'execute' => !empty($total_remaining),
'show' => true,
'progress' => [
'current' => $current,
'total' => $total,
],
];
}
}

View File

@ -2,6 +2,8 @@
include_once __DIR__.'/../../core.php';
use Models\Mail;
use Models\MailTemplate;
use Modules\Anagrafiche\Anagrafica;
use Modules\Articoli\Articolo as ArticoloOriginale;
use Modules\Interventi\Components\Articolo;
@ -11,6 +13,7 @@ use Modules\Interventi\Components\Sessione;
use Modules\Interventi\Intervento;
use Modules\Interventi\Stato;
use Modules\TipiIntervento\Tipo as TipoSessione;
use Notifications\EmailNotification;
switch (post('op')) {
case 'update':
@ -49,12 +52,14 @@ switch (post('op')) {
// Notifica chiusura intervento
$stato = $dbo->selectOne('in_statiintervento', '*', ['idstatointervento' => post('idstatointervento')]);
if (!empty($stato['notifica']) && !empty($stato['destinatari']) && $stato['idstatointervento'] != $record['idstatointervento']) {
$n = new Notifications\EmailNotification();
$template = MailTemplate::find($stato['id_email']);
$n->setTemplate($stato['id_email'], $id_record);
$n->setReceivers($stato['destinatari']);
$mail = Mail::build(auth()->getUser(), $template, $id_record);
$mail->addReceiver($stato['destinatari']);
$mail->save();
if ($n->send()) {
$email = EmailNotification::build($mail);
if ($email->send()) {
flash()->info(tr('Notifica inviata'));
} else {
flash()->warning(tr("Errore nell'invio della notifica"));
@ -480,12 +485,14 @@ switch (post('op')) {
$stato = $dbo->selectOne('in_statiintervento', '*', ['descrizione' => 'Completato']);
// Notifica chiusura intervento
if (!empty($stato['notifica']) && !empty($stato['destinatari'])) {
$n = new Notifications\EmailNotification();
$template = MailTemplate::find($stato['id_email']);
$n->setTemplate($stato['id_email'], $id_record);
$n->setReceivers($stato['destinatari']);
$mail = Mail::build(auth()->getUser(), $template, $id_record);
$mail->addReceiver($stato['destinatari']);
$mail->save();
if ($n->send()) {
$email = EmailNotification::build($mail);
if ($email->send()) {
flash()->info(tr('Notifica inviata'));
} else {
flash()->warning(tr("Errore nell'invio della notifica"));
@ -529,12 +536,14 @@ switch (post('op')) {
// Notifica rimozione dell' intervento al tecnico
if (!empty($tecnico['email'])) {
$n = new Notifications\EmailNotification();
$template = MailTemplate::get('Notifica rimozione intervento');
$n->setTemplate('Notifica rimozione intervento', $id_record);
$n->setReceivers($tecnico['email']);
$mail = Mail::build(auth()->getUser(), $template, $id_record);
$mail->addReceiver($tecnico['email']);
$mail->save();
if ($n->send()) {
$email = EmailNotification::build($mail);
if ($email->send()) {
flash()->info(tr('Notifica inviata'));
} else {
flash()->warning(tr("Errore nell'invio della notifica"));

View File

@ -2,12 +2,15 @@
include_once __DIR__.'/../../core.php';
use Models\Mail;
use Models\MailTemplate;
use Modules\Anagrafiche\Anagrafica;
use Modules\Fatture\Components\Descrizione;
use Modules\Fatture\Components\Riga;
use Modules\Fatture\Fattura;
use Modules\Interventi\Components\Sessione;
use Modules\Interventi\Intervento;
use Notifications\EmailNotification;
/**
* Recupera il totale delle ore spese per un intervento.
@ -61,12 +64,14 @@ function add_tecnico($idintervento, $idtecnico, $inizio, $fine, $idcontratto = n
// Notifica nuovo intervento al tecnico
if (!empty($tecnico['email'])) {
$n = new Notifications\EmailNotification();
$template = MailTemplate::get('Notifica intervento');
$n->setTemplate('Notifica intervento', $idintervento);
$n->setReceivers($anagrafica['email']);
$mail = Mail::build(auth()->getUser(), $template, $idintervento);
$mail->addReceiver($anagrafica['email']);
$mail->save();
$n->send();
$email = EmailNotification::build($mail);
$email->send();
}
return true;

View File

@ -446,9 +446,8 @@ class FatturaElettronica
// Codice fiscale
if (!empty($anagrafica['codice_fiscale'])) {
$result['CodiceFiscale'] = preg_replace('/\s+/', '', $anagrafica['codice_fiscale']);
//Rimuovo eventuali idicazioni relative alla nazione
$result['CodiceFiscale'] = preg_replace($anagrafica->nazione->iso2, '', $result['CodiceFiscale'], 2);
}

View File

@ -7,7 +7,7 @@ use Modules;
class InvoiceHook extends CachedManager
{
public function execute()
public function data()
{
$list = Interaction::getInvoiceList();
@ -40,7 +40,7 @@ class InvoiceHook extends CachedManager
'icon' => 'fa fa-file-text-o text-yellow',
'link' => $link,
'message' => $message,
'notify' => $notify,
'show' => $notify,
];
}
}

View File

@ -77,9 +77,9 @@ if ($structure->permission == 'rw') {
<input type="hidden" name="op" value="add_nota">
<input type="hidden" name="backto" value="record-edit">
{[ "type": "date", "label": "'.tr('Data di notifica').'", "name": "data_notifica" ]}
{[ "type": "date", "label": "'.tr('Data di notifica').'", "name": "data_notifica", "class": "unblockable" ]}
{[ "type": "ckeditor", "label": "'.tr('Nuova nota').'", "name": "contenuto", "required": 1]}
{[ "type": "ckeditor", "label": "'.tr('Nuova nota').'", "name": "contenuto", "required": 1, "class": "unblockable" ]}
<!-- PULSANTI -->
<div class="row">

View File

@ -7,7 +7,7 @@ use Modules;
class ReceiptHook extends CachedManager
{
public function execute()
public function data()
{
$list = Interaction::getReceiptList();
@ -40,7 +40,7 @@ class ReceiptHook extends CachedManager
'icon' => 'fa fa-ticket text-yellow',
'link' => $link,
'message' => $message,
'notify' => $notify,
'show' => $notify,
];
}
}

View File

@ -3,7 +3,10 @@
$skip_permissions = true;
include_once __DIR__.'/core.php';
use Models\Mail;
use Models\MailTemplate;
use Models\User;
use Notifications\EmailNotification;
$token = get('reset_token');
@ -23,12 +26,14 @@ switch (post('op')) {
$utente->reset_token = secure_random_string();
$utente->save();
$n = new Notifications\EmailNotification();
$template = MailTemplate::get('Reset password');
$n->setTemplate('Reset password', $utente->id);
$n->setReceivers($utente->email);
$mail = Mail::build($user, $template, $utente->id);
$mail->addReceiver($utente->email);
$mail->save();
$n->send();
$email = EmailNotification::build($mail);
$email->send();
}
//$message_email = substr($email, 0, 2).str_repeat('*', strlen($email)-8).substr($email, -6);

View File

@ -114,6 +114,8 @@ class Backup
if (!self::isDailyComplete()) {
return self::create();
}
return false;
}
/**

View File

@ -632,6 +632,11 @@ class Database extends Util\Singleton
Capsule::commit();
}
public function rollbackTransaction()
{
Capsule::rollBack();
}
/**
* Esegue le query interne ad un file ".sql".
*

View File

@ -10,7 +10,23 @@ abstract class CachedManager extends Manager
protected static $cache = null;
protected static $is_cached = null;
abstract public function execute();
abstract public function data();
public function execute()
{
if (self::isCached()) {
$results = self::getCache()['results'];
// Interpretazione della cache
$results = json_decode($results, true);
} else {
$results = $this->data();
self::update($results);
}
return $results;
}
public static function getCache()
{
@ -68,20 +84,4 @@ abstract class CachedManager extends Manager
return self::$is_cached;
}
public function manage()
{
if (self::isCached()) {
$results = self::getCache()['results'];
// Interpretazione della cache
$results = json_decode($results, true);
} else {
$results = $this->execute();
self::update($results);
}
return $results;
}
}

View File

@ -11,7 +11,7 @@ abstract class Manager
*
* @return mixed
*/
abstract public function manage();
abstract public function execute();
/**
* Restituisce le informazioni per la visualizzazione dell'hook.
@ -29,7 +29,22 @@ abstract class Manager
*/
public function prepare()
{
return null;
return [
'execute' => true,
];
}
public function manage()
{
$prepare = $this->prepare();
if (empty($prepare['execute'])) {
return [];
}
$data = $this->execute();
$results = $this->response($data);
return $results;
}
/**

View File

@ -3,6 +3,7 @@
namespace Models;
use Common\Model;
use Hooks\Manager;
use Illuminate\Database\Eloquent\Builder;
use Traits\StoreTrait;
@ -31,16 +32,13 @@ class Hook extends Model
$class = $this->class;
$hook = new $class();
$this->processing = true;
$this->save();
if (!$hook instanceof Manager) {
return [
'show' => false,
];
}
$data = $hook->manage();
$results = $hook->response($data);
$this->processing = false;
$this->save();
return $results;
return $hook->manage();
}
public function prepare()
@ -48,9 +46,7 @@ class Hook extends Model
$class = $this->class;
$hook = new $class();
$results = $hook->prepare();
return $results;
return $hook->prepare();
}
/* Relazioni Eloquent */

View File

@ -10,15 +10,17 @@ class Mail extends Model
protected $receivers = [];
protected $attachments = [];
protected $prints = [];
protected $attachments = null;
protected $prints = null;
protected $options = [];
protected $options = null;
public static function build($template = null, $id_record = null, $account = null)
public static function build(User $user, $template = null, $id_record = null, $account = null)
{
$model = parent::build();
$model->created_by = $user->id;
$model->id_template = $template->id;
$model->id_record = $id_record;
@ -42,6 +44,10 @@ class Mail extends Model
*/
public function addAttachment($file_id)
{
if (!isset($this->attachments)) {
$this->attachments = [];
}
$this->attachments[] = $file_id;
}
@ -58,6 +64,10 @@ class Mail extends Model
*/
public function addPrint($print_id, $name = null)
{
if (!isset($this->prints)) {
$this->prints = [];
}
$print = PrintTemplate::find($print_id);
if (empty($name)) {
@ -87,6 +97,10 @@ class Mail extends Model
return;
}
if (!isset($this->receivers)) {
$this->receivers = [];
}
$list = explode(';', $value);
foreach ($list as $element) {
$this->receivers[] = [
@ -98,10 +112,21 @@ class Mail extends Model
public function save(array $options = [])
{
$this->setReceiversAttribute($this->receivers);
$this->setAttachmentsAttribute($this->attachments);
$this->setPrintsAttribute($this->prints);
$this->setOptionsAttribute($this->options);
if (isset($this->receivers)) {
$this->setReceiversAttribute($this->receivers);
}
if (isset($this->attachments)) {
$this->setAttachmentsAttribute($this->attachments);
}
if (isset($this->prints)) {
$this->setPrintsAttribute($this->prints);
}
if (isset($this->options)) {
$this->setOptionsAttribute($this->options);
}
return parent::save($options);
}

View File

@ -71,7 +71,7 @@ class EmailNotification extends PHPMailer implements NotificationInterface
$this->WordWrap = 78;
}
public static function build(\Models\Mail $mail, $exceptions = true)
public static function build(\Models\Mail $mail, $exceptions = null)
{
$result = new self($mail->account->id, $exceptions);
@ -145,6 +145,17 @@ class EmailNotification extends PHPMailer implements NotificationInterface
$exception = $e;
}
// Registazione invio
if (!empty($this->mail)) {
if ($result) {
$this->mail->sent_at = date('Y-m-d H:i:s');
} else {
$this->mail->failed_at = date('Y-m-d H:i:s');
}
$this->mail->save();
}
$this->SmtpClose();
// Pulizia file generati

View File

@ -357,10 +357,13 @@ CREATE TABLE IF NOT EXISTS `em_emails` (
`prints` TEXT,
`options` TEXT,
`sent_at` TIMESTAMP NULL DEFAULT NULL,
`failed_at` TIMESTAMP NULL DEFAULT NULL,
`created_by` int(11) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`id_account`) REFERENCES `em_accounts`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`id_template`) REFERENCES `em_templates`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`id_campaign`) REFERENCES `em_campaigns`(`id`) ON DELETE CASCADE
FOREIGN KEY (`id_campaign`) REFERENCES `em_campaigns`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`created_by`) REFERENCES `zz_users`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `em_campaign_anagrafica` (
@ -371,3 +374,5 @@ CREATE TABLE IF NOT EXISTS `em_campaign_anagrafica` (
FOREIGN KEY (`id_anagrafica`) REFERENCES `an_anagrafiche`(`idanagrafica`) ON DELETE CASCADE,
FOREIGN KEY (`id_email`) REFERENCES `em_emails`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB;
INSERT INTO `zz_hooks` (`id`, `name`, `class`, `frequency`, `id_module`) VALUES (NULL, 'Email', 'Modules\\Emails\\EmailHook', '1 minute', (SELECT `id` FROM `zz_modules` WHERE `name` = 'Account email'));