Пример #1
0
 /**
  *
  *
  */
 public static function DoSaveOrder(&$arOrder, $arAdditionalFields, $orderId, &$arErrors, $arCoupons = array(), $arStoreBarcodeOrderFormData = array(), $bSaveBarcodes = false)
 {
     global $APPLICATION;
     $orderId = (int) $orderId;
     $isNew = $orderId <= 0;
     $isOrderConverted = \Bitrix\Main\Config\Option::get("main", "~sale_converted_15", 'N');
     $arFields = array("ID" => $arOrder["ID"], "LID" => $arOrder["SITE_ID"], "PERSON_TYPE_ID" => $arOrder["PERSON_TYPE_ID"], "PRICE" => $arOrder["PRICE"], "CURRENCY" => $arOrder["CURRENCY"], "USER_ID" => $arOrder["USER_ID"], "PAY_SYSTEM_ID" => $arOrder["PAY_SYSTEM_ID"], "PRICE_DELIVERY" => $arOrder["DELIVERY_PRICE"], "DELIVERY_ID" => strlen($arOrder["DELIVERY_ID"]) > 0 ? $arOrder["DELIVERY_ID"] : false, "DISCOUNT_VALUE" => $arOrder["DISCOUNT_PRICE"], "TAX_VALUE" => $arOrder["TAX_VALUE"], "TRACKING_NUMBER" => $arOrder["TRACKING_NUMBER"]);
     if ($arOrder["DELIVERY_PRICE"] == $arOrder["PRICE_DELIVERY"] && isset($arOrder['PRICE_DELIVERY_DIFF']) && floatval($arOrder['PRICE_DELIVERY_DIFF']) > 0) {
         $arFields["DELIVERY_PRICE"] = $arOrder['PRICE_DELIVERY_DIFF'] + $arOrder["PRICE_DELIVERY"];
     }
     if ($orderId <= 0) {
         $arFields["PAYED"] = "N";
         $arFields["CANCELED"] = "N";
         $arFields["STATUS_ID"] = "N";
     }
     $arFields = array_merge($arFields, $arAdditionalFields);
     if (!$arOrder['LOCATION_IN_CODES']) {
         // it comes from places like crm_invoice`s Add() and tells us if we need to convert location props from ID to CODE
         static::TranslateLocationPropertyValues($arOrder["PERSON_TYPE_ID"], $arOrder["ORDER_PROP"]);
     }
     unset($arOrder['LOCATION_IN_CODES']);
     if ($isOrderConverted == "Y") {
         $orderFields = array_merge($arOrder, $arFields, $arAdditionalFields);
         if (isset($orderFields['CUSTOM_DISCOUNT_PRICE']) && $orderFields['CUSTOM_DISCOUNT_PRICE'] === true) {
             Sale\Compatible\DiscountCompatibility::reInit(Sale\Compatible\DiscountCompatibility::MODE_DISABLED);
         }
         if (!empty($arStoreBarcodeOrderFormData)) {
             $orderFields['BARCODE_LIST'] = $arStoreBarcodeOrderFormData;
         }
         $orderFields['BARCODE_SAVE'] = $bSaveBarcodes;
         if ($orderId > 0) {
             $orderFields['ID'] = $orderId;
         }
         /** @var Sale\Result $r */
         $r = Sale\Compatible\OrderCompatibility::modifyOrder(Sale\Compatible\OrderCompatibility::ORDER_COMPAT_ACTION_SAVE, $orderFields);
         if ($r->isSuccess()) {
             $orderId = $r->getId();
         } else {
             foreach ($r->getErrorMessages() as $error) {
                 $arErrors[] = $error;
                 $APPLICATION->ThrowException($error);
             }
             return false;
         }
     } else {
         if ($orderId > 0) {
             $orderId = CSaleOrder::Update($orderId, $arFields);
         } else {
             if (COption::GetOptionString("sale", "product_reserve_condition", "O") == "O") {
                 $arFields["RESERVED"] = "Y";
             }
             $orderId = CSaleOrder::Add($arFields);
         }
         $orderId = (int) $orderId;
         if ($orderId <= 0) {
             if ($ex = $APPLICATION->GetException()) {
                 $arErrors[] = $ex->GetString();
             } else {
                 $arErrors[] = Loc::getMessage("SOA_ERROR_ORDER");
             }
         }
         if (!empty($arErrors)) {
             return null;
         }
         CSaleBasket::DoSaveOrderBasket($orderId, $arOrder["SITE_ID"], $arOrder["USER_ID"], $arOrder["BASKET_ITEMS"], $arErrors, $arCoupons, $arStoreBarcodeOrderFormData, $bSaveBarcodes);
         CSaleTax::DoSaveOrderTax($orderId, $arOrder["TAX_LIST"], $arErrors);
         CSaleOrderProps::DoSaveOrderProps($orderId, $arOrder["PERSON_TYPE_ID"], $arOrder["ORDER_PROP"], $arErrors);
         Sale\DiscountCouponsManager::finalApply();
         Sale\DiscountCouponsManager::saveApplied();
         foreach (GetModuleEvents("sale", "OnOrderSave", true) as $arEvent) {
             ExecuteModuleEventEx($arEvent, array($orderId, $arFields, $arOrder, $isNew));
         }
     }
     return $orderId;
 }
Пример #2
0
 /**
  * Update data element baskets
  *
  * @param $id
  * @param array $fields
  *
  * @return Sale\Result
  * @throws Main\ArgumentNullException
  * @throws Main\ArgumentTypeException
  * @throws Main\ObjectNotFoundException
  */
 public static function update($id, array $fields)
 {
     $result = new Sale\Result();
     $item = null;
     $basket = null;
     $order = null;
     $orderId = null;
     foreach (GetModuleEvents("sale", "OnBeforeBasketUpdateAfterCheck", true) as $event) {
         if (ExecuteModuleEventEx($event, array($id, &$fields)) === false) {
             return false;
         }
     }
     /** @var Sale\Result $itemResult */
     $itemResult = static::loadEntityFromBasket($id);
     if ($itemResult->isSuccess()) {
         $itemResultData = $itemResult->getData();
         if (isset($itemResultData['BASKET_ITEM'])) {
             /** @var Sale\BasketItem $item */
             $item = $itemResultData['BASKET_ITEM'];
         }
         if (isset($itemResultData['ORDER'])) {
             $order = $itemResultData['ORDER'];
         }
     }
     if (!$item) {
         $result->addError(new Sale\ResultError(Main\Localization\Loc::getMessage('SALE_BASKET_COMPATIBLE_BASKET_ITEM_NOT_FOUND'), 'BASKET_ITEM_NOT_FOUND'));
         return $result;
     }
     if (!empty($fields["PROPS"]) && is_array($fields["PROPS"])) {
         /** @var Sale\BasketPropertiesCollection $property */
         $property = $item->getPropertyCollection();
         $property->setProperty($fields["PROPS"]);
     }
     if ($order !== null && isset($fields['PRICE'])) {
         if ($fields['PRICE'] != $item->getPrice()) {
             $fields['CUSTOM_PRICE'] = 'Y';
         }
     }
     $r = $item->setFields(static::clearFields($fields));
     if (!$r->isSuccess()) {
         $result->addErrors($r->getErrors());
     }
     if (!DiscountCompatibility::isInited()) {
         DiscountCompatibility::init();
     }
     if (DiscountCompatibility::usedByClient()) {
         if (isset($fields['BASE_PRICE']) && isset($fields['CURRENCY'])) {
             DiscountCompatibility::setBasketItemBasePrice($id, $fields['BASE_PRICE'], $fields['CURRENCY']);
         }
         if (!empty($fields['DISCOUNT_LIST'])) {
             DiscountCompatibility::setBasketItemDiscounts($id, $fields['DISCOUNT_LIST']);
         }
         DiscountCompatibility::setBasketCode($id, $item->getBasketCode());
     }
     if ($order === null && !empty($fields['ORDER_ID']) && intval($fields['ORDER_ID']) > 0) {
         $orderId = intval($fields['ORDER_ID']);
         /** @var Sale\Order $order */
         if ($order = Sale\Order::load($orderId)) {
             /** @var Sale\Basket $basket */
             if ($basket = $order->getBasket()) {
                 $basket->addItem($item);
                 /** @var Sale\ShipmentCollection $shipmentCollection */
                 if (!($shipmentCollection = $order->getShipmentCollection())) {
                     throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
                 }
                 OrderCompatibility::createShipmentFromShipmentSystem($shipmentCollection);
                 /** @var Sale\Result $r */
                 $r = static::syncShipmentAndBasketItem($shipmentCollection, $item);
                 if (!$r->isSuccess()) {
                     $result->addErrors($r->getErrors());
                     return $result;
                 }
                 /** @var Sale\Result $r */
                 $r = static::syncShipmentCollectionAndBasket($shipmentCollection, $basket);
                 if (!$r->isSuccess()) {
                     $result->addErrors($r->getErrors());
                     return $result;
                 }
                 $r = $order->refreshData();
                 if (!$r->isSuccess()) {
                     $result->addErrors($r->getErrors());
                     return $result;
                 }
                 $r = $order->doFinalAction(true);
                 if (!$r->isSuccess()) {
                     $result->addErrors($r->getErrors());
                     return $result;
                 }
             }
         }
     }
     if ($order !== null) {
         /** @var Sale\ShipmentCollection $shipmentCollection */
         if ($shipmentCollection = $order->getShipmentCollection()) {
             if (count($shipmentCollection) == 2 && $shipmentCollection->isExistsSystemShipment()) {
                 /** @var Sale\Shipment $shipment */
                 foreach ($shipmentCollection as $shipment) {
                     if ($shipment->isSystem()) {
                         continue;
                     }
                     /** @var Sale\Shipment $systemShipment */
                     $systemShipment = $shipmentCollection->getSystemShipment();
                     /** @var Sale\ShipmentItemCollection $systemShipmentItemCollection */
                     $systemShipmentItemCollection = $systemShipment->getShipmentItemCollection();
                     /** @var Sale\ShipmentItem $systemShipmentItem */
                     if (!($systemShipmentItem = $systemShipmentItemCollection->getItemByBasketCode($item->getBasketCode()))) {
                         continue;
                     }
                     /** @var Sale\ShipmentItemCollection $shipmentItemCollection */
                     if (!($shipmentItemCollection = $shipment->getShipmentItemCollection())) {
                         continue;
                     }
                     /** @var Sale\ShipmentItem $shipmentItem */
                     if (!($shipmentItem = $shipmentItemCollection->getItemByBasketCode($item->getBasketCode()))) {
                         continue;
                     }
                     if ($systemShipmentItem->getQuantity() > 0) {
                         $r = $shipmentItem->setQuantity($shipmentItem->getQuantity() + $systemShipmentItem->getQuantity());
                         if (!$r->isSuccess()) {
                             $result->addErrors($r->getErrors());
                         }
                     }
                 }
             }
         }
         if (!$result->isSuccess()) {
             return $result;
         }
         $r = $order->save();
     } else {
         if (!$result->isSuccess()) {
             return $result;
         }
         $r = $item->save();
     }
     if (!$r->isSuccess()) {
         $result->addErrors($r->getErrors());
     }
     return $result;
 }
