src/AppBundle/EventSubscriber/ProposalWorkflowEventSubscriber.php line 149

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by SAWIT Mateusz Miklewski.
  4.  * User: Mateusz
  5.  * Date: 2018-02-27
  6.  * Time: 08:06
  7.  */
  8. namespace AppBundle\EventSubscriber;
  9. use AppBundle\Entity\Policy\PaymentMethod;
  10. use AppBundle\Entity\Policy\Proposal;
  11. use AppBundle\Utils\Payment\PaymentService;
  12. use AppBundle\Utils\Proposal\ProposalService;
  13. use Doctrine\ORM\EntityManager;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\Security\Core\Security;
  17. use Symfony\Component\Workflow\Event\Event;
  18. use Symfony\Component\Workflow\Event\GuardEvent;
  19. class ProposalWorkflowEventSubscriber implements EventSubscriberInterface
  20. {
  21.     protected $em;
  22.     protected $proposalService;
  23.     protected $security;
  24.     protected function getEm() {
  25.         /*if(!$this->em->isOpen()) {
  26.             $tmpEm = EntityManager::create($this->em->getConnection(), $this->em->getConfiguration(), $this->em->getEventManager());
  27.             $this->em = $tmpEm;
  28.         }*/
  29.         return $this->em;
  30.     }
  31.     public function __construct(EntityManagerInterface $emProposalService $proposalServiceSecurity $security)
  32.     {
  33.         $this->em $em;
  34.         $this->proposalService $proposalService;
  35.         $this->security $security;
  36.     }
  37.     /**
  38.      * Returns an array of event names this subscriber wants to listen to.
  39.      *
  40.      * The array keys are event names and the value can be:
  41.      *
  42.      *  * The method name to call (priority defaults to 0)
  43.      *  * An array composed of the method name to call and the priority
  44.      *  * An array of arrays composed of the method names to call and respective
  45.      *    priorities, or 0 if unset
  46.      *
  47.      * For instance:
  48.      *
  49.      *  * array('eventName' => 'methodName')
  50.      *  * array('eventName' => array('methodName', $priority))
  51.      *  * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
  52.      *
  53.      * @return array The event names to listen to
  54.      */
  55.     public static function getSubscribedEvents()
  56.     {
  57.         return [
  58.             'workflow.proposal.guard.buy' => ['guardBuy'],
  59.             'workflow.proposal.transition.mark_paid' => ['onMarkPaid'],
  60.             'workflow.proposal.transition.quiet_mark_paid' => ['onMarkPaid'],
  61.             'workflow.proposal.transition.issue' => ['onIssue'],
  62.             'workflow.proposal.transition.reissue' => ['onReIssue'],
  63.             'workflow.proposal.leave' => ['onLeave'],
  64.             'workflow.proposal.completed.buy' => ['onBuy'],
  65.             'workflow.proposal.completed.mark_paid' => ['onMarkPaidCompleted'],
  66.             'workflow.proposal.completed.accept' => ['onAcceptCompleted'],
  67.             'workflow.proposal.completed.mark_cod_payment' => ['onMarkCodCompleted'],
  68.             'workflow.proposal.completed.validate_review' => ['onAcceptCompleted'],
  69.             'workflow.proposal.completed.reissue' => ['onReIssueCompleted'],
  70.             'workflow.proposal.completed.issue' => ['onIssueCompleted'],
  71.             'workflow.proposal.completed.policy_error' => ['onPolicyErrorCompleted'],
  72.             'workflow.proposal.completed.done' => ['onPolicyDoneCompleted'],
  73.             'workflow.proposal.completed.request_contact' => ['onRequestContactCompleted'],
  74.         ];
  75.     }
  76.     public function guardBuy(GuardEvent $event)
  77.     {
  78.         /** @var \AppBundle\Entity\Policy\Proposal $proposal */
  79.         $proposal $event->getSubject();
  80.         $paymentMethod $proposal->getPaymentMethod();
  81.         if($proposal->getTuProduct()->getHasPaymentMethods()) {
  82.             if(!$paymentMethod) {
  83.                 $event->setBlocked(true);
  84.                 return;
  85.             }
  86.             if($paymentMethod->getCode() == PaymentMethod::LAGARDERE_TYPE && $this->security->isGranted('ROLE_PARTNER')) {
  87.                 return;
  88.             }
  89.             elseif($paymentMethod->getDeleted() || !$paymentMethod->getEnabled()) {
  90.               if($paymentMethod->getTuSpecific() && $paymentMethod->getSpecificForTu() && $paymentMethod->getSpecificForTu()->getCode() == $proposal->getTuProduct()->getTu()->getCode()) {
  91.                 return;
  92.               } else {
  93.                 $event->setBlocked(true);
  94.                 return;
  95.               }
  96.             }
  97.         }
  98.         else {
  99.             if($paymentMethod && $paymentMethod->getCode() != PaymentMethod::COD_TYPE) {
  100.                 $event->setBlocked(true);
  101.                 return;
  102.             }
  103.         }
  104.     }
  105.     public function onBuy(Event $event) {
  106.         /** @var \AppBundle\Entity\Policy\Proposal $proposal */
  107.         $proposal $event->getSubject();
  108.         $proposal->setIsOrdered(true);
  109.         $this->proposalService->sendOrderConfirmation($proposal);
  110.         $this->getEm()->persist($proposal);
  111.         $this->getEm()->flush();
  112.     }
  113.     public function onMarkPaid(Event $event)
  114.     {
  115.         /** @var \AppBundle\Entity\Policy\Proposal $proposal */
  116.         $proposal $event->getSubject();
  117.         $installment $proposal->getFirstInstallment();
  118.         $installment->setPaid(true);
  119.         $installment->setPaymentDate(new \DateTime());
  120.         $this->getEm()->persist($installment);
  121.         $this->getEm()->flush();
  122.     }
  123.     /**
  124.      * @param Event $event
  125.      * @throws \Exception
  126.      */
  127.     public function onReIssue(Event $event) {
  128.         /** @var \AppBundle\Entity\Policy\Proposal $proposal */
  129.         $proposal $event->getSubject();
  130.         $this->proposalService->reIssuePolicy($proposal);
  131.     }
  132.     /**
  133.      * @param Event $event
  134.      * @throws \Exception
  135.      */
  136.     public function onIssue(Event $event) {
  137.         /** @var \AppBundle\Entity\Policy\Proposal $proposal */
  138.         $proposal $event->getSubject();
  139.         $this->proposalService->issuePolicy($proposal);
  140.     }
  141.     public function onMarkPaidCompleted(Event $event)
  142.     {
  143.         /** @var \AppBundle\Entity\Policy\Proposal $proposal */
  144.         $proposal $event->getSubject();
  145.         $this->proposalService->review($proposal);
  146.         $this->getEm()->flush();
  147.         if(!$proposal->getTuProduct()->getPolicyAutoSending()) {
  148.             $this->proposalService->sendPolicy($proposaltrue);
  149.         }
  150.     }
  151.     public function onAcceptCompleted(Event $event) {
  152.         /** @var \AppBundle\Entity\Policy\Proposal $proposal */
  153.         $proposal $event->getSubject();
  154.         if(!$proposal->getPolicyIssued() && $proposal->getTuProduct()->getAutoPolicyIssue()) {
  155.             $this->proposalService->autoIssuePolicy($proposal);
  156.         }
  157.     }
  158.     public function onMarkCodCompleted(Event $event) {
  159.         /** @var \AppBundle\Entity\Policy\Proposal $proposal */
  160.         $proposal $event->getSubject();
  161.         if(!$proposal->getTuProduct()->getPolicyAutoSending()) {
  162.             $this->proposalService->sendPolicy($proposaltrue);
  163.         }
  164.         if( $proposal->getTuProduct()->getAutoReview()) {
  165.             $this->proposalService->review($proposal);
  166.         }
  167.     }
  168.     public function onReIssueCompleted(Event $event) {
  169.     }
  170.     public function onIssueCompleted(Event $event) {
  171.         /** @var \AppBundle\Entity\Policy\Proposal $proposal */
  172.         $proposal $event->getSubject();
  173.         if($proposal->getRenewal()) {
  174.             $renewal $proposal->getRenewal();
  175.             $renewal->setRenewed(true);
  176.             $this->getEm()->persist($renewal);
  177.         }
  178.         if($proposal->getPolicyIssued() && $proposal->getTuProduct()->getPolicyAutoSending() && $proposal->getPolicy()) {
  179.             $this->proposalService->sendPolicy($proposal);
  180.         }
  181.     }
  182.     public function onPolicyErrorCompleted(Event $event) {
  183.         /** @var \AppBundle\Entity\Policy\Proposal $proposal */
  184.         $proposal $event->getSubject();
  185.         $proposal->setPolicyIssued(false);
  186.         $this->getEm()->persist($proposal);
  187.         $this->getEm()->flush();
  188.     }
  189.     public function onPolicyDoneCompleted(Event $event) {
  190.         /** @var \AppBundle\Entity\Policy\Proposal $proposal */
  191.         $proposal $event->getSubject();
  192.         $this->proposalService->sendPolicyIssuedSms($proposal);
  193.     }
  194.     public function onLeave(Event $event)
  195.     {
  196.         /** @var \AppBundle\Entity\Policy\Proposal $proposal */
  197.         $proposal $event->getSubject();
  198.         $statusLog $proposal->getStatusLog();
  199.         $now = new \DateTime();
  200.         $statusLog .= "\n" $now->format('Y-m-d H:i:s') . ': ' sprintf(
  201.             'Transaction "%s" from "%s" to "%s"',
  202.             $event->getTransition()->getName(),
  203.             implode(', 'array_keys($event->getMarking()->getPlaces())),
  204.             implode(', '$event->getTransition()->getTos())
  205.         );
  206.         $proposal->setStatusLog($statusLog);
  207.         $this->getEm()->persist($proposal);
  208.         $this->getEm()->flush();
  209.     }
  210.     public function onRequestContactCompleted(Event $event) {
  211.       /** @var \AppBundle\Entity\Policy\Proposal $proposal */
  212.       $proposal $event->getSubject();
  213.       $this->proposalService->sendContactRequestConfirmation($proposal);
  214.     }
  215. }