/**
  * @Route(
  *     "/product/change_quantity",
  *     name="change_product_quantity"
  * )
  * @Method("POST")
  */
 public function changeQuantityAction()
 {
     if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
         $request = Request::createFromGlobals();
         $order_id = intval($request->request->get('order_id', 0));
         $product_id = intval($request->request->get('product_id', 0));
         $quantity = abs(intval($request->request->get('quantity', 0)));
         if ($order_id > 0 && $product_id > 0 && $quantity >= 0) {
             // Retrieve orders' details information for a details page
             $order = $this->getDoctrine()->getRepository('AppBundle:Orders')->find($order_id);
             if ($order) {
                 // Check whether order is still open to join / change quantities
                 $closing_after = Utilities::countTimeRemaining(date_timestamp_get($order->getJoiningDeadline()));
                 if ($closing_after != Utilities::$STATUS_JOINING_TIME_IS_OVER) {
                     $product = $this->getDoctrine()->getRepository('AppBundle:Product')->find($product_id);
                     if ($product && $order->getProducts()->contains($product)) {
                         $user = $this->getUser();
                         $userProduct = $this->getDoctrine()->getRepository('AppBundle:UserProduct')->findOneBy(array('user' => $user, 'product' => $product));
                         // Create new UserProduct record in DB, if such doesn't exist
                         $em = $this->getDoctrine()->getManager();
                         if (!$userProduct) {
                             $userProduct = new UserProduct();
                             $userProduct->setUser($user);
                             $userProduct->setProduct($product);
                             $userProduct->setQuantity($quantity);
                             $em->persist($userProduct);
                         } else {
                             $userProduct->setQuantity($quantity);
                         }
                         if ($quantity == 0) {
                             $em->remove($userProduct);
                         }
                         $em->flush();
                         return new Response($quantity, Response::HTTP_OK);
                     } else {
                         return new Response(AjaxResponses::$PRODUCT_NOT_FOUND, Response::HTTP_NOT_FOUND);
                     }
                 } else {
                     return new Response(AjaxResponses::$ORDER_JOINING_TIME_IS_OVER, Response::HTTP_NOT_FOUND);
                 }
             } else {
                 return new Response(AjaxResponses::$ORDER_NOT_FOUND, Response::HTTP_NOT_FOUND);
             }
         } else {
             return new Response(AjaxResponses::$WRONG_REQUEST_PARAMETERS, Response::HTTP_BAD_REQUEST);
         }
     } else {
         return new Response(AjaxResponses::$UNAUTHORIZED, Response::HTTP_UNAUTHORIZED);
     }
 }
 /**
  * @Route(
  *     "/orders/delete/{order_id}",
  *     name="order_delete",
  *     requirements={
  *          "order_id":     "\d+",
  *     }
  * )
  */
 public function deleteAction($order_id)
 {
     $user = $this->getUser();
     // Retrieve orders' details information for a details page
     $order = $this->getDoctrine()->getRepository('AppBundle:Orders')->find($order_id);
     if (!$order) {
         // Create flash message for no order found
         $this->addFlash('error', 'No order found for id: ' . $order_id . '!');
         // Redirect to orders_open screen
         return new RedirectResponse($this->generateUrl('user_history'));
     }
     if ($order->getUserId() !== $user->getId()) {
         //throw $this->createAccessDeniedException();
         // Create flash message if user not order creator
         $this->addFlash('error', 'You are not creator of order: ' . $order->getName() . '!');
         // Redirect to user_history screen
         return new RedirectResponse($this->generateUrl('user_history'));
     }
     // Get time remaining to join order
     $closing_after = Utilities::countTimeRemaining(date_timestamp_get($order->getJoiningDeadline()));
     if ($closing_after == Utilities::$STATUS_JOINING_TIME_IS_OVER) {
         $this->addFlash('error', 'You cannot remove orders from the past. Remove is allowed for open orders!');
         // Redirect to user_history screen
         return new RedirectResponse($this->generateUrl('user_history'));
     }
     $em = $this->getDoctrine()->getManager();
     $em->remove($order);
     //        $order = new Orders();
     //        $products = new Product();
     $products = $order->getProducts();
     if ($products) {
         foreach ($products as $product) {
             $em->remove($product);
             $userProducts = $product->getUserProducts();
             foreach ($userProducts as $userProduct) {
                 $em->remove($userProduct);
             }
         }
     }
     $em->flush();
     // Create flash message for successful removal
     $this->addFlash('notice', 'Order successfully deleted!');
     // Redirect to users_orders screen to refresh list
     return new RedirectResponse($this->generateUrl('user_history'));
 }