StatsService.php
680 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Services\Stats; |
| 4 | |
| 5 | use AmeliaBooking\Application\Services\Bookable\AbstractPackageApplicationService; |
| 6 | use AmeliaBooking\Application\Services\User\ProviderApplicationService; |
| 7 | use AmeliaBooking\Domain\Collection\Collection; |
| 8 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 9 | use AmeliaBooking\Domain\Entity\Booking\Appointment\Appointment; |
| 10 | use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBooking; |
| 11 | use AmeliaBooking\Domain\Entity\Booking\Event\CustomerBookingEventTicket; |
| 12 | use AmeliaBooking\Domain\Entity\Booking\Event\Event; |
| 13 | use AmeliaBooking\Domain\Entity\Booking\Event\EventTicket; |
| 14 | use AmeliaBooking\Domain\Entity\Payment\Payment; |
| 15 | use AmeliaBooking\Domain\Entity\Schedule\DayOff; |
| 16 | use AmeliaBooking\Domain\Entity\Schedule\SpecialDay; |
| 17 | use AmeliaBooking\Domain\Entity\Schedule\WeekDay; |
| 18 | use AmeliaBooking\Domain\Entity\User\Provider; |
| 19 | use AmeliaBooking\Domain\Factory\Schedule\PeriodFactory; |
| 20 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 21 | use AmeliaBooking\Domain\Services\User\ProviderService; |
| 22 | use AmeliaBooking\Domain\ValueObjects\String\BookingStatus; |
| 23 | use AmeliaBooking\Infrastructure\Common\Container; |
| 24 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 25 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\ServiceRepository; |
| 26 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\AppointmentRepository; |
| 27 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository; |
| 28 | use AmeliaBooking\Infrastructure\Repository\Location\LocationRepository; |
| 29 | use AmeliaBooking\Infrastructure\Repository\Payment\PaymentRepository; |
| 30 | use AmeliaBooking\Infrastructure\Repository\User\ProviderRepository; |
| 31 | use DateTime; |
| 32 | use Exception; |
| 33 | use Interop\Container\Exception\ContainerException; |
| 34 | use Slim\Exception\ContainerValueNotFoundException; |
| 35 | |
| 36 | /** |
| 37 | * Class StatsService |
| 38 | * |
| 39 | * @package AmeliaBooking\Application\Services\Stats |
| 40 | */ |
| 41 | class StatsService |
| 42 | { |
| 43 | private $container; |
| 44 | |
| 45 | /** |
| 46 | * StatsService constructor. |
| 47 | * |
| 48 | * @param Container $container |
| 49 | */ |
| 50 | public function __construct(Container $container) |
| 51 | { |
| 52 | $this->container = $container; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @param array $params |
| 57 | * @param Collection $services |
| 58 | * @param Collection $providers |
| 59 | * |
| 60 | * @return array |
| 61 | * @throws QueryExecutionException |
| 62 | * @throws ContainerValueNotFoundException |
| 63 | * @throws InvalidArgumentException |
| 64 | * @throws Exception |
| 65 | * @throws ContainerException |
| 66 | */ |
| 67 | public function getAppointmentsRangeStatisticsData($params, $services, $providers) |
| 68 | { |
| 69 | /** @var AppointmentRepository $appointmentRepository */ |
| 70 | $appointmentRepository = $this->container->get('domain.booking.appointment.repository'); |
| 71 | |
| 72 | /** @var Collection $appointments */ |
| 73 | $appointments = $appointmentRepository->getFiltered( |
| 74 | array_merge( |
| 75 | $params, |
| 76 | [ |
| 77 | 'skipServices' => true, |
| 78 | 'skipProviders' => true, |
| 79 | 'skipCustomers' => true, |
| 80 | 'skipPayments' => true, |
| 81 | 'skipExtras' => true, |
| 82 | 'skipCoupons' => true, |
| 83 | 'skipBookings' => true, |
| 84 | ] |
| 85 | ) |
| 86 | ); |
| 87 | |
| 88 | /** @var CustomerBookingRepository $bookingRepository */ |
| 89 | $bookingRepository = $this->container->get('domain.booking.customerBooking.repository'); |
| 90 | |
| 91 | /** @var Collection $bookings */ |
| 92 | $bookings = $appointments->length() |
| 93 | ? $bookingRepository->getByCriteria(['appointmentIds' => $appointments->keys()]) |
| 94 | : new Collection(); |
| 95 | |
| 96 | /** @var CustomerBooking $booking */ |
| 97 | foreach ($bookings->getItems() as $booking) { |
| 98 | /** @var Appointment $appointment */ |
| 99 | $appointment = $appointments->getItem($booking->getAppointmentId()->getValue()); |
| 100 | |
| 101 | $appointment->getBookings()->addItem($booking, $booking->getId()->getValue()); |
| 102 | } |
| 103 | |
| 104 | /** @var Collection $bookingsPayments */ |
| 105 | $bookingsPayments = new Collection(); |
| 106 | |
| 107 | /** @var Appointment $appointment */ |
| 108 | foreach ($appointments->getItems() as $appointment) { |
| 109 | /** @var CustomerBooking $booking */ |
| 110 | foreach ($appointment->getBookings()->getItems() as $booking) { |
| 111 | $bookingsPayments->addItem(null, $booking->getId()->getValue()); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** @var PaymentRepository $paymentRepository */ |
| 116 | $paymentRepository = $this->container->get('domain.payment.repository'); |
| 117 | |
| 118 | /** @var Collection $payments */ |
| 119 | $payments = $bookingsPayments->length() ? |
| 120 | $paymentRepository->getByCriteria(['bookingIds' => $bookingsPayments->keys()]) : new Collection(); |
| 121 | |
| 122 | /** @var Payment $payment */ |
| 123 | foreach ($payments->getItems() as $payment) { |
| 124 | if ($payment->getCustomerBookingId()) { |
| 125 | $bookingsPayments->placeItem($payment, $payment->getCustomerBookingId()->getValue(), true); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** @var Appointment $appointment */ |
| 130 | foreach ($appointments->getItems() as $appointment) { |
| 131 | /** @var CustomerBooking $booking */ |
| 132 | foreach ($appointment->getBookings()->getItems() as $booking) { |
| 133 | if ( |
| 134 | $bookingsPayments->keyExists($booking->getId()->getValue()) && |
| 135 | $bookingsPayments->getItem($booking->getId()->getValue()) |
| 136 | ) { |
| 137 | $booking->getPayments()->addItem($bookingsPayments->getItem($booking->getId()->getValue())); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** @var ProviderService $providerService */ |
| 143 | $providerService = $this->container->get('domain.user.provider.service'); |
| 144 | |
| 145 | /** @var Provider $provider */ |
| 146 | foreach ($providers->getItems() as $provider) { |
| 147 | $providerService->setProviderServices($provider, $services, true); |
| 148 | } |
| 149 | |
| 150 | /** @var ProviderApplicationService $providerApplicationService */ |
| 151 | $providerApplicationService = $this->container->get('application.user.provider.service'); |
| 152 | |
| 153 | /** @var AbstractPackageApplicationService $packageApplicationService */ |
| 154 | $packageApplicationService = $this->container->get('application.bookable.package'); |
| 155 | |
| 156 | $packageDatesData = $packageApplicationService->getPackageStatsData($params); |
| 157 | |
| 158 | /** @var Collection $appointmentsPackageCustomerServices */ |
| 159 | $appointmentsPackageCustomerServices = $packageApplicationService->getPackageCustomerServicesForAppointments( |
| 160 | $appointments |
| 161 | ); |
| 162 | |
| 163 | $stats = []; |
| 164 | |
| 165 | $statsPeriod = new \DatePeriod( |
| 166 | DateTimeService::getCustomDateTimeObject($params['dates'][0]), |
| 167 | new \DateInterval('P1D'), |
| 168 | DateTimeService::getCustomDateTimeObject($params['dates'][1]) |
| 169 | ); |
| 170 | |
| 171 | /** @var DateTime $date */ |
| 172 | foreach ($statsPeriod as $date) { |
| 173 | $stats[$date->format('Y-m-d')] = null; |
| 174 | } |
| 175 | |
| 176 | $weekDaysData = []; |
| 177 | |
| 178 | $specialDatesData = []; |
| 179 | |
| 180 | $providersDaysOff = []; |
| 181 | |
| 182 | /** @var Provider $provider */ |
| 183 | foreach ($providers->getItems() as $provider) { |
| 184 | $providerId = $provider->getId()->getValue(); |
| 185 | |
| 186 | $providersDaysOff[$providerId] = []; |
| 187 | |
| 188 | /** @var DayOff $daysOff */ |
| 189 | foreach ($provider->getDayOffList()->getItems() as $daysOff) { |
| 190 | $daysOffPeriod = new \DatePeriod( |
| 191 | $daysOff->getStartDate()->getValue(), |
| 192 | new \DateInterval('P1D'), |
| 193 | DateTimeService::getCustomDateTimeObject( |
| 194 | $daysOff->getEndDate()->getValue()->format('Y-m-d H:i:s') |
| 195 | )->modify('+1 days') |
| 196 | ); |
| 197 | |
| 198 | /** @var DateTime $date */ |
| 199 | foreach ($daysOffPeriod as $date) { |
| 200 | $providersDaysOff[$providerId][] = $date->format('Y-m-d'); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // get provider week day available time |
| 205 | /** @var WeekDay $weekDay */ |
| 206 | foreach ($provider->getWeekDayList()->getItems() as $weekDay) { |
| 207 | $dayIndex = $weekDay->getDayIndex()->getValue(); |
| 208 | |
| 209 | if (!array_key_exists($dayIndex, $weekDaysData)) { |
| 210 | $weekDaysData[$dayIndex] = []; |
| 211 | } |
| 212 | |
| 213 | if ($weekDay->getPeriodList()->length() === 0) { |
| 214 | $weekDay->getPeriodList()->addItem( |
| 215 | PeriodFactory::create( |
| 216 | [ |
| 217 | 'startTime' => $weekDay->getStartTime()->getValue()->format('H:i:s'), |
| 218 | 'endTime' => $weekDay->getEndTime()->getValue()->format('H:i:s'), |
| 219 | 'periodServiceList' => [], |
| 220 | 'periodLocationList' => [], |
| 221 | ] |
| 222 | ) |
| 223 | ); |
| 224 | } |
| 225 | |
| 226 | $weekDaysData[$dayIndex][$providerId] = $providerApplicationService->getProviderScheduleIntervals( |
| 227 | $weekDay->getPeriodList(), |
| 228 | $weekDay->getTimeOutList(), |
| 229 | $provider |
| 230 | ); |
| 231 | } |
| 232 | |
| 233 | // get provider special day available time |
| 234 | /** @var SpecialDay $specialDay */ |
| 235 | foreach ($provider->getSpecialDayList()->getItems() as $specialDay) { |
| 236 | $specialDaysPeriod = new \DatePeriod( |
| 237 | $specialDay->getStartDate()->getValue(), |
| 238 | new \DateInterval('P1D'), |
| 239 | DateTimeService::getCustomDateTimeObject( |
| 240 | $specialDay->getEndDate()->getValue()->format('Y-m-d H:i:s') |
| 241 | )->modify('+1 days') |
| 242 | ); |
| 243 | |
| 244 | $specialDayExist = false; |
| 245 | |
| 246 | foreach ($specialDaysPeriod as $date) { |
| 247 | if (array_key_exists($date->format('Y-m-d'), $stats)) { |
| 248 | $specialDayExist = true; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | if ($specialDayExist) { |
| 253 | $providerSpecialDaysIntervals = $providerApplicationService->getProviderScheduleIntervals( |
| 254 | $specialDay->getPeriodList(), |
| 255 | new Collection(), |
| 256 | $provider |
| 257 | ); |
| 258 | |
| 259 | /** @var DateTime $date */ |
| 260 | foreach ($specialDaysPeriod as $date) { |
| 261 | $dateString = $date->format('Y-m-d'); |
| 262 | |
| 263 | if (array_key_exists($dateString, $stats)) { |
| 264 | if (!array_key_exists($dateString, $specialDatesData)) { |
| 265 | $specialDatesData[$dateString] = []; |
| 266 | } |
| 267 | |
| 268 | $specialDatesData[$dateString][$providerId] = $providerSpecialDaysIntervals; |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | $appointmentDatesData = []; |
| 276 | |
| 277 | |
| 278 | |
| 279 | /** @var Appointment $appointment */ |
| 280 | foreach ($appointments->getItems() as $appointment) { |
| 281 | $date = $appointment->getBookingStart()->getValue()->format('Y-m-d'); |
| 282 | |
| 283 | $providerId = $appointment->getProviderId()->getValue(); |
| 284 | |
| 285 | $serviceId = $appointment->getServiceId()->getValue(); |
| 286 | |
| 287 | $appointmentDuration = $appointment->getBookingEnd()->getValue()->diff( |
| 288 | $appointment->getBookingStart()->getValue() |
| 289 | ); |
| 290 | |
| 291 | if (!array_key_exists($date, $appointmentDatesData)) { |
| 292 | $appointmentDatesData[$date] = [ |
| 293 | 'providers' => [], |
| 294 | 'services' => [], |
| 295 | 'customers' => [], |
| 296 | ]; |
| 297 | } |
| 298 | |
| 299 | if (!array_key_exists($providerId, $appointmentDatesData[$date]['providers'])) { |
| 300 | $appointmentDatesData[$date]['providers'][$providerId] = [ |
| 301 | 'count' => 0, |
| 302 | 'occupied' => 0, |
| 303 | 'revenue' => 0 |
| 304 | ]; |
| 305 | } |
| 306 | |
| 307 | if (!array_key_exists($serviceId, $appointmentDatesData[$date]['services'])) { |
| 308 | $appointmentDatesData[$date]['services'][$serviceId] = [ |
| 309 | 'count' => 0, |
| 310 | 'occupied' => 0, |
| 311 | 'revenue' => 0 |
| 312 | ]; |
| 313 | } |
| 314 | |
| 315 | $occupiedDuration = $appointmentDuration->h * 60 + $appointmentDuration->i; |
| 316 | |
| 317 | $appointmentDatesData[$date]['providers'][$providerId]['count']++; |
| 318 | $appointmentDatesData[$date]['providers'][$providerId]['occupied'] += $occupiedDuration; |
| 319 | |
| 320 | $appointmentDatesData[$date]['services'][$serviceId]['count']++; |
| 321 | $appointmentDatesData[$date]['services'][$serviceId]['occupied'] += $occupiedDuration; |
| 322 | |
| 323 | /** @var CustomerBooking $booking */ |
| 324 | foreach ($appointment->getBookings()->getItems() as $booking) { |
| 325 | if ($booking->getPackageCustomerService()) { |
| 326 | $packageApplicationService->updatePackageStatsData( |
| 327 | $packageDatesData, |
| 328 | $appointmentsPackageCustomerServices, |
| 329 | $booking->getPackageCustomerService()->getId()->getValue(), |
| 330 | $date, |
| 331 | $occupiedDuration |
| 332 | ); |
| 333 | } else { |
| 334 | /** @var Payment $payment */ |
| 335 | foreach ($booking->getPayments()->getItems() as $payment) { |
| 336 | $appointmentDatesData[$date]['providers'][$providerId]['revenue'] += |
| 337 | $payment->getAmount()->getValue(); |
| 338 | |
| 339 | $appointmentDatesData[$date]['services'][$serviceId]['revenue'] += |
| 340 | $payment->getAmount()->getValue(); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | if (empty($appointmentDatesData[$date]['customers'][$booking->getCustomerId()->getValue()])) { |
| 345 | $appointmentDatesData[$date]['customers'][$booking->getCustomerId()->getValue()] = 0; |
| 346 | } |
| 347 | |
| 348 | $appointmentDatesData[$date]['customers'][$booking->getCustomerId()->getValue()]++; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | foreach ($stats as $dateKey => $dateStats) { |
| 353 | $dayIndex = DateTimeService::getCustomDateTimeObject($dateKey)->format('N'); |
| 354 | |
| 355 | // parse week day for provider |
| 356 | if (array_key_exists($dayIndex, $weekDaysData)) { |
| 357 | foreach ($weekDaysData[$dayIndex] as $providerKey => $weekDayData) { |
| 358 | if (!in_array($dateKey, $providersDaysOff[$providerKey], true)) { |
| 359 | $stats[$dateKey]['providers'][$providerKey] = [ |
| 360 | 'count' => 0, |
| 361 | 'occupied' => 0, |
| 362 | 'revenue' => 0, |
| 363 | 'intervals' => $weekDayData |
| 364 | ]; |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | // parse special day for provider |
| 370 | if (array_key_exists($dateKey, $specialDatesData)) { |
| 371 | foreach ($specialDatesData[$dateKey] as $providerKey => $specialDayData) { |
| 372 | if (!in_array($dateKey, $providersDaysOff[$providerKey], true)) { |
| 373 | $stats[$dateKey]['providers'][$providerKey] = [ |
| 374 | 'count' => 0, |
| 375 | 'occupied' => 0, |
| 376 | 'revenue' => 0, |
| 377 | 'intervals' => $specialDayData |
| 378 | ]; |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | if (array_key_exists($dateKey, $appointmentDatesData)) { |
| 384 | foreach ($appointmentDatesData[$dateKey]['providers'] as $providerKey => $appointmentStatsData) { |
| 385 | if ( |
| 386 | empty($stats[$dateKey]['providers']) || |
| 387 | !array_key_exists($providerKey, $stats[$dateKey]['providers']) |
| 388 | ) { |
| 389 | $stats[$dateKey]['providers'][$providerKey] = [ |
| 390 | 'intervals' => [] |
| 391 | ]; |
| 392 | } |
| 393 | |
| 394 | $stats[$dateKey]['providers'][$providerKey]['count'] = $appointmentStatsData['count']; |
| 395 | |
| 396 | $stats[$dateKey]['providers'][$providerKey]['occupied'] = $appointmentStatsData['occupied']; |
| 397 | |
| 398 | $stats[$dateKey]['providers'][$providerKey]['revenue'] = $appointmentStatsData['revenue']; |
| 399 | } |
| 400 | |
| 401 | foreach ($appointmentDatesData[$dateKey]['services'] as $serviceKey => $appointmentStatsData) { |
| 402 | $stats[$dateKey]['services'][$serviceKey]['count'] = $appointmentStatsData['count']; |
| 403 | |
| 404 | $stats[$dateKey]['services'][$serviceKey]['occupied'] = $appointmentStatsData['occupied']; |
| 405 | |
| 406 | $stats[$dateKey]['services'][$serviceKey]['revenue'] = $appointmentStatsData['revenue']; |
| 407 | } |
| 408 | |
| 409 | foreach ($appointmentDatesData[$dateKey]['customers'] as $customerKey => $count) { |
| 410 | $stats[$dateKey]['customers'][$customerKey] = $count; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | if (array_key_exists($dateKey, $packageDatesData)) { |
| 415 | foreach ($packageDatesData[$dateKey] as $packageKey => $packageStatsData) { |
| 416 | $stats[$dateKey]['packages'][$packageKey]['count'] = $packageStatsData['count']; |
| 417 | |
| 418 | $stats[$dateKey]['packages'][$packageKey]['purchased'] = $packageStatsData['purchased']; |
| 419 | |
| 420 | $stats[$dateKey]['packages'][$packageKey]['occupied'] = $packageStatsData['occupied']; |
| 421 | |
| 422 | $stats[$dateKey]['packages'][$packageKey]['revenue'] = $packageStatsData['revenue']; |
| 423 | |
| 424 | $stats[$dateKey]['packages'][$packageKey]['intervals'] = []; |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | return $stats; |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * @param Collection $events |
| 434 | * @param string $start |
| 435 | * @param string $end |
| 436 | * |
| 437 | * @return array |
| 438 | * @throws QueryExecutionException |
| 439 | * @throws ContainerValueNotFoundException |
| 440 | * @throws InvalidArgumentException |
| 441 | * @throws Exception |
| 442 | * @throws ContainerException |
| 443 | */ |
| 444 | public function getEventsRangeStatisticsData($events, $start, $end) |
| 445 | { |
| 446 | $stats = []; |
| 447 | |
| 448 | $statsPeriod = new \DatePeriod( |
| 449 | DateTimeService::getCustomDateTimeObject($start), |
| 450 | new \DateInterval('P1D'), |
| 451 | DateTimeService::getCustomDateTimeObject($end) |
| 452 | ); |
| 453 | |
| 454 | /** @var DateTime $date */ |
| 455 | foreach ($statsPeriod as $date) { |
| 456 | $stats[$date->format('Y-m-d')] = null; |
| 457 | } |
| 458 | |
| 459 | /** @var Event $event */ |
| 460 | foreach ($events->getItems() as $event) { |
| 461 | $periodDate = $event->getPeriods()->getItem(0)->getPeriodStart()->getValue()->format('Y-m-d'); |
| 462 | |
| 463 | if (array_key_exists($periodDate, $stats)) { |
| 464 | $eventId = $event->getId()->getValue(); |
| 465 | |
| 466 | $stats[$periodDate]['events'][$eventId] = [ |
| 467 | 'spots' => $event->getCustomPricing()->getValue() |
| 468 | ? null |
| 469 | : [ |
| 470 | 'places' => $event->getMaxCapacity()->getValue(), |
| 471 | 'count' => 0, |
| 472 | 'revenue' => 0, |
| 473 | ], |
| 474 | 'tickets' => $event->getCustomPricing()->getValue() |
| 475 | ? [ |
| 476 | 'places' => 0, |
| 477 | 'items' => [], |
| 478 | ] |
| 479 | : null, |
| 480 | 'bookings' => 0, |
| 481 | ]; |
| 482 | |
| 483 | if ($event->getCustomPricing()->getValue()) { |
| 484 | if ($event->getMaxCustomCapacity() && $event->getMaxCustomCapacity()->getValue()) { |
| 485 | $stats[$periodDate]['events'][$eventId]['tickets']['places'] = |
| 486 | $event->getMaxCustomCapacity()->getValue(); |
| 487 | } |
| 488 | |
| 489 | /** @var EventTicket $ticket */ |
| 490 | foreach ($event->getCustomTickets()->getItems() as $ticket) { |
| 491 | $stats[$periodDate]['events'][$eventId]['tickets']['items'][$ticket->getId()->getValue()] = [ |
| 492 | 'places' => $event->getMaxCustomCapacity() && $event->getMaxCustomCapacity()->getValue() |
| 493 | ? 0 |
| 494 | : $ticket->getSpots()->getValue(), |
| 495 | 'count' => 0, |
| 496 | 'revenue' => 0, |
| 497 | ]; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | /** @var CustomerBooking $booking */ |
| 502 | foreach ($event->getBookings()->getItems() as $booking) { |
| 503 | $stats[$periodDate]['events'][$eventId]['bookings']++; |
| 504 | |
| 505 | if (empty($stats[$periodDate]['customers'][$booking->getCustomerId()->getValue()])) { |
| 506 | $stats[$periodDate]['customers'][$booking->getCustomerId()->getValue()] = 0; |
| 507 | } |
| 508 | |
| 509 | $stats[$periodDate]['customers'][$booking->getCustomerId()->getValue()]++; |
| 510 | |
| 511 | if ($event->getCustomPricing()->getValue()) { |
| 512 | /** @var CustomerBookingEventTicket $ticket */ |
| 513 | foreach ($booking->getTicketsBooking()->getItems() as $ticket) { |
| 514 | $ticketId = $ticket->getEventTicketId()->getValue(); |
| 515 | |
| 516 | if ( |
| 517 | $booking->getStatus()->getValue() === BookingStatus::APPROVED || |
| 518 | $booking->getStatus()->getValue() === BookingStatus::PENDING |
| 519 | ) { |
| 520 | $stats[$periodDate]['events'][$eventId]['tickets']['items'][$ticketId]['count'] += |
| 521 | $ticket->getPersons()->getValue(); |
| 522 | } |
| 523 | |
| 524 | /** @var Payment $payment */ |
| 525 | foreach ($booking->getPayments()->getItems() as $payment) { |
| 526 | $stats[$periodDate]['events'][$eventId]['tickets']['items'][$ticketId]['revenue'] += |
| 527 | $payment->getAmount()->getValue(); |
| 528 | } |
| 529 | } |
| 530 | } else { |
| 531 | if ( |
| 532 | $booking->getStatus()->getValue() === BookingStatus::APPROVED || |
| 533 | $booking->getStatus()->getValue() === BookingStatus::PENDING |
| 534 | ) { |
| 535 | $stats[$periodDate]['events'][$eventId]['spots']['count'] += |
| 536 | $booking->getPersons()->getValue(); |
| 537 | } |
| 538 | |
| 539 | /** @var Payment $payment */ |
| 540 | foreach ($booking->getPayments()->getItems() as $payment) { |
| 541 | $stats[$periodDate]['events'][$eventId]['spots']['revenue'] += |
| 542 | $payment->getAmount()->getValue(); |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | return $stats; |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * @param $params |
| 554 | * |
| 555 | * @return array |
| 556 | * @throws ContainerValueNotFoundException |
| 557 | * @throws InvalidArgumentException |
| 558 | * @throws QueryExecutionException |
| 559 | */ |
| 560 | public function getEmployeesStats($params) |
| 561 | { |
| 562 | /** @var ProviderRepository $providerRepository */ |
| 563 | $providerRepository = $this->container->get('domain.users.providers.repository'); |
| 564 | |
| 565 | $appointments = $providerRepository->getAllNumberOfAppointments($params); |
| 566 | |
| 567 | $views = $providerRepository->getAllNumberOfViews($params); |
| 568 | |
| 569 | return array_values(array_replace_recursive($appointments, $views)); |
| 570 | } |
| 571 | |
| 572 | /** |
| 573 | * @param $providerId |
| 574 | * |
| 575 | * @return void |
| 576 | * @throws ContainerValueNotFoundException |
| 577 | * @throws QueryExecutionException |
| 578 | */ |
| 579 | public function addEmployeesViewsStats($providerId) |
| 580 | { |
| 581 | /** @var ProviderRepository $providerRepository */ |
| 582 | $providerRepository = $this->container->get('domain.users.providers.repository'); |
| 583 | |
| 584 | $providerRepository->beginTransaction(); |
| 585 | |
| 586 | if (!$providerRepository->addViewStats($providerId)) { |
| 587 | $providerRepository->rollback(); |
| 588 | |
| 589 | return; |
| 590 | } |
| 591 | |
| 592 | $providerRepository->commit(); |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * @param $params |
| 597 | * |
| 598 | * @return array |
| 599 | * @throws ContainerValueNotFoundException |
| 600 | * @throws InvalidArgumentException |
| 601 | * @throws QueryExecutionException |
| 602 | */ |
| 603 | public function getServicesStats($params) |
| 604 | { |
| 605 | /** @var ServiceRepository $serviceRepository */ |
| 606 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 607 | |
| 608 | $appointments = $serviceRepository->getAllNumberOfAppointments($params); |
| 609 | |
| 610 | $views = $serviceRepository->getAllNumberOfViews($params); |
| 611 | |
| 612 | return array_values(array_replace_recursive($appointments, $views)); |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * @param $serviceId |
| 617 | * |
| 618 | * @return void |
| 619 | * @throws ContainerValueNotFoundException |
| 620 | * @throws QueryExecutionException |
| 621 | */ |
| 622 | public function addServicesViewsStats($serviceId) |
| 623 | { |
| 624 | /** @var ServiceRepository $serviceRepository */ |
| 625 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 626 | |
| 627 | $serviceRepository->beginTransaction(); |
| 628 | |
| 629 | if (!$serviceRepository->addViewStats($serviceId)) { |
| 630 | $serviceRepository->rollback(); |
| 631 | |
| 632 | return; |
| 633 | } |
| 634 | |
| 635 | $serviceRepository->commit(); |
| 636 | } |
| 637 | |
| 638 | /** |
| 639 | * @param $params |
| 640 | * |
| 641 | * @return array |
| 642 | * @throws ContainerValueNotFoundException |
| 643 | * @throws InvalidArgumentException |
| 644 | * @throws QueryExecutionException |
| 645 | */ |
| 646 | public function getLocationsStats($params) |
| 647 | { |
| 648 | /** @var LocationRepository $locationRepository */ |
| 649 | $locationRepository = $this->container->get('domain.locations.repository'); |
| 650 | |
| 651 | $appointments = $locationRepository->getAllNumberOfAppointments($params); |
| 652 | |
| 653 | $views = $locationRepository->getAllNumberOfViews($params); |
| 654 | |
| 655 | return array_values(array_replace_recursive($appointments, $views)); |
| 656 | } |
| 657 | |
| 658 | /** |
| 659 | * @param $locationId |
| 660 | * |
| 661 | * @return void |
| 662 | * @throws ContainerValueNotFoundException |
| 663 | * @throws QueryExecutionException |
| 664 | */ |
| 665 | public function addLocationsViewsStats($locationId) |
| 666 | { |
| 667 | if ($locationId) { |
| 668 | /** @var LocationRepository $locationRepository */ |
| 669 | $locationRepository = $this->container->get('domain.locations.repository'); |
| 670 | $locationRepository->beginTransaction(); |
| 671 | if (!$locationRepository->addViewStats($locationId)) { |
| 672 | $locationRepository->rollback(); |
| 673 | |
| 674 | return; |
| 675 | } |
| 676 | $locationRepository->commit(); |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 |