AddStatsCommand.php
6 years ago
AddStatsCommandHandler.php
2 years ago
GetStatsCommand.php
7 years ago
GetStatsCommandHandler.php
2 years ago
GetStatsCommandHandler.php
217 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @copyright © TMS-Plugins. All rights reserved. |
| 4 | * @licence See LICENCE.md for license details. |
| 5 | */ |
| 6 | |
| 7 | namespace AmeliaBooking\Application\Commands\Stats; |
| 8 | |
| 9 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 10 | use AmeliaBooking\Application\Commands\CommandResult; |
| 11 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 12 | use AmeliaBooking\Application\Services\Bookable\AbstractPackageApplicationService; |
| 13 | use AmeliaBooking\Application\Services\Stats\StatsService; |
| 14 | use AmeliaBooking\Domain\Collection\Collection; |
| 15 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 16 | use AmeliaBooking\Domain\Entity\Booking\Appointment\Appointment; |
| 17 | use AmeliaBooking\Domain\Entity\Entities; |
| 18 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 19 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 20 | use AmeliaBooking\Domain\ValueObjects\String\BookingStatus; |
| 21 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 22 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\AppointmentRepository; |
| 23 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository; |
| 24 | use Exception; |
| 25 | use Interop\Container\Exception\ContainerException; |
| 26 | use Slim\Exception\ContainerValueNotFoundException; |
| 27 | |
| 28 | /** |
| 29 | * Class GetStatsCommandHandler |
| 30 | * |
| 31 | * @package AmeliaBooking\Application\Commands\Stats |
| 32 | */ |
| 33 | class GetStatsCommandHandler extends CommandHandler |
| 34 | { |
| 35 | /** |
| 36 | * @param GetStatsCommand $command |
| 37 | * |
| 38 | * @return CommandResult |
| 39 | * @throws ContainerValueNotFoundException |
| 40 | * @throws AccessDeniedException |
| 41 | * @throws InvalidArgumentException |
| 42 | * @throws QueryExecutionException |
| 43 | * @throws Exception |
| 44 | * @throws ContainerException |
| 45 | */ |
| 46 | public function handle(GetStatsCommand $command) |
| 47 | { |
| 48 | if (!$command->getPermissionService()->currentUserCanRead(Entities::DASHBOARD)) { |
| 49 | throw new AccessDeniedException('You are not allowed to read coupons.'); |
| 50 | } |
| 51 | |
| 52 | $result = new CommandResult(); |
| 53 | |
| 54 | /** @var AppointmentRepository $appointmentRepo */ |
| 55 | $appointmentRepo = $this->container->get('domain.booking.appointment.repository'); |
| 56 | /** @var StatsService $statsAS */ |
| 57 | $statsAS = $this->container->get('application.stats.service'); |
| 58 | /** @var SettingsService $settingsDS */ |
| 59 | $settingsDS = $this->container->get('domain.settings.service'); |
| 60 | /** @var AbstractPackageApplicationService $packageAS */ |
| 61 | $packageAS = $this->container->get('application.bookable.package'); |
| 62 | |
| 63 | $startDate = $command->getField('params')['dates'][0] . ' 00:00:00'; |
| 64 | |
| 65 | $endDate = $command->getField('params')['dates'][1] . ' 23:59:59'; |
| 66 | |
| 67 | $previousPeriodStart = DateTimeService::getCustomDateTimeObject($startDate); |
| 68 | |
| 69 | $previousPeriodEnd = DateTimeService::getCustomDateTimeObject($endDate); |
| 70 | |
| 71 | $numberOfDays = $previousPeriodEnd->diff($previousPeriodStart)->days + 1; |
| 72 | |
| 73 | $serviceStatsParams = ['dates' => [$startDate, $endDate]]; |
| 74 | |
| 75 | $customerStatsParams = ['dates' => [$startDate, $endDate]]; |
| 76 | |
| 77 | $locationStatsParams = ['dates' => [$startDate, $endDate]]; |
| 78 | |
| 79 | $employeeStatsParams = ['dates' => [$startDate, $endDate]]; |
| 80 | |
| 81 | $appointmentStatsParams = ['dates' => [$startDate, $endDate], 'status' => BookingStatus::APPROVED]; |
| 82 | |
| 83 | // Statistic |
| 84 | $selectedPeriodStatistics = $statsAS->getRangeStatisticsData($appointmentStatsParams); |
| 85 | |
| 86 | $previousPeriodStatistics = $statsAS->getRangeStatisticsData( |
| 87 | array_merge( |
| 88 | $appointmentStatsParams, |
| 89 | [ |
| 90 | 'dates' => [ |
| 91 | $previousPeriodStart->modify("-{$numberOfDays} day")->format('Y-m-d H:i:s'), |
| 92 | $previousPeriodEnd->modify("-{$numberOfDays} day")->format('Y-m-d H:i:s'), |
| 93 | ] |
| 94 | ] |
| 95 | ) |
| 96 | ); |
| 97 | |
| 98 | // Charts |
| 99 | $customersStats = $statsAS->getCustomersStats($customerStatsParams); |
| 100 | |
| 101 | $employeesStats = $statsAS->getEmployeesStats($employeeStatsParams); |
| 102 | |
| 103 | $servicesStats = $statsAS->getServicesStats($serviceStatsParams); |
| 104 | |
| 105 | $locationsStats = $statsAS->getLocationsStats($locationStatsParams); |
| 106 | |
| 107 | /** @var Collection $periodAppointments */ |
| 108 | $periodAppointments = $appointmentRepo->getPeriodAppointments( |
| 109 | [ |
| 110 | 'dates' => [ |
| 111 | DateTimeService::getNowDateTime(), |
| 112 | ], |
| 113 | 'page' => 1 |
| 114 | ], |
| 115 | 10 |
| 116 | ); |
| 117 | |
| 118 | /** @var Collection $upcomingAppointments */ |
| 119 | $upcomingAppointments = $periodAppointments->length() ? $appointmentRepo->getFiltered( |
| 120 | array_merge( |
| 121 | [ |
| 122 | 'ids' => $periodAppointments->keys(), |
| 123 | 'skipProviders' => true, |
| 124 | ] |
| 125 | ) |
| 126 | ) : new Collection(); |
| 127 | |
| 128 | $currentDateTime = DateTimeService::getNowDateTimeObject(); |
| 129 | |
| 130 | $upcomingAppointmentsArr = []; |
| 131 | |
| 132 | $todayApprovedAppointmentsCount = 0; |
| 133 | |
| 134 | $todayPendingAppointmentsCount = 0; |
| 135 | |
| 136 | $todayDateString = explode(' ', DateTimeService::getNowDateTime())[0]; |
| 137 | |
| 138 | $packageAS->setPackageBookingsForAppointments($upcomingAppointments); |
| 139 | |
| 140 | $customersNoShowCount = []; |
| 141 | |
| 142 | $customersNoShowCountIds = []; |
| 143 | |
| 144 | $noShowTagEnabled = $settingsDS->getSetting('roles', 'enableNoShowTag'); |
| 145 | |
| 146 | |
| 147 | /** @var Appointment $appointment */ |
| 148 | foreach ($upcomingAppointments->getItems() as $appointment) { |
| 149 | if ($appointment->getBookingStart()->getValue()->format('Y-m-d') === $todayDateString) { |
| 150 | if ($appointment->getStatus()->getValue() === BookingStatus::APPROVED) { |
| 151 | $todayApprovedAppointmentsCount++; |
| 152 | } |
| 153 | |
| 154 | if ($appointment->getStatus()->getValue() === BookingStatus::PENDING) { |
| 155 | $todayPendingAppointmentsCount++; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | $minimumCancelTimeInSeconds = $settingsDS |
| 160 | ->getEntitySettings($appointment->getService()->getSettings()) |
| 161 | ->getGeneralSettings() |
| 162 | ->getMinimumTimeRequirementPriorToCanceling(); |
| 163 | |
| 164 | $minimumCancelTime = DateTimeService::getCustomDateTimeObject( |
| 165 | $appointment->getBookingStart()->getValue()->format('Y-m-d H:i:s') |
| 166 | )->modify("-{$minimumCancelTimeInSeconds} seconds"); |
| 167 | |
| 168 | $upcomingAppointmentsArr[] = array_merge( |
| 169 | $appointment->toArray(), |
| 170 | [ |
| 171 | 'cancelable' => $currentDateTime <= $minimumCancelTime, |
| 172 | 'past' => $currentDateTime >= $appointment->getBookingStart()->getValue() |
| 173 | ] |
| 174 | ); |
| 175 | |
| 176 | foreach ($appointment->getBookings()->getItems() as $booking) { |
| 177 | if ($noShowTagEnabled && !in_array($booking->getCustomerId()->getValue(), $customersNoShowCountIds)) { |
| 178 | $customersNoShowCountIds[] = $booking->getCustomerId()->getValue(); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if ($noShowTagEnabled && $customersNoShowCountIds) { |
| 184 | /** @var CustomerBookingRepository $bookingRepository */ |
| 185 | $bookingRepository = $this->container->get('domain.booking.customerBooking.repository'); |
| 186 | |
| 187 | $customersNoShowCount = $bookingRepository->countByNoShowStatus($customersNoShowCountIds); |
| 188 | } |
| 189 | |
| 190 | $selectedPeriodStatistics = apply_filters('amelia_get_stats_filter', $selectedPeriodStatistics); |
| 191 | |
| 192 | do_action('amelia_get_stats', $selectedPeriodStatistics); |
| 193 | |
| 194 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 195 | $result->setMessage('Successfully retrieved appointments.'); |
| 196 | $result->setData( |
| 197 | [ |
| 198 | 'count' => [ |
| 199 | 'approved' => $todayApprovedAppointmentsCount, |
| 200 | 'pending' => $todayPendingAppointmentsCount, |
| 201 | ], |
| 202 | 'selectedPeriodStats' => $selectedPeriodStatistics, |
| 203 | 'previousPeriodStats' => $previousPeriodStatistics, |
| 204 | 'employeesStats' => $employeesStats, |
| 205 | 'servicesStats' => $servicesStats, |
| 206 | 'locationsStats' => $locationsStats, |
| 207 | 'customersStats' => $customersStats, |
| 208 | Entities::APPOINTMENTS => $upcomingAppointmentsArr, |
| 209 | 'appointmentsCount' => 10, |
| 210 | 'customersNoShowCount' => $customersNoShowCount |
| 211 | ] |
| 212 | ); |
| 213 | |
| 214 | return $result; |
| 215 | } |
| 216 | } |
| 217 |