src/IlaveU/Apps/POSBundle/Controller/CartController.php line 141
<?phpnamespace App\IlaveU\Apps\POSBundle\Controller;use App\Entity\LdryOrder;use App\Entity\LdryTrackOrder;use App\IlaveU\ShopBundle\Repository\Product\ProductRepository;use App\IlaveU\Apps\POSBundle\Repository\TableRepository;use App\IlaveU\ShopBundle\Entity\Customer\Customer;use App\IlaveU\ShopBundle\Entity\Customer\CustomerWalletPoint;use App\IlaveU\ShopBundle\Entity\Customer\PackEngagement;use App\IlaveU\ShopBundle\Entity\Order\Order;use App\IlaveU\ShopBundle\Entity\Order\OrderItem;use App\IlaveU\ShopBundle\Entity\Order\OrderItemNote;use App\IlaveU\ShopBundle\Entity\Shipping\Shipment;use App\IlaveU\ShopBundle\Entity\Shipping\ShipmentItem;use App\IlaveU\ShopBundle\Entity\Shipping\StepType;use App\IlaveU\ShopBundle\Entity\Shipping\TrackingStep;use App\IlaveU\ShopBundle\Repository\Customer\CustomerRepository;use App\IlaveU\ShopBundle\Repository\Order\OrderRepository;use App\IlaveU\ShopBundle\Repository\Payment\PaymentMethodRepository;use App\IlaveU\ShopBundle\Repository\Product\CategoryProductRepository;use App\IlaveU\ShopBundle\Repository\Promotion\CouponRepository;use App\IlaveU\ShopBundle\Repository\Shipping\ShippingMethodRepository;use App\IlaveU\ShopBundle\Repository\VendorRepository;use App\Repository\IlaveU\ShopBundle\Entity\Customer\PackRepository;use App\IlaveU\ShopBundle\Repository\Shipping\CityRegionRepository;use App\IlaveU\ShopBundle\Repository\Resource\AgentRepository;use App\IlaveU\ShopBundle\Service\IlaveUShippingProvider;use App\IlaveU\ShopBundle\Service\IlaveUShopProvider;use Doctrine\Persistence\ManagerRegistry;use Exception;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Mercure\HubInterface;use Symfony\Component\Routing\Annotation\Route;class CartController extends AbstractController{#[Route('/cart', name: 'pos_cart')]/*** @Route("/cart", name="pos_cart")*/public function index(Request $request,ProductRepository $productRepository,CategoryProductRepository $categoryProductRepository,PaymentMethodRepository $paymentMethodRepository,ShippingMethodRepository $shippingMethodRepository,AgentRepository $agentRepository,OrderRepository $orderRepository,VendorRepository $vendorRepository,CityRegionRepository $cityRegionRepository): Response{$order = null;$editOrderIdParam = null;if( array_key_exists("routeParams",$request->query->all())){$editOrderIdParam = $request->query->all()["routeParams"]["editOrderId"];}if($editOrderIdParam){$order = $orderRepository->find((int)$editOrderIdParam);}$products = $productRepository->findBy(["vendor"=>null,"clonedProduct"=> null ]);if($this->isGranted("ROLE_VENDOR")){$vendor = $vendorRepository->findOneBy(["user"=>$this->getUser()]);$products = $productRepository->findVendorProducts($vendor);}$categories = $categoryProductRepository->findAll();$paymentMethods = $paymentMethodRepository->findBy(["isEnabled"=>true]);$shippingMethods = $shippingMethodRepository->findAll();$agents = $agentRepository->findAll();$cityRegions = $cityRegionRepository->findAll();return $this->render('@IlaveU/Apps/POSBundle/templates/cart/index.html.twig', ['order'=>$order,'products' => $products,"categories"=>$categories,"paymentMethods"=>$paymentMethods,"shippingMethods"=>$shippingMethods,"agents"=>$agents,"cityRegions"=>$cityRegions]);}#[Route('/cart/checkout', name: 'pos_cart_checkout')]#[Route(path: '/cart/checkout', name: 'pos_cart_checkout')]public function pos_cart_checkout(ProductRepository $productRepository,ShippingMethodRepository $shippingMethodRepository ,CustomerRepository $customerRepository ,PaymentMethodRepository $paymentMethodRepository ,AgentRepository $agentRepository ,IlaveUShopProvider $shopProvider,OrderRepository $orderRepository,CouponRepository $couponRepository,CityRegionRepository $cityRegionRepository,PackRepository $packRepository,IlaveUShippingProvider $ilaveShippingProvider,VendorRepository $vendorRepository,ManagerRegistry $doctrine,HubInterface $hub,Request $request): Response{//dd($request->getContentType());//$request->request->replace(["test"=>"test"]);if ($request->getContentType() == 'json' and $request->getContent()) {$contentJsonRequest = json_decode($request->getContent(),true, 512, JSON_THROW_ON_ERROR);if($contentJsonRequest){$request->request->replace($contentJsonRequest);}else{return $this->json(["status"=>"error","message"=>"Content is not well formated"]);}}//dd($request->request);$em = $doctrine->getManager();$editMode = false;$customer = $customerRepository->find((int)$request->request->get("customerId"));if($this->isGranted("ROLE_CUSTOMER")){$customer = $customerRepository->findOneBy(["user"=>$this->getUser()]);}$shippingMethod = $shippingMethodRepository->find((int)$request->request->get("shippingMethod"));$agent = $agentRepository->find((int)$request->request->get("agent"));$paymentMode = $paymentMethodRepository->find((int)$request->request->get("paymentMethod"));$deliveryType = (string)$request->request->get("deliveryType");$coupon = $couponRepository->findOneBy(["code"=>$request->request->get("codeCoupon")]);$cityRegionCollect = $cityRegionRepository->find((int)$request->request->get("cityRegionCollect"));$cityRegionShipping = $cityRegionRepository->find((int)$request->request->get("cityRegionShipping"));$startProcessingAt = new \DateTime();$startDeliveryAt = new \DateTime();if($request->request->get("startProcessingAt")){$startProcessingAt = new \DateTime($request->request->get("startProcessingAt"));}if($request->request->get("startDeliveryAt")){$startDeliveryAt = new \DateTime($request->request->get("startDeliveryAt"));}$editOrderIdParam = null;if( array_key_exists("routeParams",$request->query->all())){$editOrderIdParam = $request->query->all()["routeParams"]["editOrderId"];}if($request->request->get("editOrderId")){$editMode = true;$order = $orderRepository->find((int)$request->request->get("editOrderId"));foreach($order->getOrderItems() as $singleOrderItemToRemove){$order->removeOrderItem($singleOrderItemToRemove);}foreach($order->getCustomerWalletPoints() as $singleCustomerWalletPoints){$em->remove($singleCustomerWalletPoints);}}else{$order = new Order();// Instance Object}//$order->setOldOrderNumber($orderNumberGenerator);if($customer){$order->setFirstName($customer->getFirstName());$order->setLastName($customer->getLastName());$order->setEmail((string)$customer->getEmail());$order->setCustomer($customer);$customer->setDescription((string)$request->request->get("commentCustomer"));}$order->setEmail((string)$request->request->get("email"));//dd($couponService);if($request->request->get("status")){$order->setStatus((string)$request->request->get("status"));}if($request->request->get("distance")){$order->setDistance((float)$request->request->get("distance"));}$order->setStartProcessingAt($startProcessingAt);$order->setStartDeliveryAt($startDeliveryAt);$order->setTel((string)$request->request->get("telOrder"));$order->setCollectTel((string)$request->request->get("collectTelOrder"));$order->setCollectAddress((string)$request->request->get("collectAddress"));$order->setShippingAddress((string)$request->request->get("shippingAddress"));//$order->setNotifyCustomer(boolval($request->request->get("notifyCustomer")) ); hire$notifyCustomerParam = $request->request->get("notifyCustomer");if ($request->request->get("source") === "mobile" && $request->request->get("status") === "draft" && $notifyCustomerParam === null) {$order->setNotifyCustomer(true);} else {$order->setNotifyCustomer(boolval($notifyCustomerParam));}$order->setCollectLng((string)$request->request->get("collectLng"));$order->setCollectLat((string)$request->request->get("collectLat"));$order->setShippingLng((string)$request->request->get("shippingLng"));$order->setShippingLat((string)$request->request->get("shippingLat"));$order->setComment((string)$request->request->get("commentOrder"));$order->setOrderType((string)$request->request->get("orderType"));$order->setCity("Casablanca");$order->setDescription((string)$request->request->get("description"));$order->setDepartment("Maarif");if($request->request->get("source")){$order->setSource((string)$request->request->get("source"));}$order->setPayedAmount((float)$request->request->get("payedAmount"));$order->setReduction((float)$request->request->get("reduction"));$order->setWalletPaymentAmount((float)$request->request->get("walletPaymentAmount"));$order->setDeliveryAt($order->getCreatedAt()->format("Y-m-d H:i"));$order->setRecoveryAt($order->getCreatedAt()->format("Y-m-d H:i"));$order->setDeliveryType($deliveryType);if($request->request->get("shippingTips")){$order->setShippingTips((float)$request->request->get("shippingTips"));}$order->setAgent($agent);$order->setShippingMethod($shippingMethod);$order->setPaymentMethod($paymentMode);$order->setCityRegionCollect($cityRegionCollect);$order->setCityRegionShipping($cityRegionShipping);if($this->isGranted("ROLE_VENDOR")){$vendor = $vendorRepository->findOneBy(["user"=>$this->getUser()]);$order->setVendor($vendor);}$totalPrice = 0;$orderItemsArray=[];if(array_key_exists("orderItems",$request->request->all())){$orderItemsArray = $request->request->all()["orderItems"];}if(is_string($orderItemsArray)){$orderItemsArray = json_decode($orderItemsArray , true, 512, JSON_THROW_ON_ERROR);}foreach($orderItemsArray as $singleOrderItemCart){$reduction = 0;$orderItem = new OrderItem();// Instance Objectif (array_key_exists('productType', $singleOrderItemCart)) {if($singleOrderItemCart["productType"] == "subscription"){$pack = $packRepository->find((int)$singleOrderItemCart["idProduct"]);$packEngagement = new PackEngagement();$packEngagement->setCustomer($customer);$packEngagement->setPack($pack);$orderItem->setPackEngagement($packEngagement);}else{$product = $productRepository->find((int)$singleOrderItemCart["idProduct"]);$orderItem->setProduct($product);if($product){$reduction = ($product->getPrice()) - (float)$singleOrderItemCart["price"];}}}else{$product = $productRepository->find((int)$singleOrderItemCart["idProduct"]);$orderItem->setProduct($product);if($product){$reduction = ($product->getPrice()) - (float)$singleOrderItemCart["price"];}}$orderItem->setPrice((float)$singleOrderItemCart["price"]);$orderItem->setQuantity((int)$singleOrderItemCart["quantity"]);$orderItem->setDescription((string)$singleOrderItemCart["name"]);$orderItem->setReduction($reduction);$orderItem->setParentOrder($order);$order->addOrderItem($orderItem);if(array_key_exists("notes",$singleOrderItemCart)){foreach($singleOrderItemCart["notes"] as $singleOrderItemNote ){$orderItemNote = new OrderItemNote();// Instance Object$orderItemNote->setContent($singleOrderItemNote["note"]);$orderItemNote->setOrderItem($orderItem);$em->persist($orderItemNote);}}$totalPrice = $totalPrice + ((float)$singleOrderItemCart["price"] * (int)$singleOrderItemCart["quantity"]);$em->persist($orderItem);}$shippingFees = $order->getShippingFees();$payedAmount = (float)$request->request->get("payedAmount");$totalToPay = $totalPrice + $shippingFees;$difference = $payedAmount - $totalToPay ;$restAmount = 0;if($order->getFullRestAmount() < 0){$restAmount = abs($order->getFullRestAmount());$order->setPayedAmount($order->getPayedAmount() + $order->getFullRestAmount());}if((boolean)$request->request->get("isRestUsedAsCredit") and $restAmount > 0 ){$customerWalletPoint = new CustomerWalletPoint();// Instance Object$customerWalletPoint->setSource("POS");$customerWalletPoint->setCustomer($customer);$customerWalletPoint->setOriginOrder($order);$customerWalletPoint->setPoints($restAmount);$order->addCustomerWalletPoint($customerWalletPoint);//dd( $customerWalletPoint);$em->persist($customerWalletPoint);}//dd($order->getPayedAmount());if($order->getWalletPaymentAmount() > 0 ){$customerWalletPoint = new CustomerWalletPoint();// Instance Object$customerWalletPoint->setSource("Website");$customerWalletPoint->setCustomer($customer);$customerWalletPoint->setOriginOrder($order);$customerWalletPoint->setPoints(-$order->getWalletPaymentAmount());$order->addCustomerWalletPoint($customerWalletPoint);//dd( $customerWalletPoint);$em->persist($customerWalletPoint);}elseif( $difference > 0){if((boolean)$request->request->get("isWallet")){$customerWalletPoint = new CustomerWalletPoint();// Instance Object$customerWalletPoint->setSource("POS");$customerWalletPoint->setCustomer($customer);$customerWalletPoint->setOriginOrder($order);$customerWalletPoint->setPoints($difference);$order->addCustomerWalletPoint($customerWalletPoint);//dd( $customerWalletPoint);$em->persist($customerWalletPoint);$difference = 0;}// Or Return real change money to customer}else{if((boolean)$request->request->get("isWallet")){$points = 0;foreach($customer->getCustomerWalletPoints() as $singleWalletPoints){$points = $points + $singleWalletPoints->getPoints();}if( $points >= abs($difference)){$totalToPay = $totalToPay - $points;$customerWalletPointBalance = new CustomerWalletPoint();// Instance Object$customerWalletPointBalance->setSource("POS");$customerWalletPointBalance->setCustomer($customer);$customerWalletPointBalance->setOriginOrder($order);$customerWalletPointBalance->setPoints($difference);$order->addCustomerWalletPoint($customerWalletPointBalance);$em->persist($customerWalletPointBalance);}else{echo '<div class="alert alert-danger" >Solde insufisant : Solde de client <b>'.$customer->getFirstName().' '.$customer->getLastName().'</b> est '.$points.' DH !</div>';exit;}}}$recieveDate = "";$deliveryDate = "";$collectDateTimePreview = new \DateTime();if($request->request->get("startProcessingAt")){$collectDateTimePreview->modify($request->request->get("startProcessingAt"));}$shippingDateTimePreview = new \DateTime();if($request->request->get("startDeliveryAt")){$shippingDateTimePreview->modify($request->request->get("startDeliveryAt"));//dd($shippingDateTimePreview);}$shippingDetailsService = $shopProvider->getOrderExtraInfos($order,$collectDateTimePreview,$shippingDateTimePreview,$editMode);$couponService = $shopProvider->useCoupon($order,$request->request->get("codeCoupon"));if(!$order->getShipment()){$shipment = $ilaveShippingProvider->createShipment($order);$order->setShipment($shipment);}$em->persist($order);if($request->request->get("isSimulationPreview") ){return $this->render("@IlaveU/Apps/POSBundle/templates/cart/ajaxSimulationOrder.html.twig",["order"=>$order]);}if($request->request->get("isPromoPreview") ){return $this->json($couponService);}if($request->request->get("isChooseDatePreview") ){//dd($session->get("orderCart"));return $this->json($shippingDetailsService);}if($request->request->get("isReadyForCheckout") ){//dd($order);//dd($shippingDetailsService);$session = $request->getSession();if($request->request->get("statusShipping")){//dd($order->getShipment());if(!$order->getShipment()){$shipment = $ilaveShippingProvider->createShipment($order);$order->setShipment($shipment);}$trackingStep = new TrackingStep();$stepType = $em->getRepository(StepType::class)->findOneBy(["code"=>$request->request->get("statusShipping")]);$trackingStep->setStepType($stepType);$trackingStep->setIsCompleted(true);$trackingStep->setShipment($order->getShipment());$order->getShipment()->addTrackingStep($trackingStep);$order->setStatusShipping($request->request->get("statusShipping"));$em->persist($order->getShipment());$em->persist($trackingStep);}// if($order->getFullRestAmount() == 0 or $order->getStatus() == "paid" ){// $order->setStatus("paid");// if($order->getFullRestAmount() > 0){// $order->setPayedAmount($order->getFullRestAmount());// }// }else{// }// $order->setPayedAmount($order->getFullRestAmount());// $order->setStatus("waiting");$em->persist($order);$em->flush();$this->addFlash("success","Votre commande a été enregistrée avec succès!");$session->set("orderCart",$order->getId());return $this->json(["status"=>"success","orderId"=>$order->getId()]);}$em->flush();$request->getSession()->remove("orderStartProcessingAt");if ($request->getContentType() == 'json') {return $this->json(["status"=>"success","orderId"=>$order->getId(),"message"=>"Commande ".$order->getOrderNumber()." Crée avec succès !"]);}return $this->render('@IlaveU/Apps/POSBundle/templates/cart/ajaxSuccessOrder.html.twig',["editOrderId"=>$request->request->get("editOrderId")]);}#[Route('/cart/create-customer', name: 'pos_cart_create_customer')]/*** @Route("/cart/create-customer", name="pos_cart_create_customer")*/public function pos_cart_create_customer(ProductRepository $productRepository,CustomerRepository $customerRepository ,VendorRepository $vendorRepository,ManagerRegistry $doctrine,Request $request): Response{$em = $doctrine->getManager();$firstName = $request->request->get("firstName");$lastName = $request->request->get("lastName");$email = $request->request->get("Email");$phone = $request->request->get("phone");$address = $request->request->get("address");$customer = new Customer();$customer->setFirstName($firstName);$customer->setLastName($lastName);$customer->setEmail($email);$customer->setPhone($phone);$customer->setAddress($address);if($this->isGranted("ROLE_VENDOR")){$vendor = $vendorRepository->findOneBy(["user"=>$this->getUser()]);$customer->setVendor($vendor);}$em->persist($customer);$em->flush();return $this->redirectToRoute("select_customer_ajax");}#[Route('/select-customer-ajax', name: 'select_customer_ajax')]/*** @Route("/select-customer-ajax", name="select_customer_ajax")*/public function select_customer_ajax(CustomerRepository $customerRepository,VendorRepository $vendorRepository): Response{$customers = $customerRepository->findBy([],["id"=>"DESC"]);if($this->isGranted("ROLE_VENDOR")){$vendor = $vendorRepository->findOneBy(["user"=>$this->getUser()]);$customers = $customerRepository->findBy(["vendor"=>$vendor],["id"=>"DESC"]);}return $this->render('@IlaveU/Apps/POSBundle/templates/cart/ajaxSelectFormCustomer.html.twig', ['customers' => $customers,]);}#[Route('/tables', name: 'tables')]/*** @Route("/tables", name="tables")*/public function tables(TableRepository $tableRepository): Response{$tables = $tableRepository->findAll();return $this->render('@IlaveU/Apps/POSBundle/templates/cart/tables.html.twig', ['tables' => $tables,]);}}