Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
togglecashboxesonupdatepaysystem.php
1<?php
2
4
8
10{
12 private $service;
14 private $oldFields;
16 private $newFields;
17
18 public function __construct(Event $event)
19 {
20 $paySystemId = $event->getParameter('PAY_SYSTEM_ID');
21 $this->service = Manager::getObjectById($paySystemId);
22 $this->oldFields = $event->getParameter('OLD_FIELDS');
23 $this->newFields = $event->getParameter('NEW_FIELDS');
24 }
25
29 public function executeEvent(): Sale\Result
30 {
31 $result = new Sale\Result();
32
33 if (!$this->service || !$this->service->isSupportPrintCheck())
34 {
35 return $result;
36 }
37
38 if (
39 !isset($this->oldFields['ACTIVE'], $this->newFields['ACTIVE'])
40 ||
41 (
42 isset($this->oldFields['ACTIVE'], $this->newFields['ACTIVE'])
43 && $this->oldFields['ACTIVE'] === $this->newFields['ACTIVE']
44 )
45 )
46 {
47 return $result;
48 }
49
50 $newStatus = $this->newFields['ACTIVE'];
51
53 $cashboxClass = $this->service->getCashboxClass();
54
55 $kkmId = $this->getKkmID();
56
57 if ($this->isCashboxUsedByOtherPaySystems())
58 {
59 return $result;
60 }
61
62 $cashboxList = Sale\Cashbox\Manager::getList([
63 'select' => ['ID', 'KKM_ID'],
64 'filter' => [
65 '=HANDLER' => $cashboxClass,
66 '=KKM_ID' => $kkmId,
67 ],
68 ]);
69 foreach ($cashboxList as $cashboxItem)
70 {
71 $updateResult = Sale\Cashbox\Manager::update($cashboxItem['ID'], ['ACTIVE' => $newStatus]);
72 if (!$updateResult->isSuccess())
73 {
74 $result->addErrors($updateResult->getErrors());
75 }
76 }
77
78 return $result;
79 }
80
81 private function isCashboxUsedByOtherPaySystems(): bool
82 {
83 $cashboxClass = $this->service->getCashboxClass();
84
85 $paySystemIterator = Sale\PaySystem\Manager::getList([
86 'filter' => [
87 '=ACTIVE' => 'Y',
88 '!=ID' => $this->service->getField('ID'),
89 ]
90 ]);
91
92 $kkmId = $this->getKkmId();
93 $paySystemCodeForKkm = $cashboxClass::getPaySystemCodeForKkm();
94
95 while ($paySystemItem = $paySystemIterator->fetch())
96 {
97 $paySystemService = new Sale\PaySystem\Service($paySystemItem);
98 if (
99 $paySystemService->isSupportPrintCheck()
100 && $paySystemService->getCashboxClass() === $cashboxClass
101 && Sale\BusinessValue::getValuesByCode($paySystemService->getConsumerName(), $paySystemCodeForKkm) === $kkmId
102 )
103 {
104 return true;
105 }
106 }
107
108 return false;
109 }
110
111 private function getKkmId(): array
112 {
113 $cashboxClass = $this->service->getCashboxClass();
114 $paySystemCodeForKkm = $cashboxClass::getPaySystemCodeForKkm();
115
116 return Sale\BusinessValue::getValuesByCode($this->service->getConsumerName(), $paySystemCodeForKkm);
117 }
118}
getParameter($key)
Definition event.php:80
static getValuesByCode(string $consumerName, string $code)