Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
cashboxservice.php
1<?php
2
4
14
15if (!Main\Loader::includeModule('rest'))
16{
17 return;
18}
19
25{
26 private const ERROR_CASHBOX_ADD = 'ERROR_CASHBOX_ADD';
27 private const ERROR_CASHBOX_NOT_FOUND = 'ERROR_CASHBOX_NOT_FOUND';
28 private const ERROR_CASHBOX_UPDATE = 'ERROR_CASHBOX_UPDATE';
29 private const ERROR_CASHBOX_DELETE = 'ERROR_CASHBOX_DELETE';
30
31 private const ALLOWED_CASHBOX_FIELDS = [
32 'ID', 'NAME', 'OFD', 'EMAIL',
33 'DATE_CREATE', 'DATE_LAST_CHECK', 'NUMBER_KKM', 'KKM_ID',
34 'ACTIVE', 'SORT', 'USE_OFFLINE', 'ENABLED',
35 ];
36
42 private static function checkParamsBeforeAddCashbox($params)
43 {
44 if (empty($params['NAME']))
45 {
46 throw new RestException('Parameter NAME is not defined', self::ERROR_CHECK_FAILURE);
47 }
48
49 if (empty($params['REST_CODE']))
50 {
51 throw new RestException('Parameter REST_CODE is not defined', self::ERROR_CHECK_FAILURE);
52 }
53
54 $restHandler = CashboxRestHandlerTable::getList(['filter' => ['=CODE' => $params['REST_CODE']]])->fetch();
55 if (!$restHandler)
56 {
57 throw new RestException("Rest handler with code {$params['REST_CODE']} not found", self::ERROR_CHECK_FAILURE);
58 }
59
60 if ($params['APP_ID'] && !empty($restHandler['APP_ID']) && $restHandler['APP_ID'] !== $params['APP_ID'])
61 {
62 throw new AccessException();
63 }
64
65 if (empty($params['EMAIL']))
66 {
67 throw new RestException('Parameter EMAIL is not defined', self::ERROR_CHECK_FAILURE);
68 }
69
70 if (!empty($params['OFD']))
71 {
72 $ofdHandlerClass = self::getOfdHandlerClassByCode($params['OFD']);
73 if (is_null($ofdHandlerClass))
74 {
75 throw new RestException('Ofd handler not found', self::ERROR_CHECK_FAILURE);
76 }
77 }
78 }
79
85 private static function checkParamsBeforeUpdateCashbox($params)
86 {
87 if (empty($params['ID']))
88 {
89 throw new RestException('Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
90 }
91
92 $cashbox = Manager::getObjectById($params['ID']);
93 if (!$cashbox)
94 {
95 throw new RestException('Cashbox not found', self::ERROR_CASHBOX_NOT_FOUND);
96 }
97
98 if (empty($params['FIELDS']) || !is_array($params['FIELDS']))
99 {
100 throw new RestException('Parameter FIELDS is not defined', self::ERROR_CHECK_FAILURE);
101 }
102
103 if (isset($params['FIELDS']['EMAIL']) && empty($params['FIELDS']['EMAIL']))
104 {
105 throw new RestException('Parameter EMAIL cannot be empty', self::ERROR_CHECK_FAILURE);
106 }
107
108 if (!empty($params['FIELDS']['OFD']))
109 {
110 $ofdHandlerClass = self::getOfdHandlerClassByCode($params['FIELDS']['OFD']);
111 if (is_null($ofdHandlerClass))
112 {
113 throw new RestException('Ofd handler not found', self::ERROR_CHECK_FAILURE);
114 }
115 }
116
117 if (!self::hasAccessToCashbox($cashbox, $params['APP_ID']))
118 {
119 throw new AccessException();
120 }
121 }
122
127 private static function getOfdHandlerClassByCode($ofdCode): ?string
128 {
129 $ofdHandlers = Ofd::getHandlerList();
130 foreach ($ofdHandlers as $handler => $name)
131 {
132 $currentHandlerCode = $handler::getCode();
133 if ($currentHandlerCode === $ofdCode)
134 {
135 return $handler;
136 }
137 }
138
139 return null;
140 }
141
147 private static function checkParamsBeforeDeleteCashbox($params)
148 {
149 if (empty($params['ID']))
150 {
151 throw new RestException('Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
152 }
153
154 $cashbox = Manager::getObjectById($params['ID']);
155 if (!$cashbox)
156 {
157 throw new RestException('Cashbox not found', self::ERROR_CASHBOX_NOT_FOUND);
158 }
159
160 if (!self::hasAccessToCashbox($cashbox, $params['APP_ID']))
161 {
162 throw new AccessException();
163 }
164 }
165
171 public static function addCashbox($params, $page, $server)
172 {
173 Helpers\Rest\AccessChecker::checkAccessPermission();
174 $params = self::prepareHandlerParams($params, $server);
175 self::checkParamsBeforeAddCashbox($params);
176
177 $settings = $params['SETTINGS'] ?: [];
178 $settings['REST']['REST_CODE'] = $params['REST_CODE'];
179
180 $cashboxFields = [
181 'NAME' => $params['NAME'],
182 'HANDLER' => '\\' . CashboxRest::class,
183 'OFD' => empty($params['OFD']) ? '' : self::getOfdHandlerClassByCode($params['OFD']),
184 'OFD_SETTINGS' => $params['OFD_SETTINGS'] ?: [],
185 'EMAIL' => $params['EMAIL'],
186 'NUMBER_KKM' => empty($params['NUMBER_KKM']) ? '' : $params['NUMBER_KKM'],
187 'KKM_ID' => empty($params['KKM_ID']) ? '' : $params['KKM_ID'],
188 'ACTIVE' => ($params['ACTIVE'] === 'Y') ? 'Y' : 'N',
189 'SORT' => is_numeric($params['SORT']) ? (int)$params['SORT'] : 100,
190 'USE_OFFLINE' => ($params['USE_OFFLINE'] === 'Y') ? 'Y' : 'N',
191 'ENABLED' => 'Y',
192 'SETTINGS' => $settings,
193 ];
194
195 $result = Manager::add($cashboxFields);
196 if ($result->isSuccess())
197 {
198 return $result->getId();
199 }
200
201 $errors = implode("\n", $result->getErrorMessages());
202 throw new RestException($errors, self::ERROR_CASHBOX_ADD);
203 }
204
210 public static function updateCashbox($params, $page, \CRestServer $server)
211 {
212 Helpers\Rest\AccessChecker::checkAccessPermission();
213 $params = self::prepareHandlerParams($params, $server);
214 self::checkParamsBeforeUpdateCashbox($params);
215
216 $allowedFields = [
217 'NAME', 'OFD', 'OFD_SETTINGS', 'EMAIL',
218 'NUMBER_KKM', 'KKM_ID', 'ACTIVE', 'SORT',
219 'USE_OFFLINE', 'ENABLED', 'SETTINGS',
220 ];
221
222 // remove non-whitelisted keys using $allowedFields values as keys
223 $cashboxFields = array_intersect_key($params['FIELDS'], array_flip($allowedFields));
224
225 if (isset($cashboxFields['OFD']))
226 {
227 $cashboxFields['OFD'] = self::getOfdHandlerClassByCode($cashboxFields['OFD']);
228 }
229
230 if ($cashboxFields['SETTINGS'])
231 {
232 $cashboxFields['SETTINGS'] = self::mergeCashboxSettings($params['ID'], $cashboxFields['SETTINGS']);
233 }
234
235 $result = Manager::update($params['ID'], $cashboxFields);
236 if ($result->isSuccess())
237 {
238 return true;
239 }
240
241 $errors = implode("\n", $result->getErrorMessages());
242 throw new RestException($errors, self::ERROR_CASHBOX_UPDATE);
243 }
244
250 private static function mergeCashboxSettings($cashboxId, $newSettings)
251 {
252 $existingSettings = Manager::getList([
253 'select' => ['SETTINGS'],
254 'filter' => ['=ID' => $cashboxId],
255 'limit' => 1,
256 ])->fetch()['SETTINGS'];
257
258 if (!$existingSettings)
259 {
260 return $newSettings;
261 }
262
263 $mergedSettings = array_replace_recursive($existingSettings, $newSettings);
264 return $mergedSettings;
265 }
266
272 public static function deleteCashbox($params, $page, $server)
273 {
274 Helpers\Rest\AccessChecker::checkAccessPermission();
275 $params = self::prepareHandlerParams($params, $server);
276 self::checkParamsBeforeDeleteCashbox($params);
277
278 $result = Manager::delete($params['ID']);
279 if ($result->isSuccess())
280 {
281 return true;
282 }
283
284 $errors = implode("\n", $result->getErrorMessages());
285 throw new RestException($errors, self::ERROR_CASHBOX_DELETE);
286 }
287
294 public static function getCashboxList($params, $page, \CRestServer $server)
295 {
296 Helpers\Rest\AccessChecker::checkAccessPermission();
297 $params = self::prepareHandlerParams($params, $server);
298 self::checkParamsBeforeCashboxListGet($params);
299
300 $select =
301 isset($params['SELECT']) && is_array($params['SELECT'])
302 ? array_flip(self::prepareIncomingParams(array_flip($params['SELECT'])))
303 : self::ALLOWED_CASHBOX_FIELDS
304 ;
305
306 $filter = isset($params['FILTER']) && is_array($params['FILTER']) ? $params['FILTER'] : [];
307 $order = isset($params['ORDER']) && is_array($params['ORDER']) ? $params['ORDER'] : [];
308
309 $result = [];
310 $cashboxListResult = Manager::getList([
311 'select' => $select,
312 'filter' => $filter,
313 'order' => $order,
314 ]);
315 while ($cashbox = $cashboxListResult->fetch())
316 {
317 if ($cashbox['OFD'])
318 {
319 $cashbox['OFD'] = $cashbox['OFD']::getCode();
320 }
321
322 $result[] = $cashbox;
323 }
324
325 return $result;
326 }
327
328 private static function checkParamsBeforeCashboxListGet(array $params)
329 {
330 $select = isset($params['SELECT']) && is_array($params['SELECT']) ? $params['SELECT'] : [];
331 if ($select)
332 {
333 $select = array_flip(self::prepareIncomingParams(array_flip($select)));
334 $diffSelect = array_diff($select, self::ALLOWED_CASHBOX_FIELDS);
335
336 if ($diffSelect)
337 {
338 throw new RestException(implode(', ', $diffSelect) . ' not allowed for select');
339 }
340 }
341
342 $filter = isset($params['FILTER']) && is_array($params['FILTER']) ? $params['FILTER'] : [];
343 if ($filter)
344 {
345 $filterFields = [];
346 foreach ($filter as $rawName => $value)
347 {
348 $filterField = \CSqlUtil::GetFilterOperation($rawName);
349 if (isset($filterField['FIELD']))
350 {
351 $filterFields[] = $filterField['FIELD'];
352 }
353 }
354
355 $filterFields = array_flip(self::prepareIncomingParams(array_flip($filterFields)));
356 $diffFilter = array_diff($filterFields, self::ALLOWED_CASHBOX_FIELDS);
357 if ($diffFilter)
358 {
359 throw new RestException(implode(', ', $diffFilter) . ' not allowed for filter');
360 }
361 }
362
363 $order =
364 isset($params['ORDER']) && is_array($params['ORDER'])
365 ? self::prepareIncomingParams($params['ORDER'])
366 : []
367 ;
368 if ($order)
369 {
370 $diffOrder = array_diff(array_keys($order), self::ALLOWED_CASHBOX_FIELDS);
371 if ($diffOrder)
372 {
373 throw new RestException(implode(', ', $diffOrder) . ' not allowed for order');
374 }
375 }
376 }
377
384 public static function getCashboxSettings($params, $page, \CRestServer $server)
385 {
386 Helpers\Rest\AccessChecker::checkAccessPermission();
387 $params = self::prepareHandlerParams($params, $server);
388 self::checkParamsBeforeCashboxSettingsGet($params);
389
390 $cashbox = Manager::getObjectById($params['ID']);
391 if ($cashbox)
392 {
393 $settings = $cashbox->getField('SETTINGS');
394 unset($settings['REST']);
395
396 return $settings;
397 }
398
399 return [];
400 }
401
408 public static function getCashboxOfdSettings($params, $page, \CRestServer $server)
409 {
410 Helpers\Rest\AccessChecker::checkAccessPermission();
411 $params = self::prepareHandlerParams($params, $server);
412 self::checkParamsBeforeCashboxSettingsGet($params);
413
414 $cashbox = Manager::getObjectById($params['ID']);
415 if ($cashbox)
416 {
417 return $cashbox->getField('OFD_SETTINGS');
418 }
419
420 return [];
421 }
422
428 private static function checkParamsBeforeCashboxSettingsGet(array $params)
429 {
430 if (empty($params['ID']))
431 {
432 throw new RestException('Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
433 }
434
435 $cashbox = Manager::getObjectById($params['ID']);
436 if (!$cashbox)
437 {
438 throw new RestException('Cashbox not found', self::ERROR_CASHBOX_NOT_FOUND);
439 }
440
441 if (!self::hasAccessToCashbox($cashbox, $params['APP_ID']))
442 {
443 throw new AccessException();
444 }
445 }
446
454 public static function updateCashboxSettings($params, $page, \CRestServer $server)
455 {
456 Helpers\Rest\AccessChecker::checkAccessPermission();
457 $params = self::prepareHandlerParams($params, $server);
458 self::checkParamsBeforeCashboxSettingsUpdate($params);
459
460 $cashbox = Manager::getObjectById($params['ID']);
461 $restHandlerCode = $cashbox->getValueFromSettings('REST', 'REST_CODE');
462
463 $params['FIELDS']['REST']['REST_CODE'] = $restHandlerCode;
464
465 $result = Manager::update($params['ID'], ['SETTINGS' => $params['FIELDS']]);
466 if ($result->isSuccess())
467 {
468 return true;
469 }
470
471 $errors = implode("\n", $result->getErrorMessages());
472 throw new RestException($errors, self::ERROR_CASHBOX_UPDATE);
473 }
474
475 public static function updateCashboxOfdSettings($params, $page, \CRestServer $server)
476 {
477 Helpers\Rest\AccessChecker::checkAccessPermission();
478 $params = self::prepareHandlerParams($params, $server);
479 self::checkParamsBeforeCashboxSettingsUpdate($params);
480
481 $result = Manager::update($params['ID'], ['OFD_SETTINGS' => $params['FIELDS']]);
482 if ($result->isSuccess())
483 {
484 return true;
485 }
486
487 $errors = implode("\n", $result->getErrorMessages());
488 throw new RestException($errors, self::ERROR_CASHBOX_UPDATE);
489 }
490
496 private static function checkParamsBeforeCashboxSettingsUpdate(array $params)
497 {
498 if (empty($params['ID']))
499 {
500 throw new RestException('Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
501 }
502
503 if (empty($params['FIELDS']))
504 {
505 throw new RestException('Parameter FIELDS is not defined', self::ERROR_CHECK_FAILURE);
506 }
507
508 $cashbox = Manager::getObjectById($params['ID']);
509 if (!$cashbox)
510 {
511 throw new RestException('Cashbox not found', self::ERROR_CASHBOX_NOT_FOUND);
512 }
513
514 if (!self::hasAccessToCashbox($cashbox, $params['APP_ID']))
515 {
516 throw new AccessException();
517 }
518 }
519
520 private static function hasAccessToCashbox(Cashbox $cashbox, string $appId = null): bool
521 {
522 $handler = $cashbox->getField('HANDLER');
523 if (self::isRestHandler($handler))
524 {
525 $restHandlerCode = $cashbox->getValueFromSettings('REST', 'REST_CODE');
526
527 $handlerData = self::getHandlerData($restHandlerCode);
528 if ($appId && !empty($handlerData['APP_ID']) && $handlerData['APP_ID'] !== $appId)
529 {
530 return false;
531 }
532 }
533 else
534 {
535 return false;
536 }
537
538 return true;
539 }
540
541 private static function isRestHandler(string $handler): bool
542 {
543 return $handler === '\\' . CashboxRest::class;
544 }
545
546 private static function getHandlerData(string $code): ?array
547 {
548 static $result = [];
549
550 if (!empty($result[$code]))
551 {
552 return $result[$code];
553 }
554
555 $handlerData = CashboxRestHandlerTable::getList([
556 'filter' => ['CODE' => $code],
557 'limit' => 1,
558 ])->fetch();
559 if ($handlerData)
560 {
561 $result[$code] = $handlerData;
562 }
563
564 return $result[$code] ?? null;
565 }
566}
static includeModule($moduleName)
Definition loader.php:69
static getList(array $parameters=array())
static add(array $data)
Definition manager.php:333
static getList(array $parameters=[])
Definition manager.php:135
static getCashboxSettings($params, $page, \CRestServer $server)
static getCashboxList($params, $page, \CRestServer $server)
static updateCashboxSettings($params, $page, \CRestServer $server)
static addCashbox($params, $page, $server)
static updateCashbox($params, $page, \CRestServer $server)
static updateCashboxOfdSettings($params, $page, \CRestServer $server)
static deleteCashbox($params, $page, $server)
static getCashboxOfdSettings($params, $page, \CRestServer $server)
static prepareHandlerParams($data, \CRestServer $server)
static prepareIncomingParams(array $data)