示例#1
0
 /**
  * calculate the cost according to the order basket
  * @param array $arBasketItems
  * @return array|bool
  */
 public static function CalculateOrderPrices($arBasketItems)
 {
     if (!isset($arBasketItems) || isset($arBasketItems) && sizeof($arBasketItems) <= 0) {
         return false;
     }
     $arResult = array("ORDER_PRICE" => 0, "ORDER_WEIGHT" => 0, "VAT_RATE" => 0, "VAT_SUM" => 0, "USE_VAT" => 'N', "BASKET_ITEMS" => $arBasketItems);
     foreach ($arResult['BASKET_ITEMS'] as &$arItem) {
         if (!CSaleBasketHelper::isSetItem($arItem)) {
             if (array_key_exists('CUSTOM_PRICE', $arItem) && $arItem['CUSTOM_PRICE'] == 'Y') {
                 $arItem['DISCOUNT_PRICE'] = $arItem['DEFAULT_PRICE'] - $arItem['PRICE'];
                 if ($arItem['DISCOUNT_PRICE'] < 0) {
                     $arItem['DISCOUNT_PRICE'] = 0;
                 }
                 if (doubleval($arItem['DEFAULT_PRICE']) > 0) {
                     $arItem['DISCOUNT_PRICE_PERCENT'] = $arItem['DISCOUNT_PRICE'] * 100 / $arItem['DEFAULT_PRICE'];
                 } else {
                     $arItem['DISCOUNT_PRICE_PERCENT'] = 0;
                 }
                 $arItem["DISCOUNT_PRICE_PERCENT_FORMATED"] = roundEx($arItem["DISCOUNT_PRICE_PERCENT"], SALE_VALUE_PRECISION) . "%";
             }
             if (isset($arItem['CURRENCY']) && strlen($arItem['CURRENCY']) > 0) {
                 $arItem["PRICE_FORMATED"] = SaleFormatCurrency($arItem["PRICE"], $arItem["CURRENCY"]);
             }
             $arResult['ORDER_PRICE'] += CSaleBasketHelper::getFinalPrice($arItem);
             $arResult['ORDER_WEIGHT'] += $arItem["WEIGHT"] * $arItem["QUANTITY"];
             if ($arItem["VAT_RATE"] > 0) {
                 $arResult['USE_VAT'] = 'Y';
                 if ($arItem["VAT_RATE"] > $arResult['VAT_RATE']) {
                     $arResult['VAT_RATE'] = $arItem["VAT_RATE"];
                 }
                 $v = CSaleBasketHelper::getVat($arItem);
                 $arItem["VAT_VALUE"] = \Bitrix\Sale\PriceMaths::roundPrecision($v / $arItem["QUANTITY"]);
                 $arResult["VAT_SUM"] += $v;
             }
         }
     }
     $arResult['ORDER_PRICE'] = \Bitrix\Sale\PriceMaths::roundPrecision($arResult['ORDER_PRICE']);
     $arResult['VAT_SUM'] = \Bitrix\Sale\PriceMaths::roundPrecision($arResult['VAT_SUM']);
     unset($arItem);
     return $arResult;
 }
示例#2
0
 /**
  * @param \Bitrix\Main\Event $event
  *
  * @return \Bitrix\Main\EventResult
  */
 public function onSaleBasketItemEntitySaved(\Bitrix\Main\Event $event)
 {
     $basketItem = $event->getParameter('ENTITY');
     $originalValues = $event->getParameter('VALUES');
     if (!$basketItem instanceof \Bitrix\Sale\BasketItem) {
         return new \Bitrix\Main\EventResult(\Bitrix\Main\EventResult::SUCCESS, null, 'sale');
     }
     /** @var \Bitrix\Sale\Basket $basket */
     if (!($basket = $basketItem->getCollection()) || $basketItem->getFUserId() != \Bitrix\Sale\Fuser::getId(true)) {
         return new \Bitrix\Main\EventResult(\Bitrix\Main\EventResult::SUCCESS, null, 'sale');
     }
     if ($basketItem->isChanged()) {
         $updateSessionData = false;
         $updateQuantity = false;
         if (!$basket->getOrder()) {
             if (!$updateSessionData && array_key_exists('QUANTITY', $originalValues)) {
                 $updateSessionData = true;
             }
             if (!$updateSessionData && (array_key_exists('PRICE', $originalValues) && \Bitrix\Sale\PriceMaths::roundPrecision($originalValues['PRICE']) !== \Bitrix\Sale\PriceMaths::roundPrecision($basketItem->getPrice()))) {
                 $updateSessionData = true;
             }
             if (!$updateSessionData && (array_key_exists('DISCOUNT_PRICE', $originalValues) && \Bitrix\Sale\PriceMaths::roundPrecision($originalValues['DISCOUNT_PRICE']) !== \Bitrix\Sale\PriceMaths::roundPrecision($basketItem->getDiscountPrice()))) {
                 $updateSessionData = true;
             }
         }
         if (!$updateSessionData && (array_key_exists('ORDER_ID', $originalValues) && intval($originalValues['ORDER_ID']) == 0 && intval($basketItem->getField('ORDER_ID') > 0))) {
             $updateSessionData = true;
             $updateQuantity = true;
         }
         if ($updateSessionData) {
             /** @var \Bitrix\Sale\Basket $allBasket */
             if ($allBasket = $basketItem->getCollection()) {
                 $_SESSION["SALE_BASKET_PRICE"][SITE_ID] = static::getActualBasketPrice($allBasket);
                 if ($updateQuantity) {
                     $_SESSION["SALE_BASKET_NUM_PRODUCTS"][SITE_ID] = static::getActualBasketQuantity($allBasket);
                 }
             }
         }
     }
     return new \Bitrix\Main\EventResult(\Bitrix\Main\EventResult::SUCCESS, null, 'sale');
 }
