protected function calculateConcrete(\Bitrix\Sale\Shipment $shipment = null) { $result = new CalculationResult(); $price = $this->config["MAIN"]["PRICE"]; if ($shipment && \Bitrix\Main\Loader::includeModule('currency')) { $rates = new \CCurrencyRates(); $currency = $this->currency; $shipmentCurrency = $shipment->getCollection()->getOrder()->getCurrency(); $price = $rates->convertCurrency($price, $currency, $shipmentCurrency); } $result->setDeliveryPrice(roundEx($price, SALE_VALUE_PRECISION)); $result->setPeriodDescription($this->getPeriodText()); return $result; }
public function checkByShipment(\Bitrix\Sale\Shipment $shipment, array $restrictionParams, $deliveryId = 0) { if (empty($restrictionParams)) { return true; } $result = true; if (!($itemCollection = $shipment->getShipmentItemCollection())) { throw new SystemException("Cant get ShipmentItemCollection"); } $shipmentPrice = $itemCollection->getPrice(); if (\Bitrix\Main\Loader::includeModule('currency')) { $shipmentPrice = \CCurrencyRates::convertCurrency($shipmentPrice, $shipment->getCurrency(), $restrictionParams["CURRENCY"]); } if ($shipmentPrice >= 0) { $result = $this->check($shipmentPrice, $restrictionParams, $deliveryId); } return $result; }
/** * @param $profileId * @param array $profileConfig * @param \Bitrix\Sale\Shipment $shipment * @return CalculationResult * @throws SystemException * @throws \Bitrix\Main\LoaderException */ public function calculateProfile($profileId, array $profileConfig, \Bitrix\Sale\Shipment $shipment) { global $APPLICATION; $result = new CalculationResult(); $step = 0; $tmp = false; /** @var ShipmentCollection $shipmentCollection */ $shipmentCollection = $shipment->getCollection(); /** @var Order $order */ $order = $shipmentCollection->getOrder(); $shipmentCurrency = $order->getCurrency(); if (!Loader::includeModule('currency')) { throw new SystemException("Can't include module \"Currency\""); } $calculator = $this->getCalcultor(); if ($calculator !== false) { if ($res = call_user_func($calculator, $profileId, $profileConfig["CONFIG"], self::convertNewOrderToOld($shipment), ++$step, $tmp)) { if (is_array($res)) { if ($res["RESULT"] == "OK") { if (isset($res["TEXT"])) { $result->setDescription($res["TEXT"]); } if (isset($res["VALUE"])) { $result->setDeliveryPrice(floatval($res["VALUE"])); } if (isset($res["TRANSIT"])) { $result->setPeriodDescription($res["TRANSIT"]); } } else { if (isset($res["TEXT"]) && strlen($res["TEXT"]) > 0) { $result->addError(new EntityError($res["TEXT"], 'DELIVERY_CALCULATION')); } else { $result->addError(new EntityError(Loc::getMessage('SALE_DLVR_HANDL_AUT_ERROR_CALCULATION'), 'DELIVERY_CALCULATION')); } } } elseif (is_numeric($res)) { $result->setDeliveryPrice(floatval($res)); } } else { if ($ex = $APPLICATION->getException()) { $result->addError(new EntityError($ex->getString(), 'DELIVERY_CALCULATION')); } else { $result->setDeliveryPrice(0); } } if ($result->isSuccess() && $this->currency != $shipmentCurrency) { $result->setDeliveryPrice(\CCurrencyRates::convertCurrency($result->getPrice(), $this->currency, $shipmentCurrency)); } } $result->setDeliveryPrice($result->getPrice() + $this->getMarginPrice($shipment)); return $result; }
/** * @return array */ protected function loadAvailableList() { $order = $this->getOrder(); $basket = $order->getBasket(); if (!$basket) { return null; } if (!$order->isUsedVat()) { $taxExemptList = static::loadExemptList($order->getUserId()); $taxRateRes = \CSaleTaxRate::GetList(array("APPLY_ORDER" => "ASC"), array("LID" => $order->getSiteId(), "PERSON_TYPE_ID" => $order->getPersonTypeId(), "ACTIVE" => "Y", "LOCATION" => $order->getTaxLocation())); while ($taxRate = $taxRateRes->GetNext()) { if (!in_array(intval($taxRate["TAX_ID"]), $taxExemptList)) { if ($taxRate["IS_PERCENT"] != "Y") { $taxRate["VALUE"] = RoundEx(\CCurrencyRates::convertCurrency($taxRate["VALUE"], $taxRate["CURRENCY"], $order->getCurrency()), SALE_VALUE_PRECISION); $taxRate["CURRENCY"] = $order->getCurrency(); } $this->availableList[] = $taxRate; } } } else { $this->availableList[] = array("NAME" => Loc::getMessage("SOA_VAT"), "IS_PERCENT" => "Y", "VALUE" => $order->getVatRate() * 100, "VALUE_FORMATED" => "(" . $order->getVatRate() * 100 . "%, " . GetMessage("SOA_VAT_INCLUDED") . ")", "VALUE_MONEY" => $order->getVatSum(), "VALUE_MONEY_FORMATED" => SaleFormatCurrency($order->getVatSum(), $order->getCurrency()), "APPLY_ORDER" => 100, "IS_IN_PRICE" => "Y", "CODE" => "VAT"); } return $this->availableList; }
/** * Convert discount data to other currency (sale currency). * * @param array &$discount Discout data. * @param string $currency New currency. * @return void */ public static function convertCurrency(&$discount, $currency) { $currency = Currency\CurrencyManager::checkCurrencyID($currency); if ($currency === false || empty($discount) || !is_array($discount)) { return; } if (!isset($discount['VALUE_TYPE']) || !isset($discount['CURRENCY']) || $discount['CURRENCY'] == $currency) { return; } switch ($discount['VALUE_TYPE']) { case self::VALUE_TYPE_FIX: case self::VALUE_TYPE_SALE: $discount['VALUE'] = roundEx(\CCurrencyRates::convertCurrency($discount['VALUE'], $discount['CURRENCY'], $currency), CATALOG_VALUE_PRECISION); $discount['CURRENCY'] = $currency; break; case self::VALUE_TYPE_PERCENT: if ($discount['MAX_DISCOUNT'] > 0) { $discount['MAX_DISCOUNT'] = roundEx(\CCurrencyRates::convertCurrency($discount['MAX_DISCOUNT'], $discount['CURRENCY'], $currency), CATALOG_VALUE_PRECISION); } $discount['CURRENCY'] = $currency; break; } }
protected function getItemCartInfo($arItem, $currency) { $arResult = array(); $arProduct = $this->getProductById($arItem["offerId"], $arItem["count"]); if ($arProduct["CURRENCY"] != $currency && \Bitrix\Main\Loader::includeModule('currency')) { $price = \CCurrencyRates::convertCurrency($arProduct["PRICE"], $arProduct["CURRENCY"], $currency); } else { $price = $arProduct["PRICE"]; } if (isset($arProduct["error"])) { $arResult = $arProduct; } elseif (!empty($arProduct)) { $arResult = array("feedId" => $arItem["feedId"], "offerId" => $arItem["offerId"], "price" => round(floatval($price), 2), "count" => $arProduct["QUANTITY"], "weight" => $arProduct["WEIGHT"]); } return $arResult; }
/** * Set base price for all basket items. * * @param array $basket Basket. * @return void * @throws Main\ArgumentNullException */ public static function setBasketBasePrice($basket) { if (!self::$init) { self::init(); } if (!self::isSuccess() || self::$useMode == self::MODE_SYSTEM || self::$useMode == self::MODE_DISABLED) { return; } self::$basketBasePrice = array(); if (empty($basket) || !is_array($basket)) { return; } foreach ($basket as $code => $basketItem) { self::$basketBasePrice[$code] = $basketItem['CURRENCY'] == self::$config['CURRENCY'] ? $basketItem['PRICE'] : \CCurrencyRates::convertCurrency($basketItem['PRICE'], $basketItem['CURRENCY'], self::$config['CURRENCY']); } unset($code, $basketItem); }
protected static function getProductProfit($productInfo) { if (strlen($productInfo['GROSS_PROFIT']) > 0) { $profit = doubleval($productInfo['GROSS_PROFIT']); } else { $purchasingCost = 0; if (strlen($productInfo['SUMMARY_PURCHASING_PRICE']) > 0) { $purchasingCost = doubleval($productInfo['SUMMARY_PURCHASING_PRICE']); } else { $dbRes = ProductTable::getList(array('filter' => array('=ID' => $productInfo['PRODUCT_ID']), 'select' => array('ID', 'PURCHASING_PRICE', 'PURCHASING_CURRENCY'))); $productInfoBase = $dbRes->fetch(); if ($productInfoBase) { $purchasingCost = $productInfoBase['PURCHASING_PRICE'] * $productInfo['QUANTITY']; $baseCurrency = CurrencyManager::getBaseCurrency(); if ($baseCurrency != $productInfoBase['PURCHASING_CURRENCY']) { $purchasingCost = \CCurrencyRates::convertCurrency($purchasingCost, $productInfoBase['PURCHASING_CURRENCY'], $baseCurrency); } } } $profit = doubleval($productInfo['SUMMARY_PRICE']) - $purchasingCost; } return $profit; }
/** * Converts structured $where array into sql condition or empty string. * * @param array $where Structured condition. * @param string $tableAlias Table alias to use in sql. * @param string $subsectionsCondition If not empty will be added. * * @return string */ public function whereToSql(array $where, $tableAlias, $subsectionsCondition = "") { $sqlWhere = ""; $sectionCondition = "{$tableAlias}.SECTION_ID = " . $this->sectionId; $facetCondition = "{$tableAlias}.FACET_ID = " . $where["FACET_ID"]; switch ($where["TYPE"]) { case Storage::DICTIONARY: case Storage::STRING: if ($where["OP"] == "=") { $sqlWhere = $sectionCondition . " AND " . $facetCondition . " AND {$tableAlias}.VALUE_NUM = 0" . " AND {$tableAlias}.VALUE in (" . implode(", ", $where["VALUES"]) . ")"; } break; case Storage::NUMERIC: if ($where["OP"] == ">=" || $where["OP"] == "<=") { $sqlWhere = $sectionCondition . " AND " . $facetCondition . " AND {$tableAlias}.VALUE_NUM " . $where["OP"] . " " . $where["VALUES"][0] . " AND {$tableAlias}.VALUE = 0"; } elseif ($where["OP"] == "><") { $sqlWhere = $sectionCondition . " AND " . $facetCondition . " AND {$tableAlias}.VALUE_NUM BETWEEN " . $where["VALUES"][0] . " AND " . $where["VALUES"][1] . " AND {$tableAlias}.VALUE = 0"; } break; case Storage::DATETIME: if ($where["OP"] == ">=" || $where["OP"] == "<=") { $sqlWhere = $sectionCondition . " AND " . $facetCondition . " AND {$tableAlias}.VALUE_NUM " . $where["OP"] . " " . $where["VALUES"][0]; } elseif ($where["OP"] == "><") { $sqlWhere = $sectionCondition . " AND " . $facetCondition . " AND {$tableAlias}.VALUE_NUM BETWEEN " . $where["VALUES"][0] . " AND " . $where["VALUES"][1]; } elseif ($where["OP"] == "=") { $sqlWhere = $sectionCondition . " AND " . $facetCondition . " AND {$tableAlias}.VALUE_NUM in (" . implode(", ", $where["VALUES"]) . ")"; } break; case Storage::PRICE: //TODO AND FC.VALUE = 0 if ($where["OP"] == ">=" || $where["OP"] == "<=") { $sqlWhere = $sectionCondition . " AND " . $facetCondition; if ($this->toCurrencyId && $this->convertCurrencyId) { $sqlOr = array(); foreach ($this->convertCurrencyId as $currency => $currencyDictionaryId) { $convertedPrice = \CCurrencyRates::convertCurrency($where["VALUES"][0], $this->toCurrencyId, $currency); $sqlOr[] = "({$tableAlias}.VALUE = {$currencyDictionaryId} AND {$tableAlias}.VALUE_NUM " . $where["OP"] . " {$convertedPrice})"; } $sqlWhere .= " AND (" . implode(" OR ", $sqlOr) . ")"; } else { $sqlWhere .= " AND {$tableAlias}.VALUE_NUM " . $where["OP"] . " " . $where["VALUES"][0]; } } elseif ($where["OP"] == "><") { $sqlWhere = $sectionCondition . " AND " . $facetCondition; if ($this->toCurrencyId && $this->convertCurrencyId) { $sqlOr = array(); foreach ($this->convertCurrencyId as $currency => $currencyDictionaryId) { $convertedPrice1 = \CCurrencyRates::convertCurrency($where["VALUES"][0], $this->toCurrencyId, $currency); $convertedPrice2 = \CCurrencyRates::convertCurrency($where["VALUES"][1], $this->toCurrencyId, $currency); $sqlOr[] = "({$tableAlias}.VALUE = {$currencyDictionaryId} AND {$tableAlias}.VALUE_NUM BETWEEN {$convertedPrice1} AND {$convertedPrice2})"; } $sqlWhere .= " AND (" . implode(" OR ", $sqlOr) . ")"; } else { $sqlWhere .= " AND {$tableAlias}.VALUE_NUM BETWEEN " . $where["VALUES"][0] . " AND " . $where["VALUES"][1]; } } break; } if ($sqlWhere && $subsectionsCondition !== '') { $sqlWhere .= " and " . $tableAlias . "." . $subsectionsCondition; } return $sqlWhere; }
/** * Set base price for all basket items. * * @param array $basket Basket. * @return void * @throws Main\ArgumentNullException */ public function setBasketBasePrice($basket) { $order = $this->getOrder(); if ($order === null) { throw new Main\ArgumentNullException('order'); } $this->basketBasePrice = array(); if (empty($basket) || !is_array($basket)) { return; } $orderCurrency = $order->getCurrency(); foreach ($basket as $code => $basketItem) { $this->basketBasePrice[$code] = $basketItem['CURRENCY'] == $orderCurrency ? $basketItem['PRICE'] : \CCurrencyRates::convertCurrency($basketItem['PRICE'], $basketItem['CURRENCY'], $orderCurrency); } unset($code, $basketItem); }