Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
cashbox.php
1<?php
2
3namespace Bitrix\Sale\Cashbox;
4
10
11Loc::loadMessages(__FILE__);
12
17abstract class Cashbox
18{
19 const UUID_TYPE_CHECK = 'check';
20 const UUID_TYPE_REPORT = 'report';
21 const UUID_DELIMITER = '|';
22
23 const EVENT_ON_GET_CUSTOM_CASHBOX_HANDLERS = 'OnGetCustomCashboxHandlers';
24
26 private $fields = array();
27
28 private $ofd;
29
34 public static function init()
35 {
36 $handlers = static::getHandlerList();
37 Main\Loader::registerAutoLoadClasses(null, $handlers);
38 }
39
40 public static function getCode()
41 {
42 $className = (new \ReflectionClass(static::class))->getShortName();
43 return mb_strtolower($className);
44 }
45
49 public static function getHandlerList()
50 {
51 static $handlerList = array();
52
53 if (!$handlerList)
54 {
55 $zone = '';
56 $isCloud = Main\Loader::includeModule("bitrix24");
57 if ($isCloud)
58 {
59 $zone = \CBitrix24::getLicensePrefix();
60 }
61 elseif (Main\Loader::includeModule('intranet'))
62 {
63 $zone = \CIntranetUtils::getPortalZone();
64 }
65 if ($zone === 'ru' && $isCloud)
66 {
67 $handlerList = [
68 '\Bitrix\Sale\Cashbox\CashboxAtolFarm' => '/bitrix/modules/sale/lib/cashbox/cashboxatolfarm.php',
69 '\Bitrix\Sale\Cashbox\CashboxAtolFarmV4' => '/bitrix/modules/sale/lib/cashbox/cashboxatolfarmv4.php',
70 '\Bitrix\Sale\Cashbox\CashboxAtolFarmV5' => '/bitrix/modules/sale/lib/cashbox/cashboxatolfarmv5.php',
71 '\Bitrix\Sale\Cashbox\CashboxOrangeData' => '/bitrix/modules/sale/lib/cashbox/cashboxorangedata.php',
72 '\Bitrix\Sale\Cashbox\CashboxOrangeDataFfd12' => '/bitrix/modules/sale/lib/cashbox/cashboxorangedataffd12.php',
73 '\Bitrix\Sale\Cashbox\CashboxBusinessRu' => '/bitrix/modules/sale/lib/cashbox/cashboxbusinessru.php',
74 '\Bitrix\Sale\Cashbox\CashboxBusinessRuV5' => '/bitrix/modules/sale/lib/cashbox/cashboxbusinessruv5.php',
75 ];
76 }
77 elseif ($zone === 'ua')
78 {
79 $handlerList = [
80 '\Bitrix\Sale\Cashbox\CashboxCheckbox' => '/bitrix/modules/sale/lib/cashbox/cashboxcheckbox.php',
81 ];
82 }
83 else
84 {
85 $handlerList = [
86 '\Bitrix\Sale\Cashbox\CashboxAtolFarm' => '/bitrix/modules/sale/lib/cashbox/cashboxatolfarm.php',
87 '\Bitrix\Sale\Cashbox\CashboxAtolFarmV4' => '/bitrix/modules/sale/lib/cashbox/cashboxatolfarmv4.php',
88 '\Bitrix\Sale\Cashbox\CashboxAtolFarmV5' => '/bitrix/modules/sale/lib/cashbox/cashboxatolfarmv5.php',
89 '\Bitrix\Sale\Cashbox\CashboxOrangeData' => '/bitrix/modules/sale/lib/cashbox/cashboxorangedata.php',
90 '\Bitrix\Sale\Cashbox\CashboxOrangeDataFfd12' => '/bitrix/modules/sale/lib/cashbox/cashboxorangedataffd12.php',
91 '\Bitrix\Sale\Cashbox\CashboxBitrixV2' => '/bitrix/modules/sale/lib/cashbox/cashboxbitrixv2.php',
92 '\Bitrix\Sale\Cashbox\CashboxBitrixV3' => '/bitrix/modules/sale/lib/cashbox/cashboxbitrixv3.php',
93 '\Bitrix\Sale\Cashbox\CashboxBitrix' => '/bitrix/modules/sale/lib/cashbox/cashboxbitrix.php',
94 '\Bitrix\Sale\Cashbox\Cashbox1C' => '/bitrix/modules/sale/lib/cashbox/cashbox1c.php',
95 '\Bitrix\Sale\Cashbox\CashboxCheckbox' => '/bitrix/modules/sale/lib/cashbox/cashboxcheckbox.php',
96 '\Bitrix\Sale\Cashbox\CashboxBusinessRu' => '/bitrix/modules/sale/lib/cashbox/cashboxbusinessru.php',
97 '\Bitrix\Sale\Cashbox\CashboxBusinessRuV5' => '/bitrix/modules/sale/lib/cashbox/cashboxbusinessruv5.php',
98 ];
99 }
100
101 $handlerList['\Bitrix\Sale\Cashbox\CashboxRest'] = '/bitrix/modules/sale/lib/cashbox/cashboxrest.php';
102
103 $handlerList['\Bitrix\Sale\Cashbox\CashboxRobokassa'] = '/bitrix/modules/sale/lib/cashbox/cashboxrobokassa.php';
104 $handlerList['\Bitrix\Sale\Cashbox\CashboxYooKassa'] = '/bitrix/modules/sale/lib/cashbox/cashboxyookassa.php';
105
106 $event = new Main\Event('sale', static::EVENT_ON_GET_CUSTOM_CASHBOX_HANDLERS);
107 $event->send();
108 $resultList = $event->getResults();
109
110 if (is_array($resultList) && !empty($resultList))
111 {
112 foreach ($resultList as $eventResult)
113 {
115 if ($eventResult->getType() === Main\EventResult::SUCCESS)
116 {
117 $params = $eventResult->getParameters();
118 if (!empty($params) && is_array($params))
119 $handlerList = array_merge($handlerList, $params);
120 }
121 }
122 }
123 }
124
125 return $handlerList;
126 }
127
133 public static function create(array $settings)
134 {
135 static::init();
136
137 $handler = $settings['HANDLER'];
138 if (class_exists($handler))
139 return new $handler($settings);
140
141 return null;
142 }
143
148 private function __construct($settings)
149 {
150 $this->fields = $settings;
151 }
152
157 public function getField($name)
158 {
159 return $this->fields[$name];
160 }
161
165 public function getOfd()
166 {
167 if ($this->ofd === null)
168 {
169 $this->ofd = Ofd::create($this);
170 }
171
172 return $this->ofd;
173 }
174
179 abstract public function buildCheckQuery(Check $check);
180
185 abstract public function buildZReportQuery($id);
186
191 public static function getName()
192 {
193 throw new NotImplementedException();
194 }
195
201 public function getValueFromSettings($name, $code)
202 {
203 $map = $this->fields['SETTINGS'];
204 if (isset($map[$name]))
205 {
206 if (is_array($map[$name]))
207 {
208 if (isset($map[$name][$code]))
209 return $map[$name][$code];
210
211 return null;
212 }
213
214 return $map[$name];
215 }
216
217 $settings = static::getSettings($this->getField('KKM_ID'));
218
219 return $settings[$name]['ITEMS'][$code]['VALUE'] ?? null;
220 }
221
226 public function getCheckLink(array $linkParams)
227 {
228 if ($linkParams)
229 {
231 $ofd = $this->getOfd();
232 if ($ofd !== null)
233 return $ofd->generateCheckLink($linkParams);
234 }
235
236 return '';
237 }
238
244 protected static function getErrorType($errorCode)
245 {
246 throw new NotImplementedException();
247 }
248
254 protected static function extractCheckData(array $data)
255 {
256 throw new NotImplementedException();
257 }
258
264 protected static function extractZReportData(array $data)
265 {
266 throw new NotImplementedException();
267 }
268
273 public static function applyCheckResult(array $data)
274 {
275 $result = static::extractCheckData($data);
276
277 $checkId = $result['ID'] ?? 0;
278
279 return CheckManager::savePrintResult($checkId, $result);
280 }
281
286 public static function applyZReportResult(array $data)
287 {
288 $result = static::extractZReportData($data);
289
290 return ReportManager::saveZReportPrintResult($result['ID'], $result);
291 }
292
297 public static function getSettings($modelId = 0)
298 {
299 return array();
300 }
301
305 public function validate()
306 {
307 $fields = $this->fields;
308 unset($fields['OFD_SETTINGS']);
309
310 $result = $this->validateFields($fields);
311
312 $ofd = $this->getOfd();
313 if ($ofd)
314 {
315 $r = $ofd->validate();
316 if (!$r->isSuccess())
317 {
318 $result->addErrors($r->getErrors());
319 }
320 }
321
322 return $result;
323 }
324
325 protected function validateFields($fields)
326 {
327 $result = new Result();
328
329 foreach ($fields as $code => $value)
330 {
331 if (is_array($value))
332 {
333 $r = $this->validateFields($value);
334 if (!$r->isSuccess())
335 {
336 $result->addErrors($r->getErrors());
337 }
338
339 continue;
340 }
341
342 if (
343 $this->isRequiredField($code)
344 && $value === ''
345 )
346 {
347 $requiredFields = $this->getRequiredFields();
348
349 $result->addError(
350 new Main\Error(
352 'SALE_CASHBOX_VALIDATE_ERROR',
353 ['#FIELD_ID#' => $requiredFields[$code]]
354 )
355 )
356 );
357 }
358 }
359
360 return $result;
361 }
362
363 protected function isRequiredField($field) : bool
364 {
365 $requiredFields = $this->getRequiredFields();
366
367 return isset($requiredFields[$field]);
368 }
369
374 public static function extractSettingsFromRequest(Main\HttpRequest $request)
375 {
377 $settings = $request->get('SETTINGS');
378
379 return $settings;
380 }
381
386 private function getRequiredFields()
387 {
388 $result = static::getGeneralRequiredFields();
389
390 $settings = static::getSettings($this->getField('KKM_ID'));
391 foreach ($settings as $groupId => $group)
392 {
393 foreach ($group['ITEMS'] as $code => $item)
394 {
395 $isRequired =
396 isset($group['REQUIRED']) && $group['REQUIRED'] === 'Y'
397 || isset($item['REQUIRED']) && $item['REQUIRED'] === 'Y'
398 ;
399 if ($isRequired)
400 {
401 $result[$code] = $item['LABEL'];
402 }
403 }
404 }
405
406 return $result;
407 }
408
412 public static function getGeneralRequiredFields()
413 {
414 $map = CashboxTable::getMap();
415
416 return array(
417 'NAME' => $map['NAME']['title'],
418 'EMAIL' => $map['EMAIL']['title'],
419 'HANDLER' => $map['HANDLER']['title']
420 );
421 }
422
428 protected static function buildUuid($type, $id)
429 {
430 $context = Main\Application::getInstance()->getContext();
431 $server = $context->getServer();
432 $domain = $server->getServerName();
433
434 return $type.static::UUID_DELIMITER.$domain.static::UUID_DELIMITER.$id;
435 }
436
441 protected static function parseUuid($uuid)
442 {
443 $info = explode(static::UUID_DELIMITER, $uuid);
444
445 return array('type' => $info[0], 'id' => $info[2]);
446 }
447
451 public static function getSupportedKkmModels()
452 {
453 return array();
454 }
455
459 public function isCheckable()
460 {
461 return $this instanceof ICheckable;
462 }
463
467 public function isCorrection()
468 {
469 return (
470 $this instanceof ICorrection
471 && $this::isCorrectionOn()
472 );
473 }
474
478 public static function isCorrectionOn(): bool
479 {
480 return true;
481 }
482
486 public static function getFfdVersion(): ?float
487 {
488 return null;
489 }
490
496 public static function isSupportedFFD105()
497 {
498 return static::getFfdVersion() >= 1.05;
499 }
500}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static extractZReportData(array $data)
Definition cashbox.php:264
static buildUuid($type, $id)
Definition cashbox.php:428
static getErrorType($errorCode)
Definition cashbox.php:244
static getSettings($modelId=0)
Definition cashbox.php:297
buildCheckQuery(Check $check)
static applyZReportResult(array $data)
Definition cashbox.php:286
static extractCheckData(array $data)
Definition cashbox.php:254
static create(array $settings)
Definition cashbox.php:133
static applyCheckResult(array $data)
Definition cashbox.php:273
const EVENT_ON_GET_CUSTOM_CASHBOX_HANDLERS
Definition cashbox.php:23
getValueFromSettings($name, $code)
Definition cashbox.php:201
static create(Cashbox $cashbox)
Definition ofd.php:85
static saveZReportPrintResult($reportId, $data)