openstamanager/modules/emails/src/EmailHook.php

143 lines
4.7 KiB
PHP
Raw Normal View History

2019-08-27 15:42:13 +02:00
<?php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright (C) DevCode s.r.l.
2020-09-07 15:04:06 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2019-08-27 15:42:13 +02:00
namespace Modules\Emails;
use Carbon\Carbon;
2019-08-27 15:42:13 +02:00
use Hooks\Manager;
use Notifications\EmailNotification;
2019-12-27 15:56:20 +01:00
use PHPMailer\PHPMailer\Exception;
2019-08-27 15:42:13 +02:00
class EmailHook extends Manager
{
2019-08-29 11:25:13 +02:00
public function isSingleton()
{
return true;
}
public function needsExecution()
{
2021-08-02 15:42:08 +02:00
// Email fallite nelle ultime 4 ore
2019-09-06 17:40:02 +02:00
$diff = date('Y-m-d H:i:s', strtotime('-4 hours'));
2019-08-29 11:25:13 +02:00
$failed = function ($query) use ($diff) {
2019-09-12 10:45:36 +02:00
$query->where('failed_at', '<', $diff)
2019-08-29 11:25:13 +02:00
->orWhereNull('failed_at');
};
2021-08-02 15:42:08 +02:00
// Email da inviare per tutti gli account
2019-09-12 10:45:36 +02:00
$accounts = Account::all();
2019-08-29 11:25:13 +02:00
$remaining = Mail::whereNull('sent_at')
->where($failed)
->where('attempt', '<', setting('Numero massimo di tentativi'))
2019-09-12 10:45:36 +02:00
->whereIn('id_account', $accounts->pluck('id'))
2019-08-29 11:25:13 +02:00
->count();
return !empty($remaining);
}
2019-08-27 15:42:13 +02:00
public function execute()
{
2021-08-02 15:42:08 +02:00
// Email fallite nelle ultime 4 ore
2019-09-06 17:40:02 +02:00
$diff = date('Y-m-d H:i:s', strtotime('-4 hours'));
$failed = function ($query) use ($diff) {
2019-09-12 10:45:36 +02:00
$query->where('failed_at', '<', $diff)
->orWhereNull('failed_at');
};
2019-08-27 15:42:13 +02:00
2021-08-02 15:42:08 +02:00
// Parametri per l'invio
$numero_tentativi = setting('Numero massimo di tentativi');
$numero_email = setting('Numero email da inviare in contemporanea per account');
$numero_email = $numero_email < 1 ? 1 : $numero_email;
// Selezione email per account
$accounts = Account::all();
$lista = collect();
2019-08-27 15:42:13 +02:00
foreach ($accounts as $account) {
2021-08-02 15:42:08 +02:00
// Ultima email inviata per l'account
2019-09-12 10:45:36 +02:00
$last_mail = $account->emails()
->whereNotNull('sent_at')
->orderBy('sent_at')
->first();
// Controllo sul timeout dell'account
$date = new Carbon($last_mail->sent_at);
$now = new Carbon();
2019-09-12 10:45:36 +02:00
$diff_milliseconds = $date->diffInMilliseconds($now);
2021-08-02 15:42:08 +02:00
// Timeout per l'uso dell'account email
2019-09-12 10:45:36 +02:00
if (empty($last_mail) || $diff_milliseconds > $account->timeout) {
2021-08-02 15:42:08 +02:00
$lista_account = Mail::whereNull('sent_at')
->where('id_account', $account->id)
->where($failed)
2021-08-02 15:42:08 +02:00
->where('attempt', '<', $numero_tentativi)
->orderBy('created_at')
2021-08-02 15:42:08 +02:00
->take($numero_email)
->get();
$lista = $lista->concat($lista_account);
2019-08-27 15:42:13 +02:00
}
}
2021-08-02 15:42:08 +02:00
// Invio effettivo
foreach ($lista as $mail) {
2019-08-27 15:42:13 +02:00
try {
$email = EmailNotification::build($mail);
2019-08-27 15:42:13 +02:00
$email->send();
2019-12-27 15:56:20 +01:00
} catch (Exception $e) {
2019-08-27 15:42:13 +02:00
}
}
2021-08-02 15:42:08 +02:00
return $lista;
2019-08-27 15:42:13 +02:00
}
2019-08-29 11:25:13 +02:00
public function response()
2019-08-27 15:42:13 +02:00
{
$yesterday = date('Y-m-d', strtotime('-1 days'));
$user = auth()->getUser();
2021-08-02 15:42:08 +02:00
// Numero di email inviate
2019-08-27 17:25:52 +02:00
$current = Mail::whereDate('sent_at', '>', $yesterday)
->where('attempt', '<', setting('Numero massimo di tentativi'))
2019-08-27 15:42:13 +02:00
->where('created_by', $user->id)
->count();
2021-08-02 15:42:08 +02:00
// Numero totale di email
2019-08-29 16:01:49 +02:00
$total = Mail::where(function ($query) use ($yesterday) {
$query->whereDate('sent_at', '>', $yesterday)
->orWhereNull('sent_at');
})
->where('attempt', '<', setting('Numero massimo di tentativi'))
2019-08-27 15:42:13 +02:00
->where('created_by', $user->id)
->count();
2019-08-29 11:25:13 +02:00
$message = $total != $current ? tr('Invio email in corso...') : tr('Invio email completato!');
2019-08-28 11:52:04 +02:00
$message = empty($total) ? tr('Nessuna email presente...') : $message;
2019-08-27 15:42:13 +02:00
return [
'icon' => 'fa fa-envelope text-info',
'message' => $message,
2019-08-29 16:01:49 +02:00
'show' => ($total != $current),
2019-08-27 15:42:13 +02:00
'progress' => [
'current' => $current,
'total' => $total,
],
];
}
}