Пример #3
0
 /**
  * @internal
  * @param Sale\Order $order
  * @return array
  */
 public static function convertOrderToArray(Sale\Order $order)
 {
     $fields = $order->getFieldValues();
     //getWeight
     $fields = array_merge($fields, array('ORDER_WEIGHT' => 0, 'BASKET_ITEMS' => array(), 'ORDER_PROP' => array(), 'DISCOUNT_LIST' => array(), 'TAX_LIST' => array(), 'VAT_RATE' => $order->getVatRate(), 'VAT_SUM' => $order->getVatSum()));
     /** @var Sale\Basket $basket */
     if ($basket = $order->getBasket()) {
         /** @var Sale\BasketItem $basketItem */
         foreach ($basket as $basketItem) {
             $fields['BASKET_ITEMS'][] = BasketCompatibility::convertBasketItemToArray($basketItem);
         }
     }
     /** @var Sale\PropertyValueCollection $basket */
     if ($propertyCollection = $order->getPropertyCollection()) {
         /** @var Sale\PropertyValue $property */
         foreach ($propertyCollection as $property) {
             //				$propertyValue = $property->getValue();
             $fields['ORDER_PROP'][$property->getPropertyId()] = $property->getValue();
         }
     }
     if ($propProfileName = $propertyCollection->getProfileName()) {
         $fields['PROFILE_NAME'] = $propProfileName->getValue();
     }
     if ($propPayerName = $propertyCollection->getPayerName()) {
         $fields['PAYER_NAME'] = $propPayerName->getValue();
     }
     if ($propUserEmail = $propertyCollection->getUserEmail()) {
         $fields['USER_EMAIL'] = $propUserEmail->getValue();
     }
     if ($propDeliveryLocationZip = $propertyCollection->getDeliveryLocationZip()) {
         $fields['DELIVERY_LOCATION_ZIP'] = $propDeliveryLocationZip->getValue();
     }
     if ($propDeliveryLocation = $propertyCollection->getDeliveryLocation()) {
         $fields['DELIVERY_LOCATION'] = $propDeliveryLocation->getValue();
     }
     if ($propTaxLocation = $propertyCollection->getTaxLocation()) {
         $fields['TAX_LOCATION'] = $propTaxLocation->getValue();
     }
     /** @var Sale\ShipmentCollection $shipmentCollection */
     if ($shipmentCollection = $order->getShipmentCollection()) {
         $fields['ORDER_WEIGHT'] = $shipmentCollection->getWeight();
     }
     $fields['DISCOUNT_LIST'] = DiscountCompatibility::getOldDiscountResult();
     /** @var Sale\Tax $tax */
     if ($tax = $order->getTax()) {
         $fields['TAX_LIST'] = $tax->getTaxList();
     }
     return $fields;
 }
