示例#1
0
 public function buildModelCriteria()
 {
     $search = TaxRuleQuery::create();
     /* manage translations */
     $this->configureI18nProcessing($search, array('TITLE', 'DESCRIPTION'));
     $id = $this->getId();
     if (null !== $id) {
         $search->filterById($id, Criteria::IN);
     }
     $exclude = $this->getExclude();
     if (null !== $exclude) {
         $search->filterById($exclude, Criteria::NOT_IN);
     }
     $orders = $this->getOrder();
     foreach ($orders as $order) {
         switch ($order) {
             case "id":
                 $search->orderById(Criteria::ASC);
                 break;
             case "id_reverse":
                 $search->orderById(Criteria::DESC);
                 break;
             case "alpha":
                 $search->addAscendingOrderByColumn('i18n_TITLE');
                 break;
             case "alpha_reverse":
                 $search->addDescendingOrderByColumn('i18n_TITLE');
                 break;
         }
     }
     return $search;
 }
 public function verifyTaxRuleId($value, ExecutionContextInterface $context)
 {
     $taxRule = TaxRuleQuery::create()->findPk($value);
     if (null === $taxRule) {
         $context->addViolation(Translator::getInstance()->trans("Tax rule ID not found"));
     }
 }
示例#3
0
 public function testCreate()
 {
     $event = new ProductCreateEvent();
     $defaultCategory = CategoryQuery::create()->select('id')->addAscendingOrderByColumn('RAND()')->findOne();
     $taxRuleId = TaxRuleQuery::create()->select('id')->addAscendingOrderByColumn('RAND()')->findOne();
     $currencyId = CurrencyQuery::create()->select('id')->addAscendingOrderByColumn('RAND()')->findOne();
     $event->setRef('testCreation')->setLocale('fr_FR')->setTitle('test create new product')->setVisible(1)->setDefaultCategory($defaultCategory)->setBasePrice(10)->setTaxRuleId($taxRuleId)->setBaseWeight(10)->setCurrencyId($currencyId)->setDispatcher($this->getDispatcher());
     $action = new Product();
     $action->create($event);
     $createdProduct = $event->getProduct();
     $this->assertInstanceOf('Thelia\\Model\\Product', $createdProduct);
     $this->assertFalse($createdProduct->isNew());
     $createdProduct->setLocale('fr_FR');
     $this->assertEquals('test create new product', $createdProduct->getTitle());
     $this->assertEquals('testCreation', $createdProduct->getRef());
     $this->assertEquals(1, $createdProduct->getVisible());
     $this->assertEquals($defaultCategory, $createdProduct->getDefaultCategoryId());
     $this->assertGreaterThan(0, $createdProduct->getPosition());
     $productSaleElements = $createdProduct->getProductSaleElementss();
     $this->assertEquals(1, count($productSaleElements));
     $defaultProductSaleElement = $productSaleElements->getFirst();
     $this->assertTrue($defaultProductSaleElement->getIsDefault());
     $this->assertEquals(0, $defaultProductSaleElement->getPromo());
     $this->assertEquals(0, $defaultProductSaleElement->getNewness());
     $this->assertEquals($createdProduct->getRef(), $defaultProductSaleElement->getRef());
     $this->assertEquals(10, $defaultProductSaleElement->getWeight());
     $productPrice = $defaultProductSaleElement->getProductPrices()->getFirst();
     $this->assertEquals(10, $productPrice->getPrice());
     $this->assertEquals($currencyId, $productPrice->getCurrencyId());
     return $createdProduct;
 }
