src/Controller/Admin/PendingOrdersAdminController.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use App\Entity\Address;
  4. use App\Entity\Orders;
  5. use App\Entity\PushNotification;
  6. use App\Entity\Rider;
  7. use App\Entity\SmsNotification;
  8. use App\Entity\User;
  9. use App\Service\NotificationService;
  10. use App\Service\SmsService;
  11. use App\Utils\MTools;
  12. use Sonata\AdminBundle\Controller\CRUDController;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. class PendingOrdersAdminController extends CRUDController{
  16.     public function listAction()
  17.     {   $em $this->getDoctrine()->getManager();
  18.         $pendingOrders $em->getRepository(Orders::class)
  19.             ->findBy(['status' => 'pending']);
  20.         $oRiders $em->getRepository(Rider::class)
  21.             ->findBy(['a_status' => 1]);
  22.         $aPendingOrders = [];
  23.         foreach ($pendingOrders as $pendingOrder){
  24.             $oCustomer $em->getRepository(User::class)
  25.                 ->findOneBy(['id' => $pendingOrder->getUid()]);
  26.             $oAddress $em->getRepository(Address::class)
  27.                 ->findOneBy(['id' => $pendingOrder->getAddressId()]);
  28.             $aPendingOrders [] = [
  29.                 'id' => $pendingOrder->getId(),
  30.                 'reference' => $pendingOrder->getOid(),
  31.                 'prix' => $pendingOrder->getTotal(),
  32.                 'qte' => count($pendingOrder->getOrderDetails()),
  33.                 'customer' => $oCustomer->getName(),
  34.                 'mobile' => $oCustomer->getMobile(),
  35.                 'customerAddress' => $oAddress->getHno(),
  36.                 'status' =>$pendingOrder->getStatus(),
  37.                 'date' => $pendingOrder->getDdate()
  38.             ];
  39.         }
  40.         return $this->renderWithExtraParams('admin/pending_orders.html.twig', [
  41.             'orders' => $aPendingOrders,
  42.             'riders' => $oRiders
  43.         ]);
  44.     }
  45.     public function affectAction(Request $requestSmsService $smsService,NotificationService $notificationService){
  46.         $em $this->getDoctrine()->getManager();
  47.         $oOrder $em->getRepository(Orders::class)
  48.             ->findOneBy(['id' => $request->request->get('orderId')]);
  49.         $oOrder->setRid($request->request->get('riderId'));
  50.         $oOrder->setStatus('on_the_way');
  51.         $oUser $em->getRepository(User::class)
  52.             ->findOneBy(['id' => $oOrder->getUid()]);
  53.         $oRider $em->getRepository(Rider::class)
  54.             ->findOneBy(['id' => $request->request->get('riderId')]);
  55.         $em->persist($oOrder);
  56.         $em->flush();
  57.         $readyDeliverySms $em->getRepository(SmsNotification::class)
  58.             ->findOneBy(['code' => 'READY_DELIVERY']);
  59.         $readyDeliveryPush $em->getRepository(PushNotification::class)
  60.             ->findOneBy(['code' => 'READY_DELIVERY']);
  61.         if ($readyDeliverySms instanceof SmsNotification && $readyDeliverySms->isActive()){
  62.             $smsService->sendSMS(
  63.                 MTools::formatMessage($readyDeliverySms->getValue(), [
  64.                     '((rider_name))' => $oRider->getName(),
  65.                     '((verification_code))' => $oOrder->getVerificationCode()
  66.                 ]),
  67.                 $oUser->getMobile()
  68.             );
  69.         }
  70.         if ($readyDeliveryPush instanceof PushNotification && $readyDeliveryPush->isActive()){
  71.             $notificationService->sendNotification(
  72.                 $oOrder->getCustomerToken(),
  73.                 [
  74.                     'title' => $readyDeliveryPush->getTitle(),
  75.                     'body' => MTools::formatMessage($readyDeliveryPush->getValue(), [
  76.                         '((rider_name))' => $oRider->getName(),
  77.                         '((verification_code))' => $oOrder->getVerificationCode()
  78.                     ])
  79.                 ]
  80.             );
  81.         }
  82.         return new JsonResponse(['code' => 200'message' => 'success'], 200);
  83.     }
  84. }