Пример #4
0
 public static function DoProcessOrder(&$arOrder, $arOptions, &$arErrors)
 {
     if (empty($arOrder['BASKET_ITEMS']) || !is_array($arOrder['BASKET_ITEMS'])) {
         return;
     }
     $isOrderConverted = \Bitrix\Main\Config\Option::get("main", "~sale_converted_15", 'N');
     if ($isOrderConverted == 'Y') {
         Sale\Compatible\DiscountCompatibility::clearDiscountResult();
         Sale\Compatible\DiscountCompatibility::fillBasketData($arOrder['BASKET_ITEMS']);
         Sale\Compatible\DiscountCompatibility::calculateBasketDiscounts($arOrder['BASKET_ITEMS']);
         Sale\Compatible\DiscountCompatibility::setApplyMode($arOrder['BASKET_ITEMS']);
     }
     $arIDS = array();
     $groupDiscountIterator = Sale\Internals\DiscountGroupTable::getList(array('select' => array('DISCOUNT_ID'), 'filter' => array('@GROUP_ID' => CUser::GetUserGroup($arOrder['USER_ID']), '=ACTIVE' => 'Y')));
     while ($groupDiscount = $groupDiscountIterator->fetch()) {
         $groupDiscount['DISCOUNT_ID'] = (int) $groupDiscount['DISCOUNT_ID'];
         if ($groupDiscount['DISCOUNT_ID'] > 0) {
             $arIDS[$groupDiscount['DISCOUNT_ID']] = true;
         }
     }
     if (!empty($arIDS)) {
         $arIDS = array_keys($arIDS);
         $couponList = Sale\DiscountCouponsManager::getForApply(array('MODULE' => 'sale', 'DISCOUNT_ID' => $arIDS), array(), true);
         $arExtend = array('catalog' => array('fields' => true, 'props' => true));
         foreach (GetModuleEvents('sale', 'OnExtendBasketItems', true) as $arEvent) {
             ExecuteModuleEventEx($arEvent, array(&$arOrder['BASKET_ITEMS'], $arExtend));
         }
         foreach ($arOrder['BASKET_ITEMS'] as &$arOneItem) {
             if (array_key_exists('PRODUCT_PROVIDER_CLASS', $arOneItem) && empty($arOneItem['PRODUCT_PROVIDER_CLASS']) && array_key_exists('CALLBACK_FUNC', $arOneItem) && empty($arOneItem['CALLBACK_FUNC']) && (!isset($arOneItem['CUSTOM_PRICE']) || $arOneItem['CUSTOM_PRICE'] != 'Y')) {
                 if (isset($arOneItem['DISCOUNT_PRICE'])) {
                     $arOneItem['PRICE'] += $arOneItem['DISCOUNT_PRICE'];
                     $arOneItem['DISCOUNT_PRICE'] = 0;
                     $arOneItem['BASE_PRICE'] = $arOneItem['PRICE'];
                 }
             }
         }
         if (isset($arOneItem)) {
             unset($arOneItem);
         }
         if (empty(self::$cacheDiscountHandlers)) {
             self::$cacheDiscountHandlers = CSaleDiscount::getDiscountHandlers($arIDS);
         } else {
             $needDiscountHandlers = array();
             foreach ($arIDS as &$discountID) {
                 if (!isset(self::$cacheDiscountHandlers[$discountID])) {
                     $needDiscountHandlers[] = $discountID;
                 }
             }
             unset($discountID);
             if (!empty($needDiscountHandlers)) {
                 $discountHandlersList = CSaleDiscount::getDiscountHandlers($needDiscountHandlers);
                 if (!empty($discountHandlersList)) {
                     foreach ($discountHandlersList as $discountID => $discountHandlers) {
                         self::$cacheDiscountHandlers[$discountID] = $discountHandlers;
                     }
                     unset($discountHandlers, $discountID);
                 }
                 unset($discountHandlersList);
             }
             unset($needDiscountHandlers);
         }
         $currentDatetime = new Main\Type\DateTime();
         $discountSelect = array('ID', 'PRIORITY', 'SORT', 'LAST_DISCOUNT', 'UNPACK', 'APPLICATION', 'USE_COUPONS', 'EXECUTE_MODULE', 'NAME', 'CONDITIONS_LIST', 'ACTIONS_LIST');
         $discountOrder = array('PRIORITY' => 'DESC', 'SORT' => 'ASC', 'ID' => 'ASC');
         $discountFilter = array('@ID' => $arIDS, '=LID' => $arOrder['SITE_ID'], array('LOGIC' => 'OR', 'ACTIVE_FROM' => '', '<=ACTIVE_FROM' => $currentDatetime), array('LOGIC' => 'OR', 'ACTIVE_TO' => '', '>=ACTIVE_TO' => $currentDatetime));
         if (empty($couponList)) {
             $discountFilter['=USE_COUPONS'] = 'N';
         } else {
             $discountFilter[] = array('LOGIC' => 'OR', '=USE_COUPONS' => 'N', array('=USE_COUPONS' => 'Y', '=COUPON.COUPON' => array_keys($couponList)));
             $discountSelect['DISCOUNT_COUPON'] = 'COUPON.COUPON';
         }
         $discountIterator = Sale\Internals\DiscountTable::getList(array('select' => $discountSelect, 'filter' => $discountFilter, 'order' => $discountOrder));
         $discountApply = array();
         $resultDiscountList = array();
         $resultDiscountKeys = array();
         $resultDiscountIndex = 0;
         while ($discount = $discountIterator->fetch()) {
             $discount['ID'] = (int) $discount['ID'];
             if (isset($discountApply[$discount['ID']])) {
                 continue;
             }
             $discount['MODULE'] = 'sale';
             $discount['MODULE_ID'] = 'sale';
             if ($discount['USE_COUPONS'] == 'Y') {
                 $discount['COUPON'] = $couponList[$discount['DISCOUNT_COUPON']];
             }
             $discountApply[$discount['ID']] = true;
             $applyFlag = true;
             if (isset(self::$cacheDiscountHandlers[$discount['ID']])) {
                 $moduleList = self::$cacheDiscountHandlers[$discount['ID']]['MODULES'];
                 if (!empty($moduleList)) {
                     foreach ($moduleList as &$moduleID) {
                         if (!isset(self::$usedModules[$moduleID])) {
                             self::$usedModules[$moduleID] = Loader::includeModule($moduleID);
                         }
                         if (!self::$usedModules[$moduleID]) {
                             $applyFlag = false;
                             break;
                         }
                     }
                     unset($moduleID);
                     if ($applyFlag) {
                         $discount['MODULES'] = $moduleList;
                     }
                 }
                 unset($moduleList);
             }
             if ($isOrderConverted == 'Y') {
                 Sale\Compatible\DiscountCompatibility::setOrderData($arOrder);
             }
             if ($applyFlag && self::__Unpack($arOrder, $discount['UNPACK'])) {
                 $oldOrder = $arOrder;
                 self::__ApplyActions($arOrder, $discount['APPLICATION']);
                 if ($isOrderConverted == 'Y') {
                     if (Sale\Compatible\DiscountCompatibility::calculateSaleDiscount($arOrder, $discount)) {
                         $resultDiscountList[$resultDiscountIndex] = array('MODULE_ID' => $discount['MODULE_ID'], 'ID' => $discount['ID'], 'NAME' => $discount['NAME'], 'PRIORITY' => $discount['PRIORITY'], 'SORT' => $discount['SORT'], 'LAST_DISCOUNT' => $discount['LAST_DISCOUNT'], 'CONDITIONS' => serialize($discount['CONDITIONS_LIST']), 'UNPACK' => $discount['UNPACK'], 'ACTIONS' => serialize($discount['ACTIONS_LIST']), 'APPLICATION' => $discount['APPLICATION'], 'RESULT' => self::getDiscountResult($oldOrder, $arOrder, false), 'HANDLERS' => self::$cacheDiscountHandlers[$discount['ID']], 'USE_COUPONS' => $discount['USE_COUPONS'], 'COUPON' => $discount['USE_COUPONS'] == 'Y' ? $couponList[$discount['DISCOUNT_COUPON']] : false);
                         $resultDiscountKeys[$discount['ID']] = $resultDiscountIndex;
                         $resultDiscountIndex++;
                         if ($discount['LAST_DISCOUNT'] == 'Y') {
                             break;
                         }
                     }
                 } else {
                     $discountResult = self::getDiscountResult($oldOrder, $arOrder, false);
                     if (!empty($discountResult['DELIVERY']) || !empty($discountResult['BASKET'])) {
                         if ($discount['USE_COUPONS'] == 'Y' && !empty($discount['DISCOUNT_COUPON'])) {
                             if ($couponList[$discount['DISCOUNT_COUPON']]['TYPE'] == Sale\Internals\DiscountCouponTable::TYPE_BASKET_ROW) {
                                 self::changeDiscountResult($oldOrder, $arOrder, $discountResult);
                             }
                             $couponApply = Sale\DiscountCouponsManager::setApply($discount['DISCOUNT_COUPON'], $discountResult);
                             unset($couponApply);
                         }
                         $resultDiscountList[$resultDiscountIndex] = array('MODULE_ID' => $discount['MODULE_ID'], 'ID' => $discount['ID'], 'NAME' => $discount['NAME'], 'PRIORITY' => $discount['PRIORITY'], 'SORT' => $discount['SORT'], 'LAST_DISCOUNT' => $discount['LAST_DISCOUNT'], 'CONDITIONS' => serialize($discount['CONDITIONS_LIST']), 'UNPACK' => $discount['UNPACK'], 'ACTIONS' => serialize($discount['ACTIONS_LIST']), 'APPLICATION' => $discount['APPLICATION'], 'RESULT' => $discountResult, 'HANDLERS' => self::$cacheDiscountHandlers[$discount['ID']], 'USE_COUPONS' => $discount['USE_COUPONS'], 'COUPON' => $discount['USE_COUPONS'] == 'Y' ? $couponList[$discount['DISCOUNT_COUPON']] : false);
                         $resultDiscountKeys[$discount['ID']] = $resultDiscountIndex;
                         $resultDiscountIndex++;
                         if ($discount['LAST_DISCOUNT'] == 'Y') {
                             break;
                         }
                     }
                     unset($discountResult);
                 }
             }
         }
         unset($discount, $discountIterator);
         $arOrder["ORDER_PRICE"] = 0;
         $arOrder["ORDER_WEIGHT"] = 0;
         $arOrder["USE_VAT"] = false;
         $arOrder["VAT_RATE"] = 0;
         $arOrder["VAT_SUM"] = 0;
         $arOrder["DISCOUNT_PRICE"] = 0.0;
         $arOrder["DISCOUNT_VALUE"] = $arOrder["DISCOUNT_PRICE"];
         $arOrder["PRICE_DELIVERY"] = roundEx($arOrder["PRICE_DELIVERY"], SALE_VALUE_PRECISION);
         $arOrder["DELIVERY_PRICE"] = $arOrder["PRICE_DELIVERY"];
         foreach ($arOrder['BASKET_ITEMS'] as &$arShoppingCartItem) {
             if (!CSaleBasketHelper::isSetItem($arShoppingCartItem)) {
                 $customPrice = isset($arShoppingCartItem['CUSTOM_PRICE']) && ($arShoppingCartItem['CUSTOM_PRICE'] = 'Y');
                 if (!$customPrice) {
                     $arShoppingCartItem['DISCOUNT_PRICE'] = roundEx($arShoppingCartItem['DISCOUNT_PRICE'], SALE_VALUE_PRECISION);
                     if ($arShoppingCartItem['DISCOUNT_PRICE'] > 0) {
                         $arShoppingCartItem['PRICE'] = $arShoppingCartItem['BASE_PRICE'] - $arShoppingCartItem['DISCOUNT_PRICE'];
                     } else {
                         $arShoppingCartItem['PRICE'] = roundEx($arShoppingCartItem['PRICE'], SALE_VALUE_PRECISION);
                     }
                 } else {
                     $arShoppingCartItem['DISCOUNT_PRICE'] = 0;
                 }
                 $arOrder["ORDER_PRICE"] += $arShoppingCartItem["PRICE"] * $arShoppingCartItem["QUANTITY"];
                 $arOrder["ORDER_WEIGHT"] += $arShoppingCartItem["WEIGHT"] * $arShoppingCartItem["QUANTITY"];
                 $arShoppingCartItem["PRICE_FORMATED"] = CCurrencyLang::CurrencyFormat($arShoppingCartItem["PRICE"], $arShoppingCartItem["CURRENCY"], true);
                 $arShoppingCartItem["DISCOUNT_PRICE_PERCENT"] = 0;
                 if ($arShoppingCartItem["DISCOUNT_PRICE"] + $arShoppingCartItem["PRICE"] > 0) {
                     $arShoppingCartItem["DISCOUNT_PRICE_PERCENT"] = $arShoppingCartItem["DISCOUNT_PRICE"] * 100 / ($arShoppingCartItem["DISCOUNT_PRICE"] + $arShoppingCartItem["PRICE"]);
                 }
                 $arShoppingCartItem["DISCOUNT_PRICE_PERCENT_FORMATED"] = roundEx($arShoppingCartItem["DISCOUNT_PRICE_PERCENT"], SALE_VALUE_PRECISION) . "%";
                 if ($arShoppingCartItem["VAT_RATE"] > 0) {
                     $arOrder["USE_VAT"] = true;
                     if ($arShoppingCartItem["VAT_RATE"] > $arOrder["VAT_RATE"]) {
                         $arOrder["VAT_RATE"] = $arShoppingCartItem["VAT_RATE"];
                     }
                     $arOrder["VAT_SUM"] += $arShoppingCartItem["VAT_VALUE"] * $arShoppingCartItem["QUANTITY"];
                 }
             }
         }
         unset($arShoppingCartItem);
         $arOrder['DISCOUNT_LIST'] = $resultDiscountList;
         if ($isOrderConverted == 'Y') {
             Sale\Compatible\DiscountCompatibility::setOldDiscountResult($resultDiscountList);
         }
     }
     $arOrder["ORDER_PRICE"] = roundEx($arOrder["ORDER_PRICE"], SALE_VALUE_PRECISION);
 }
