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