15 private $context = array();
16 private $pool = array();
23 public static function create(array $context)
25 $creator =
new static();
26 $creator->context = $context;
36 $providerName = $basketItem->getProviderName();
37 if (empty($providerName))
39 $providerName = $basketItem->getCallbackFunction();
41 $builder = $this->createBuilder($providerName);
42 $builder->addProductByBasketItem($basketItem);
50 $basketItem = $shipmentItem->getBasketItem();
56 $providerName = $basketItem->getProviderName();
57 if (empty($providerName))
59 $providerName = $basketItem->getCallbackFunction();
61 $builder = $this->createBuilder($providerName);
63 $builder->addProductByShipmentItem($shipmentItem);
71 $builder = $this->createBuilder($productData[
'PROVIDER_NAME']);
72 $builder->addProductData($productData);
81 $providerName = $basketItem->getProviderName();
82 if (empty($providerName))
84 $providerName = $basketItem->getCallbackFunction();
86 $builder = $this->createBuilder($providerName);
87 $builder->addBasketItemBarcodeData($barcodeParams);
98 $basketItem = $shipmentItem->getBasketItem();
100 $shipment = $shipmentItem->getCollection()->getShipment();
102 $quantity = $shipmentItem->getQuantity();
104 if ($shipment->needShip() ==
Sale\Internals\Catalog\Provider::SALE_TRANSFER_PROVIDER_SHIPMENT_NEED_SHIP)
112 $needUseReserve = $quantity < 0 || Sale\Configuration::isEnableAutomaticReservation();
114 $providerName = $basketItem->getProviderName();
115 $providerName = $this->clearProviderName($providerName);
116 if (empty($needShipList[$providerName]) && $shipmentItem->getReservedQuantity() > 0)
122 'PROVIDER_NAME' => $basketItem->getProviderName(),
123 'SHIPMENT_ITEM' => $shipmentItem,
124 'BASKET_ITEM' => $basketItem,
125 'QUANTITY' => $quantity,
126 'RESERVED_QUANTITY_BY_STORE' => [
127 $shipmentItem->getInternalIndex() => []
131 $storeData = Sale\Internals\Catalog\Provider::createMapShipmentItemStoreData($shipmentItem, $needUseReserve);
134 $item[
'STORE_DATA'] = $storeData;
136 $needReserveByStore = [];
137 foreach ($storeData as $data)
139 $item[
'RESERVED_QUANTITY_BY_STORE'][$shipmentItem->getInternalIndex()][$data[
'STORE_ID']] = $data[
'RESERVED_QUANTITY'];
141 $needReserveByStore[$data[
'STORE_ID']] = $data[
'RESERVED_QUANTITY'] > 0;
144 $item[
'RESERVED_QUANTITY'] = array_sum($item[
'RESERVED_QUANTITY_BY_STORE'][$shipmentItem->getInternalIndex()]);
145 $item[
'NEED_RESERVE'] = $item[
'RESERVED_QUANTITY'] > 0;
146 $item[
'NEED_RESERVE_BY_STORE'] = [
147 $shipmentItem->getInternalIndex() => $needReserveByStore
152 $item[
'RESERVED_QUANTITY'] = $needUseReserve ? $basketItem->getReservedQuantity() : 0;
153 $item[
'NEED_RESERVE'] = $item[
'RESERVED_QUANTITY'] > 0;
165 public function createItemForReserveByShipmentItem(
Sale\
ShipmentItem $shipmentItem) : array
167 $basketItem = $shipmentItem->getBasketItem();
170 'PROVIDER_NAME' => $basketItem->getProviderName(),
171 'BASKET_ITEM' => $basketItem,
172 'SHIPMENT_ITEM' => $shipmentItem,
174 'RESERVED_QUANTITY' => 0,
177 if (!$basketItem->isReservableItem())
182 $shipmentItemQuantity = $shipmentItem->getQuantity();
183 if ($shipmentItemQuantity == 0)
185 $storeId = Sale\Configuration::getDefaultStoreId();
187 $result[
'QUANTITY_BY_STORE'][$storeId] = $shipmentItemQuantity - $shipmentItem->getReservedQuantity();
188 $result[
'QUANTITY'] += $result[
'QUANTITY_BY_STORE'][$storeId];
190 $result[
'RESERVED_QUANTITY_BY_STORE'][$storeId] = $shipmentItem->getReservedQuantity();
191 $result[
'RESERVED_QUANTITY'] += $result[
'RESERVED_QUANTITY_BY_STORE'][$storeId];
195 $quantityStoreList = [];
198 $shipmentItemStoreCollection = $shipmentItem->getShipmentItemStoreCollection();
199 if ($shipmentItemStoreCollection)
202 foreach ($shipmentItemStoreCollection as $itemStore)
204 $storeId = $itemStore->getStoreId();
205 if (!isset($quantityStoreList[$storeId]))
207 $quantityStoreList[$storeId] = 0;
210 $quantityStoreList[$storeId] += $itemStore->getQuantity();
211 $shipmentItemQuantity -= $itemStore->getQuantity();
215 if ($shipmentItemQuantity > 0)
217 $storeId = Sale\Configuration::getDefaultStoreId();
218 if (!isset($quantityStoreList[$storeId]))
223 $quantityStoreList[
$storeId] += $shipmentItemQuantity;
226 $result[
'QUANTITY_BY_STORE'] = [];
227 $result[
'RESERVED_QUANTITY_BY_STORE'] = [];
229 $shipmentItemReservedQuantity = $shipmentItem->getReservedQuantity();
230 foreach ($quantityStoreList as $storeId => $quantity)
232 $reserveQuantity = 0;
235 $reserveQuantityCollection = $basketItem->getReserveQuantityCollection();
237 if ($reserveQuantityCollection && $shipmentItemReservedQuantity > 0)
239 $reserveQuantity = $reserveQuantityCollection->getQuantityByStoreId($storeId);
240 if ($reserveQuantity > $shipmentItemReservedQuantity)
242 $reserveQuantity = $shipmentItemReservedQuantity;
245 $shipmentItemReservedQuantity -= $reserveQuantity;
248 $result[
'QUANTITY_BY_STORE'][
$storeId] = $quantity - $reserveQuantity;
249 $result[
'QUANTITY'] += $result[
'QUANTITY_BY_STORE'][
$storeId];
251 $result[
'RESERVED_QUANTITY_BY_STORE'][
$storeId] = $reserveQuantity;
252 $result[
'RESERVED_QUANTITY'] += $result[
'RESERVED_QUANTITY_BY_STORE'][
$storeId];
266 $basketItem = $reserve->getCollection()->getBasketItem();
268 $originalFields = $reserve->getFields()->getOriginalValues();
269 $reservedQuantity = $originalFields[
'QUANTITY'] ?? 0;
270 $quantity = $reserve->getField(
'QUANTITY') - $reservedQuantity;
273 'PROVIDER_NAME' => $basketItem->getProviderName(),
274 'RESERVE_ITEM' => $reserve,
275 'BASKET_ITEM' => $basketItem,
276 'QUANTITY' => $quantity,
277 'QUANTITY_BY_STORE' => [
278 $reserve->getStoreId() => $quantity
280 'RESERVED_QUANTITY' => $reservedQuantity,
281 'RESERVED_QUANTITY_BY_STORE' => [
282 $reserve->getStoreId() => $reservedQuantity
292 return $this->callBuilderMethod(
'getProductData', [
'PRODUCT_DATA_LIST']);
300 return $this->callBuilderMethod(
'getAvailableQuantity', [
'AVAILABLE_QUANTITY_LIST']);
308 return $this->callBuilderMethod(
'getAvailableQuantityByStore', [
'AVAILABLE_QUANTITY_LIST_BY_STORE']);
316 return $this->callBuilderMethod(
'getAvailableQuantityAndPrice', [
'PRODUCT_DATA_LIST']);
325 public function setItemsResultAfterTryShip(
PoolQuantity $pool, array $productTryShipList)
330 foreach ($this->pool as $builder)
332 $providerName = $builder->getProviderName();
334 if (!$productTryShipList[$providerName])
339 $r = $builder->setItemsResultAfterTryShip($pool, $productTryShipList[$providerName]);
340 if (!$r->isSuccess())
342 $result->addErrors($r->getErrors());
356 return $this->callBuilderMethod(
'setItemsResultAfterShip', [
'RESULT_AFTER_SHIP_LIST'], $resultAfterShip);
366 return $this->callBuilderMethod(
'createItemsResultAfterDeliver', [
'RESULT_AFTER_DELIVER_LIST'], $resultAfterDeliver);
375 return $this->callBuilderMethod(
'tryShip', [
'TRY_SHIP_PRODUCTS_LIST']);
383 return $this->callBuilderMethod(
'isNeedShip', [
'IS_NEED_SHIP']);
392 return $this->callBuilderMethod(
'checkBarcode', [
'BARCODE_CHECK_LIST']);
400 return $this->callBuilderMethod(
'reserve', [
'RESERVED_PRODUCTS_LIST']);
408 return $this->callBuilderMethod(
'ship', [
'SHIPPED_PRODUCTS_LIST']);
416 return $this->callBuilderMethod(
'getBundleItems', [
'BUNDLE_LIST']);
424 return $this->callBuilderMethod(
'deliver', [
'DELIVER_PRODUCTS_LIST']);
432 return $this->callBuilderMethod(
'viewProduct', [
'VIEW_PRODUCTS_LIST']);
440 return $this->callBuilderMethod(
'getProductStores', [
'PRODUCT_STORES_LIST']);
448 return $this->callBuilderMethod(
'recurring', [
'RECURRING_PRODUCTS_LIST']);
458 return $this->callBuilderMethod(
'createItemsResultAfterDeliver', [
'RESULT_AFTER_DELIVER_LIST'], $resultAfterDeliver);
467 return $this->callBuilderMethod(
'returnProductBatches', []);
476 return $this->callBuilderMethod(
'writeOffProductBatches', []);
487 private function callBuilderMethod($method, array $outputNames, $methodParameters =
null)
491 $resultList = array();
494 foreach ($this->pool as $builder)
496 if (!method_exists($builder, $method))
501 if (!$methodParameters)
504 $compatibilityOutputName = $outputNames[0];
506 $r = $builder->$method($compatibilityOutputName);
511 $r = $builder->$method($methodParameters);
514 if (!$r->isSuccess())
516 $result->addErrors($r->getErrors());
519 if ($r->hasWarnings())
521 $result->addWarnings($r->getWarnings());
524 $data = $r->getData();
527 $providerName =
null;
529 $providerClass = $builder->getProviderClass();
532 $reflect = new \ReflectionClass($providerClass);
533 $providerName = $this->clearProviderName($reflect->getName());
536 if (empty($providerName))
538 $providerName = $builder->getCallbackFunction();
541 foreach ($outputNames as $outputName)
543 if (!empty($data[$outputName]))
545 $resultList[$outputName] = [
546 $providerName => $data[$outputName],
553 if (!empty($resultList))
555 $result->setData($resultList);
566 private function createBuilder($providerName)
568 if (!$this->isExistsProvider($providerName))
570 $providerClass =
null;
572 if (!empty($providerName) && class_exists($providerName))
574 $providerClass =
new $providerName($this->getContext());
579 $providerClass = $providerName;
584 $this->addBuilder($providerName, $builder);
588 $builder = $this->getBuilder($providerName);
598 private function addBuilder($providerName, ProviderBuilderBase $builder)
600 $providerName = $this->clearProviderName($providerName);
602 $this->pool[$providerName] = $builder;
610 private function getBuilder($providerName)
612 $providerName = $this->clearProviderName($providerName);
614 if ($this->isExistsProvider($providerName))
616 return $this->pool[$providerName];
626 private function isExistsProvider($providerName)
628 $providerName = $this->clearProviderName($providerName);
629 return (isset($this->pool[$providerName]));
635 private function getContext()
637 return $this->context;
645 private function clearProviderName($providerName)
647 if (!empty($providerName) && $providerName[0] ==
"\\")
649 $providerName = ltrim($providerName,
'\\');
652 return $providerName;