Пример #5
0
 function UpdateBasketPrices($fuserID, $siteID)
 {
     $fuserID = (int) $fuserID;
     if ($fuserID <= 0) {
         return false;
     }
     $siteID = (string) $siteID;
     if ($siteID == '') {
         $siteID = SITE_ID;
     }
     $isOrderConverted = \Bitrix\Main\Config\Option::get("main", "~sale_converted_15", 'N');
     $dbBasketItems = CSaleBasket::GetList(array("ALL_PRICE" => "DESC"), array("FUSER_ID" => $fuserID, "LID" => $siteID, "ORDER_ID" => "NULL", "SUBSCRIBE" => "N"), false, false, array("ID", "MODULE", "PRODUCT_ID", "QUANTITY", "CALLBACK_FUNC", "PRODUCT_PROVIDER_CLASS", "CAN_BUY", "DELAY", "NOTES", "TYPE", "SET_PARENT_ID"));
     while ($arItem = $dbBasketItems->Fetch()) {
         if ($arItem['CAN_BUY'] != 'Y') {
             continue;
         }
         $arFields = false;
         $arItem['CALLBACK_FUNC'] = (string) $arItem['CALLBACK_FUNC'];
         $arItem['PRODUCT_PROVIDER_CLASS'] = (string) $arItem['PRODUCT_PROVIDER_CLASS'];
         if ('' != $arItem['PRODUCT_PROVIDER_CLASS'] || '' != $arItem['CALLBACK_FUNC']) {
             $arItem['MODULE'] = (string) $arItem['MODULE'];
             $arItem['PRODUCT_ID'] = (int) $arItem['PRODUCT_ID'];
             $arItem['QUANTITY'] = (double) $arItem['QUANTITY'];
             if ($productProvider = CSaleBasket::GetProductProvider($arItem)) {
                 $arFields = $productProvider::GetProductData(array("PRODUCT_ID" => $arItem["PRODUCT_ID"], "QUANTITY" => $arItem["QUANTITY"], "RENEWAL" => "N", "CHECK_COUPONS" => 'Y' == $arItem['CAN_BUY'] && 'N' == $arItem['DELAY'] ? 'Y' : 'N', "CHECK_DISCOUNT" => CSaleBasketHelper::isSetItem($arItem) ? 'N' : 'Y', "BASKET_ID" => $arItem["ID"], "NOTES" => $arItem["NOTES"]));
             } else {
                 $arFields = CSaleBasket::ExecuteCallbackFunction($arItem["CALLBACK_FUNC"], $arItem["MODULE"], $arItem["PRODUCT_ID"], $arItem["QUANTITY"], "N");
             }
             if (!empty($arFields) && is_array($arFields)) {
                 if ($isOrderConverted == 'Y' && $arItem['DELAY'] == 'N') {
                     if (!Sale\Compatible\DiscountCompatibility::isInited()) {
                         Sale\Compatible\DiscountCompatibility::init();
                     }
                     if (Sale\Compatible\DiscountCompatibility::usedByClient()) {
                         if (isset($arFields['BASE_PRICE']) && isset($arFields['CURRENCY'])) {
                             Sale\Compatible\DiscountCompatibility::setBasketItemBasePrice($arItem['ID'], $arFields['BASE_PRICE'], $arFields['CURRENCY']);
                         }
                         if (!empty($arFields['DISCOUNT_LIST'])) {
                             Sale\Compatible\DiscountCompatibility::setBasketItemDiscounts($arItem['ID'], $arFields['DISCOUNT_LIST']);
                         }
                     }
                 }
                 $arFields['CAN_BUY'] = 'Y';
                 $arFields['TYPE'] = (int) $arItem['TYPE'];
                 $arFields['SET_PARENT_ID'] = (int) $arItem['SET_PARENT_ID'];
             } else {
                 $arFields = array('CAN_BUY' => 'N');
             }
             CSaleBasket::Update($arItem['ID'], $arFields);
         }
     }
     return true;
 }
