src/EventSubscriber/PriceInquiryWorkflowSubscriber.php line 207

Open in your IDE?
  1. <?php
  2. // src/App/EventSubscriber/BlogPostReviewSubscriber.php
  3. namespace App\EventSubscriber;
  4. use App\Entity\CustomerQuotations;
  5. use App\Entity\PriceInquiry;
  6. use App\Service\Constants\LogEvents;
  7. use App\Service\Log\QuotationLogService;
  8. use Doctrine\Common\Util\ClassUtils;
  9. use App\Repository\SubscriptionRepository;
  10. use App\Service\RabbitMQ\RabbitMQDispatcher;
  11. use App\Service\Subscription\SubscriptionService;
  12. use App\Service\UserLog;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Security\Core\Security;
  16. use Symfony\Component\Workflow\Event\Event;
  17. use App\Service\PriceInquiry\PriceInquiryService;
  18. use Symfony\Component\Workflow\Registry;
  19. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  20. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  21. class PriceInquiryWorkflowSubscriber implements EventSubscriberInterface
  22. {
  23.     private $security;
  24.     private $inquiryService;
  25.     private $em;
  26.     private $workflows;
  27.     private $rabbitMQDispatcher;
  28.     private $userLog;
  29.     private $subscriptionRepo;
  30.     private $subscriptionService;
  31.     private $logService;
  32.     public function __construct(
  33.         Security            $security,
  34.         PriceInquiryService $inquiryService,
  35.         EntityManagerInterface $em,
  36.         Registry $workflows,
  37.         RabbitMQDispatcher $rabbitMQDispatcher,
  38.         UserLog $userLog,
  39.         QuotationLogService $logService,
  40.         SubscriptionRepository $subscriptionRepo,
  41.         SubscriptionService $subscriptionService
  42.     )
  43.     {
  44.         $this->security $security;
  45.         $this->inquiryService $inquiryService;
  46.         $this->userLog $userLog;
  47.         $this->em $em;
  48.         $this->workflows $workflows;
  49.         $this->rabbitMQDispatcher $rabbitMQDispatcher;
  50.         $this->subscriptionService $subscriptionService;
  51.         $this->subscriptionRepo $subscriptionRepo;
  52.         $this->logService $logService;
  53.     }
  54.     public static function getSubscribedEvents()
  55.     {
  56.         return [
  57.             'workflow.price_inquiry_status.guard.to_accepted' => ['canBeAccepted'],
  58.             'workflow.price_inquiry_status.completed.to_open' => [
  59.                 ['createSubscriptions']
  60.             ],
  61. //            'workflow.price_inquiry_status.completed.to_rejected' => [
  62. //                ['removeSubscriptions']
  63. //            ],
  64.             'workflow.price_inquiry_status.completed.to_offer' => [
  65.                 ['setDeliveryPrices'50],
  66.                 ['setCustomsPricesAmounts'40],
  67.                 ['setDeliveryTime'30],
  68.                 ['setIsTransit'20],
  69.                 ['updateQuotationStatus'10],
  70.             ],
  71.             'workflow.price_inquiry_status.completed.to_accepted' => [
  72.                 ['setOrderPrice'],
  73.                 ['setOrderDeliveryPrice'],
  74.                 ['setQuotationAccepted'],
  75.             ],
  76.             'workflow.price_inquiry_status.announce' => [
  77.                 ['addQuotationLastUpdateTime'],
  78.                 ['logTransition'],
  79.                 ['sendNotification']
  80.             ],
  81.         ];
  82.     }
  83.     public function setOrderPrice(Event $event) {
  84.         $inquiry $event->getSubject();
  85.         $inquiry->setOrderPrice($inquiry->getCustomerPriceOffer()[$inquiry->getOrderQuantity()]??null);
  86.     }
  87.     public function setOrderDeliveryPrice(Event $event) {
  88.         $inquiry $event->getSubject();
  89.         $inquiry->setCustomerDeliveryPrice($inquiry->getCustomerDeliveryPrices()[$inquiry->getOrderQuantity()]??0);
  90.     }
  91.     public function setQuotationAccepted(Event $event) {
  92.         $inquiry $event->getSubject();
  93.         $quotation $inquiry->getQuotation();
  94.         if ( !$quotation->getIsOrder() ) {
  95.             $workflowQuotation $this->workflows->get($quotation);
  96.             $workflowQuotation->apply($quotation'to_ordered');
  97.         }
  98.         $quotation->setIsOrder(true);
  99.         $quotation->setOrderInquiry($inquiry);
  100.         // decline all other inquiries
  101.         // check if there are more than one - so there is something to decline
  102.         if ( count($quotation->getPriceInquiries()) > ) {
  103.             foreach ( $quotation->getPriceInquiries() as $priceInquiry ) {
  104.                 // ignore the one which was chosen now
  105.                 if ( $priceInquiry === $inquiry ) {
  106.                     continue;
  107.                 }
  108.                 $workflowPriceInqyiry $this->workflows->get($priceInquiry);
  109.                 if ( $workflowPriceInqyiry->can($priceInquiry'to_declined') ) {
  110.                     $workflowPriceInqyiry->apply($priceInquiry'to_declined');
  111.                 }
  112.             }
  113.         }
  114.     }
  115.     public function createSubscriptions(Event $event)
  116.     {
  117.         $this->subscriptionService->generateSubscriptions($event->getSubject(), $event->getSubject()->getUserCreated());
  118.     }
  119.     public function canBeAccepted(Event $event)
  120.     {
  121.         $inquiry $event->getSubject();
  122.         if ( $inquiry->getStatus() != PriceInquiry::STATUS_OFFER ) {
  123.             $event->setBlocked(true);
  124.         }
  125.         if ( $this->security->isGranted('user.is_manufacturer') ) {
  126.             $event->setBlocked(true);
  127.         }
  128.         $statuses = [
  129.             CustomerQuotations::STATUS_PROPOSAL_SENT,
  130.             CustomerQuotations::STATUS_HOLD,
  131.         ];
  132.         if ( !in_array($inquiry->getQuotation()->getStatus(),$statuses) ) {
  133.             $event->setBlocked(true);
  134.         }
  135.         if ( $this->security->getUser() && $this->security->isGranted('user.is_app_customer'$this->security->getUser()) ) {
  136.             return true;
  137.         }
  138.         if ( !$this->security->isGranted('ROLE_SALES_MANAGER') ) {
  139.             $event->setBlocked(true);
  140.         }
  141.         if ( $inquiry->getQuotation()->getManager() != $this->security->getUser() ) {
  142.             $event->setBlocked(true);
  143.         }
  144.         return true;
  145.     }
  146.     public function addQuotationLastUpdateTime(Event $event)
  147.     {
  148.         $quotation $event->getSubject()->getQuotation();
  149.         $quotation->setLastUpdate(new \DateTime());
  150.         $this->em->persist($quotation);
  151.         $this->em->flush();
  152.     }
  153.     public function setDeliveryPrices(Event $event)
  154.     {
  155.         $this->rabbitMQDispatcher->dispatch([
  156.             'data' => [
  157.                 'inquiry_id' => $event->getSubject()->getId(),
  158.                 'inquiry_uuid' => $event->getSubject()->getUuid()
  159.             ],
  160.             'action_name' => 'cargoSonPriceUpdate',
  161.             'consumer_name' => 'price_inquiry_consumer'
  162.         ]);
  163.     }
  164.     public function setIsTransit(Event $event)
  165.     {
  166.         $this->inquiryService->setDefaultTransit($event->getSubject());
  167.         if ($event->getSubject()->getIsTransit()) {
  168.             $this->logService->logAction(
  169.                 $event->getSubject()->getQuotation(),
  170.                 'log.'.LogEvents::INQUIRY_UPDATED_IS_TRANSIT,
  171.                 $event->getSubject()->getInquiryNumber()
  172.             );
  173.         }
  174.     }
  175.     public function setCustomsPricesAmounts(Event $event)
  176.     {
  177.         $this->inquiryService->updateCustomsPricesAmounts($event->getSubject());
  178.     }
  179.     public function setDeliveryTime(Event $event)
  180.     {
  181.         $this->inquiryService->updateDeliveryTime($event->getSubject());
  182.     }
  183.     public function sendNotification(Event $event)
  184.     {
  185.         if ( in_array($event->getSubject()->getQuotation()->getStatus(),
  186.             [CustomerQuotations::STATUS_REJECTEDCustomerQuotations::STATUS_DELETED]) ) {
  187.             return;
  188.         }
  189.         $response $this->subscriptionService->proceedEvent(
  190.             'inquiry.transition.' $event->getTransition()->getName(),
  191.             $event->getSubject(),
  192.             [
  193.                 'data' => [
  194.                     'uuid' => $event->getSubject()->getUuid(),
  195.                     'user_id' => $this->security->getUser()? $this->security->getUser()->getId():null,
  196.                     'transition' => $event->getTransition()->getName()
  197.                 ],
  198.                 'action_name' => 'inquiryStatusChangeNotify',
  199.             ]
  200.         );
  201.         if ($response['notifies_count']) {
  202.             $this->userLog->log(
  203.                 'notification',
  204.                 'inquiry.to_offer.notifications.created',
  205.                 $event->getSubject()->getId(),
  206.                 'New notifications created. Notifications count: '.$response['notifies_count']. ', inquiry uuid: ' $event->getSubject()->getUuid() . ', users: ' $response['users']
  207.             );
  208.         }
  209.         $this->inquiryService->updateQuotationStatus($event->getSubject()->getQuotation());
  210.     }
  211.     public function updateQuotationStatus(Event $event)
  212.     {
  213.         if ( $event->getTransition()->getName() == 'to_offer' ) {
  214.             $quotation $event->getSubject()->getQuotation();
  215.             $workflowQuotation $this->workflows->get($quotation);
  216.             if ( $workflowQuotation->can($quotation,'to_offers_partially_received') ) {
  217.                 $workflowQuotation->apply($quotation'to_offers_partially_received');
  218.                 $this->em->persist($quotation);
  219.                 $this->em->flush();
  220.             }
  221.         }
  222.         else if ( $event->getTransition()->getName() == 'to_accepted' ) {
  223.            //TODO
  224.         }
  225.     }
  226.     public function logTransition(Event $event)
  227.     {
  228.         $this->userLog->log(
  229.             'inquiry',
  230.             'status.update.'.$event->getTransition()->getName(),
  231.             $event->getSubject()->getId(),
  232.             'Inquiry status update: '.$event->getTransition()->getName(). ' ' $event->getSubject()->getUuid()
  233.         );
  234.     }
  235.     public function removeSubscriptions(Event $event)
  236.     {
  237.         if ($subscriptions $this->subscriptionRepo->getAll([
  238.             ClassUtils::getClass($event->getSubject()) => $event->getSubject()
  239.             ])) {
  240.             $this->subscriptionService->removeSubs($subscriptions);
  241.         }
  242.     }
  243. }