43 $params = self::prepareExtraServicesParams($query, $server);
45 if (empty($params[
'DELIVERY_ID']))
47 throw new RestException(
'Parameter DELIVERY_ID is not defined', self::ERROR_CHECK_FAILURE);
50 if (empty($params[
'NAME']))
52 throw new RestException(
'Parameter NAME is not defined', self::ERROR_CHECK_FAILURE);
55 $extraServiceType = $params[
'TYPE'] ??
null;
56 if (!$extraServiceType)
58 throw new RestException(
'Parameter TYPE is not defined', self::ERROR_CHECK_FAILURE);
61 $extraServicesTypeMap = self::getExtraServicesTypeMap();
62 if (empty($extraServicesTypeMap[$extraServiceType]))
64 throw new RestException(
'Parameter TYPE is unknown', self::ERROR_CHECK_FAILURE);
67 if ($params[
'TYPE'] === self::ENUM_TYPE)
69 if (!isset($params[
'ITEMS']) || !is_array($params[
'ITEMS']) || empty($params[
'ITEMS']))
71 throw new RestException(
'Parameter ITEMS must be defined for enum type', self::ERROR_CHECK_FAILURE);
75 $deliveryService = Sale\Delivery\Services\Manager::getById($params[
'DELIVERY_ID']);
78 if (!self::hasAccessToDelivery($deliveryService, $params[
'APP_ID']))
85 throw new RestException(
'Delivery not found', self::ERROR_DELIVERY_NOT_FOUND);
88 if (!empty($params[
'CODE']))
90 $extraServiceData = Sale\Delivery\ExtraServices\Table::getList([
93 '=DELIVERY_ID' => $params[
'DELIVERY_ID'],
94 '=CODE' => $params[
'CODE'],
98 if ($extraServiceData)
101 'CODE "' . $params[
'CODE'] .
'" already exists for this delivery',
102 self::ERROR_CHECK_FAILURE
107 $fields = Sale\Delivery\ExtraServices\Manager::prepareParamsToSave([
108 'CLASS_NAME' => $extraServicesTypeMap[$params[
'TYPE']],
109 'DELIVERY_ID' => $params[
'DELIVERY_ID'],
110 'NAME' => $params[
'NAME'],
111 'ACTIVE' => (!empty($params[
'ACTIVE']) && $params[
'ACTIVE'] ===
'Y') ?
'Y' :
'N',
112 'CODE' => $params[
'CODE'] ??
'',
113 'DESCRIPTION' => $params[
'DESCRIPTION'] ??
'',
114 'PARAMS' => self::makeInternalParams($params[
'TYPE'], $params),
115 'SORT' => $params[
'SORT'] ? (int)$params[
'SORT'] : 100,
117 Sale\Delivery\ExtraServices\Manager::RIGHTS_ADMIN_IDX =>
'Y',
118 Sale\Delivery\ExtraServices\Manager::RIGHTS_MANAGER_IDX =>
'Y',
119 Sale\Delivery\ExtraServices\Manager::RIGHTS_CLIENT_IDX =>
'Y',
122 $result = Sale\Delivery\ExtraServices\Table::add($fields);
123 if ($result->isSuccess())
125 return $result->getId();
128 $error = implode(
"\n", $result->getErrorMessages());
129 throw new RestException($error, self::ERROR_EXTRA_SERVICE_ADD);
142 $params = self::prepareExtraServicesParams($query, $server);
144 if (empty($params[
'ID']))
146 throw new RestException(
'Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
149 $extraService = Sale\Delivery\ExtraServices\Table::getList([
150 'select' => [
'ID',
'CODE',
'DELIVERY_ID',
'CLASS_NAME'],
152 '=ID' => $params[
'ID'],
158 $deliveryService = Sale\Delivery\Services\Manager::getById($extraService[
'DELIVERY_ID']);
159 if ($deliveryService && !self::hasAccessToDelivery($deliveryService, $params[
'APP_ID']))
164 $newCode = $params[
'CODE'] ??
'';
167 $existingExtraService = Sale\Delivery\ExtraServices\Table::getList([
170 '=DELIVERY_ID' => $extraService[
'DELIVERY_ID'],
175 if ($existingExtraService && (
int)$existingExtraService[
'ID'] !== (
int)$params[
'ID'])
178 'FIELDS[CODE] "' . $newCode .
'" already exists for this delivery',
179 self::ERROR_CHECK_FAILURE
186 throw new RestException(
'Extra service not found', self::ERROR_EXTRA_SERVICE_NOT_FOUND);
189 $extraServicesTypeMap = array_flip(self::getExtraServicesTypeMap());
191 if (!isset($extraServicesTypeMap[$extraService[
'CLASS_NAME']]))
193 throw new RestException(
'Unknown type', self::ERROR_CHECK_FAILURE);
195 $type = $extraServicesTypeMap[$extraService[
'CLASS_NAME']];
199 if (!empty($params[
'NAME']))
201 $fields[
'NAME'] = $params[
'NAME'];
204 if (!empty($params[
'ACTIVE']))
206 $fields[
'ACTIVE'] = $params[
'ACTIVE'] ===
'Y' ?
'Y' :
'N';
209 if (isset($params[
'DESCRIPTION']))
211 $fields[
'DESCRIPTION'] = $params[
'DESCRIPTION'];
214 if (!empty($params[
'SORT']))
216 $fields[
'SORT'] = (int)$params[
'SORT'];
219 if (isset($params[
'CODE']))
221 $fields[
'CODE'] = $params[
'CODE'];
224 $internalParams = self::makeInternalParams($type, $params);
225 if (!is_null($internalParams))
227 $fields[
'PARAMS'] = $internalParams;
230 $fields = Sale\Delivery\ExtraServices\Manager::prepareParamsToSave($fields);
232 $result = Sale\Delivery\ExtraServices\Table::update($params[
'ID'], $fields);
233 if ($result->isSuccess())
238 $error = implode(
"\n", $result->getErrorMessages());
239 throw new RestException($error, self::ERROR_EXTRA_SERVICE_UPDATE);
251 $params = self::prepareExtraServicesParams($query, $server);
253 if (empty($params[
'ID']))
255 throw new RestException(
'Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
258 $extraService = Sale\Delivery\ExtraServices\Table::getById($params[
'ID'])->fetch();
261 $data = Sale\Delivery\Services\Manager::getById($extraService[
'DELIVERY_ID']);
262 if ($data && !self::hasAccessToDelivery($data, $params[
'APP_ID']))
269 throw new RestException(
'Extra service not found', self::ERROR_EXTRA_SERVICE_NOT_FOUND);
272 $result = Sale\Delivery\ExtraServices\Table::delete($params[
'ID']);
273 if ($result->isSuccess())
278 $error = implode(
"\n", $result->getErrorMessages());
279 throw new RestException($error, self::ERROR_EXTRA_SERVICE_DELETE);
288 public static function get($query, $n, \CRestServer $server): array
293 if (empty($params[
'DELIVERY_ID']))
295 throw new RestException(
'Parameter DELIVERY_ID is not defined', self::ERROR_CHECK_FAILURE);
298 $data = Sale\Delivery\Services\Manager::getById($params[
'DELIVERY_ID']);
301 throw new RestException(
'Delivery not found', self::ERROR_DELIVERY_NOT_FOUND);
304 $esClasses = Sale\Delivery\ExtraServices\Manager::getClassesList();
306 $extraServices = Sale\Delivery\ExtraServices\Table::getList([
308 '=DELIVERY_ID' => $params[
'DELIVERY_ID'],
309 '=CLASS_NAME' => $esClasses
311 'select' => [
'ID',
'CODE',
'NAME',
'DESCRIPTION',
'CLASS_NAME',
'ACTIVE',
'SORT',
'PARAMS'],
317 $extraServicesTypeMap = array_flip(self::getExtraServicesTypeMap());
319 foreach ($extraServices as $key => $extraService)
321 $type = $extraServicesTypeMap[$extraService[
'CLASS_NAME']];
322 $extraServices[$key][
'TYPE'] = $type;
323 unset($extraServices[$key][
'CLASS_NAME']);
325 if ($type === self::ENUM_TYPE)
327 if (array_key_exists(
'PRICES', $extraServices[$key][
'PARAMS']))
329 if (is_array($extraServices[$key][
'PARAMS'][
'PRICES']))
331 $extraServices[$key][
'ITEMS'] = [];
332 foreach ($extraServices[$key][
'PARAMS'][
'PRICES'] as $price)
334 $extraServices[$key][
'ITEMS'][] = [
335 'TITLE' => $price[
'TITLE'],
336 'CODE' => $price[
'CODE'],
337 'PRICE' => $price[
'PRICE'] ??
null,
343 elseif (in_array($type, [self::CHECKBOX_TYPE, self::QUANTITY_TYPE]))
345 if (array_key_exists(
'PRICE', $extraServices[$key][
'PARAMS']))
347 $extraServices[$key][
'PRICE'] = $extraServices[$key][
'PARAMS'][
'PRICE'];
350 unset($extraServices[$key][
'PARAMS']);
353 return $extraServices ?: [];