/** * @Route("/Soda/{id}", name="soda_detail") */ public function sodaDetail($id, Request $request) { $soda = $this->getDoctrine()->getRepository('AppBundle:Soda')->find($id); //Setting up form $orderdetail = new Orderdetail(); $orderdetail->setSoda($soda); $form = $this->createForm('orderdetailsubmit', $orderdetail); $form->handleRequest($request); if ($form->isValid()) { //get user $user = $this->get('security.token_storage')->getToken()->getUser(); //adding order $order = new Order(); $order->setUser($user); //Relation setup $order->addOrderdetail($orderdetail); $order->setOrderdate(); $orderdetail->setOrder($order); //Persist $em = $this->getDoctrine()->getManager(); $em->persist($order); //Updating left sodas $oldStorage = $this->getDoctrine()->getRepository('AppBundle:Storage')->find($soda->getStorage()->getId()); $oldStorage->setQuantity($oldStorage->getQuantity() - $orderdetail->getAmount()); $em->merge($oldStorage); $em->flush(); return $this->redirectToRoute('soda_overview'); } return $this->render('default/detail.html.twig', array('soda' => $soda, 'form' => $form->createView())); }
/** * @Route("/admin/order/create", defaults={"amount" = "1"}) * @Route("/admin/order/create/{amount}", name="admin_ordercreate") */ public function createOrder($amount, Request $request) { if ($amount < 1) { $amount = 1; } //counting how many sodas $qb = $this->getDoctrine()->getManager()->createQueryBuilder()->select('count(soda.id)')->from('AppBundle:Soda', 'soda'); $count = $qb->getQuery()->getSingleScalarResult(); if ($amount > $count) { $amount = $count; } $order = new Order(); for ($i = 0; $i < $amount; $i++) { $order->addOrderdetail(new Orderdetail()); } $form = $this->createForm('order', $order); $order->setOrderdate(); $form->handleRequest($request); if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($order); $em->flush(); //updating stock: $em = $this->getDoctrine()->getManager(); foreach ($order->getOrderdetails() as $odetail) { $soda = $odetail->getSoda(); $oldStorage = $soda->getStorage(); $oldStorage->setQuantity($oldStorage->getQuantity() - $odetail->getAmount()); $em->merge($oldStorage); } return $this->redirectToRoute('admin_front'); } return $this->render('admin/addorder.html.twig', array('order' => $order, 'id' => $amount, 'form' => $form->createView())); }