Example #1
0
    /**
     * Add to stock quantity of an existing Stock.
     *
     * @Route("/stock/addtoqty", name="TohouriStoreBundle_stock_addToQuantity")
     * @Method("post")
     * @Template("TohouriStoreBundle:Stock:add.html.twig")
     */
    public function addToQuantityAction(Request $request)
    {
        //setting default value of the form
        $stock = new Stock();
        $stock->setQuantity(0);
        $form = $this->createFormBuilder($stock)->add('quantity', 'integer')->add('product')->getForm();
        if ('POST' === $request->getMethod()) {
            $form->bindRequest($request);
            if ($form->isValid()) {
                // taking selected Product Id to retreave the related stock instance
                $qty = $stock->getQuantity();
                $product = $stock->getProduct();
                $em = $this->getDoctrine()->getEntityManager();
                $stock = $em->getRepository('TohouriStoreBundle:Stock')->findOneBy(array('product' => $product->getId()));
                if (!$stock) {
                    throw $this->createNotFoundException('No stock found for product ' . $product);
                }
                // updating the retreaved stock quantity property by adding the number post from the form to existing quantity
                $oldStock = $stock->getQuantity();
                $stock->setQuantity($stock->getQuantity() + $qty);
                $em->persist($stock);
                $em->flush();
                $this->get('session')->setFlash('notice', 'Une nouvelle quantité (' . $qty . ') à été ajoutée au stock précédent (' . $oldStock . ') 
				le stock total est maintenant=' . $stock->getQuantity() . '');
                return $this->redirect($this->generateUrl('TohouriStoreBundle_stock_show', array('id' => $stock->getId())));
            }
        }
        return array('stock' => $stock, 'form' => $form->createView());
    }
Example #2
0
 public function addAction(Request $request)
 {
     //setting default value of the form
     $stock = new Stock();
     $shipping = new Shipping();
     $cartOrder = new CartOrder();
     $stock->setQuantity(0);
     $user = $this->container->get('security.context')->getToken()->getUser();
     $em = $this->getDoctrine()->getEntityManager();
     $cart = new Cart($this->container->get('request')->getSession());
     $cartArray = $cart->getSessionCart();
     $form1 = $this->createFormBuilder($stock)->add('quantity', 'integer')->add('product')->getForm();
     $form2 = $this->createFormBuilder($shipping)->add('organisationunit')->getForm();
     if ('POST' === $request->getMethod()) {
         if (isset($form1)) {
             $form1->bindRequest($request);
             if ($form1->isValid()) {
                 // taking selected Product Id to retreave the related stock instance
                 $qty = $stock->getQuantity();
                 //$product = $stock->getProduct();
                 $stock = $em->getRepository('TohouriStoreBundle:Stock')->findOneBy(array('product' => $stock->getProduct()->getId()));
                 $products = $em->getRepository('TohouriStoreBundle:Product')->findAll();
                 if (!$stock) {
                     throw $this->createNotFoundException('No stock found for product ' . $product);
                 }
                 $cart->addItem($product->getId(), $qty);
                 return $this->render('TohouriStoreBundle:Cart:index.html.twig', array('products' => $products, 'cart' => $cart->getSessionCart(), 'form1' => $form1->createView(), 'form2' => $form2->createView()));
             }
         }
         if (isset($form2)) {
             $form2->bindRequest($request);
             $user = $this->container->get('security.context')->getToken()->getUser();
             if ($form2->isValid()) {
                 $isCartExist = $em->getRepository('TohouriStoreBundle:Cart')->findBySessionId($this->container->get('request')->getSession()->getId());
                 // saving the cart if not already saved
                 if (count($isCartExist) === 0) {
                     $cart->setUser($user);
                     $cart->setSessionId($this->container->get('request')->getSession()->getId());
                     $em->persist($cart);
                     $em->flush();
                 }
                 // Saving client orders related to the cart
                 $sessionCart = $em->getRepository('TohouriStoreBundle:Cart')->findOneBySessionId($this->container->get('request')->getSession()->getId());
                 if (!$sessionCart) {
                     throw $this->createNotFoundException('error! no cart found! ' . $this->container->get('request')->getSession()->getId());
                 }
                 if (count($sessionCart->getCartOrder()) < 1) {
                     foreach ($cartArray as $id => $quantity) {
                         $product = $em->getRepository('TohouriStoreBundle:Product')->find($id);
                         $cartOrder->setProduct($product);
                         $cartOrder->setQuantity($quantity);
                         //remove the same quantity to product stock
                         $currentQuantity = $product->getStock()->getQuantity();
                         $product->getStock()->setQuantity($currentQuantity - $quantity);
                         $cartOrder->setCart($sessionCart);
                         $sessionCart->addCartOrder($cartOrder);
                         $em->persist($sessionCart);
                         $em->persist($product);
                         $em->flush();
                         $em->clear();
                     }
                     // persisting the shipping object
                     $cartId = $sessionCart->getId();
                     $cart = $em->getRepository('TohouriStoreBundle:Cart')->find($cartId);
                     $orgunit = $em->getRepository('TohouriStoreBundle:OrganisationUnit')->find($shipping->getOrganisationUnit()->getId());
                     $shipping->setStatus(FALSE);
                     $shipping->setCart($cart);
                     $shipping->setOrganisationUnit($orgunit);
                     $em->persist($shipping);
                     $em->flush();
                     //Empty the cart
                     $cart = new Cart($this->container->get('request')->getSession());
                     $cart->removeAllItem($id);
                     $this->get('session')->setFlash('notice', 'Votre commande de ' . count($cartArray) . ' produit(s) à été envoyée à ' . $orgunit);
                 } else {
                     $cartOrderCollection = $sessionCart->getCartOrder();
                     $cartId = $sessionCart->getId();
                     $cartOrderItem = new CartOrder();
                     // updating the cart products into cartOrder table
                     foreach ($cartArray as $id => $quantity) {
                         $currentCartOrder = $em->getRepository('TohouriStoreBundle:CartOrder')->findOneBy(array('product' => $id, 'cart' => $cartId));
                         if (!$currentCartOrder) {
                             // specific product not found! save this new item in cartOrder table
                             $cart = $em->getRepository('TohouriStoreBundle:Cart')->find($cartId);
                             $product = $em->getRepository('TohouriStoreBundle:Product')->find($id);
                             $cartOrderItem->setQuantity($quantity);
                             //remove the same quantity to product stock
                             $currentQuantity = $product->getStock()->getQuantity();
                             $product->getStock()->setQuantity($currentQuantity - $quantity);
                             $cartOrder->setCart($sessionCart);
                             $cartOrderItem->setProduct($product);
                             $cartOrderItem->setCart($cart);
                             $cart->addCartOrder($cartOrderItem);
                             $em->persist($cart);
                             $em->persist($product);
                             $em->flush();
                             $em->clear();
                         } else {
                             $currentCartOrder = $em->getRepository('TohouriStoreBundle:CartOrder')->findOneBy(array('product' => $id, 'cart' => $cartId));
                             $product = $em->getRepository('TohouriStoreBundle:Product')->find($id);
                             //update cartOrder item with new quantity
                             $currentCartOrder->setQuantity($quantity);
                             //remove the same quantity to product stock
                             $currentQuantity = $product->getStock()->getQuantity();
                             $product->getStock()->setQuantity($currentQuantity - $quantity);
                             $cartOrder->setCart($sessionCart);
                             $em->persist($currentCartOrder);
                             $em->persist($product);
                             $em->flush();
                             $em->clear();
                         }
                     }
                     // persisting the shipping object
                     //if($form2->isValid()) throw $this->createNotFoundException('form2 is valid '.$shipping->getOrganisationUnit());
                     $cart = $em->getRepository('TohouriStoreBundle:Cart')->find($cartId);
                     $orgunit = $em->getRepository('TohouriStoreBundle:OrganisationUnit')->find($shipping->getOrganisationUnit()->getId());
                     $shipping->setStatus(FALSE);
                     $shipping->setCart($cart);
                     $shipping->setOrganisationUnit($orgunit);
                     $em->persist($shipping);
                     $em->flush();
                     //Empty the cart
                     $cart = new Cart($this->container->get('request')->getSession());
                     $cart->removeAllItem($id);
                     $this->get('session')->setFlash('notice', 'Votre commande de ' . count($cartArray) . ' produit(s) à été envoyée à ' . $orgunit);
                 }
             }
         }
     }
     $save = $request->query->get('save');
     // retreiving GET variable
     if ($save === "yes") {
         $cart = new Cart($this->container->get('request')->getSession());
         $cartArray = $cart->getSessionCart();
         $cartOrder = new CartOrder();
         $em = $this->getDoctrine()->getEntityManager();
         $user = $this->container->get('security.context')->getToken()->getUser();
         $isCartExist = $em->getRepository('TohouriStoreBundle:Cart')->findBySessionId($this->container->get('request')->getSession()->getId());
         if (!$isCartExist) {
             // throw $this->createNotFoundException('No cart found for session '.$this->container->get('request')->getSession()->getId());
             $cart->setUser($user);
             $cart->setSessionId($this->container->get('request')->getSession()->getId());
             $em->persist($cart);
             $em->flush();
         }
         // Saving client order
         foreach ($cartArray as $id => $quantity) {
             $em = $this->getDoctrine()->getEntityManager();
             $product = $em->getRepository('TohouriStoreBundle:Product')->find($id);
             $clientCart = $em->getRepository('TohouriStoreBundle:Cart')->findOneBySessionId($this->container->get('request')->getSession()->getId());
             $cartOrder->setProduct($product);
             $cartOrder->setQuantity($quantity);
             $cartOrder->setCart($clientCart);
             $clientCart->addCartOrder($cartOrder);
             $em->persist($clientCart);
             $em->flush();
             $em->clear();
         }
         // --**A faire**--: comparer le count de la collection cartOrder lié a la session avec $cartArray
         // avant de confirmer la sauvegarde correcte
         $this->get('session')->setFlash('notice', 'Votre commande de ' . count($cartArray) . ' produit(s) à été envoyée! User id=' . $user->getId());
     }
     return $this->redirect($this->generateUrl('TohouriStoreBundle_cart'));
 }