Пример #6
0
 protected function processOrderAcceptRequest($arPostData)
 {
     $arResult = array();
     DiscountCompatibility::reInit(DiscountCompatibility::MODE_EXTERNAL, array('SITE_ID' => $this->siteId));
     if ($this->checkOrderAcceptStructure($arPostData)) {
         $dbRes = \Bitrix\Sale\TradingPlatform\OrderTable::getList(array("filter" => array("TRADING_PLATFORM_ID" => YandexMarket::getInstance()->getId(), "EXTERNAL_ORDER_ID" => $arPostData["order"]["id"])));
         if (!($orderCorrespondence = $dbRes->fetch())) {
             require_once $_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/sale/general/admin_tool.php";
             $arProducts = array();
             foreach ($arPostData["order"]["items"] as $arItem) {
                 $arProduct = $this->getProductById($arItem["offerId"], $arItem["count"]);
                 $arProduct["PRODUCT_ID"] = $arItem["offerId"];
                 $arProduct["MODULE"] = "catalog";
                 $arProduct["PRODUCT_PROVIDER_CLASS"] = "CCatalogProductProvider";
                 $dbIblockElement = CIBlockElement::GetList(array(), array("ID" => $arItem["offerId"]), false, false, array('XML_ID', 'IBLOCK_EXTERNAL_ID'));
                 if ($IblockElement = $dbIblockElement->Fetch()) {
                     if (strlen($IblockElement["XML_ID"]) > 0) {
                         $arProduct["PRODUCT_XML_ID"] = $IblockElement["XML_ID"];
                     }
                     if (strlen($IblockElement["IBLOCK_EXTERNAL_ID"]) > 0) {
                         $arProduct["CATALOG_XML_ID"] = $IblockElement["IBLOCK_EXTERNAL_ID"];
                     }
                 }
                 if ($arProduct["CAN_BUY"] == "Y") {
                     $arProducts[] = $arProduct;
                 }
             }
             $arOrderProductPrice = fGetUserShoppingCart($arProducts, $this->siteId, "N");
             $arErrors = array();
             $userId = intval(CSaleUser::GetAnonymousUserID());
             $arShoppingCart = CSaleBasket::DoGetUserShoppingCart($this->siteId, $userId, $arOrderProductPrice, $arErrors);
             $deliveryId = $arPostData["order"]["delivery"]["id"];
             $paySystemId = $this->mapPaySystems[$arPostData["order"]["paymentMethod"]];
             $locationId = $this->locationMapper->getLocationByCityName($arPostData["order"]["delivery"]["region"]["name"]);
             if ($locationId === false) {
                 $this->log(self::LOG_LEVEL_INFO, "YMARKET_LOCATION_MAPPING", $arPostData["order"]["delivery"]["region"]["name"], GetMessage("SALE_YMH_LOCATION_NOT_FOUND"));
             }
             $arErrors = $arWarnings = array();
             $arOptions = array();
             $arOrderPropsValues = $this->makeAdditionalOrderProps($arPostData["order"]["delivery"]["address"], array(), $this->mapPaySystems[$arPostData["order"]["paymentMethod"]], $arPostData["order"]["delivery"]["id"], $locationId);
             $CSaleOrder = new CSaleOrder();
             $arOrder = $CSaleOrder->DoCalculateOrder($this->siteId, $userId, $arShoppingCart, $this->personTypeId, $arOrderPropsValues, $deliveryId, $paySystemId, $arOptions, $arErrors, $arWarnings);
             $arErrors = array();
             $arAdditionalFields = array("XML_ID" => self::XML_ID_PREFIX . $arPostData["order"]["id"]);
             $arOrder["LID"] = $this->siteId;
             if (isset($arPostData["order"]["notes"])) {
                 $arAdditionalFields["USER_DESCRIPTION"] = $arPostData["order"]["notes"];
             }
             $orderID = $CSaleOrder->DoSaveOrder($arOrder, $arAdditionalFields, 0, $arErrors);
             $res = \Bitrix\Sale\TradingPlatform\OrderTable::add(array("ORDER_ID" => $orderID, "TRADING_PLATFORM_ID" => YandexMarket::getInstance()->getId(), "EXTERNAL_ORDER_ID" => $arPostData["order"]["id"]));
             if (!$res->isSuccess()) {
                 foreach ($res->getErrors() as $error) {
                     $this->log(self::LOG_LEVEL_ERROR, "YMARKET_PLATFORM_ORDER_ADD_ERROR", $orderID, $error);
                 }
             }
         } else {
             $orderID = $orderCorrespondence["ORDER_ID"];
         }
         if (intval($orderID > 0)) {
             $arResult["order"]["accepted"] = true;
             $arResult["order"]["id"] = strval($orderID);
             $this->log(self::LOG_LEVEL_INFO, "YMARKET_ORDER_CREATE", $arPostData["order"]["id"], GetMessage("SALE_YMH_ORDER_CREATED") . " " . $orderID);
         } else {
             $arResult["order"]["accepted"] = false;
             $arResult["order"]["reason"] = "OUT_OF_DATE";
             $this->log(self::LOG_LEVEL_ERROR, "YMARKET_ORDER_CREATE", $arPostData["order"]["id"], print_r($arErrors, true));
         }
     } else {
         $arResult = $this->processError(self::ERROR_STATUS_400, GetMessage("SALE_YMH_ERROR_BAD_STRUCTURE"));
     }
     return $arResult;
 }
