← Back
Editing: Utils.php
<?php namespace App\Service; use App\Entity\User; use Doctrine\Common\Collections\ArrayCollection; use PhpOffice\PhpSpreadsheet\Reader\Csv; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; class Utils { /** * @param User[] $agents * * @throws \Exception */ public function sortAgentsByPercentDone(array $agents, AgentService $agentService, string $sort = 'DESC'): ArrayCollection { uasort( $agents, function ($a, $b) use ($sort, $agentService) { if (in_array('ROLE_MANAGER', $b->getArrayRoles())) { return -1; } if (in_array('ROLE_MANAGER', $a->getArrayRoles())) { return 1; } $percentA = $agentService->setAgent($a)->getPercentDone(); $percentB = $agentService->setAgent($b)->getPercentDone(); if ('ASC' === $sort) { return ($percentA < $percentB) ? -1 : 1; } return ($percentA >= $percentB) ? -1 : 1; } ); return new ArrayCollection($agents); } /** * @return \DateTime[] * * @throws \Exception */ public function getControleRangeDate($frequency) { switch ($frequency) { case 'JOUR': $startStr = 'today'; $endStr = 'tomorrow'; break; case 'SEMAINE': $startStr = 'midnight last monday'; if (1 == date('N')) { $startStr = 'midnight'; } $endStr = 'midnight next monday'; break; case 'MOIS': $startStr = 'midnight first day of this month'; $endStr = 'midnight first day of next month'; break; case 'ANNEE': $startStr = 'first day of january this year'; $endStr = 'first day of january next year'; break; } $startDate = new \DateTime($startStr); $endDate = new \DateTime($endStr); return [ 'startDate' => $startDate, 'endDate' => $endDate, ]; } public function getMonthsTimeline( \DateTimeInterface $startDate, \DateTimeInterface $endDate, bool $forApi = false, string $format = 'MMM yy' ): array { $tz = new \DateTimeZone('Europe/Paris'); $cloneStartDate = \DateTime::createFromInterface($startDate); $cloneEndDate = \DateTime::createFromInterface($endDate); $cloneStartDate->setTimezone($tz); $cloneEndDate->setTimezone($tz); $formatter = new \IntlDateFormatter( 'fr_FR', \IntlDateFormatter::TRADITIONAL, \IntlDateFormatter::TRADITIONAL, $tz ); $formatter->setPattern($format); $cloneStartDate->modify('midnight first day of this month'); $cloneEndDate->modify('midnight first day of next month'); $cloneEndDate->sub(new \DateInterval('PT1S')); $listMonth = []; while ($cloneStartDate <= $cloneEndDate) { $key = $formatter->format($cloneStartDate); $listMonth[$key] = clone $cloneStartDate; $cloneStartDate->add(new \DateInterval('P1M')); } if ($forApi) { return $this->formatForApi($listMonth); } return $listMonth; } /** * @return array */ private function formatForApi(array $listMonth) { $result = []; foreach ($listMonth as $key => $month) { $result[] = [ 'label' => $key, 'idx' => $month->format('Y-m'), ]; } return $result; } public function calculateEvolution($total, $prevTotal): float { if ($prevTotal > 0) { return (($total - $prevTotal) / $prevTotal) * 100; } return $total * 100; } public function imageToBase64($path): string { $type = pathinfo($path, PATHINFO_EXTENSION); $data = @file_get_contents($path); return 'data:image/'.$type.';base64,'.base64_encode($data); } public function csvToSpreadsheet(string $csv): Xlsx { $reader = new Csv(); $reader->setDelimiter(','); $spreadsheet = $reader->loadSpreadsheetFromString($csv); return new Xlsx($spreadsheet); } }
Save File
Cancel