public function recalculateBasket($arPost) { global $USER; $arRes = array(); if ($this->hideCoupon != "Y") { if (!empty($arPost['delete_coupon'])) { $arRes['VALID_COUPON'] = DiscountCouponsManager::delete($arPost['delete_coupon']); } else { $oldUse = false; if (array_key_exists('coupon', $arPost)) { $arPost['coupon'] = trim((string) $arPost['coupon']); if ($arPost['coupon'] != '') { $arRes['VALID_COUPON'] = DiscountCouponsManager::add($arPost['coupon']); } else { $oldUse = true; } } if ($oldUse) { if (!isset($arRes["VALID_COUPON"]) || $arRes["VALID_COUPON"] === false) { DiscountCouponsManager::clear(true); } } } } $arTmpItems = array(); $dbItems = CSaleBasket::GetList(array("PRICE" => "DESC"), array("FUSER_ID" => CSaleBasket::GetBasketUserID(), "LID" => SITE_ID, "ORDER_ID" => "NULL"), false, false, array("ID", "NAME", "PRODUCT_PROVIDER_CLASS", "CALLBACK_FUNC", "MODULE", "PRODUCT_ID", "QUANTITY", "DELAY", "CAN_BUY", "CURRENCY", "SUBSCRIBE", "TYPE", "SET_PARENT_ID", "NOTES")); while ($arItem = $dbItems->Fetch()) { if (CSaleBasketHelper::isSetItem($arItem)) { continue; } $arTmpItems[] = $arItem; } if (!empty($arTmpItems)) { if (self::$catalogIncluded === null) { self::$catalogIncluded = Loader::includeModule('catalog'); } if (self::$catalogIncluded) { $arTmpItems = getRatio($arTmpItems); } foreach ($arTmpItems as $arItem) { $isFloatQuantity = isset($arItem["MEASURE_RATIO"]) && floatval($arItem["MEASURE_RATIO"]) > 0 && $arItem["MEASURE_RATIO"] != 1 || $this->quantityFloat == "Y" ? true : false; if (!isset($arPost["QUANTITY_" . $arItem["ID"]]) || floatval($arPost["QUANTITY_" . $arItem["ID"]]) <= 0) { $quantityTmp = $isFloatQuantity === true ? floatval($arItem["QUANTITY"]) : intval($arItem["QUANTITY"]); } else { $quantityTmp = $isFloatQuantity === true ? floatval($arPost["QUANTITY_" . $arItem["ID"]]) : intval($arPost["QUANTITY_" . $arItem["ID"]]); } $deleteTmp = $arPost["DELETE_" . $arItem["ID"]] == "Y" ? "Y" : "N"; $delayTmp = $arPost["DELAY_" . $arItem["ID"]] == "Y" ? "Y" : "N"; if ($arItem["CAN_BUY"] == "Y") { $res = $this->checkQuantity($arItem, $quantityTmp); if (!empty($res)) { $arRes["WARNING_MESSAGE"][] = $res["ERROR"]; } } if ($deleteTmp == "Y" && in_array("DELETE", $this->columns)) { if ($arItem["SUBSCRIBE"] == "Y" && is_array($_SESSION["NOTIFY_PRODUCT"][$USER->GetID()])) { unset($_SESSION["NOTIFY_PRODUCT"][$USER->GetID()][$arItem["PRODUCT_ID"]]); } CSaleBasket::Delete($arItem["ID"]); } elseif ($arItem["DELAY"] == "N" && $arItem["CAN_BUY"] == "Y") { $arFields = array(); if (in_array("QUANTITY", $this->columns)) { $arFields["QUANTITY"] = $quantityTmp; } if (in_array("DELAY", $this->columns)) { $arFields["DELAY"] = $delayTmp; } if (!empty($arFields) && ($arItem["QUANTITY"] != $arFields["QUANTITY"] && in_array("QUANTITY", $this->columns) || $arItem["DELAY"] != $arFields["DELAY"] && in_array("DELAY", $this->columns))) { CSaleBasket::Update($arItem["ID"], $arFields); } } elseif ($arItem["DELAY"] == "Y" && $arItem["CAN_BUY"] == "Y") { $arFields = array(); if (in_array("DELAY", $this->columns)) { $arFields["DELAY"] = $delayTmp; } if (!empty($arFields) && ($arItem["DELAY"] != $arFields["DELAY"] && in_array("DELAY", $this->columns))) { CSaleBasket::Update($arItem["ID"], $arFields); } } } } return $arRes; }
/** * @deprecated deprecated since catalog 15.0.4 * @see \Bitrix\Sale\DiscountCouponsManager * @param int $intUserID User id. * @param array $arModules Modules list. * @return bool */ public static function OnDeleteCouponList($intUserID, $arModules) { global $USER; $boolResult = false; if (empty($arModules) || is_array($arModules) && in_array('catalog', $arModules)) { $intUserID = (int) $intUserID; if (self::$existCouponsManager === null) { self::initCouponManager(); } if (self::$existCouponsManager) { if ($intUserID == DiscountCouponsManager::getUserId()) { return DiscountCouponsManager::clear(true); } return false; } else { if (0 < $intUserID) { $boolCurrentUser = $USER->IsAuthorized() && $intUserID == $USER->GetID(); $boolResult = self::ClearCouponsByManage($intUserID); if ($boolCurrentUser) { self::ClearCoupon(); } } elseif (0 == $intUserID && !$USER->IsAuthorized()) { self::ClearCoupon(); } } } return $boolResult; }
CSaleBasket::Update($arBasket["ID"], array("DELAY" => "N")); } } } } } } /* * coupons */ $COUPON = ""; if ($arParams["HIDE_COUPON"] != "Y" and isset($_REQUEST["COUPON"])) { if (isset($_REQUEST["COUPON"])) { $COUPON = (string) $_REQUEST['COUPON']; if ($COUPON === '') { DiscountCouponsManager::clear(true); } else { $arCoupons = array(); $cupons = explode(",", $COUPON); foreach ($cupons as $val) { $val = trim($val); if ($val != '') { $arCoupons[] = $val; } } if (!empty($arCoupons)) { foreach ($arCoupons as $oneCoupon) { DiscountCouponsManager::add($oneCoupon); } } }
/** * Save discount result for exist order. * * @return Result */ protected function saveApply() { $result = new Result(); $process = true; $order = $this->getOrder(); $orderId = $order->getId(); if ($this->convertedOrder) { return $result; } $deleteList = array(); $rulesIterator = Internals\OrderRulesTable::getList(array('select' => array('ID', 'ORDER_ID'), 'filter' => array('=ORDER_ID' => $orderId))); while ($rule = $rulesIterator->fetch()) { $rule['ID'] = (int) $rule['ID']; $deleteList[$rule['ID']] = $rule['ID']; } unset($rule, $rulesIterator); $rulesList = array(); if (!empty($this->discountResult['BASKET'])) { foreach ($this->discountResult['BASKET'] as $basketCode => $discountList) { foreach ($discountList as $discount) { if (!isset($discount['RULE_ID']) || (int) $discount['RULE_ID'] < 0) { $process = false; $result->addError(new Main\Entity\EntityError(Loc::getMessage('BX_SALE_DISCOUNT_ERR_EMPTY_RULE_ID_EXT_DISCOUNT'), self::ERROR_ID)); continue; } $orderDiscountId = $discount['DISCOUNT_ID']; $ruleData = array('RULE_ID' => $discount['RULE_ID'], 'APPLY' => $discount['RESULT']['APPLY'], 'DESCR_ID' => isset($discount['RULE_DESCR_ID']) ? (int) $discount['RULE_DESCR_ID'] : 0, 'DESCR' => $discount['RESULT']['DESCR_DATA']['BASKET']); if ($ruleData['DESCR_ID'] <= 0) { $ruleData['DESCR_ID'] = 0; $ruleData['MODULE_ID'] = $this->discountsCache[$orderDiscountId]['MODULE_ID']; $ruleData['ORDER_DISCOUNT_ID'] = $orderDiscountId; $ruleData['ORDER_ID'] = $orderId; } $rulesList[] = $ruleData; unset($ruleData); } unset($discount); } unset($basketCode, $discountList); } if (!empty($this->discountResult['ORDER'])) { foreach ($this->discountResult['ORDER'] as $discount) { $orderDiscountId = $discount['DISCOUNT_ID']; if (!empty($discount['RESULT']['BASKET'])) { foreach ($discount['RESULT']['BASKET'] as $basketCode => $applyData) { if (!isset($applyData['RULE_ID']) || (int) $applyData['RULE_ID'] < 0) { $process = false; $result->addError(new Main\Entity\EntityError(Loc::getMessage('BX_SALE_DISCOUNT_ERR_EMPTY_RULE_ID_SALE_DISCOUNT'), self::ERROR_ID)); continue; } $ruleData = array('RULE_ID' => $applyData['RULE_ID'], 'APPLY' => $applyData['APPLY'], 'DESCR_ID' => isset($applyData['RULE_DESCR_ID']) ? (int) $applyData['RULE_DESCR_ID'] : 0, 'DESCR' => $applyData['DESCR_DATA']); if ($ruleData['DESCR_ID'] <= 0) { $ruleData['DESCR_ID'] = 0; $ruleData['MODULE_ID'] = $this->discountsCache[$orderDiscountId]['MODULE_ID']; $ruleData['ORDER_DISCOUNT_ID'] = $orderDiscountId; $ruleData['ORDER_ID'] = $orderId; } $rulesList[] = $ruleData; unset($ruleData); } unset($basketCode, $applyData); } if (!empty($discount['RESULT']['DELIVERY'])) { if (!isset($discount['RESULT']['DELIVERY']['RULE_ID']) || (int) $discount['RESULT']['DELIVERY']['RULE_ID'] < 0) { $process = false; $result->addError(new Main\Entity\EntityError(Loc::getMessage('BX_SALE_DISCOUNT_ERR_EMPTY_RULE_ID_SALE_DISCOUNT'), self::ERROR_ID)); continue; } $ruleData = array('RULE_ID' => $discount['RESULT']['DELIVERY']['RULE_ID'], 'APPLY' => $discount['RESULT']['DELIVERY']['APPLY'], 'DESCR_ID' => isset($discount['RESULT']['DELIVERY']['RULE_DESCR_ID']) ? (int) $discount['RESULT']['DELIVERY']['RULE_DESCR_ID'] : 0, 'DESCR' => $discount['RESULT']['DELIVERY']['DESCR_DATA']); if ($ruleData['DESCR_ID'] <= 0) { $ruleData['DESCR_ID'] = 0; $ruleData['MODULE_ID'] = $this->discountsCache[$orderDiscountId]['MODULE_ID']; $ruleData['ORDER_DISCOUNT_ID'] = $orderDiscountId; $ruleData['ORDER_ID'] = $orderId; } $rulesList[] = $ruleData; unset($ruleData); } } unset($discount); } if ($process && !empty($rulesList)) { foreach ($rulesList as &$ruleRow) { $ruleResult = Internals\OrderRulesTable::update($ruleRow['RULE_ID'], array('APPLY' => $ruleRow['APPLY'])); if ($ruleResult->isSuccess()) { if (isset($deleteList[$ruleRow['RULE_ID']])) { unset($deleteList[$ruleRow['RULE_ID']]); } if ($ruleRow['DESCR_ID'] > 0) { $descrResult = Internals\OrderRulesDescrTable::update($ruleRow['DESCR_ID'], array('DESCR' => $ruleRow['DESCR'])); } else { $descrData = array('DESCR' => $ruleRow['DESCR'], 'MODULE_ID' => $ruleRow['MODULE_ID'], 'ORDER_DISCOUNT_ID' => $ruleRow['ORDER_DISCOUNT_ID'], 'ORDER_ID' => $ruleRow['ORDER_ID']); $descrResult = Internals\OrderRulesDescrTable::add($descrData); } if (!$descrResult->isSuccess(true)) { $process = false; $result->addError(new Main\Entity\EntityError(Loc::getMessage('BX_SALE_DISCOUNT_ERR_SAVE_APPLY_RULES'), self::ERROR_ID)); unset($descrResult); break; } unset($descrResult); } else { $process = false; $result->addError(new Main\Entity\EntityError(Loc::getMessage('BX_SALE_DISCOUNT_ERR_SAVE_APPLY_RULES'), self::ERROR_ID)); unset($ruleResult); break; } unset($ruleResult); } unset($ruleRow); } if ($process && !empty($deleteList)) { $conn = Main\Application::getConnection(); $helper = $conn->getSqlHelper(); $ruleRows = array_chunk($deleteList, 500); $mainQuery = 'delete from ' . $helper->quote(Internals\OrderRulesTable::getTableName()) . ' where ' . $helper->quote('ID') . ' in ('; $descrQuery = 'delete from ' . $helper->quote(Internals\OrderRulesDescrTable::getTableName()) . ' where ' . $helper->quote('RULE_ID') . ' in ('; foreach ($ruleRows as &$row) { $conn->queryExecute($mainQuery . implode(', ', $row) . ')'); $conn->queryExecute($descrQuery . implode(', ', $row) . ')'); } unset($row, $descrQuery, $mainQuery, $ruleRows); unset($helper, $conn); } unset($deleteList); if ($process) { if (DiscountCouponsManager::usedByManager()) { DiscountCouponsManager::clear(true); } } return $result; }