示例#4
0
 public function testSearchById()
 {
     $tr = TaxRuleQuery::create()->findOne();
     if (null === $tr) {
         $tr = new \Thelia\Model\TaxRule();
         $tr->setTitle('foo');
         $tr->save();
     }
     $this->baseTestSearchById($tr->getId(), array('force_return' => true));
 }
 public function loadTaxRule(TaxRule $taxRule, Country $country, Product $product)
 {
     $this->product = null;
     $this->country = null;
     $this->taxRulesCollection = null;
     if ($taxRule->getId() === null) {
         throw new TaxEngineException('TaxRule id is empty in Calculator::loadTaxRule', TaxEngineException::UNDEFINED_TAX_RULE);
     }
     if ($country->getId() === null) {
         throw new TaxEngineException('Country id is empty in Calculator::loadTaxRule', TaxEngineException::UNDEFINED_COUNTRY);
     }
     if ($product->getId() === null) {
         throw new TaxEngineException('Product id is empty in Calculator::loadTaxRule', TaxEngineException::UNDEFINED_PRODUCT);
     }
     $this->country = $country;
     $this->product = $product;
     $key = $product->getTaxRule()->getId() . '-' . $country->getId();
     if (!isset(self::$taxRulesMap[$key])) {
         self::$taxRulesMap[$key] = $this->taxRuleQuery->getTaxCalculatorCollection($taxRule, $country);
     }
     $this->taxRulesCollection = self::$taxRulesMap[$key];
     return $this;
 }
示例#6
0
 public function loadTaxRuleWithoutProduct(TaxRule $taxRule, Country $country)
 {
     $this->product = null;
     $this->country = null;
     $this->taxRulesCollection = null;
     if ($taxRule->getId() === null) {
         throw new TaxEngineException('TaxRule id is empty in Calculator::loadTaxRule', TaxEngineException::UNDEFINED_TAX_RULE);
     }
     if ($country->getId() === null) {
         throw new TaxEngineException('Country id is empty in Calculator::loadTaxRule', TaxEngineException::UNDEFINED_COUNTRY);
     }
     $this->country = $country;
     $this->product = new Product();
     $this->taxRulesCollection = $this->taxRuleQuery->getTaxCalculatorCollection($taxRule, $country);
     return $this;
 }
示例#7
0
 public function testCreateAction()
 {
     $client = static::createClient();
     $category = CategoryQuery::create()->addAscendingOrderByColumn('RAND()')->findOne();
     $defaultCurrency = CurrencyQuery::create()->findOneByByDefault(1);
     $taxRule = TaxRuleQuery::create()->findOneByIsDefault(1);
     $product = ['ref' => uniqid('testCreateProduct'), 'locale' => 'en_US', 'title' => 'product create from api', 'description' => 'product description from api', 'default_category' => $category->getId(), 'visible' => 1, 'price' => '10', 'currency' => $defaultCurrency->getId(), 'tax_rule' => $taxRule->getId(), 'weight' => 10, 'brand_id' => 0];
     $requestContent = json_encode($product);
     $servers = $this->getServerParameters();
     $servers['CONTENT_TYPE'] = 'application/json';
     $client->request('POST', '/api/products?&sign=' . $this->getSignParameter($requestContent), [], [], $servers, $requestContent);
     $this->assertEquals(201, $client->getResponse()->getStatusCode(), 'Http status code must be 201');
     $content = json_decode($client->getResponse()->getContent(), true);
     $this->assertEquals('en_US', $content[0]['LOCALE']);
     return $content['0']['ID'];
 }
示例#8
0
 public function preImport()
 {
     // Delete table before proceeding
     ProductQuery::create()->deleteAll();
     ProductImageQuery::create()->deleteAll();
     ProductDocumentQuery::create()->deleteAll();
     TaxRuleQuery::create()->deleteAll();
     TaxQuery::create()->deleteAll();
     ProductSaleElementsQuery::create()->deleteAll();
     ProductPriceQuery::create()->deleteAll();
     // Create T1 <-> T2 IDs correspondance tables
     $this->product_corresp->reset();
     $this->tax_corresp->reset();
     // Importer les taxes
     $this->importTaxes();
 }
