Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
cashboxrest.php
1<?php
2
3namespace Bitrix\Sale\Cashbox;
4
9
15{
19 public function getHandlerCode()
20 {
21 $settings = $this->getField("SETTINGS");
22 return $settings["REST"]["REST_CODE"];
23 }
24
32 public static function getHandlerParams($handlerCode)
33 {
34 $handlerList = Manager::getRestHandlersList();
35 return $handlerList[$handlerCode];
36 }
37
45 public static function getConfigStructure($handlerCode)
46 {
47 $handlerParams = self::getHandlerParams($handlerCode);
48
49 $result = $handlerParams["SETTINGS"]["CONFIG"];
50 $result["REST"] = [
51 "REQUIRED" => "Y",
52 "ITEMS" => [
53 "REST_CODE" => [
54 "TYPE" => "STRING",
55 "LABEL" => Loc::getMessage("SALE_CASHBOX_REST_HANDLER_CODE"),
56 "READONLY" => true,
57 "VALUE" => $handlerCode,
58 ]
59 ]
60 ];
61
62 return $result;
63 }
64
68 protected static function getCheckTypeMap(): array
69 {
70 return array(
71 SellCheck::getType() => 'full_payment',
72 SellReturnCashCheck::getType() => 'full_payment',
73 SellReturnCheck::getType() => 'full_payment',
74 AdvancePaymentCheck::getType() => 'advance',
76 AdvanceReturnCheck::getType() => 'advance',
77 PrepaymentCheck::getType() => 'prepayment',
78 PrepaymentReturnCheck::getType() => 'prepayment',
79 PrepaymentReturnCashCheck::getType() => 'prepayment',
80 FullPrepaymentCheck::getType() => 'full_prepayment',
81 FullPrepaymentReturnCheck::getType() => 'full_prepayment',
82 FullPrepaymentReturnCashCheck::getType() => 'full_prepayment',
83 CreditCheck::getType() => 'credit',
84 CreditReturnCheck::getType() => 'credit',
85 CreditPaymentCheck::getType() => 'credit_payment',
86 );
87 }
88
95 private function getPrintUrl()
96 {
97 $handlerCode = $this->getHandlerCode();
98 $handlerParams = self::getHandlerParams($handlerCode);
99 return $handlerParams["SETTINGS"]["PRINT_URL"];
100 }
101
105 private function getRequestOptions(): array
106 {
107 $options = [];
108
109 $handlerCode = $this->getHandlerCode();
110 $handlerParams = self::getHandlerParams($handlerCode);
111 if (isset($handlerParams['SETTINGS']['HTTP_VERSION']))
112 {
113 $options['HTTP_CLIENT_OPTIONS'] = [
114 'version' => $handlerParams['SETTINGS']['HTTP_VERSION'],
115 ];
116 }
117
118 return $options;
119 }
120
127 private function getCheckUrl()
128 {
129 $handlerCode = $this->getHandlerCode();
130 $handlerParams = self::getHandlerParams($handlerCode);
131 return $handlerParams["SETTINGS"]["CHECK_URL"];
132 }
133
147 public function buildCheckQuery(Check $check): array
148 {
149 $data = $check->getDataForCheck();
150
151 $data["uuid"] = static::buildUuid(static::UUID_TYPE_CHECK, $data['unique_id']);
152
154 $dateTime = $data["date_create"];
155 $dateTimestamp = $dateTime->getTimestamp();
156 $data["date_create"] = $dateTimestamp;
157
158 $data["operation"] = $check::getCalculatedSign();
159
160 $checkTypeMap = self::getCheckTypeMap();
161 if (is_array($data["items"])) {
162 foreach ($data["items"] as $index => $item) {
163 $data["items"][$index]['payment_method'] = $checkTypeMap[$check::getType()];
164 }
165 }
166
167 $data["number_kkm"] = $this->getField('NUMBER_KKM');
168 $data["service_email"] = $this->getField('EMAIL');
169 $cashboxParams = $this->getField("SETTINGS");
170
171 // there's no need to pass our REST configuration (i.e. the handler code) to the application
172 unset($cashboxParams["REST"]);
173 $data["cashbox_params"] = $cashboxParams;
174
175 return $data;
176 }
177
182 public function buildZReportQuery($id): array
183 {
184 return [];
185 }
186
187
196 protected static function extractCheckData(array $data): array
197 {
198 $result = [];
199
200 if (!$data['UUID'])
201 {
202 return $result;
203 }
204
205 $checkInfo = CheckManager::getCheckInfoByExternalUuid($data['UUID']);
206 if (empty($checkInfo))
207 {
208 return $result;
209 }
210
211 if ($data['STATUS'] === 'ERROR')
212 {
213 $result['ERROR'] = [
214 'TYPE' => Errors\Error::TYPE,
215 'MESSAGE' => $data['ERROR'],
216 ];
217 }
218
219 $result['ID'] = $checkInfo['ID'];
220 $result['CHECK_TYPE'] = $checkInfo['TYPE'];
221
222 $check = CheckManager::getObjectById($checkInfo['ID']);
223 $dateTime = Main\Type\DateTime::createFromTimestamp($data["PRINT_END_TIME"]);
224 $result['LINK_PARAMS'] = [
225 Check::PARAM_REG_NUMBER_KKT => $data['REG_NUMBER_KKT'],
226 Check::PARAM_FISCAL_DOC_ATTR => $data['FISCAL_DOC_ATTR'],
227 Check::PARAM_FISCAL_DOC_NUMBER => $data['FISCAL_DOC_NUMBER'],
228 Check::PARAM_FISCAL_RECEIPT_NUMBER => $data['FISCAL_RECEIPT_NUMBER'],
229 Check::PARAM_FN_NUMBER => $data['FN_NUMBER'],
230 Check::PARAM_SHIFT_NUMBER => $data['SHIFT_NUMBER'],
231 Check::PARAM_DOC_SUM => (float)$checkInfo['SUM'],
232 Check::PARAM_DOC_TIME => $dateTime->getTimestamp(),
233 Check::PARAM_CALCULATION_ATTR => $check::getCalculatedSign()
234 ];
235
236 return $result;
237 }
238
246 public function check(Check $check): Result
247 {
248 $result = new Result();
249 $url = $this->getCheckUrl();
250 $checkUUID = $check->getField('EXTERNAL_UUID');
251 $queryResult = Sale\Helpers\Rest\Http::sendRequest(
252 $url,
253 ["uuid" => $checkUUID],
254 $this->getRequestOptions()
255 );
256 $response = $queryResult->getData();
257
258 if ($response === false)
259 {
260 return $result->addError(new Errors\Error(Loc::getMessage("SALE_CASHBOX_REST_DATA_ERROR_CHECK_CHECK")));
261 }
262
263 $response['UUID'] = $checkUUID;
264
265 if ($response['STATUS'] === 'WAIT')
266 {
267 $result = $result->addError(new Main\Error(Loc::getMessage('SALE_CASHBOX_REST_PRINT_IN_PROGRESS')));
268
269 return $result;
270 }
271
272 return static::applyCheckResult($response);
273 }
274
288 public function printImmediately(Check $check): Result
289 {
290 $url = $this->getPrintUrl();
291 $printResult = Sale\Helpers\Rest\Http::sendRequest(
292 $url,
293 $this->buildCheckQuery($check),
294 $this->getRequestOptions()
295 );
296
297 if (!$printResult->isSuccess())
298 {
299 return $printResult;
300 }
301
302 $printData = $printResult->getData();
303 if (isset($printData['ERRORS']) && is_array($printData['ERRORS']))
304 {
305 foreach ($printData['ERRORS'] as $errorMessage)
306 {
307 $printResult->addError(new Errors\Error($errorMessage));
308 }
309 }
310
311 return $printResult;
312 }
313
320 public function validate(): Result
321 {
322 $result = parent::validate();
323
324 // check whether the specified handler actually exists
325 $handlerCode = $this->getHandlerCode();
326 $handlerList = Manager::getRestHandlersList();
327 if (!isset($handlerList[$handlerCode]))
328 {
329 $result->addError(
330 new Main\Error(
332 "SALE_CASHBOX_REST_HANDLER_NOT_FOUND",
333 ["#HANDLER_CODE#" => $handlerCode]
334 )
335 )
336 );
337 }
338
339 return $result;
340 }
341}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
buildCheckQuery(Check $check)
static getConfigStructure($handlerCode)
static extractCheckData(array $data)
static getHandlerParams($handlerCode)