Example #1
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;
 }