src/Admin/Controller/ReserveAdminController.php line 21

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Admin\Controller;
  4. use App\Entity\Client;
  5. use App\Entity\Motorcycle;
  6. use App\Entity\Reserve;
  7. use App\Entity\Type\WeekDay;
  8. use App\EntityService\ReserveEntityService;
  9. use App\Repository\MotorcycleRepository;
  10. use App\Repository\ReserveRepository;
  11. use DateTime;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Sonata\AdminBundle\Controller\CRUDController;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. class ReserveAdminController extends CRUDController
  18. {
  19.     private const DEFAULT_CALENDAR_DAYS_LIMIT  30;
  20.     private EntityManagerInterface $entityManager;
  21.     private ReserveRepository $reserveRepository;
  22.     private MotorcycleRepository $motorcycleRepository;
  23.     private ReserveEntityService $reserveEntityService;
  24.     public function __construct(
  25.         EntityManagerInterface $entityManager,
  26.         ReserveRepository $reserveRepository,
  27.         MotorcycleRepository $motorcycleRepository,
  28.         ReserveEntityService $reserveEntityService
  29.     )
  30.     {
  31.         $this->entityManager $entityManager;
  32.         $this->reserveRepository $reserveRepository;
  33.         $this->motorcycleRepository $motorcycleRepository;
  34.         $this->reserveEntityService $reserveEntityService;
  35.     }
  36.     public function getReserveCalendarAction(Request $request): JsonResponse
  37.     {
  38.         $days $request->get('days');
  39.         if (!$days) {
  40.             $days self::DEFAULT_CALENDAR_DAYS_LIMIT;
  41.         }
  42.         $mot $this->motorcycleRepository->find(7);
  43.         $searchStartDate = new \DateTime();
  44.         $searchEndDate = new \DateTime();
  45.         $searchEndDate->setTime(235959);
  46.         $searchStartDate->modify('- 1 day');
  47.         $searchEndDate->modify('- 1 day');
  48.         $graph = [];
  49.         for ($i 0$i <= $days$i++) {
  50.             $searchStartDate->setTime(0,0);
  51.             $searchStartDate->modify('+ 1 day');
  52.             $searchEndDate->modify('+ 1 day');
  53.             $result $this->reserveRepository->reserveList($searchStartDate$searchEndDate);
  54.             $reserves = [];
  55.             $result->map(static function (Reserve $reserve) use (&$reserves$searchStartDate$searchEndDate) {
  56.                 $motName $reserve->getMotorcycle()->getFullName();
  57.                 $startTime $reserve->getStartDate();
  58.                 
  59.                 $endDate $reserve->getEndDate();
  60.                 $endDay = (clone $searchEndDate)->setTime(23,59);
  61.                 if ((int)$startTime->format('d') !== (int) $searchStartDate->format('d')) {
  62.                     $startTime = clone $searchStartDate;
  63.                 }
  64.                 if ($endDate $endDay) {
  65.                     $endDate $endDay;
  66.                 }
  67.                 $list = [];
  68.                 while ($startTime <= $endDate) {
  69.                     $list[] = $startTime->format('H:i');
  70.                     $startTime->modify('+ 1 hour');
  71.                 }
  72.                 if ((int)$reserve->getEndDate()->format('d') !== (int) $searchEndDate->format('d') ||
  73.                     (int)$reserve->getEndDate()->format('m') > (int) $searchEndDate->format('m')
  74.                 ) {
  75.                     $list[] = '24:00';
  76.                 }
  77.                 
  78.                 array_pop($list);
  79.                 $reserves[$motName]['reserves'][] = $list;
  80.                 $reserves[$motName]['color'] = $reserve->getMotorcycle()->getColor();
  81.             });
  82.             $schedules $this->prepareReserves($reserves);
  83.             $graph[] = [
  84.                 'date' => sprintf(
  85.                     '(%s) %s',
  86.                     WeekDay::WEEKDAY_RU[(int)$searchStartDate->format('w')],
  87.                     $searchStartDate->format('d.m.Y')),
  88.                 'schedules' => $schedules
  89.             ];
  90.         }
  91.         return new JsonResponse($graph);
  92.     }
  93.     private function prepareReserves($reserves): array
  94.     {
  95.         $schedules = [];
  96.         foreach ($reserves as $key => $value) {
  97.             $schedules[] = [
  98.                 'name'=> $key,
  99.                 'color' => $value['color'],
  100.                 'reserves' =>  $value['reserves']
  101.             ];
  102.         }
  103.         return $schedules;
  104.     }
  105.     public function createAcceptanceCertificateAction(Request $request): Response
  106.     {
  107.         /** @var Reserve $reserve */
  108.         $reserve $this->reserveRepository->find($request->get('id'));
  109.         /** @var Client $client */
  110.         $client $reserve->getClient();
  111.         /** @var Motorcycle $motorcycle */
  112.         $motorcycle $reserve->getMotorcycle();
  113.         $this->reserveEntityService->toActiveStatus($reserve);
  114.         $this->entityManager->flush();
  115.         return $this->renderWithExtraParams('admin/Document/acceptance_certificate.html.twig', [
  116.             'currentDate' => (new DateTime())->format('d.m.Y'),
  117.             'client' => [
  118.                 'number' => $client->getUserNumber(),
  119.                 'fio' => $client->getFio(),
  120.                 'signDate' => $client->getDateSigningContract()->format('d.m.Y')
  121.             ],
  122.             'motorcycle' => [
  123.                 'name' => $motorcycle->getFullName(),
  124.                 'vin' => $motorcycle->getVinNumber(),
  125.                 'year' => $motorcycle->getIssueYear()->format('Y'),
  126.                 'registrationNumber' => $motorcycle->getRegistrationNumber(),
  127.                 'paymentAmount' => $motorcycle->getPaymentAmount()
  128.             ],
  129.             'reserve' => [
  130.                 'amount' => $reserve->getAmount(),
  131.                 'amountWords' => (new \MessageFormatter('ru-RU''{n, spellout}'))->format(['n' => $reserve->getAmount()]),
  132.                 'startDate' => $reserve->getStartDate()->format('d.m.Y H:i'),
  133.                 'endDate' => $reserve->getEndDate()->format('d.m.Y H:i'),
  134.                 'returnDate' => $reserve->getEndDate()->format('d.m.Y'),
  135.                 'returnTimeIntervalStart' => $reserve->getEndDate()->format('H:i'),
  136.                 'returnTimeIntervalEnd' => (clone $reserve->getEndDate())->modify('+ 15 minutes')->format('H:i'),
  137.             ]
  138.         ]);
  139.     }
  140. }