Bitrix-D7  20.5.0
sale/lib/internals/catalog/provider.php
См. документацию.
1 <?php
2 
3 
5 
6 
7 use Bitrix\Main;
8 use Bitrix\Sale;
11 
12 /**
13  * Class Provider
14  *
15  * @package Bitrix\Sale\Internals
16  */
17 final class Provider
18 {
19 
23 
24  private static $ignoreErrors = false;
25 
26 
27  /**
28  * @param $basketList
29  * @param array $context
30  *
31  * @return Sale\Result
32  * @throws Main\ArgumentNullException
33  * @throws Main\ArgumentTypeException
34  */
35  public static function getProductData($basketList, array $context)
36  {
37  $result = new Sale\Result();
38 
39  if (empty($context))
40  {
41  throw new Main\ArgumentNullException('context');
42  }
43 
44  if (!is_array($basketList) && !($basketList instanceof Sale\BasketBase))
45  {
46  throw new Main\ArgumentTypeException('basketList');
47  }
48 
49  $creator = Sale\Internals\ProviderCreator::create($context);
50  /** @var Sale\BasketItem $basketItem */
51  foreach ($basketList as $basketItem)
52  {
53  $creator->addBasketItem($basketItem);
54  }
55 
56  $r = $creator->getProductData();
57  if ($r->isSuccess())
58  {
59  $data = $r->getData();
60  if (array_key_exists('PRODUCT_DATA_LIST', $data))
61  {
62  $result->setData($data);
63  }
64  }
65  else
66  {
67  $result->addErrors($r->getErrors());
68  }
69 
70  return $result;
71  }
72 
73  /**
74  * @param Sale\BasketItemBase $basketItem
75  * @param array $context
76  *
77  * @return Sale\Result
78  * @throws Main\ArgumentNullException
79  */
80  public static function getBundleItems(Sale\BasketItemBase $basketItem, array $context)
81  {
82  if (empty($context))
83  {
84  throw new Main\ArgumentNullException('context');
85  }
86 
87  $result = new Sale\Result();
88 
89  $creator = Sale\Internals\ProviderCreator::create($context);
90  /** @var Sale\BasketItem $basketItem */
91  $creator->addBasketItem($basketItem);
92 
93  $r = $creator->getBundleItems();
94  if ($r->isSuccess())
95  {
96  return $r;
97  }
98  else
99  {
100  $result->addErrors($r->getErrors());
101  }
102 
103  return $result;
104  }
105 
106  /**
107  * @param Sale\BasketItemBase $basketItem
108  * @param array|null $context
109  *
110  * @return Sale\Result
111  * @throws Main\ObjectNotFoundException
112  * @throws Main\SystemException
113  */
114  public static function checkAvailableQuantityByBasketItem(Sale\BasketItemBase $basketItem, array $context = array())
115  {
116  $result = new Sale\Result();
117 
118  /** @var \Bitrix\Sale\Basket $basket */
119  if (!$basket = $basketItem->getCollection())
120  {
121  throw new Main\ObjectNotFoundException('Entity "Basket" not found');
122  }
123 
124  $order = $basket->getOrder();
125  if (empty($context) && $order)
126  {
127  $context = static::prepareContext($order, $context);
128  }
129 
130  $r = static::checkContext($context);
131  if (!$r->isSuccess())
132  {
133  return $r;
134  }
135 
136  $quantity = $basketItem->getQuantity();
137  $productId = $basketItem->getProductId();
138  $poolQuantity = 0;
139 
140  if ($order)
141  {
142  $fields = $basketItem->getFields();
143  $originalValues = $fields->getOriginalValues();
144  $values = $fields->getValues();
145  $deltaQuantity = floatval($values['QUANTITY']) - floatval($originalValues['QUANTITY']);
146 
147  if ($deltaQuantity <= 0)
148  {
149  $result->setData(
150  array(
151  'AVAILABLE_QUANTITY' => $quantity
152  )
153  );
154 
155  return $result;
156  }
157 
158  $pool = PoolQuantity::getInstance($order->getInternalId());
159  $poolQuantity = $pool->get(PoolQuantity::POOL_RESERVE_TYPE, $productId);
160  if (!empty($poolQuantity))
161  {
162  $tryQuantity = $quantity + $poolQuantity;
163  if ($tryQuantity == 0)
164  {
165  $result->setData(array(
166  'AVAILABLE_QUANTITY' => $quantity
167  ));
168 
169  return $result;
170  }
171  }
172  }
173 
174 
175  $resultList = array();
176 
177  $creator = Sale\Internals\ProviderCreator::create($context);
178 
179  /** @var Sale\BasketItem $basketItem */
180  $creator->addBasketItem($basketItem);
181 
182  $r = $creator->getAvailableQuantity();
183  if ($r->isSuccess())
184  {
185  $providerName = null;
186  $providerName = $basketItem->getProviderName();
187 
188  if (strval(trim($providerName)) == '')
189  {
190  $providerName = $basketItem->getCallbackFunction();
191  }
192 
193  if (!empty($providerName) && $providerName[0] == "\\")
194  {
195  $providerName = ltrim($providerName, '\\');
196  }
197 
198  $checkProviderName = $providerName;
199  $data = $r->getData();
200  if (array_key_exists('AVAILABLE_QUANTITY_LIST', $data) && isset($data['AVAILABLE_QUANTITY_LIST'][$checkProviderName]))
201  {
202  $resultList = $data['AVAILABLE_QUANTITY_LIST'][$checkProviderName];
203  }
204  }
205  else
206  {
207  $result->addErrors($r->getErrors());
208  }
209 
210  if (isset($resultList[$productId]))
211  {
212  $result->setData(
213  array(
214  'AVAILABLE_QUANTITY' => $resultList[$productId] - $poolQuantity
215  )
216  );
217  }
218 
219  return $result;
220  }
221 
222  /**
223  * @param Sale\BasketItemBase $basketItem
224  * @param array|null $context
225  *
226  * @return Sale\Result
227  * @throws Main\ObjectNotFoundException
228  * @throws Main\SystemException
229  */
230  public static function getAvailableQuantityAndPriceByBasketItem(Sale\BasketItemBase $basketItem, array $context = array())
231  {
232  $result = new Sale\Result();
233 
234  /** @var \Bitrix\Sale\Basket $basket */
235  if (!$basket = $basketItem->getCollection())
236  {
237  throw new Main\ObjectNotFoundException('Entity "Basket" not found');
238  }
239 
240  $order = $basket->getOrder();
241 
242  if (empty($context) && !$order)
243  {
244  $context = $basket->getContext();
245  }
246 
247  if (empty($context) && $order)
248  {
249  $context = static::prepareContext($order, $context);
250  }
251 
252  $r = static::checkContext($context);
253  if (!$r->isSuccess())
254  {
255  return $r;
256  }
257 
258  $resultData = array();
259 
260  $creator = Sale\Internals\ProviderCreator::create($context);
261 
262  /** @var Sale\BasketItem $basketItem */
263  $creator->addBasketItem($basketItem);
264 
265  $r = $creator->getAvailableQuantityAndPrice();
266  if ($r->isSuccess())
267  {
268  $providerName = $basketItem->getProviderName();
269 
270  if (strval($providerName) == '')
271  {
272  $providerName = $basketItem->getCallbackFunction();
273  }
274 
275  $providerName = static::clearProviderName($providerName);
276 
277  $checkProviderName = $providerName;
278  $data = $r->getData();
279  if (array_key_exists('PRODUCT_DATA_LIST', $data) && isset($data['PRODUCT_DATA_LIST'][$checkProviderName]))
280  {
281  $productData = $data['PRODUCT_DATA_LIST'][$checkProviderName];
282 
283  if (isset($productData['PRICE_LIST'][$basketItem->getProductId()][$basketItem->getBasketCode()]))
284  {
285  $resultData['PRICE_DATA'] = $productData['PRICE_LIST'][$basketItem->getProductId()][$basketItem->getBasketCode()];
286  }
287 
288  if (isset($productData['AVAILABLE_QUANTITY_LIST'][$basketItem->getProductId()]))
289  {
290  $resultData['AVAILABLE_QUANTITY'] = $productData['AVAILABLE_QUANTITY_LIST'][$basketItem->getProductId()];
291  }
292  }
293  }
294  else
295  {
296  $result->addErrors($r->getErrors());
297  }
298 
299  if (isset($resultData))
300  {
301  $result->setData(
302  $resultData
303  );
304  }
305 
306  return $result;
307  }
308 
309  /**
310  * @param Sale\Shipment $shipment
311  * @param array $context
312  *
313  * @return Sale\Result
314  * @throws Main\ArgumentNullException
315  * @throws Main\ObjectNotFoundException
316  */
317  public static function tryReserveShipment(Sale\Shipment $shipment, array $context = array())
318  {
319  $result = new Sale\Result();
320 
321  /** @var Sale\ShipmentCollection $shipmentCollection */
322  if (!$shipmentCollection = $shipment->getCollection())
323  {
324  throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
325  }
326 
327  /** @var Sale\Order $order */
328  $order = $shipmentCollection->getOrder();
329  if (!$order)
330  {
331  throw new Main\ObjectNotFoundException('Entity "Order" not found');
332  }
333 
334  $context = static::prepareContext($order, $context);
335  $r = static::checkContext($context);
336  if (!$r->isSuccess())
337  {
338  return $r;
339  }
340 
341  $pool = PoolQuantity::getInstance($order->getInternalId());
342 
343  /** @var Sale\ShipmentItemCollection $shipmentCollection */
344  $shipmentItemCollection = $shipment->getShipmentItemCollection();
345 
346  $availableQuantityList = array();
347  $needQuantityList = array();
348 
349  /** @var Sale\Result $r */
350  $r = static::getNeedQuantityByShipmentItemCollection($shipmentItemCollection);
351  if (!$r->isSuccess())
352  {
353  $result->addErrors($r->getErrors());
354  }
355  else
356  {
357  $data = $r->getData();
358  if (!empty($data['NEED_QUANTITY_LIST']))
359  {
360  $needQuantityList = $data['NEED_QUANTITY_LIST'];
361  }
362  }
363 
364  if ($r->hasWarnings())
365  {
366  $result->addWarnings($r->getWarnings());
367  }
368 
369  if ($shipmentItemCollection->count() == 0)
370  {
371  return $result;
372  }
373 
374  $creator = Sale\Internals\ProviderCreator::create($context);
375  /** @var Sale\ShipmentItem $shipmentItem */
376  foreach ($shipmentItemCollection as $shipmentItem)
377  {
378  $shipmentProductData = $creator->createItemForReserve($shipmentItem);
379  $creator->addShipmentProductData($shipmentProductData);
380  }
381 
382  $r = $creator->getAvailableQuantity();
383  if ($r->isSuccess())
384  {
385  $data = $r->getData();
386  if (array_key_exists('AVAILABLE_QUANTITY_LIST', $data))
387  {
388  $availableQuantityList = $data['AVAILABLE_QUANTITY_LIST'] + $availableQuantityList;
389  }
390  }
391  else
392  {
393  $result->addErrors($r->getErrors());
394  }
395 
396  if ($r->hasWarnings())
397  {
398  $result->addWarnings($r->getWarnings());
399  }
400 
401  if (!empty($needQuantityList) && $result->isSuccess())
402  {
403  $applyItemsList = array();
404 
405  foreach ($availableQuantityList as $providerName => $productAvailableQuantityList)
406  {
407  $providerName = trim($providerName);
408  foreach ($productAvailableQuantityList as $productId => $productAvailableQuantity)
409  {
410  if (array_key_exists($productId, $needQuantityList[$providerName]))
411  {
413  {
414  $poolQuantity = 0;
415  if ($order->getId() > 0)
416  {
417  $poolQuantity = (float)$pool->get(PoolQuantity::POOL_RESERVE_TYPE, $productId);
418  }
419  $needQuantity = $needQuantityList[$providerName][$productId];
420 
421  $productAvailableQuantity -= $poolQuantity;
422  $reservedQuantity = ($needQuantity >= $productAvailableQuantity ? $productAvailableQuantity : $needQuantity);
423 
424  $applyItemsList[$providerName][$productId] = $reservedQuantity;
425  }
426  }
427  else
428  {
429  /** @var Sale\ShipmentItem $shipmentItem */
430  foreach ($shipmentItemCollection as $shipmentItem)
431  {
432  $basketItem = $shipmentItem->getBasketItem();
433 
434  if ($basketItem->getProductId() == $productId)
435  {
436  $result->addWarning( new Sale\ResultWarning(Main\Localization\Loc::getMessage('SALE_PROVIDER_RESERVE_SHIPMENT_ITEM_WRONG_AVAILABLE_QUANTITY', array(
437  '#PRODUCT_NAME#' => $basketItem->getField('NAME')
438  )), 'SALE_PROVIDER_RESERVE_SHIPMENT_ITEM_WRONG_AVAILABLE_QUANTITY') );
439  break;
440  }
441  }
442  }
443  }
444 
445  }
446 
447  if (!empty($applyItemsList))
448  {
449  $shipmentProductIndex = static::createProductShipmentItemMapByShipmentItemCollection($shipmentItemCollection);
450 
451  /** @var Sale\Shipment $shipment */
452  if (!$shipment = $shipmentItemCollection->getShipment())
453  {
454  throw new Main\ObjectNotFoundException('Entity "ShipmentItemCollectionCollection" not found');
455  }
456 
457  /** @var Sale\ShipmentCollection $shipmentCollection */
458  if (!$shipmentCollection = $shipment->getCollection())
459  {
460  throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
461  }
462 
463  /** @var Sale\Order $order */
464  if (!$order = $shipmentCollection->getOrder())
465  {
466  throw new Main\ObjectNotFoundException('Entity "Order" not found');
467  }
468 
469  $pool = PoolQuantity::getInstance($order->getInternalId());
470 
471  static::setAvailableQuantityToShipmentItemCollection($pool, $shipmentProductIndex, $applyItemsList);
472  }
473  }
474 
475  return $result;
476  }
477 
478  /**
479  * @param Sale\ShipmentItem $shipmentItem
480  * @param array $context
481  *
482  * @return Sale\Result
483  * @throws Main\ArgumentNullException
484  * @throws Main\ArgumentOutOfRangeException
485  * @throws Main\NotSupportedException
486  * @throws Main\ObjectNotFoundException
487  * @throws \Exception
488  */
489  public static function tryReserveShipmentItem(Sale\ShipmentItem $shipmentItem, array $context = array())
490  {
491  $result = new Sale\Result();
492 
493  /** @var Sale\ShipmentItemCollection $shipmentItemCollection */
494  $shipmentItemCollection = $shipmentItem->getCollection();
495  if (!$shipmentItemCollection)
496  {
497  throw new Main\ObjectNotFoundException('Entity "ShipmentItemCollection" not found');
498  }
499 
500  /** @var Sale\Shipment $shipment */
501  $shipment = $shipmentItemCollection->getShipment();
502  if (!$shipment)
503  {
504  throw new Main\ObjectNotFoundException('Entity "Shipment" not found');
505  }
506 
507  /** @var Sale\ShipmentCollection $shipmentCollection */
508  $shipmentCollection = $shipment->getCollection();
509  if (!$shipmentCollection)
510  {
511  throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
512  }
513 
514  /** @var Sale\Order $order */
515  $order = $shipmentCollection->getOrder();
516  if (!$order)
517  {
518  throw new Main\ObjectNotFoundException('Entity "Order" not found');
519  }
520 
521  $context = static::prepareContext($order, $context);
522  $r = static::checkContext($context);
523  if (!$r->isSuccess())
524  {
525  return $r;
526  }
527 
528  $pool = PoolQuantity::getInstance($order->getInternalId());
529 
530  /** @var Sale\ShipmentItemCollection $shipmentCollection */
531  $shipmentItemCollection = $shipment->getShipmentItemCollection();
532 
533  $availableQuantityList = [];
534  $needQuantityList = [];
535 
536  $r = static::getNeedQuantityByShipmentItem($shipmentItem);
537  if (!$r->isSuccess())
538  {
539  $result->addErrors($r->getErrors());
540  }
541  else
542  {
543  $data = $r->getData();
544  if (!empty($data['NEED_QUANTITY_LIST']))
545  {
546  $needQuantityList = $data['NEED_QUANTITY_LIST'];
547  }
548  }
549 
550  $creator = Sale\Internals\ProviderCreator::create($context);
551 
552  $shipmentProductData = $creator->createItemForReserve($shipmentItem);
553  $creator->addShipmentProductData($shipmentProductData);
554 
555  $r = $creator->getAvailableQuantity();
556  if ($r->isSuccess())
557  {
558  $data = $r->getData();
559  if (array_key_exists('AVAILABLE_QUANTITY_LIST', $data))
560  {
561  $availableQuantityList = $data['AVAILABLE_QUANTITY_LIST'];
562  }
563  }
564  else
565  {
566  $result->addErrors($r->getErrors());
567  }
568 
569  if ($r->hasWarnings())
570  {
571  $result->addWarnings($r->getWarnings());
572  }
573 
574  if (!empty($needQuantityList) && $result->isSuccess())
575  {
576  $applyItemsList = [];
577 
578  foreach ($availableQuantityList as $providerName => $productAvailableQuantityList)
579  {
580  $providerName = trim($providerName);
581  foreach ($productAvailableQuantityList as $productId => $productAvailableQuantity)
582  {
583  if (array_key_exists($productId, $needQuantityList[$providerName]))
584  {
586  {
587  $poolQuantity = 0;
588  if ($order->getId() > 0)
589  {
590  $poolQuantity = (float)$pool->get(PoolQuantity::POOL_RESERVE_TYPE, $productId);
591  }
592  $needQuantity = $needQuantityList[$providerName][$productId];
593 
594  $productAvailableQuantity -= $poolQuantity;
595  $reservedQuantity = ($needQuantity >= $productAvailableQuantity ? $productAvailableQuantity : $needQuantity);
596 
597  $applyItemsList[$providerName][$productId] = $reservedQuantity;
598  }
599  }
600  else
601  {
602  /** @var Sale\ShipmentItem $shipmentItem */
603  $basketItem = $shipmentItem->getBasketItem();
604 
605  if ($basketItem->getProductId() == $productId)
606  {
607  $result->addWarning( new Sale\ResultWarning(Main\Localization\Loc::getMessage('SALE_PROVIDER_RESERVE_SHIPMENT_ITEM_WRONG_AVAILABLE_QUANTITY', array(
608  '#PRODUCT_NAME#' => $basketItem->getField('NAME')
609  )), 'SALE_PROVIDER_RESERVE_SHIPMENT_ITEM_WRONG_AVAILABLE_QUANTITY') );
610  break;
611  }
612  }
613  }
614 
615  }
616 
617  if (!empty($applyItemsList))
618  {
619  $shipmentProductIndex = static::createProductShipmentItemMapByShipmentItem($shipmentItem);
620 
621  /** @var Sale\Shipment $shipment */
622  if (!$shipment = $shipmentItemCollection->getShipment())
623  {
624  throw new Main\ObjectNotFoundException('Entity "ShipmentItemCollectionCollection" not found');
625  }
626 
627  /** @var Sale\ShipmentCollection $shipmentCollection */
628  if (!$shipmentCollection = $shipment->getCollection())
629  {
630  throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
631  }
632 
633  /** @var Sale\Order $order */
634  if (!$order = $shipmentCollection->getOrder())
635  {
636  throw new Main\ObjectNotFoundException('Entity "Order" not found');
637  }
638 
639  $pool = PoolQuantity::getInstance($order->getInternalId());
640  static::setAvailableQuantityToShipmentItemCollection($pool, $shipmentProductIndex, $applyItemsList);
641  }
642  }
643 
644  return $result;
645  }
646 
647  /**
648  * @param Sale\ShipmentItem $shipmentItem
649  *
650  * @return Sale\Result
651  * @throws Main\ArgumentOutOfRangeException
652  * @throws Main\NotSupportedException
653  * @throws Main\ObjectNotFoundException
654  * @throws \Exception
655  */
656  public static function tryUnreserveShipmentItem(Sale\ShipmentItem $shipmentItem)
657  {
658  $result = new Sale\Result();
659 
660  /** @var Sale\ShipmentItemCollection $shipmentItemCollection */
661  $shipmentItemCollection = $shipmentItem->getCollection();
662  if (!$shipmentItemCollection)
663  {
664  throw new Main\ObjectNotFoundException('Entity "ShipmentItemCollection" not found');
665  }
666 
667  /** @var Sale\Shipment $shipment */
668  $shipment = $shipmentItemCollection->getShipment();
669  if (!$shipment)
670  {
671  throw new Main\ObjectNotFoundException('Entity "Shipment" not found');
672  }
673 
674  /** @var Sale\ShipmentCollection $shipmentCollection */
675  $shipmentCollection = $shipment->getCollection();
676  if (!$shipmentCollection)
677  {
678  throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
679  }
680 
681  /** @var Sale\Order $order */
682  $order = $shipmentCollection->getOrder();
683  if (!$order)
684  {
685  throw new Main\ObjectNotFoundException('Entity "Order" not found');
686  }
687 
688  $pool = PoolQuantity::getInstance($order->getInternalId());
689 
690  /** @var Sale\BasketItem $basketItem */
691  $basketItem = $shipmentItem->getBasketItem();
692  if (!$basketItem)
693  {
694  throw new Main\ObjectNotFoundException('Entity "BasketItem" not found');
695  }
696 
697  $productId = $basketItem->getProductId();
698 
699  $reservedQuantity = $shipmentItem->getReservedQuantity();
700 
701  if ($reservedQuantity == 0)
702  {
703  return $result;
704  }
705 
706  $pool->add(Sale\Internals\PoolQuantity::POOL_RESERVE_TYPE, $productId, -1 * $reservedQuantity);
707  if ($shipmentItem)
708  {
709  $foundItem = false;
710  $poolItems = Sale\Internals\ItemsPool::get($order->getInternalId(), $productId);
711  if (!empty($poolItems))
712  {
713  /** @var Sale\Shipment $poolItem */
714  foreach ($poolItems as $poolItem)
715  {
716  if ($poolItem->getInternalIndex() == $shipmentItem->getInternalIndex())
717  {
718  $foundItem = true;
719  break;
720  }
721  }
722  }
723 
724  if (!$foundItem)
725  {
726  Sale\Internals\ItemsPool::add($order->getInternalId(), $productId, $shipmentItem);
727  }
728  }
729 
730  $r = $shipmentItem->setField('RESERVED_QUANTITY', $shipmentItem->getReservedQuantity() + -1 * $reservedQuantity);
731  if (!$r->isSuccess())
732  {
733  $result->addErrors($r->getErrors());
734  }
735 
736  return $result;
737  }
738 
739  /**
740  * @param Sale\ShipmentItemCollection $shipmentItemCollection
741  *
742  * @return Sale\Result
743  */
744  private static function getNeedQuantityByShipmentItemCollection(Sale\ShipmentItemCollection $shipmentItemCollection)
745  {
746  $result = new Sale\Result();
747 
748  if ($shipmentItemCollection->count() == 0)
749  {
750  return $result;
751  }
752 
753  $needQuantityList = array();
754  /** @var Sale\ShipmentItem $shipmentItem */
755  foreach ($shipmentItemCollection as $shipmentItem)
756  {
757  $basketItem = $shipmentItem->getBasketItem();
758  if (!$basketItem)
759  {
760  continue;
761  }
762 
763  $productId = $basketItem->getProductId();
764 
765  $providerName = $basketItem->getProviderName();
766  $providerName = static::clearProviderName($providerName);
767 
768  $needQuantity = ($shipmentItem->getQuantity() - $shipmentItem->getReservedQuantity());
769  if (!isset($needQuantityList[$providerName]) || !array_key_exists($productId, $needQuantityList[$providerName]))
770  {
771  $needQuantityList[$providerName][$productId] = 0;
772  }
773 
774  $needQuantityList[$providerName][$productId] += $needQuantity;
775  }
776 
777  if (!empty($needQuantityList))
778  {
779  $result->setData(
780  array(
781  'NEED_QUANTITY_LIST' => $needQuantityList,
782  )
783  );
784  }
785  return $result;
786  }
787 
788  /**
789  * @param Sale\ShipmentItem $shipmentItem
790  *
791  * @return Sale\Result
792  */
793  private static function getNeedQuantityByShipmentItem(Sale\ShipmentItem $shipmentItem)
794  {
795  $result = new Sale\Result();
796  $needQuantityData = array();
797 
798  $basketItem = $shipmentItem->getBasketItem();
799 
800  $productId = $basketItem->getProductId();
801 
802  $providerName = $basketItem->getProviderName();
803  $providerName = static::clearProviderName($providerName);
804 
805  $needQuantityData[$providerName][$productId] = ($shipmentItem->getQuantity() - $shipmentItem->getReservedQuantity());
806 
807  $result->setData(
808  array(
809  'NEED_QUANTITY_LIST' => $needQuantityData,
810  )
811  );
812 
813  return $result;
814  }
815 
816  /**
817  * @param Sale\Shipment $shipment
818  *
819  * @return Sale\Result
820  * @throws Main\ArgumentOutOfRangeException
821  * @throws Main\NotSupportedException
822  * @throws Main\ObjectNotFoundException
823  * @throws \Exception
824  */
825  public static function tryUnreserveShipment(Sale\Shipment $shipment)
826  {
827  $result = new Sale\Result();
828 
829  /** @var Sale\ShipmentCollection $shipmentCollection */
830  if (!$shipmentCollection = $shipment->getCollection())
831  {
832  throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
833  }
834 
835  /** @var Sale\Order $order */
836  if (!$order = $shipmentCollection->getOrder())
837  {
838  throw new Main\ObjectNotFoundException('Entity "Order" not found');
839  }
840 
841  $pool = PoolQuantity::getInstance($order->getInternalId());
842 
843  /** @var Sale\ShipmentItemCollection $shipmentCollection */
844  $shipmentItemCollection = $shipment->getShipmentItemCollection();
845 
846  /** @var Sale\ShipmentItem $shipmentItem */
847  foreach ($shipmentItemCollection as $shipmentItem)
848  {
849  /** @var Sale\BasketItem $basketItem */
850  if (!$basketItem = $shipmentItem->getBasketItem())
851  {
852  continue;
853  }
854 
855  $productId = $basketItem->getProductId();
856 
857  $reservedQuantity = $shipmentItem->getReservedQuantity();
858 
859  if ($reservedQuantity == 0)
860  continue;
861 
862  $pool->add(Sale\Internals\PoolQuantity::POOL_RESERVE_TYPE, $productId, -1 * $reservedQuantity);
863  if ($shipmentItem)
864  {
865  $foundItem = false;
866  $poolItems = Sale\Internals\ItemsPool::get($order->getInternalId(), $productId);
867  if (!empty($poolItems))
868  {
869  /** @var Sale\Shipment $poolItem */
870  foreach ($poolItems as $poolItem)
871  {
872  if ($poolItem->getInternalIndex() == $shipmentItem->getInternalIndex())
873  {
874  $foundItem = true;
875  break;
876  }
877  }
878  }
879 
880  if (!$foundItem)
881  {
882  Sale\Internals\ItemsPool::add($order->getInternalId(), $productId, $shipmentItem);
883  }
884  }
885 
886  $r = $shipmentItem->setField('RESERVED_QUANTITY', $shipmentItem->getReservedQuantity() + -1 * $reservedQuantity);
887  if (!$r->isSuccess())
888  {
889  $result->addErrors($r->getErrors());
890  }
891 
892  }
893 
894  return $result;
895  }
896  /**
897  * @param $needQuantity
898  * @param $reservedQuantity
899  *
900  * @return float|int
901  */
902  private static function countNeedQuantity($needQuantity, $reservedQuantity)
903  {
904  $setQuantity = $needQuantity;
905  if ($needQuantity >= $reservedQuantity)
906  {
907  $setQuantity = $reservedQuantity;
908  }
909 
910  return $setQuantity;
911  }
912 
913  /**
914  * @param Sale\ShipmentItemCollection $shipmentItemCollection
915  *
916  * @return array
917  */
918  private static function createProductShipmentItemMapByShipmentItemCollection(Sale\ShipmentItemCollection $shipmentItemCollection)
919  {
920  static $shipmentProductIndex = array();
921 
922  /** @var Sale\ShipmentItem $shipmentItem */
923  foreach ($shipmentItemCollection as $shipmentItem)
924  {
925  $shipmentProductIndexData = static::createProductShipmentItemMapByShipmentItem($shipmentItem);
926  $shipmentProductIndex = array_merge($shipmentProductIndex, $shipmentProductIndexData);
927  }
928 
929  return $shipmentProductIndex;
930  }
931 
932  /**
933  * @param Sale\ShipmentItem $shipmentItem
934  *
935  * @return array
936  */
937  private static function createProductShipmentItemMapByShipmentItem(Sale\ShipmentItem $shipmentItem)
938  {
939  static $shipmentProductIndex = array();
940 
941  /** @var Sale\ShipmentItem $shipmentItem */
942  $basketItem = $shipmentItem->getBasketItem();
943 
944  $providerName = $basketItem->getProviderName();
945  $providerName = static::clearProviderName($providerName);
946 
947  $productId = $basketItem->getProductId();
948  $index = $shipmentItem->getInternalIndex();
949 
950  $shipmentProductIndex[$providerName][$productId][$index] = $shipmentItem;
951 
952  return $shipmentProductIndex;
953  }
954 
955  /**
956  * @param PoolQuantity $pool
957  * @param array $shipmentProductIndex
958  * @param array $items
959  *
960  * @return Sale\Result
961  * @throws Main\ArgumentOutOfRangeException
962  * @throws Main\NotSupportedException
963  * @throws Main\ObjectNotFoundException
964  * @throws \Exception
965  */
966  private static function setAvailableQuantityToShipmentItemCollection(PoolQuantity $pool, array $shipmentProductIndex, array $items)
967  {
968  $result = new Sale\Result();
969 
970  foreach ($items as $providerName => $productsList)
971  {
972  foreach ($productsList as $productId => $reservedQuantity)
973  {
974  if (empty($shipmentProductIndex[$providerName][$productId]))
975  continue;
976 
977  /**
978  * @var $shipmetnItemIndex
979  * @var Sale\ShipmentItem $shipmentItem
980  */
981  foreach ($shipmentProductIndex[$providerName][$productId] as $shipmetnItemIndex => $shipmentItem)
982  {
983  $needQuantity = ($shipmentItem->getQuantity() - $shipmentItem->getReservedQuantity());
984 
985  $setQuantity = static::countNeedQuantity($needQuantity, $reservedQuantity);
986 
987  if ($needQuantity == 0 || $setQuantity == 0 )
988  continue;
989 
990  $reservedQuantity -= $setQuantity;
991 
992  $pool->add(Sale\Internals\PoolQuantity::POOL_RESERVE_TYPE, $productId, $setQuantity);
993 
994  /** @var Sale\ShipmentItemCollection $shipmentItemCollection */
995  $shipmentItemCollection = $shipmentItem->getCollection();
996  if (!$shipmentItemCollection)
997  {
998  throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
999  }
1000 
1001  /** @var Sale\Shipment $shipment */
1002  $shipment = $shipmentItemCollection->getShipment();
1003  if (!$shipment)
1004  {
1005  throw new Main\ObjectNotFoundException('Entity "Shipment" not found');
1006  }
1007 
1008  /** @var Sale\ShipmentCollection $shipmentCollection */
1009  if (!$shipmentCollection = $shipment->getCollection())
1010  {
1011  throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
1012  }
1013 
1014  /** @var Sale\Order $order */
1015  if (!$order = $shipmentCollection->getOrder())
1016  {
1017  throw new Main\ObjectNotFoundException('Entity "Order" not found');
1018  }
1019 
1020  $foundItem = false;
1021  $poolItems = Sale\Internals\ItemsPool::get($order->getInternalId(), $productId);
1022  if (!empty($poolItems))
1023  {
1024  /** @var Sale\Shipment $poolItem */
1025  foreach ($poolItems as $poolItem)
1026  {
1027  if ($poolItem->getInternalIndex() == $shipmentItem->getInternalIndex())
1028  {
1029  $foundItem = true;
1030  break;
1031  }
1032  }
1033  }
1034 
1035  if (!$foundItem)
1036  {
1037  Sale\Internals\ItemsPool::add($order->getInternalId(), $productId, $shipmentItem);
1038  }
1039 
1040 
1041  $r = $shipmentItem->setField('RESERVED_QUANTITY', $shipmentItem->getReservedQuantity() + $setQuantity);
1042  if (!$r->isSuccess())
1043  {
1044  $result->addErrors($r->getErrors());
1045  }
1046  }
1047 
1048  }
1049  }
1050  return $result;
1051  }
1052 
1053  /**
1054  * @param Sale\Shipment $shipment
1055  * @param array $context
1056  *
1057  * @return Sale\Result
1058  * @throws Main\ObjectNotFoundException
1059  */
1060  public static function tryShipShipment(Sale\Shipment $shipment, array $context = array())
1061  {
1062  $result = new Sale\Result();
1063 
1064  $context = static::prepareContextByShipment($shipment, $context);
1065 
1066  /** @var Sale\ShipmentCollection $shipmentCollection */
1067  $shipmentCollection = $shipment->getCollection();
1068  if (!$shipmentCollection)
1069  {
1070  throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
1071  }
1072 
1073  $order = $shipmentCollection->getOrder();
1074  if (!$order)
1075  {
1076  throw new Main\ObjectNotFoundException('Entity "Order" not found');
1077  }
1078 
1079  $pool = PoolQuantity::getInstance($order->getInternalId());
1080 
1081  /** @var Sale\ShipmentItemCollection $shipmentCollection */
1082  $shipmentItemCollection = $shipment->getShipmentItemCollection();
1083 
1084  $needShipList = array();
1085  $creator = Sale\Internals\ProviderCreator::create($context);
1086  /** @var Sale\ShipmentItem $shipmentItem */
1087  foreach ($shipmentItemCollection as $shipmentItem)
1088  {
1089  $creator->addShipmentItem($shipmentItem);
1090  }
1091 
1092  $r = $creator->isNeedShip();
1093  if ($r->isSuccess())
1094  {
1095  $data = $r->getData();
1096  if (array_key_exists('IS_NEED_SHIP', $data))
1097  {
1098  $needShipList = $data['IS_NEED_SHIP'] + $needShipList;
1099  }
1100  }
1101 
1102  $creator = Sale\Internals\ProviderCreator::create($context);
1103  /** @var Sale\ShipmentItem $shipmentItem */
1104  foreach ($shipmentItemCollection as $shipmentItem)
1105  {
1106  $shipmentProductData = $creator->createItemForShip($shipmentItem, $needShipList);
1107  $creator->addShipmentProductData($shipmentProductData);
1108  }
1109 
1110  $tryShipProductList = array();
1111 
1112  $isIgnoreErrors = false;
1113 
1114  $r = $creator->tryShip();
1115 
1116  $needSetAfterResult = false;
1117  if ($r->isSuccess())
1118  {
1119  if ($r->hasWarnings())
1120  {
1121  $result->addWarnings($r->getWarnings());
1122  }
1123  else
1124  {
1125  $needSetAfterResult = true;
1126  }
1127  }
1128  else
1129  {
1130  $result->addWarnings($r->getErrors());
1131 
1132  if (static::isIgnoreErrors())
1133  {
1134  $isIgnoreErrors = true;
1135  $needSetAfterResult = true;
1136  }
1137  else
1138  {
1139  $result->addErrors($r->getErrors());
1140  }
1141 
1142  }
1143 
1144  $data = $r->getData();
1145  if (array_key_exists('TRY_SHIP_PRODUCTS_LIST', $data))
1146  {
1147  $tryShipProductList = $data['TRY_SHIP_PRODUCTS_LIST'] + $tryShipProductList;
1148  }
1149 
1150  if ($needSetAfterResult && !empty($tryShipProductList))
1151  {
1152 
1153  if ($isIgnoreErrors)
1154  {
1155  foreach ($tryShipProductList as $providerName => &$productList)
1156  {
1157  $productList = array_fill_keys(array_keys($productList), true);
1158  }
1159  }
1160 
1161  $creator->setItemsResultAfterTryShip($pool, $tryShipProductList);
1162  }
1163 
1164  return $result;
1165  }
1166 
1167  /**
1168  * @param Sale\ShipmentItemCollection $shipmentItemCollection
1169  *
1170  * @return array
1171  * @throws Main\LoaderException
1172  * @throws Main\ObjectNotFoundException
1173  */
1174  public static function createProviderItemsMap(Sale\ShipmentItemCollection $shipmentItemCollection)
1175  {
1176  $providerProductList = array();
1177  /** @var Sale\ShipmentItem $shipmentItem */
1178  foreach ($shipmentItemCollection as $shipmentItem)
1179  {
1180  $basketItem = $shipmentItem->getBasketItem();
1181 
1182  if (!$basketItem)
1183  {
1184  throw new Main\ObjectNotFoundException('Entity "BasketItem" not found');
1185  }
1186  if ($basketItem->isBundleParent())
1187  {
1188  continue;
1189  }
1190 
1191  $productId = $basketItem->getProductId();
1192  $providerName = $basketItem->getProvider();
1193  if (!isset($providerProductList[$providerName][$productId]))
1194  {
1195  $fields = array(
1196  'PRODUCT_ID' => $productId,
1197  'QUANTITY' => floatval($shipmentItem->getQuantity()),
1198  'SHIPMENT_ITEM_LIST' => array(),
1199  'IS_BUNDLE_CHILD' => $basketItem->isBundleChild(),
1200  );
1201  }
1202  else
1203  {
1204  $fields = $providerProductList[$providerName][$productId];
1205  $fields['QUANTITY'] += floatval($shipmentItem->getQuantity());
1206  }
1207 
1208  $shipmentItemIndex = $shipmentItem->getInternalIndex();
1209  $barcodeStoreData = static::createMapShipmentItemStoreData($shipmentItem);
1210  if (!empty($barcodeStoreData))
1211  {
1212  $fields['STORE'][$shipmentItemIndex] = $barcodeStoreData;
1213 // $fields['STORE'][$shipmentItemIndex]['IS_BARCODE_MULTI'] = $basketItem->isBarcodeMulti();
1214  }
1215 
1216  $fields['SHIPMENT_ITEM_LIST'][$shipmentItemIndex] = $shipmentItem;
1217 
1218  $providerProductList[$providerName][$productId] = $fields;
1219  }
1220 
1221  return $providerProductList;
1222  }
1223 
1224  /**
1225  * @param array $rulesProducts
1226  *
1227  * @return array
1228  * @throws Main\ObjectNotFoundException
1229  */
1230  public static function createProviderItemsMapByRules(array $rulesProducts)
1231  {
1232  $providerProductList = array();
1233 
1234  foreach ($rulesProducts as $ruleData)
1235  {
1236  /** @var Sale\ShipmentItem $shipmentItem */
1237  $shipmentItem = $ruleData['SHIPMENT_ITEM'];
1238  if (!$shipmentItem)
1239  {
1240  throw new Main\ObjectNotFoundException('Entity "ShipmentItem" not found');
1241  }
1242 
1243 
1244  $productId = $ruleData['PRODUCT_ID'];
1245  $providerName = $ruleData['PROVIDER_NAME'];
1246  $shipmentItemIndex = $shipmentItem->getInternalIndex();
1247 
1248 
1249  if (!isset($providerProductList[$providerName][$productId]))
1250  {
1251  $fields = $ruleData;
1252  unset($fields['SHIPMENT_ITEM']);
1253  unset($fields['STORE']);
1254  unset($fields['NEED_RESERVE']);
1255  unset($fields['NEED_SHIP']);
1256  $fields['SHIPMENT_ITEM_LIST'] = array();
1257  }
1258  else
1259  {
1260  $fields = $providerProductList[$providerName][$productId];
1261  $fields['QUANTITY'] += $ruleData['QUANTITY'];
1262  }
1263 
1264  if (array_key_exists('NEED_RESERVE', $ruleData))
1265  {
1266  $fields['NEED_RESERVE'][$shipmentItemIndex] = $ruleData['NEED_RESERVE'];
1267  }
1268 
1269  if (array_key_exists('NEED_SHIP', $ruleData))
1270  {
1271  $fields['NEED_SHIP'][$shipmentItemIndex] = $ruleData['NEED_SHIP'];
1272  }
1273 
1274  if (!empty($ruleData['STORE']))
1275  {
1276  $fields['STORE'][$shipmentItemIndex] = $ruleData['STORE'][$shipmentItemIndex];
1277  }
1278 
1279  $fields['SHIPMENT_ITEM_LIST'][$shipmentItemIndex] = $shipmentItem;
1280 
1281  $providerProductList[$providerName][$productId] = $fields;
1282  }
1283 
1284  return $providerProductList;
1285  }
1286 
1287  /**
1288  * @internal
1289  * @param Sale\ShipmentItem $shipmentItem
1290  *
1291  * @return array|bool
1292  * @throws Main\ObjectNotFoundException
1293  */
1294  public static function createMapShipmentItemStoreData(Sale\ShipmentItem $shipmentItem)
1295  {
1296  $resultList = array();
1297 
1298  /** @var Sale\BasketItem $basketItem */
1299  if (!$basketItem = $shipmentItem->getBasketItem())
1300  {
1301  throw new Main\ObjectNotFoundException('Entity "BasketItem" not found');
1302  }
1303 
1304  if ($basketItem->isBundleParent())
1305  {
1306  return false;
1307  }
1308 
1309  /** @var Sale\ShipmentItemStoreCollection $shipmentItemStoreCollection */
1310  if (!$shipmentItemStoreCollection = $shipmentItem->getShipmentItemStoreCollection())
1311  {
1312  throw new Main\ObjectNotFoundException('Entity "ShipmentItemStoreCollection" not found');
1313  }
1314 
1315  if ($shipmentItemStoreCollection->count() > 0)
1316  {
1317  $countBarcode = 0;
1318  /** @var Sale\ShipmentItemStore $shipmentItemStore */
1319  foreach ($shipmentItemStoreCollection as $shipmentItemStore)
1320  {
1321  $productId = $basketItem->getProductId();
1322 
1323  $storeId = $shipmentItemStore->getStoreId();
1324 
1325  if (!isset($resultList[$storeId]))
1326  {
1327  $resultList[$storeId] = array(
1328  'PRODUCT_ID' => $productId,
1329  'QUANTITY' => 0,
1330  'STORE_ID' => $storeId,
1331  'IS_BARCODE_MULTI' => $basketItem->isBarcodeMulti(),
1332  'BARCODE' => array()
1333  );
1334  }
1335 
1336  $barcodeId = ($shipmentItemStore->getId() > 0)? $shipmentItemStore->getId() : 'n'.$countBarcode;
1337  $countBarcode++;
1338  $resultList[$storeId]['QUANTITY'] += $basketItem->isBarcodeMulti()? 1 : $shipmentItemStore->getQuantity();
1339  $resultList[$storeId]['BARCODE'][$barcodeId] = $shipmentItemStore->getBarcode();
1340 
1341  }
1342  }
1343 
1344  return $resultList;
1345  }
1346 
1347  /**
1348  * @internal
1349  * @param $shipmentItemList
1350  *
1351  * @return array
1352  * @throws Main\ObjectNotFoundException
1353  * @throws Main\SystemException
1354  */
1355  public static function createMapShipmentItemCollectionStoreData($shipmentItemList)
1356  {
1357  $resultList = array();
1358 
1359  /** @var Sale\ShipmentItem $shipmentItem */
1360  foreach ($shipmentItemList as $shipmentItem)
1361  {
1362  $basketCode = $shipmentItem->getBasketCode();
1363 
1364  if (!isset($resultList[$basketCode]))
1365  {
1366  $resultList[$basketCode] = array();
1367  }
1368 
1369  $map = static::createMapShipmentItemStoreData($shipmentItem);
1370  if (!empty($map) && is_array($map))
1371  {
1372  $resultList[$basketCode] = $map + $resultList[$basketCode];
1373  }
1374  }
1375 
1376  return $resultList;
1377  }
1378 
1379 
1380  /**
1381  * @param Sale\Order $order
1382  * @param array|null $context
1383  *
1384  * @return Sale\Result
1385  * @throws Main\SystemException
1386  */
1387  public static function save(Sale\Order $order, array $context = array())
1388  {
1389  $result = new Sale\Result();
1390 
1391  $context = static::prepareContext($order, $context);
1392 
1393  $r = static::checkContext($context);
1394  if (!$r->isSuccess())
1395  {
1396  $result->addErrors($r->getErrors());
1397  return $result;
1398  }
1399 
1400  $pool = PoolQuantity::getInstance($order->getInternalId());
1401  /** @var array $poolQuantitiesList */
1402  $poolQuantitiesList = $pool->getQuantities(PoolQuantity::POOL_QUANTITY_TYPE);
1403 
1404  /** @var array $poolReservationList */
1405  $poolReservationList = $pool->getQuantities(PoolQuantity::POOL_RESERVE_TYPE);
1406 
1407  if (empty($poolQuantitiesList) && empty($poolReservationList))
1408  return $result;
1409 
1410  $rulesMap = ShipmentRules::createOrderRuleMap($order, $pool);
1411 
1412  if (empty($rulesMap))
1413  {
1414  return $result;
1415  }
1416 
1417  $r = ShipmentRules::saveRules($rulesMap, $context);
1418  if (!$r->isSuccess())
1419  {
1420  $result->addErrors($r->getErrors());
1421  }
1422 
1423  if ($r->hasWarnings())
1424  {
1425  $result->addWarnings($r->getWarnings());
1426  }
1427 
1428  $pool->reset(PoolQuantity::POOL_QUANTITY_TYPE);
1429  $pool->reset(PoolQuantity::POOL_RESERVE_TYPE);
1430 
1431  return $result;
1432  }
1433 
1434 
1435  /**
1436  * @param Sale\Shipment $shipment
1437  *
1438  * @return Sale\Order
1439  * @throws Main\ObjectNotFoundException
1440  */
1441  public static function getOrderByShipment(Sale\Shipment $shipment)
1442  {
1443  /** @var Sale\ShipmentCollection $shipmentCollection */
1444  $shipmentCollection = $shipment->getCollection();
1445  if (!$shipmentCollection)
1446  {
1447  throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
1448  }
1449 
1450  $order = $shipmentCollection->getOrder();
1451  if (!$order)
1452  {
1453  throw new Main\ObjectNotFoundException('Entity "Order" not found');
1454  }
1455 
1456  return $order;
1457  }
1458 
1459 
1460 
1461  /**
1462  * @param Sale\Shipment $shipment
1463  * @param array $context
1464  *
1465  * @return array
1466  * @throws Main\ObjectNotFoundException
1467  */
1468  private static function prepareContextByShipment(Sale\Shipment $shipment, array $context = array())
1469  {
1470  $order = static::getOrderByShipment($shipment);
1471 
1472  if (empty($context))
1473  {
1474  $context = array(
1475  'SITE_ID' => $order->getSiteId(),
1476  'USER_ID' => $order->getUserId(),
1477  'CURRENCY' => $order->getCurrency(),
1478  );
1479  }
1480  else
1481  {
1482  if (empty($context['SITE_ID']))
1483  {
1484  $context['SITE_ID'] = $order->getSiteId();
1485  }
1486 
1487  if (empty($context['USER_ID']) && $order->getUserId() > 0)
1488  {
1489  $context['USER_ID'] = $order->getUserId();
1490  }
1491 
1492  if (empty($context['CURRENCY']))
1493  {
1494  $context['CURRENCY'] = $order->getCurrency();
1495  }
1496  }
1497 
1498  return $context;
1499  }
1500 
1501  /**
1502  * @param Sale\OrderBase $order
1503  * @param array $context
1504  *
1505  * @return array
1506  */
1507  private static function prepareContext(Sale\OrderBase $order, array $context = array())
1508  {
1509  if (empty($context))
1510  {
1511  $context = array(
1512  'SITE_ID' => $order->getSiteId(),
1513  'USER_ID' => $order->getUserId(),
1514  'CURRENCY' => $order->getCurrency(),
1515  );
1516  }
1517 
1518  if (!empty($context))
1519  {
1520  if (empty($context['SITE_ID']))
1521  {
1522  $context['SITE_ID'] = $order->getSiteId();
1523  }
1524 
1525  if (empty($context['USER_ID']) && $order->getUserId() > 0)
1526  {
1527  $context['USER_ID'] = $order->getUserId();
1528  }
1529 
1530  if (empty($context['CURRENCY']))
1531  {
1532  $context['CURRENCY'] = $order->getCurrency();
1533  }
1534  }
1535 
1536  return $context;
1537  }
1538 
1539  /**
1540  * @param array $context
1541  *
1542  * @return Sale\Result
1543  * @throws Main\ArgumentNullException
1544  */
1545  private static function checkContext(array $context)
1546  {
1547  $result = new Sale\Result();
1548 
1549  if (empty($context['SITE_ID']))
1550  {
1551  throw new Main\ArgumentNullException('SITE_ID');
1552  }
1553 
1554  if (empty($context['CURRENCY']))
1555  {
1556  throw new Main\ArgumentNullException('CURRENCY');
1557  }
1558 
1559  return $result;
1560  }
1561 
1562  /**
1563  * @param $module
1564  * @param $name
1565  *
1566  * @return string|null
1567  * @throws Main\LoaderException
1568  */
1569  public static function getProviderName($module, $name)
1570  {
1571  static $providerProxy = array();
1572  $code = $module."|".$name;
1573 
1574  if (array_key_exists($code, $providerProxy))
1575  {
1576  return $providerProxy[$code];
1577  }
1578 
1579  $providerName = null;
1580  if (strval($module) != '' && Main\Loader::includeModule($module) && class_exists($name))
1581  {
1582  $provider = static::getProviderEntity($name);
1583  if ($provider)
1584  {
1585  $providerName = $name;
1586  }
1587  }
1588 
1589  if ($providerName !== null)
1590  {
1591  $providerProxy[$code] = $providerName;
1592  }
1593 
1594  return $providerName;
1595  }
1596 
1597  /**
1598  * @param $name
1599  *
1600  * @return mixed|null
1601  */
1602  public static function getProviderEntity($name)
1603  {
1604  static $providerEntityProxy = array();
1605  if (array_key_exists($name, $providerEntityProxy))
1606  {
1607  return $providerEntityProxy[$name];
1608  }
1609 
1610  if (class_exists($name))
1611  {
1612  $productProvider = new $name();
1613  if ($productProvider instanceof Sale\SaleProviderBase || array_key_exists("IBXSaleProductProvider", class_implements($name)))
1614  {
1615  $providerEntityProxy[$name] = $productProvider;
1616  return $productProvider;
1617  }
1618  }
1619 
1620  return null;
1621  }
1622 
1623  /**
1624  * @param $name
1625  *
1626  * @return bool
1627  */
1628  public static function isProviderCallbackFunction($name)
1629  {
1630  return (array_key_exists("IBXSaleProductProvider", class_implements($name)));
1631  }
1632 
1633 
1634  /**
1635  * @param $providerName
1636  *
1637  * @return string
1638  */
1639  private static function clearProviderName($providerName)
1640  {
1641  if (mb_substr($providerName, 0, 1) == "\\")
1642  {
1643  $providerName = mb_substr($providerName, 1);
1644  }
1645 
1646  return trim($providerName);
1647  }
1648 
1649  /**
1650  * @internal
1651  * @param $value
1652  */
1653  public static function setIgnoreErrors($value)
1654  {
1655  static::$ignoreErrors = ($value === true);
1656  }
1657 
1658  /**
1659  * @internal
1660  * @return bool
1661  */
1662  public static function isIgnoreErrors()
1663  {
1664  return static::$ignoreErrors;
1665  }
1666 
1667 }
Exception is thrown when "empty" value is passed to a function that does not accept it as a valid arg...
Exception is thrown when the type of an argument is not accepted by function.
static includeModule($moduleName)
Includes a module by its name.
static getMessage($code, $replace=null, $language=null)
Returns translation by message code.
Definition: loc.php:29
Exception is thrown when an object is not present.
static getProductReservationCondition()
Returns current reservation condition.
static tryUnreserveShipmentItem(Sale\ShipmentItem $shipmentItem)
static tryReserveShipment(Sale\Shipment $shipment, array $context=array())
static getAvailableQuantityAndPriceByBasketItem(Sale\BasketItemBase $basketItem, array $context=array())
static checkAvailableQuantityByBasketItem(Sale\BasketItemBase $basketItem, array $context=array())
static createMapShipmentItemStoreData(Sale\ShipmentItem $shipmentItem)
static tryShipShipment(Sale\Shipment $shipment, array $context=array())
static getBundleItems(Sale\BasketItemBase $basketItem, array $context)
static createProviderItemsMap(Sale\ShipmentItemCollection $shipmentItemCollection)
static tryReserveShipmentItem(Sale\ShipmentItem $shipmentItem, array $context=array())
static save(Sale\Order $order, array $context=array())
static add($code, $type, $value)
Definition: poolbase.php:51
static get($code, $type)
Definition: poolbase.php:19
static saveRules(array $rules, array $context)
static createOrderRuleMap(Sale\Order $order, PoolQuantity $pool)
Class RestHandler \Handlers\PaySystem.