示例#3
0
 function CalculateAffiliate($affiliate, $dateFrom = false, $dateTo = false, $datePlanFrom = false, $datePlanTo = false)
 {
     global $DB;
     // Prepare function params - affiliate
     $arAffiliate = CSaleAffiliate::CheckAffiliateFunc($affiliate);
     if (!$arAffiliate) {
         return False;
     }
     $db_events = GetModuleEvents("sale", "OnBeforeAffiliateCalculate");
     while ($arEvent = $db_events->Fetch()) {
         if (ExecuteModuleEventEx($arEvent, array(&$arAffiliate, &$dateFrom, &$dateTo, &$datePlanFrom, &$datePlanTo)) === false) {
             return false;
         }
     }
     $affiliateID = IntVal($arAffiliate["ID"]);
     if (!$dateFrom || StrLen($dateFrom) <= 0) {
         if (StrLen($arAffiliate["LAST_CALCULATE"]) > 0) {
             $dateFrom = $arAffiliate["LAST_CALCULATE"];
         } else {
             $dateFrom = date($DB->DateFormatToPHP(CSite::GetDateFormat("FULL")), mktime(0, 0, 0, 1, 1, 1990));
         }
     }
     if (!$dateTo || StrLen($dateTo) <= 0) {
         $dateTo = date($DB->DateFormatToPHP(CSite::GetDateFormat("FULL")), time() + CTimeZone::GetOffset());
     }
     // Get affiliate plan
     $arAffiliatePlan = CSaleAffiliate::SetAffiliatePlan($arAffiliate, $datePlanFrom, $datePlanTo);
     if (!$arAffiliatePlan) {
         return False;
     }
     if ($arAffiliatePlan && !is_array($arAffiliatePlan)) {
         return true;
     }
     // Get affiliate plan params
     $arPlanSections = array();
     $dbPlanSection = CSaleAffiliatePlanSection::GetList(array(), array("PLAN_ID" => $arAffiliate["PLAN_ID"]), false, false, array("ID", "MODULE_ID", "SECTION_ID", "RATE", "RATE_TYPE", "RATE_CURRENCY"));
     while ($arPlanSection = $dbPlanSection->Fetch()) {
         $arPlanSections[$arPlanSection["MODULE_ID"] . $arPlanSection["SECTION_ID"]] = $arPlanSection;
     }
     // Get affiliate parents
     $arAffiliateParents = array();
     $affiliateParent = IntVal($arAffiliate["AFFILIATE_ID"]);
     $count = 0;
     while ($affiliateParent > 0 && $count < 5) {
         $dbAffiliateParent = CSaleAffiliate::GetList(array(), array("ID" => $affiliateParent, "ACTIVE" => "Y"), false, false, array("ID", "AFFILIATE_ID"));
         if ($arAffiliateParent = $dbAffiliateParent->Fetch()) {
             $count++;
             $arAffiliateParents[] = $affiliateParent;
             $affiliateParent = IntVal($arAffiliateParent["AFFILIATE_ID"]);
         } else {
             $affiliateParent = 0;
         }
     }
     // Get tier
     if (!array_key_exists("SALE_AFFILIATE_TIER_TMP_CACHE", $GLOBALS)) {
         $GLOBALS["SALE_AFFILIATE_TIER_TMP_CACHE"] = array();
     }
     if (!array_key_exists($arAffiliate["SITE_ID"], $GLOBALS["SALE_AFFILIATE_TIER_TMP_CACHE"])) {
         $dbAffiliateTier = CSaleAffiliateTier::GetList(array(), array("SITE_ID" => $arAffiliate["SITE_ID"]), false, false, array("RATE1", "RATE2", "RATE3", "RATE4", "RATE5"));
         if ($arAffiliateTier = $dbAffiliateTier->Fetch()) {
             $GLOBALS["SALE_AFFILIATE_TIER_TMP_CACHE"][$arAffiliate["SITE_ID"]] = array(DoubleVal($arAffiliateTier["RATE1"]), DoubleVal($arAffiliateTier["RATE2"]), DoubleVal($arAffiliateTier["RATE3"]), DoubleVal($arAffiliateTier["RATE4"]), DoubleVal($arAffiliateTier["RATE5"]));
         } else {
             $GLOBALS["SALE_AFFILIATE_TIER_TMP_CACHE"][$arAffiliate["SITE_ID"]] = array(0, 0, 0, 0, 0);
         }
     }
     // Orders cicle
     $affiliateSum = 0;
     $affiliateCurrency = CSaleLang::GetLangCurrency($arAffiliate["SITE_ID"]);
     $dbOrders = \Bitrix\Sale\Internals\OrderTable::getList(array('filter' => array("=ALLOW_DELIVERY" => 'Y', ">=DATE_ALLOW_DELIVERY" => $dateFrom, "<DATE_ALLOW_DELIVERY" => $dateTo, "=AFFILIATE_ID" => $affiliateID, "=LID" => $arAffiliate["SITE_ID"], "=CANCELED" => 'N'), 'select' => array("ID", "LID", "PRICE_DELIVERY", "PRICE", "CURRENCY", "TAX_VALUE", "AFFILIATE_ID", "BASKET_QUANTITY" => 'BASKET.QUANTITY', "BASKET_PRODUCT_ID" => 'BASKET.PRODUCT_ID', "BASKET_MODULE" => 'BASKET.MODULE', "BASKET_PRICE" => 'BASKET.PRICE', "BASKET_CURRENCY" => 'BASKET.CURRENCY', "BASKET_DISCOUNT_PRICE" => 'BASKET.DISCOUNT_PRICE'), 'order' => array('ID' => 'ASC')));
     $fOrderId = "";
     while ($arOrder = $dbOrders->fetch()) {
         $arProductSections = array();
         if (!array_key_exists("SALE_PRODUCT_SECTION_CACHE", $GLOBALS)) {
             $GLOBALS["SALE_PRODUCT_SECTION_CACHE"] = array();
         }
         if (array_key_exists($arOrder["BASKET_MODULE"] . $arOrder["BASKET_PRODUCT_ID"], $GLOBALS["SALE_PRODUCT_SECTION_CACHE"])) {
             $arProductSections = $GLOBALS["SALE_PRODUCT_SECTION_CACHE"][$arOrder["BASKET_MODULE"] . $arOrder["BASKET_PRODUCT_ID"]];
             unset($GLOBALS["SALE_PRODUCT_SECTION_CACHE"][$arOrder["BASKET_MODULE"] . $arOrder["BASKET_PRODUCT_ID"]]);
             $GLOBALS["SALE_PRODUCT_SECTION_CACHE"] = $GLOBALS["SALE_PRODUCT_SECTION_CACHE"] + array($arOrder["BASKET_MODULE"] . $arOrder["BASKET_PRODUCT_ID"] => $arProductSections);
         } else {
             if ($arOrder["BASKET_MODULE"] == "catalog") {
                 CModule::IncludeModule("catalog");
                 $arSku = CCatalogSku::GetProductInfo($arOrder["BASKET_PRODUCT_ID"]);
                 if ($arSku && count($arSku) > 0) {
                     $elementId = $arSku["ID"];
                 } else {
                     $elementId = $arOrder["BASKET_PRODUCT_ID"];
                 }
                 $arProductSections = CCatalogProduct::GetProductSections($elementId);
             } else {
                 $events = GetModuleEvents("sale", "OnAffiliateGetSections");
                 if ($arEvent = $events->Fetch()) {
                     $arProductSections = ExecuteModuleEventEx($arEvent, array($arOrder["BASKET_MODULE"], $arOrder["BASKET_PRODUCT_ID"]));
                 }
             }
             $GLOBALS["SALE_PRODUCT_SECTION_CACHE"] = $GLOBALS["SALE_PRODUCT_SECTION_CACHE"] + array($arOrder["BASKET_MODULE"] . $arOrder["BASKET_PRODUCT_ID"] => $arProductSections);
             if (count($GLOBALS["SALE_PRODUCT_SECTION_CACHE"]) > 20) {
                 array_shift($GLOBALS["SALE_PRODUCT_SECTION_CACHE"]);
             }
         }
         $realRate = $arAffiliatePlan["BASE_RATE"];
         $realRateType = $arAffiliatePlan["BASE_RATE_TYPE"];
         $realRateCurrency = $arAffiliatePlan["BASE_RATE_CURRENCY"];
         $coountArProd = count($arProductSections);
         for ($i = 0; $i < $coountArProd; $i++) {
             if (array_key_exists($arOrder["BASKET_MODULE"] . $arProductSections[$i], $arPlanSections)) {
                 $realRate = $arPlanSections[$arOrder["BASKET_MODULE"] . $arProductSections[$i]]["RATE"];
                 $realRateType = $arPlanSections[$arOrder["BASKET_MODULE"] . $arProductSections[$i]]["RATE_TYPE"];
                 $realRateCurrency = $arPlanSections[$arOrder["BASKET_MODULE"] . $arProductSections[$i]]["RATE_CURRENCY"];
                 break;
             }
         }
         if ($realRateType == "P") {
             if ($arOrder["CURRENCY"] != $affiliateCurrency) {
                 if (!array_key_exists("SALE_CONVERT_CURRENCY_CACHE", $GLOBALS)) {
                     $GLOBALS["SALE_CONVERT_CURRENCY_CACHE"] = array();
                 }
                 if (!array_key_exists($arOrder["CURRENCY"] . "-" . $affiliateCurrency, $GLOBALS["SALE_CONVERT_CURRENCY_CACHE"])) {
                     $GLOBALS["SALE_CONVERT_CURRENCY_CACHE"][$arOrder["CURRENCY"] . "-" . $affiliateCurrency] = CCurrencyRates::GetConvertFactor($arOrder["CURRENCY"], $affiliateCurrency);
                 }
                 if ($fOrderId != $arOrder["ID"]) {
                     $affiliateSum += \Bitrix\Sale\PriceMaths::roundPrecision(($arOrder["PRICE"] - $arOrder["PRICE_DELIVERY"]) * $GLOBALS["SALE_CONVERT_CURRENCY_CACHE"][$arOrder["CURRENCY"] . "-" . $affiliateCurrency] * $realRate / 100);
                     $fOrderId = $arOrder["ID"];
                 }
             } else {
                 if ($fOrderId != $arOrder["ID"]) {
                     $affiliateSum += \Bitrix\Sale\PriceMaths::roundPrecision(($arOrder["PRICE"] - $arOrder["PRICE_DELIVERY"]) * $realRate / 100);
                     $fOrderId = $arOrder["ID"];
                 }
             }
         } else {
             if ($realRateCurrency != $affiliateCurrency) {
                 if (!array_key_exists("SALE_CONVERT_CURRENCY_CACHE", $GLOBALS)) {
                     $GLOBALS["SALE_CONVERT_CURRENCY_CACHE"] = array();
                 }
                 if (!array_key_exists($realRateCurrency . "-" . $affiliateCurrency, $GLOBALS["SALE_CONVERT_CURRENCY_CACHE"])) {
                     $GLOBALS["SALE_CONVERT_CURRENCY_CACHE"][$realRateCurrency . "-" . $affiliateCurrency] = CCurrencyRates::GetConvertFactor($realRateCurrency, $affiliateCurrency);
                 }
                 $affiliateSum += roundEx($realRate * $GLOBALS["SALE_CONVERT_CURRENCY_CACHE"][$realRateCurrency . "-" . $affiliateCurrency], SALE_VALUE_PRECISION);
             } else {
                 $affiliateSum += roundEx($realRate, SALE_VALUE_PRECISION);
             }
         }
     }
     $arFields = array("=PENDING_SUM" => "PENDING_SUM + " . $affiliateSum, "LAST_CALCULATE" => $dateTo);
     $res = CSaleAffiliate::Update($affiliateID, $arFields);
     if (!$res) {
         return False;
     }
     if ($affiliateSum > 0) {
         $cnt = min(count($arAffiliateParents), count($GLOBALS["SALE_AFFILIATE_TIER_TMP_CACHE"][$arAffiliate["SITE_ID"]]));
         for ($i = 0; $i < $cnt; $i++) {
             $affiliateSumTmp = roundEx($affiliateSum * $GLOBALS["SALE_AFFILIATE_TIER_TMP_CACHE"][$arAffiliate["SITE_ID"]][$i] / 100, SALE_VALUE_PRECISION);
             $arFields = array("=PENDING_SUM" => "PENDING_SUM + " . $affiliateSumTmp);
             CSaleAffiliate::Update($arAffiliateParents[$i], $arFields);
         }
     }
     $events = GetModuleEvents("sale", "OnAfterAffiliateCalculate");
     while ($arEvent = $events->Fetch()) {
         ExecuteModuleEventEx($arEvent, array($affiliateID));
     }
     return True;
 }