示例#9
0
 /**
  * Create a new product entry
  *
  * @param \Thelia\Core\Event\Product\ProductCreateEvent $event
  */
 public function create(ProductCreateEvent $event)
 {
     $product = new ProductModel();
     $product->setDispatcher($event->getDispatcher())->setRef($event->getRef())->setLocale($event->getLocale())->setTitle($event->getTitle())->setVisible($event->getVisible() ? 1 : 0)->setVirtual($event->getVirtual() ? 1 : 0)->setTaxRule(TaxRuleQuery::create()->findOneByIsDefault(true))->create($event->getDefaultCategory(), $event->getBasePrice(), $event->getCurrencyId(), $event->getTaxRuleId(), $event->getBaseWeight());
     // Set the product template, if one is defined in the category tree
     $parentCatId = $event->getDefaultCategory();
     while ($parentCatId > 0) {
         if (null === ($cat = CategoryQuery::create()->findPk($parentCatId))) {
             break;
         }
         if ($cat->getDefaultTemplateId()) {
             $product->setTemplateId($cat->getDefaultTemplateId())->save();
             break;
         }
         $parentCatId = $cat->getParent();
     }
     $event->setProduct($product);
 }
 protected function getExistingObject()
 {
     $taxRule = TaxRuleQuery::create()->findOneById($this->getRequest()->get('tax_rule_id'));
     if (null !== $taxRule) {
         $taxRule->setLocale($this->getCurrentEditionLocale());
     }
     return $taxRule;
 }
示例#11
0
 /**
  * Removes this object from datastore and sets delete attribute.
  *
  * @param      ConnectionInterface $con
  * @return void
  * @throws PropelException
  * @see TaxRule::setDeleted()
  * @see TaxRule::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(TaxRuleTableMap::DATABASE_NAME);
     }
     $con->beginTransaction();
     try {
         $deleteQuery = ChildTaxRuleQuery::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;
     }
 }
示例#12
0
 /**
  * Return the postage without tax
  * @return float|int
  */
 public function getUntaxedPostage()
 {
     // get default tax rule
     $taxRuleQuery = new TaxRuleQuery();
     $taxRule = $taxRuleQuery->findOneByIsDefault(true);
     // get default country
     $countryQuery = new CountryQuery();
     $country = $countryQuery->findOneByByDefault(true);
     // get calculator for this tax / country
     $calculator = new \Thelia\TaxEngine\Calculator();
     $calculator->loadTaxRuleWithoutProduct($taxRule, $country);
     // return untaxed price
     return round($calculator->getUntaxedPrice($this->getPostage()), 2);
 }
示例#13
0
 /**
  *
  * Calculate tax or untax price for a non existing product.
  *
  * For an existing product, use self::priceCaclulator
  *
  * @return JsonResponse
  */
 public function calculatePrice()
 {
     $return_price = 0;
     $price = floatval($this->getRequest()->query->get('price'));
     $tax_rule_id = intval($this->getRequest()->query->get('tax_rule'));
     $action = $this->getRequest()->query->get('action');
     // With ot without tax
     $taxRule = TaxRuleQuery::create()->findPk($tax_rule_id);
     if (null !== $price && null !== $taxRule) {
         $calculator = new Calculator();
         $calculator->loadTaxRuleWithoutProduct($taxRule, Country::getShopLocation());
         if ($action == 'to_tax') {
             $return_price = $calculator->getTaxedPrice($price);
         } elseif ($action == 'from_tax') {
             $return_price = $calculator->getUntaxedPrice($price);
         } else {
             $return_price = $price;
         }
     }
     return new JsonResponse(array('result' => $this->formatPrice($return_price)));
 }
示例#14
0
 /**
  * Returns a new ChildTaxRuleQuery object.
  *
  * @param     string $modelAlias The alias of a model in the query
  * @param     Criteria $criteria Optional Criteria to build the query from
  *
  * @return ChildTaxRuleQuery
  */
 public static function create($modelAlias = null, $criteria = null)
 {
     if ($criteria instanceof \Thelia\Model\TaxRuleQuery) {
         return $criteria;
     }
     $query = new \Thelia\Model\TaxRuleQuery();
     if (null !== $modelAlias) {
         $query->setModelAlias($modelAlias);
     }
     if ($criteria instanceof Criteria) {
         $query->mergeWith($criteria);
     }
     return $query;
 }
