<?php
declare(strict_types=1);
namespace App\Admin;
use App\Entity\Reserve;
use App\Entity\Type\RentStatusType;
use App\Repository\ReserveRepository;
use Doctrine\ORM\EntityManagerInterface;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Form\Type\ModelType;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\Form\Validator\ErrorElement;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
final class ReserveAdmin extends AbstractAdmin
{
private EntityManagerInterface $entityManager;
protected $datagridValues = [
'_sort_order' => 'DESC',
'_sort_by' => 'id'
];
protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
{
$datagridMapper
->add('id')
->add('startDate')
->add('endDate')
;
}
protected function configureListFields(ListMapper $listMapper): void
{
$listMapper->add('motorcycle');
$listMapper->add('client');
$listMapper->add('startDate', null, ['template' => 'admin/Reserve/list_start_date.html.twig']);
$listMapper->add('endDate', null, ['template' => 'admin/Reserve/list_end_date.html.twig']);
$listMapper->add('amount', null, ['template' => 'admin/Reserve/list_amount.html.twig']);
$listMapper->add('acceptanceCertificate', null, [
'label' => 'Действия',
'template' => 'admin/Reserve/list_create_acceptance_certificate.html.twig'
]);
}
protected function configureFormFields(FormMapper $formMapper): void
{
$formMapper->with('Данные резерва', ['class' => 'col-md-6 box-transition']);
$formMapper->add('motorcycle', ModelType::class, [
'btn_add' => false,
'multiple' => false,
'label' => 'Мотоцикл',
'error_bubbling' => true
]);
$formMapper->add('startDate', DateTimeType::class, [
'widget' => 'single_text',
'required' => false,
'error_bubbling' => true,
'label' => 'Дана и время начала аренды',
]);
$formMapper->add('endDate', DateTimeType::class, [
'widget' => 'single_text',
'required' => false,
'error_bubbling' => true,
'label' => 'Дана и время окончания аренды',
]);
$formMapper->add('amount', null, ['label' => 'Стоимость аренды (BYN)']);
$formMapper->end();
$formMapper->with('Клиент', ['class' => 'col-md-6 box-transition']);
$formMapper->add('client', ModelType::class, [
'btn_add' => true,
'multiple' => false,
'label' => 'Клиент',
]);
$formMapper->add('notes', null, ['label' => 'Заметки']);
$formMapper->end();
}
protected function configureShowFields(ShowMapper $showMapper): void
{
$showMapper
->add('id')
->add('startDate')
->add('endDate')
;
}
/**
* {@inheritdoc}
* @param $object Reserve
*/
public function validate(ErrorElement $errorElement, $object) {
/** @var ReserveRepository $reserveRepository */
$reserveRepository = $this->entityManager->getRepository(Reserve::class);
$intersections = $reserveRepository->reserveList(clone $object->getStartDate(),(clone $object->getEndDate())->modify('- 1 second'), $object->getId(), $object->getMotorcycle());
if ($intersections->count() > 0) {
$intersections->map(static function (Reserve $reserve) use (&$errorElement) {
$errorElement->with('motorcycle')->addViolation(sprintf('Пересечение: %s - %s (%s)',
$reserve->getStartDate()->format('Y-m-d H:i:s'),
$reserve->getEndDate()->format('Y-m-d H:i:s'),
$reserve->getClient()
))->end();
});
}
}
/**
* @param Reserve $object
*/
public function prePersist($object): void
{
$object->setEndDate($object->getEndDate()->modify('- 1 second'));
$object->setStatus(RentStatusType::APPROVED);
}
/**
* @param Reserve $object
*/
public function postPersist($object): void
{
}
/**
* @required
* @param EntityManagerInterface $entityManager
*/
public function setEntityManager(EntityManagerInterface $entityManager): void
{
$this->entityManager = $entityManager;
}
protected function configureRoutes(RouteCollection $collection): void
{
parent::configureRoutes($collection);
$collection->add('get_reserve_calendar', 'get-reserve-calendar');
$collection->add('create_acceptance_certificate', 'create-acceptance-certificate');
}
}