示例#4
0
 function NextPayment($ID)
 {
     global $DB;
     global $USER;
     $ID = IntVal($ID);
     if ($ID <= 0) {
         $GLOBALS["APPLICATION"]->ThrowException(GetMessage("SKGR_NO_RECID"), "NO_RECORD_ID");
         return False;
     }
     $arRecur = CSaleRecurring::GetByID($ID);
     if (!$arRecur) {
         $GLOBALS["APPLICATION"]->ThrowException(str_replace("#ID#", $ID, GetMessage("SKGR_NO_RECID1")), "NO_RECORD");
         return False;
     }
     $arOrder = CSaleOrder::GetByID($arRecur["ORDER_ID"]);
     if (!$arOrder) {
         $GLOBALS["APPLICATION"]->ThrowException(str_replace("#ID#", $arRecur["ORDER_ID"], GetMessage("SKGR_NO_ORDER1")), "NO_ORDER");
         return False;
     }
     $bSuccess = True;
     $newOrderID = IntVal($arRecur["ORDER_ID"]);
     /** @var $productProvider IBXSaleProductProvider */
     if ($productProvider = CSaleBasket::GetProductProvider($arRecur)) {
         $arProduct = $productProvider::RecurringOrderProduct(array("PRODUCT_ID" => $arRecur["PRODUCT_ID"], "USER_ID" => $arOrder["USER_ID"]));
     } else {
         $arProduct = CSaleRecurring::ExecuteCallbackFunction($arRecur["CALLBACK_FUNC"], $arRecur["MODULE"], $arRecur["PRODUCT_ID"], $arOrder["USER_ID"]);
     }
     if (!$arProduct || !is_array($arProduct) || empty($arProduct)) {
         CSaleRecurring::CancelRecurring($arRecur["ID"], "Y", "Product is not found");
         return true;
     }
     if ($arProduct["WITHOUT_ORDER"] == "Y" || $arRecur["SUCCESS_PAYMENT"] == "Y") {
         $baseSiteCurrency = CSaleLang::GetLangCurrency($arOrder["LID"]);
         $productPrice = \Bitrix\Sale\PriceMaths::roundPrecision(CCurrencyRates::ConvertCurrency($arProduct["PRICE"], $arProduct["CURRENCY"], $baseSiteCurrency));
         // Delivery
         $deliveryPrice = 0;
         $deliveryID = 0;
         $arOrder["DELIVERY_ID"] = IntVal($arOrder["DELIVERY_ID"]);
         if ($arOrder["DELIVERY_ID"] > 0) {
             $deliveryLocation = 0;
             $dbOrderPropValues = CSaleOrderPropsValue::GetList(array(), array("ORDER_ID" => $arRecur["ORDER_ID"], "PROP_IS_LOCATION" => "Y"), false, false, array("VALUE"));
             if ($arOrderPropValues = $dbOrderPropValues->Fetch()) {
                 $deliveryLocation = IntVal($arOrderPropValues["VALUE"]);
             }
             $dbDelivery = CSaleDelivery::GetList(array("SORT" => "ASC", "NAME" => "ASC"), array("LID" => $arOrder["LID"], "WEIGHT" => DoubleVal($arProduct["WEIGHT"]) * DoubleVal($arProduct["QUANTITY"]), "ORDER_PRICE" => $productPrice * DoubleVal($arProduct["QUANTITY"]), "ACTIVE" => "Y", "LOCATION" => $deliveryLocation));
             while ($arDelivery = $dbDelivery->Fetch()) {
                 $deliveryPriceTmp = \Bitrix\Sale\PriceMaths::roundPrecision(CCurrencyRates::ConvertCurrency($arDelivery["PRICE"], $arDelivery["CURRENCY"], $baseSiteCurrency));
                 if (IntVal($arDelivery["ID"]) == $arOrder["DELIVERY_ID"]) {
                     $deliveryID = IntVal($arDelivery["ID"]);
                     $deliveryPrice = $deliveryPriceTmp;
                     break;
                 }
                 if ($deliveryPriceTmp < $deliveryPrice || $deliveryID <= 0) {
                     $deliveryID = IntVal($arDelivery["ID"]);
                     $deliveryPrice = $deliveryPriceTmp;
                 }
             }
             if ($deliveryID <= 0) {
                 $deliveryID = \Bitrix\Sale\Delivery\Services\EmptyDeliveryService::getEmptyDeliveryServiceId();
                 if ($deliveryID > 0) {
                     $deliveryID = \CSaleDelivery::getCodeById($deliveryID);
                 }
             }
             if ($deliveryID <= 0) {
                 $GLOBALS["APPLICATION"]->ThrowException(GetMessage("SKGR_NO_DELIVERY"), "NO_DELIVERY");
                 return False;
             }
         }
         // Sale discounts
         $discount = 0;
         $discountPrice = $productPrice;
         $discountProduct = 0;
         $dbDiscount = CSaleDiscount::GetList(array("SORT" => "ASC"), array("LID" => $arOrder["LID"], "ACTIVE" => "Y", "!>ACTIVE_FROM" => Date($DB->DateFormatToPHP(CSite::GetDateFormat("FULL"))), "!<ACTIVE_TO" => Date($DB->DateFormatToPHP(CSite::GetDateFormat("FULL"))), "<=PRICE_FROM" => $productPrice, ">=PRICE_TO" => $productPrice, "USER_GROUPS" => $USER->GetUserGroup($arOrder['USER_ID'])));
         if ($arDiscount = $dbDiscount->Fetch()) {
             if ($arDiscount["DISCOUNT_TYPE"] == "P") {
                 $discountProduct = \Bitrix\Sale\PriceMaths::roundPrecision($productPrice * $arDiscount["DISCOUNT_VALUE"] / 100);
                 $discount = \Bitrix\Sale\PriceMaths::roundPrecision($discountProduct * DoubleVal($arProduct["QUANTITY"]));
                 // Changed by Sigurd, 2007-08-16
                 $discountPrice = $productPrice - $discountProduct;
             } else {
                 $discountValue = CCurrencyRates::ConvertCurrency($arDiscount["DISCOUNT_VALUE"], $arDiscount["CURRENCY"], $baseSiteCurrency);
                 $discountValue = \Bitrix\Sale\PriceMaths::roundPrecision($discountValue);
                 $discountProduct = \Bitrix\Sale\PriceMaths::roundPrecision(1.0 * $discountValue / DoubleVal($arProduct["QUANTITY"]));
                 // Changed by Sigurd, 2007-08-16
                 $discount = \Bitrix\Sale\PriceMaths::roundPrecision($curDiscount * DoubleVal($arProduct["QUANTITY"]));
                 $discountPrice = $productPrice - $discountProduct;
             }
         }
         $bUseVat = false;
         $vatRate = 0;
         if (DoubleVal($arProduct["VAT_RATE"]) > 0) {
             $bUseVat = true;
             $vatRate = $arProduct["VAT_RATE"];
         }
         // Tax
         $arTaxExempt = array();
         $dbUserGroups = CUser::GetUserGroupEx($arOrder["USER_ID"]);
         while ($arUserGroups = $dbUserGroups->Fetch()) {
             $dbTaxExemptTmp = CSaleTax::GetExemptList(array("GROUP_ID" => $arUserGroups["GROUP_ID"]));
             while ($arTaxExemptTmp = $dbTaxExemptTmp->Fetch()) {
                 $arTaxExemptTmp["TAX_ID"] = IntVal($arTaxExemptTmp["TAX_ID"]);
                 if (!in_array($arTaxExemptTmp["TAX_ID"], $arTaxExempt)) {
                     $arTaxExempt[] = $arTaxExemptTmp["TAX_ID"];
                 }
             }
         }
         $taxPrice = 0;
         $taxVatPrice = 0;
         if (!$bUseVat) {
             $taxLocation = 0;
             $dbOrderPropValues = CSaleOrderPropsValue::GetList(array(), array("ORDER_ID" => $arRecur["ORDER_ID"], "PROP_IS_LOCATION4TAX" => "Y"), false, false, array("VALUE"));
             if ($arOrderPropValues = $dbOrderPropValues->Fetch()) {
                 $taxLocation = IntVal($arOrderPropValues["VALUE"]);
             }
             $arTaxList = array();
             $dbTaxRateTmp = CSaleTaxRate::GetList(array("APPLY_ORDER" => "ASC"), array("LID" => $arOrder["LID"], "PERSON_TYPE_ID" => $arOrder["PERSON_TYPE_ID"], "ACTIVE" => "Y", "LOCATION" => $taxLocation));
             while ($arTaxRateTmp = $dbTaxRateTmp->Fetch()) {
                 if (!in_array(IntVal($arTaxRateTmp["TAX_ID"]), $arTaxExempt)) {
                     $arTaxList[] = $arTaxRateTmp;
                 }
             }
         } else {
             $arTaxList[] = array("ID" => 0, "TAX_NAME" => GetMessage("SKGR_VAT"), "IS_PERCENT" => "Y", "VALUE" => $vatRate * 100, "VALUE_MONEY" => 0, "APPLY_ORDER" => 100, "IS_IN_PRICE" => "Y", "CODE" => "VAT");
         }
         $arTaxSums = array();
         if (!empty($arTaxList)) {
             if (!$bUseVat) {
                 $taxPriceTmp = CSaleOrderTax::CountTaxes($discountPrice * DoubleVal($arProduct["QUANTITY"]), $arTaxList, $baseSiteCurrency);
                 for ($di = 0, $intCount = count($arTaxList); $di < $intCount; $di++) {
                     $arTaxList[$di]["VALUE_MONEY"] += $arTaxList[$di]["TAX_VAL"];
                 }
                 for ($di = 0, $intCount = count($arTaxList); $di < $intCount; $di++) {
                     $arTaxSums[$arTaxList[$di]["TAX_ID"]]["VALUE"] = $arTaxList[$di]["VALUE_MONEY"];
                     $arTaxSums[$arTaxList[$di]["TAX_ID"]]["NAME"] = $arTaxList[$di]["NAME"];
                     if ($arTaxList[$di]["IS_IN_PRICE"] != "Y") {
                         $taxPrice += $arTaxList[$di]["VALUE_MONEY"];
                     }
                 }
             } else {
                 $arTaxList[0]["VALUE_MONEY"] = \Bitrix\Sale\PriceMaths::roundPrecision($discountPrice / ($vatRate + 1) * $vatRate * DoubleVal($arProduct["QUANTITY"]));
                 $taxVatPrice = $arTaxList[0]["VALUE_MONEY"];
             }
         }
         // Changed by Sigurd, 2007-08-16
         $totalOrderPrice = $discountPrice * DoubleVal($arProduct["QUANTITY"]) + $deliveryPrice + $taxPrice;
         $arProduct["WITHOUT_ORDER"] = $arProduct["WITHOUT_ORDER"] == "Y" ? "Y" : "N";
         if ($arProduct["WITHOUT_ORDER"] == "N") {
             $DB->StartTransaction();
             // Saving
             $arSaleUser = CSaleUser::GetList(array(), array("USER_ID" => $arOrder["USER_ID"]));
             if (!empty($arSaleUser)) {
                 $currentFUser = $arSaleUser["ID"];
             } else {
                 $currentFUser = CSaleUser::_Add(array("=DATE_INSERT" => $DB->GetNowFunction(), "=DATE_UPDATE" => $DB->GetNowFunction(), "USER_ID" => $arOrder["USER_ID"]));
             }
             $arFields = array("FUSER_ID" => $currentFUser, "PRODUCT_ID" => $arProduct["PRODUCT_ID"], "PRODUCT_NAME" => $arProduct["PRODUCT_NAME"], "PRODUCT_URL" => $arProduct["PRODUCT_URL"], "PRODUCT_PRICE_ID" => $arProduct["PRODUCT_PRICE_ID"], "PRICE" => $arProduct["PRICE"], "CURRENCY" => $arProduct["CURRENCY"], "WEIGHT" => $arProduct["WEIGHT"], "QUANTITY" => $arProduct["QUANTITY"], "LID" => $arOrder["LID"], "DELAY" => "N", "CAN_BUY" => "Y", "NAME" => $arProduct["NAME"], "CALLBACK_FUNC" => $arProduct["CALLBACK_FUNC"], "ORDER_CALLBACK_FUNC" => $arProduct["ORDER_CALLBACK_FUNC"], "CANCEL_CALLBACK_FUNC" => $arProduct["CANCEL_CALLBACK_FUNC"], "PAY_CALLBACK_FUNC" => $arProduct["PAY_CALLBACK_FUNC"], "PRODUCT_PROVIDER_CLASS" => $arProduct["PRODUCT_PROVIDER_CLASS"], "MODULE" => $arRecur["MODULE"], "NOTES" => $arProduct["CATALOG_GROUP_NAME"], "DETAIL_PAGE_URL" => $arProduct["DETAIL_PAGE_URL"], "VATE_RATE" => $arProduct["VATE_RATE"], "PRODUCT_XML_ID" => $arProduct["PRODUCT_XML_ID"], "TYPE" => $arProduct["TYPE"], "RENEWAL" => "Y");
             $basketID = CSaleBasket::Add($arFields);
             $basketID = IntVal($basketID);
             if ($basketID <= 0) {
                 $bSuccess = False;
             }
             if ($bSuccess) {
                 if (CModule::IncludeModule("statistic")) {
                     CStatistic::Set_Event("eStore", "add2basket", $arFields["PRODUCT_ID"]);
                 }
                 $arFields = array("LID" => $arOrder["LID"], "PERSON_TYPE_ID" => $arOrder["PERSON_TYPE_ID"], "PAYED" => "N", "CANCELED" => "N", "STATUS_ID" => "N", "PRICE_DELIVERY" => $deliveryPrice, "ALLOW_DELIVERY" => "N", "PRICE" => $totalOrderPrice, "CURRENCY" => $baseSiteCurrency, "DISCOUNT_VALUE" => $discount, "USER_ID" => $arOrder["USER_ID"], "PAY_SYSTEM_ID" => $arOrder["PAY_SYSTEM_ID"], "DELIVERY_ID" => $deliveryID, "USER_DESCRIPTION" => $arOrder["USER_DESCRIPTION"], "TAX_VALUE" => $bUseVat ? $taxVatPrice : $taxPrice, "STAT_GID" => $arOrder["STAT_GID"], "RECURRING_ID" => $arRecur["ID"]);
                 $newOrderID = CSaleOrder::Add($arFields);
                 $newOrderID = IntVal($newOrderID);
                 if ($newOrderID <= 0) {
                     $bSuccess = False;
                 }
             }
             if ($bSuccess) {
                 $arDiscounts = array();
                 $arDiscounts[$basketID] = $discountProduct;
                 CSaleBasket::OrderBasket($newOrderID, $currentFUser, $arOrder["LID"], $arDiscounts);
             }
             if ($bSuccess) {
                 for ($it = 0, $intCount = count($arTaxList); $it < $intCount; $it++) {
                     $arFields = array("ORDER_ID" => $newOrderID, "TAX_NAME" => $arTaxList[$it]["TAX_NAME"], "IS_PERCENT" => $arTaxList[$it]["IS_PERCENT"], "VALUE" => $arTaxList[$it]["IS_PERCENT"] == "Y" ? $arTaxList[$it]["VALUE"] : RoundEx(CCurrencyRates::ConvertCurrency($arTaxList[$it]["VALUE"], $arTaxList[$it]["CURRENCY"], $baseSiteCurrency), 2), "VALUE_MONEY" => $arTaxList[$it]["VALUE_MONEY"], "APPLY_ORDER" => $arTaxList[$it]["APPLY_ORDER"], "IS_IN_PRICE" => $arTaxList[$it]["IS_IN_PRICE"], "CODE" => $arTaxList[$it]["CODE"]);
                     CSaleOrderTax::Add($arFields);
                 }
                 $dbOrderPropValues = CSaleOrderPropsValue::GetList(array(), array("ORDER_ID" => $arRecur["ORDER_ID"]), false, false, array("ORDER_PROPS_ID", "NAME", "CODE", "VALUE", "PROP_IS_PAYER", "PROP_IS_EMAIL"));
                 while ($arOrderPropValues = $dbOrderPropValues->Fetch()) {
                     $arFields = array("ORDER_ID" => $newOrderID, "ORDER_PROPS_ID" => $arOrderPropValues["ORDER_PROPS_ID"], "NAME" => $arOrderPropValues["NAME"], "CODE" => $arOrderPropValues["CODE"], "VALUE" => $arOrderPropValues["VALUE"]);
                     CSaleOrderPropsValue::Add($arFields);
                     if ($arOrderPropValues["PROP_IS_PAYER"] == "Y") {
                         $payerName = $arOrderPropValues["VALUE"];
                     }
                     if ($arOrderPropValues["PROP_IS_EMAIL"] == "Y") {
                         $payerEMail = $arOrderPropValues["VALUE"];
                     }
                 }
             }
             if ($bSuccess) {
                 if (CModule::IncludeModule("statistic")) {
                     CStatistic::Set_Event("eStore", "order_create", $newOrderID);
                 }
             }
             if ($bSuccess) {
                 $strOrderList = "";
                 $dbBasketTmp = CSaleBasket::GetList(array("NAME" => "ASC"), array("ORDER_ID" => $newOrderID));
                 while ($arBasketTmp = $dbBasketTmp->Fetch()) {
                     $strOrderList .= $arBasketTmp["NAME"] . " - " . $arBasketTmp["QUANTITY"] . " " . GetMessage("SALE_QUANTITY_UNIT");
                     $strOrderList .= "\n";
                 }
                 if (strlen($payerName) <= 0 || strlen($payerEMail) <= 0) {
                     $dbUser = CUser::GetByID($arOrder["USER_ID"]);
                     if ($arUser = $dbUser->Fetch()) {
                         if (strlen($payerName) <= 0) {
                             $payerName = $arUser["NAME"] . (strlen($arUser["NAME"]) <= 0 || strlen($arUser["LAST_NAME"]) <= 0 ? "" : " ") . $arUser["LAST_NAME"];
                         }
                         if (strlen($payerEMail) <= 0) {
                             $payerEMail = $arUser["EMAIL"];
                         }
                     }
                 }
                 $arFields = array("ORDER_ID" => $newOrderID, "ORDER_DATE" => Date($DB->DateFormatToPHP(CLang::GetDateFormat("SHORT", $arOrder["LID"]))), "ORDER_USER" => $payerName, "PRICE" => SaleFormatCurrency($totalOrderPrice, $baseSiteCurrency), "BCC" => COption::GetOptionString("sale", "order_email", "order@" . $SERVER_NAME), "EMAIL" => $payerEMail, "ORDER_LIST" => $strOrderList, "SALE_EMAIL" => COption::GetOptionString("sale", "order_email", "order@" . $SERVER_NAME));
                 $eventName = "SALE_NEW_ORDER_RECURRING";
                 $bSend = true;
                 foreach (GetModuleEvents("sale", "OnOrderRecurringSendEmail", true) as $arEvent) {
                     if (ExecuteModuleEventEx($arEvent, array($newOrderID, &$eventName, &$arFields)) === false) {
                         $bSend = false;
                     }
                 }
                 if ($bSend) {
                     $event = new CEvent();
                     $event->Send($eventName, $arOrder["LID"], $arFields, "N");
                 }
             }
             if ($bSuccess) {
                 $DB->Commit();
             } else {
                 $DB->Rollback();
             }
         }
     } else {
         $totalOrderPrice = $arOrder["PRICE"];
         $baseSiteCurrency = $arOrder["CURRENCY"];
     }
     $res = False;
     if ($bSuccess) {
         $res = CSaleUserAccount::Pay($arOrder["USER_ID"], $totalOrderPrice, $baseSiteCurrency, $newOrderID, True);
         if ($res) {
             if ($arProduct["WITHOUT_ORDER"] == "N") {
                 CSaleOrder::PayOrder($newOrderID, "Y", False, False, $arRecur["ID"]);
                 CSaleOrder::DeliverOrder($newOrderID, "Y", $arRecur["ID"]);
                 CSaleOrder::DeductOrder($newOrderID, "Y", "", true, array(), $arRecur["ID"]);
             } else {
                 /** @var $productProvider IBXSaleProductProvider */
                 if ($productProvider = CSaleBasket::GetProductProvider($arProduct)) {
                     $r = $productProvider::DeliverProduct(array("PRODUCT_ID" => $arProduct["PRODUCT_ID"], "USER_ID" => $arOrder["USER_ID"], "PAID" => true, 'BASKET_ID' => $basketID));
                 } else {
                     $r = CSaleBasket::ExecuteCallbackFunction($arProduct["PAY_CALLBACK_FUNC"], $arRecur["MODULE"], $arProduct["PRODUCT_ID"], $arOrder["USER_ID"], true);
                 }
             }
             $arFields = array("ORDER_ID" => $newOrderID, "PRODUCT_NAME" => $arProduct["PRODUCT_NAME"], "PRODUCT_URL" => $arProduct["PRODUCT_URL"], "PRICE_TYPE" => $arProduct["PRICE_TYPE"], "RECUR_SCHEME_TYPE" => $arProduct["RECUR_SCHEME_TYPE"], "RECUR_SCHEME_LENGTH" => $arProduct["RECUR_SCHEME_LENGTH"], "WITHOUT_ORDER" => $arProduct["WITHOUT_ORDER"], "PRIOR_DATE" => Date($GLOBALS["DB"]->DateFormatToPHP(CLang::GetDateFormat("FULL", SITE_ID))), "NEXT_DATE" => $arProduct["NEXT_DATE"], "REMAINING_ATTEMPTS" => Defined("SALE_PROC_REC_ATTEMPTS") ? SALE_PROC_REC_ATTEMPTS : 3, "SUCCESS_PAYMENT" => "Y");
             CSaleRecurring::Update($arRecur["ID"], $arFields);
         } else {
             $arFields = array("ORDER_ID" => $newOrderID, "PRODUCT_NAME" => $arProduct["PRODUCT_NAME"], "PRODUCT_URL" => $arProduct["PRODUCT_URL"], "PRICE_TYPE" => $arProduct["PRICE_TYPE"], "RECUR_SCHEME_LENGTH" => $arProduct["RECUR_SCHEME_LENGTH"], "RECUR_SCHEME_TYPE" => $arProduct["RECUR_SCHEME_TYPE"], "WITHOUT_ORDER" => $arProduct["WITHOUT_ORDER"], "NEXT_DATE" => Date($GLOBALS["DB"]->DateFormatToPHP(CLang::GetDateFormat("FULL", SITE_ID)), time() + SALE_PROC_REC_TIME + CTimeZone::GetOffset()), "REMAINING_ATTEMPTS" => IntVal($arRecur["REMAINING_ATTEMPTS"]) - 1, "SUCCESS_PAYMENT" => "N");
             CSaleRecurring::Update($arRecur["ID"], $arFields);
             if (IntVal($arRecur["REMAINING_ATTEMPTS"]) - 1 <= 0) {
                 CSaleRecurring::CancelRecurring($arRecur["ID"], "Y", "Can't pay order");
                 /*
                 $arFields["CANCELED"] = "Y";
                 $arFields["DATE_CANCELED"] = Date(CDatabase::DateFormatToPHP(CLang::GetDateFormat("FULL", LANG)));
                 $arFields["CANCELED_REASON"] = "Can't pay order";
                 */
             }
         }
     }
     return $res;
 }