示例#15
0
 public function testCreateWithOptionalParametersAction()
 {
     $event = new ProductCreateEvent();
     /** @var Category $defaultCategory */
     $defaultCategory = CategoryQuery::create()->addAscendingOrderByColumn('RAND()')->findOne();
     $taxRuleId = TaxRuleQuery::create()->select('id')->addAscendingOrderByColumn('RAND()')->findOne();
     $currencyId = CurrencyQuery::create()->select('id')->addAscendingOrderByColumn('RAND()')->findOne();
     $templateId = $defaultCategory->getDefaultTemplateId();
     if (null === $templateId) {
         $templateId = TemplateQuery::create()->addAscendingOrderByColumn('RAND()')->findOne()->getId();
     }
     $newRef = 'testCreateWithOptionalParameters' . uniqid('_');
     $event->setRef($newRef)->setLocale('fr_FR')->setTitle('test create new product with optional parameters')->setVisible(1)->setDefaultCategory($defaultCategory->getId())->setBasePrice(10)->setTaxRuleId($taxRuleId)->setBaseWeight(10)->setCurrencyId($currencyId)->setBaseQuantity(10)->setTemplateId($templateId);
     $action = new Product($this->getMockEventDispatcher());
     $action->create($event);
     $createdProduct = $event->getProduct();
     $this->assertInstanceOf('Thelia\\Model\\Product', $createdProduct);
     $this->assertFalse($createdProduct->isNew());
     $createdProduct->setLocale('fr_FR');
     $this->assertEquals('test create new product with optional parameters', $createdProduct->getTitle());
     $this->assertEquals($newRef, $createdProduct->getRef());
     $this->assertEquals(1, $createdProduct->getVisible());
     $this->assertEquals($defaultCategory->getId(), $createdProduct->getDefaultCategoryId());
     $this->assertGreaterThan(0, $createdProduct->getPosition());
     $this->assertEquals($templateId, $createdProduct->getTemplateId());
     $productSaleElements = $createdProduct->getProductSaleElementss();
     $this->assertEquals(1, count($productSaleElements));
     $defaultProductSaleElement = $productSaleElements->getFirst();
     $this->assertTrue($defaultProductSaleElement->getIsDefault());
     $this->assertEquals(0, $defaultProductSaleElement->getPromo());
     $this->assertEquals(0, $defaultProductSaleElement->getNewness());
     $this->assertEquals($createdProduct->getRef(), $defaultProductSaleElement->getRef());
     $this->assertEquals(10, $defaultProductSaleElement->getWeight());
     $this->assertEquals(10, $defaultProductSaleElement->getQuantity());
     /** @var ProductPrice $productPrice */
     $productPrice = $defaultProductSaleElement->getProductPrices()->getFirst();
     $this->assertEquals(10, $productPrice->getPrice());
     $this->assertEquals($currencyId, $productPrice->getCurrencyId());
 }
 /**
  * @param array $data
  * @return array
  *
  * Return the untaxed prices to store
  */
 protected function extractPrices(array $data)
 {
     $calculator = new Calculator();
     $calculator->loadTaxRuleWithoutProduct(TaxRuleQuery::create()->findPk($data["tax_rule_id"]), Country::getShopLocation());
     $price = null === $data["price_with_tax"] ? $data["price"] : $calculator->getUntaxedPrice($data["price_with_tax"]);
     $salePrice = null === $data["sale_price_with_tax"] ? $data["sale_price"] : $calculator->getUntaxedPrice($data["sale_price_with_tax"]);
     return [$price, $salePrice];
 }
 /**
  * Copy of default product loop price filter but with tax applied to asked price
  * @param $search
  * @param $minPriceTTC
  * @param $maxPriceTTC
  * @throws \Propel\Runtime\Exception\PropelException
  */
 protected function managePriceFilter(&$search, $minPriceTTC, $maxPriceTTC)
 {
     $categoryId = $this->getCategoryId();
     $taxeRuleQuery = TaxRuleQuery::create();
     $categoryJoin = new Join();
     $categoryJoin->addExplicitCondition(TaxRuleTableMap::TABLE_NAME, 'ID', null, CriteriaSearchCategoryTaxRuleTableMap::TABLE_NAME, 'TAX_RULE_ID', null);
     $categoryJoin->setJoinType(Criteria::LEFT_JOIN);
     $taxeRuleQuery->addJoinObject($categoryJoin, 'category_join')->addJoinCondition('category_join', CriteriaSearchCategoryTaxRuleTableMap::CATEGORY_ID . ' = ' . $categoryId);
     $taxeRule = $taxeRuleQuery->findOne();
     $taxCountry = $this->container->get('thelia.taxEngine')->getDeliveryCountry();
     $calculator = new Calculator();
     $calculator->loadTaxRuleWithoutProduct($taxeRule, $taxCountry);
     $currencyId = $this->getCurrency();
     if (null !== $currencyId) {
         $currency = CurrencyQuery::create()->findOneById($currencyId);
         if (null === $currency) {
             throw new \InvalidArgumentException('Cannot found currency id: `' . $currency . '` in product_sale_elements loop');
         }
     } else {
         $currency = $this->request->getSession()->getCurrency();
     }
     $defaultCurrency = CurrencyQuery::create()->findOneByByDefault(1);
     $defaultCurrencySuffix = '_default_currency';
     if (null !== $minPriceTTC) {
         $minPriceHt = round($calculator->getUntaxedPrice($minPriceTTC), 2);
         $isPSELeftJoinList[] = 'is_min_price';
         $minPriceJoin = new Join();
         $minPriceJoin->addExplicitCondition(ProductSaleElementsTableMap::TABLE_NAME, 'ID', 'is_min_price_ttc', ProductPriceTableMap::TABLE_NAME, 'PRODUCT_SALE_ELEMENTS_ID', 'min_price_ttc_data');
         $minPriceJoin->setJoinType(Criteria::LEFT_JOIN);
         $search->joinProductSaleElements('is_min_price_ttc', Criteria::LEFT_JOIN)->addJoinObject($minPriceJoin, 'is_min_price_ttc_join')->addJoinCondition('is_min_price_ttc_join', '`min_price_ttc_data`.`currency_id` = ?', $currency->getId(), null, \PDO::PARAM_INT);
         if ($defaultCurrency->getId() != $currency->getId()) {
             $minPriceJoinDefaultCurrency = new Join();
             $minPriceJoinDefaultCurrency->addExplicitCondition(ProductSaleElementsTableMap::TABLE_NAME, 'ID', 'is_min_price_ttc', ProductPriceTableMap::TABLE_NAME, 'PRODUCT_SALE_ELEMENTS_ID', 'min_price_ttc_data' . $defaultCurrencySuffix);
             $minPriceJoinDefaultCurrency->setJoinType(Criteria::LEFT_JOIN);
             $search->addJoinObject($minPriceJoinDefaultCurrency, 'is_min_price_ttc_join' . $defaultCurrencySuffix)->addJoinCondition('is_min_price_ttc_join' . $defaultCurrencySuffix, '`min_price_ttc_data' . $defaultCurrencySuffix . '`.`currency_id` = ?', $defaultCurrency->getId(), null, \PDO::PARAM_INT);
             /**
              * In propel we trust : $currency->getRate() always returns a float.
              * Or maybe not : rate value is checked as a float in overloaded getRate method.
              */
             $MinPriceToCompareAsSQL = 'CASE WHEN ISNULL(CASE WHEN `is_min_price_ttc`.PROMO=1 THEN `min_price_ttc_data`.PROMO_PRICE ELSE `min_price_ttc_data`.PRICE END) OR `min_price_ttc_data`.FROM_DEFAULT_CURRENCY = 1 THEN
                 CASE WHEN `is_min_price_ttc`.PROMO=1 THEN `min_price_ttc_data' . $defaultCurrencySuffix . '`.PROMO_PRICE ELSE `min_price_ttc_data' . $defaultCurrencySuffix . '`.PRICE END * ' . $currency->getRate() . '
             ELSE
                 CASE WHEN `is_min_price_ttc`.PROMO=1 THEN `min_price_ttc_data`.PROMO_PRICE ELSE `min_price_ttc_data`.PRICE END
             END';
         } else {
             $MinPriceToCompareAsSQL = 'CASE WHEN `is_min_price_ttc`.PROMO=1 THEN `min_price_ttc_data`.PROMO_PRICE ELSE `min_price_ttc_data`.PRICE END';
         }
         $search->where('ROUND(' . $MinPriceToCompareAsSQL . ', 2)>=?', $minPriceHt, \PDO::PARAM_STR);
     }
     if (null !== $maxPriceTTC) {
         $maxPriceHt = round($calculator->getUntaxedPrice($maxPriceTTC), 2);
         $isPSELeftJoinList[] = 'is_max_price_ttc';
         $maxPriceJoin = new Join();
         $maxPriceJoin->addExplicitCondition(ProductSaleElementsTableMap::TABLE_NAME, 'ID', 'is_max_price_ttc', ProductPriceTableMap::TABLE_NAME, 'PRODUCT_SALE_ELEMENTS_ID', 'max_price_ttc_data');
         $maxPriceJoin->setJoinType(Criteria::LEFT_JOIN);
         $search->joinProductSaleElements('is_max_price_ttc', Criteria::LEFT_JOIN)->addJoinObject($maxPriceJoin, 'is_max_price_ttc_join')->addJoinCondition('is_max_price_ttc_join', '`max_price_ttc_data`.`currency_id` = ?', $currency->getId(), null, \PDO::PARAM_INT);
         if ($defaultCurrency->getId() != $currency->getId()) {
             $maxPriceJoinDefaultCurrency = new Join();
             $maxPriceJoinDefaultCurrency->addExplicitCondition(ProductSaleElementsTableMap::TABLE_NAME, 'ID', 'is_max_price_ttc', ProductPriceTableMap::TABLE_NAME, 'PRODUCT_SALE_ELEMENTS_ID', 'max_price_ttc_data' . $defaultCurrencySuffix);
             $maxPriceJoinDefaultCurrency->setJoinType(Criteria::LEFT_JOIN);
             $search->addJoinObject($maxPriceJoinDefaultCurrency, 'is_max_price_ttc_join' . $defaultCurrencySuffix)->addJoinCondition('is_max_price_ttc_join' . $defaultCurrencySuffix, '`max_price_ttc_data' . $defaultCurrencySuffix . '`.`currency_id` = ?', $defaultCurrency->getId(), null, \PDO::PARAM_INT);
             /**
              * In propel we trust : $currency->getRate() always returns a float.
              * Or maybe not : rate value is checked as a float in overloaded getRate method.
              */
             $MaxPriceToCompareAsSQL = 'CASE WHEN ISNULL(CASE WHEN `is_max_price_ttc`.PROMO=1 THEN `max_price_ttc_data`.PROMO_PRICE ELSE `max_price_ttc_data`.PRICE END) OR `min_price_data`.FROM_DEFAULT_CURRENCY = 1 THEN
                 CASE WHEN `is_max_price_ttc`.PROMO=1 THEN `max_price_ttc_data' . $defaultCurrencySuffix . '`.PROMO_PRICE ELSE `max_price_ttc_data' . $defaultCurrencySuffix . '`.PRICE END * ' . $currency->getRate() . '
             ELSE
                 CASE WHEN `is_max_price_ttc`.PROMO=1 THEN `max_price_ttc_data`.PROMO_PRICE ELSE `max_price_ttc_data`.PRICE END
             END';
         } else {
             $MaxPriceToCompareAsSQL = 'CASE WHEN `is_max_price_ttc`.PROMO=1 THEN `max_price_ttc_data`.PROMO_PRICE ELSE `max_price_ttc_data`.PRICE END';
         }
         $search->where('ROUND(' . $MaxPriceToCompareAsSQL . ', 2)<=?', $maxPriceHt, \PDO::PARAM_STR);
     }
 }
 protected function getTaxRule()
 {
     $product = TaxRuleQuery::create()->findOne();
     if (null === $product) {
         $this->markTestSkipped("You must have at least one tax rule to run this test");
     }
     return $product;
 }
