/** * Update result user handlers for event OnGetOptimalPrice. * * @param array &$userResult Optimal price array. * @return void */ public static function updateUserHandlerOptimalPrice(&$userResult) { global $APPLICATION; if (empty($userResult) || !is_array($userResult)) { $userResult = false; return; } if (empty($userResult['PRICE']) || !is_array($userResult['PRICE'])) { $userResult = false; return; } if (empty($userResult['RESULT_PRICE']) || !is_array($userResult['RESULT_PRICE'])) { $resultCurrency = CCurrency::GetBaseCurrency(); if (empty($resultCurrency)) { $APPLICATION->ThrowException(Loc::getMessage('BT_MOD_CATALOG_PROD_ERR_NO_BASE_CURRENCY'), 'NO_BASE_CURRENCY'); $userResult = false; return; } if (self::$usedCurrency !== null) { $resultCurrency = self::$usedCurrency; } $oldDiscountExist = !empty($userResult['DISCOUNT']) && is_array($userResult['DISCOUNT']); if ($oldDiscountExist) { if (empty($userResult['DISCOUNT']['MODULE_ID'])) { $userResult['DISCOUNT']['MODULE_ID'] = 'catalog'; } if ($userResult['DISCOUNT']['CURRENCY'] != $resultCurrency) { Catalog\DiscountTable::convertCurrency($userResult['DISCOUNT'], $resultCurrency); } } if (!isset($userResult['DISCOUNT_LIST']) || !is_array($userResult['DISCOUNT_LIST'])) { $userResult['DISCOUNT_LIST'] = array(); if ($oldDiscountExist) { $userResult['DISCOUNT_LIST'][] = $userResult['DISCOUNT']; } } if (isset($userResult['DISCOUNT_LIST'])) { foreach ($userResult['DISCOUNT_LIST'] as &$discount) { if (empty($discount['MODULE_ID'])) { $discount['MODULE_ID'] = 'catalog'; } if ($discount['CURRENCY'] != $resultCurrency) { Catalog\DiscountTable::convertCurrency($discount, $resultCurrency); } } unset($discount); } $userResult['RESULT_PRICE'] = CCatalogDiscount::calculateDiscountList($userResult['PRICE'], $resultCurrency, $userResult['DISCOUNT_LIST'], self::$optimalPriceWithVat); } else { $userResult['RESULT_PRICE']['BASE_PRICE'] = roundEx($userResult['RESULT_PRICE']['BASE_PRICE'], CATALOG_VALUE_PRECISION); $userResult['RESULT_PRICE']['DISCOUNT'] = roundEx($userResult['RESULT_PRICE']['DISCOUNT'], CATALOG_VALUE_PRECISION); $userResult['RESULT_PRICE']['DISCOUNT_PRICE'] = $userResult['RESULT_PRICE']['BASE_PRICE'] - $userResult['RESULT_PRICE']['DISCOUNT']; $userResult['RESULT_PRICE']['VAT_RATE'] = $userResult['PRICE']['VAT_RATE']; } }
/** * Load discount data from db. * @param int $id Discount id. * @param array $discount Exist discount data. * @return bool|array */ protected static function loadFromDatabase($id, $discount) { $select = array(); if (!isset($discount['NAME'])) { $select['NAME'] = true; } if (empty($discount['CONDITIONS'])) { $select['CONDITIONS_LIST'] = true; } if (empty($discount['UNPACK'])) { $select['UNPACK'] = true; } if (empty($discount['USE_COUPONS'])) { $discount['USE_COUPONS'] = !empty($discount['COUPON']) ? 'Y' : 'N'; } if (!isset($discount['SORT'])) { $select['SORT'] = true; } if (!isset($discount['PRIORITY'])) { $select['PRIORITY'] = true; } if (!isset($discount['LAST_DISCOUNT'])) { $select['LAST_DISCOUNT'] = true; } if (!isset($discount['TYPE']) || $discount['TYPE'] != Catalog\DiscountTable::TYPE_DISCOUNT && $discount['TYPE'] != Catalog\DiscountTable::TYPE_DISCOUNT_SAVE) { $select['TYPE'] = true; } if (!isset($discount['VALUE_TYPE'])) { $select['VALUE_TYPE'] = true; $select['VALUE'] = true; $select['MAX_DISCOUNT'] = true; $select['CURRENCY'] = true; } else { if (!isset($discount['VALUE'])) { $select['VALUE'] = true; } if (!isset($discount['CURRENCY'])) { $select['CURRENCY'] = true; } if ($discount['VALUE_TYPE'] == Catalog\DiscountTable::VALUE_TYPE_PERCENT && !isset($discount['MAX_VALUE'])) { $select['MAX_DISCOUNT'] = true; } } $selectKeys = array_keys($select); if (!empty($select)) { $discountIterator = Catalog\DiscountTable::getList(array('select' => $selectKeys, 'filter' => array('=ID' => $id))); $loadData = $discountIterator->fetch(); if (empty($loadData)) { return false; } $discount = array_merge($loadData, $discount); if (isset($discount['CONDITIONS_LIST'])) { $discount['CONDITIONS'] = $discount['CONDITIONS_LIST']; unset($discount['CONDITIONS_LIST']); } if (isset($discount['MAX_DISCOUNT'])) { $discount['MAX_VALUE'] = $discount['MAX_DISCOUNT']; unset($discount['MAX_DISCOUNT']); } unset($loadData, $discountIterator); } $discount['DISCOUNT_ID'] = $id; if (empty($discount['MODULE_ID'])) { $discount['MODULE_ID'] = 'catalog'; } if (array_key_exists('HANDLERS', $discount)) { if (!empty($discount['HANDLERS']['MODULES']) && empty($discount['MODULES'])) { $discount['MODULES'] = $discount['HANDLERS']['MODULES']; } unset($discount['HANDLERS']); } if (empty($discount['MODULES'])) { $discount['MODULES'] = array(); $conn = Main\Application::getConnection(); $helper = $conn->getSqlHelper(); $moduleIterator = $conn->query('select MODULE_ID from ' . $helper->quote('b_catalog_discount_module') . ' where ' . $helper->quote('DISCOUNT_ID') . ' = ' . $id); while ($module = $moduleIterator->fetch()) { $discount['MODULES'][] = $module['MODULE_ID']; } unset($module, $moduleIterator, $helper, $conn); if (!in_array('catalog', $discount['MODULES'])) { $discount['MODULES'][] = 'catalog'; } } self::$typeCache[$id] = $discount['TYPE']; return $discount; }
/** * Check discount for convert. * * @param array &$discountData Discount data. * @return void * @throws Main\ArgumentException * @throws Main\LoaderException */ private static function checkMigrateDiscount(&$discountData) { if (self::$catalogIncluded === null) { self::$catalogIncluded = Main\Loader::includeModule('catalog'); } if (!self::$catalogIncluded) { return; } $coupon = $discountData['COUPON']; $hash = md5($discountData['DISCOUNT_ID'] . '|' . $discountData['NAME']); if (!isset(self::$catalogDiscountsCache[$hash])) { $discountIterator = Catalog\DiscountTable::getList(array('select' => array('*'), 'filter' => array('=ID' => $discountData['DISCOUNT_ID'], '=NAME' => $discountData['NAME']))); $existDiscount = $discountIterator->fetch(); unset($discountIterator); if (!empty($existDiscount)) { if ($existDiscount['NAME'] != $discountData['NAME']) { self::createEmptyDiscount($discountData); } else { if ($existDiscount['TYPE'] == Catalog\DiscountTable::TYPE_DISCOUNT_SAVE) { self::createEmptyDiscount($discountData, true); } else { $existDiscount['COUPON'] = $discountData['COUPON']; $discountData = self::executeDiscountProvider('catalog', $existDiscount); } } } else { self::createEmptyDiscount($discountData); } unset($existDiscount); self::$catalogDiscountsCache[$hash] = $discountData; } else { $discountData = self::$catalogDiscountsCache[$hash]; } $discountData['COUPON'] = $coupon; }