$statsType * @return array */ public function getStats(array $statsType, CarbonInterface $start, CarbonInterface $end): array { $stats = []; foreach ($statsType as $type) { $previous = $this->getPrevious(sprintf('get%s', ucfirst((string) $type->name)), $start, $end); $current = $this->{sprintf('get%s', ucfirst((string) $type->name))}($start, $end); $stats[$type->value] = [ 'previous' => $previous, 'current' => $current, 'diff' => $current - $previous, ]; } return $stats; } /** * getPrevious */ private function getPrevious(string $method, CarbonInterface $start, CarbonInterface $end): int | float { $previousDates = $this->getPreviousPeriod($start, $end); return $this->{$method}($previousDates->start, $previousDates->end); } /** * Get all validated orders. */ private function getNbSalarie(CarbonInterface $start, CarbonInterface $end): int { return $this->employeeDirectory->getNbSalarie($start, $end); } /** * Get all validated orders. */ private function getNbSalarieWithAccount(CarbonInterface $start, CarbonInterface $end): int { return $this->getNumberSalaries($start, $end); } /** * Get the percentage of registered people / people with a generated code */ public function getNumberSalaries(CarbonInterface $start, CarbonInterface $end): int { return $this->customerDirectory->getNumberSalariesWithAccount($start, $end); } /** * Get the percentage of registered people / people with a generated code */ public function getMembershipRate(CarbonInterface $start, CarbonInterface $end): float { $registered = $this->getNumberSalaries($start, $end); $employee = $this->employeeDirectory->getNbSalarie($start, $end); return $employee ? round($registered / $employee * 100, 2) : 0.0; } /** * Get all validated orders. * * @return Collection */ private function getValidatedOrders(CarbonInterface $start, CarbonInterface $end): Collection { return $this->orderDirectory->getValidatedOrders($start, $end); } /** * Get the number of validated command. */ private function getNbValidateCmd(CarbonInterface $start, CarbonInterface $end): int { return $this->getValidatedOrders($start, $end)->count(); } /** * Get the number of waiting command. */ private function getNbWaitingCmd(CarbonInterface $start, CarbonInterface $end): int { return $this->getOrdersByStatus(OrderStatus::WAITING, $start, $end)->count(); } /** * Get the number of canceled command. */ private function getNbCanceledCmd(CarbonInterface $start, CarbonInterface $end): int { return $this->getOrdersByStatus(OrderStatus::CANCELED, $start, $end)->count(); } /** * Get orders by status. * * @return Collection */ private function getOrdersByStatus(OrderStatus $statusId, CarbonInterface $start, CarbonInterface $end): Collection { return $this->orderDirectory->getOrdersByStatus($statusId, $start, $end); } /** * Get the canceled command rate. */ private function getCanceledRate(CarbonInterface $start, CarbonInterface $end): float { $nbWaitingCmd = $this->getNbWaitingCmd($start, $end); $nbCanceledCmd = $this->getNbCanceledCmd($start, $end); $nbValidateCmd = $this->getNbValidateCmd($start, $end); // Prevent division by 0. if (($nbValidateCmd + $nbWaitingCmd + $nbCanceledCmd) !== 0) { return round( (($nbWaitingCmd + $nbCanceledCmd) / ($nbValidateCmd + $nbWaitingCmd + $nbCanceledCmd)) * 100, 2 ); } return 0.0; } /** * Get the order frequency. */ private function getOrderFrequency(CarbonInterface $start, CarbonInterface $end): float { $nbValidateCmd = $this->getNbValidateCmd($start, $end); $period = new CarbonPeriod($start, $end)->count(); return round($nbValidateCmd / $period, 2); } /** * Get the order frequency. */ private function getPurchaseFrequency(CarbonInterface $start, CarbonInterface $end): float { $activeClients = $this->getNbActiveClients($start, $end); if (! $activeClients) { return 0.0; } $nbValidateCmd = $this->getNbValidateCmd($start, $end); return round($nbValidateCmd / $activeClients, 2); } /** * Get the number of active clients (ordered in the period). */ private function getNbActiveClients(CarbonInterface $start, CarbonInterface $end): int { return $this->orderDirectory->getNbActiveClients($start, $end); } /** * Get the number command where the collect day is 7 days after the order. */ private function getNbCmdWith7DayDelay(CarbonInterface $start, CarbonInterface $end): int { return $this->orderDirectory->getNbCmdWith7DayDelay($start, $end); } /** * Get the number command where the collect day is 14 days after the order. */ private function getNbCmdWith14DayDelay(CarbonInterface $start, CarbonInterface $end): int { return $this->orderDirectory->getNbCmdWith14DayDelay($start, $end); } /** * Get the CA HT. */ private function getCaHt(CarbonInterface $start, CarbonInterface $end): float { return $this->orderDirectory->getCaHt($start, $end); } /** * Get the CA TTC. */ private function getCaTtc(CarbonInterface $start, CarbonInterface $end): float { return $this->orderDirectory->getCaTtc($start, $end); } /** * Get the average basket HT. */ private function getAverageBasketHt(CarbonInterface $start, CarbonInterface $end): float { // Prevent division by 0. if ($this->getNbValidateCmd($start, $end) !== 0) { return round($this->getCaHt($start, $end) / $this->getNbValidateCmd($start, $end), 2); } return 0.0; } /** * Get the average product count per order. */ private function getAverageProductCountPerOrder(CarbonInterface $start, CarbonInterface $end): float { $nbValidateCmd = $this->getNbValidateCmd($start, $end); // Prevent division by 0. if ($nbValidateCmd !== 0) { $totalQty = $this->orderDirectory->getTotalQuantity($start, $end); return round((float) $totalQty / $nbValidateCmd, 2); } return 0.0; } /** * Get the previous period of the selected date. * * @return object{start:\Carbon\CarbonInterface,end:\Carbon\CarbonInterface} */ public function getPreviousPeriod(CarbonInterface $start, CarbonInterface $end): object { $diffDays = $start->copy()->diffInDays($end); return (object) [ 'start' => $start->copy()->subDays($diffDays + 1), 'end' => $start->copy()->subDay(), ]; } /** * Get the percentage of registered people / people with a generated code */ public function getMembershipPercentage(): float { $employee = $this->employeeDirectory->getTotalEmployees(); $registered = $this->customerDirectory->getTotalRegisteredSalaries(); return round($registered / $employee * 100, 2); } }