Пример #7
0
 public static function DoProcessOrder(&$arOrder, $arOptions, &$arErrors)
 {
     if (empty($arOrder['BASKET_ITEMS']) || !is_array($arOrder['BASKET_ITEMS'])) {
         return;
     }
     $isOrderConverted = \Bitrix\Main\Config\Option::get("main", "~sale_converted_15", 'N');
     $oldDelivery = '';
     $checkIds = true;
     $arIDS = array();
     if ($isOrderConverted == 'Y') {
         if (isset($arOrder['DELIVERY_ID']) && $arOrder['DELIVERY_ID'] != '') {
             $oldDelivery = $arOrder['DELIVERY_ID'];
             $arOrder['DELIVERY_ID'] = \CSaleDelivery::getIdByCode($arOrder['DELIVERY_ID']);
         }
         $adminSection = defined('ADMIN_SECTION') && ADMIN_SECTION === true;
         if (!$adminSection) {
             $mode = Sale\Compatible\DiscountCompatibility::MODE_MANAGER;
             $modeParams = array();
             if (isset($arOrder['CURRENCY'])) {
                 $modeParams['CURRENCY'] = $arOrder['CURRENCY'];
             }
             if (isset($arOrder['SITE_ID'])) {
                 $modeParams['SITE_ID'] = $arOrder['SITE_ID'];
                 if (!isset($modeParams['CURRENCY'])) {
                     $modeParams['CURRENCY'] = Sale\Internals\SiteCurrencyTable::getSiteCurrency($modeParams['SITE_ID']);
                 }
             }
         } else {
             $mode = Sale\Compatible\DiscountCompatibility::MODE_CLIENT;
             $modeParams = array('SITE_ID' => SITE_ID, 'CURRENCY' => Sale\Internals\SiteCurrencyTable::getSiteCurrency(SITE_ID));
         }
         unset($adminSection);
         if (!empty($modeParams)) {
             Sale\Discount\Actions::setUseMode(Sale\Discount\Actions::MODE_CALCULATE, array('USE_BASE_PRICE' => \Bitrix\Main\Config\Option::get('sale', 'get_discount_percent_from_base_price'), 'SITE_ID' => $modeParams['SITE_ID'], 'CURRENCY' => $modeParams['CURRENCY']));
         }
         if (!Sale\Compatible\DiscountCompatibility::isInited()) {
             if (!empty($modeParams)) {
                 Sale\Compatible\DiscountCompatibility::init($mode, $modeParams);
             }
         }
         unset($modeParams, $mode);
         Sale\Compatible\DiscountCompatibility::clearDiscountResult();
         Sale\Compatible\DiscountCompatibility::fillBasketData($arOrder['BASKET_ITEMS']);
         Sale\Compatible\DiscountCompatibility::calculateBasketDiscounts($arOrder['BASKET_ITEMS']);
         Sale\Compatible\DiscountCompatibility::setApplyMode($arOrder['BASKET_ITEMS']);
         $applyMode = Sale\Discount::getApplyMode();
         if ($applyMode == Sale\Discount::APPLY_MODE_FULL_LAST || $applyMode == Sale\Discount::APPLY_MODE_FULL_DISABLE) {
             foreach ($arOrder['BASKET_ITEMS'] as &$basketItem) {
                 if (isset($basketItem['LAST_DISCOUNT']) && $basketItem['LAST_DISCOUNT'] == 'Y') {
                     $checkIds = false;
                     break;
                 }
             }
             unset($basketItem);
         }
     }
     if ($checkIds) {
         $groupDiscountIterator = Sale\Internals\DiscountGroupTable::getList(array('select' => array('DISCOUNT_ID'), 'filter' => array('@GROUP_ID' => CUser::GetUserGroup($arOrder['USER_ID']), '=ACTIVE' => 'Y')));
         while ($groupDiscount = $groupDiscountIterator->fetch()) {
             $groupDiscount['DISCOUNT_ID'] = (int) $groupDiscount['DISCOUNT_ID'];
             if ($groupDiscount['DISCOUNT_ID'] > 0) {
                 $arIDS[$groupDiscount['DISCOUNT_ID']] = true;
             }
         }
     }
     if (!empty($arIDS)) {
         $arIDS = array_keys($arIDS);
         $couponList = Sale\DiscountCouponsManager::getForApply(array('MODULE_ID' => 'sale', 'DISCOUNT_ID' => $arIDS), array(), true);
         //TODO: fix this condition
         $useProps = true;
         $iblockPropList = array();
         $entityList = Sale\Internals\DiscountEntitiesTable::getByDiscount($arIDS, array('=MODULE_ID' => 'catalog', '=ENTITY' => 'ELEMENT_PROPERTY'));
         if (empty($entityList)) {
             $useProps = false;
         } else {
             if (empty($entityList['catalog']['ELEMENT_PROPERTY'])) {
                 $useProps = false;
             } else {
                 foreach ($entityList['catalog']['ELEMENT_PROPERTY'] as $entity) {
                     $entityField = explode(':', $entity['FIELD_TABLE']);
                     if (isset($entityField[1])) {
                         $propId = (int) $entityField[1];
                         if ($propId > 0) {
                             $iblockPropList[$propId] = $propId;
                         }
                         unset($propId);
                     }
                     unset($entityField);
                 }
                 unset($entity);
                 if (empty($iblockPropList)) {
                     $useProps = false;
                 }
             }
         }
         $arExtend = array('catalog' => array('fields' => true, 'props' => $useProps));
         if ($useProps) {
             $arExtend['iblock']['props'] = $iblockPropList;
         }
         unset($iblockPropList, $useProps);
         foreach (GetModuleEvents('sale', 'OnExtendBasketItems', true) as $arEvent) {
             ExecuteModuleEventEx($arEvent, array(&$arOrder['BASKET_ITEMS'], $arExtend));
         }
         foreach ($arOrder['BASKET_ITEMS'] as &$arOneItem) {
             if (array_key_exists('PRODUCT_PROVIDER_CLASS', $arOneItem) && empty($arOneItem['PRODUCT_PROVIDER_CLASS']) && array_key_exists('CALLBACK_FUNC', $arOneItem) && empty($arOneItem['CALLBACK_FUNC']) && (!isset($arOneItem['CUSTOM_PRICE']) || $arOneItem['CUSTOM_PRICE'] != 'Y')) {
                 if (isset($arOneItem['DISCOUNT_PRICE'])) {
                     $arOneItem['PRICE'] += $arOneItem['DISCOUNT_PRICE'];
                     $arOneItem['DISCOUNT_PRICE'] = 0;
                     $arOneItem['BASE_PRICE'] = $arOneItem['PRICE'];
                 }
             }
         }
         if (isset($arOneItem)) {
             unset($arOneItem);
         }
         if (empty(self::$cacheDiscountHandlers)) {
             self::$cacheDiscountHandlers = CSaleDiscount::getDiscountHandlers($arIDS);
         } else {
             $needDiscountHandlers = array();
             foreach ($arIDS as &$discountID) {
                 if (!isset(self::$cacheDiscountHandlers[$discountID])) {
                     $needDiscountHandlers[] = $discountID;
                 }
             }
             unset($discountID);
             if (!empty($needDiscountHandlers)) {
                 $discountHandlersList = CSaleDiscount::getDiscountHandlers($needDiscountHandlers);
                 if (!empty($discountHandlersList)) {
                     foreach ($discountHandlersList as $discountID => $discountHandlers) {
                         self::$cacheDiscountHandlers[$discountID] = $discountHandlers;
                     }
                     unset($discountHandlers, $discountID);
                 }
                 unset($discountHandlersList);
             }
             unset($needDiscountHandlers);
         }
         $currentDatetime = new Main\Type\DateTime();
         $discountSelect = array('ID', 'PRIORITY', 'SORT', 'LAST_DISCOUNT', 'UNPACK', 'APPLICATION', 'USE_COUPONS', 'EXECUTE_MODULE', 'NAME', 'CONDITIONS_LIST', 'ACTIONS_LIST');
         $discountOrder = array('PRIORITY' => 'DESC', 'SORT' => 'ASC', 'ID' => 'ASC');
         $discountFilter = array('@ID' => $arIDS, '=LID' => $arOrder['SITE_ID'], array('LOGIC' => 'OR', 'ACTIVE_FROM' => '', '<=ACTIVE_FROM' => $currentDatetime), array('LOGIC' => 'OR', 'ACTIVE_TO' => '', '>=ACTIVE_TO' => $currentDatetime));
         if (empty($couponList)) {
             $discountFilter['=USE_COUPONS'] = 'N';
         } else {
             $discountFilter[] = array('LOGIC' => 'OR', '=USE_COUPONS' => 'N', array('=USE_COUPONS' => 'Y', '=COUPON.COUPON' => array_keys($couponList)));
             $discountSelect['DISCOUNT_COUPON'] = 'COUPON.COUPON';
         }
         $discountIterator = Sale\Internals\DiscountTable::getList(array('select' => $discountSelect, 'filter' => $discountFilter, 'order' => $discountOrder));
         $discountApply = array();
         $resultDiscountFullList = array();
         $resultDiscountList = array();
         $resultDiscountKeys = array();
         $resultDiscountIndex = 0;
         while ($discount = $discountIterator->fetch()) {
             $discount['ID'] = (int) $discount['ID'];
             if (isset($discountApply[$discount['ID']])) {
                 continue;
             }
             $discount['MODULE'] = 'sale';
             $discount['MODULE_ID'] = 'sale';
             if ($discount['USE_COUPONS'] == 'Y') {
                 $discount['COUPON'] = $couponList[$discount['DISCOUNT_COUPON']];
             }
             $discountApply[$discount['ID']] = true;
             $applyFlag = true;
             if (isset(self::$cacheDiscountHandlers[$discount['ID']])) {
                 $moduleList = self::$cacheDiscountHandlers[$discount['ID']]['MODULES'];
                 if (!empty($moduleList)) {
                     foreach ($moduleList as &$moduleID) {
                         if (!isset(self::$usedModules[$moduleID])) {
                             self::$usedModules[$moduleID] = Loader::includeModule($moduleID);
                         }
                         if (!self::$usedModules[$moduleID]) {
                             $applyFlag = false;
                             break;
                         }
                     }
                     unset($moduleID);
                     if ($applyFlag) {
                         $discount['MODULES'] = $moduleList;
                     }
                 }
                 unset($moduleList);
             }
             if ($isOrderConverted == 'Y') {
                 Sale\Compatible\DiscountCompatibility::setOrderData($arOrder);
             }
             if ($applyFlag && self::__Unpack($arOrder, $discount['UNPACK'])) {
                 $oldOrder = $arOrder;
                 if ($isOrderConverted == 'Y') {
                     Sale\Discount\Actions::clearAction();
                 }
                 self::__ApplyActions($arOrder, $discount['APPLICATION']);
                 if ($isOrderConverted == 'Y') {
                     $resultDiscountFullList[] = $discount;
                     if (Sale\Compatible\DiscountCompatibility::calculateSaleDiscount($arOrder, $discount)) {
                         $resultDiscountList[$resultDiscountIndex] = array('MODULE_ID' => $discount['MODULE_ID'], 'ID' => $discount['ID'], 'NAME' => $discount['NAME'], 'PRIORITY' => $discount['PRIORITY'], 'SORT' => $discount['SORT'], 'LAST_DISCOUNT' => $discount['LAST_DISCOUNT'], 'CONDITIONS' => serialize($discount['CONDITIONS_LIST']), 'UNPACK' => $discount['UNPACK'], 'ACTIONS' => serialize($discount['ACTIONS_LIST']), 'APPLICATION' => $discount['APPLICATION'], 'RESULT' => self::getDiscountResult($oldOrder, $arOrder, false), 'HANDLERS' => self::$cacheDiscountHandlers[$discount['ID']], 'USE_COUPONS' => $discount['USE_COUPONS'], 'COUPON' => $discount['USE_COUPONS'] == 'Y' ? $couponList[$discount['DISCOUNT_COUPON']] : false);
                         $resultDiscountKeys[$discount['ID']] = $resultDiscountIndex;
                         $resultDiscountIndex++;
                         if ($discount['LAST_DISCOUNT'] == 'Y') {
                             break;
                         }
                     }
                     Sale\Discount\Actions::clearAction();
                 } else {
                     $discountResult = self::getDiscountResult($oldOrder, $arOrder, false);
                     if (!empty($discountResult['DELIVERY']) || !empty($discountResult['BASKET'])) {
                         if ($discount['USE_COUPONS'] == 'Y' && !empty($discount['DISCOUNT_COUPON'])) {
                             if ($couponList[$discount['DISCOUNT_COUPON']]['TYPE'] == Sale\Internals\DiscountCouponTable::TYPE_BASKET_ROW) {
                                 self::changeDiscountResult($oldOrder, $arOrder, $discountResult);
                             }
                             $couponApply = Sale\DiscountCouponsManager::setApply($discount['DISCOUNT_COUPON'], $discountResult);
                             unset($couponApply);
                         }
                         $resultDiscountList[$resultDiscountIndex] = array('MODULE_ID' => $discount['MODULE_ID'], 'ID' => $discount['ID'], 'NAME' => $discount['NAME'], 'PRIORITY' => $discount['PRIORITY'], 'SORT' => $discount['SORT'], 'LAST_DISCOUNT' => $discount['LAST_DISCOUNT'], 'CONDITIONS' => serialize($discount['CONDITIONS_LIST']), 'UNPACK' => $discount['UNPACK'], 'ACTIONS' => serialize($discount['ACTIONS_LIST']), 'APPLICATION' => $discount['APPLICATION'], 'RESULT' => $discountResult, 'HANDLERS' => self::$cacheDiscountHandlers[$discount['ID']], 'USE_COUPONS' => $discount['USE_COUPONS'], 'COUPON' => $discount['USE_COUPONS'] == 'Y' ? $couponList[$discount['DISCOUNT_COUPON']] : false);
                         $resultDiscountKeys[$discount['ID']] = $resultDiscountIndex;
                         $resultDiscountIndex++;
                         if ($discount['LAST_DISCOUNT'] == 'Y') {
                             break;
                         }
                     }
                     unset($discountResult);
                 }
             }
         }
         unset($discount, $discountIterator);
         $arOrder["ORDER_PRICE"] = 0;
         $arOrder["ORDER_WEIGHT"] = 0;
         $arOrder["USE_VAT"] = false;
         $arOrder["VAT_RATE"] = 0;
         $arOrder["VAT_SUM"] = 0;
         $arOrder["DISCOUNT_PRICE"] = 0.0;
         $arOrder["DISCOUNT_VALUE"] = $arOrder["DISCOUNT_PRICE"];
         $arOrder["PRICE_DELIVERY"] = roundEx($arOrder["PRICE_DELIVERY"], SALE_VALUE_PRECISION);
         $arOrder["DELIVERY_PRICE"] = $arOrder["PRICE_DELIVERY"];
         foreach ($arOrder['BASKET_ITEMS'] as &$arShoppingCartItem) {
             if (isset($arShoppingCartItem['CATALOG'])) {
                 unset($arShoppingCartItem['CATALOG']);
             }
             if (!CSaleBasketHelper::isSetItem($arShoppingCartItem)) {
                 $customPrice = isset($arShoppingCartItem['CUSTOM_PRICE']) && ($arShoppingCartItem['CUSTOM_PRICE'] = 'Y');
                 if (!$customPrice) {
                     $arShoppingCartItem['DISCOUNT_PRICE'] = roundEx($arShoppingCartItem['DISCOUNT_PRICE'], SALE_VALUE_PRECISION);
                     if ($arShoppingCartItem['DISCOUNT_PRICE'] > 0) {
                         $arShoppingCartItem['PRICE'] = $arShoppingCartItem['BASE_PRICE'] - $arShoppingCartItem['DISCOUNT_PRICE'];
                     } else {
                         $arShoppingCartItem['PRICE'] = roundEx($arShoppingCartItem['PRICE'], SALE_VALUE_PRECISION);
                     }
                 } else {
                     $arShoppingCartItem['DISCOUNT_PRICE'] = 0;
                 }
                 if (isset($arShoppingCartItem['VAT_RATE'])) {
                     $vatRate = (double) $arShoppingCartItem['VAT_RATE'];
                     if ($vatRate > 0) {
                         $arShoppingCartItem['VAT_VALUE'] = $arShoppingCartItem['PRICE'] / ($vatRate + 1) * $vatRate;
                     }
                     unset($vatRate);
                 }
                 $arOrder["ORDER_PRICE"] += $arShoppingCartItem["PRICE"] * $arShoppingCartItem["QUANTITY"];
                 $arOrder["ORDER_WEIGHT"] += $arShoppingCartItem["WEIGHT"] * $arShoppingCartItem["QUANTITY"];
                 $arShoppingCartItem["PRICE_FORMATED"] = CCurrencyLang::CurrencyFormat($arShoppingCartItem["PRICE"], $arShoppingCartItem["CURRENCY"], true);
                 $arShoppingCartItem["DISCOUNT_PRICE_PERCENT"] = 0;
                 if ($arShoppingCartItem["DISCOUNT_PRICE"] + $arShoppingCartItem["PRICE"] > 0) {
                     $arShoppingCartItem["DISCOUNT_PRICE_PERCENT"] = $arShoppingCartItem["DISCOUNT_PRICE"] * 100 / ($arShoppingCartItem["DISCOUNT_PRICE"] + $arShoppingCartItem["PRICE"]);
                 }
                 $arShoppingCartItem["DISCOUNT_PRICE_PERCENT_FORMATED"] = roundEx($arShoppingCartItem["DISCOUNT_PRICE_PERCENT"], SALE_VALUE_PRECISION) . "%";
                 if ($arShoppingCartItem["VAT_RATE"] > 0) {
                     $arOrder["USE_VAT"] = true;
                     if ($arShoppingCartItem["VAT_RATE"] > $arOrder["VAT_RATE"]) {
                         $arOrder["VAT_RATE"] = $arShoppingCartItem["VAT_RATE"];
                     }
                     $arOrder["VAT_SUM"] += $arShoppingCartItem["VAT_VALUE"] * $arShoppingCartItem["QUANTITY"];
                 }
             }
         }
         unset($arShoppingCartItem);
         $arOrder['DISCOUNT_LIST'] = $resultDiscountList;
         $arOrder['FULL_DISCOUNT_LIST'] = $resultDiscountFullList;
         if ($isOrderConverted == 'Y') {
             Sale\Compatible\DiscountCompatibility::setOldDiscountResult($resultDiscountList);
         }
     }
     if ($isOrderConverted == 'Y' && $oldDelivery != '') {
         $arOrder['DELIVERY_ID'] = $oldDelivery;
     }
     $arOrder["ORDER_PRICE"] = roundEx($arOrder["ORDER_PRICE"], SALE_VALUE_PRECISION);
 }
