47 if (!is_array(
$fields[
'PRODUCT'][
'PRICES']))
49 $this->addError(
new Error(
'Product prices are empty'));
54 $r = $this->modifyValidate(
$fields[
'PRODUCT'][
'PRICES']);
67 $this->addErrors($r->getErrors());
72 $ids = $r->getData()[0];
77 $entityTable::getList(
78 [
'filter' => [
'=ID' => $ids]]
100 if (!$r->isSuccess())
102 $this->addErrors($r->getErrors());
117 $price = $this->
get($id);
120 $this->addError(
new Error(
'Price is not exists'));
126 return $this->modifySingleProductPrice(
$fields);
137 return $this->modifySingleProductPrice(
$fields);
144 if (!$resultGroupType)
146 $this->addError(
new Error(
'Validate price error. Catalog price group is wrong'));
151 $entityTable = $this->getEntityTable();
153 $entityTable::getList(
156 '=PRODUCT_ID' =>
$fields[
'PRODUCT_ID'],
157 '=CATALOG_GROUP_ID' =>
$fields[
'CATALOG_GROUP_ID'],
164 $prices = array_combine(array_column($prices,
'ID'), $prices);
167 $r = $this->modifyValidate($prices);
178 if (!$r->isSuccess())
180 $this->addErrors($r->getErrors());
184 $ids = $r->getData()[0];
187 $this->getServiceItemName() => $this->
get($ids[0])
194 $productId =
$fields[
'PRODUCT'][
'ID'];
195 $prices =
$fields[
'PRODUCT'][
'PRICES'];
197 $r = $this->checkPermissionIBlockElementPriceModify($productId);
200 foreach ($prices as $price)
202 if(isset($price[
'ID']))
204 self::normalizeFields($price);
209 $ids[] = $price[
'ID'];
215 'PRODUCT_ID' => $productId,
216 'CATALOG_GROUP_ID' => $price[
'CATALOG_GROUP_ID'],
217 'CURRENCY' => $price[
'CURRENCY'],
218 'PRICE' => $price[
'PRICE'],
219 'QUANTITY_FROM' => isset($price[
'QUANTITY_FROM']) ? $price[
'QUANTITY_FROM']:
null,
220 'QUANTITY_TO' => isset($price[
'QUANTITY_TO']) ? $price[
'QUANTITY_TO']:
null,
221 'EXTRA_ID' => isset($price[
'EXTRA_ID']) ? $price[
'EXTRA_ID']:
null,
230 if(
$result->isSuccess() ==
false)
232 $r->addErrors(
$result->getErrors());
245 $productId =
$fields[
'PRODUCT'][
'ID'];
248 $prices =
$fields[
'PRODUCT'][
'PRICES'];
249 foreach ($prices as $price)
256 $res = $entityTable::getList([
'filter'=>[
'PRODUCT_ID'=>$productId]]);
257 while ($item =
$res->fetch())
259 if(in_array($item[
'ID'], $ids) ==
false)
261 $entityTable::delete($item[
'ID']);
270 if (isset(
$fields[
'QUANTITY_FROM']))
272 if (is_string(
$fields[
'QUANTITY_FROM']) &&
$fields[
'QUANTITY_FROM'] ===
'')
273 $fields[
'QUANTITY_FROM'] =
null;
275 $fields['QUANTITY_FROM'] = null;
279 $fields[
'QUANTITY_FROM'] =
null;
282 if (isset(
$fields[
'QUANTITY_TO']))
284 if (is_string(
$fields[
'QUANTITY_TO']) &&
$fields[
'QUANTITY_TO'] ===
'')
294 if (isset(
$fields[
'EXTRA_ID']))
307 private function modifyValidate(
array $items): Result
315 $extendPrices =
false;
318 $groupId = (int)
$fields[
'CATALOG_GROUP_ID'];
319 if (!isset($groupTypes[$groupId]))
321 $r->addError(
new Error(
'Validate price error. Catalog price group is wrong'));
328 $extendPrices = (isset(
$fields[
'QUANTITY_FROM']) || isset(
$fields[
'QUANTITY_TO']));
330 $sortedByType[$groupId][] =
$fields;
333 $allowEmptyRange = Option::get(
'catalog',
'save_product_with_empty_price_range') ===
'Y';
334 $enableQuantityRanges = Feature::isPriceQuantityRangesEnabled();
340 $r->addError(
new Error(
'Validate price error. Catalog product is allowed has only single price without ranges in price group.'));
346 if ($enableQuantityRanges ===
false)
348 $r->addError(
new Error(
'Validate price error. Price quantity ranges disabled'));
353 $basePrices = $sortedByType[$basePriceTypeId];
356 $r->addError(
new Error(
'Validate price error. Ranges of base price are not equal to another price group range'));
361 $basePrices = $this->sortPriceRanges($basePrices);
363 foreach ($sortedByType as $typeId => $prices)
366 $prices = $this->sortPriceRanges($prices);
368 foreach ($prices as
$i => $item)
370 $quantityFrom = (float)$item[
'QUANTITY_FROM'];
371 $quantityTo = (float)$item[
'QUANTITY_TO'];
374 $typeId !== $basePriceTypeId
376 !isset($basePrices[
$i])
377 || $quantityFrom !== (
float)$basePrices[
$i][
'QUANTITY_FROM']
378 || $quantityTo !== (
float)$basePrices[
$i][
'QUANTITY_TO']
384 'Validate price error. Ranges of base price are not equal to another price group range'
392 (
$i !== 0 && $quantityFrom <= 0)
393 || (
$i === 0 && $quantityFrom < 0)
398 "Quantity bounds error: lower bound {$quantityFrom} must be above zero (for the first range)"
404 (
$i !==
$count-1 && $quantityTo <= 0)
405 || (
$i ===
$count-1 && $quantityTo < 0)
410 "Quantity bounds error: higher bound {$quantityTo} must be above zero (for the last range)"
416 if ($quantityFrom > $quantityTo && (
$i !==
$count-1 || $quantityTo > 0))
420 "Quantity bounds error: range {$quantityFrom}-{$quantityTo} is incorrect"
425 $nextQuantityFrom = (float)$prices[
$i + 1][
"QUANTITY_FROM"];
426 $nextQuantityTo = (float)$prices[
$i + 1][
"QUANTITY_TO"];
431 "Quantity bounds error: ranges {$quantityFrom}-{$quantityTo} and {$nextQuantityFrom}-{$nextQuantityTo} overlap"
438 && $nextQuantityFrom - $quantityTo > 1
442 $validRangeFrom = $quantityTo + 1;
443 $validRangeTo = $nextQuantityFrom - 1;
447 "Invalid quantity range entry: no price is specified for range {$validRangeFrom}-{$validRangeTo})"
452 if (
$i >=
$count-1 && $quantityTo > 0)
456 "Invalid quantity range entry: no price is specified for quantity over {$quantityTo}"
466 private function sortPriceRanges(
array $prices):
array
472 for ($j =
$i + 1; $j <
$count; $j++)
474 if ($prices[
$i][
"QUANTITY_FROM"] > $prices[$j][
"QUANTITY_FROM"])
477 $prices[
$i] = $prices[$j];
501 if(
$name ==
'modify')
507 $r = parent::checkPermissionEntity(
$name);
517 if (!$this->accessController->check(ActionDictionary::ACTION_PRICE_EDIT))
531 !$this->accessController->check(ActionDictionary::ACTION_CATALOG_READ)
532 && !$this->accessController->check(ActionDictionary::ACTION_PRICE_EDIT)
542 private function checkPermissionIBlockElementPriceModify($productId)
546 $iblockId = \CIBlockElement::GetIBlockByID($productId);
547 if(!\CIBlockElementRights::UserHasRightTo(
$iblockId, $productId, self::IBLOCK_ELEMENT_EDIT_PRICE))
549 $r->addError(
new Error(
'Access Denied', 200040300030));
static getRowById($id, array $parameters=[])
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)