/** * @param $productId * @return array */ public function getDelayForProduct($productId) { $delay = ProductDelayQuery::create()->filterByProductId($productId)->findOneOrCreate(); $defaultDelay = (new ProductDelay())->getDefaultValue(); $product = ProductQuery::create()->findOneById($productId); if (!$product) { return null; } $quantity = $this->productHasQuantity($product); if (true === $quantity) { $delayMin = $delay->getDeliveryDelayMin() ? $delay->getDeliveryDelayMin() : $defaultDelay->getDeliveryDelayMin(); $delayMax = $delay->getDeliveryDelayMax() ? $delay->getDeliveryDelayMax() : $defaultDelay->getDeliveryDelayMax(); } else { $delayMin = $delay->getRestockDelayMin() ? $delay->getRestockDelayMin() : $defaultDelay->getRestockDelayMin(); $delayMax = $delay->getRestockDelayMax() ? $delay->getRestockDelayMax() : $defaultDelay->getRestockDelayMax(); } $startDate = date("Y-m-d"); $delivery["deliveryDateStart"] = null; if (null !== $delay->getDeliveryDateStart() && time() < strtotime($delay->getDeliveryDateStart())) { $startDate = $delivery["deliveryDateStart"] = $delay->getDeliveryDateStart(); } $delivery["deliveryType"] = $delay->getDeliveryType(); $delivery["deliveryMin"] = $this->computeDeliveryDate($startDate, $delayMin); $delivery["deliveryMax"] = $this->computeDeliveryDate($startDate, $delayMax); return $delivery; }
public function setProductConfig($product_id) { if (null !== ($response = $this->checkAuth(array(AdminResources::MODULE), array('DeliveryDelay'), AccessManager::UPDATE))) { return $response; } $form = $this->createForm("deliverydelay.form"); try { $data = $this->validateForm($form)->getData(); $productDelay = ProductDelayQuery::create()->filterByProductId($product_id)->findOneOrCreate(); $productDelay->setDeliveryDelayMin($data["delivery_min"])->setDeliveryDelayMax($data["delivery_max"])->setRestockDelayMin($data["restock_min"])->setRestockDelayMax($data["restock_max"])->setDeliveryDateStart($data["delivery_date_start"])->setDeliveryType($data["delivery_type"])->save(); return new JsonResponse($this->getTranslator()->trans("Delivery delay product configuration updated with success!", [], DeliveryDelay::DOMAIN_NAME)); } catch (\Exception $e) { $message = $this->getTranslator()->trans("Error on delivery delay product configuration : %message", ["%message" => $e->getMessage()], DeliveryDelay::DOMAIN_NAME); $form->setErrorMessage($message); $this->getParserContext()->addForm($form)->setGeneralError($message); return new JsonResponse($message, 500); } }
protected function buildForm() { $request = $this->getRequest(); $productId = $request->get("product_id"); $productData = ProductDelayQuery::create()->filterByProductId($productId)->findOneOrCreate(); if (null !== $productId) { $data["delivery_min"] = $productData->getDeliveryDelayMin(); $data["delivery_max"] = $productData->getDeliveryDelayMax(); $data["restock_min"] = $productData->getRestockDelayMin(); $data["restock_max"] = $productData->getRestockDelayMax(); $data["delivery_date_start"] = $productData->getDeliveryDateStart(); $data["delivery_type"] = $productData->getDeliveryType(); } else { $data["delivery_min"] = DeliveryDelay::getConfigValue("delivery_min"); $data["delivery_max"] = DeliveryDelay::getConfigValue("delivery_max"); $data["restock_min"] = DeliveryDelay::getConfigValue("restock_min"); $data["restock_max"] = DeliveryDelay::getConfigValue("restock_max"); $data["delivery_date_start"] = null; $data["delivery_type"] = null; } $this->formBuilder->add("delivery_min", "text", array('data' => $data["delivery_min"], 'label' => Translator::getInstance()->trans("Minimum delivery delay (days)", array(), DeliveryDelay::DOMAIN_NAME), 'label_attr' => array("for" => "delivery_min")))->add("delivery_max", "text", array('data' => $data["delivery_max"], 'label' => Translator::getInstance()->trans("Maximum delivery delay (days)", array(), DeliveryDelay::DOMAIN_NAME), 'label_attr' => array("for" => "delivery_max")))->add("restock_min", "text", array('data' => $data["restock_min"], 'label' => Translator::getInstance()->trans("Minimum restock delay (days)", array(), DeliveryDelay::DOMAIN_NAME), 'label_attr' => array("for" => "restock_min")))->add("restock_max", "text", array('data' => $data["restock_max"], 'label' => Translator::getInstance()->trans("Maximum restock delay (days)", array(), DeliveryDelay::DOMAIN_NAME), 'label_attr' => array("for" => "restock_max")))->add("exclude_weekend", "text", array('label' => Translator::getInstance()->trans("Exclude weekend from delay count", array(), DeliveryDelay::DOMAIN_NAME), 'data' => DeliveryDelay::getConfigValue("exclude_weekend"), 'label_attr' => array("for" => "exclude_weekend")))->add("exclude_easter_day", "text", array('label' => Translator::getInstance()->trans("Exclude easter day from delay count", array(), DeliveryDelay::DOMAIN_NAME), 'data' => DeliveryDelay::getConfigValue("exclude_easter_day"), 'label_attr' => array("for" => "exclude_easter_day")))->add("exclude_easter_day_based_holidays", "text", array('label' => Translator::getInstance()->trans("Exclude holidays based on easter day date", array(), DeliveryDelay::DOMAIN_NAME), 'data' => DeliveryDelay::getConfigValue("exclude_easter_day_based_holidays"), 'label_attr' => array("for" => "exclude_easter_day_based_holidays")))->add("delivery_date_start", "text", array('label' => Translator::getInstance()->trans("This product is only available from", array(), DeliveryDelay::DOMAIN_NAME), 'data' => $data['delivery_date_start'], 'label_attr' => array("for" => "delivery_date_start")))->add("delivery_type", "text", array('label' => Translator::getInstance()->trans("Type of delivery", array(), DeliveryDelay::DOMAIN_NAME), 'data' => $data["delivery_type"], 'label_attr' => array("for" => "delivery_type"))); }
/** * Performs an INSERT on the database, given a ProductDelay or Criteria object. * * @param mixed $criteria Criteria or ProductDelay object containing data that is used to create the INSERT statement. * @param ConnectionInterface $con the ConnectionInterface connection to use * @return mixed The new primary key. * @throws PropelException Any exceptions caught during processing will be * rethrown wrapped into a PropelException. */ public static function doInsert($criteria, ConnectionInterface $con = null) { if (null === $con) { $con = Propel::getServiceContainer()->getWriteConnection(ProductDelayTableMap::DATABASE_NAME); } if ($criteria instanceof Criteria) { $criteria = clone $criteria; // rename for clarity } else { $criteria = $criteria->buildCriteria(); // build Criteria from ProductDelay object } if ($criteria->containsKey(ProductDelayTableMap::ID) && $criteria->keyContainsValue(ProductDelayTableMap::ID)) { throw new PropelException('Cannot insert a value for auto-increment primary key (' . ProductDelayTableMap::ID . ')'); } // Set the correct dbName $query = ProductDelayQuery::create()->mergeWith($criteria); try { // use transaction because $criteria could contain info // for more than one table (I guess, conceivably) $con->beginTransaction(); $pk = $query->doInsert($con); $con->commit(); } catch (PropelException $e) { $con->rollBack(); throw $e; } return $pk; }
/** * Removes this object from datastore and sets delete attribute. * * @param ConnectionInterface $con * @return void * @throws PropelException * @see ProductDelay::setDeleted() * @see ProductDelay::isDeleted() */ public function delete(ConnectionInterface $con = null) { if ($this->isDeleted()) { throw new PropelException("This object has already been deleted."); } if ($con === null) { $con = Propel::getServiceContainer()->getWriteConnection(ProductDelayTableMap::DATABASE_NAME); } $con->beginTransaction(); try { $deleteQuery = ChildProductDelayQuery::create()->filterByPrimaryKey($this->getPrimaryKey()); $ret = $this->preDelete($con); if ($ret) { $deleteQuery->delete($con); $this->postDelete($con); $con->commit(); $this->setDeleted(true); } else { $con->commit(); } } catch (Exception $e) { $con->rollBack(); throw $e; } }