Bitrix-D7  20.5.0
abstractcheck.php
См. документацию.
1 <?php
2 
4 
5 use Bitrix\Main;
10 use Bitrix\Catalog;
16 
17 /**
18  * Class AbstractCheck
19  * @package Bitrix\Sale\Cashbox
20  */
21 abstract 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 
49  /** @var array $fields */
50  protected $fields = array();
51 
52  /** @var array $cashboxList */
53  protected $cashboxList = array();
54 
55  /** @var CollectableEntity[] $entities */
56  protected $entities = array();
57 
58  /**
59  * @return string
60  */
61  abstract public static function getType();
62 
63  /**
64  * @return string
65  */
66  abstract public static function getCalculatedSign();
67 
68  /**
69  * @return string
70  */
71  abstract public static function getName();
72 
73  /**
74  * @return array
75  */
76  abstract public function getDataForCheck();
77 
78  /**
79  * @return array
80  */
81  abstract protected function extractDataInternal();
82 
83  /**
84  * @return string
85  */
86  abstract static function getSupportedEntityType();
87 
88  /**
89  * @param string $handler
90  * @return null|Check
91  */
92  public static function create($handler)
93  {
94  if (class_exists($handler))
95  {
96  return new $handler();
97  }
98 
99  return null;
100  }
101 
102  /**
103  * Check constructor.
104  */
105  protected function __construct()
106  {
107  $this->fields['TYPE'] = static::getType();
108  }
109 
110  /**
111  * @param $name
112  * @return mixed
113  */
114  public function getField($name)
115  {
116  return $this->fields[$name];
117  }
118 
119  /**
120  * @param $name
121  * @param $value
122  */
123  public function setField($name, $value)
124  {
125  $this->fields[$name] = $value;
126  }
127 
128  /**
129  * @param $fields
130  */
131  public function setFields($fields)
132  {
133  foreach ($fields as $name => $value)
134  {
135  $this->setField($name, $value);
136  }
137  }
138 
139  /**
140  * @return string
141  */
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 
164  /**
165  * @param array $cashboxList
166  */
167  public function setAvailableCashbox(array $cashboxList)
168  {
169  $this->cashboxList = $cashboxList;
170  }
171 
172  /**
173  * @param array $entities
174  * @throws Main\ArgumentTypeException
175  * @throws Main\NotImplementedException
176  */
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 
208  /** @var PaymentCollection|ShipmentCollection $collection */
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 
225  /**
226  * @return array|CollectableEntity[]
227  * @throws Main\SystemException
228  */
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  {
244  /** @var Payment $paymentClassName */
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  {
256  /** @var Shipment $shipmentClassName */
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 
275  /** @var Order $order */
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 
302  /**
303  * @return Main\ORM\Data\AddResult|Main\ORM\Data\UpdateResult
304  * @throws \Exception
305  */
306  public function save()
307  {
308  if ((int)$this->fields['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 
335  /**
336  * @param $cashboxId
337  */
338  public function linkCashbox($cashboxId)
339  {
340  $this->fields['CASHBOX_ID'] = $cashboxId;
341  }
342 
343  /**
344  * @param $settings
345  */
346  public function init($settings)
347  {
348  $this->fields = $settings;
349  }
350 
351  /**
352  * @return array|null
353  */
354  protected function extractData()
355  {
356  $result = $this->extractDataInternal();
357 
358  $event = new Main\Event(
359  'sale',
360  self::EVENT_ON_CHECK_PREPARE_DATA,
361  [$result, static::getType()]
362  );
363  $event->send();
364 
365  if ($event->getResults())
366  {
367  foreach ($event->getResults() as $eventResult)
368  {
369  if ($eventResult->getType() !== Main\EventResult::ERROR)
370  {
371  $result = $eventResult->getParameters();
372  }
373  }
374  }
375 
376  return $result;
377  }
378 
379 
380  /**
381  * @param $vatRate
382  * @return int|mixed
383  * @throws Main\ArgumentException
384  * @throws Main\LoaderException
385  * @throws Main\ObjectPropertyException
386  * @throws Main\SystemException
387  */
388  protected function getVatIdByVatRate($vatRate)
389  {
390  static $vatList = array();
391 
392  if (!$vatList)
393  {
394  if (Main\Loader::includeModule('catalog'))
395  {
396  $dbRes = Catalog\VatTable::getList(array('filter' => array('ACTIVE' => 'Y')));
397  while ($data = $dbRes->fetch())
398  {
399  $vatList[(int)$data['RATE']] = (int)$data['ID'];
400  }
401  }
402  }
403 
404  if (!isset($vatList[$vatRate]))
405  {
406  return 0;
407  }
408 
409  return $vatList[$vatRate];
410  }
411 }
Exception is thrown when the type of an argument is not accepted by function.
static includeModule($moduleName)
Includes a module by its name.
static add(array $data)
Adds row to entity table.
static update($primary, array $data)
Updates row in entity table by primary key.
Base class for fatal exceptions.
setAvailableCashbox(array $cashboxList)