105 $strAction = mb_strtoupper($strAction);
106 if (
'UPDATE' != $strAction &&
'ADD' != $strAction)
108 $intID = (int)$intID;
115 $clearFields =
array(
137 if ($strAction ==
'UPDATE')
138 $clearFields[] =
'CREATED_BY';
140 foreach ($clearFields as &$fieldName)
145 unset($fieldName, $clearFields);
156 $arMsg[] =
array(
'id' =>
'SITE_ID',
'text' =>
Loc::getMessage(
'BT_MOD_CAT_DSC_SV_ERR_EMPTY_SITE'));
164 $arMsg[] =
array(
'id' =>
'SITE_ID',
'text' =>
Loc::getMessage(
'BT_MOD_CAT_DSC_SV_ERR_BAD_SITE'));
171 $arMsg[] =
array(
'id' =>
'NAME',
'text' =>
Loc::getMessage(
'BT_MOD_CAT_DSC_SV_ERR_EMPTY_NAME'));
180 $arMsg[] =
array(
'id' =>
'CURRENCY',
'text' =>
Loc::getMessage(
'BT_MOD_CAT_DSC_SV_ERR_EMPTY_CURRENCY'));
183 if ((
is_set(
$arFields,
"ACTIVE_FROM") || $strAction==
"ADD") && (!
$DB->IsDate(
$arFields[
"ACTIVE_FROM"],
false, LANGUAGE_ID,
"FULL")))
199 $strCountPeriod = self::COUNT_TIME_ALL;
201 $strCountPeriod = self::COUNT_TIME_PERIOD;
203 $strCountPeriod = self::COUNT_TIME_INTERVAL;
204 $arFields[
'COUNT_PERIOD'] = $strCountPeriod;
214 $intUserID = (int)
$USER->GetID();
215 $strDateFunction =
$DB->GetNowFunction();
216 $arFields[
'~TIMESTAMP_X'] = $strDateFunction;
219 if (!array_key_exists(
'MODIFIED_BY',
$arFields) || (
int)
$arFields[
"MODIFIED_BY"] <= 0)
222 if (
'ADD' == $strAction)
224 $arFields[
'~DATE_CREATE'] = $strDateFunction;
236 $arMsg[] =
array(
'id' =>
'RANGES',
'text' =>
Loc::getMessage(
'BT_MOD_CAT_DSC_SV_ERR_EMPTY_RANGES'));
242 $arRangeList =
array();
243 foreach (
$arFields[
'RANGES'] as &$arRange)
245 if (!is_array($arRange) || empty($arRange))
247 $arMsg[] =
array(
'id' =>
'RANGES',
'text' =>
Loc::getMessage(
'BT_MOD_CAT_DSC_SV_ERR_BAD_RANGE'));
252 if (empty($arRange[
'TYPE']) || $arRange[
'TYPE'] != self::TYPE_FIX)
253 $arRange[
'TYPE'] = self::TYPE_PERCENT;
254 if (isset($arRange[
'VALUE']))
256 $arRange[
"VALUE"] = str_replace(
",",
".", $arRange[
"VALUE"]);
257 $arRange[
"VALUE"] = doubleval($arRange[
"VALUE"]);
258 if (!(0 < $arRange[
"VALUE"]))
260 $arMsg[] =
array(
'id' =>
'RANGES',
'text' =>
Loc::getMessage(
'BT_MOD_CAT_DSC_SV_ERR_BAD_RANGE_VALUE'));
263 elseif (self::TYPE_PERCENT == $arRange[
'TYPE'] && 100 < $arRange[
"VALUE"])
265 $arMsg[] =
array(
'id' =>
'RANGES',
'text' =>
Loc::getMessage(
'BT_MOD_CAT_DSC_SV_ERR_BAD_RANGE_VALUE'));
271 $arMsg[] =
array(
'id' =>
'RANGES',
'text' =>
Loc::getMessage(
'BT_MOD_CAT_DSC_SV_ERR_BAD_RANGE_VALUE'));
275 if (isset($arRange[
'RANGE_FROM']))
277 $arRange[
"RANGE_FROM"] = str_replace(
",",
".", $arRange[
"RANGE_FROM"]);
278 $arRange[
"RANGE_FROM"] = doubleval($arRange[
"RANGE_FROM"]);
279 if (0 > $arRange[
"RANGE_FROM"])
281 $arMsg[] =
array(
'id' =>
'RANGES',
'text' =>
Loc::getMessage(
'BT_MOD_CAT_DSC_SV_ERR_BAD_RANGE_FROM'));
286 if (in_array($arRange[
"RANGE_FROM"], $arRangeList))
292 $arRangeList[] = $arRange[
"RANGE_FROM"];
298 $arMsg[] =
array(
'id' =>
'RANGES',
'text' =>
Loc::getMessage(
'BT_MOD_CAT_DSC_SV_ERR_BAD_RANGE_FROM'));
307 $arMsg[] =
array(
'id' =>
'RANGES',
'text' =>
Loc::getMessage(
'BT_MOD_CAT_DSC_SV_ERR_DUP_RANGE_FROM'));
312 if (isset(
$arFields[
'GROUP_IDS']) || $strAction ==
'ADD')
319 foreach (
$arFields[
'GROUP_IDS'] as &$intGroupID)
321 $intGroupID = (int)$intGroupID;
322 if (0 < $intGroupID && 2 != $intGroupID)
323 $arValid[] = $intGroupID;
325 if (isset($intGroupID))
327 $arFields[
'GROUP_IDS'] = array_unique($arValid);
331 $arMsg[] =
array(
'id' =>
'GROUP_IDS',
'text' =>
Loc::getMessage(
'BT_MOD_CAT_DSC_SV_ERR_EMPTY_GROUP_IDS'));
345 $arFields[
'CONDITIONS'] = $cond->GetDefaultConditions();
447 $adminSection = (defined(
'ADMIN_SECTION') &&
ADMIN_SECTION ===
true);
454 foreach (
GetModuleEvents(
"catalog",
"OnGetDiscountSave",
true) as $arEvent)
457 if ($mxResult !==
true)
465 $arUserGroups =
array();
470 $arUserGroups =
$arParams[
'USER_GROUPS'];
474 if (self::GetDiscountUserID() > 0)
476 $intUserID = (int)self::GetDiscountUserID();
477 $arUserGroups =
array();
479 if ($intUserID <= 0 && !$adminSection)
481 $intUserID = (int)
$USER->GetID();
482 $arUserGroups =
array();
484 if (empty($arUserGroups))
486 if (!isset(self::$userGroups[$intUserID]))
487 self::$userGroups[$intUserID] =
$USER->GetUserGroup($intUserID);
488 $arUserGroups = self::$userGroups[$intUserID];
490 if (empty($arUserGroups) || !is_array($arUserGroups) || $intUserID <= 0)
492 $key = array_search(2, $arUserGroups);
494 unset($arUserGroups[
$key]);
495 if (empty($arUserGroups))
498 if (empty($arUserGroups))
500 if ($strSiteID ===
false)
502 $cacheKey = md5(
'U'.implode(
'_', $arUserGroups));
503 if (!isset(self::$discountFilterCache[$cacheKey]))
505 if (empty(self::$discountFilterCache[$cacheKey]))
508 $arCurrentDiscountID = self::$discountFilterCache[$cacheKey];
514 $arCurrentDiscountID = array_intersect($arCurrentDiscountID,
$arParams[
'ID']);
517 if (!empty($arCurrentDiscountID))
519 $getAll = ($getAll ===
true);
520 $cacheKey =
'DS'.implode(
'_', $arCurrentDiscountID).
'|'.$strSiteID;
521 if (!isset(self::$discountResultCache[$cacheKey]))
523 self::$discountResultCache[$cacheKey] =
array();
525 $intCurrentTime = microtime(
true);
526 $strDate = date(
$DB->DateFormatToPHP(CSite::GetDateFormat(
"FULL")), $intCurrentTime);
528 'ID' => $arCurrentDiscountID,
529 'SITE_ID' => $strSiteID,
530 'TYPE' => self::ENTITY_ID,
532 '+<=ACTIVE_FROM' => $strDate,
533 '+>=ACTIVE_TO' => $strDate
535 CTimeZone::Disable();
538 while ($arDiscSave = $rsDiscSaves->Fetch())
540 $arDiscSave[
'ACTION_SIZE'] = (int)$arDiscSave[
'ACTION_SIZE'];
541 $arDiscSave[
'COUNT_SIZE'] = (int)$arDiscSave[
'COUNT_SIZE'];
542 $arDiscSave[
'ACTIVE_FROM_UT'] =
false;
543 $arDiscSave[
'ACTIVE_TO_UT'] =
false;
544 $arDiscSave[
'COUNT_FROM_UT'] =
false;
545 $arDiscSave[
'COUNT_TO_UT'] =
false;
546 $arDiscSave[
'TYPE'] = (int)$arDiscSave[
'TYPE'];
547 $arDiscSave[
'MODULE_ID'] =
'catalog';
548 $arDiscSave[
'MICROTIME'] = $intCurrentTime;
549 $arDiscSave[
'LAST_DISCOUNT'] =
'N';
550 $arDiscSave[
'PRIORITY'] = 1;
552 self::$discountResultCache[$cacheKey][] = $arDiscSave;
554 unset($arDiscSave, $rsDiscSaves);
555 unset($intCurrentTime);
557 $discountList = self::$discountResultCache[$cacheKey];
559 foreach ($discountList as $arDiscSave)
561 $strCountPeriod = self::COUNT_TIME_ALL;
562 $strActionPeriod = self::ACTION_TIME_ALL;
563 $arCountPeriodBack =
array();
564 $arActionPeriodBack =
array();
565 $arActionPeriod =
array();
567 $arStartDate =
false;
568 $arOldOrderSumm =
false;
569 $arOrderSumm =
false;
570 $boolPeriodInsert =
true;
572 $intCountTime = $arDiscSave[
'MICROTIME'];
573 $arOrderFilter =
array(
574 'USER_ID' => $intUserID,
575 'LID' => $arDiscSave[
'SITE_ID'],
579 $arOldOrderFilter = $arOrderFilter;
581 if (!empty($arDiscSave[
'ACTIVE_FROM']) || !empty($arDiscSave[
'ACTIVE_TO']))
583 $strActionPeriod = self::ACTION_TIME_INTERVAL;
584 if (!empty($arDiscSave[
'ACTIVE_FROM']))
585 $arDiscSave[
'ACTIVE_FROM_UT'] =
MakeTimeStamp($arDiscSave[
'ACTIVE_FROM']);
586 if (!empty($arDiscSave[
'ACTIVE_TO']))
587 $arDiscSave[
'ACTIVE_TO_UT'] =
MakeTimeStamp($arDiscSave[
'ACTIVE_TO']);
589 elseif ($arDiscSave[
'ACTION_SIZE'] > 0 && in_array($arDiscSave[
'ACTION_TYPE'],
array(
'D',
'M',
'Y')))
591 $strActionPeriod = self::ACTION_TIME_PERIOD;
595 if (!empty($arDiscSave[
'COUNT_FROM']) || !empty($arDiscSave[
'COUNT_TO']))
597 $strCountPeriod = self::COUNT_TIME_INTERVAL;
598 if (!empty($arDiscSave[
'COUNT_FROM']))
599 $arDiscSave[
'COUNT_FROM_UT'] =
MakeTimeStamp($arDiscSave[
'COUNT_FROM']);
600 if (!empty($arDiscSave[
'COUNT_TO']))
602 $arDiscSave[
'COUNT_TO_UT'] =
MakeTimeStamp($arDiscSave[
'COUNT_TO']);
603 if ($arDiscSave[
'COUNT_TO_UT'] > $intCountTime)
605 $arDiscSave[
'COUNT_TO_UT'] = $intCountTime;
606 $arDiscSave[
'COUNT_TO'] = ConvertTimeStamp($intCountTime,
'FULL');
610 elseif ($arDiscSave[
'COUNT_SIZE'] > 0 && in_array($arDiscSave[
'COUNT_TYPE'],
array(
'D',
'M',
'Y')))
612 $strCountPeriod = self::COUNT_TIME_PERIOD;
616 if ($strCountPeriod == self::COUNT_TIME_INTERVAL)
618 if (
false !== $arDiscSave[
'COUNT_FROM_UT'])
620 if ($arDiscSave[
'COUNT_FROM_UT'] > $intCountTime)
622 if (
false !== $arDiscSave[
'COUNT_TO_UT'] && $arDiscSave[
'COUNT_TO_UT'] <= $arDiscSave[
'COUNT_FROM_UT'])
624 if (
false !== $arDiscSave[
'ACTIVE_TO_UT'] && $arDiscSave[
'COUNT_FROM_UT'] >= $arDiscSave[
'ACTIVE_TO_UT'])
627 if (
false !== $arDiscSave[
'COUNT_TO_UT'])
629 if ($strActionPeriod == self::ACTION_TIME_PERIOD && ($arDiscSave[
'COUNT_TO_UT'] <
AddToTimeStamp($arActionPeriodBack, $intCountTime)))
634 if ($strActionPeriod == self::ACTION_TIME_PERIOD)
636 if ($strCountPeriod == self::COUNT_TIME_PERIOD)
639 'DISCOUNT_ID' => $arDiscSave[
'ID'],
640 'USER_ID' => $intUserID,
641 'ACTIVE_FROM' => date(
$DB->DateFormatToPHP(CSite::GetDateFormat(
"FULL")),
AddToTimeStamp($arActionPeriodBack, $intCountTime)),
643 if (is_array($arStartDate) && !empty($arStartDate))
645 $arOldOrderFilter[
'<DATE_INSERT'] = $arStartDate[
'ACTIVE_FROM_FORMAT'];
646 $arOldOrderFilter[
'>=DATE_INSERT'] = date(
$DB->DateFormatToPHP(CSite::GetDateFormat(
"FULL")),
AddToTimeStamp($arCountPeriodBack,
MakeTimeStamp($arStartDate[
'ACTIVE_FROM_FORMAT'])));
655 'DISCOUNT_ID' => $arDiscSave[
'ID'],
656 'USER_ID' => $intUserID,
659 'ACTIVE_FROM' =>
false,
663 if (is_array($arStartDate) && !empty($arStartDate))
665 $intTimeStart =
MakeTimeStamp($arStartDate[
'ACTIVE_FROM_FORMAT']);
666 $intTimeFinish =
MakeTimeStamp($arStartDate[
'ACTIVE_TO_FORMAT']);
667 if (!($intTimeStart <= $intCountTime && $intTimeFinish >= $intCountTime))
673 $boolPeriodInsert =
false;
679 $intTimeStart =
false;
680 $intTimeFinish =
false;
682 if ($strCountPeriod == self::COUNT_TIME_INTERVAL)
684 $intTimeStart = (!empty($arDiscSave[
'COUNT_FROM']) ? $arDiscSave[
'COUNT_FROM'] :
false);
685 $intTimeFinish = (!empty($arDiscSave[
'COUNT_TO']) ? $arDiscSave[
'COUNT_TO'] :
false);
687 elseif ($strCountPeriod == self::COUNT_TIME_PERIOD)
689 $intTimeStart = date(
$DB->DateFormatToPHP(CSite::GetDateFormat(
"FULL")),
AddToTimeStamp($arCountPeriodBack, $intCountTime));
692 $arOrderFilter[
'>=DATE_INSERT'] = $intTimeStart;
694 $arOrderFilter[
'<DATE_INSERT'] = $intTimeFinish;
698 if (is_array($arOldOrderSumm) && 0 < $arOldOrderSumm[
'RANGE_SUMM'])
700 if ($arOrderSumm[
'RANGE_SUMM'] <= $arOldOrderSumm[
'RANGE_SUMM'])
702 $arOrderSumm = $arOldOrderSumm;
706 $arOldOrderSumm =
false;
711 array(
'RANGE_FROM' =>
'DESC'),
712 array(
'DISCOUNT_ID' => $arDiscSave[
'ID'],
'<=RANGE_FROM' => $arOrderSumm[
'RANGE_SUMM']),
714 array(
'nTopCount' => 1)
716 $arRange = $rsRanges->Fetch();
717 if (!empty($arRange) || $getAll)
719 if (!empty($arRange))
721 if ($strActionPeriod == self::ACTION_TIME_PERIOD)
723 if ($strCountPeriod == self::COUNT_TIME_PERIOD)
725 if (!is_array($arOldOrderSumm))
728 'DISCOUNT_ID' => $arDiscSave[
'ID'],
729 'USER_ID' => $intUserID,
730 'ACTIVE_FROM' => date(
$DB->DateFormatToPHP(CSite::GetDateFormat(
"FULL")),$intCountTime),
731 'ACTIVE_TO' => date(
$DB->DateFormatToPHP(CSite::GetDateFormat(
"FULL")),
AddToTimeStamp($arActionPeriod,$intCountTime)),
738 if ($boolPeriodInsert)
742 'DISCOUNT_ID' => $arDiscSave[
'ID'],
743 'USER_ID' => $intUserID,
744 'ACTIVE_FROM' => date(
$DB->DateFormatToPHP(CSite::GetDateFormat(
"FULL")),$intCountTime),
745 'ACTIVE_TO' => date(
$DB->DateFormatToPHP(CSite::GetDateFormat(
"FULL")),
AddToTimeStamp($arActionPeriod,$intCountTime)),
758 unset($arDiscSave[
'ACTIVE_FROM_UT'], $arDiscSave[
'ACTIVE_TO_UT'], $arDiscSave[
'COUNT_FROM_UT'], $arDiscSave[
'COUNT_TO_UT']);
759 unset($arDiscSave[
'MICROTIME']);
761 $arOneResult = $arDiscSave;
762 if (!empty($arRange))
764 $arOneResult[
'VALUE'] = $arRange[
'VALUE'];
765 $arOneResult[
'VALUE_TYPE'] = $arRange[
'TYPE'];
766 $arOneResult[
'RANGE_FROM'] = $arRange[
'RANGE_FROM'];
767 $arOneResult[
'MAX_DISCOUNT'] = 0;
771 $arOneResult[
'VALUE'] = 0;
772 $arOneResult[
'VALUE_TYPE'] = self::TYPE_PERCENT;
773 $arOneResult[
'MAX_DISCOUNT'] = 0;
775 array(
'RANGE_FROM' =>
'ASC'),
776 array(
'DISCOUNT_ID' => $arDiscSave[
'ID']),
778 array(
'nTopCount' => 1)
780 $arRange = $rsRanges->Fetch();
781 $arOneResult[
'NEXT_RANGE_FROM'] = $arRange[
'RANGE_FROM'];
782 $arOneResult[
'NEXT_VALUE'] = $arRange[
'VALUE'];
783 $arOneResult[
'NEXT_VALUE_TYPE'] = $arRange[
'TYPE'];
785 $arOneResult[
'SUMM'] = $arOrderSumm[
'SUMM'];
786 $arOneResult[
'SUMM_CURRENCY'] = $arOrderSumm[
'CURRENCY'];
787 $arOneResult[
'RANGE_SUMM'] = $arOrderSumm[
'RANGE_SUMM'];
788 $arOneResult[
'LAST_ORDER_DATE'] = $arOrderSumm[
'LAST_ORDER_DATE'];