Пример #8
0
 function UpdateBasketPrices($fuserID, $siteID)
 {
     $fuserID = (int) $fuserID;
     if ($fuserID <= 0) {
         return false;
     }
     $siteID = (string) $siteID;
     if ($siteID == '') {
         $siteID = SITE_ID;
     }
     $isOrderConverted = \Bitrix\Main\Config\Option::get("main", "~sale_converted_15", 'N');
     DiscountCouponsManager::clearApply(false);
     $basketItems = array();
     /*
     if (!empty($arBasketItems) && is_array($arBasketItems))
     			{
     				foreach ($arBasketItems as &$basketItemData)
     				{
     					if (array_key_exists('MEASURE_RATIO', $basketItemData))
     					{
     						$basketItemQuantity = floatval($basketItemData['QUANTITY']);
     						$basketItemRatio = floatval($basketItemData['MEASURE_RATIO']);
     
     						$mod = roundEx(($basketItemQuantity / $basketItemRatio - intval($basketItemQuantity / $basketItemRatio)), 6);
     
     						if ($mod !== 0)
     						{
     							$basketItemData['QUANTITY'] = ceil(ceil($basketItemQuantity) / $basketItemRatio)* $basketItemRatio;
     							CSaleBasket::Update($basketItemData['ID'], array('QUANTITY' => $basketItemData['QUANTITY']));
     						}
     					}
     				}
     				unset($basketItemData);
     			}
     */
     $dbBasketItems = CSaleBasket::GetList(array("ALL_PRICE" => "DESC"), array("FUSER_ID" => $fuserID, "LID" => $siteID, "ORDER_ID" => "NULL", "SUBSCRIBE" => "N"), false, false, array("ID", "MODULE", "PRODUCT_ID", "QUANTITY", "CALLBACK_FUNC", "PRODUCT_PROVIDER_CLASS", "CAN_BUY", "DELAY", "NOTES", "TYPE", "SET_PARENT_ID"));
     while ($arItem = $dbBasketItems->Fetch()) {
         $basketItems[] = $arItem;
     }
     if (!empty($basketItems) && is_array($basketItems)) {
         $basketItems = getRatio($basketItems);
         foreach ($basketItems as $basketItem) {
             $fields = array();
             $basketItem['CALLBACK_FUNC'] = (string) $basketItem['CALLBACK_FUNC'];
             $basketItem['PRODUCT_PROVIDER_CLASS'] = (string) $basketItem['PRODUCT_PROVIDER_CLASS'];
             if (strval(trim($basketItem['PRODUCT_PROVIDER_CLASS'])) !== '' || strval(trim($basketItem['CALLBACK_FUNC'])) !== '') {
                 $basketItem['MODULE'] = (string) $basketItem['MODULE'];
                 $basketItem['PRODUCT_ID'] = (int) $basketItem['PRODUCT_ID'];
                 $basketItem['QUANTITY'] = (double) $basketItem['QUANTITY'];
                 if ($productProvider = CSaleBasket::GetProductProvider($basketItem)) {
                     $fields = $productProvider::GetProductData(array("PRODUCT_ID" => $basketItem["PRODUCT_ID"], "QUANTITY" => $basketItem["QUANTITY"], "RENEWAL" => "N", "CHECK_COUPONS" => 'Y' == $basketItem['CAN_BUY'] && 'N' == $basketItem['DELAY'] ? 'Y' : 'N', "CHECK_DISCOUNT" => CSaleBasketHelper::isSetItem($basketItem) ? 'N' : 'Y', "BASKET_ID" => $basketItem["ID"], "NOTES" => $basketItem["NOTES"]));
                 } else {
                     $fields = CSaleBasket::ExecuteCallbackFunction($basketItem["CALLBACK_FUNC"], $basketItem["MODULE"], $basketItem["PRODUCT_ID"], $basketItem["QUANTITY"], "N");
                 }
                 if (!empty($fields) && is_array($fields)) {
                     if ($isOrderConverted == 'Y' && $basketItem['DELAY'] == 'N') {
                         if (!Sale\Compatible\DiscountCompatibility::isInited()) {
                             Sale\Compatible\DiscountCompatibility::init();
                         }
                         if (Sale\Compatible\DiscountCompatibility::usedByClient()) {
                             Sale\Compatible\DiscountCompatibility::setBasketItemData($basketItem['ID'], $fields);
                         }
                     }
                     $fields['CAN_BUY'] = 'Y';
                     $fields['TYPE'] = (int) $basketItem['TYPE'];
                     $fields['SET_PARENT_ID'] = (int) $basketItem['SET_PARENT_ID'];
                 } else {
                     $fields = array('CAN_BUY' => 'N');
                 }
             }
             if (array_key_exists('MEASURE_RATIO', $basketItem)) {
                 $basketItemQuantity = floatval($basketItem['QUANTITY']);
                 $basketItemRatio = floatval($basketItem['MEASURE_RATIO']);
                 $mod = roundEx($basketItemQuantity / $basketItemRatio - intval($basketItemQuantity / $basketItemRatio), 6);
                 if ($mod != 0) {
                     $fields['QUANTITY'] = floor(ceil($basketItemQuantity) / $basketItemRatio) * $basketItemRatio;
                 }
             }
             if (!empty($fields) && is_array($fields)) {
                 CSaleBasket::Update($basketItem['ID'], $fields);
             }
         }
     }
     return true;
 }
