| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Services; |
| 4 |
|
| 5 |
use FluentBooking\App\App; |
| 6 |
use FluentBooking\App\Services\Helper; |
| 7 |
use FluentBooking\App\Models\Booking; |
| 8 |
use FluentBooking\Framework\Support\Arr; |
| 9 |
use FluentBooking\App\Services\Libs\Emogrifier\Emogrifier; |
| 10 |
|
| 11 |
class SummaryReportService |
| 12 |
{ |
| 13 |
public static function maybeSendSummary() |
| 14 |
{ |
| 15 |
$notificationSettings = Helper::getGlobalSettings('administration'); |
| 16 |
|
| 17 |
$currentDay = strtolower(gmdate('D')); |
| 18 |
|
| 19 |
$status = Arr::get($notificationSettings, 'summary_notification'); |
| 20 |
$frequency = Arr::get($notificationSettings, 'notification_frequency'); |
| 21 |
$adminEmail = Arr::get($notificationSettings, 'admin_email'); |
| 22 |
$sendingDay = Arr::get($notificationSettings, 'notification_day'); |
| 23 |
|
| 24 |
if ($status != 'yes' || ($frequency == 'weekly' && $sendingDay != $currentDay)) { |
| 25 |
return; |
| 26 |
} |
| 27 |
$reportDays = $frequency == 'daily' ? 1 : 7; |
| 28 |
|
| 29 |
$reportDateFrom = gmdate('Y-m-d H:i:s', strtotime("-$reportDays days")); |
| 30 |
|
| 31 |
$totalBooked = Booking::where('created_at', '>=', $reportDateFrom) |
| 32 |
->where('created_at', '<=', gmdate('Y-m-d H:i:s')) |
| 33 |
->count(); |
| 34 |
|
| 35 |
$totalCompleted = Booking::where('end_time', '>=', $reportDateFrom) |
| 36 |
->where('end_time', '<=', gmdate('Y-m-d H:i:s')) |
| 37 |
->where('status', 'completed') |
| 38 |
->count(); |
| 39 |
|
| 40 |
if (!$totalBooked && !$totalCompleted) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
$data = [ |
| 45 |
'days' => $reportDays, |
| 46 |
'frequency' => $frequency, |
| 47 |
'totalBooked' => $totalBooked, |
| 48 |
'totalCompleted' => $totalCompleted, |
| 49 |
]; |
| 50 |
|
| 51 |
$adminEmail = str_replace('{{wp.admin_email}}', get_option('admin_email'), $adminEmail); |
| 52 |
|
| 53 |
if(!$adminEmail) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
// translators: %d is replaced with the number of days |
| 58 |
$emailSubject = sprintf(esc_html__('Email Summary of Your Bookings (Last %d Days)', 'fluent-booking'), $reportDays); |
| 59 |
|
| 60 |
$emailBody = (string)App::make('view')->make('emails.summary_report', $data); |
| 61 |
|
| 62 |
$emogrifier = new Emogrifier($emailBody); |
| 63 |
$emogrifier->disableInvisibleNodeRemoval(); |
| 64 |
$emailBbody = (string)$emogrifier->emogrify(); |
| 65 |
|
| 66 |
return Mailer::send($adminEmail, $emailSubject, $emailBbody); |
| 67 |
} |
| 68 |
} |
| 69 |
|