| 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 |
|
| 28 |
$lastSend = get_option('fcom_last_summary_email_send'); |
| 29 |
$interval = ($frequency == 'daily') ? DAY_IN_SECONDS : WEEK_IN_SECONDS; |
| 30 |
if ($lastSend && (current_time('timestamp') - strtotime($lastSend)) < $interval) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
$reportDays = $frequency == 'daily' ? 1 : 7; |
| 35 |
|
| 36 |
$reportDateFrom = gmdate('Y-m-d H:i:s', strtotime("-$reportDays days")); |
| 37 |
|
| 38 |
$totalBooked = Booking::where('created_at', '>=', $reportDateFrom) |
| 39 |
->where('created_at', '<=', gmdate('Y-m-d H:i:s')) |
| 40 |
->count(); |
| 41 |
|
| 42 |
$totalCompleted = Booking::where('end_time', '>=', $reportDateFrom) |
| 43 |
->where('end_time', '<=', gmdate('Y-m-d H:i:s')) |
| 44 |
->where('status', 'completed') |
| 45 |
->count(); |
| 46 |
|
| 47 |
if (!$totalBooked && !$totalCompleted) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
$data = [ |
| 52 |
'days' => $reportDays, |
| 53 |
'frequency' => $frequency, |
| 54 |
'totalBooked' => $totalBooked, |
| 55 |
'totalCompleted' => $totalCompleted, |
| 56 |
]; |
| 57 |
|
| 58 |
$adminEmail = str_replace('{{wp.admin_email}}', (string) get_option('admin_email', ''), $adminEmail); |
| 59 |
if(!$adminEmail) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
update_option('fcom_last_summary_email_send', current_time('mysql')); |
| 64 |
|
| 65 |
$emailSubject = sprintf( |
| 66 |
// translators: %d: number of days for which the summary is being reported |
| 67 |
esc_html(_n( |
| 68 |
'Email Summary of Your Bookings (Last %d Day)', |
| 69 |
'Email Summary of Your Bookings (Last %d Days)', |
| 70 |
$reportDays, |
| 71 |
'fluent-booking' |
| 72 |
)), |
| 73 |
$reportDays |
| 74 |
); |
| 75 |
|
| 76 |
$emailBody = (string)App::make('view')->make('emails.summary_report', $data); |
| 77 |
|
| 78 |
$emogrifier = new Emogrifier($emailBody); |
| 79 |
$emogrifier->disableInvisibleNodeRemoval(); |
| 80 |
$emailBbody = (string)$emogrifier->emogrify(); |
| 81 |
|
| 82 |
return Mailer::send($adminEmail, $emailSubject, $emailBbody); |
| 83 |
} |
| 84 |
} |
| 85 |
|