示例#5
0
 /**
  * @internal
  * @param $arOrder
  * @param $arOptions
  * @param $arErrors
  */
 public static function calculateDeliveryTax(&$arOrder, $arOptions, &$arErrors)
 {
     if ((!array_key_exists("TAX_LOCATION", $arOrder) || strval(trim($arOrder["TAX_LOCATION"])) == "") && (!$arOrder["USE_VAT"] || $arOrder["USE_VAT"] != "Y")) {
         return;
     }
     if (!array_key_exists("COUNT_DELIVERY_TAX", $arOptions)) {
         $arOptions["COUNT_DELIVERY_TAX"] = COption::GetOptionString("sale", "COUNT_DELIVERY_TAX", "N");
     }
     if (doubleval($arOrder["DELIVERY_PRICE"]) <= 0 || $arOptions["COUNT_DELIVERY_TAX"] != "Y") {
         return;
     }
     if (!$arOrder["USE_VAT"] || $arOrder["USE_VAT"] != "Y") {
         if (!array_key_exists("TAX_EXEMPT", $arOrder)) {
             $arUserGroups = CUser::GetUserGroup($arOrder["USER_ID"]);
             $dbTaxExemptList = CSaleTax::GetExemptList(array("GROUP_ID" => $arUserGroups));
             while ($TaxExemptList = $dbTaxExemptList->Fetch()) {
                 if (!in_array(intval($TaxExemptList["TAX_ID"]), $arOrder["TAX_EXEMPT"])) {
                     $arOrder["TAX_EXEMPT"][] = intval($TaxExemptList["TAX_ID"]);
                 }
             }
         }
         if (!array_key_exists("TAX_LIST", $arOrder)) {
             $arOrder["TAX_LIST"] = array();
             $dbTaxRate = CSaleTaxRate::GetList(array("APPLY_ORDER" => "ASC"), array("LID" => $arOrder["SITE_ID"], "PERSON_TYPE_ID" => $arOrder["PERSON_TYPE_ID"], "ACTIVE" => "Y", "LOCATION_CODE" => $arOrder["TAX_LOCATION"]));
             while ($arTaxRate = $dbTaxRate->GetNext()) {
                 if (is_array($arOrder["TAX_EXEMPT"]) && !in_array(intval($arTaxRate["TAX_ID"]), $arOrder["TAX_EXEMPT"])) {
                     if ($arTaxRate["IS_PERCENT"] != "Y") {
                         $arTaxRate["VALUE"] = \Bitrix\Sale\PriceMaths::roundPrecision(CurrencyRates::ConvertCurrency($arTaxRate["VALUE"], $arTaxRate["CURRENCY"], $arOrder["CURRENCY"]));
                         $arTaxRate["CURRENCY"] = $arOrder["CURRENCY"];
                     }
                     $arOrder["TAX_LIST"][] = $arTaxRate;
                 }
             }
         }
         if (count($arOrder["TAX_LIST"]) > 0) {
             CSaleOrderTax::CountTaxes($arOrder["DELIVERY_PRICE"], $arOrder["TAX_LIST"], $arOrder["CURRENCY"]);
             $arOrder["TAX_PRICE"] = 0;
             foreach ($arOrder["TAX_LIST"] as &$arTax) {
                 $arTax["VALUE_MONEY"] += \Bitrix\Sale\PriceMaths::roundPrecision($arTax["TAX_VAL"]);
                 $arTax['VALUE_MONEY_FORMATED'] = SaleFormatCurrency($arTax["VALUE_MONEY"], $arOrder["CURRENCY"]);
                 if ($arTax["IS_IN_PRICE"] != "Y" || !empty($arOptions['ENABLE_INCLUSIVE_TAX']) && $arOptions['ENABLE_INCLUSIVE_TAX'] == "Y") {
                     $arOrder["TAX_PRICE"] += $arTax["VALUE_MONEY"];
                 }
             }
             unset($arTax);
         }
     } else {
         $deliveryVat = $arOrder["DELIVERY_PRICE"] * $arOrder["VAT_RATE"] / (1 + $arOrder["VAT_RATE"]);
         $arOrder["VAT_SUM"] += $deliveryVat;
         $arOrder["VAT_DELIVERY"] += $deliveryVat;
         //if (!array_key_exists("TAX_LIST", $arOrder))
         //{
         $arOrder["TAX_LIST"][0] = array("NAME" => GetMessage("SOA_VAT"), "IS_PERCENT" => "Y", "VALUE" => $arOrder["VAT_RATE"] * 100, "VALUE_FORMATED" => "(" . $arOrder["VAT_RATE"] * 100 . "%, " . GetMessage("SOA_VAT_INCLUDED") . ")", "VALUE_MONEY" => $arOrder["VAT_SUM"], "VALUE_MONEY_FORMATED" => SaleFormatCurrency($arOrder["VAT_SUM"], $arOrder["CURRENCY"]), "APPLY_ORDER" => 100, "IS_IN_PRICE" => "Y", "CODE" => "VAT");
         //}
     }
     $arOrder["TAX_PRICE"] = \Bitrix\Sale\PriceMaths::roundPrecision($arOrder["TAX_PRICE"]);
     $arOrder["VAT_SUM"] = \Bitrix\Sale\PriceMaths::roundPrecision($arOrder["VAT_SUM"]);
     $arOrder["VAT_DELIVERY"] = \Bitrix\Sale\PriceMaths::roundPrecision($arOrder["VAT_DELIVERY"]);
 }
