<?php
/**
* Created by SAWIT Mateusz Miklewski.
* User: Mateusz
* Date: 2018-02-27
* Time: 08:06
*/
namespace AppBundle\EventSubscriber;
use AppBundle\Entity\Policy\PaymentMethod;
use AppBundle\Entity\Policy\Proposal;
use AppBundle\Utils\Payment\PaymentService;
use AppBundle\Utils\Proposal\ProposalService;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Workflow\Event\Event;
use Symfony\Component\Workflow\Event\GuardEvent;
class ProposalWorkflowEventSubscriber implements EventSubscriberInterface
{
protected $em;
protected $proposalService;
protected $security;
protected function getEm() {
/*if(!$this->em->isOpen()) {
$tmpEm = EntityManager::create($this->em->getConnection(), $this->em->getConfiguration(), $this->em->getEventManager());
$this->em = $tmpEm;
}*/
return $this->em;
}
public function __construct(EntityManagerInterface $em, ProposalService $proposalService, Security $security)
{
$this->em = $em;
$this->proposalService = $proposalService;
$this->security = $security;
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * array('eventName' => 'methodName')
* * array('eventName' => array('methodName', $priority))
* * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents()
{
return [
'workflow.proposal.guard.buy' => ['guardBuy'],
'workflow.proposal.transition.mark_paid' => ['onMarkPaid'],
'workflow.proposal.transition.quiet_mark_paid' => ['onMarkPaid'],
'workflow.proposal.transition.issue' => ['onIssue'],
'workflow.proposal.transition.reissue' => ['onReIssue'],
'workflow.proposal.leave' => ['onLeave'],
'workflow.proposal.completed.buy' => ['onBuy'],
'workflow.proposal.completed.mark_paid' => ['onMarkPaidCompleted'],
'workflow.proposal.completed.accept' => ['onAcceptCompleted'],
'workflow.proposal.completed.mark_cod_payment' => ['onMarkCodCompleted'],
'workflow.proposal.completed.validate_review' => ['onAcceptCompleted'],
'workflow.proposal.completed.reissue' => ['onReIssueCompleted'],
'workflow.proposal.completed.issue' => ['onIssueCompleted'],
'workflow.proposal.completed.policy_error' => ['onPolicyErrorCompleted'],
'workflow.proposal.completed.done' => ['onPolicyDoneCompleted'],
'workflow.proposal.completed.request_contact' => ['onRequestContactCompleted'],
];
}
public function guardBuy(GuardEvent $event)
{
/** @var \AppBundle\Entity\Policy\Proposal $proposal */
$proposal = $event->getSubject();
$paymentMethod = $proposal->getPaymentMethod();
if($proposal->getTuProduct()->getHasPaymentMethods()) {
if(!$paymentMethod) {
$event->setBlocked(true);
return;
}
if($paymentMethod->getCode() == PaymentMethod::LAGARDERE_TYPE && $this->security->isGranted('ROLE_PARTNER')) {
return;
}
elseif($paymentMethod->getDeleted() || !$paymentMethod->getEnabled()) {
if($paymentMethod->getTuSpecific() && $paymentMethod->getSpecificForTu() && $paymentMethod->getSpecificForTu()->getCode() == $proposal->getTuProduct()->getTu()->getCode()) {
return;
} else {
$event->setBlocked(true);
return;
}
}
}
else {
if($paymentMethod && $paymentMethod->getCode() != PaymentMethod::COD_TYPE) {
$event->setBlocked(true);
return;
}
}
}
public function onBuy(Event $event) {
/** @var \AppBundle\Entity\Policy\Proposal $proposal */
$proposal = $event->getSubject();
$proposal->setIsOrdered(true);
$this->proposalService->sendOrderConfirmation($proposal);
$this->getEm()->persist($proposal);
$this->getEm()->flush();
}
public function onMarkPaid(Event $event)
{
/** @var \AppBundle\Entity\Policy\Proposal $proposal */
$proposal = $event->getSubject();
$installment = $proposal->getFirstInstallment();
$installment->setPaid(true);
$installment->setPaymentDate(new \DateTime());
$this->getEm()->persist($installment);
$this->getEm()->flush();
}
/**
* @param Event $event
* @throws \Exception
*/
public function onReIssue(Event $event) {
/** @var \AppBundle\Entity\Policy\Proposal $proposal */
$proposal = $event->getSubject();
$this->proposalService->reIssuePolicy($proposal);
}
/**
* @param Event $event
* @throws \Exception
*/
public function onIssue(Event $event) {
/** @var \AppBundle\Entity\Policy\Proposal $proposal */
$proposal = $event->getSubject();
$this->proposalService->issuePolicy($proposal);
}
public function onMarkPaidCompleted(Event $event)
{
/** @var \AppBundle\Entity\Policy\Proposal $proposal */
$proposal = $event->getSubject();
$this->proposalService->review($proposal);
$this->getEm()->flush();
if(!$proposal->getTuProduct()->getPolicyAutoSending()) {
$this->proposalService->sendPolicy($proposal, true);
}
}
public function onAcceptCompleted(Event $event) {
/** @var \AppBundle\Entity\Policy\Proposal $proposal */
$proposal = $event->getSubject();
if(!$proposal->getPolicyIssued() && $proposal->getTuProduct()->getAutoPolicyIssue()) {
$this->proposalService->autoIssuePolicy($proposal);
}
}
public function onMarkCodCompleted(Event $event) {
/** @var \AppBundle\Entity\Policy\Proposal $proposal */
$proposal = $event->getSubject();
if(!$proposal->getTuProduct()->getPolicyAutoSending()) {
$this->proposalService->sendPolicy($proposal, true);
}
if( $proposal->getTuProduct()->getAutoReview()) {
$this->proposalService->review($proposal);
}
}
public function onReIssueCompleted(Event $event) {
}
public function onIssueCompleted(Event $event) {
/** @var \AppBundle\Entity\Policy\Proposal $proposal */
$proposal = $event->getSubject();
if($proposal->getRenewal()) {
$renewal = $proposal->getRenewal();
$renewal->setRenewed(true);
$this->getEm()->persist($renewal);
}
if($proposal->getPolicyIssued() && $proposal->getTuProduct()->getPolicyAutoSending() && $proposal->getPolicy()) {
$this->proposalService->sendPolicy($proposal);
}
}
public function onPolicyErrorCompleted(Event $event) {
/** @var \AppBundle\Entity\Policy\Proposal $proposal */
$proposal = $event->getSubject();
$proposal->setPolicyIssued(false);
$this->getEm()->persist($proposal);
$this->getEm()->flush();
}
public function onPolicyDoneCompleted(Event $event) {
/** @var \AppBundle\Entity\Policy\Proposal $proposal */
$proposal = $event->getSubject();
$this->proposalService->sendPolicyIssuedSms($proposal);
}
public function onLeave(Event $event)
{
/** @var \AppBundle\Entity\Policy\Proposal $proposal */
$proposal = $event->getSubject();
$statusLog = $proposal->getStatusLog();
$now = new \DateTime();
$statusLog .= "\n" . $now->format('Y-m-d H:i:s') . ': ' . sprintf(
'Transaction "%s" from "%s" to "%s"',
$event->getTransition()->getName(),
implode(', ', array_keys($event->getMarking()->getPlaces())),
implode(', ', $event->getTransition()->getTos())
);
$proposal->setStatusLog($statusLog);
$this->getEm()->persist($proposal);
$this->getEm()->flush();
}
public function onRequestContactCompleted(Event $event) {
/** @var \AppBundle\Entity\Policy\Proposal $proposal */
$proposal = $event->getSubject();
$this->proposalService->sendContactRequestConfirmation($proposal);
}
}