src/EventSubscriber/PriceInquirySubscriber.php line 126

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Event;
  4. use App\Event\PriceInquiryPriceUpdateEvent;
  5. use App\Event\PriceInquiryVerifiedEvent;
  6. use App\Repository\UserRepository;
  7. use App\Security\Voters\PriceInquiryVoter;
  8. use App\Service\Shipment\ShipmentService;
  9. use App\Service\Constants\LogEvents;
  10. use App\Service\Helpers\StringHelper;
  11. use App\Service\Log\QuotationLogService;
  12. use App\Service\Mailing\MailContentPriceInquiry;
  13. use App\Service\Mailing\MailingService;
  14. use App\Service\PriceInquiry\PriceInquiryService;
  15. use App\Service\Subscription\SubscriptionService;
  16. use App\Service\UserLog;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\Security\Core\Security;
  20. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  21. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  22. class PriceInquirySubscriber implements EventSubscriberInterface
  23. {
  24.     protected Security $security;
  25.     protected UserRepository $userRepo;
  26.     protected SubscriptionService $subscriptionService;
  27.     private ShipmentService $shipmentService;
  28.     private EntityManagerInterface $em;
  29.     private QuotationLogService $logService;
  30.     private UserLog $userLog;
  31.     private PriceInquiryService $inquiryService;
  32.     public function __construct(
  33.         SubscriptionService    $subscriptionService,
  34.         Security               $security,
  35.         ShipmentService        $shipmentService,
  36.         EntityManagerInterface $em,
  37.         UserRepository         $userRepo,
  38.         QuotationLogService    $logService,
  39.         PriceInquiryService    $inquiryService,
  40.         UserLog                $userLog
  41.     )
  42.     {
  43.         $this->security $security;
  44.         $this->subscriptionService $subscriptionService;
  45.         $this->userRepo $userRepo;
  46.         $this->shipmentService $shipmentService;
  47.         $this->em $em;
  48.         $this->logService $logService;
  49.         $this->userLog $userLog;
  50.         $this->inquiryService $inquiryService;
  51.     }
  52.     public static function getSubscribedEvents(): array
  53.     {
  54.         return [
  55.             PriceInquiryPriceUpdateEvent::PRICE_OFFER_UPDATED => [
  56.                 ['updateDeliveryPrices'],
  57.                 ['updateFieldsWithAmounts'],
  58.             ],
  59.             PriceInquiryPriceUpdateEvent::CUSTOMS_PRICES_UPDATED => [
  60.                 ['logCustomsPricesChange']
  61.             ],
  62.             PriceInquiryPriceUpdateEvent::PRICES_UPDATED => [
  63.                 ['createPriceUpdateNotification'],
  64.                 ['logPricesChange'],
  65.                 ['updateCustomerDeliveryPricesCommission']
  66.             ],
  67.             PriceInquiryVerifiedEvent::VERIFIED_MFM => [
  68.                 ['logOfferVerifiedMfm'],
  69.                 ['setCustomerPrices'],
  70.             ],
  71.             PriceInquiryVerifiedEvent::VERIFIED_SM => [
  72.                 ['logOfferVerifiedSm'],
  73.                 ['setCustomsPrices'],
  74.                 ['createNoCustomsNotification'],
  75.             ],
  76.         ];
  77.     }
  78.     public function createPriceUpdateNotification(PriceInquiryPriceUpdateEvent $event)
  79.     {
  80.         $inquiry $event->getInquiry();
  81.         $this->subscriptionService->proceedEvent(
  82.             Event::INQUIRY_PRICES_UPDATED,
  83.             $inquiry,
  84.             ['data' => ['uuid' => $inquiry->getUuid()]]
  85.         );
  86.     }
  87.     public function updateDeliveryPrices(PriceInquiryPriceUpdateEvent $event)
  88.     {
  89.         $inquiry $event->getInquiry();
  90.         $deliveryPrices $inquiry->getDeliveryPrices();
  91.         $isNewAmount false;
  92.         foreach ($inquiry->getPriceOffer() as $amount => $price) {
  93.             if ($isNewAmount = !isset($deliveryPrices[$amount])) {
  94.                 break;
  95.             }
  96.         }
  97.         if ($isNewAmount) {
  98.             $newPrices $this->shipmentService->getDeliveryPrices($inquiry);
  99.             foreach ($inquiry->getPriceOffer() as $amount => $price) {
  100.                 if (!isset($deliveryPrices[$amount])) {
  101.                     $deliveryPrices[$amount] = $newPrices[$amount] ?? 0;
  102.                 }
  103.             }
  104.             $inquiry->setDeliveryPrices($deliveryPrices);
  105.             $this->em->flush();
  106.         }
  107.     }
  108.     public function updateFieldsWithAmounts(PriceInquiryPriceUpdateEvent $event)
  109.     {
  110.         $inquiry $event->getInquiry();
  111.         $this->inquiryService->updateCustomerPriceOfferAmounts($inquiry);
  112.         $this->inquiryService->updateDeliveryPricesAmounts($inquiry);
  113.         $this->inquiryService->updateCustomerDeliveryPricesAmounts($inquiry);
  114.         $this->inquiryService->updateCustomsPricesAmounts($inquiry);
  115.     }
  116.     public function setCustomerPrices(PriceInquiryVerifiedEvent $event)
  117.     {
  118.         $inquiry $event->getInquiry();
  119.         $this->inquiryService->updateDeliveryPricesAmounts($inquiry);
  120.         $this->inquiryService->updateCustomsPricesAmounts($inquiry);
  121.         //AFTER MFM APPROVE -> SET CUSTOMER PRICES
  122.         if ( !$inquiry->getCustomerPriceOffer() ) {
  123.             $this->inquiryService->addCustomerPrices($inquiry);
  124.         } else {
  125.             $this->inquiryService->updateCustomerPriceOfferAmounts($inquiry);
  126.             $this->inquiryService->updateCustomerDeliveryPricesAmounts($inquiry);
  127.         }
  128.     }
  129.     /**
  130.      * @throws TransportExceptionInterface
  131.      * @throws DecodingExceptionInterface
  132.      */
  133.     public function setCustomsPrices(PriceInquiryVerifiedEvent $event)
  134.     {
  135.         $inquiry $event->getInquiry();
  136.         $this->inquiryService->autoUpdateCustomsPrices($inquiry);
  137.     }
  138.     public function createNoCustomsNotification(PriceInquiryVerifiedEvent $event)
  139.     {
  140.         $inquiry $event->getInquiry();
  141.         if ($this->security->isGranted(PriceInquiryVoter::CAN_UPDATE_CUSTOMS_PRICES$inquiry)) {
  142.             if (!$this->inquiryService->areCustomsSet($inquiry)) {
  143.                 $this->subscriptionService->proceedEvent(
  144.                     Event::INQUIRY_CUSTOMS_WERE_NOT_ADDED,
  145.                     $inquiry,
  146.                     ['data' => ['uuid' => $inquiry->getUuid()]]
  147.                 );
  148.             }
  149.         }
  150.     }
  151.     /**
  152.      * @param PriceInquiryVerifiedEvent $event
  153.      */
  154.     public function logOfferVerifiedSm(PriceInquiryVerifiedEvent $event)
  155.     {
  156.         $inquiry $event->getInquiry();
  157.         $this->logService->logAction(
  158.             $inquiry->getQuotation(),
  159.             'log.inquiry.verified_sm',
  160.             $inquiry->getInquiryNumber()
  161.         );
  162.         $this->userLog->log(
  163.             'inquiry',
  164.             LogEvents::INQUIRY_UPDATED_VERIFY_SM,
  165.             $inquiry->getId(),
  166.             $inquiry->getFullInquiryNumber()
  167.         );
  168.     }
  169.     /**
  170.      * @param PriceInquiryVerifiedEvent $event
  171.      */
  172.     public function logOfferVerifiedMfm(PriceInquiryVerifiedEvent $event)
  173.     {
  174.         $inquiry $event->getInquiry();
  175.         $this->logService->logAction(
  176.             $inquiry->getQuotation(),
  177.             'log.inquiry.verified_mfm',
  178.             $inquiry->getInquiryNumber()
  179.         );
  180.         $this->userLog->log(
  181.             'inquiry',
  182.             LogEvents::INQUIRY_UPDATED_VERIFY_MFM,
  183.             $inquiry->getId(),
  184.             $inquiry->getFullInquiryNumber()
  185.         );
  186.     }
  187.     public function logPricesChange(PriceInquiryPriceUpdateEvent $event)
  188.     {
  189.         $inquiry $event->getInquiry();
  190.         $changeSet $event->getChangeSet();
  191.         $changed array_key_first($changeSet[0]);
  192.         $this->logService->logAction(
  193.             $inquiry->getQuotation(),
  194.             'log.inquiry.' . ($changeSet[0][$changed] ? 'updated' 'added') . '.' StringHelper::fromCamelCase($changeSet[2]),
  195.             $inquiry->getFullInquiryNumber() . '; ' $changed ':   CHANGED ' . ($changeSet[0][$changed] ?? 0) . ' -> ' . ($changeSet[1][$changed] ?? 0)
  196.         );
  197.         $this->userLog->log(
  198.             'inquiry',
  199.             'inquiry.' . ($changeSet[0][$changed] ? 'updated' 'added') . '.' StringHelper::fromCamelCase($changeSet[2]),
  200.             $inquiry->getId(),
  201.             $inquiry->getFullInquiryNumber() . '; ' $changed ':   CHANGED ' . ($changeSet[0][$changed] ?? 0) . ' -> ' . ($changeSet[1][$changed] ?? 0)
  202.         );
  203.     }
  204.     public function updateCustomerDeliveryPricesCommission(PriceInquiryPriceUpdateEvent $event)
  205.     {
  206.         $inquiry $event->getInquiry();
  207.         $changeSet $event->getChangeSet();
  208.         if ($changeSet[2] == 'deliveryPrices') {
  209.             foreach ($changeSet[1] as $amount => $deliveryPrice ) {
  210.                 if (isset($inquiry->getCustomerDeliveryPrices()[$amount])) {
  211.                     $customerDeliveryPrices $inquiry->getCustomerDeliveryPrices();
  212.                     $commission 0;
  213.                     if ($deliveryPrice $customerDeliveryPrices[$amount]) {
  214.                         $difference $customerDeliveryPrices[$amount] - $deliveryPrice;
  215.                         $commission =  $deliveryPrice ? ($difference $deliveryPrice) * 100 100;
  216.                     }
  217.                     if ($commission 25) {
  218.                         $customerDeliveryPrices[$amount] = $deliveryPrice + ($deliveryPrice 0.25);
  219.                         $this->logService->logAction(
  220.                             $inquiry->getQuotation(),
  221.                             'log.inquiry.updated.customer_delivery_prices',
  222.                             $inquiry->getFullInquiryNumber() . '; ' $amount ':   CHANGED ' $inquiry->getCustomerDeliveryPrices()[$amount] . ' -> ' $customerDeliveryPrices[$amount]
  223.                         );
  224.                         $inquiry->setCustomerDeliveryPrices($customerDeliveryPrices);
  225.                     }
  226.                 }
  227.             }
  228.         }
  229.     }
  230.     public function logCustomsPricesChange(PriceInquiryPriceUpdateEvent $event)
  231.     {
  232.         $inquiry $event->getInquiry();
  233.         $changeSet $event->getChangeSet();
  234.         $amount $changeSet array_key_first($changeSet[0]) : null;
  235.         $this->logService->logAction(
  236.             $inquiry->getQuotation(),
  237.             LogEvents::INQUIRY_UPDATED_CUSTOMS,
  238.             $inquiry->getFullInquiryNumber() . '; ' $amount ':  CHANGED ' . ($changeSet[0][$amount] ?? 0) . ' -> ' . ($changeSet[1][$amount] ?? 0)
  239.         );
  240.         $this->userLog->log(
  241.             'inquiry',
  242.             LogEvents::INQUIRY_UPDATED_CUSTOMS,
  243.             $inquiry->getId(),
  244.             $inquiry->getFullInquiryNumber() . '; ' . ($changeSet ? ('; ' $amount ':  CHANGED ' . ($changeSet[0][$amount] ?? 0) . ' -> ' . ($changeSet[1][$amount] ?? 0)) : '')
  245.         );
  246.     }
  247. }