/** * Product Model Edit * * @param ProductModel $model * @Route("/edit/{id}", name="product_model_edit") * @return \Symfony\Component\HttpFoundation\Response */ public function editAction(Request $request, ProductModel $model) { // Keep old materials $originalMaterials = new ArrayCollection(); foreach ($model->getMaterials() as $material) { $originalMaterials->add($material); } $form = $this->createForm(new ProductModelType(), $model); if ($request->isMethod('POST')) { $form->handleRequest($request); if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); // New materials foreach ($originalMaterials as $material) { if ($model->getMaterials()->contains($material) === false) { $em->remove($material); } } $em->persist($model); $em->flush(); $this->get('session')->getFlashBag()->add('success', 'Model edited !'); return $this->redirect($this->generateUrl('product_model_index')); } } return $this->render('ValentinStockBundle:ProductModel:edit.html.twig', array('model' => $model, 'form' => $form->createView())); }
/** * Check if there is enough material * for a model given * * @param ProductModel $productModel * @param $total * * @return bool */ public function isEnoughMaterialsForModel(ProductModel $productModel, $total) { $isAvailable = true; $total = (int) $total; $materials = $productModel->getMaterials(); foreach ($materials as $mp) { $materialsNeeded = $mp->getQuantity() * $total; if ($mp->getMaterial()->isAvailableQuantity($materialsNeeded) === false) { $isAvailable = false; break; } } return $isAvailable; }