Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
updatecashboxesonbusinessvalueupdate.php
1<?php
2
4
10
12{
14 private $oldMapping;
16 private $newMapping;
18 private $consumerKey;
20 private $codeKey;
22 private $currentPaySystemService;
23
24 public function __construct(Event $event)
25 {
26 $this->oldMapping = $event->getParameter('OLD_MAPPING');
27 $this->newMapping = $event->getParameter('NEW_MAPPING');
28 $this->consumerKey = $event->getParameter('CONSUMER_KEY');
29 $this->codeKey = $event->getParameter('CODE_KEY');
30 }
31
35 public function executeEvent(): Sale\Result
36 {
37 $valueUnchanged =
38 isset($this->oldMapping['PROVIDER_VALUE'], $this->newMapping['PROVIDER_VALUE'])
39 && $this->oldMapping['PROVIDER_VALUE'] === $this->newMapping['PROVIDER_VALUE']
40 ;
41 $isPaySystemValue = isset($this->consumerKey) && mb_strpos($this->consumerKey, Service::PAY_SYSTEM_PREFIX) === 0;
42
43 if (empty($this->oldMapping) || $valueUnchanged || !$isPaySystemValue)
44 {
45 return new Sale\Result();
46 }
47
48 $paySystemIterator = Sale\PaySystem\Manager::getList([
49 'filter' => [
50 '=ACTIVE' => 'Y',
51 ]
52 ]);
53
54 while ($paySystemItem = $paySystemIterator->fetch())
55 {
56 $this->currentPaySystemService = new Service($paySystemItem);
57 if ($this->currentPaySystemService->getConsumerName() !== $this->consumerKey || !($this->currentPaySystemService->isSupportPrintCheck()))
58 {
59 continue;
60 }
61
63 $cashboxClass = $this->currentPaySystemService->getCashboxClass();
64 $paySystemCodeForKkm = $cashboxClass::getPaySystemCodeForKkm();
65 if ($paySystemCodeForKkm !== $this->codeKey)
66 {
67 continue;
68 }
69
70 $cashboxesToChange = $this->getListOfCashboxesToChange();
71
72 $newKkmId = $this->newMapping['PROVIDER_VALUE'];
73
74 if ($this->valueHasBeenCleared())
75 {
76 $newKkmId = $this->getNewKkmId();
77
78 /*
79 * if we can't set a new ID, then it's
80 * impossible to use an existing cashbox
81 * or create a new one, so we have to
82 * delete the old cashbox
83 */
84 if (empty($newKkmId))
85 {
86 $this->deleteCashboxesToChange();
87 continue;
88 }
89
90 $existingCashboxes = Sale\Cashbox\Manager::getList([
91 'select' => ['ID', 'KKM_ID'],
92 'filter' => [
93 '=HANDLER' => $cashboxClass,
94 '=KKM_ID' => $newKkmId,
95 ],
96 ]);
97
104 if ($existingCashboxes->fetch())
105 {
106 $this->deleteCashboxesToChange();
107 continue;
108 }
109 }
110
111 foreach ($cashboxesToChange as $cashboxItem)
112 {
113 Sale\Cashbox\Manager::update($cashboxItem['ID'], ['KKM_ID' => $newKkmId]);
114 }
115 }
116
117 return new Sale\Result();
118 }
119
126 private function getListOfCashboxesToChange(): Result
127 {
128 return Sale\Cashbox\Manager::getList([
129 'select' => ['ID', 'KKM_ID'],
130 'filter' => [
131 '=HANDLER' => $this->currentPaySystemService->getCashboxClass(),
132 '=KKM_ID' => $this->oldMapping['PROVIDER_VALUE'],
133 ],
134 ]);
135 }
136
137 private function deleteCashboxesToChange(): void
138 {
139 $cashboxesToChange = $this->getListOfCashboxesToChange();
140 foreach ($cashboxesToChange as $cashboxItem)
141 {
142 $serviceCashbox = Sale\Cashbox\Manager::getObjectById($cashboxItem['ID']);
143 $deleteResult = Sale\Cashbox\Manager::delete($cashboxItem['ID']);
144 if ($deleteResult->isSuccess())
145 {
146 AddEventToStatFile('sale', 'deleteCashbox', '', $serviceCashbox::getCode());
147 }
148 }
149 }
150
155 private function getNewKkmId()
156 {
157 $paySystemCodeForKkm = $this->currentPaySystemService->getCashboxClass()::getPaySystemCodeForKkm();
158 return Sale\BusinessValue::getMapping(
159 $paySystemCodeForKkm,
160 $this->currentPaySystemService->getConsumerName(),
161 BusinessValueTable::COMMON_PERSON_TYPE_ID
162 )['PROVIDER_VALUE'];
163 }
164
170 private function valueHasBeenCleared(): bool
171 {
172 return empty($this->newMapping);
173 }
174}
getParameter($key)
Definition event.php:80