예제 #1
0
 function Update($ID, $arFields, $bDateUpdate = true)
 {
     global $DB;
     $ID = IntVal($ID);
     $arFields1 = array();
     foreach ($arFields as $key => $value) {
         if (substr($key, 0, 1) == "=") {
             $arFields1[substr($key, 1)] = $value;
             unset($arFields[$key]);
         }
     }
     if (!CSaleStoreBarcode::CheckFields("UPDATE", $arFields)) {
         return false;
     }
     $strUpdate = $DB->PrepareUpdate("b_sale_store_barcode", $arFields);
     foreach ($arFields1 as $key => $value) {
         if (strlen($strUpdate) > 0) {
             $strUpdate .= ", ";
         }
         $strUpdate .= $key . "=" . $value . " ";
     }
     $strSql = "UPDATE b_sale_store_barcode SET " . "\t" . $strUpdate . " ";
     if ($bDateUpdate) {
         $strSql .= ",\tDATE_MODIFY = " . $DB->GetNowFunction() . " ";
     }
     $strSql .= "WHERE ID = " . $ID . " ";
     $res = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
     return $ID;
 }
예제 #2
0
 /**
  * Method is called to deduct all products of the order or undo deduction
  *
  * @param int $orderID
  * @param bool $bUndoDeduction
  * @param int $recurringID
  * @param bool $bAutoDeduction
  * @param array $arStoreBarcodeOrderFormData
  * @return mixed array
  */
 function OrderDeduction($orderID, $bUndoDeduction = false, $recurringID = 0, $bAutoDeduction = true, $arStoreBarcodeOrderFormData = array())
 {
     global $APPLICATION;
     static $storesCount = NULL;
     static $bAutoDeductionAllowed = NULL;
     $bRealDeductionAllowed = true;
     $defaultDeductionStore = 0;
     $arSavedStoreBarcodeData = array();
     $arItems = array();
     $arResult = array();
     if (defined("SALE_DEBUG") && SALE_DEBUG) {
         CSaleHelper::WriteToLog("OrderDeduction: started", array("orderID" => $orderID, "bUndoDeduction" => intval($bUndoDeduction), "bAutoDeduction" => intval($bAutoDeduction), "arStoreBarcodeOrderFormData" => $arStoreBarcodeOrderFormData), "OD1");
     }
     //TODO - recurringID - ?
     $orderID = IntVal($orderID);
     if ($orderID <= 0) {
         $arResult["RESULT"] = false;
         return $arResult;
     }
     $dbBasketList = CSaleBasket::GetList(array(), array("ORDER_ID" => $orderID), false, false, array('ID', 'LID', 'PRODUCT_ID', 'PRODUCT_PROVIDER_CLASS', 'MODULE', 'BARCODE_MULTI', 'QUANTITY', 'RESERVED', 'TYPE', 'SET_PARENT_ID'));
     //check basket items and emulate deduction
     while ($arBasket = $dbBasketList->Fetch()) {
         if (CSaleBasketHelper::isSetParent($arBasket)) {
             continue;
         }
         if (defined("SALE_DEBUG") && SALE_DEBUG) {
             CSaleHelper::WriteToLog("Deducting product #" . $arBasket["PRODUCT_ID"], array(), "OD2");
         }
         /** @var $productProvider IBXSaleProductProvider */
         if ($productProvider = CSaleBasket::GetProductProvider($arBasket)) {
             if (is_null($storesCount)) {
                 $storesCount = intval($productProvider::GetStoresCount(array("SITE_ID" => $arBasket["LID"])));
             }
             if (defined("SALE_DEBUG") && SALE_DEBUG) {
                 CSaleHelper::WriteToLog("stores count: " . $storesCount, array(), "OD3");
             }
             if (is_null($bAutoDeductionAllowed)) {
                 $defaultDeductionStore = COption::GetOptionString("sale", "deduct_store_id", "", $arBasket["LID"]);
                 if ($storesCount == 1 || $storesCount == -1 || intval($defaultDeductionStore) > 0) {
                     // if stores' count = 1 or stores aren't used or default deduction store is defined
                     $bAutoDeductionAllowed = true;
                 } else {
                     $bAutoDeductionAllowed = false;
                 }
             }
             if (defined("SALE_DEBUG") && SALE_DEBUG) {
                 CSaleHelper::WriteToLog("auto deduction allowed: " . intval($bAutoDeductionAllowed), array(), "OD4");
             }
             if ($bAutoDeduction && !$bAutoDeductionAllowed && !$bUndoDeduction) {
                 if (defined("SALE_DEBUG") && SALE_DEBUG) {
                     CSaleHelper::WriteToLog("DDCT_AUTO_DEDUCT_WRONG_STORES_QUANTITY", array(), "OD5");
                 }
                 $APPLICATION->ThrowException(Loc::getMessage("DDCT_AUTO_DEDUCT_WRONG_STORES_QUANTITY"), "DDCT_WRONG_STORES_QUANTITY");
                 $bRealDeductionAllowed = false;
             } else {
                 if ($bAutoDeduction && $arBasket["BARCODE_MULTI"] == "Y" && !$bUndoDeduction) {
                     if (defined("SALE_DEBUG") && SALE_DEBUG) {
                         CSaleHelper::WriteToLog("DDCT_AUTO_DEDUCT_BARCODE_MULTI", array(), "OD6");
                     }
                     $APPLICATION->ThrowException(Loc::getMessage("DDCT_AUTO_DEDUCT_BARCODE_MULTI", array("#PRODUCT_ID#" => $arBasket["PRODUCT_ID"])), "DDCT_CANT_DEDUCT_BARCODE_MULTI");
                     $bRealDeductionAllowed = false;
                 } else {
                     //get saved store & barcode data if stores are used to know where to return products
                     if ($bUndoDeduction && $storesCount > 0) {
                         $dbStoreBarcode = CSaleStoreBarcode::GetList(array(), array("BASKET_ID" => $arBasket["ID"]), false, false, array("ID", "BASKET_ID", "BARCODE", "QUANTITY", "STORE_ID"));
                         while ($arStoreBarcode = $dbStoreBarcode->Fetch()) {
                             $arSavedStoreBarcodeData[$arBasket["ID"]][] = $arStoreBarcode;
                         }
                         if (defined("SALE_DEBUG") && SALE_DEBUG) {
                             CSaleHelper::WriteToLog("OrderDeduction: CSaleStoreBarcode data (stores) to return products to", array("arSavedStoreBarcodeData" => $arSavedStoreBarcodeData), "OD7");
                         }
                     }
                     $arFields = array("PRODUCT_ID" => $arBasket["PRODUCT_ID"], "EMULATE" => "Y", "PRODUCT_RESERVED" => $arBasket["RESERVED"], "UNDO_DEDUCTION" => $bUndoDeduction ? "Y" : "N");
                     if ($bUndoDeduction) {
                         if ($storesCount > 0) {
                             $arFields["QUANTITY"] = 0;
                             //won't be used during deduction
                             $arFields["STORE_DATA"] = $arSavedStoreBarcodeData[$arBasket["ID"]];
                         } else {
                             $arFields["QUANTITY"] = $arBasket["QUANTITY"];
                             $arFields["STORE_DATA"] = array();
                         }
                     } else {
                         if ($storesCount == 1) {
                             $arFields["QUANTITY"] = 0;
                             if ($bAutoDeduction) {
                                 if ($arProductStore = $productProvider::GetProductStores(array("PRODUCT_ID" => $arBasket["PRODUCT_ID"], "SITE_ID" => $arBasket["LID"], 'BASKET_ID' => $arBasket['ID']))) {
                                     $arFields["STORE_DATA"] = array("0" => array("STORE_ID" => $arProductStore[0]["STORE_ID"], "QUANTITY" => $arBasket["QUANTITY"], "AMOUNT" => $arProductStore[0]["AMOUNT"]));
                                 } else {
                                     $arFields["STORE_DATA"] = array();
                                 }
                             } else {
                                 $arFields["STORE_DATA"] = $arStoreBarcodeOrderFormData[$arBasket["ID"]];
                             }
                         } else {
                             if (intval($defaultDeductionStore) > 0) {
                                 $arFields["QUANTITY"] = 0;
                                 if ($bAutoDeduction) {
                                     if ($arProductStore = $productProvider::GetProductStores(array("PRODUCT_ID" => $arBasket["PRODUCT_ID"], "SITE_ID" => $arBasket["LID"], 'BASKET_ID' => $arBasket['ID']))) {
                                         foreach ($arProductStore as $storeData) {
                                             if ($storeData["STORE_ID"] == intval($defaultDeductionStore)) {
                                                 $arFields["STORE_DATA"] = array("0" => array("STORE_ID" => $storeData["STORE_ID"], "QUANTITY" => $arBasket["QUANTITY"], "AMOUNT" => $storeData["AMOUNT"]));
                                                 break;
                                             }
                                         }
                                     } else {
                                         $arFields["STORE_DATA"] = array();
                                     }
                                 } else {
                                     $arFields["STORE_DATA"] = $arStoreBarcodeOrderFormData[$arBasket["ID"]];
                                 }
                             } else {
                                 if ($storesCount > 1) {
                                     $arFields["QUANTITY"] = 0;
                                     //won't be used during deduction
                                     $arFields["STORE_DATA"] = $arStoreBarcodeOrderFormData[$arBasket["ID"]];
                                 } else {
                                     $arFields["QUANTITY"] = $arBasket["QUANTITY"];
                                     $arFields["STORE_DATA"] = array();
                                 }
                             }
                         }
                     }
                     if (defined("SALE_DEBUG") && SALE_DEBUG) {
                         CSaleHelper::WriteToLog("Emulating ::DeductProduct call", array("arFields" => $arFields), "OD7");
                     }
                     $eventParams = array('ORDER_ID' => $orderID, 'RECURRING_ID' => $recurringID, 'AUTO_DEDUCTION' => $bAutoDeduction, 'STORE_DATA' => $arStoreBarcodeOrderFormData);
                     foreach (GetModuleEvents('sale', 'OnBeforeBasketDeductProduct', true) as $event) {
                         if (ExecuteModuleEventEx($event, array($eventParams, $arBasket, &$arFields)) === false) {
                             if (defined("SALE_DEBUG") && SALE_DEBUG) {
                                 CSaleHelper::WriteToLog("Emulating ::DeductProduct call - error", array(), "OD7-1");
                             }
                             $arResult["RESULT"] = false;
                             return $arResult;
                         }
                     }
                     unset($eventParams);
                     //emulate deduction
                     $res = $productProvider::DeductProduct($arFields);
                     if ($res["RESULT"]) {
                         $arBasket["FIELDS"] = $arFields;
                         $arItems[] = $arBasket;
                         if (defined("SALE_DEBUG") && SALE_DEBUG) {
                             CSaleHelper::WriteToLog("Emulating ::DeductProduct call - success", array(), "OD8");
                         }
                     } else {
                         $bRealDeductionAllowed = false;
                         if (defined("SALE_DEBUG") && SALE_DEBUG) {
                             CSaleHelper::WriteToLog("Emulating ::DeductProduct call - error", array(), "OD9");
                         }
                     }
                 }
             }
             if ($ex = $APPLICATION->GetException()) {
                 $arResult["ERROR"]["MESSAGE"] = $ex->GetString();
                 $arResult["ERROR"]["CODE"] = $ex->GetID();
             }
             if (!$bRealDeductionAllowed) {
                 break;
             }
         }
     }
     // real deduction
     if ($bRealDeductionAllowed) {
         $bProductsDeductedSuccessfully = true;
         $arDeductedItems = array();
         foreach ($arItems as $arItem) {
             /** @var $productProvider IBXSaleProductProvider */
             if ($productProvider = CSaleBasket::GetProductProvider($arItem)) {
                 $arItem["FIELDS"]["EMULATE"] = "N";
                 if (defined("SALE_DEBUG") && SALE_DEBUG) {
                     CSaleHelper::WriteToLog("Call ::DeductProduct", array("fields" => $arItem["FIELDS"]), "OD10");
                 }
                 // finally real deduction
                 $res = $productProvider::DeductProduct($arItem["FIELDS"]);
                 if ($res["RESULT"]) {
                     $arDeductedItems[] = $arItem;
                     if (!$bUndoDeduction && $storesCount > 0) {
                         if ($bAutoDeduction) {
                             $arStoreBarcodeFields = array("BASKET_ID" => $arItem["ID"], "BARCODE" => "", "STORE_ID" => array_pop(array_keys($res["STORES"])), "QUANTITY" => $arItem["QUANTITY"], "CREATED_BY" => intval($GLOBALS["USER"]->GetID()) > 0 ? IntVal($GLOBALS["USER"]->GetID()) : "", "MODIFIED_BY" => intval($GLOBALS["USER"]->GetID()) > 0 ? IntVal($GLOBALS["USER"]->GetID()) : "");
                             if (defined("SALE_DEBUG") && SALE_DEBUG) {
                                 CSaleHelper::WriteToLog("Call CSaleStoreBarcode::Add (auto deduction = true)", array("arStoreBarcodeFields" => $arStoreBarcodeFields), "OD11");
                             }
                             CSaleStoreBarcode::Add($arStoreBarcodeFields);
                         }
                     }
                     if ($bUndoDeduction) {
                         $dbStoreBarcode = CSaleStoreBarcode::GetList(array(), array("BASKET_ID" => $arItem["ID"]), false, false, array("ID", "BASKET_ID"));
                         while ($arStoreBarcode = $dbStoreBarcode->GetNext()) {
                             CSaleStoreBarcode::Delete($arStoreBarcode["ID"]);
                         }
                     }
                     $tmpRes = $bUndoDeduction ? "N" : "Y";
                     CSaleBasket::Update($arItem["ID"], array("DEDUCTED" => $tmpRes));
                     // set parent deducted status
                     if ($bUndoDeduction) {
                         if (CSaleBasketHelper::isSetItem($arItem)) {
                             CSaleBasket::Update($arItem["SET_PARENT_ID"], array("DEDUCTED" => "N"));
                         }
                     } else {
                         if (CSaleBasketHelper::isSetItem($arItem) && CSaleBasketHelper::isSetDeducted($arItem["SET_PARENT_ID"])) {
                             CSaleBasket::Update($arItem["SET_PARENT_ID"], array("DEDUCTED" => "Y"));
                         }
                     }
                     if (defined("SALE_DEBUG") && SALE_DEBUG) {
                         CSaleHelper::WriteToLog("Call ::DeductProduct - Success (DEDUCTED = " . $tmpRes . ")", array(), "OD11");
                     }
                 } else {
                     CSaleBasket::Update($arItem["ID"], array("DEDUCTED" => "N"));
                     $bProductsDeductedSuccessfully = false;
                     if ($ex = $APPLICATION->GetException()) {
                         $arResult["ERROR"]["MESSAGE"] = $ex->GetString();
                         $arResult["ERROR"]["CODE"] = $ex->GetID();
                     }
                     if (defined("SALE_DEBUG") && SALE_DEBUG) {
                         CSaleHelper::WriteToLog("Call ::DeductProduct - Error (DEDUCTED = N)", array(), "OD12");
                     }
                     break;
                 }
             }
         }
         if ($bProductsDeductedSuccessfully) {
             $arResult["RESULT"] = true;
         } else {
             $arFields = array();
             foreach ($arDeductedItems as $arItem) {
                 /** @var $productProvider IBXSaleProductProvider */
                 if ($productProvider = CSaleBasket::GetProductProvider($arItem)) {
                     if ($storesCount > 0) {
                         $arFields = array("PRODUCT_ID" => $arItem["PRODUCT_ID"], "QUANTITY" => $arItem["QUANTITY"], "UNDO_DEDUCTION" => "Y", "EMULATE" => "N", "PRODUCT_RESERVED" => $arItem["FIELDS"]["PRODUCT_RESERVED"], "STORE_DATA" => $arItem["FIELDS"]["STORE_DATA"]);
                     } else {
                         $arFields = array("PRODUCT_ID" => $arItem["PRODUCT_ID"], "QUANTITY" => $arItem["QUANTITY"], "UNDO_DEDUCTION" => "Y", "PRODUCT_RESERVED" => $arItem["FIELDS"]["PRODUCT_RESERVED"], "EMULATE" => "N");
                     }
                     if (defined("SALE_DEBUG") && SALE_DEBUG) {
                         CSaleHelper::WriteToLog("Call ::DeductProduct - Revert deduction", array("storesCount" => $storesCount, "arFields" => $arFields), "OD13");
                     }
                     $res = $productProvider::DeductProduct($arFields);
                     if ($res["RESULT"]) {
                         CSaleBasket::Update($arItem["ID"], array("DEDUCTED" => "N"));
                         if (CSaleBasketHelper::isSetItem($arItem)) {
                             // todo - possibly not all the time, but once
                             CSaleBasket::Update($arItem["SET_PARENT_ID"], array("DEDUCTED" => "N"));
                         }
                     }
                 }
             }
             $arResult["RESULT"] = false;
         }
     } else {
         $arResult["RESULT"] = false;
     }
     if (defined("SALE_DEBUG") && SALE_DEBUG) {
         CSaleHelper::WriteToLog("OrderDeduction - result", array("arResult" => $arResult), "OD14");
     }
     return $arResult;
 }
