| @@ -3,8 +3,9 @@ | ||
| 3 | 3 | namespace FluentBooking\App\Http\Controllers; |
| 4 | 4 | |
| 5 | 5 | use FluentBooking\App\Models\Booking; |
| 6 | 6 | use FluentBooking\App\Models\BookingActivity; |
| 7 | +use FluentBooking\App\Services\BookingReportService; | |
| 7 | 8 | use FluentBooking\App\Services\ReportingHelperTrait; |
| 8 | 9 | use FluentBooking\Framework\Http\Request\Request; |
| 9 | 10 | use FluentBooking\Framework\Support\Arr; |
| 10 | 11 | use FluentBooking\App\Services\DateTimeHelper; |
| @@ -116,22 +117,15 @@ | ||
| 116 | 117 | |
| 117 | 118 | list($groupBy, $orderBy) = $this->getGroupAndOrder($frequency); |
| 118 | 119 | |
| 119 | 120 | // Define a function to fetch booking data based on status |
| 120 | - $fetchBookingsByStatus = function ($status) use ($period, $groupBy, $orderBy, $frequency, $from, $to) { | |
| 121 | - | |
| 122 | - if (!PermissionManager::userCanSeeAllBookings()) { | |
| 123 | - | |
| 124 | - return Booking::select($this->prepareSelect($frequency)) | |
| 125 | - ->where('status', $status) | |
| 126 | - ->whereBetween('created_at', [$from->format('Y-m-d'), $to->format('Y-m-d')]) | |
| 127 | - ->where('host_user_id', get_current_user_id()) | |
| 128 | - ->groupBy($groupBy) | |
| 129 | - ->orderBy($orderBy, 'ASC') | |
| 130 | - ->get(); | |
| 131 | - } | |
| 132 | - | |
| 133 | - return Booking::select($this->prepareSelect($frequency)) | |
| 121 | + // Scoped through BookingReportService so this graph counts the same | |
| 122 | + // bookings as the widgets above it and the schedules list beside it. | |
| 123 | + // It previously scoped on the host_user_id column alone, which hid | |
| 124 | + // bookings a limited host was a secondary host on. | |
| 125 | + $fetchBookingsByStatus = function ($status) use ($groupBy, $orderBy, $frequency, $from, $to) { | |
| 126 | + return BookingReportService::scoped() | |
| 127 | + ->select($this->prepareSelect($frequency)) | |
| 134 | 128 | ->where('status', $status) |
| 135 | 129 | ->whereBetween('created_at', [$from->format('Y-m-d'), $to->format('Y-m-d')]) |
| 136 | 130 | ->groupBy($groupBy) |
| 137 | 131 | ->orderBy($orderBy, 'ASC') |
| @@ -172,26 +166,16 @@ | ||
| 172 | 166 | } |
| 173 | 167 | |
| 174 | 168 | private function getBookingStats($currentMonthStart, $currentMonthEnd, $lastMonthStart, $lastMonthEnd) |
| 175 | 169 | { |
| 176 | - $scopeByUser = !PermissionManager::userCanSeeAllBookings(); | |
| 177 | - | |
| 178 | - $scopeToUser = function ($query) { | |
| 179 | - $query->whereHas('hosts', function ($hostQuery) { | |
| 180 | - $hostQuery->where('user_id', get_current_user_id()); | |
| 181 | - }); | |
| 170 | + $createdBetween = function ($start, $end) { | |
| 171 | + return BookingReportService::scoped()->whereBetween('created_at', [$start, $end]); | |
| 182 | 172 | }; |
| 183 | 173 | |
| 184 | - $createdBetween = function ($start, $end) use ($scopeByUser, $scopeToUser) { | |
| 185 | - return Booking::whereBetween('created_at', [$start, $end]) | |
| 186 | - ->when($scopeByUser, $scopeToUser); | |
| 174 | + $endTimeBetween = function ($start, $end) { | |
| 175 | + return BookingReportService::scoped()->whereBetween('end_time', [$start, $end]); | |
| 187 | 176 | }; |
| 188 | 177 | |
| 189 | - $endTimeBetween = function ($start, $end) use ($scopeByUser, $scopeToUser) { | |
| 190 | - return Booking::whereBetween('end_time', [$start, $end]) | |
| 191 | - ->when($scopeByUser, $scopeToUser); | |
| 192 | - }; | |
| 193 | - | |
| 194 | 178 | // Bookings and guests based on 'created_at' |
| 195 | 179 | $totalBookedCurrentMonth = $createdBetween($currentMonthStart, $currentMonthEnd)->count(); |
| 196 | 180 | $totalBookedLastMonth = $createdBetween($lastMonthStart, $lastMonthEnd)->count(); |
| 197 | 181 | |
| @@ -229,26 +213,16 @@ | ||
| 229 | 213 | } |
| 230 | 214 | |
| 231 | 215 | private function getBookingWidgetNumbers($startTime, $endTime) |
| 232 | 216 | { |
| 233 | - $scopeByUser = !PermissionManager::userCanSeeAllBookings(); | |
| 234 | - | |
| 235 | - $scopeToUser = function ($query) { | |
| 236 | - $query->whereHas('hosts', function ($hostQuery) { | |
| 237 | - $hostQuery->where('user_id', get_current_user_id()); | |
| 238 | - }); | |
| 217 | + $createdBetween = function () use ($startTime, $endTime) { | |
| 218 | + return BookingReportService::scoped()->whereBetween('created_at', [$startTime, $endTime]); | |
| 239 | 219 | }; |
| 240 | 220 | |
| 241 | - $createdBetween = function () use ($startTime, $endTime, $scopeByUser, $scopeToUser) { | |
| 242 | - return Booking::whereBetween('created_at', [$startTime, $endTime]) | |
| 243 | - ->when($scopeByUser, $scopeToUser); | |
| 221 | + $endTimeBetween = function () use ($startTime, $endTime) { | |
| 222 | + return BookingReportService::scoped()->whereBetween('end_time', [$startTime, $endTime]); | |
| 244 | 223 | }; |
| 245 | 224 | |
| 246 | - $endTimeBetween = function () use ($startTime, $endTime, $scopeByUser, $scopeToUser) { | |
| 247 | - return Booking::whereBetween('end_time', [$startTime, $endTime]) | |
| 248 | - ->when($scopeByUser, $scopeToUser); | |
| 249 | - }; | |
| 250 | - | |
| 251 | 225 | $totalBooked = $createdBetween()->count(); |
| 252 | 226 | $totalGuests = $createdBetween()->distinct()->count('email'); |
| 253 | 227 | |
| 254 | 228 | $bookingCompleted = $endTimeBetween()->where('status', 'completed')->count(); |
| @@ -263,45 +237,37 @@ | ||
| 263 | 237 | } |
| 264 | 238 | |
| 265 | 239 | private function getAllBookingWidgetNumbers() |
| 266 | 240 | { |
| 267 | - $scopeByUser = !PermissionManager::userCanSeeAllBookings(); | |
| 241 | + // Four undated counts, so four full reads of the bookings table on | |
| 242 | + // every dashboard load. They are lifetime totals; five minutes stale | |
| 243 | + // is invisible. | |
| 244 | + $cacheKey = 'fcal_report_all_widgets_' . get_current_blog_id() . '_' . get_current_user_id(); | |
| 268 | 245 | |
| 269 | - $userId = get_current_user_id(); | |
| 246 | + $cached = get_transient($cacheKey); | |
| 270 | 247 | |
| 271 | - $totalBooked = Booking::when($scopeByUser, function($q) use ($userId) { | |
| 272 | - $q->whereHas('hosts', function ($hostQuery) use ($userId) { | |
| 273 | - $hostQuery->where('user_id', $userId); | |
| 274 | - }); | |
| 275 | - })->count(); | |
| 248 | + if (is_array($cached)) { | |
| 249 | + return $cached; | |
| 250 | + } | |
| 276 | 251 | |
| 277 | - $bookingCompleted = Booking::where('status', 'completed') | |
| 278 | - ->when($scopeByUser, function($q) use ($userId) { | |
| 279 | - $q->whereHas('hosts', function ($hostQuery) use ($userId) { | |
| 280 | - $hostQuery->where('user_id', $userId); | |
| 281 | - }); | |
| 282 | - })->count(); | |
| 252 | + $totalBooked = BookingReportService::scoped()->count(); | |
| 283 | 253 | |
| 284 | - $bookingCancelled = Booking::where('status', 'cancelled') | |
| 285 | - ->when($scopeByUser, function($q) use ($userId) { | |
| 286 | - $q->whereHas('hosts', function ($hostQuery) use ($userId) { | |
| 287 | - $hostQuery->where('user_id', $userId); | |
| 288 | - }); | |
| 289 | - })->count(); | |
| 254 | + $bookingCompleted = BookingReportService::scoped()->where('status', 'completed')->count(); | |
| 290 | 255 | |
| 291 | - $totalGuests = Booking::distinct() | |
| 292 | - ->when($scopeByUser, function($q) use ($userId) { | |
| 293 | - $q->whereHas('hosts', function ($hostQuery) use ($userId) { | |
| 294 | - $hostQuery->where('user_id', $userId); | |
| 295 | - }); | |
| 296 | - })->count('email'); | |
| 256 | + $bookingCancelled = BookingReportService::scoped()->where('status', 'cancelled')->count(); | |
| 297 | 257 | |
| 298 | - return [ | |
| 258 | + $totalGuests = BookingReportService::scoped()->distinct()->count('email'); | |
| 259 | + | |
| 260 | + $numbers = [ | |
| 299 | 261 | 'totalBooked' => $totalBooked, |
| 300 | 262 | 'totalGuests' => $totalGuests, |
| 301 | 263 | 'bookingCompleted' => $bookingCompleted, |
| 302 | 264 | 'bookingCancelled' => $bookingCancelled |
| 303 | 265 | ]; |
| 266 | + | |
| 267 | + set_transient($cacheKey, $numbers, 300); | |
| 268 | + | |
| 269 | + return $numbers; | |
| 304 | 270 | } |
| 305 | 271 | |
| 306 | 272 | /** |
| 307 | 273 | * The window the percentage beside each number is measured against. |