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';
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',
42 private static function checkParamsBeforeAddCashbox($params)
44 if (empty($params[
'NAME']))
46 throw new RestException(
'Parameter NAME is not defined', self::ERROR_CHECK_FAILURE);
49 if (empty($params[
'REST_CODE']))
51 throw new RestException(
'Parameter REST_CODE is not defined', self::ERROR_CHECK_FAILURE);
57 throw new RestException(
"Rest handler with code {$params['REST_CODE']} not found", self::ERROR_CHECK_FAILURE);
60 if ($params[
'APP_ID'] && !empty($restHandler[
'APP_ID']) && $restHandler[
'APP_ID'] !== $params[
'APP_ID'])
65 if (empty($params[
'EMAIL']))
67 throw new RestException(
'Parameter EMAIL is not defined', self::ERROR_CHECK_FAILURE);
70 if (!empty($params[
'OFD']))
72 $ofdHandlerClass = self::getOfdHandlerClassByCode($params[
'OFD']);
73 if (is_null($ofdHandlerClass))
75 throw new RestException(
'Ofd handler not found', self::ERROR_CHECK_FAILURE);
85 private static function checkParamsBeforeUpdateCashbox($params)
87 if (empty($params[
'ID']))
89 throw new RestException(
'Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
95 throw new RestException(
'Cashbox not found', self::ERROR_CASHBOX_NOT_FOUND);
98 if (empty($params[
'FIELDS']) || !is_array($params[
'FIELDS']))
100 throw new RestException(
'Parameter FIELDS is not defined', self::ERROR_CHECK_FAILURE);
103 if (isset($params[
'FIELDS'][
'EMAIL']) && empty($params[
'FIELDS'][
'EMAIL']))
105 throw new RestException(
'Parameter EMAIL cannot be empty', self::ERROR_CHECK_FAILURE);
108 if (!empty($params[
'FIELDS'][
'OFD']))
110 $ofdHandlerClass = self::getOfdHandlerClassByCode($params[
'FIELDS'][
'OFD']);
111 if (is_null($ofdHandlerClass))
113 throw new RestException(
'Ofd handler not found', self::ERROR_CHECK_FAILURE);
117 if (!self::hasAccessToCashbox($cashbox, $params[
'APP_ID']))
127 private static function getOfdHandlerClassByCode($ofdCode): ?
string
129 $ofdHandlers = Ofd::getHandlerList();
130 foreach ($ofdHandlers as $handler => $name)
132 $currentHandlerCode = $handler::getCode();
133 if ($currentHandlerCode === $ofdCode)
147 private static function checkParamsBeforeDeleteCashbox($params)
149 if (empty($params[
'ID']))
151 throw new RestException(
'Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
157 throw new RestException(
'Cashbox not found', self::ERROR_CASHBOX_NOT_FOUND);
160 if (!self::hasAccessToCashbox($cashbox, $params[
'APP_ID']))
173 Helpers\Rest\AccessChecker::checkAccessPermission();
175 self::checkParamsBeforeAddCashbox($params);
177 $settings = $params[
'SETTINGS'] ?: [];
178 $settings[
'REST'][
'REST_CODE'] = $params[
'REST_CODE'];
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',
192 'SETTINGS' => $settings,
196 if ($result->isSuccess())
198 return $result->getId();
201 $errors = implode(
"\n", $result->getErrorMessages());
212 Helpers\Rest\AccessChecker::checkAccessPermission();
214 self::checkParamsBeforeUpdateCashbox($params);
217 'NAME',
'OFD',
'OFD_SETTINGS',
'EMAIL',
218 'NUMBER_KKM',
'KKM_ID',
'ACTIVE',
'SORT',
219 'USE_OFFLINE',
'ENABLED',
'SETTINGS',
223 $cashboxFields = array_intersect_key($params[
'FIELDS'], array_flip($allowedFields));
225 if (isset($cashboxFields[
'OFD']))
227 $cashboxFields[
'OFD'] = self::getOfdHandlerClassByCode($cashboxFields[
'OFD']);
230 if ($cashboxFields[
'SETTINGS'])
232 $cashboxFields[
'SETTINGS'] = self::mergeCashboxSettings($params[
'ID'], $cashboxFields[
'SETTINGS']);
235 $result = Manager::update($params[
'ID'], $cashboxFields);
236 if ($result->isSuccess())
241 $errors = implode(
"\n", $result->getErrorMessages());
242 throw new RestException($errors, self::ERROR_CASHBOX_UPDATE);
250 private static function mergeCashboxSettings($cashboxId, $newSettings)
253 'select' => [
'SETTINGS'],
254 'filter' => [
'=ID' => $cashboxId],
256 ])->fetch()[
'SETTINGS'];
258 if (!$existingSettings)
263 $mergedSettings = array_replace_recursive($existingSettings, $newSettings);
264 return $mergedSettings;
274 Helpers\Rest\AccessChecker::checkAccessPermission();
276 self::checkParamsBeforeDeleteCashbox($params);
278 $result = Manager::delete($params[
'ID']);
279 if ($result->isSuccess())
284 $errors = implode(
"\n", $result->getErrorMessages());
285 throw new RestException($errors, self::ERROR_CASHBOX_DELETE);
296 Helpers\Rest\AccessChecker::checkAccessPermission();
298 self::checkParamsBeforeCashboxListGet($params);
301 isset($params[
'SELECT']) && is_array($params[
'SELECT'])
302 ? array_flip(self::prepareIncomingParams(array_flip($params[
'SELECT'])))
303 : self::ALLOWED_CASHBOX_FIELDS
306 $filter = isset($params[
'FILTER']) && is_array($params[
'FILTER']) ? $params[
'FILTER'] : [];
307 $order = isset($params[
'ORDER']) && is_array($params[
'ORDER']) ? $params[
'ORDER'] : [];
315 while ($cashbox = $cashboxListResult->fetch())
319 $cashbox[
'OFD'] = $cashbox[
'OFD']::getCode();
322 $result[] = $cashbox;
328 private static function checkParamsBeforeCashboxListGet(array $params)
330 $select = isset($params[
'SELECT']) && is_array($params[
'SELECT']) ? $params[
'SELECT'] : [];
333 $select = array_flip(self::prepareIncomingParams(array_flip($select)));
334 $diffSelect = array_diff($select, self::ALLOWED_CASHBOX_FIELDS);
338 throw new RestException(implode(
', ', $diffSelect) .
' not allowed for select');
342 $filter = isset($params[
'FILTER']) && is_array($params[
'FILTER']) ? $params[
'FILTER'] : [];
346 foreach ($filter as $rawName => $value)
348 $filterField = \CSqlUtil::GetFilterOperation($rawName);
349 if (isset($filterField[
'FIELD']))
351 $filterFields[] = $filterField[
'FIELD'];
355 $filterFields = array_flip(self::prepareIncomingParams(array_flip($filterFields)));
356 $diffFilter = array_diff($filterFields, self::ALLOWED_CASHBOX_FIELDS);
359 throw new RestException(implode(
', ', $diffFilter) .
' not allowed for filter');
364 isset($params[
'ORDER']) && is_array($params[
'ORDER'])
370 $diffOrder = array_diff(array_keys($order), self::ALLOWED_CASHBOX_FIELDS);
373 throw new RestException(implode(
', ', $diffOrder) .
' not allowed for order');
386 Helpers\Rest\AccessChecker::checkAccessPermission();
388 self::checkParamsBeforeCashboxSettingsGet($params);
393 $settings = $cashbox->getField(
'SETTINGS');
394 unset($settings[
'REST']);
410 Helpers\Rest\AccessChecker::checkAccessPermission();
412 self::checkParamsBeforeCashboxSettingsGet($params);
417 return $cashbox->getField(
'OFD_SETTINGS');
428 private static function checkParamsBeforeCashboxSettingsGet(array $params)
430 if (empty($params[
'ID']))
432 throw new RestException(
'Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
438 throw new RestException(
'Cashbox not found', self::ERROR_CASHBOX_NOT_FOUND);
441 if (!self::hasAccessToCashbox($cashbox, $params[
'APP_ID']))
456 Helpers\Rest\AccessChecker::checkAccessPermission();
458 self::checkParamsBeforeCashboxSettingsUpdate($params);
461 $restHandlerCode = $cashbox->getValueFromSettings(
'REST',
'REST_CODE');
463 $params[
'FIELDS'][
'REST'][
'REST_CODE'] = $restHandlerCode;
465 $result = Manager::update($params[
'ID'], [
'SETTINGS' => $params[
'FIELDS']]);
466 if ($result->isSuccess())
471 $errors = implode(
"\n", $result->getErrorMessages());
472 throw new RestException($errors, self::ERROR_CASHBOX_UPDATE);
477 Helpers\Rest\AccessChecker::checkAccessPermission();
479 self::checkParamsBeforeCashboxSettingsUpdate($params);
481 $result = Manager::update($params[
'ID'], [
'OFD_SETTINGS' => $params[
'FIELDS']]);
482 if ($result->isSuccess())
487 $errors = implode(
"\n", $result->getErrorMessages());
488 throw new RestException($errors, self::ERROR_CASHBOX_UPDATE);
496 private static function checkParamsBeforeCashboxSettingsUpdate(array $params)
498 if (empty($params[
'ID']))
500 throw new RestException(
'Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
503 if (empty($params[
'FIELDS']))
505 throw new RestException(
'Parameter FIELDS is not defined', self::ERROR_CHECK_FAILURE);
511 throw new RestException(
'Cashbox not found', self::ERROR_CASHBOX_NOT_FOUND);
514 if (!self::hasAccessToCashbox($cashbox, $params[
'APP_ID']))
516 throw new AccessException();
520 private static function hasAccessToCashbox(Cashbox $cashbox,
string $appId =
null): bool
522 $handler = $cashbox->getField(
'HANDLER');
523 if (self::isRestHandler($handler))
525 $restHandlerCode = $cashbox->getValueFromSettings(
'REST',
'REST_CODE');
527 $handlerData = self::getHandlerData($restHandlerCode);
528 if ($appId && !empty($handlerData[
'APP_ID']) && $handlerData[
'APP_ID'] !== $appId)
541 private static function isRestHandler(
string $handler): bool
543 return $handler ===
'\\' . CashboxRest::class;
546 private static function getHandlerData(
string $code): ?array
550 if (!empty($result[$code]))
552 return $result[$code];
556 'filter' => [
'CODE' => $code],
561 $result[$code] = $handlerData;
564 return $result[$code] ??
null;
static includeModule($moduleName)