Пример #9
0
 /**
  * Save discount result.
  *
  * @return Result
  * @throws Main\ArgumentNullException
  */
 public function save()
 {
     $process = true;
     $result = new Result();
     $order = $this->getOrder();
     if ($order === null) {
         throw new Main\ArgumentNullException('order');
     }
     if (Compatible\DiscountCompatibility::isInited()) {
         $basket = $order->getBasket();
         if (!count($basket)) {
             return $result;
         }
         unset($basket);
         if (Compatible\DiscountCompatibility::isSaved()) {
             return $result;
         }
         $compatibleResult = Compatible\DiscountCompatibility::getResult();
         if ($compatibleResult === false) {
             return $result;
         }
         if (empty($compatibleResult)) {
             return $result;
         }
         $this->setUseMode($compatibleResult['CALCULATE']['USE_MODE']);
         $this->newOrder = $compatibleResult['CALCULATE']['NEW_ORDER'];
         $this->basketBasePrice = $compatibleResult['BASE_PRICE'];
         $this->discountsCache = $compatibleResult['DISCOUNT_LIST'];
         $this->couponsCache = $compatibleResult['COUPONS_LIST'];
         $this->discountResult = $compatibleResult['DISCOUNT_RESULT'];
         if ($this->newOrder) {
             $shipmentResult = $this->loadShipment();
             if (!$shipmentResult->isSuccess()) {
             }
             unset($shipmentResult);
             $this->fillShipmentData();
         }
         unset($compatibleResult);
     }
     if ($this->newOrder === null) {
         $process = false;
         $result->addError(new Main\Entity\EntityError(Loc::getMessage('BX_SALE_DISCOUNT_ERR_NEW_ORDER_IS_NULL'), self::ERROR_ID));
     }
     if ($this->getUseMode() === null) {
         return $result;
     }
     if ($process) {
         switch ($this->getUseMode()) {
             case self::USE_MODE_FULL:
                 $saveResult = $this->saveFull();
                 break;
             case self::USE_MODE_APPLY:
                 $saveResult = $this->saveApply();
                 break;
             case self::USE_MODE_MIXED:
                 $saveResult = $this->saveMixed();
                 break;
             default:
                 $saveResult = new Result();
                 $saveResult->addError(new Main\Entity\EntityError(Loc::getMessage('BX_SALE_DISCOUNT_ERR_BAD_USE_MODE'), self::ERROR_ID));
         }
         if (!$saveResult->isSuccess()) {
             $result->addErrors($saveResult->getErrors());
         }
         unset($saveResult);
     }
     if (Compatible\DiscountCompatibility::isInited()) {
         if ($result->isSuccess()) {
             Compatible\DiscountCompatibility::setSaved(true);
         }
     }
     return $result;
 }
Пример #10
0
 // saving
 if (strlen($errorMessage) <= 0) {
     $couponsMode = $ID > 0 ? DiscountCouponsManager::MODE_ORDER : DiscountCouponsManager::MODE_MANAGER;
     $couponsParams = array('userId' => $str_USER_ID);
     if ($ID > 0) {
         $couponsParams['orderId'] = $ID;
     }
     DiscountCouponsManager::init($couponsMode, $couponsParams, false);
     unset($couponsParams, $couponsMode);
     if ($isOrderConverted == 'Y') {
         $discountMode = $ID > 0 ? Sale\Compatible\DiscountCompatibility::MODE_ORDER : Sale\Compatible\DiscountCompatibility::MODE_MANAGER;
         $discountParams = array('SITE_ID' => $LID, 'CURRENCY' => $BASE_LANG_CURRENCY);
         if ($ID > 0) {
             $discountParams['ORDER_ID'] = $ID;
         }
         Sale\Compatible\DiscountCompatibility::init($discountMode, $discountParams);
         unset($discountParams, $discountMode);
     }
     //send new user mail
     if ($btnNewBuyer == "Y" && strlen($userNew) > 0) {
         CUser::SendUserInfo($str_USER_ID, $LID, $userNew, true);
     }
     $arShoppingCart = array();
     $arOrderProductPrice = fGetUserShoppingCart($_POST["PRODUCT"], $LID, $recalcOrder);
     foreach ($arOrderProductPrice as &$arItem) {
         $arItem["ID_TMP"] = $arItem["ID"];
         unset($arItem["ID"]);
     }
     unset($arItem);
     $tmpOrderId = $ID == 0 ? 0 : $ID;
     $arOrderOptions = array('CART_FIX' => isset($_REQUEST['CART_FIX']) && 'Y' == $_REQUEST['CART_FIX'] ? 'Y' : 'N');
Пример #11
0
            $basket->usePrepayment = isset($_POST["use_prepayment"]) && $_POST["use_prepayment"] == "Y" ? "Y" : "N";
            $res = $basket->recalculateBasket($_POST);
            foreach ($res as $key => $value) {
                $arRes[$key] = $value;
            }
            $arRes["BASKET_DATA"] = $basket->getBasketItems();
            $arRes["BASKET_DATA"]["GRID"]["HEADERS"] = $basket->getCustomColumns();
            $arRes["COLUMNS"] = $strColumns;
            $arRes["CODE"] = "SUCCESS";
            if (!empty($_POST["coupon"]) && $arRes['VALID_COUPON'] === true) {
                if (!empty($arRes['BASKET_DATA']['FULL_DISCOUNT_LIST'])) {
                    global $USER;
                    $userId = $USER instanceof CAllUser ? $USER->getId() : null;
                    $giftManager = \Bitrix\Sale\Discount\Gift\Manager::getInstance()->setUserId($userId);
                    \Bitrix\Sale\Compatible\DiscountCompatibility::stopUsageCompatible();
                    $collections = $giftManager->getCollectionsByBasket(\Bitrix\Sale\Basket::loadItemsForFUser(\Bitrix\Sale\Fuser::getId(), SITE_ID), $arRes['BASKET_DATA']['FULL_DISCOUNT_LIST'], $arRes['BASKET_DATA']['APPLIED_DISCOUNT_LIST']);
                    \Bitrix\Sale\Compatible\DiscountCompatibility::revertUsageCompatible();
                    if (count($collections)) {
                        $arRes['BASKET_DATA']['NEED_TO_RELOAD_FOR_GETTING_GIFTS'] = true;
                    }
                }
            }
        }
    }
}
unset($arRes['BASKET_DATA']['APPLIED_DISCOUNT_LIST'], $arRes['BASKET_DATA']['FULL_DISCOUNT_LIST']);
$arRes["PARAMS"]["QUANTITY_FLOAT"] = isset($_POST["quantity_float"]) && $_POST["quantity_float"] == "Y" ? "Y" : "N";
$APPLICATION->RestartBuffer();
header('Content-Type: application/json; charset=' . LANG_CHARSET);
echo CUtil::PhpToJSObject($arRes);
die;