Пример #1
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;
 }
Пример #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
 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;
 }