Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
extraservicesservice.php
1<?php
2
4
9
10if (!Main\Loader::includeModule('rest'))
11{
12 return;
13}
14
20{
21 private const ERROR_EXTRA_SERVICE_ADD = 'ERROR_EXTRA_SERVICE_ADD';
22 private const ERROR_EXTRA_SERVICE_UPDATE = 'ERROR_EXTRA_SERVICE_UPDATE';
23 private const ERROR_EXTRA_SERVICE_DELETE = 'ERROR_EXTRA_SERVICE_DELETE';
24
25 private const ERROR_CHECK_FAILURE = 'ERROR_CHECK_FAILURE';
26 private const ERROR_EXTRA_SERVICE_NOT_FOUND = 'ERROR_EXTRA_SERVICE_NOT_FOUND';
27 private const ERROR_DELIVERY_NOT_FOUND = 'ERROR_DELIVERY_NOT_FOUND';
28
29 private const ENUM_TYPE = 'enum';
30 private const CHECKBOX_TYPE = 'checkbox';
31 private const QUANTITY_TYPE = 'quantity';
32
40 public static function addExtraServices($query, $n, \CRestServer $server)
41 {
43 $params = self::prepareExtraServicesParams($query, $server);
44
45 if (empty($params['DELIVERY_ID']))
46 {
47 throw new RestException('Parameter DELIVERY_ID is not defined', self::ERROR_CHECK_FAILURE);
48 }
49
50 if (empty($params['NAME']))
51 {
52 throw new RestException('Parameter NAME is not defined', self::ERROR_CHECK_FAILURE);
53 }
54
55 $extraServiceType = $params['TYPE'] ?? null;
56 if (!$extraServiceType)
57 {
58 throw new RestException('Parameter TYPE is not defined', self::ERROR_CHECK_FAILURE);
59 }
60
61 $extraServicesTypeMap = self::getExtraServicesTypeMap();
62 if (empty($extraServicesTypeMap[$extraServiceType]))
63 {
64 throw new RestException('Parameter TYPE is unknown', self::ERROR_CHECK_FAILURE);
65 }
66
67 if ($params['TYPE'] === self::ENUM_TYPE)
68 {
69 if (!isset($params['ITEMS']) || !is_array($params['ITEMS']) || empty($params['ITEMS']))
70 {
71 throw new RestException('Parameter ITEMS must be defined for enum type', self::ERROR_CHECK_FAILURE);
72 }
73 }
74
75 $deliveryService = Sale\Delivery\Services\Manager::getById($params['DELIVERY_ID']);
76 if ($deliveryService)
77 {
78 if (!self::hasAccessToDelivery($deliveryService, $params['APP_ID']))
79 {
80 throw new AccessException();
81 }
82 }
83 else
84 {
85 throw new RestException('Delivery not found', self::ERROR_DELIVERY_NOT_FOUND);
86 }
87
88 if (!empty($params['CODE']))
89 {
90 $extraServiceData = Sale\Delivery\ExtraServices\Table::getList([
91 'select' => ['ID'],
92 'filter' => [
93 '=DELIVERY_ID' => $params['DELIVERY_ID'],
94 '=CODE' => $params['CODE'],
95 ],
96 'limit' => 1,
97 ])->fetch();
98 if ($extraServiceData)
99 {
100 throw new RestException(
101 'CODE "' . $params['CODE'] . '" already exists for this delivery',
102 self::ERROR_CHECK_FAILURE
103 );
104 }
105 }
106
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,
116 'RIGHTS' => [
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',
120 ],
121 ]);
122 $result = Sale\Delivery\ExtraServices\Table::add($fields);
123 if ($result->isSuccess())
124 {
125 return $result->getId();
126 }
127
128 $error = implode("\n", $result->getErrorMessages());
129 throw new RestException($error, self::ERROR_EXTRA_SERVICE_ADD);
130 }
131
139 public static function updateExtraServices($query, $n, \CRestServer $server)
140 {
142 $params = self::prepareExtraServicesParams($query, $server);
143
144 if (empty($params['ID']))
145 {
146 throw new RestException('Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
147 }
148
149 $extraService = Sale\Delivery\ExtraServices\Table::getList([
150 'select' => ['ID', 'CODE', 'DELIVERY_ID', 'CLASS_NAME'],
151 'filter' => [
152 '=ID' => $params['ID'],
153 ],
154 'limit' => 1,
155 ])->fetch();
156 if ($extraService)
157 {
158 $deliveryService = Sale\Delivery\Services\Manager::getById($extraService['DELIVERY_ID']);
159 if ($deliveryService && !self::hasAccessToDelivery($deliveryService, $params['APP_ID']))
160 {
161 throw new AccessException();
162 }
163
164 $newCode = $params['CODE'] ?? '';
165 if ($newCode)
166 {
167 $existingExtraService = Sale\Delivery\ExtraServices\Table::getList([
168 'select' => ['ID'],
169 'filter' => [
170 '=DELIVERY_ID' => $extraService['DELIVERY_ID'],
171 '=CODE' => $newCode,
172 ],
173 'limit' => 1,
174 ])->fetch();
175 if ($existingExtraService && (int)$existingExtraService['ID'] !== (int)$params['ID'])
176 {
177 throw new RestException(
178 'FIELDS[CODE] "' . $newCode . '" already exists for this delivery',
179 self::ERROR_CHECK_FAILURE
180 );
181 }
182 }
183 }
184 else
185 {
186 throw new RestException('Extra service not found', self::ERROR_EXTRA_SERVICE_NOT_FOUND);
187 }
188
189 $extraServicesTypeMap = array_flip(self::getExtraServicesTypeMap());
190
191 if (!isset($extraServicesTypeMap[$extraService['CLASS_NAME']]))
192 {
193 throw new RestException('Unknown type', self::ERROR_CHECK_FAILURE);
194 }
195 $type = $extraServicesTypeMap[$extraService['CLASS_NAME']];
196
197 $fields = [];
198
199 if (!empty($params['NAME']))
200 {
201 $fields['NAME'] = $params['NAME'];
202 }
203
204 if (!empty($params['ACTIVE']))
205 {
206 $fields['ACTIVE'] = $params['ACTIVE'] === 'Y' ? 'Y' : 'N';
207 }
208
209 if (isset($params['DESCRIPTION']))
210 {
211 $fields['DESCRIPTION'] = $params['DESCRIPTION'];
212 }
213
214 if (!empty($params['SORT']))
215 {
216 $fields['SORT'] = (int)$params['SORT'];
217 }
218
219 if (isset($params['CODE']))
220 {
221 $fields['CODE'] = $params['CODE'];
222 }
223
224 $internalParams = self::makeInternalParams($type, $params);
225 if (!is_null($internalParams))
226 {
227 $fields['PARAMS'] = $internalParams;
228 }
229
230 $fields = Sale\Delivery\ExtraServices\Manager::prepareParamsToSave($fields);
231
232 $result = Sale\Delivery\ExtraServices\Table::update($params['ID'], $fields);
233 if ($result->isSuccess())
234 {
235 return true;
236 }
237
238 $error = implode("\n", $result->getErrorMessages());
239 throw new RestException($error, self::ERROR_EXTRA_SERVICE_UPDATE);
240 }
241
248 public static function deleteExtraServices($query, $n, \CRestServer $server)
249 {
251 $params = self::prepareExtraServicesParams($query, $server);
252
253 if (empty($params['ID']))
254 {
255 throw new RestException('Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
256 }
257
258 $extraService = Sale\Delivery\ExtraServices\Table::getById($params['ID'])->fetch();
259 if ($extraService)
260 {
261 $data = Sale\Delivery\Services\Manager::getById($extraService['DELIVERY_ID']);
262 if ($data && !self::hasAccessToDelivery($data, $params['APP_ID']))
263 {
264 throw new AccessException();
265 }
266 }
267 else
268 {
269 throw new RestException('Extra service not found', self::ERROR_EXTRA_SERVICE_NOT_FOUND);
270 }
271
272 $result = Sale\Delivery\ExtraServices\Table::delete($params['ID']);
273 if ($result->isSuccess())
274 {
275 return true;
276 }
277
278 $error = implode("\n", $result->getErrorMessages());
279 throw new RestException($error, self::ERROR_EXTRA_SERVICE_DELETE);
280 }
281
288 public static function get($query, $n, \CRestServer $server): array
289 {
291 $params = self::prepareIncomingParams($query);
292
293 if (empty($params['DELIVERY_ID']))
294 {
295 throw new RestException('Parameter DELIVERY_ID is not defined', self::ERROR_CHECK_FAILURE);
296 }
297
298 $data = Sale\Delivery\Services\Manager::getById($params['DELIVERY_ID']);
299 if (!$data)
300 {
301 throw new RestException('Delivery not found', self::ERROR_DELIVERY_NOT_FOUND);
302 }
303
304 $esClasses = Sale\Delivery\ExtraServices\Manager::getClassesList();
305
306 $extraServices = Sale\Delivery\ExtraServices\Table::getList([
307 'filter' => [
308 '=DELIVERY_ID' => $params['DELIVERY_ID'],
309 '=CLASS_NAME' => $esClasses
310 ],
311 'select' => ['ID', 'CODE', 'NAME', 'DESCRIPTION', 'CLASS_NAME', 'ACTIVE', 'SORT', 'PARAMS'],
312 'order' => [
313 'SORT' => 'ASC',
314 ],
315 ])->fetchAll();
316
317 $extraServicesTypeMap = array_flip(self::getExtraServicesTypeMap());
318
319 foreach ($extraServices as $key => $extraService)
320 {
321 $type = $extraServicesTypeMap[$extraService['CLASS_NAME']];
322 $extraServices[$key]['TYPE'] = $type;
323 unset($extraServices[$key]['CLASS_NAME']);
324
325 if ($type === self::ENUM_TYPE)
326 {
327 if (array_key_exists('PRICES', $extraServices[$key]['PARAMS']))
328 {
329 if (is_array($extraServices[$key]['PARAMS']['PRICES']))
330 {
331 $extraServices[$key]['ITEMS'] = [];
332 foreach ($extraServices[$key]['PARAMS']['PRICES'] as $price)
333 {
334 $extraServices[$key]['ITEMS'][] = [
335 'TITLE' => $price['TITLE'],
336 'CODE' => $price['CODE'],
337 'PRICE' => $price['PRICE'] ?? null,
338 ];
339 }
340 }
341 }
342 }
343 elseif (in_array($type, [self::CHECKBOX_TYPE, self::QUANTITY_TYPE]))
344 {
345 if (array_key_exists('PRICE', $extraServices[$key]['PARAMS']))
346 {
347 $extraServices[$key]['PRICE'] = $extraServices[$key]['PARAMS']['PRICE'];
348 }
349 }
350 unset($extraServices[$key]['PARAMS']);
351 }
352
353 return $extraServices ?: [];
354 }
355
362 private static function makeInternalParams(string $type, array $params): ?array
363 {
364 $result = null;
365
366 if ($type === self::ENUM_TYPE)
367 {
368 if (is_array($params['ITEMS']))
369 {
370 $result = [
371 'PRICES' => [],
372 ];
373
374 foreach ($params['ITEMS'] as $item)
375 {
376 if (!isset($item['TITLE']) || empty($item['TITLE']))
377 {
378 throw new RestException('Item title must be defined for enum item', self::ERROR_CHECK_FAILURE);
379 }
380
381 if (!isset($item['CODE']) || empty($item['CODE']))
382 {
383 throw new RestException('Item code must be defined for enum item', self::ERROR_CHECK_FAILURE);
384 }
385
386 $result['PRICES'][$item['CODE']] = [
387 'TITLE' => $item['TITLE'],
388 'CODE' => $item['CODE'],
389 'PRICE' => isset($item['PRICE']) ? (float)$item['PRICE'] : null,
390 ];
391 }
392 }
393 }
394 elseif (in_array($type, [self::CHECKBOX_TYPE, self::QUANTITY_TYPE]) && array_key_exists('PRICE', $params))
395 {
396 $result['PRICE'] = isset($params['PRICE']) ? (float)$params['PRICE'] : null;
397 }
398
399 return $result;
400 }
401
406 private static function getExtraServicesTypeMap(): array
407 {
408 $result = [];
409
410 $allowedTypes = [
411 self::ENUM_TYPE,
412 self::CHECKBOX_TYPE,
413 self::QUANTITY_TYPE,
414 ];
415 $esClasses = Sale\Delivery\ExtraServices\Manager::getClassesList();
416 foreach ($esClasses as $className)
417 {
418 $classCode = (new \ReflectionClass($className))->getShortName();
419 $type = mb_strtolower($classCode);
420 if (!in_array($type, $allowedTypes, true))
421 {
422 continue;
423 }
424 $result[$type] = $className;
425 }
426
427 return $result;
428 }
429
435 private static function prepareExtraServicesParams(array $data, \CRestServer $server): array
436 {
437 $data = self::prepareIncomingParams($data);
438 $data['APP_ID'] = $server->getClientId();
439
440 return $data;
441 }
442}
static includeModule($moduleName)
Definition loader.php:69
static prepareIncomingParams(array $data)
static addExtraServices($query, $n, \CRestServer $server)
static updateExtraServices($query, $n, \CRestServer $server)
static deleteExtraServices($query, $n, \CRestServer $server)