示例#6
0
 public static function DoGetUserShoppingCart($siteId, $userId, $shoppingCart, &$arErrors, $arCoupons = array(), $orderId = 0, $enableCustomCurrency = false)
 {
     $isOrderConverted = \Bitrix\Main\Config\Option::get("main", "~sale_converted_15", 'N');
     $siteId = trim($siteId);
     if (empty($siteId)) {
         $arErrors[] = array("CODE" => "PARAM", "TEXT" => Loc::getMessage('SKGB_PARAM_SITE_ERROR'));
         return null;
     }
     $userId = intval($userId);
     if (!is_array($shoppingCart)) {
         if (intval($shoppingCart) . "|" != $shoppingCart . "|") {
             $arErrors[] = array("CODE" => "PARAM", "TEXT" => Loc::getMessage('SKGB_PARAM_SK_ERROR'));
             return null;
         }
         $shoppingCart = intval($shoppingCart);
         $dbShoppingCartItems = CSaleBasket::GetList(array("NAME" => "ASC"), array("FUSER_ID" => $shoppingCart, "LID" => $siteId, "ORDER_ID" => "NULL", "DELAY" => "N"), false, false, array("ID", "LID", "CALLBACK_FUNC", "MODULE", "PRODUCT_ID", "QUANTITY", "DELAY", "CAN_BUY", "PRICE", "WEIGHT", "NAME", "CURRENCY", "CATALOG_XML_ID", "VAT_RATE", "NOTES", "DISCOUNT_PRICE", "DETAIL_PAGE_URL", "PRODUCT_PROVIDER_CLASS", "RESERVED", "DEDUCTED", "RESERVE_QUANTITY", "DIMENSIONS", "TYPE", "SET_PARENT_ID"));
         $arTmp = array();
         while ($arShoppingCartItem = $dbShoppingCartItems->Fetch()) {
             $arTmp[] = $arShoppingCartItem;
         }
         $shoppingCart = $arTmp;
     }
     $arOldShoppingCart = array();
     if ($orderId != 0) {
         $dbs = CSaleBasket::GetList(array("NAME" => "ASC"), array("LID" => $siteId, "ORDER_ID" => $orderId, "DELAY" => "N"), false, false, array("ID", "LID", "CALLBACK_FUNC", "MODULE", "PRODUCT_ID", "PRODUCT_PRICE_ID", "PRICE", "QUANTITY", "DELAY", "CAN_BUY", "PRICE", "WEIGHT", "NAME", "CURRENCY", "CATALOG_XML_ID", "VAT_RATE", "NOTES", "DISCOUNT_PRICE", "DETAIL_PAGE_URL", "PRODUCT_PROVIDER_CLASS", "RESERVED", "DEDUCTED", "BARCODE_MULTI", "DIMENSIONS", "TYPE", "SET_PARENT_ID"));
         while ($arOldShoppingCartItem = $dbs->Fetch()) {
             $arOldShoppingCart[$arOldShoppingCartItem["ID"]] = $arOldShoppingCartItem;
         }
     }
     if (CSaleHelper::IsAssociativeArray($shoppingCart)) {
         $shoppingCart = array($shoppingCart);
     }
     if (!empty($arCoupons)) {
         if (!is_array($arCoupons)) {
             $arCoupons = array($arCoupons);
         }
         foreach (GetModuleEvents("sale", "OnSetCouponList", true) as $arEvent) {
             ExecuteModuleEventEx($arEvent, array($userId, $arCoupons, array()));
         }
         foreach ($arCoupons as &$coupon) {
             $couponResult = DiscountCouponsManager::add($coupon);
         }
         unset($coupon, $couponResult);
     }
     if (!is_bool($enableCustomCurrency)) {
         $enableCustomCurrency = false;
     }
     $arResult = array();
     $emptyID = 1;
     foreach ($shoppingCart as $itemIndex => $arShoppingCartItem) {
         if (array_key_exists("CALLBACK_FUNC", $arShoppingCartItem) && !empty($arShoppingCartItem["CALLBACK_FUNC"]) || array_key_exists("PRODUCT_PROVIDER_CLASS", $arShoppingCartItem) && !empty($arShoppingCartItem["PRODUCT_PROVIDER_CLASS"])) {
             // get quantity difference to check its availability
             if ($orderId != 0) {
                 $quantity = $arShoppingCartItem["QUANTITY"] - $arOldShoppingCart[$arShoppingCartItem["ID_TMP"]]["QUANTITY"];
             } else {
                 $quantity = $arShoppingCartItem["QUANTITY"];
             }
             $customPrice = isset($arShoppingCartItem['CUSTOM_PRICE']) && $arShoppingCartItem['CUSTOM_PRICE'] == 'Y';
             $existBasketID = isset($arShoppingCartItem['ID']) && (int) $arShoppingCartItem['ID'] > 0;
             /** @var $productProvider IBXSaleProductProvider */
             if ($productProvider = CSaleBasket::GetProductProvider($arShoppingCartItem)) {
                 if ($existBasketID) {
                     $basketID = $arShoppingCartItem['ID'];
                 } elseif (isset($arShoppingCartItem["ID_TMP"])) {
                     $basketID = $arShoppingCartItem["ID_TMP"];
                 } else {
                     $basketID = 'tmp_' . $emptyID;
                     $emptyID++;
                 }
                 $providerParams = array("PRODUCT_ID" => $arShoppingCartItem["PRODUCT_ID"], "QUANTITY" => $quantity > 0 ? $quantity : $arShoppingCartItem["QUANTITY"], "RENEWAL" => "N", "USER_ID" => $userId, "SITE_ID" => $siteId, "BASKET_ID" => $basketID, "CHECK_QUANTITY" => $quantity > 0 ? "Y" : "N", "CHECK_COUPONS" => 'Y' == $arShoppingCartItem['CAN_BUY'] && (!array_key_exists('DELAY', $arShoppingCartItem) || 'Y' != $arShoppingCartItem['DELAY']) ? 'Y' : 'N', "CHECK_PRICE" => $customPrice ? "N" : "Y");
                 if (isset($arShoppingCartItem['NOTES'])) {
                     $providerParams['NOTES'] = $arShoppingCartItem['NOTES'];
                 }
                 $arFieldsTmp = $productProvider::GetProductData($providerParams);
                 unset($providerParams);
             } else {
                 $arFieldsTmp = CSaleBasket::ExecuteCallbackFunction($arShoppingCartItem["CALLBACK_FUNC"], $arShoppingCartItem["MODULE"], $arShoppingCartItem["PRODUCT_ID"], $quantity, "N", $userId, $siteId);
                 if (!empty($arFieldsTmp) && is_array($arFieldsTmp)) {
                     if ($customPrice) {
                         unset($arFieldsTmp['PRICE'], $arFieldsTmp['CURRENCY']);
                     }
                 }
             }
             if (!empty($arFieldsTmp) && is_array($arFieldsTmp)) {
                 $arFieldsTmp["CAN_BUY"] = "Y";
                 $arFieldsTmp["SUBSCRIBE"] = "N";
                 $arFieldsTmp['TYPE'] = (int) $arShoppingCartItem['TYPE'];
                 $arFieldsTmp['SET_PARENT_ID'] = $arShoppingCartItem['SET_PARENT_ID'];
                 $arFieldsTmp['LID'] = $siteId;
             } else {
                 $arFieldsTmp = array("CAN_BUY" => "N");
             }
             if ($isOrderConverted == 'Y') {
                 if (!Sale\Compatible\DiscountCompatibility::isInited()) {
                     Sale\Compatible\DiscountCompatibility::init();
                 }
                 $basketCode = Sale\Compatible\DiscountCompatibility::usedByClient() ? $arShoppingCartItem['ID'] : $itemIndex;
                 Sale\Compatible\DiscountCompatibility::setBasketItemData($basketCode, $arFieldsTmp);
             }
             if ($existBasketID) {
                 $arFieldsTmp["IGNORE_CALLBACK_FUNC"] = "Y";
                 CSaleBasket::Update($arShoppingCartItem["ID"], $arFieldsTmp);
                 $dbTmp = CSaleBasket::GetList(array(), array("ID" => $arShoppingCartItem["ID"]), false, false, array("ID", "CALLBACK_FUNC", "MODULE", "PRODUCT_ID", "QUANTITY", "DELAY", "CAN_BUY", "PRICE", "TYPE", "SET_PARENT_ID", "WEIGHT", "NAME", "CURRENCY", "CATALOG_XML_ID", "VAT_RATE", "NOTES", "DISCOUNT_PRICE", "DETAIL_PAGE_URL", "PRODUCT_PROVIDER_CLASS", "DIMENSIONS"));
                 $arTmp = $dbTmp->Fetch();
                 foreach ($arTmp as $key => $val) {
                     $arShoppingCartItem[$key] = $val;
                 }
             } else {
                 foreach ($arFieldsTmp as $key => $val) {
                     // update returned quantity for the product if quantity difference is available
                     if ($orderId != 0 && $key == "QUANTITY" && $arOldShoppingCart[$arShoppingCartItem["ID_TMP"]]["RESERVED"] == "Y" && $quantity > 0) {
                         $arShoppingCartItem[$key] = $val + $arOldShoppingCart[$arShoppingCartItem["ID_TMP"]]["QUANTITY"];
                     } else {
                         $arShoppingCartItem[$key] = $val;
                     }
                 }
             }
         }
         if ($arShoppingCartItem["CAN_BUY"] == "Y") {
             if (!$enableCustomCurrency) {
                 $baseLangCurrency = CSaleLang::GetLangCurrency($siteId);
                 if ($baseLangCurrency != $arShoppingCartItem["CURRENCY"]) {
                     $arShoppingCartItem["PRICE"] = CCurrencyRates::ConvertCurrency($arShoppingCartItem["PRICE"], $arShoppingCartItem["CURRENCY"], $baseLangCurrency);
                     if (is_set($arShoppingCartItem, "DISCOUNT_PRICE")) {
                         $arShoppingCartItem["DISCOUNT_PRICE"] = CCurrencyRates::ConvertCurrency($arShoppingCartItem["DISCOUNT_PRICE"], $arShoppingCartItem["CURRENCY"], $baseLangCurrency);
                     }
                     $arShoppingCartItem["CURRENCY"] = $baseLangCurrency;
                 }
             }
             $arShoppingCartItem["PRICE"] = \Bitrix\Sale\PriceMaths::roundPrecision($arShoppingCartItem["PRICE"]);
             $arShoppingCartItem["QUANTITY"] = floatval($arShoppingCartItem["QUANTITY"]);
             $arShoppingCartItem["WEIGHT"] = floatval($arShoppingCartItem["WEIGHT"]);
             $arShoppingCartItem["DIMENSIONS"] = unserialize($arShoppingCartItem["DIMENSIONS"]);
             $arShoppingCartItem["VAT_RATE"] = floatval($arShoppingCartItem["VAT_RATE"]);
             $arShoppingCartItem["DISCOUNT_PRICE"] = roundEx($arShoppingCartItem["DISCOUNT_PRICE"], SALE_VALUE_PRECISION);
             if ($arShoppingCartItem["VAT_RATE"] > 0) {
                 $arShoppingCartItem["VAT_VALUE"] = \Bitrix\Sale\PriceMaths::roundPrecision($arShoppingCartItem["PRICE"] / ($arShoppingCartItem["VAT_RATE"] + 1) * $arShoppingCartItem["VAT_RATE"]);
             }
             //$arShoppingCartItem["VAT_VALUE"] = roundEx((($arShoppingCartItem["PRICE"] / ($arShoppingCartItem["VAT_RATE"] + 1)) * $arShoppingCartItem["VAT_RATE"]), SALE_VALUE_PRECISION);
             if ($arShoppingCartItem["DISCOUNT_PRICE"] > 0) {
                 $arShoppingCartItem["DISCOUNT_PRICE_PERCENT"] = $arShoppingCartItem["DISCOUNT_PRICE"] * 100 / ($arShoppingCartItem["DISCOUNT_PRICE"] + $arShoppingCartItem["PRICE"]);
             }
             $arResult[$itemIndex] = $arShoppingCartItem;
         }
     }
     if (isset($arShoppingCartItem)) {
         unset($arShoppingCartItem);
     }
     if (!empty($arCoupons) && is_array($arCoupons)) {
         foreach (GetModuleEvents("sale", "OnClearCouponList", true) as $arEvent) {
             ExecuteModuleEventEx($arEvent, array($userId, $arCoupons, array()));
         }
     }
     return $arResult;
 }
