Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
cashboxbitrix.php
1<?php
2
3namespace Bitrix\Sale\Cashbox;
4
10
11Localization\Loc::loadMessages(__FILE__);
12
18{
19 const TYPE_Z_REPORT = 1;
20
25 public function buildCheckQuery(Check $check)
26 {
27 $result = array();
28
29 $data = $check->getDataForCheck();
30
31 foreach ($data['payments'] as $payment)
32 {
33 $result['payments'][] = array(
34 'type' => $this->getValueFromSettings('PAYMENT_TYPE', $payment['type']),
35 'value' => $payment['sum']
36 );
37 }
38
39 $typeMap = $this->getCheckTypeMap();
40 if (isset($typeMap[$data['type']]))
41 {
42 $result['type'] = $typeMap[$data['type']];
43 }
44 else
45 {
46 return array();
47 }
48
49 $result['uuid'] = static::buildUuid(static::UUID_TYPE_CHECK, $data['unique_id']);
50 $result['zn'] = $this->getField('NUMBER_KKM');
51 $result['items'] = array();
52 foreach ($data['items'] as $item)
53 {
54 $vat = $this->getValueFromSettings('VAT', $item['vat']);
55 if ($vat === null)
56 $vat = $this->getValueFromSettings('VAT', 'NOT_VAT');
57
58 $value = array(
59 'name' => $item['name'],
60 'price' => (float)$item['base_price'],
61 'quantity' => $item['quantity'],
62 'VAT' => (int)$vat
63 );
64
65 if (isset($item['discount']) && is_array($item['discount']))
66 {
67 $discountValue = PriceMaths::roundPrecision($item['base_price']*$item['quantity']) - $item['sum'];
68 $value['discount'] = $discountValue;
69
70 $discountType = $item['discount']['discount_type'] === 'P' ? 1 : 0;
71 $value['discount_type'] = $discountType;
72 }
73
74 $result['items'][] = $value;
75 }
76 $result['client'] = $data['client_email'];
77
79 $dateTime = $data['date_create'];
80 $result['timestamp'] = (string)$dateTime->getTimestamp();
81
82 return $result;
83 }
84
89 public function buildZReportQuery($id)
90 {
91 $dateTime = new Main\Type\DateTime();
92
93 return array(
94 'type' => static::TYPE_Z_REPORT,
95 'uuid' => static::buildUuid(static::UUID_TYPE_REPORT, $id),
96 'timestamp' => (string)$dateTime->getTimestamp(),
97 'zn' => $this->getField('NUMBER_KKM')
98 );
99 }
100
104 public static function getName()
105 {
106 return Localization\Loc::getMessage('SALE_CASHBOX_BITRIX_TITLE');
107 }
108
116 public static function saveCashbox(array $cashbox)
117 {
118 if (isset($cashbox['ID']) && (int)$cashbox['ID'] > 0)
119 {
120 if ($cashbox['ENABLED'] !== $cashbox['PRESENTLY_ENABLED'])
121 {
122 Manager::update($cashbox['ID'], array('ENABLED' => $cashbox['PRESENTLY_ENABLED']));
123
124 if ($cashbox['PRESENTLY_ENABLED'] === 'N')
125 {
126 static::showAlarmMessage($cashbox['ID']);
127 }
128 }
129
130 $fields = array('DATE_LAST_CHECK' => new Main\Type\DateTime());
131 if (isset($cashbox['SETTINGS']))
132 {
133 $fields['SETTINGS'] = $cashbox['SETTINGS'];
134 }
135
136 if (isset($cashbox['HANDLER']))
137 {
138 $fields['HANDLER'] = $cashbox['HANDLER'];
139 }
140
141 Manager::update($cashbox['ID'], $fields);
142 }
143 else
144 {
145 $result = Manager::add(
146 array(
147 'ACTIVE' => 'N',
148 'DATE_CREATE' => new Main\Type\DateTime(),
149 'NAME' => static::getName(),
150 'NUMBER_KKM' => $cashbox['NUMBER_KKM'],
151 'HANDLER' => $cashbox['HANDLER'],
152 'ENABLED' => $cashbox['PRESENTLY_ENABLED'],
153 'DATE_LAST_CHECK' => new Main\Type\DateTime(),
154 'EMAIL' => self::getCashboxDefaultEmail(),
155 )
156 );
157
158 if ($result->isSuccess())
159 {
160 if ($cashbox['PRESENTLY_ENABLED'] === 'N')
161 {
162 static::showAlarmMessage($result->getId());
163 }
164
165 Internals\CashboxZReportTable::add(array(
166 'STATUS' => 'Y',
167 'CASHBOX_ID' => $result->getId(),
168 'DATE_CREATE' => new Main\Type\DateTime(),
169 'DATE_PRINT_START' => new Main\Type\DateTime(),
170 'LINK_PARAMS' => '',
171 'CASH_SUM' => $cashbox['CACHE'],
172 'CASHLESS_SUM' => $cashbox['INCOME'] - $cashbox['CACHE'],
173 'CUMULATIVE_SUM' => $cashbox['NZ_SUM'],
174 'RETURNED_SUM' => 0,
175 'CURRENCY' => 'RUB',
176 'DATE_PRINT_END' => new Main\Type\DateTime()
177 ));
178 }
179 }
180 }
181
190 private static function getCashboxDefaultEmail()
191 {
192 $email = Main\Config\Option::get('main', 'email_from');
193 if (!$email)
194 {
195 $dbRes = Main\UserGroupTable::getList([
196 'select' => ['EMAIL' => 'USER.EMAIL'],
197 'filter' => [
198 '=GROUP_ID' => 1
199 ],
200 'order' => [
201 'USER.ID' => 'ASC'
202 ]
203 ]);
204
205 $data = $dbRes->fetch();
206 if ($data)
207 {
208 $email = $data['EMAIL'];
209 }
210 }
211
212 return $email;
213 }
214
218 protected static function showAlarmMessage($cashboxId)
219 {
220 $tag = "CASHBOX_STATUS_ERROR";
221
222 $dbRes = \CAdminNotify::GetList([], ["TAG" => $tag]);
223
224 if ($res = $dbRes->Fetch())
225 {
226 return;
227 }
228
229 \CAdminNotify::Add([
230 "MESSAGE" => Localization\Loc::getMessage('SALE_CASHBOX_ACCESS_UNAVAILABLE', ['#CASHBOX_ID#' => $cashboxId]),
231 "TAG" => $tag,
232 "MODULE_ID" => "SALE",
233 "ENABLE_CLOSE" => "Y",
234 "NOTIFY_TYPE" => \CAdminNotify::TYPE_ERROR
235 ]);
236 }
237
238
243 public static function getCashboxList(array $data)
244 {
245 $result = array();
246
247 if (isset($data['kkm']) && is_array($data['kkm']))
248 {
249 $factoryNum = array();
250 foreach ($data['kkm'] as $kkm)
251 {
252 $factoryNum[] = $kkm['zn'];
253 }
254
255 $cashboxList = Manager::getListFromCache();
256 foreach ($cashboxList as $item)
257 {
258 if (in_array($item['NUMBER_KKM'], $factoryNum))
259 {
260 $result[$item['NUMBER_KKM']] = $item;
261 }
262 }
263
264 foreach ($data['kkm'] as $kkm)
265 {
266 if (!isset($result[$kkm['zn']]))
267 {
268 $result[$kkm['zn']] = array(
269 'NUMBER_KKM' => $kkm['zn'],
270 'NUMBER_FN' => $kkm['fn'],
271 'HANDLER' => '\\'.get_called_class(),
272 'CACHE' => $kkm['cache'],
273 'INCOME' => $kkm['reg_income'],
274 'NZ_SUM' => $kkm['nz_sum']
275 );
276 }
277
278 $result[$kkm['zn']]['PRESENTLY_ENABLED'] = ($kkm['status'] === 'ok') ? 'Y' : 'N';
279 }
280 }
281
282 return $result;
283 }
284
289 public static function applyPrintResult(array $data)
290 {
291 $processedIds = array();
292
293 foreach ($data['kkm'] as $kkm)
294 {
295 if (isset($kkm['printed']) && is_array($kkm['printed']))
296 {
297 foreach ($kkm['printed'] as $item)
298 {
299 $uuid = static::parseUuid($item['uuid']);
300
301 $result = null;
302 if ($uuid['type'] === static::UUID_TYPE_CHECK)
303 {
304 $result = static::applyCheckResult($item);
305 }
306 elseif ($uuid['type'] === static::UUID_TYPE_REPORT)
307 {
308 $result = static::applyZReportResult($item);
309 }
310
311 if ($result !== null)
312 {
313 if ($result->isSuccess())
314 {
315 $processedIds[] = $item['uuid'];
316 }
317 else
318 {
319 $errors = $result->getErrors();
320 foreach ($errors as $error)
321 {
322 if ($error instanceof Errors\Error)
323 {
324 $processedIds[] = $item['uuid'];
325 break;
326 }
327 }
328 }
329 }
330 }
331 }
332 }
333
334 return $processedIds;
335 }
336
341 protected static function extractCheckData(array $data)
342 {
343 $uuid = self::parseUuid($data['uuid']);
344 $result = array(
345 'ID' => $uuid['id'],
346 'TYPE' => $uuid['type'],
347 );
348
349 if ($data['code'] !== 0 && isset($data['message']))
350 {
351 $errorMsg = Localization\Loc::getMessage('SALE_CASHBOX_BITRIX_ERR'.$data['code']);
352 if (!$errorMsg)
353 $errorMsg = $data['message'];
354
355 $errorType = static::getErrorType($data['code']);
356
357 $result['ERROR'] = array(
358 'CODE' => $data['code'],
359 'MESSAGE' => $errorMsg,
360 'TYPE' => ($errorType === Errors\Error::TYPE) ? Errors\Error::TYPE : Errors\Warning::TYPE
361 );
362 }
363
364 $result['LINK_PARAMS'] = static::getCheckLinkParams($data);
365
366 return $result;
367 }
368
373 private static function getCheckLinkParams($data)
374 {
375 $linkParams = static::parseQrParam($data['qr']);
376
377 $uuid = self::parseUuid($data['uuid']);
378 $check = CheckManager::getObjectById($uuid['id']);
379 if ($check)
380 {
381 $linkParams[Check::PARAM_CALCULATION_ATTR] = $check::getCalculatedSign();
382 }
383
384 if (isset($data['rn']))
385 {
386 $linkParams[Check::PARAM_REG_NUMBER_KKT] = $data['rn'];
387 }
388
389 return $linkParams;
390 }
391
396 protected static function extractZReportData(array $data)
397 {
398 $uuid = self::parseUuid($data['uuid']);
399 $result = array(
400 'ID' => $uuid['id'],
401 'TYPE' => $uuid['type'],
402 );
403
404 if ($data['code'] !== 0 && isset($data['message']))
405 {
406 $errorMsg = Localization\Loc::getMessage('SALE_CASHBOX_BITRIX_ERR'.$data['code']);
407 if (!$errorMsg)
408 $errorMsg = $data['message'];
409
410 $errorType = static::getErrorType($data['code']);
411 if ($errorType == null)
412 $errorType = Errors\Warning::TYPE;
413
414 $result['ERROR'] = array('MESSAGE' => $errorMsg, 'CODE' => $data['code'], 'TYPE' => $errorType);
415 }
416
417 $result['CASH_SUM'] = $data['payments_cache'];
418 $result['CASHLESS_SUM'] = $data['reg_income'] - $data['payments_cache'];
419 $result['CUMULATIVE_SUM'] = $data['nz_sum'];
420 $result['RETURNED_SUM'] = $data['reg_return'];
421 $result['LINK_PARAMS'] = static::parseQrParam($data['qr']);
422
423 return $result;
424 }
425
429 protected function getCheckTypeMap()
430 {
431 return array(
432 SellCheck::getType() => 1,
435 );
436 }
437
442 private static function parseQrParam($qr)
443 {
444 $result = array();
445 $params = explode('&', $qr);
446 if ($params)
447 {
448 foreach ($params as $param)
449 {
450 [$key, $value] = explode('=', $param);
451 switch ($key)
452 {
453 case 'fn' :
455 break;
456 case 'fp' :
458 break;
459 case 'i' :
461 break;
462 case 't' :
464 $dateTime = new Main\Type\DateTime($value, 'Ymd\THis');
465 $value = (string)$dateTime->getTimestamp();
466 break;
467 case 's' :
469 break;
470 case 'n' :
472 break;
473 default:
474 continue 2;
475 }
476
477 $result[$key] = $value;
478 }
479 }
480
481 return $result;
482 }
483
489 protected static function getErrorType($errorCode)
490 {
491 $errors = array(-3800, -3803, -3804, -3805, -3816, -3807, -3896, -3897, -4026);
492 if (in_array($errorCode, $errors))
493 return Errors\Error::TYPE;
494
495 $warnings = array();
496 if (in_array($errorCode, $warnings))
497 return Errors\Warning::TYPE;
498
499 return null;
500 }
501
506 public static function getSettings($modelId = 0)
507 {
508 $settings = array();
509
510 $kkmList = static::getSupportedKkmModels();
511 if (isset($kkmList[$modelId]))
512 {
513 $defaultSettings = $kkmList[$modelId]['SETTINGS'];
514
515 if (isset($defaultSettings['PAYMENT_TYPE']))
516 {
517 $settings['PAYMENT_TYPE'] = array(
518 'LABEL' => Localization\Loc::getMessage('SALE_CASHBOX_BITRIX_SETTINGS_P_TYPE'),
519 'REQUIRED' => 'Y',
520 'ITEMS' => array()
521 );
522
523 foreach ($defaultSettings['PAYMENT_TYPE'] as $type => $value)
524 {
525 $settings['PAYMENT_TYPE']['ITEMS'][$type] = array(
526 'TYPE' => 'STRING',
527 'LABEL' => Localization\Loc::getMessage('SALE_CASHBOX_BITRIX_SETTINGS_P_TYPE_LABEL_'.ToUpper($type)),
528 'VALUE' => $value
529 );
530 }
531 }
532
533
534 $settings['VAT'] = array(
535 'LABEL' => Localization\Loc::getMessage('SALE_CASHBOX_BITRIX_SETTINGS_VAT'),
536 'REQUIRED' => 'Y',
537 'ITEMS' => array(
538 'NOT_VAT' => array(
539 'TYPE' => 'STRING',
540 'LABEL' => Localization\Loc::getMessage('SALE_CASHBOX_BITRIX_SETTINGS_VAT_LABEL_NOT_VAT'),
541 'VALUE' => $defaultSettings['VAT']['NOT_VAT']
542 )
543 )
544 );
545
546 if (Main\Loader::includeModule('catalog'))
547 {
548 $dbRes = Catalog\VatTable::getList(array('filter' => array('ACTIVE' => 'Y')));
549 $vatList = $dbRes->fetchAll();
550 if ($vatList)
551 {
552 foreach ($vatList as $vat)
553 {
554 $value = '';
555 if (isset($defaultSettings['VAT'][(int)$vat['RATE']]))
556 $value = $defaultSettings['VAT'][(int)$vat['RATE']];
557
558 $settings['VAT']['ITEMS'][(int)$vat['ID']] = array(
559 'TYPE' => 'STRING',
560 'LABEL' => $vat['NAME'].' ['.(int)$vat['RATE'].'%]',
561 'VALUE' => $value
562 );
563 }
564 }
565 }
566 }
567
568 $hours = array();
569 for ($i = 0; $i < 24; $i++)
570 {
571 $value = ($i < 10) ? '0'.$i : $i;
572 $hours[$i] = $value;
573 }
574
575 $minutes = array();
576 for ($i = 0; $i < 60; $i+=5)
577 {
578 $value = ($i < 10) ? '0'.$i : $i;
579 $minutes[$i] = $value;
580 }
581
582 $settings['Z_REPORT'] = array(
583 'LABEL' => Localization\Loc::getMessage('SALE_CASHBOX_BITRIX_SETTINGS_Z_REPORT'),
584 'ITEMS' => array(
585 'TIME' => array(
586 'TYPE' => 'DELIVERY_MULTI_CONTROL_STRING',
587 'LABEL' => Localization\Loc::getMessage('SALE_CASHBOX_BITRIX_SETTINGS_Z_REPORT_LABEL'),
588 'ITEMS' => array(
589 'H' => array(
590 'TYPE' => 'ENUM',
591 'LABEL' => Localization\Loc::getMessage('SALE_CASHBOX_BITRIX_SETTINGS_Z_REPORT_LABEL_H'),
592 'VALUE' => 23,
593 'OPTIONS' => $hours
594 ),
595 'M' => array(
596 'TYPE' => 'ENUM',
597 'LABEL' => Localization\Loc::getMessage('SALE_CASHBOX_BITRIX_SETTINGS_Z_REPORT_LABEL_M'),
598 'VALUE' => 30,
599 'OPTIONS' => $minutes
600 ),
601 )
602 )
603 )
604 );
605
606 return $settings;
607 }
608
613 public static function extractSettingsFromRequest(Main\HttpRequest $request)
614 {
615 $settings = parent::extractSettingsFromRequest($request);
616
617 if ($settings['PAYMENT_TYPE'])
618 {
619 /* hack is for difference between real values of payment cashbox's settings and user view (diff is '-1') */
620 foreach ($settings['PAYMENT_TYPE'] as $i => $payment)
621 {
622 if ((int)$payment)
623 $settings['PAYMENT_TYPE'][$i] = (int)$payment - 1;
624 else
625 $settings['PAYMENT_TYPE'][$i] = 0;
626 }
627 }
628
629 return $settings;
630 }
631
635 public static function getGeneralRequiredFields()
636 {
637 $generalRequiredFields = parent::getGeneralRequiredFields();
638
639 $map = Internals\CashboxTable::getMap();
640 $generalRequiredFields['KKM_ID'] = $map['KKM_ID']['title'];
641 $generalRequiredFields['NUMBER_KKM'] = $map['NUMBER_KKM']['title'];
642
643 return $generalRequiredFields;
644 }
645
649 public static function getSupportedKkmModels()
650 {
651 return array(
652 'atol' => array(
653 'NAME' => 'ATOL',
654 'SETTINGS' => array(
655 'VAT' => array(
656 'NOT_VAT' => 4,
657 0 => 1,
658 10 => 2,
659 18 => 3
660 ),
661 'PAYMENT_TYPE' => array(
664 )
665 )
666 ),
667 );
668 }
669
670 public static function getFfdVersion(): ?float
671 {
672 return 1.0;
673 }
674}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static extractZReportData(array $data)
static extractSettingsFromRequest(Main\HttpRequest $request)
static saveCashbox(array $cashbox)
buildCheckQuery(Check $check)
getValueFromSettings($name, $code)
Definition cashbox.php:201
static add(array $data)
Definition manager.php:333
static roundPrecision($value)