openstamanager/modules/emails/src/EmailHook.php

131 lines
4.0 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()
{
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');
};
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)
2019-10-29 16:29:59 +01:00
->where('attempt', '<', 10)
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()
{
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
$accounts = Account::all();
2019-08-27 15:42:13 +02:00
$list = [];
foreach ($accounts as $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);
2019-09-12 10:45:36 +02:00
if (empty($last_mail) || $diff_milliseconds > $account->timeout) {
$mail = Mail::whereNull('sent_at')
->where('id_account', $account->id)
->where($failed)
2019-10-29 16:29:59 +01:00
->where('attempt', '<', 10)
->orderBy('created_at')
->first();
if (!empty($mail)) {
$list[] = $mail;
}
2019-08-27 15:42:13 +02:00
}
}
foreach ($list as $mail) {
$email = EmailNotification::build($mail);
try {
// Invio mail
$email->send();
2019-12-27 15:56:20 +01:00
} catch (Exception $e) {
2019-08-27 15:42:13 +02:00
}
}
2019-08-29 11:25:13 +02:00
return $list;
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();
2019-08-27 17:25:52 +02:00
$current = Mail::whereDate('sent_at', '>', $yesterday)
2019-10-29 16:29:59 +01:00
->where('attempt', '<', 10)
2019-08-27 15:42:13 +02:00
->where('created_by', $user->id)
->count();
2019-08-29 16:01:49 +02:00
$total = Mail::where(function ($query) use ($yesterday) {
$query->whereDate('sent_at', '>', $yesterday)
->orWhereNull('sent_at');
})
2019-10-29 16:29:59 +01:00
->where('attempt', '<', 10)
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,
],
];
}
}