src/EventSubscriber/CalendarSubscriber.php line 64

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\CustomerQuotations;
  4. use App\Repository\CustomerQuotationsRepository;
  5. use App\Repository\ManufacturerRepository;
  6. use App\Repository\UserRepository;
  7. use App\Service\Helpers\DateHelper;
  8. use App\Service\Log\QuotationLogService;
  9. use CalendarBundle\CalendarEvents;
  10. use CalendarBundle\Entity\Event;
  11. use CalendarBundle\Event\CalendarEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. class CalendarSubscriber implements EventSubscriberInterface
  16. {
  17.     private const COLOR = [
  18.         CustomerQuotations::STATUS_PRODUCTION => "#ADAABF",
  19.         CustomerQuotations::STATUS_PRODUCED => "#92828D",
  20.         CustomerQuotations::STATUS_PRODUCTION_FINISHED => "#CEB5A7",
  21.         CustomerQuotations::STATUS_DELIVERED => "#EBEFBF",
  22.         CustomerQuotations::STATUS_TRANSPORT => "#EAF7CF",
  23.         CustomerQuotations::STATUS_FINISHED => "#FCD3DE",
  24.         CustomerQuotations::STATUS_ACCEPTED => "#FCD3DE",
  25.         CustomerQuotations::STATUS_WAITING_PAYMENT => "#FCD3DE",
  26.     ];
  27.     private CustomerQuotationsRepository $quotationsRepo;
  28.     private QuotationLogService $logService;
  29.     private UrlGeneratorInterface $router;
  30.     private UserRepository $userRepo;
  31.     private TranslatorInterface $translator;
  32.     private DateHelper $dateHelper;
  33.     private ManufacturerRepository $manufacturerRepo;
  34.     public function __construct(
  35.         CustomerQuotationsRepository $quotationsRepo,
  36.         UserRepository               $userRepo,
  37.         ManufacturerRepository $manufacturerRepo,
  38.         QuotationLogService          $logService,
  39.         TranslatorInterface          $translator,
  40.         UrlGeneratorInterface        $router,
  41.         DateHelper                   $dateHelper
  42.     )
  43.     {
  44.         $this->quotationsRepo $quotationsRepo;
  45.         $this->manufacturerRepo $manufacturerRepo;
  46.         $this->userRepo $userRepo;
  47.         $this->logService $logService;
  48.         $this->translator $translator;
  49.         $this->router $router;
  50.         $this->dateHelper $dateHelper;
  51.     }
  52.     public static function getSubscribedEvents(): array
  53.     {
  54.         return [
  55.             CalendarEvents::SET_DATA => 'onCalendarSetData',
  56.         ];
  57.     }
  58.     public function onCalendarSetData(CalendarEvent $calendar)
  59.     {
  60.         $filters $this->normalizeFilters($calendar->getFilters());
  61.         $quotations $this->getQuotations($filters);
  62.         foreach ($quotations as $quotation) {
  63.             $event $this->createEvent($quotation);
  64.             if ($event) {
  65.                 $calendar->addEvent($event);
  66.             }
  67.         }
  68.     }
  69.     private function normalizeFilters(array $filters): array
  70.     {
  71.         $filters['status'] = $filters['status'] === "[]" null $filters['status'];
  72.         $filters['manager'] = $filters['manager'] ? $this->userRepo->findByUuid($filters['manager']) : null;
  73.         if ($filters['status']) {
  74.             $filters['status'] = preg_replace('/\d+/u'"", (str_replace(["&quot""[""{""}""]"";"":"], ""$filters['status'])));
  75.             $filters['status'] = explode(','$filters['status']);
  76.         }
  77.         $filters['manufacturer'] = $filters['manufacturer'] ? $this->manufacturerRepo->findByUuid($filters['manufacturer']) : null;
  78.         return $filters;
  79.     }
  80.     private function getQuotations(array $filters)
  81.     {
  82.         $defaultStatuses = [
  83.             CustomerQuotations::STATUS_PRODUCTION,
  84.             CustomerQuotations::STATUS_PRODUCED,
  85.             CustomerQuotations::STATUS_PRODUCTION_FINISHED,
  86.             CustomerQuotations::STATUS_TRANSPORT,
  87.             CustomerQuotations::STATUS_DELIVERED,
  88.             CustomerQuotations::STATUS_FINISHED,
  89.         ];
  90.         $filter = [
  91.             'inStatus' => $filters['status'] ?? $defaultStatuses,
  92.             'manager' => $filters['manager'],
  93.             'inquiryManufacturer' => $filters['manufacturer'],
  94.         ];
  95.         if ($filters['quotation']) {
  96.             $quotations = [$this->quotationsRepo->findByUuid($filters['quotation'])];
  97.         } else {
  98.             $quotations $this->quotationsRepo->getAll($filter);
  99.         }
  100.         return $quotations;
  101.     }
  102.     private function createEvent($quotation): ?Event
  103.     {
  104.         $orderInquiry $quotation->getOrderInquiry();
  105.         $log $this->logService->getRegisteredLogForStatus($quotationCustomerQuotations::STATUS_PRODUCTION);
  106.         if (!$log || !$orderInquiry) {
  107.             return null;
  108.         }
  109.         $leadDays $orderInquiry->getLeadTime();
  110.         $realLeadDays null;
  111.         $producedDate = (clone $log->getDate())->modify('+' $orderInquiry->getLeadTime() . ' day');
  112.         $actualProducedDate null;
  113.         $eventTitle '#' $quotation->getOrderNumber() . ' ' .
  114.             $quotation->getCustomer()->getName() . ' ' .
  115.             $orderInquiry->getManufacturer()->getCode() . ' (' .
  116.             $this->translator->trans($quotation->getStatus()) . ')';
  117.         if ($orderInquiry->getDateProduced() &&
  118.             $orderInquiry->getDateProduced()->setTime(000) !== $producedDate->setTime(000)) {
  119.             $actualProducedDate $orderInquiry->getDateProduced();
  120.             $realLeadDays $this->dateHelper->diffInDaysBetweenTwoDates($log->getDate(), $actualProducedDate) + 1;
  121.         }
  122.         $orderEvent = new Event(
  123.             $eventTitle,
  124.             ($actualProducedDate ?? $producedDate)
  125.         );
  126.         $orderEvent->setOptions([
  127.             'backgroundColor' => $actualProducedDate '#f2c5b0' 'light-gray',
  128.             'borderColor' => self::COLOR[$quotation->getStatus()],
  129.             'id' => $quotation->getUuid(),
  130.             'extendedProps' => [
  131.                 'inProductionDate' => ($log->getDate())->format('d.m.y'),
  132.                 'deliveryDate' => $orderInquiry->getDeliveryTime() ? (clone $producedDate)->modify('+' $orderInquiry->getDeliveryTime() . ' day')->format('d.m.y') : $producedDate->format('d.m.y'),
  133.                 'actualDeliveryDate' => $orderInquiry->getDeliveryTime() ? (clone($actualProducedDate ?? $producedDate))->modify('+' $orderInquiry->getDeliveryTime() . ' day')->format('d.m.y') : (clone($actualProducedDate ?? $producedDate))->format('d.m.y'),
  134.                 'leadDays' => $leadDays ' Days',
  135.                 'realLeadDays' => ($realLeadDays ?? $leadDays) . ' Days',
  136.                 'deliveryDays' => $orderInquiry->getDeliveryTime() . ' Days',
  137.                 'deliveryComment' => $orderInquiry->getDeliveryComment() ?? "",
  138.                 'direction' => $orderInquiry->getManufacturer()->getAddressDetails()['country'] . ' -> ' $quotation->getCustomer()->getAddressDetails()['country'],
  139.                 'orderNumber' => '#' $quotation->getOrderNumber(),
  140.                 'orderStatus' => $this->translator->trans($quotation->getStatus()),
  141.                 'orderUrl' => $this->router->generate('quotations.open', [
  142.                     'uuid' => $quotation->getUuid(),
  143.                 ]),
  144.                 'customer' => $quotation->getCustomer()->getName(),
  145.                 'customerUrl' => $this->router->generate('customers.open', [
  146.                     'uuid' => $quotation->getCustomer()->getUuid(),
  147.                 ]),
  148.                 'manufacturer' => $orderInquiry->getManufacturer()->getName() . ' ' $orderInquiry->getManufacturer()->getCode(),
  149.                 'manufacturerUrl' => $this->router->generate('manufacturers.open', [
  150.                     'uuid' => $orderInquiry->getManufacturer()->getUuid(),
  151.                 ]),
  152.             ]
  153.         ]);
  154.         $orderEvent->addOption(
  155.             'url',
  156.             $this->router->generate('quotations.open', [
  157.                 'uuid' => $quotation->getUuid(),
  158.             ])
  159.         );
  160.         return $orderEvent;
  161.     }
  162. }