Beispiel #1
0
 public function format($number, $decimals = null, $decPoint = null, $thousandsSep = null, $symbol = null)
 {
     $number = parent::format($number, $decimals, $decPoint, $thousandsSep);
     if ($symbol !== null) {
         $number = $number . ' ' . $symbol;
     }
     return $number;
 }
Beispiel #2
0
 /**
  * @since 2.3
  * @param float $number
  * @param int $decimals
  * @param string $decPoint
  * @param string $thousandsSep
  * @param int|null $currencyId
  * @return string
  */
 public function formatByCurrency($number, $decimals = null, $decPoint = null, $thousandsSep = null, $currencyId = null)
 {
     $number = parent::format($number, $decimals, $decPoint, $thousandsSep);
     $currency = $currencyId !== null ? CurrencyQuery::create()->findPk($currencyId) : $this->request->getSession()->getCurrency();
     if ($currency !== null && strpos($currency->getFormat(), '%n') !== false) {
         return str_replace(['%n', '%s', '%c'], [$number, $currency->getSymbol(), $currency->getCode()], $currency->getFormat());
     }
     return $number;
 }
 protected function buildForm()
 {
     $this->formBuilder->add('product_id', 'integer', ['label' => Translator::getInstance()->trans('Product'), 'required' => true, 'constraints' => [new NotBlank()]])->add('currency_id', 'integer', ['label' => Translator::getInstance()->trans('Currency'), 'required' => true, 'constraints' => [new NotBlank()]]);
     $productId = $this->request->get('product_id');
     if ($productId === null) {
         $productId = $this->request->get($this->getName())['product_id'];
     }
     $product = ProductQuery::create()->findPk($productId);
     if ($product->getTemplate() === null) {
         return;
     }
     $currencyId = $this->request->get('edit_currency_id');
     if ($currencyId === null) {
         $defaultCurrency = CurrencyQuery::create()->findOneByByDefault(true);
         if ($defaultCurrency !== null) {
             $currencyId = $defaultCurrency->getId();
         }
     }
     $productAttributeAvs = AttributeAvQuery::create()->useAttributeQuery()->filterByTemplate($product->getTemplate())->endUse()->find();
     $formData = ['price_delta' => [], 'price_delta_with_tax' => []];
     /** @var TaxEngine $taxEngine */
     $taxEngine = $this->container->get('thelia.taxEngine');
     $taxCalculator = (new Calculator())->load($product, $taxEngine->getDeliveryCountry());
     /** @var AttributeAv $productAttributeAv */
     foreach ($productAttributeAvs as $productAttributeAv) {
         $legacyProductAttributeValuePrice = LegacyProductAttributeValuePriceQuery::create()->findPk([$product->getId(), $productAttributeAv->getId(), $currencyId]);
         $priceDelta = 0;
         $priceDeltaWithTax = 0;
         if (null !== $legacyProductAttributeValuePrice) {
             $priceDelta = $legacyProductAttributeValuePrice->getDelta();
             $priceDeltaWithTax = $taxCalculator->getTaxedPrice($legacyProductAttributeValuePrice->getDelta());
         }
         $numberFormatter = NumberFormat::getInstance($this->getRequest());
         $formData['price_delta'][$productAttributeAv->getId()] = $numberFormatter->formatStandardNumber($priceDelta);
         $formData['price_delta_with_tax'][$productAttributeAv->getId()] = $numberFormatter->formatStandardNumber($priceDeltaWithTax);
     }
     $this->formBuilder->add('legacy_product_attribute_value_price_delta', 'collection', ['label' => Translator::getInstance()->trans('Price difference excluding taxes', [], LegacyProductAttributes::MESSAGE_DOMAIN_BO), 'type' => 'number', 'allow_add' => true, 'allow_delete' => true, 'data' => $formData['price_delta']])->add('legacy_product_attribute_value_price_delta_with_tax', 'collection', ['label' => Translator::getInstance()->trans('Price difference including taxes', [], LegacyProductAttributes::MESSAGE_DOMAIN_BO), 'type' => 'number', 'allow_add' => true, 'allow_delete' => true, 'data' => $formData['price_delta_with_tax']]);
 }
 /**
  * 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);
 }
Beispiel #5
0
 /**
  *
  * display numbers in expected format
  *
  * available parameters :
  *  number => int or float number
  *  decimals => how many decimals format expected
  *  dec_point => separator for the decimal point
  *  thousands_sep => thousands separator
  *
  *  ex : {format_number number="1246.12" decimals="1" dec_point="," thousands_sep=" "} will output "1 246,1"
  *
  * @param $params
  * @param  null                                                   $template
  * @throws \TheliaSmarty\Template\Exception\SmartyPluginException
  * @return string                                                 the expected number formatted
  */
 public function formatNumber($params, $template = null)
 {
     $number = $this->getParam($params, "number", false);
     if ($number === false || $number === '') {
         return "";
     }
     return NumberFormat::getInstance($this->request)->format($number, $this->getParam($params, "decimals", null), $this->getParam($params, "dec_point", null), $this->getParam($params, "thousands_sep", null));
 }