Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
abstractcheck.php
1<?php
2
4
16
21abstract class AbstractCheck
22{
23 public const PARAM_FISCAL_DOC_NUMBER = 'fiscal_doc_number';
24 public const PARAM_FISCAL_DOC_ATTR = 'fiscal_doc_attribute';
25 public const PARAM_FISCAL_RECEIPT_NUMBER = 'fiscal_receipt_number';
26 public const PARAM_FN_NUMBER = 'fn_number';
27 public const PARAM_SHIFT_NUMBER = 'shift_number';
28 public const PARAM_REG_NUMBER_KKT = 'reg_number_kkt';
29 public const PARAM_DOC_TIME = 'doc_time';
30 public const PARAM_DOC_SUM = 'doc_sum';
31 public const PARAM_CALCULATION_ATTR = 'calculation_attribute';
32
33 public const CALCULATED_SIGN_INCOME = 'income';
34 public const CALCULATED_SIGN_CONSUMPTION = 'consumption';
35
36 public const SHIPMENT_TYPE_NONE = '';
37 public const PAYMENT_TYPE_CASH = 'cash';
38 public const PAYMENT_TYPE_ADVANCE = 'advance';
39 public const PAYMENT_TYPE_CASHLESS = 'cashless';
40 public const PAYMENT_TYPE_CREDIT = 'credit';
41
42 public const SUPPORTED_ENTITY_TYPE_PAYMENT = 'payment';
43 public const SUPPORTED_ENTITY_TYPE_SHIPMENT = 'shipment';
44 public const SUPPORTED_ENTITY_TYPE_ALL = 'all';
45 public const SUPPORTED_ENTITY_TYPE_NONE = 'none';
46
47 protected const EVENT_ON_CHECK_PREPARE_DATA = 'OnSaleCheckPrepareData';
48
50 protected $fields = array();
51
53 protected $cashboxList = array();
54
56 protected $entities = array();
57
61 abstract public static function getType();
62
66 abstract public static function getCalculatedSign();
67
71 abstract public static function getName();
72
76 abstract public function getDataForCheck();
77
81 abstract protected function extractDataInternal();
82
86 abstract static function getSupportedEntityType();
87
92 public static function create($handler)
93 {
94 if (class_exists($handler))
95 {
96 return new $handler();
97 }
98
99 return null;
100 }
101
105 protected function __construct()
106 {
107 $this->fields['TYPE'] = static::getType();
108 }
109
114 public function getField($name)
115 {
116 return $this->fields[$name] ?? null;
117 }
118
123 public function setField($name, $value)
124 {
125 $this->fields[$name] = $value;
126 }
127
131 public function setFields($fields)
132 {
133 foreach ($fields as $name => $value)
134 {
135 $this->setField($name, $value);
136 }
137 }
138
142 public function getUrl()
143 {
144 if (!$this->getField('LINK_PARAMS'))
145 {
146 return '';
147 }
148
149 $cashbox = Manager::getObjectById($this->getField('CASHBOX_ID'));
150 if (!$cashbox)
151 {
152 return '';
153 }
154
155 $ofd = $cashbox->getOfd();
156 if (!$ofd)
157 {
158 return '';
159 }
160
161 return $ofd->generateCheckLink($this->getField('LINK_PARAMS'));
162 }
163
167 public function setAvailableCashbox(array $cashboxList)
168 {
169 $this->cashboxList = $cashboxList;
170 }
171
177 public function setEntities(array $entities)
178 {
179 $this->entities = $entities;
180
181 $orderId = null;
182 $entityRegistryType = null;
183
184 foreach ($this->entities as $entity)
185 {
186 if ($entity instanceof Payment)
187 {
188 $this->fields['PAYMENT_ID'] = $entity->getId();
189 $this->fields['SUM'] = $entity->getSum();
190 $this->fields['CURRENCY'] = $entity->getField('CURRENCY');
191 }
192
193 // compatibility
194 if ($entity instanceof Shipment)
195 {
196 $this->fields['SHIPMENT_ID'] = $entity->getId();
197 }
198
199 if ($entityRegistryType === null)
200 {
201 $entityRegistryType = $entity::getRegistryType();
202 }
203 elseif ($entityRegistryType !== $entity::getRegistryType())
204 {
205 throw new Main\ArgumentTypeException('entities');
206 }
207
209 $collection = $entity->getCollection();
210
211 if ($orderId === null)
212 {
213 $orderId = $collection->getOrder()->getId();
214 }
215 elseif ($orderId != $collection->getOrder()->getId())
216 {
217 throw new Main\ArgumentTypeException('entities');
218 }
219 }
220
221 $this->fields['ORDER_ID'] = $orderId;
222 $this->fields['ENTITY_REGISTRY_TYPE'] = $entityRegistryType;
223 }
224
229 public function getEntities()
230 {
231 if ($this->entities)
232 {
233 return $this->entities;
234 }
235
236 $registry = Registry::getInstance($this->fields['ENTITY_REGISTRY_TYPE']);
237
238 if ($this->fields['ORDER_ID'] > 0)
239 {
240 $orderId = $this->fields['ORDER_ID'];
241 }
242 elseif ($this->fields['PAYMENT_ID'] > 0)
243 {
245 $paymentClassName = $registry->getPaymentClassName();
246 $dbRes = $paymentClassName::getList([
247 'filter' => [
248 'ID' => $this->fields['PAYMENT_ID']
249 ]
250 ]);
251 $data = $dbRes->fetch();
252 $orderId = $data['ORDER_ID'];
253 }
254 elseif ($this->fields['SHIPMENT_ID'] > 0)
255 {
257 $shipmentClassName = $registry->getShipmentClassName();
258 $dbRes = $shipmentClassName::getList([
259 'filter' => [
260 'ID' => $this->fields['SHIPMENT_ID']
261 ]
262 ]);
263 $data = $dbRes->fetch();
264 $orderId = $data['ORDER_ID'];
265 }
266 else
267 {
268 throw new Main\SystemException();
269 }
270
271 if ($orderId > 0)
272 {
273 $orderClassName = $registry->getOrderClassName();
274
276 $order = $orderClassName::load($orderId);
277 if ($order)
278 {
279 if ($this->fields['PAYMENT_ID'] > 0)
280 {
281 $payment = $order->getPaymentCollection()->getItemById($this->fields['PAYMENT_ID']);
282 if ($payment)
283 {
284 $this->entities[] = $payment;
285 }
286 }
287
288 if ($this->fields['SHIPMENT_ID'] > 0)
289 {
290 $shipment = $order->getShipmentCollection()->getItemById($this->fields['SHIPMENT_ID']);
291 if ($shipment)
292 {
293 $this->entities[] = $shipment;
294 }
295 }
296 }
297 }
298
299 return $this->entities;
300 }
301
306 public function save()
307 {
308 if ((int)$this->getField('ID') > 0)
309 {
310 return CashboxCheckTable::update($this->fields['ID'], $this->fields);
311 }
312
313 $this->fields['DATE_CREATE'] = new Main\Type\DateTime();
314
315 $result = CashboxCheckTable::add($this->fields);
316 if (!$result->isSuccess())
317 {
318 return $result;
319 }
320
321 $checkId = $result->getId();
322 $this->fields['ID'] = $checkId;
323
324 foreach ($this->cashboxList as $cashbox)
325 {
327 'CHECK_ID' => $checkId,
328 'CASHBOX_ID' => $cashbox['ID']
329 ]);
330 }
331
332 return $result;
333 }
334
338 public function linkCashbox($cashboxId)
339 {
340 $this->fields['CASHBOX_ID'] = $cashboxId;
341 }
342
346 public function init($settings)
347 {
348 $this->fields = $settings;
349 }
350
354 protected function extractData()
355 {
356 $result = $this->extractDataInternal();
357
358 $eventHandlerList = Main\EventManager::getInstance()->findEventHandlers('sale', self::EVENT_ON_CHECK_PREPARE_DATA);
359 foreach ($eventHandlerList as $event)
360 {
361 $result = ExecuteModuleEventEx($event, [$result, static::getType()]);
362 }
363
364 return $result;
365 }
366
367
376 protected function getVatIdByVatRate($vatRate)
377 {
378 static $vatList = array();
379
380 if (!$vatList)
381 {
382 if (Main\Loader::includeModule('catalog'))
383 {
384 $dbRes = Catalog\VatTable::getList(array('filter' => array('ACTIVE' => 'Y')));
385 while ($data = $dbRes->fetch())
386 {
387 $vatList[(int)$data['RATE']] = (int)$data['ID'];
388 }
389 }
390 }
391
392 if (!isset($vatList[$vatRate]))
393 {
394 return 0;
395 }
396
397 return $vatList[$vatRate];
398 }
399}
setAvailableCashbox(array $cashboxList)
static getInstance($type)
Definition registry.php:183