示例#7
0
 public function getBasketItems()
 {
     global $APPLICATION;
     if (self::$catalogIncluded === null) {
         self::$catalogIncluded = Loader::includeModule('catalog');
     }
     self::$iblockIncluded = self::$catalogIncluded;
     CSaleBasket::UpdateBasketPrices(CSaleBasket::GetBasketUserID(), SITE_ID);
     $bShowReady = false;
     $bShowDelay = false;
     $bShowSubscribe = false;
     $bShowNotAvail = false;
     $allSum = 0;
     $allWeight = 0;
     $allCurrency = CSaleLang::GetLangCurrency(SITE_ID);
     $allVATSum = 0;
     $arParents = array();
     $arResult["ITEMS"]["AnDelCanBuy"] = array();
     $arResult["ITEMS"]["DelDelCanBuy"] = array();
     $arResult["ITEMS"]["nAnCanBuy"] = array();
     $arResult["ITEMS"]["ProdSubscribe"] = array();
     $DISCOUNT_PRICE_ALL = 0;
     // BASKET PRODUCTS (including measures, ratio, iblock properties data)
     $arImgFields = array("PREVIEW_PICTURE", "DETAIL_PICTURE");
     $arBasketItems = array();
     $basketKey = 0;
     $basketIds = array();
     $arSku2Parent = array();
     $arSetParentWeight = array();
     $arElementId = array();
     $dbItems = CSaleBasket::GetList(array("ID" => "ASC"), array("FUSER_ID" => CSaleBasket::GetBasketUserID(), "LID" => SITE_ID, "ORDER_ID" => "NULL"), false, false, array("ID", "NAME", "CALLBACK_FUNC", "MODULE", "PRODUCT_ID", "QUANTITY", "DELAY", "CAN_BUY", "PRICE", "WEIGHT", "DETAIL_PAGE_URL", "NOTES", "CURRENCY", "VAT_RATE", "CATALOG_XML_ID", "PRODUCT_XML_ID", "SUBSCRIBE", "DISCOUNT_PRICE", "PRODUCT_PROVIDER_CLASS", "TYPE", "SET_PARENT_ID"));
     while ($arItem = $dbItems->GetNext()) {
         $arItem['PROPS'] = array();
         $arBasketItems[$basketKey] = $arItem;
         $basketIds[$arItem['ID']] =& $arBasketItems[$basketKey];
         $basketKey++;
         if (CSaleBasketHelper::isSetItem($arItem)) {
             continue;
         }
         $arElementId[] = $arItem["PRODUCT_ID"];
     }
     if (!empty($arElementId) && self::$catalogIncluded) {
         $productList = CCatalogSKU::getProductList($arElementId);
         if (!empty($productList)) {
             foreach ($productList as $offerId => $offerInfo) {
                 $offerInfo['PRODUCT_ID'] = $offerInfo['ID'];
                 $arElementId[] = $offerInfo['ID'];
                 $arSku2Parent[$offerId] = $offerInfo['ID'];
                 $arParents[$offerId] = $offerInfo;
             }
             unset($offerInfo, $offerId);
         }
         unset($productList);
         // get measures, ratio, sku props data and available quantity
         $arBasketItems = getMeasures($arBasketItems);
         $arBasketItems = getRatio($arBasketItems);
         $arBasketItems = $this->getAvailableQuantity($arBasketItems);
         $propsIterator = CSaleBasket::GetPropsList(array('BASKET_ID' => 'ASC', 'SORT' => 'ASC', 'ID' => 'ASC'), array('BASKET_ID' => array_keys($basketIds)));
         while ($property = $propsIterator->GetNext()) {
             $property['CODE'] = (string) $property['CODE'];
             if ($property['CODE'] == 'CATALOG.XML_ID' || $property['CODE'] == 'PRODUCT.XML_ID') {
                 continue;
             }
             if (!isset($basketIds[$property['BASKET_ID']])) {
                 continue;
             }
             $basketIds[$property['BASKET_ID']]['PROPS'][] = $property;
         }
         unset($property, $propsIterator, $basketIds);
     }
     // get product properties data
     $arProductData = getProductProps($arElementId, array_merge(array("ID"), $arImgFields, $this->arCustomSelectFields));
     foreach ($arBasketItems as &$arItem) {
         $quantityIsFloat = false;
         if (number_format(doubleval($arItem['QUANTITY']), 2, '.', '') != intval($arItem['QUANTITY'])) {
             $quantityIsFloat = true;
         }
         $arItem["QUANTITY"] = $quantityIsFloat === false && $this->quantityFloat != "Y" ? intval($arItem['QUANTITY']) : number_format(doubleval($arItem['QUANTITY']), 4, '.', '') * 1;
         $arItem["PRICE_VAT_VALUE"] = $arItem["PRICE"] * $arItem["QUANTITY"] / ($arItem["VAT_RATE"] + 1) * $arItem["VAT_RATE"] / $arItem["QUANTITY"];
         //$arItem["PRICE_VAT_VALUE"] = (($arItem["PRICE"] / ($arItem["VAT_RATE"] +1)) * $arItem["VAT_RATE"]);
         $arItem["PRICE_FORMATED"] = CCurrencyLang::CurrencyFormat($arItem["PRICE"], $arItem["CURRENCY"], true);
         $arItem["WEIGHT"] = doubleval($arItem["WEIGHT"]);
         $arItem["WEIGHT_FORMATED"] = roundEx(doubleval($arItem["WEIGHT"] / $this->weightKoef), SALE_WEIGHT_PRECISION) . " " . $this->weightUnit;
         if (CSaleBasketHelper::isSetItem($arItem)) {
             $arSetParentWeight[$arItem["SET_PARENT_ID"]] += $arItem["WEIGHT"] * $arItem["QUANTITY"];
         }
         if (isset($arProductData[$arItem['PRODUCT_ID']]) && is_array($arProductData[$arItem['PRODUCT_ID']])) {
             foreach ($arProductData[$arItem["PRODUCT_ID"]] as $key => $value) {
                 if (strpos($key, "PROPERTY_") !== false || in_array($key, $arImgFields)) {
                     $arItem[$key] = $value;
                 }
             }
         }
         if (array_key_exists($arItem["PRODUCT_ID"], $arSku2Parent)) {
             $replaceImageFields = true;
             $arFieldsToFill = array_merge($this->arCustomSelectFields, $arImgFields);
             // fields to be filled with parents' values if empty
             $parentId = $arSku2Parent[$arItem["PRODUCT_ID"]];
             foreach ($arImgFields as $imageFieldName) {
                 if (!empty($arProductData[$arItem["PRODUCT_ID"]][$imageFieldName])) {
                     $replaceImageFields = false;
                     $arItem[$imageFieldName] = $arProductData[$arItem["PRODUCT_ID"]][$imageFieldName];
                 }
             }
             foreach ($arFieldsToFill as $field) {
                 $fieldVal = in_array($field, $arImgFields) ? $field : $field . "_VALUE";
                 if ((!isset($arItem[$fieldVal]) || isset($arItem[$fieldVal]) && strlen($arItem[$fieldVal]) == 0) && (isset($arProductData[$parentId][$fieldVal]) && !empty($arProductData[$parentId][$fieldVal]))) {
                     if (in_array($field, $arImgFields) && !$replaceImageFields) {
                         continue;
                     }
                     $arItem[$fieldVal] = $arProductData[$parentId][$fieldVal];
                 }
             }
         }
         foreach ($arItem as $key => $value) {
             if (strpos($key, "PROPERTY_", 0) === 0 && strrpos($key, "_VALUE") == strlen($key) - 6) {
                 $code = ToUpper(str_replace(array("PROPERTY_", "_VALUE"), "", $key));
                 $propData = $this->arIblockProps[$code];
                 $arItem[$key] = CSaleHelper::getIblockPropInfo($value, $propData);
             }
         }
         $arItem["PREVIEW_PICTURE_SRC"] = "";
         if (isset($arItem["PREVIEW_PICTURE"]) && intval($arItem["PREVIEW_PICTURE"]) > 0) {
             $arImage = CFile::GetFileArray($arItem["PREVIEW_PICTURE"]);
             if ($arImage) {
                 $arFileTmp = CFile::ResizeImageGet($arImage, array("width" => "110", "height" => "110"), BX_RESIZE_IMAGE_PROPORTIONAL, true);
                 $arItem["PREVIEW_PICTURE_SRC"] = $arFileTmp["src"];
             }
         }
         $arItem["DETAIL_PICTURE_SRC"] = "";
         if (isset($arItem["DETAIL_PICTURE"]) && intval($arItem["DETAIL_PICTURE"]) > 0) {
             $arImage = CFile::GetFileArray($arItem["DETAIL_PICTURE"]);
             if ($arImage) {
                 $arFileTmp = CFile::ResizeImageGet($arImage, array("width" => "110", "height" => "110"), BX_RESIZE_IMAGE_PROPORTIONAL, true);
                 $arItem["DETAIL_PICTURE_SRC"] = $arFileTmp["src"];
             }
         }
     }
     unset($arItem);
     // get sku props data
     if (!empty($arBasketItems) && self::$catalogIncluded && isset($this->offersProps) && !empty($this->offersProps)) {
         $arBasketItems = $this->getSkuPropsData($arBasketItems, $arParents, $this->offersProps);
     }
     // count weight for set parent products
     foreach ($arBasketItems as &$arItem) {
         if (CSaleBasketHelper::isSetParent($arItem)) {
             $arItem["WEIGHT"] = $arSetParentWeight[$arItem["ID"]] / $arItem["QUANTITY"];
             $arItem["WEIGHT_FORMATED"] = roundEx(doubleval($arItem["WEIGHT"] / $this->weightKoef), SALE_WEIGHT_PRECISION) . " " . $this->weightUnit;
         }
     }
     if (isset($arItem)) {
         unset($arItem);
     }
     // fill item arrays for old templates
     foreach ($arBasketItems as &$arItem) {
         if (CSaleBasketHelper::isSetItem($arItem)) {
             continue;
         }
         $arItem['DISCOUNT_PRICE'] = (double) $arItem['DISCOUNT_PRICE'];
         $arItem['PRICE'] = (double) $arItem['PRICE'];
         $arItem['DISCOUNT_PRICE_PERCENT'] = 0;
         $arItem['DISCOUNT_PRICE_PERCENT_FORMATED'] = '';
         $arItem['FULL_PRICE'] = $arItem['PRICE'];
         if ($arItem['DISCOUNT_PRICE'] > 0) {
             if ($arItem['DISCOUNT_PRICE'] + $arItem['PRICE'] > 0) {
                 $arItem['DISCOUNT_PRICE_PERCENT'] = $arItem['DISCOUNT_PRICE'] * 100 / ($arItem['DISCOUNT_PRICE'] + $arItem['PRICE']);
                 $arItem['DISCOUNT_PRICE_PERCENT_FORMATED'] = CSaleBasketHelper::formatQuantity($arItem['DISCOUNT_PRICE_PERCENT']) . '%';
                 $arItem['FULL_PRICE'] = $arItem["PRICE"] + $arItem["DISCOUNT_PRICE"];
             }
         }
         $arItem['FULL_PRICE_FORMATED'] = CCurrencyLang::CurrencyFormat($arItem['FULL_PRICE'], $arItem['CURRENCY'], true);
         if ($arItem["CAN_BUY"] == "Y" && $arItem["DELAY"] == "N") {
             $allSum += $arItem["PRICE"] * $arItem["QUANTITY"];
             $allWeight += $arItem["WEIGHT"] * $arItem["QUANTITY"];
             $allVATSum += roundEx($arItem["PRICE_VAT_VALUE"] * $arItem["QUANTITY"], SALE_VALUE_PRECISION);
             $bShowReady = true;
             if ($arItem["DISCOUNT_PRICE"] > 0) {
                 $DISCOUNT_PRICE_ALL += $arItem["DISCOUNT_PRICE"] * $arItem["QUANTITY"];
             }
             $arResult["ITEMS"]["AnDelCanBuy"][] = $arItem;
         } elseif ($arItem["CAN_BUY"] == "Y" && $arItem["DELAY"] == "Y") {
             $bShowDelay = true;
             $arItem["SUM"] = CCurrencyLang::CurrencyFormat($arItem["PRICE"] * $arItem["QUANTITY"], $arItem["CURRENCY"], true);
             $arResult["ITEMS"]["DelDelCanBuy"][] = $arItem;
         } elseif ($arItem["CAN_BUY"] == "N" && $arItem["SUBSCRIBE"] == "Y") {
             $bShowSubscribe = true;
             $arResult["ITEMS"]["ProdSubscribe"][] = $arItem;
         } else {
             $bShowNotAvail = true;
             $arItem["NOT_AVAILABLE"] = true;
             $arResult["ITEMS"]["nAnCanBuy"][] = $arItem;
         }
     }
     unset($arItem);
     $arResult["ShowReady"] = $bShowReady ? "Y" : "N";
     $arResult["ShowDelay"] = $bShowDelay ? "Y" : "N";
     $arResult["ShowNotAvail"] = $bShowNotAvail ? "Y" : "N";
     $arResult["ShowSubscribe"] = $bShowSubscribe ? "Y" : "N";
     $arOrder = array('SITE_ID' => SITE_ID, 'USER_ID' => $GLOBALS["USER"]->GetID(), 'ORDER_PRICE' => $allSum, 'ORDER_WEIGHT' => $allWeight, 'BASKET_ITEMS' => $arResult["ITEMS"]["AnDelCanBuy"]);
     $arOptions = array('COUNT_DISCOUNT_4_ALL_QUANTITY' => $this->countDiscount4AllQuantity);
     $arErrors = array();
     CSaleDiscount::DoProcessOrder($arOrder, $arOptions, $arErrors);
     if (isset($arOrder['ORDER_PRICE'])) {
         $roundOrderFields = CSaleOrder::getRoundFields();
         foreach ($arOrder as $fieldName => $fieldValue) {
             if (in_array($fieldName, $roundOrderFields)) {
                 $arOrder[$fieldName] = PriceMaths::roundPrecision($arOrder[$fieldName]);
             }
         }
     }
     if (!empty($arOrder['BASKET_ITEMS']) && is_array($arOrder['BASKET_ITEMS'])) {
         $arOrder['ORDER_PRICE'] = 0;
         $roundBasketFields = CSaleBasket::getRoundFields();
         foreach ($arOrder['BASKET_ITEMS'] as &$basketItem) {
             foreach ($basketItem as $fieldName => $fieldValue) {
                 if (in_array($fieldName, $roundBasketFields)) {
                     if (isset($basketItem[$fieldName])) {
                         $basketItem[$fieldName] = PriceMaths::roundPrecision($basketItem[$fieldName]);
                     }
                 }
             }
             $arOrder['ORDER_PRICE'] += $basketItem['PRICE'] * $basketItem['QUANTITY'];
         }
         $arOrder['ORDER_PRICE'] = PriceMaths::roundPrecision($arOrder['ORDER_PRICE']);
     }
     $allSum = 0;
     $allWeight = 0;
     $allVATSum = 0;
     $DISCOUNT_PRICE_ALL = 0;
     $priceWithoutDiscount = 0;
     foreach ($arOrder["BASKET_ITEMS"] as &$arOneItem) {
         $allWeight += $arOneItem["WEIGHT"] * $arOneItem["QUANTITY"];
         $allSum += $arOneItem["PRICE"] * $arOneItem["QUANTITY"];
         if (array_key_exists('VAT_VALUE', $arOneItem)) {
             $arOneItem["PRICE_VAT_VALUE"] = $arOneItem["VAT_VALUE"];
         }
         $allVATSum += roundEx($arOneItem["PRICE_VAT_VALUE"] * $arOneItem["QUANTITY"], SALE_VALUE_PRECISION);
         $arOneItem["PRICE_FORMATED"] = CCurrencyLang::CurrencyFormat($arOneItem["PRICE"], $arOneItem["CURRENCY"], true);
         $arOneItem["FULL_PRICE"] = PriceMaths::roundByFormatCurrency($arOneItem["PRICE"] + $arOneItem["DISCOUNT_PRICE"], $arOneItem["CURRENCY"]);
         $arOneItem["FULL_PRICE_FORMATED"] = CCurrencyLang::CurrencyFormat($arOneItem["FULL_PRICE"], $arOneItem["CURRENCY"], true);
         $arOneItem["SUM"] = CCurrencyLang::CurrencyFormat($arOneItem["PRICE"] * $arOneItem["QUANTITY"], $arOneItem["CURRENCY"], true);
         if (0 < doubleval($arOneItem["DISCOUNT_PRICE"] + $arOneItem["PRICE"])) {
             $arOneItem["DISCOUNT_PRICE_PERCENT"] = PriceMaths::roundByFormatCurrency($arOneItem["DISCOUNT_PRICE"] * 100 / ($arOneItem["DISCOUNT_PRICE"] + $arOneItem["PRICE"]), $arOneItem["CURRENCY"]);
         } else {
             $arOneItem["DISCOUNT_PRICE_PERCENT"] = 0;
         }
         $arOneItem["DISCOUNT_PRICE_PERCENT_FORMATED"] = CSaleBasketHelper::formatQuantity($arOneItem["DISCOUNT_PRICE_PERCENT"]) . "%";
         $DISCOUNT_PRICE_ALL += $arOneItem["DISCOUNT_PRICE"] * $arOneItem["QUANTITY"];
     }
     unset($arOneItem);
     $arResult["ITEMS"]["AnDelCanBuy"] = $arOrder["BASKET_ITEMS"];
     // fill grid data (for new templates with custom columns)
     foreach ($arResult["ITEMS"] as $type => $arItems) {
         foreach ($arItems as $k => $arItem) {
             $arResult["GRID"]["ROWS"][$arItem["ID"]] = $arItem;
         }
     }
     $arResult["allSum"] = PriceMaths::roundByFormatCurrency($allSum, $allCurrency);
     $arResult["allWeight"] = $allWeight;
     $arResult["allWeight_FORMATED"] = roundEx(doubleval($allWeight / $this->weightKoef), SALE_WEIGHT_PRECISION) . " " . $this->weightUnit;
     $arResult["allSum_FORMATED"] = CCurrencyLang::CurrencyFormat($allSum, $allCurrency, true);
     $arResult["DISCOUNT_PRICE_FORMATED"] = CCurrencyLang::CurrencyFormat($arResult["DISCOUNT_PRICE"], $allCurrency, true);
     $arResult["PRICE_WITHOUT_DISCOUNT"] = CCurrencyLang::CurrencyFormat($allSum + $DISCOUNT_PRICE_ALL, $allCurrency, true);
     if ($this->priceVatShowValue == 'Y') {
         $arResult["allVATSum"] = PriceMaths::roundByFormatCurrency($allVATSum, $allCurrency);
         $arResult["allVATSum_FORMATED"] = CCurrencyLang::CurrencyFormat($allVATSum, $allCurrency, true);
         $arResult["allSum_wVAT_FORMATED"] = CCurrencyLang::CurrencyFormat(doubleval($arResult["allSum"] - $allVATSum), $allCurrency, true);
     }
     $arResult['COUPON_LIST'] = array();
     $arResult['COUPON'] = '';
     if ($this->hideCoupon != "Y") {
         $arCoupons = DiscountCouponsManager::get(true, array(), true, true);
         if (!empty($arCoupons)) {
             foreach ($arCoupons as &$oneCoupon) {
                 if ($arResult['COUPON'] == '') {
                     $arResult['COUPON'] = $oneCoupon['COUPON'];
                 }
                 if ($oneCoupon['STATUS'] == DiscountCouponsManager::STATUS_NOT_FOUND || $oneCoupon['STATUS'] == DiscountCouponsManager::STATUS_FREEZE) {
                     $oneCoupon['JS_STATUS'] = 'BAD';
                 } elseif ($oneCoupon['STATUS'] == DiscountCouponsManager::STATUS_NOT_APPLYED || $oneCoupon['STATUS'] == DiscountCouponsManager::STATUS_ENTERED) {
                     $oneCoupon['JS_STATUS'] = 'ENTERED';
                 } else {
                     $oneCoupon['JS_STATUS'] = 'APPLYED';
                 }
                 $oneCoupon['JS_CHECK_CODE'] = '';
                 if (isset($oneCoupon['CHECK_CODE_TEXT'])) {
                     $oneCoupon['JS_CHECK_CODE'] = is_array($oneCoupon['CHECK_CODE_TEXT']) ? implode('<br>', $oneCoupon['CHECK_CODE_TEXT']) : $oneCoupon['CHECK_CODE_TEXT'];
                 }
                 $arResult['COUPON_LIST'][] = $oneCoupon;
             }
             unset($oneCoupon);
             $arResult['COUPON_LIST'] = array_values($arCoupons);
         }
         unset($arCoupons);
     }
     if (empty($arBasketItems)) {
         $arResult["ERROR_MESSAGE"] = Loc::getMessage("SALE_EMPTY_BASKET");
     }
     $arResult["DISCOUNT_PRICE_ALL"] = $DISCOUNT_PRICE_ALL;
     $arResult["APPLIED_DISCOUNT_LIST"] = $arOrder['DISCOUNT_LIST'];
     $arResult["FULL_DISCOUNT_LIST"] = $arOrder['FULL_DISCOUNT_LIST'];
     $arResult["DISCOUNT_PRICE_ALL_FORMATED"] = CCurrencyLang::CurrencyFormat($DISCOUNT_PRICE_ALL, $allCurrency, true);
     if ($this->usePrepayment == "Y") {
         if (doubleval($arResult["allSum"]) > 0) {
             $personType = array();
             $dbPersonType = CSalePersonType::GetList(array("SORT" => "ASC", "NAME" => "ASC"), array("LID" => SITE_ID, "ACTIVE" => "Y"));
             while ($arPersonType = $dbPersonType->Fetch()) {
                 $personType[] = $arPersonType["ID"];
             }
             if (!empty($personType)) {
                 $dbPaySysAction = CSalePaySystemAction::GetList(array(), array("PS_ACTIVE" => "Y", "HAVE_PREPAY" => "Y", "PERSON_TYPE_ID" => $personType), false, false, array("ID", "PAY_SYSTEM_ID", "PERSON_TYPE_ID", "NAME", "ACTION_FILE", "RESULT_FILE", "NEW_WINDOW", "PARAMS", "ENCODING", "LOGOTIP"));
                 if ($arPaySysAction = $dbPaySysAction->Fetch()) {
                     CSalePaySystemAction::InitParamarrays(false, false, $arPaySysAction["PARAMS"]);
                     $pathToAction = $_SERVER["DOCUMENT_ROOT"] . $arPaySysAction["ACTION_FILE"];
                     $pathToAction = str_replace("\\", "/", $pathToAction);
                     while (substr($pathToAction, strlen($pathToAction) - 1, 1) == "/") {
                         $pathToAction = substr($pathToAction, 0, strlen($pathToAction) - 1);
                     }
                     if (file_exists($pathToAction)) {
                         if (is_dir($pathToAction) && file_exists($pathToAction . "/pre_payment.php")) {
                             $pathToAction .= "/pre_payment.php";
                         }
                         try {
                             include_once $pathToAction;
                         } catch (\Bitrix\Main\SystemException $e) {
                             if ($e->getCode() == CSalePaySystemAction::GET_PARAM_VALUE) {
                                 $message = Loc::getMessage("SOA_TEMPL_ORDER_PS_ERROR");
                             } else {
                                 $message = $e->getMessage();
                             }
                             $arResult["ERROR_MESSAGE"] = $message;
                         }
                         $psPreAction = new CSalePaySystemPrePayment();
                         if ($psPreAction->init()) {
                             $orderData = array("PATH_TO_ORDER" => $this->pathToOrder, "AMOUNT" => $arResult["allSum"], "BASKET_ITEMS" => $arResult["ITEMS"]["AnDelCanBuy"]);
                             if (!$psPreAction->BasketButtonAction($orderData)) {
                                 if ($e = $APPLICATION->GetException()) {
                                     $arResult["WARNING_MESSAGE"][] = $e->GetString();
                                 }
                             }
                             $arResult["PREPAY_BUTTON"] = $psPreAction->BasketButtonShow();
                         }
                     }
                 }
             }
         }
     }
     return $arResult;
 }
 /**
  * @param array $basketItemData
  *
  * @return float|int
  */
 public static function getVat(array $basketItemData)
 {
     if (empty($basketItemData['VAT_RATE']) || $basketItemData['VAT_RATE'] == 0) {
         return 0;
     }
     if (isset($basketItemData['VAT_INCLUDED']) && $basketItemData['VAT_INCLUDED'] === 'N') {
         $vat = \Bitrix\Sale\PriceMaths::roundPrecision($basketItemData['PRICE'] * $basketItemData['QUANTITY'] * $basketItemData['VAT_RATE']);
     } else {
         $vat = \Bitrix\Sale\PriceMaths::roundPrecision($basketItemData['PRICE'] * $basketItemData['QUANTITY'] * $basketItemData['VAT_RATE'] / ($basketItemData['VAT_RATE'] + 1));
     }
     return $vat;
 }