<?php
declare(strict_types=1);
namespace App\Admin\Controller;
use App\Entity\Client;
use App\Entity\Motorcycle;
use App\Entity\Reserve;
use App\Entity\Type\WeekDay;
use App\EntityService\ReserveEntityService;
use App\Repository\MotorcycleRepository;
use App\Repository\ReserveRepository;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Sonata\AdminBundle\Controller\CRUDController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class ReserveAdminController extends CRUDController
{
private const DEFAULT_CALENDAR_DAYS_LIMIT = 30;
private EntityManagerInterface $entityManager;
private ReserveRepository $reserveRepository;
private MotorcycleRepository $motorcycleRepository;
private ReserveEntityService $reserveEntityService;
public function __construct(
EntityManagerInterface $entityManager,
ReserveRepository $reserveRepository,
MotorcycleRepository $motorcycleRepository,
ReserveEntityService $reserveEntityService
)
{
$this->entityManager = $entityManager;
$this->reserveRepository = $reserveRepository;
$this->motorcycleRepository = $motorcycleRepository;
$this->reserveEntityService = $reserveEntityService;
}
public function getReserveCalendarAction(Request $request): JsonResponse
{
$days = $request->get('days');
if (!$days) {
$days = self::DEFAULT_CALENDAR_DAYS_LIMIT;
}
$mot = $this->motorcycleRepository->find(7);
$searchStartDate = new \DateTime();
$searchEndDate = new \DateTime();
$searchEndDate->setTime(23, 59, 59);
$searchStartDate->modify('- 1 day');
$searchEndDate->modify('- 1 day');
$graph = [];
for ($i = 0; $i <= $days; $i++) {
$searchStartDate->setTime(0,0);
$searchStartDate->modify('+ 1 day');
$searchEndDate->modify('+ 1 day');
$result = $this->reserveRepository->reserveList($searchStartDate, $searchEndDate);
$reserves = [];
$result->map(static function (Reserve $reserve) use (&$reserves, $searchStartDate, $searchEndDate) {
$motName = $reserve->getMotorcycle()->getFullName();
$startTime = $reserve->getStartDate();
$endDate = $reserve->getEndDate();
$endDay = (clone $searchEndDate)->setTime(23,59);
if ((int)$startTime->format('d') !== (int) $searchStartDate->format('d')) {
$startTime = clone $searchStartDate;
}
if ($endDate > $endDay) {
$endDate = $endDay;
}
$list = [];
while ($startTime <= $endDate) {
$list[] = $startTime->format('H:i');
$startTime->modify('+ 1 hour');
}
if ((int)$reserve->getEndDate()->format('d') !== (int) $searchEndDate->format('d') ||
(int)$reserve->getEndDate()->format('m') > (int) $searchEndDate->format('m')
) {
$list[] = '24:00';
}
array_pop($list);
$reserves[$motName]['reserves'][] = $list;
$reserves[$motName]['color'] = $reserve->getMotorcycle()->getColor();
});
$schedules = $this->prepareReserves($reserves);
$graph[] = [
'date' => sprintf(
'(%s) %s',
WeekDay::WEEKDAY_RU[(int)$searchStartDate->format('w')],
$searchStartDate->format('d.m.Y')),
'schedules' => $schedules
];
}
return new JsonResponse($graph);
}
private function prepareReserves($reserves): array
{
$schedules = [];
foreach ($reserves as $key => $value) {
$schedules[] = [
'name'=> $key,
'color' => $value['color'],
'reserves' => $value['reserves']
];
}
return $schedules;
}
public function createAcceptanceCertificateAction(Request $request): Response
{
/** @var Reserve $reserve */
$reserve = $this->reserveRepository->find($request->get('id'));
/** @var Client $client */
$client = $reserve->getClient();
/** @var Motorcycle $motorcycle */
$motorcycle = $reserve->getMotorcycle();
$this->reserveEntityService->toActiveStatus($reserve);
$this->entityManager->flush();
return $this->renderWithExtraParams('admin/Document/acceptance_certificate.html.twig', [
'currentDate' => (new DateTime())->format('d.m.Y'),
'client' => [
'number' => $client->getUserNumber(),
'fio' => $client->getFio(),
'signDate' => $client->getDateSigningContract()->format('d.m.Y')
],
'motorcycle' => [
'name' => $motorcycle->getFullName(),
'vin' => $motorcycle->getVinNumber(),
'year' => $motorcycle->getIssueYear()->format('Y'),
'registrationNumber' => $motorcycle->getRegistrationNumber(),
'paymentAmount' => $motorcycle->getPaymentAmount()
],
'reserve' => [
'amount' => $reserve->getAmount(),
'amountWords' => (new \MessageFormatter('ru-RU', '{n, spellout}'))->format(['n' => $reserve->getAmount()]),
'startDate' => $reserve->getStartDate()->format('d.m.Y H:i'),
'endDate' => $reserve->getEndDate()->format('d.m.Y H:i'),
'returnDate' => $reserve->getEndDate()->format('d.m.Y'),
'returnTimeIntervalStart' => $reserve->getEndDate()->format('H:i'),
'returnTimeIntervalEnd' => (clone $reserve->getEndDate())->modify('+ 15 minutes')->format('H:i'),
]
]);
}
}