src/Diplix/KMGBundle/Controller/InstantOrderController.php line 128

Open in your IDE?
  1. <?php
  2. namespace Diplix\KMGBundle\Controller;
  3. use Diplix\KMGBundle\Entity\Customer;
  4. use Diplix\KMGBundle\Entity\Order;
  5. use Diplix\KMGBundle\Entity\OrderStatus;
  6. use Diplix\KMGBundle\Entity\Role;
  7. use Diplix\KMGBundle\Entity\User;
  8. use Diplix\KMGBundle\Service\Notifier;
  9. use Diplix\KMGBundle\Service\OrderHandler;
  10. use http\Exception\RuntimeException;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
  13. use Symfony\Component\PropertyAccess\PropertyAccess;
  14. class InstantOrderController extends BaseController
  15. {
  16.     public function __construct(
  17.         protected Notifier $notifier,
  18.         protected OrderHandler $orderHandler,
  19.         private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry
  20.     )
  21.     {
  22.     }
  23.     protected function getPending()
  24.     {
  25.         $repo $this->managerRegistry->getRepository(Order::class);
  26.         $allRelevant $repo->getPendingInstantOrder($this->getCurrentUser());
  27.         return array_filter($allRelevant, function(array $o){ return $o['orderStatus']['id']==OrderStatus::STATUS_INSTANT_ORDER_PENDING;});
  28.     }
  29.     public function indexAction(Request $request)
  30.     {
  31.         if (!$this->isGranted(Role::ALLOW_INSTANT_ORDER))
  32.         {
  33.             throw $this->createAccessDeniedException();
  34.         }
  35.         return $this->render('@DiplixKMG/Orders/instant-order.html.twig');
  36.     }
  37.     public function getPendingAction(Request $request)
  38.     {
  39.         $repo $this->managerRegistry->getRepository(Order::class);
  40.         if (!$this->isGranted(Role::ALLOW_INSTANT_ORDER))
  41.         {
  42.             throw $this->createAccessDeniedException();
  43.         }
  44.         $existing $repo->getPendingInstantOrder($this->getCurrentUser());
  45.         return $this->getJsonDataResponse($requesttrue,[
  46.                 'count'=>count($existing),
  47.                 'orders' => $existing,
  48.                 'homeAddress' => $this->getCurrentUser()->getHomeAddress()
  49.             ]
  50.         );
  51.     }
  52.     protected function prepareOrder(array $setup)
  53.     {
  54.         $pa PropertyAccess::createPropertyAccessor();
  55.         $oh $this->orderHandler;
  56.         $u $this->managerRegistry->getManager()->find(User::class, $this->getCurrentUser()->getId());
  57.         assert($u!==null);
  58.         $customer $this->managerRegistry->getManager()->getReference(Customer::class,  $this->getCustomerContextFromSession()->getId());
  59.         assert($customer !==null);
  60.         if ($u->getHomeAddress()===null)
  61.         {
  62.             throw new RuntimeException('Bitte hinterlegen Sie eine Heimatadresse');
  63.         }
  64.         $row $oh->getNewOrder($u,$customer,OrderStatus::STATUS_INSTANT_ORDER_PENDING,null,true);
  65.         $row->setOrderTime( new \DateTime() );
  66.         foreach ($setup as $k=>$e)
  67.         {
  68.             if ($pa->isWritable($row,$k))
  69.                 $pa->setValue($row,$k,$e);
  70.         }
  71.         $row->clearAddressList();
  72.         $row->addAddress(clone $u->getHomeAddress());
  73.         $row->addAddress(clone $u->getHomeAddress());
  74.         // overwrite destination address
  75.         $d 'destination_';
  76.         foreach ($setup as $k=>$e)
  77.         {
  78.             if (strpos($k$d) !== 0) continue;
  79.             $pa->setValue($row->getAddressList()[1],str_replace($d,'',$k),$e);
  80.         }
  81.         $row->setInstantOrderInitiated(new \DateTime());
  82.         $row->setInstantOrderStatus(Order::INSTANT_ORDER_REQUEST_PENDING);
  83.         return $row;
  84.     }
  85.     public function createAction(Request $request)
  86.     {
  87.         $oh $this->orderHandler;
  88.         $nn $this->notifier;
  89.         try {
  90.             if (!$this->isGranted(Role::ALLOW_INSTANT_ORDER))
  91.             {
  92.                 throw $this->createAccessDeniedException();
  93.             }
  94.             if ($request->getMethod()!==Request::METHOD_POST)
  95.             {
  96.                 throw new MethodNotAllowedHttpException([Request::METHOD_POST],"Verwenden Sie http/POST");
  97.             }
  98.             $exist $this->getPending();
  99.             if (count($exist)>0)
  100.             {
  101.                 throw new \Exception('Es wartet bereits eine Sofortbestellung. Bitte haben Sie einen Moment Geduld.');
  102.             }
  103.             $setup $request->query->get('setup',[]);
  104.             $row $this->prepareOrder($setup);
  105.             $oh->storeOrUpdate($row);
  106.             $repo $this->managerRegistry->getRepository(Order::class);
  107.             $newAO $repo->getSingleAsArray($row->getId()); // todo: create serializr
  108.             $nn->notifyTelegramAboutInstantOrder($row);
  109.             $nn->notifyWebsocketAboutInstantOrder($row);
  110.             return $this->getJsonDataResponse($requesttrue, ['order' => $newAO]);
  111.         }
  112.         catch (\Throwable $ex)
  113.         {
  114.             return $this->getJsonDataResponse($request,false,$ex,$ex->getMessage());
  115.         }
  116.     }
  117.     public function getPendingForDispoAction(Request $request)
  118.     {
  119.         $repo $this->managerRegistry->getRepository(Order::class);
  120.         if (!$this->isGranted(Role::DISPO))
  121.         {
  122.             throw $this->createAccessDeniedException();
  123.         }
  124.         $existing $repo->getPendingInstantOrdersForDispo();
  125.         return $this->getJsonDataResponse($requesttrue, ['count'=>count($existing),'orders' => $existing]);
  126.     }
  127.     public function dispoReplyAction(Request $request)
  128.     {
  129.         $repo $this->managerRegistry->getRepository(Order::class);
  130.         $nn $this->notifier;
  131.         try {
  132.             if (!$this->isGranted(Role::DISPO))
  133.             {
  134.                 throw $this->createAccessDeniedException();
  135.             }
  136.             $data \GuzzleHttp\json_decode($request->getContent(),true);
  137.             $id $data['id'];
  138.             $comment $data['comment'];
  139.             $action $data['action'];
  140.             $order $repo->findOneBy(['id'=>$id'instantOrderStatus'=>Order::INSTANT_ORDER_REQUEST_PENDING]);
  141.             if ($order===null)
  142.             {
  143.                 throw new \Exception(sprintf('Bestellung #%d ungültig oder bereits bearbeitet.',$id));
  144.             }
  145.             $order->setInstantOrderResponded(new \DateTime());
  146.             $order->setInstantOrderRespondedBy($this->getUser());
  147.             $order->setOrderStatus($this->managerRegistry->getManager()->getReference(OrderStatus::class, OrderStatus::STATUS_DRAFT));
  148.             $oh $this->orderHandler;
  149.             if ($action==='accept')
  150.             {
  151.                 $order->setInstantOrderComment($comment);
  152.                 $order->setInstantOrderStatus(Order::INSTANT_ORDER_ACCEPTED);
  153.                 $oh->storeOrUpdate($order);
  154.                 $oh->initiateOrder($order);
  155.                 $nn->notifyWebsocketAboutInstantOrder($order,Notifier::M_INSTANT_RIDE_RESPONSE);
  156.                 return $this->getJsonDataResponse($request,true,[],sprintf('Bestellung %s ausgelöst.',$order->getOrderId()));
  157.             }
  158.             else
  159.             if ($action==='decline')
  160.             {
  161.                 $order->setInstantOrderStatus(Order::INSTANT_ORDER_DECLINED);
  162.                 $order->setInstantOrderComment($comment);
  163.                 $oh->storeOrUpdate($order);
  164.                 $nn->notifyWebsocketAboutInstantOrder($order,Notifier::M_INSTANT_RIDE_RESPONSE);
  165.                 return $this->getJsonDataResponse($request,true,[],sprintf('Bestellung wurde abgelehnt.'));
  166.             }
  167.             else
  168.             {
  169.                 throw new \Exception(sprintf('Unbekannte Aktion: %s',$action));
  170.             }
  171.         }
  172.         catch (\Throwable $ex)
  173.         {
  174.             return $this->getJsonDataResponse($request,false,$ex,$ex->getMessage());
  175.         }
  176.     }
  177. }