예제 #3
0
 $arBasket["HAS_SAVED_BARCODES"] = false;
 /** @var $productProvider IBXSaleProductProvider */
 if ($productProvider = CSaleBasket::GetProductProvider($arBasket)) {
     $storeCount = $productProvider::GetStoresCount(array("SITE_ID" => $LID));
     if ($storeCount > 0) {
         if ($arProductStore = $productProvider::GetProductStores(array("PRODUCT_ID" => $arBasket["PRODUCT_ID"], "SITE_ID" => $LID, 'BASKET_ID' => $arBasket['ID']))) {
             foreach ($arProductStore as $arStore) {
                 $arBasket["STORES"][$arStore["STORE_ID"]] = $arStore;
             }
             if (!$useStores && $storeCount != -1) {
                 $useStores = true;
             }
             // if barcodes/store quantity are already saved for this product,
             // then check if barcodes are still valid and save them to the store array
             $ind = 0;
             $dbres = CSaleStoreBarcode::GetList(array(), array("BASKET_ID" => $arBasket["ID"]), false, false, array("ID", "BASKET_ID", "BARCODE", "STORE_ID", "ORDER_ID", "QUANTITY", "DEDUCTED"));
             while ($arRes = $dbres->GetNext()) {
                 $arCheckBarcodeFields = array("BARCODE" => $arRes["BARCODE"], "PRODUCT_ID" => $arBasket["PRODUCT_ID"], "ORDER_ID" => $arParams["ORDER_ID"]);
                 if ($arBasket["BARCODE_MULTI"] == "Y") {
                     $arCheckBarcodeFields["STORE_ID"] = $arRes["STORE_ID"];
                 }
                 if ($arRes["BARCODE"] == "") {
                     $res = true;
                 } else {
                     if ($arRes["DEDUCTED"] == "Y") {
                         $res = false;
                     } else {
                         $res = $productProvider::CheckProductBarcode($arCheckBarcodeFields);
                     }
                 }
                 if (is_array($arBasket["STORES"])) {
예제 #4
0
 /**
  * Method is called to deduct all products of the order
  *
  * @param int $orderID
  * @param bool $bUndoReservation
  * @return mixed array
  */
 function OrderDeduction($orderID, $bReturn = false, $recurringID = 0, $bAutoDeduction = true, $arStoreBarcodeOrderFormData = array())
 {
     global $DB;
     static $storesCount = NULL;
     static $bAutoDeductionAllowed = NULL;
     $bRealDeductionAllowed = true;
     $arSavedStoreBarcodeData = array();
     $arItems = array();
     $arResult = array();
     if (defined("SALE_DEBUG") && SALE_DEBUG) {
         CSaleHelper::WriteToLog("OrderDeduction: started", array("orderID" => $orderID, "bReturn" => intval($bReturn), "bAutoDeduction" => intval($bAutoDeduction), "arStoreBarcodeOrderFormData" => $arStoreBarcodeOrderFormData), "OD1");
     }
     //TODO - recurringID - ?
     $orderID = IntVal($orderID);
     if ($orderID <= 0) {
         $arResult["RESULT"] = false;
         return $arResult;
     }
     $dbBasketList = CSaleBasket::GetList(array(), array("ORDER_ID" => $orderID), false, false, array('ID', 'PRODUCT_ID', 'PRODUCT_PROVIDER_CLASS', 'MODULE', 'BARCODE_MULTI', 'QUANTITY', 'RESERVED'));
     //check and emulate deduction
     while ($arBasket = $dbBasketList->Fetch()) {
         if (defined("SALE_DEBUG") && SALE_DEBUG) {
             CSaleHelper::WriteToLog("Deducting product #" . $arBasket["PRODUCT_ID"], array(), "OD2");
         }
         /** @var $productProvider IBXSaleProductProvider */
         if ($productProvider = CSaleBasket::GetProductProvider($arBasket)) {
             if (is_null($storesCount)) {
                 $storesCount = intval($productProvider::GetStoresCount());
             }
             if (defined("SALE_DEBUG") && SALE_DEBUG) {
                 CSaleHelper::WriteToLog("stores count: " . $storesCount, array(), "OD3");
             }
             if (is_null($bAutoDeductionAllowed)) {
                 if ($storesCount == 1 || $storesCount == -1) {
                     // if stores' count = 1 or stores aren't used
                     $bAutoDeductionAllowed = true;
                 } else {
                     $bAutoDeductionAllowed = false;
                 }
             }
             if (defined("SALE_DEBUG") && SALE_DEBUG) {
                 CSaleHelper::WriteToLog("auto deduction allowed: " . intval($bAutoDeductionAllowed), array(), "OD4");
             }
             if ($bAutoDeduction && !$bAutoDeductionAllowed && !$bReturn) {
                 if (defined("SALE_DEBUG") && SALE_DEBUG) {
                     CSaleHelper::WriteToLog("DDCT_AUTO_DEDUCT_WRONG_STORES_QUANTITY", array(), "OD5");
                 }
                 $GLOBALS["APPLICATION"]->ThrowException(GetMessage("DDCT_AUTO_DEDUCT_WRONG_STORES_QUANTITY"), "DDCT_WRONG_STORES_QUANTITY");
                 $bRealDeductionAllowed = false;
             } else {
                 if ($bAutoDeduction && $arBasket["BARCODE_MULTI"] == "Y" && !$bReturn) {
                     if (defined("SALE_DEBUG") && SALE_DEBUG) {
                         CSaleHelper::WriteToLog("DDCT_AUTO_DEDUCT_BARCODE_MULTI", array(), "OD6");
                     }
                     $GLOBALS["APPLICATION"]->ThrowException(GetMessage("DDCT_AUTO_DEDUCT_BARCODE_MULTI", array("#PRODUCT_ID#" => $arBasket["PRODUCT_ID"])), "DDCT_CANT_DEDUCT_BARCODE_MULTI");
                     $bRealDeductionAllowed = false;
                 } else {
                     //get saved store & barcode data if stores are used to know where to return products
                     if ($bReturn && $storesCount > 0) {
                         $dbStoreBarcode = CSaleStoreBarcode::GetList(array(), array("BASKET_ID" => $arBasket["ID"]), false, false, array("ID", "BASKET_ID", "BARCODE", "QUANTITY", "STORE_ID"));
                         while ($arStoreBarcode = $dbStoreBarcode->Fetch()) {
                             $arSavedStoreBarcodeData[$arBasket["ID"]][] = $arStoreBarcode;
                         }
                         if (defined("SALE_DEBUG") && SALE_DEBUG) {
                             CSaleHelper::WriteToLog("OrderDeduction: CSaleStoreBarcode data (stores) to return products to", array("arSavedStoreBarcodeData" => $arSavedStoreBarcodeData), "OD7");
                         }
                     }
                     $arFields = array("PRODUCT_ID" => $arBasket["PRODUCT_ID"], "EMULATE" => "Y", "PRODUCT_RESERVED" => $arBasket["RESERVED"], "UNDO_DEDUCTION" => $bReturn ? "Y" : "N");
                     if ($bReturn) {
                         if ($storesCount > 0) {
                             $arFields["QUANTITY"] = 0;
                             //won't be used during deduction
                             $arFields["STORE_DATA"] = $arSavedStoreBarcodeData[$arBasket["ID"]];
                         } else {
                             $arFields["QUANTITY"] = $arBasket["QUANTITY"];
                             $arFields["STORE_DATA"] = array();
                         }
                     } else {
                         if ($storesCount == 1) {
                             $arFields["QUANTITY"] = 0;
                             if ($bAutoDeduction) {
                                 if ($arProductStore = $productProvider::GetProductStores(array("PRODUCT_ID" => $arBasket["PRODUCT_ID"]))) {
                                     $arFields["STORE_DATA"] = array("0" => array("STORE_ID" => $arProductStore[0]["STORE_ID"], "QUANTITY" => $arBasket["QUANTITY"], "AMOUNT" => $arProductStore[0]["AMOUNT"]));
                                 } else {
                                     $arFields["STORE_DATA"] = array();
                                 }
                             } else {
                                 $arFields["STORE_DATA"] = $arStoreBarcodeOrderFormData[$arBasket["ID"]];
                             }
                         } else {
                             if ($storesCount > 1) {
                                 $arFields["QUANTITY"] = 0;
                                 //won't be used during deduction
                                 $arFields["STORE_DATA"] = $arStoreBarcodeOrderFormData[$arBasket["ID"]];
                             } else {
                                 $arFields["QUANTITY"] = $arBasket["QUANTITY"];
                                 $arFields["STORE_DATA"] = array();
                             }
                         }
                     }
                     if (defined("SALE_DEBUG") && SALE_DEBUG) {
                         CSaleHelper::WriteToLog("Emulating call ::DeductProduct", array("arFields" => $arFields), "OD7");
                     }
                     //emulate deduction
                     $res = $productProvider::DeductProduct($arFields);
                     if ($res["RESULT"]) {
                         $arBasket["FIELDS"] = $arFields;
                         $arItems[] = $arBasket;
                         if (defined("SALE_DEBUG") && SALE_DEBUG) {
                             CSaleHelper::WriteToLog("Emulating call ::DeductProduct - success", array(), "OD8");
                         }
                     } else {
                         $bRealDeductionAllowed = false;
                         if (defined("SALE_DEBUG") && SALE_DEBUG) {
                             CSaleHelper::WriteToLog("Emulating call ::DeductProduct - error", array(), "OD9");
                         }
                     }
                 }
             }
             if ($ex = $GLOBALS["APPLICATION"]->GetException()) {
                 $arResult["ERROR"]["MESSAGE"] = $ex->GetString();
                 $arResult["ERROR"]["CODE"] = $ex->GetID();
             }
             if (!$bRealDeductionAllowed) {
                 break;
             }
         }
     }
     //real deduction
     if ($bRealDeductionAllowed) {
         $bProductsDeductedSuccessfully = true;
         $arDeductedItems = array();
         foreach ($arItems as $arItem) {
             /** @var $productProvider IBXSaleProductProvider */
             if ($productProvider = CSaleBasket::GetProductProvider($arItem)) {
                 $arItem["FIELDS"]["EMULATE"] = "N";
                 if (defined("SALE_DEBUG") && SALE_DEBUG) {
                     CSaleHelper::WriteToLog("Call ::DeductProduct", array("fields" => $arItem["FIELDS"]), "OD10");
                 }
                 //finally real deduction
                 $res = $productProvider::DeductProduct($arItem["FIELDS"]);
                 if ($res["RESULT"]) {
                     $arDeductedItems[] = $arItem;
                     if (!$bReturn && $storesCount > 0) {
                         if ($bAutoDeduction) {
                             $arStoreBarcodeFields = array("BASKET_ID" => $arItem["ID"], "BARCODE" => "", "STORE_ID" => array_pop(array_keys($res["STORES"])), "QUANTITY" => $arItem["QUANTITY"], "CREATED_BY" => intval($GLOBALS["USER"]->GetID()) > 0 ? IntVal($GLOBALS["USER"]->GetID()) : "", "MODIFIED_BY" => intval($GLOBALS["USER"]->GetID()) > 0 ? IntVal($GLOBALS["USER"]->GetID()) : "");
                             if (defined("SALE_DEBUG") && SALE_DEBUG) {
                                 CSaleHelper::WriteToLog("Call CSaleStoreBarcode::Add (auto deduction = true)", array("arStoreBarcodeFields" => $arStoreBarcodeFields), "OD11");
                             }
                             CSaleStoreBarcode::Add($arStoreBarcodeFields);
                         }
                         // else
                         // {
                         // 	foreach ($arStoreBarcodeOrderFormData as $id => $arRecord)
                         // 	{
                         // 		$arStoreBarcodeFields = array(
                         // 			"BASKET_ID"   => $arRecord["ID"],
                         // 			"BARCODE"     => ($arItem["BARCODE_MULTI"] == "Y") ? $arRecord["BARCODE"] : "",
                         // 			"STORE_ID"    => $arRecord["STORE_ID"],
                         // 			"QUANTITY"    => $arRecord["QUANTITY"],
                         // 			"CREATED_BY"  => ((intval($GLOBALS["USER"]->GetID())>0) ? IntVal($GLOBALS["USER"]->GetID()) : ""),
                         // 			"MODIFIED_BY" => ((intval($GLOBALS["USER"]->GetID())>0) ? IntVal($GLOBALS["USER"]->GetID()) : "")
                         // 		);
                         // 		if (defined("SALE_DEBUG") && SALE_DEBUG)
                         // 			CSaleHelper::WriteToLog("Call CSaleStoreBarcode::Add", array("arStoreBarcodeFields" => $arStoreBarcodeFields), "OD11");
                         // 		CSaleStoreBarcode::Add($arStoreBarcodeFields);
                         // 	}
                         // }
                     }
                     if (defined("SALE_DEBUG") && SALE_DEBUG) {
                         CSaleHelper::WriteToLog("Call ::DeductProduct - Success (DEDUCTED = Y)", array(), "OD11");
                     }
                     CSaleBasket::Update($arItem["ID"], array("DEDUCTED" => "Y"));
                     //CSaleOrder::AddOrderHistory
                 } else {
                     CSaleBasket::Update($arItem["ID"], array("DEDUCTED" => "N"));
                     $bProductsDeductedSuccessfully = false;
                     if ($ex = $GLOBALS["APPLICATION"]->GetException()) {
                         $arResult["ERROR"]["MESSAGE"] = $ex->GetString();
                         $arResult["ERROR"]["CODE"] = $ex->GetID();
                     }
                     if (defined("SALE_DEBUG") && SALE_DEBUG) {
                         CSaleHelper::WriteToLog("Call ::DeductProduct - Error (DEDUCTED = N)", array(), "OD12");
                     }
                     break;
                 }
             }
         }
         if ($bProductsDeductedSuccessfully) {
             $arResult["RESULT"] = true;
         } else {
             $arFields = array();
             foreach ($arDeductedItems as $arItem) {
                 /** @var $productProvider IBXSaleProductProvider */
                 if ($productProvider = CSaleBasket::GetProductProvider($arItem)) {
                     if ($storesCount > 0) {
                         $arFields = array("PRODUCT_ID" => $arItem["PRODUCT_ID"], "QUANTITY" => $arItem["QUANTITY"], "UNDO_DEDUCTION" => "Y", "EMULATE" => "N", "PRODUCT_RESERVED" => $arItem["FIELDS"]["PRODUCT_RESERVED"], "STORE_DATA" => $arItem["FIELDS"]["STORE_DATA"]);
                     } else {
                         $arFields = array("PRODUCT_ID" => $arItem["PRODUCT_ID"], "QUANTITY" => $arItem["QUANTITY"], "UNDO_DEDUCTION" => "Y", "PRODUCT_RESERVED" => $arItem["FIELDS"]["PRODUCT_RESERVED"], "EMULATE" => "N");
                     }
                     if (defined("SALE_DEBUG") && SALE_DEBUG) {
                         CSaleHelper::WriteToLog("Call ::DeductProduct - Revert deduction", array("storesCount" => $storesCount, "arFields" => $arFields), "OD13");
                     }
                     $res = $productProvider::DeductProduct($arFields);
                     if ($res["RESULT"]) {
                         CSaleBasket::Update($arItem["ID"], array("DEDUCTED" => "N"));
                     }
                 }
             }
             $arResult["RESULT"] = false;
         }
     } else {
         $arResult["RESULT"] = false;
     }
     if (defined("SALE_DEBUG") && SALE_DEBUG) {
         CSaleHelper::WriteToLog("OrderDeduction - result", array("arResult" => $arResult), "OD14");
     }
     return $arResult;
 }