41 public function __construct($initParam, $currency =
"", $values = array(), array $additionalParams = array())
43 if(!is_array($initParam))
45 if(intval($initParam) <= 0)
48 $itemsParams = self::getExtraServicesList($initParam);
53 $itemsParams = $initParam;
56 if(empty($itemsParams))
59 if(!empty($this->items))
60 sortByColumn($itemsParams, array(
"SORT" => SORT_ASC,
"NAME" => SORT_ASC),
'',
null,
true);
62 foreach($itemsParams as $params)
64 if($currency ===
"" && !empty($params[
"CURRENCY"]))
65 $currency = $params[
"CURRENCY"];
67 $this->
addItem($params, $currency, isset($values[$params[
"ID"]]) ? $values[$params[
"ID"]] :
null, $additionalParams);
76 if(static::$classes ===
null)
77 self::initClassesList();
79 return static::$classes;
87 public static function initClassesList()
89 if(static::$classes !==
null)
93 '\Bitrix\Sale\Delivery\ExtraServices\Enum' =>
'lib/delivery/extra_services/enum.php',
94 '\Bitrix\Sale\Delivery\ExtraServices\Store' =>
'lib/delivery/extra_services/store.php',
95 '\Bitrix\Sale\Delivery\ExtraServices\Quantity' =>
'lib/delivery/extra_services/quantity.php',
96 '\Bitrix\Sale\Delivery\ExtraServices\Checkbox' =>
'lib/delivery/extra_services/checkbox.php'
99 \Bitrix\Main\Loader::registerAutoLoadClasses(
'sale', $classes);
100 Services\Manager::getHandlersList();
101 unset($classes[
'\Bitrix\Sale\Delivery\ExtraServices\Store']);
102 $event =
new Event(
'sale',
'onSaleDeliveryExtraServicesClassNamesBuildList');
104 $resultList = $event->getResults();
106 if (is_array($resultList) && !empty($resultList))
108 $customClasses = array();
110 foreach ($resultList as $eventResult)
113 if ($eventResult->getType() != EventResult::SUCCESS)
116 $params = $eventResult->getParameters();
118 if(!empty($params) && is_array($params))
119 $customClasses = array_merge($customClasses, $params);
122 if(!empty($customClasses))
124 \Bitrix\Main\Loader::registerAutoLoadClasses(
null, $customClasses);
125 $classes = array_merge($customClasses, $classes);
129 static::$classes = array_merge(array_keys($classes));
131 return static::$classes;
148 return (isset($this->items[$id]) ? $this->items[$id] :
null);
157 foreach($this->items as $item)
158 if($item->getCode() == $code)
172 foreach($this->items as $itemId => $item)
173 $result += $item->getCostShipment($shipment);
185 if(isset($params[
"RIGHTS"]))
188 (isset($params[
"RIGHTS"][self::RIGHTS_ADMIN_IDX]) ? $params[
"RIGHTS"][self::RIGHTS_ADMIN_IDX] :
"Y").
189 (isset($params[
"RIGHTS"][self::RIGHTS_MANAGER_IDX]) ? $params[
"RIGHTS"][self::RIGHTS_MANAGER_IDX] :
"Y").
190 (isset($params[
"RIGHTS"][self::RIGHTS_CLIENT_IDX]) ? $params[
"RIGHTS"][self::RIGHTS_CLIENT_IDX] :
"Y");
193 if(!isset($params[
"CLASS_NAME"]) || $params[
"CLASS_NAME"] ==
'' || !class_exists($params[
"CLASS_NAME"]))
196 if(!isset($params[
"ACTIVE"]))
197 $params[
"ACTIVE"] =
"Y";
199 if(isset($params[
"CLASS_NAME_DISABLED"]))
200 unset($params[
"CLASS_NAME_DISABLED"]);
202 if(is_callable($params[
"CLASS_NAME"].
"::prepareParamsToSave"))
203 $params = $params[
"CLASS_NAME"]::prepareParamsToSave($params);
221 if(!is_callable($className.
'::getAdminParamsControl'))
222 throw new SystemException(
'"'.$className.
'::getAdminParamsControl" does not exist!');
224 return $className::getAdminParamsControl($name, $params);
236 public function addItem($params, $currency, $value =
null, array $additionalParams = array())
238 if($params[
"CLASS_NAME"] ===
'' )
241 if(!isset($params[
"CLASS_NAME"]))
244 if(!class_exists($params[
"CLASS_NAME"]))
247 if(!is_subclass_of($params[
"CLASS_NAME"], Base::class))
249 throw new \Bitrix\Main\SystemException(
250 'Class "' . $params[
"CLASS_NAME"] .
'" is not a subclass of the \Bitrix\Sale\Delivery\ExtraServices\Base'
254 $item =
new $params[
"CLASS_NAME"]($params[
"ID"], $params, $currency, $value, $additionalParams);
255 $this->items[$params[
"ID"]] = $item;
256 return $params[
"ID"];
264 foreach($values as $eSrvId => $value)
266 $item = $this->
getItem($eSrvId);
269 $item->setValue($value);
279 foreach($this->items as $itemId => $item)
280 $item->setOperatingCurrency($currency);
292 if(intval($shipmentId) > 0 && intval($deliveryId) > 0)
294 $dbRes = ShipmentExtraServiceTable::getList(array(
296 '=SHIPMENT_ID' => $shipmentId,
297 '!=ID' => self::getStoresValueId($deliveryId)
301 while($row = $dbRes->fetch())
302 $result[$row[
"EXTRA_SERVICE_ID"]] = $row[
"VALUE"];
320 if(intval($shipmentId) <= 0)
325 $dbRes = ShipmentExtraServiceTable::getList(array(
327 '=SHIPMENT_ID' => $shipmentId
331 while($row = $dbRes->fetch())
332 $exist[$row[
"EXTRA_SERVICE_ID"]] = $row[
"ID"];
334 if(is_array($extraServices))
336 foreach($extraServices as $extraServiceId => $value)
338 if(array_key_exists($extraServiceId, $exist))
340 $res = ShipmentExtraServiceTable::update($exist[$extraServiceId], array(
"VALUE" => $value));
344 $res = ShipmentExtraServiceTable::add(array(
345 "EXTRA_SERVICE_ID" => $extraServiceId,
346 "SHIPMENT_ID" => $shipmentId,
351 if($res->isSuccess())
352 unset($exist[$extraServiceId]);
354 foreach($res->getErrors() as $error)
355 $result->addError($error);
360 foreach($exist as $extraServiceId => $value)
362 $res = ShipmentExtraServiceTable::delete($extraServiceId);
364 if(!$res->isSuccess())
365 foreach($res->getErrors() as $error)
366 $result->addError($error);
379 public static function getObjectsForShipment(
int $shipmentId,
int $deliveryId,
string $currency): array
383 $extraServiceValuesList = ShipmentExtraServiceTable::getList(
386 '=SHIPMENT_ID' => $shipmentId,
387 '!=ID' => self::getStoresValueId($deliveryId)
396 while ($extraServiceValue = $extraServiceValuesList->fetchObject())
398 $extraService = $extraServiceValue->getExtraService();
400 $className = $extraService->getClassName();
403 $extraServiceInstance =
new $className(
404 $extraService->getId(),
406 'NAME' => $extraService->getName(),
407 'CODE' => $extraService->getCode(),
408 'INIT_VALUE' => $extraService->getInitValue(),
409 'PARAMS' => $extraService->getParams()
412 $extraServiceValue->getValue()
415 if (!$extraServiceInstance instanceof
Base)
419 'Object is not of expected type: %s',
425 $result[$extraServiceValue[
'EXTRA_SERVICE_ID']] = $extraServiceInstance;
440 if(intval($shipmentId) > 0 && intval($deliveryId) > 0)
442 $storeFields = self::getStoresFields($deliveryId);
444 if(!empty($storeFields))
446 $dbRes = ShipmentExtraServiceTable::getList(array(
448 '=SHIPMENT_ID' => $shipmentId,
449 '=EXTRA_SERVICE_ID' => $storeFields[
'ID']
453 if($row = $dbRes->fetch())
454 $result = $row[
"VALUE"];
472 if(intval($shipmentId) <= 0)
477 if(intval($deliveryId) <= 0)
480 $storeFields = self::getStoresFields($deliveryId,
false);
482 if(isset($storeFields[
'ID']))
484 $dbRes = ShipmentExtraServiceTable::getList(array(
486 '=SHIPMENT_ID' => $shipmentId,
487 '=EXTRA_SERVICE_ID' => $storeFields[
'ID']
493 if($row = $dbRes->fetch())
494 $storeRowId = $row[
"ID"];
498 $res = ShipmentExtraServiceTable::update($storeRowId, array(
"VALUE" => $storeId));
502 $res = ShipmentExtraServiceTable::add(array(
503 "EXTRA_SERVICE_ID" => $storeFields[
'ID'],
504 "SHIPMENT_ID" => $shipmentId,
509 if(!$res->isSuccess())
510 foreach($res->getErrors() as $error)
511 $result->addError($error);
523 $fields = self::getStoresFields($deliveryId);
525 if(isset($fields[
"ID"]))
526 $result = $fields[
"ID"];
540 if(intval($deliveryId) <= 0)
543 $result = self::getExtraServicesList($deliveryId,
true);
547 && (empty($result[
'ACTIVE']) || $result[
'ACTIVE'] !==
'Y')
562 $stores = self::getStoresFields($deliveryId);
563 return isset($stores[
"PARAMS"][
"STORES"]) ? $stores[
"PARAMS"][
"STORES"] : array();
573 $storesFields = self::getStoresFields($deliveryId,
false);
575 if(empty($storesFields[
'ID']))
580 if($result->isSuccess())
581 unset(static::$cachedFields[$deliveryId][$storesFields[
'ID']]);
593 if(intval($deliveryId) <= 0)
596 $storesFields = self::getStoresFields($deliveryId);
598 if(empty($storesFields[
'ID']))
601 $result = Table::update(
608 if($result->isSuccess())
609 static::$cachedFields[$deliveryId][$storesFields[
'ID']][
"ACTIVE"] =
"N";
620 public static function saveStores($deliveryId, array $storesList)
623 $storesFields = self::getStoresFields($deliveryId,
false);
625 if(!empty($storesFields[
'ID']))
627 $res = Table::update(
632 "STORES" => $storesList
641 "CODE" => self::STORE_PICKUP_CODE,
643 "DESCRIPTION" =>
Loc::getMessage(
"DELIVERY_SERVICE_MANAGER_ES_DESCRIPTION"),
644 "CLASS_NAME" => self::STORE_PICKUP_CLASS,
645 "DELIVERY_ID" => $deliveryId,
649 "STORES" => $storesList
655 if(!$res->isSuccess())
656 $result->addErrors($res->getErrors());
669 if(intval($deliveryId) <= 0)
672 if(!isset(static::$cachedFields[$deliveryId]))
674 $srv = Services\Manager::getById($deliveryId);
676 if(!empty($srv[
'PARENT_ID']))
678 self::prepareData(array($deliveryId, $srv[
'PARENT_ID']));
679 static::$cachedFields[$deliveryId] = static::$cachedFields[$deliveryId] + static::$cachedFields[$srv[
'PARENT_ID']];
683 self::prepareData(array($deliveryId));
689 foreach(static::$cachedFields[$deliveryId] as $id => $es)
691 if($es[
'CLASS_NAME'] == self::STORE_PICKUP_CLASS)
714 if(empty($servicesIds))
717 foreach($servicesIds as $id)
719 $srv = Services\Manager::getById($id);
721 if(!empty($srv[
'PARENT_ID']) && !in_array($id, $servicesIds))
722 $servicesIds[] = $id;
725 $ids = array_diff($servicesIds, array_keys(static::$cachedFields));
727 $dbRes = Table::getList(array(
729 '=DELIVERY_ID' => $ids,
733 "=CLASS_NAME" => self::STORE_PICKUP_CLASS
740 "select" => array(
"*",
"CURRENCY" =>
"DELIVERY_SERVICE.CURRENCY")
743 while($es = $dbRes->fetch())
745 if(!isset(static::$cachedFields[$es[
'DELIVERY_ID']]))
746 static::$cachedFields[$es[
'DELIVERY_ID']] = array();
748 static::$cachedFields[$es[
'DELIVERY_ID']][$es[
"ID"]] = $es;
751 foreach($ids as $deliveryId)
752 if(!isset(static::$cachedFields[$deliveryId]))
753 static::$cachedFields[$deliveryId] = array();
764 if ($this->
isClone() && $cloneEntity->contains($this))
766 return $cloneEntity[$this];
769 $extraServiceClone = clone $this;
770 $extraServiceClone->isClone =
true;
772 if (!$cloneEntity->contains($this))
774 $cloneEntity[$this] = $extraServiceClone;
777 return $extraServiceClone;
785 return $this->isClone;
797 foreach($this->items as $itemId => $item)
798 $result += $item->getCost();
static loadMessages($file)
static getMessage($code, $replace=null, $language=null)