Example #1
0
 /**
  * Update PSE for a given product
  *
  * @param array               $pseList              an array of priduct sale elements
  * @param bool                $promoStatus          true if the PSEs are on sale, false otherwise
  * @param int                 $offsetType           the offset type, see SaleModel::OFFSET_* constants
  * @param Calculator          $taxCalculator        the tax calculator
  * @param array               $saleOffsetByCurrency an array of price offset for each currency (currency ID => offset_amount)
  * @param ConnectionInterface $con
  */
 protected function updateProductSaleElementsPrices($pseList, $promoStatus, $offsetType, Calculator $taxCalculator, $saleOffsetByCurrency, ConnectionInterface $con)
 {
     /** @var ProductSaleElements $pse */
     foreach ($pseList as $pse) {
         if ($pse->getPromo() != $promoStatus) {
             $pse->setPromo($promoStatus)->save($con);
         }
         /** @var SaleOffsetCurrency $offsetByCurrency */
         foreach ($saleOffsetByCurrency as $currencyId => $offset) {
             $productPrice = ProductPriceQuery::create()->filterByProductSaleElementsId($pse->getId())->filterByCurrencyId($currencyId)->findOne($con);
             if (null !== $productPrice) {
                 // Get the taxed price
                 $priceWithTax = $taxCalculator->getTaxedPrice($productPrice->getPrice());
                 // Remove the price offset to get the taxed promo price
                 switch ($offsetType) {
                     case SaleModel::OFFSET_TYPE_AMOUNT:
                         $promoPrice = max(0, $priceWithTax - $offset);
                         break;
                     case SaleModel::OFFSET_TYPE_PERCENTAGE:
                         $promoPrice = $priceWithTax * (1 - $offset / 100);
                         break;
                     default:
                         $promoPrice = $priceWithTax;
                 }
                 // and then get the untaxed promo price.
                 $promoPrice = $taxCalculator->getUntaxedPrice($promoPrice);
                 $productPrice->setPromoPrice($promoPrice)->save($con);
             }
         }
     }
 }
Example #2
0
 /**
  * Calculate taxed/untexted price for a product
  *
  * @param $price
  * @param $price_type
  * @param  Product $product
  * @param  bool    $convert
  * @return string
  */
 protected function computePrice($price, $price_type, Product $product, $convert = false)
 {
     $calc = new Calculator();
     $calc->load($product, Country::getShopLocation());
     if ($price_type == 'without_tax') {
         $return_price = $calc->getTaxedPrice($price);
     } elseif ($price_type == 'with_tax') {
         $return_price = $calc->getUntaxedPrice($price);
     } else {
         $return_price = $price;
     }
     if ($convert != 0) {
         $return_price = $price * Currency::getDefaultCurrency()->getRate();
     }
     return floatval($return_price);
 }
 /**
  * Calculate taxed/untexted price for a product
  *
  * @param $price
  * @param $price_type
  * @param  Product $product
  * @param  bool    $convert
  * @return string
  */
 protected function computePrice($price, $price_type, Product $product, $convert = false)
 {
     $calc = new Calculator();
     $calc->load($product, Country::getShopLocation());
     if ($price_type == 'without_tax') {
         $return_price = $calc->getTaxedPrice($price);
     } elseif ($price_type == 'with_tax') {
         $return_price = $calc->getUntaxedPrice($price);
     } else {
         $return_price = $price;
     }
     if ($convert != 0) {
         $return_price = $price * Currency::getDefaultCurrency()->getRate();
     }
     // Format the number using '.', to perform further calculation
     return NumberFormat::getInstance($this->getRequest())->formatStandardNumber($return_price);
 }
 /**
  * @param Cart $cart
  * @param $areaId
  * @return |null
  */
 protected function getSlicePostage(Cart $cart, Country $country)
 {
     $config = self::getConfig();
     $currency = $cart->getCurrency();
     $areaId = $country->getAreaId();
     $query = CustomDeliverySliceQuery::create()->filterByAreaId($areaId);
     if ($config['method'] != CustomDelivery::METHOD_PRICE) {
         $query->filterByWeightMax($cart->getWeight(), Criteria::GREATER_THAN);
         $query->orderByWeightMax(Criteria::ASC);
     }
     if ($config['method'] != CustomDelivery::METHOD_WEIGHT) {
         $total = $cart->getTotalAmount();
         // convert amount to the default currency
         if (0 == $currency->getByDefault()) {
             $total = $total / $currency->getRate();
         }
         $query->filterByPriceMax($total, Criteria::GREATER_THAN);
         $query->orderByPriceMax(Criteria::ASC);
     }
     $slice = $query->findOne();
     $postage = null;
     if (null !== $slice) {
         $postage = new OrderPostage();
         if (0 == $currency->getByDefault()) {
             $price = $slice->getPrice() * $currency->getRate();
         } else {
             $price = $slice->getPrice();
         }
         $price = round($price, 2);
         $postage->setAmount($price);
         $postage->setAmountTax(0);
         // taxed amount
         if (0 !== $config['tax']) {
             $taxRuleI18N = I18n::forceI18nRetrieving($this->getRequest()->getSession()->getLang()->getLocale(), 'TaxRule', $config['tax']);
             $taxRule = TaxRuleQuery::create()->findPk($config['tax']);
             if (null !== $taxRule) {
                 $taxCalculator = new Calculator();
                 $taxCalculator->loadTaxRuleWithoutProduct($taxRule, $country);
                 $postage->setAmount(round($taxCalculator->getTaxedPrice($price), 2));
                 $postage->setAmountTax($postage->getAmount() - $price);
                 $postage->setTaxRuleTitle($taxRuleI18N->getTitle());
             }
         }
     }
     return $postage;
 }
Example #5
0
 /**
  * @expectedException \Thelia\Exception\TaxEngineException
  * @expectedExceptionCode 503
  */
 public function testGetTaxedPriceBadTaxRulesCollection()
 {
     $calculator = new Calculator();
     $calculator->getTaxedPrice(500);
 }