<?php
namespace App\Controller\Admin;
use App\Entity\Address;
use App\Entity\Orders;
use App\Entity\PushNotification;
use App\Entity\Rider;
use App\Entity\SmsNotification;
use App\Entity\User;
use App\Service\NotificationService;
use App\Service\SmsService;
use App\Utils\MTools;
use Sonata\AdminBundle\Controller\CRUDController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
class PendingOrdersAdminController extends CRUDController{
public function listAction()
{ $em = $this->getDoctrine()->getManager();
$pendingOrders = $em->getRepository(Orders::class)
->findBy(['status' => 'pending']);
$oRiders = $em->getRepository(Rider::class)
->findBy(['a_status' => 1]);
$aPendingOrders = [];
foreach ($pendingOrders as $pendingOrder){
$oCustomer = $em->getRepository(User::class)
->findOneBy(['id' => $pendingOrder->getUid()]);
$oAddress = $em->getRepository(Address::class)
->findOneBy(['id' => $pendingOrder->getAddressId()]);
$aPendingOrders [] = [
'id' => $pendingOrder->getId(),
'reference' => $pendingOrder->getOid(),
'prix' => $pendingOrder->getTotal(),
'qte' => count($pendingOrder->getOrderDetails()),
'customer' => $oCustomer->getName(),
'mobile' => $oCustomer->getMobile(),
'customerAddress' => $oAddress->getHno(),
'status' =>$pendingOrder->getStatus(),
'date' => $pendingOrder->getDdate()
];
}
return $this->renderWithExtraParams('admin/pending_orders.html.twig', [
'orders' => $aPendingOrders,
'riders' => $oRiders
]);
}
public function affectAction(Request $request, SmsService $smsService,NotificationService $notificationService){
$em = $this->getDoctrine()->getManager();
$oOrder = $em->getRepository(Orders::class)
->findOneBy(['id' => $request->request->get('orderId')]);
$oOrder->setRid($request->request->get('riderId'));
$oOrder->setStatus('on_the_way');
$oUser = $em->getRepository(User::class)
->findOneBy(['id' => $oOrder->getUid()]);
$oRider = $em->getRepository(Rider::class)
->findOneBy(['id' => $request->request->get('riderId')]);
$em->persist($oOrder);
$em->flush();
$readyDeliverySms = $em->getRepository(SmsNotification::class)
->findOneBy(['code' => 'READY_DELIVERY']);
$readyDeliveryPush = $em->getRepository(PushNotification::class)
->findOneBy(['code' => 'READY_DELIVERY']);
if ($readyDeliverySms instanceof SmsNotification && $readyDeliverySms->isActive()){
$smsService->sendSMS(
MTools::formatMessage($readyDeliverySms->getValue(), [
'((rider_name))' => $oRider->getName(),
'((verification_code))' => $oOrder->getVerificationCode()
]),
$oUser->getMobile()
);
}
if ($readyDeliveryPush instanceof PushNotification && $readyDeliveryPush->isActive()){
$notificationService->sendNotification(
$oOrder->getCustomerToken(),
[
'title' => $readyDeliveryPush->getTitle(),
'body' => MTools::formatMessage($readyDeliveryPush->getValue(), [
'((rider_name))' => $oRider->getName(),
'((verification_code))' => $oOrder->getVerificationCode()
])
]
);
}
return new JsonResponse(['code' => 200, 'message' => 'success'], 200);
}
}