/** * Get prices by agegroup * * @param \ErsBase\Entity\Agegroup $agegroup * @return type */ public function getProductPrice(Agegroup $agegroup = null, Deadline $deadline = null, $search = true) { $ret = new ProductPrice(); foreach ($this->getProductPrices() as $price) { /* * if a agegroup is given but price has none */ if ($price->getAgegroup() == null && $agegroup != null) { continue; } /* * if a deadline is given but price has none */ if ($price->getDeadline() == null && $deadline != null) { continue; } /* * if no agegroup is given but price has one */ if ($price->getAgegroup() != null && $agegroup == null) { continue; } /* * if no deadline is given but price has one */ if ($price->getDeadline() != null && $deadline == null) { continue; } /* * if agegroup does not match */ if ($price->getAgegroup() != null && $agegroup != null && $price->getAgegroup()->getId() != $agegroup->getId()) { continue; } /* * if deadline does not match */ if ($price->getDeadline() != null && $deadline != null && $price->getDeadline()->getId() != $deadline->getId()) { continue; } /* * at this point we should only have the prices we want, take the highest one. */ if ($ret->getCharge() < $price->getCharge()) { $ret = $price; } } if ($ret->getCharge() == null && $search) { /* * start searching only by agegroup */ $ret = $this->getProductPrice($agegroup, null, false); if ($ret->getCharge() == null) { $ret = $this->getProductPrice(null, null, false); } } return $ret; }
public function addAction() { $id = (int) $this->params()->fromRoute('id', 0); if (!$id) { return $this->redirect()->toRoute('admin/product'); } $forrest = new Service\BreadcrumbService(); $em = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); $productprice = new Entity\ProductPrice(); $productprice->setProductId($id); $form = new Form\ProductPrice(); $form->bind($productprice); $form->get('Deadline_id')->setValueOptions($this->getDeadlineOptions()); $form->get('Agegroup_id')->setValueOptions($this->getAgegroupOptions()); $form->get('submit')->setValue('Add'); $request = $this->getRequest(); if ($request->isPost()) { $productprice = new Entity\ProductPrice(); #$form->setInputFilter($productprice->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $productprice = $form->getData(); if ($productprice->getDeadlineId() == 0) { $productprice->setDeadline(null); } else { $deadline = $em->getRepository("ErsBase\\Entity\\Deadline")->findOneBy(array('id' => $productprice->getDeadlineId())); $productprice->setDeadline($deadline); } if ($productprice->getAgegroupId() == 0) { $productprice->setAgegroup(null); } else { $agegroup = $em->getRepository("ErsBase\\Entity\\Agegroup")->findOneBy(array('id' => $productprice->getAgegroupId())); $productprice->setAgegroup($agegroup); } $product = $em->getRepository("ErsBase\\Entity\\Product")->findOneBy(array('id' => $productprice->getProductId())); $productprice->setProduct($product); $em->persist($productprice); $em->flush(); if (!$forrest->exists('product-price')) { $forrest->set('product-price', 'product'); } $breadcrumb = $forrest->get('product-price'); return $this->redirect()->toRoute($breadcrumb->route, $breadcrumb->params, $breadcrumb->options); } else { $logger = $this->getServiceLocator()->get('Logger'); $logger->warn($form->getMessages()); } } $product = $em->getRepository("ErsBase\\Entity\\Product")->findOneBy(array('id' => $id)); if (!$forrest->exists('product-price')) { $forrest->set('product-price', 'admin/product'); } return new ViewModel(array('product' => $product, 'form' => $form, 'breadcrumb' => $forrest->get('product-price'))); }