示例#19
0
 /**
  * Create a new product entry
  *
  * @param \Thelia\Core\Event\Product\ProductCreateEvent $event
  */
 public function create(ProductCreateEvent $event)
 {
     $product = new ProductModel();
     $product->setDispatcher($event->getDispatcher())->setRef($event->getRef())->setLocale($event->getLocale())->setTitle($event->getTitle())->setVisible($event->getVisible() ? 1 : 0)->setVirtual($event->getVirtual() ? 1 : 0)->setTaxRule(TaxRuleQuery::create()->findOneByIsDefault(true))->create($event->getDefaultCategory(), $event->getBasePrice(), $event->getCurrencyId(), $event->getTaxRuleId(), $event->getBaseWeight());
     $event->setProduct($product);
 }
示例#20
0
 /**
  * Performs an INSERT on the database, given a TaxRule or Criteria object.
  *
  * @param mixed               $criteria Criteria or TaxRule 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(TaxRuleTableMap::DATABASE_NAME);
     }
     if ($criteria instanceof Criteria) {
         $criteria = clone $criteria;
         // rename for clarity
     } else {
         $criteria = $criteria->buildCriteria();
         // build Criteria from TaxRule object
     }
     if ($criteria->containsKey(TaxRuleTableMap::ID) && $criteria->keyContainsValue(TaxRuleTableMap::ID)) {
         throw new PropelException('Cannot insert a value for auto-increment primary key (' . TaxRuleTableMap::ID . ')');
     }
     // Set the correct dbName
     $query = TaxRuleQuery::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;
 }
示例#21
0
 protected function getPersistentData($taxRuleId)
 {
     $taxRule = TaxRuleQuery::create()->findPk($taxRuleId);
     if (null === $taxRule) {
         throw new HttpException(404, json_encode(["error" => sprintf("The tax rule %d doesn't exist", $taxRuleId)]));
     }
     $countries = TaxRuleCountryQuery::create()->filterByTaxRuleId($taxRuleId)->distinct()->select(TaxRuleCountryTableMap::COUNTRY_ID)->find()->toArray();
     $taxes = TaxRuleCountryQuery::create()->filterByTaxRuleId($taxRuleId)->distinct()->select(TaxRuleCountryTableMap::TAX_ID)->find()->toArray();
     $data = ["default" => (bool) $taxRule->getIsDefault(), "tax" => $taxes, "country" => $countries];
     return $data;
 }
示例#22
0
 public static function tearDownAfterClass()
 {
     TaxRuleQuery::create()->filterById(self::$defaultId)->update(array('IsDefault' => true));
 }
示例#23
0
 /**
  * @param TaxRuleEvent $event
  */
 public function setDefault(TaxRuleEvent $event)
 {
     if (null !== ($taxRule = TaxRuleQuery::create()->findPk($event->getId()))) {
         TaxRuleQuery::create()->update(array("IsDefault" => 0));
         $taxRule->setIsDefault(1)->save();
         $event->setTaxRule($taxRule);
     }
 }
示例#24
0
 /**
  * Get the associated ChildTaxRule object
  *
  * @param      ConnectionInterface $con Optional Connection object.
  * @return                 ChildTaxRule The associated ChildTaxRule object.
  * @throws PropelException
  */
 public function getTaxRule(ConnectionInterface $con = null)
 {
     if ($this->aTaxRule === null && $this->tax_rule_id !== null) {
         $this->aTaxRule = ChildTaxRuleQuery::create()->findPk($this->tax_rule_id, $con);
         /* The following can be used additionally to
               guarantee the related object contains a reference
               to this object.  This level of coupling may, however, be
               undesirable since it could result in an only partially populated collection
               in the referenced object.
               $this->aTaxRule->addProducts($this);
            */
     }
     return $this->aTaxRule;
 }