- Модуль: sale
- Путь к файлу: ~/bitrix/modules/sale/lib/internals/discountcoupon.php
- Класс: BitrixSaleInternalsDiscountCouponTable
- Вызов: DiscountCouponTable::saveApplied
static function saveApplied($coupons, $userId, MainTypeDateTime $currentTime)
{
$currentTimestamp = $currentTime->getTimestamp();
if ($userId === null || (int)$userId == 0)
return false;
$userId = (int)$userId;
if (!is_array($coupons))
$coupons = array($coupons);
if (empty($coupons))
return false;
MainTypeCollection::normalizeArrayValuesByInt($coupons);
if (empty($coupons))
return false;
$errorList = [];
$deactivateCoupons = array();
$incrementalCoupons = array();
$limitedCoupons = array();
$couponIterator = self::getList(array(
'select' => array(
'ID', 'COUPON', 'DISCOUNT_ID', 'TYPE', 'ACTIVE', 'MAX_USE', 'USE_COUNT', 'USER_ID', 'ACTIVE_TO', 'ACTIVE_FROM',
'DISCOUNT_ACTIVE' => 'DISCOUNT.ACTIVE',
'DISCOUNT_ACTIVE_FROM' => 'DISCOUNT.ACTIVE_FROM', 'DISCOUNT_ACTIVE_TO' => 'DISCOUNT.ACTIVE_TO'
),
'filter' => array('@ID' => $coupons),
'order' => array('ID' => 'ASC')
));
while ($existCoupon = $couponIterator->fetch())
{
$couponCode = $existCoupon['COUPON'];
if ($existCoupon['DISCOUNT_ACTIVE'] != 'Y')
{
$errorList[$couponCode] = Loc::getMessage('DISCOUNT_COUPON_SAVE_ERROR_DISCOUNT_INACTIVE');
continue;
}
if (
($existCoupon['DISCOUNT_ACTIVE_FROM'] instanceof MainTypeDateTime && $existCoupon['DISCOUNT_ACTIVE_FROM']->getTimestamp() > $currentTimestamp)
||
($existCoupon['DISCOUNT_ACTIVE_TO'] instanceof MainTypeDateTime && $existCoupon['DISCOUNT_ACTIVE_TO']->getTimestamp() < $currentTimestamp)
)
{
$errorList[$couponCode] = Loc::getMessage('DISCOUNT_COUPON_SAVE_ERROR_DISCOUNT_WRONG_ACTIVE_PERIOD');
continue;
}
$existCoupon['USER_ID'] = (int)$existCoupon['USER_ID'];
if ($existCoupon['USER_ID'] > 0 && $existCoupon['USER_ID'] != $userId)
{
$errorList[$couponCode] = Loc::getMessage('DISCOUNT_COUPON_SAVE_ERROR_WRONG_USER_COUPON');
continue;
}
if (
($existCoupon['ACTIVE_FROM'] instanceof MainTypeDateTime && $existCoupon['ACTIVE_FROM']->getTimestamp() > $currentTimestamp)
||
($existCoupon['ACTIVE_TO'] instanceof MainTypeDateTime && $existCoupon['ACTIVE_TO']->getTimestamp() < $currentTimestamp)
)
{
$errorList[$couponCode] = Loc::getMessage('DISCOUNT_COUPON_SAVE_ERROR_COUPON_WRONG_ACTIVE_PERIOD');
continue;
}
if ($existCoupon['ACTIVE'] != 'Y')
{
switch ($existCoupon['TYPE'])
{
case self::TYPE_BASKET_ROW:
$errorList[$couponCode] = Loc::getMessage('DISCOUNT_COUPON_SAVE_ERROR_COUPON_BASKET_ROW_INACTIVE');
break;
case self::TYPE_ONE_ORDER:
$errorList[$couponCode] = Loc::getMessage('DISCOUNT_COUPON_SAVE_ERROR_COUPON_ONE_ORDER_INACTIVE');
break;
default:
$errorList[$couponCode] = Loc::getMessage('DISCOUNT_COUPON_SAVE_ERROR_COUPON_INACTIVE');
break;
}
continue;
}
if (
$existCoupon['TYPE'] == self::TYPE_BASKET_ROW
|| $existCoupon['TYPE'] == self::TYPE_ONE_ORDER
)
{
$deactivateCoupons[$couponCode] = $existCoupon['ID'];
}
elseif ($existCoupon['TYPE'] == self::TYPE_MULTI_ORDER)
{
$existCoupon['MAX_USE'] = (int)$existCoupon['MAX_USE'];
$existCoupon['USE_COUNT'] = (int)$existCoupon['USE_COUNT'];
if ($existCoupon['MAX_USE'] > 0 && $existCoupon['USE_COUNT'] >= $existCoupon['MAX_USE'])
{
$errorList[$couponCode] = Loc::getMessage('DISCOUNT_COUPON_SAVE_ERROR_COUPON_MAX_USE_LIMIT');
continue;
}
if ($existCoupon['MAX_USE'] > 0 && $existCoupon['USE_COUNT'] >= ($existCoupon['MAX_USE'] - 1))
{
$limitedCoupons[$existCoupon['COUPON']] = $existCoupon['ID'];
}
else
{
$incrementalCoupons[$existCoupon['COUPON']] = $existCoupon['ID'];
}
}
}
unset($existCoupon, $couponIterator);
if (!empty($errorList))
{
return [
'STATUS' => false,
'ERROR' => $errorList,
'DEACTIVATE' => 0,
'LIMITED' => 0,
'INCREMENT' => 0
];
}
if (!empty($deactivateCoupons) || !empty($limitedCoupons) || !empty($incrementalCoupons))
{
$conn = Application::getConnection();
$helper = $conn->getSqlHelper();
$tableName = $helper->quote(self::getTableName());
if (!empty($deactivateCoupons))
{
$conn->queryExecute(
'update '.$tableName.' set '.$helper->quote('ACTIVE').' = 'N', '.$helper->quote('DATE_APPLY').' = '.$helper->getCurrentDateTimeFunction().
' where '.$helper->quote('ID').' in ('.implode(',', $deactivateCoupons).')'
);
}
if (!empty($incrementalCoupons))
{
$conn->queryExecute(
'update '.$tableName.' set '.$helper->quote('DATE_APPLY').' = '.$helper->getCurrentDateTimeFunction().', '.
$helper->quote('USE_COUNT').' = '.$helper->quote('USE_COUNT').' + 1'.
' where '.$helper->quote('ID').' in ('.implode(',', $incrementalCoupons).')'
);
}
if (!empty($limitedCoupons))
{
$conn->queryExecute(
'update '.$tableName.' set '.$helper->quote('DATE_APPLY').' = '.$helper->getCurrentDateTimeFunction().', '.
$helper->quote('ACTIVE').' = 'N', '.$helper->quote('USE_COUNT').' = '.$helper->quote('USE_COUNT').' + 1'.
' where '.$helper->quote('ID').' in ('.implode(',', $limitedCoupons).')'
);
}
unset($tableName, $helper);
}
return [
'STATUS' => true,
'ERROR_LIST' => [],
'DEACTIVATE' => $deactivateCoupons,
'LIMITED' => $limitedCoupons,
'INCREMENT' => $incrementalCoupons
];
}