Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
providerbuilderbase.php
1<?php
2
4
7
12abstract class ProviderBuilderBase
13{
14 protected $items = array();
15 protected $providerClass = null;
16 protected $callbackFunction = null;
17 protected $context = array();
18
25 public static function createBuilder($providerClass, $context)
26 {
28 {
30 }
31 else
32 {
34 }
35
36 return $builder;
37 }
38
45 public static function create($providerClass, $context)
46 {
47 $builder = new static();
48
49 if ($providerClass && !is_string($providerClass))
50 {
51 $builder->providerClass = $providerClass;
52 }
53 $builder->context = $context;
54
55 return $builder;
56 }
57
66 protected function callTransferMethod($className, $methodName, Sale\Result $result = null)
67 {
68 if (!class_exists($className))
69 {
70 throw new Main\ArgumentOutOfRangeException('className');
71 }
72
77 $transfer = $className::create($this->getProviderClass(), $this->getContext());
78 if (!method_exists($transfer, $methodName))
79 {
80 throw new Main\ArgumentOutOfRangeException('methodName');
81 }
82
83 if ($result)
84 {
85 $r = $transfer->$methodName($this->getItems(), $result);
86 }
87 else
88 {
89 $r = $transfer->$methodName($this->getItems());
90 }
91
92 return $r;
93
94 }
95
99 abstract public function addProductByBasketItem(Sale\BasketItemBase $basketItem);
100
104 abstract public function addProductByShipmentItem(Sale\ShipmentItem $shipmentItem);
105
109 abstract public function addProductData(array $productData);
110
114 public function addBasketItemBarcodeData(array $barcodeParams)
115 {
116 $this->addItem($barcodeParams['PRODUCT_ID'], $barcodeParams);
117 }
118
122 public function addProductById($productId)
123 {
124 $fields = array(
125 'PRODUCT_ID' => $productId,
126 );
127
128 $this->addItem($productId, $fields);
129 }
130
136 public function getProductData($outputName)
137 {
138 $r = static::callTransferMethod($this->getTransferClassName(), 'getProductData');
139 if (!$r->isSuccess())
140 {
141 return $r;
142 }
143
144 return $this->decomposeIntoProvider($r, $outputName);
145 }
146
152 public function getAvailableQuantity($outputName)
153 {
154 $r = static::callTransferMethod($this->getTransferClassName(), 'getAvailableQuantity');
155 if (!$r->isSuccess())
156 {
157 return $r;
158 }
159
160 return $this->decomposeIntoProvider($r, $outputName);
161 }
162
168 public function getAvailableQuantityByStore($outputName)
169 {
170 $r = static::callTransferMethod($this->getTransferClassName(), 'getAvailableQuantityByStore');
171 if (!$r->isSuccess())
172 {
173 return $r;
174 }
175
176 return $this->decomposeIntoProvider($r, $outputName);
177 }
178
184 public function getAvailableQuantityAndPrice($outputName)
185 {
186 $r = static::callTransferMethod($this->getTransferClassName(), 'getAvailableQuantityAndPrice');
187 if (!$r->isSuccess())
188 {
189 return $r;
190 }
191
192 return $this->decomposeIntoProvider($r, $outputName);
193 }
194
201 protected function decomposeIntoProvider(Sale\Result $resultProvider, $outputName)
202 {
203 $result = new Sale\Result();
204 $providerData = $resultProvider->getData();
205
206 if (empty($providerData[$outputName]))
207 {
208 return $result;
209 }
210
211 $result->setData($providerData);
212 return $result;
213 }
214
218 public function tryShip()
219 {
220 return static::callTransferMethod($this->getTransferClassName(), 'tryShip');
221 }
222
226 public function isNeedShip()
227 {
228 return static::callTransferMethod($this->getTransferClassName(), 'isNeedShip');
229 }
230
234 public function getBundleItems()
235 {
236 return static::callTransferMethod($this->getTransferClassName(), 'getBundleItems');
237 }
238
242 public function deliver()
243 {
244 return static::callTransferMethod($this->getTransferClassName(), 'deliver');
245 }
246
250 public function viewProduct()
251 {
252 return static::callTransferMethod($this->getTransferClassName(), 'viewProduct');
253 }
254
258 public function getProductStores()
259 {
260 return static::callTransferMethod($this->getTransferClassName(), 'getProductStores');
261 }
262
266 public function checkBarcode()
267 {
268 return static::callTransferMethod($this->getTransferClassName(), 'checkBarcode');
269 }
270
271
275 public function recurring()
276 {
277 return static::callTransferMethod($this->getTransferClassName(), 'recurring');
278 }
279
283 public function writeOffProductBatches(): Main\Result
284 {
285 return static::callTransferMethod($this->getTransferClassName(), 'writeOffProductBatches');
286 }
287
291 public function returnProductBatches(): Main\Result
292 {
293 return static::callTransferMethod($this->getTransferClassName(), 'returnProductBatches');
294 }
295
303 abstract public function setItemsResultAfterTryShip(PoolQuantity $pool, array $productTryShipList);
304
310 public function setItemsResultAfterShip(Sale\Result $result)
311 {
312 return static::callTransferMethod($this->getTransferClassName(), 'setItemsResultAfterShip', $result);
313 }
314
318 public function reserve()
319 {
320 return static::callTransferMethod($this->getTransferClassName(), 'reserve');
321 }
322
326 public function ship()
327 {
328 return static::callTransferMethod($this->getTransferClassName(), 'ship');
329 }
330
337 public function createItemsResultAfterDeliver(Sale\Result $resultAfterDeliver)
338 {
339 $result = new Sale\Result();
340 $resultList = array();
341 $products = $this->getItems();
342
343 if (empty($products))
344 {
345 return $result;
346 }
347
348 $resultDeliverData = $resultAfterDeliver->getData();
349
350 foreach ($products as $productId => $productData)
351 {
352 $providerName = $this->getProviderName();
353 if (empty($resultDeliverData['DELIVER_PRODUCTS_LIST']) ||
354 empty($resultDeliverData['DELIVER_PRODUCTS_LIST'][$providerName]) ||
355 !array_key_exists($productId, $resultDeliverData['DELIVER_PRODUCTS_LIST'][$providerName]))
356 {
357 continue;
358 }
359
360 if (empty($productData['SHIPMENT_ITEM_LIST']))
361 {
362 continue;
363 }
364
369 foreach ($productData['SHIPMENT_ITEM_LIST'] as $shipmentItemIndex => $shipmentItem)
370 {
371 $basketItem = $shipmentItem->getBasketItem();
372
373 if (!$basketItem)
374 {
375 throw new Main\ObjectNotFoundException('Entity "BasketItem" not found');
376 }
377
378 $resultList[$basketItem->getBasketCode()] = $resultDeliverData['DELIVER_PRODUCTS_LIST'][$providerName][$productId];
379 }
380 }
381
382 if (!empty($resultList))
383 {
384 $result->setData(
385 array(
386 'RESULT_AFTER_DELIVER_LIST' => $resultList
387 )
388 );
389 }
390
391 return $result;
392 }
393
397 protected function addProduct(array $productData)
398 {
399 $this->items[$productData['PRODUCT_ID']] = $productData;
400 }
401
406 public function getProviderClass()
407 {
409 }
410
415 public function getCallbackFunction()
416 {
418 }
419
424 public function getProviderName()
425 {
426 $providerName = null;
428 if ($providerClass)
429 {
430 $reflect = new \ReflectionClass($this->getProviderClass());
431 $providerName = $reflect->getName();
432 }
433
434 return $this->clearProviderName($providerName);
435 }
436
442 protected function clearProviderName($providerName)
443 {
444 if (is_string($providerName) && mb_substr($providerName, 0, 1) == "\\")
445 {
446 $providerName = mb_substr($providerName, 1);
447 }
448
449 return $providerName;
450 }
451
452
457 protected function getContext()
458 {
459 return $this->context;
460 }
461
465 protected function getItems()
466 {
467 return $this->items;
468 }
469
474 protected function addItem($productId, array $productData)
475 {
476 $fields = array();
477 if (isset($this->items[$productId]))
478 {
479 $fields = $this->items[$productId];
480 }
481
482 $fields = $productData + $fields;
483
484 if (isset($fields['QUANTITY_LIST'][$productData['BASKET_CODE']]))
485 {
486 $fields['QUANTITY_LIST'][$productData['BASKET_CODE']] += (float)$productData['QUANTITY'];
487 }
488 else
489 {
490 $fields['QUANTITY_LIST'][$productData['BASKET_CODE']] = (float)$productData['QUANTITY'];
491 }
492
493 unset($fields['QUANTITY']);
494
495 if (isset($fields['QUANTITY_LIST_BY_STORE'][$productData['BASKET_CODE']]))
496 {
497 foreach ($productData['QUANTITY_BY_STORE'] as $storeId => $quantity)
498 {
499 if (!isset($fields['QUANTITY_LIST_BY_STORE'][$productData['BASKET_CODE']][$storeId]))
500 {
501 $fields['QUANTITY_LIST_BY_STORE'][$productData['BASKET_CODE']][$storeId] = 0;
502 }
503
504 $fields['QUANTITY_LIST_BY_STORE'][$productData['BASKET_CODE']][$storeId] += $quantity;
505 }
506 }
507 else
508 {
509 $fields['QUANTITY_LIST_BY_STORE'][$productData['BASKET_CODE']] = $productData['QUANTITY_BY_STORE'] ?? [];
510 }
511
512 unset($fields['QUANTITY_BY_STORE']);
513
514 if (isset($fields['RESERVED_QUANTITY_LIST'][$productData['BASKET_CODE']]))
515 {
516 $fields['RESERVED_QUANTITY_LIST'][$productData['BASKET_CODE']] += (float)$productData['RESERVED_QUANTITY'];
517 }
518 else
519 {
520 $fields['RESERVED_QUANTITY_LIST'][$productData['BASKET_CODE']] = (float)($productData['RESERVED_QUANTITY'] ?? 0.0);
521 }
522
523 unset($fields['RESERVED_QUANTITY']);
524
525 if (isset($fields['RESERVED_QUANTITY_LIST_BY_STORE'][$productData['BASKET_CODE']]))
526 {
527 foreach ($productData['RESERVED_QUANTITY_BY_STORE'] as $storeId => $quantity)
528 {
529 if (!isset($fields['RESERVED_QUANTITY_LIST_BY_STORE'][$productData['BASKET_CODE']][$storeId]))
530 {
531 $fields['RESERVED_QUANTITY_LIST_BY_STORE'][$productData['BASKET_CODE']][$storeId] = 0;
532 }
533
534 $fields['RESERVED_QUANTITY_LIST_BY_STORE'][$productData['BASKET_CODE']][$storeId] += $quantity;
535 }
536 }
537 else
538 {
539 $fields['RESERVED_QUANTITY_LIST_BY_STORE'][$productData['BASKET_CODE']] = $productData['RESERVED_QUANTITY_BY_STORE'] ?? [];
540 }
541
542 unset($fields['RESERVED_QUANTITY_BY_STORE']);
543
544 if (isset($productData['SHIPMENT_ITEM']))
545 {
547 $shipmentItem = $productData['SHIPMENT_ITEM'];
548 unset($fields['SHIPMENT_ITEM']);
549
550 $fields['SHIPMENT_ITEM_LIST'][$shipmentItem->getInternalIndex()] = $shipmentItem;
551 $fields['SHIPMENT_ITEM_QUANTITY_LIST'][$shipmentItem->getInternalIndex()] = (float)$productData['QUANTITY'];
552 }
553
554 if (isset($productData['STORE_DATA']))
555 {
556 if (!isset($fields['STORE_DATA_LIST']))
557 {
558 $fields['STORE_DATA_LIST'] = array();
559 }
560
561 $fields['STORE_DATA_LIST'] = $productData['STORE_DATA'] + $fields['STORE_DATA_LIST'];
562 unset($fields['STORE_DATA']);
563 }
564
565 if (isset($productData['IS_BARCODE_MULTI']) && !isset($fields['IS_BARCODE_MULTI']))
566 {
567 $fields['IS_BARCODE_MULTI'] = $productData['IS_BARCODE_MULTI'];
568 }
569
570 if (isset($productData['SHIPMENT_ITEM_DATA']))
571 {
572 if (!isset($fields['SHIPMENT_ITEM_DATA_LIST']))
573 {
574 $fields['SHIPMENT_ITEM_DATA_LIST'] = array();
575 }
576
577 $fields['SHIPMENT_ITEM_DATA_LIST'] = $productData['SHIPMENT_ITEM_DATA'] + $fields['SHIPMENT_ITEM_DATA_LIST'];
578 unset($fields['SHIPMENT_ITEM_DATA']);
579 }
580
581 if (isset($productData['NEED_RESERVE']))
582 {
583 if (!isset($fields['NEED_RESERVE_LIST']))
584 {
585 $fields['NEED_RESERVE_LIST'] = [];
586 }
587
588 $fields['NEED_RESERVE_LIST'] = $productData['NEED_RESERVE'] + $fields['NEED_RESERVE_LIST'];
589
590 unset($fields['NEED_RESERVE']);
591 }
592
593 if (isset($productData['NEED_RESERVE_BY_STORE']))
594 {
595 if (!isset($fields['NEED_RESERVE_BY_STORE_LIST']))
596 {
597 $fields['NEED_RESERVE_BY_STORE_LIST'] = [];
598 }
599
600 $fields['NEED_RESERVE_BY_STORE_LIST'] = $productData['NEED_RESERVE_BY_STORE'] + $fields['NEED_RESERVE_BY_STORE_LIST'];
601
602 unset($fields['NEED_RESERVE_BY_STORE']);
603 }
604
605 $this->items[$productId] = $fields;
606 }
607
613 protected function getItem($productId)
614 {
615 $item = false;
616 if ($this->isExistsProductIdInItems($productId))
617 {
618 $item = $this->items[$productId];
619 }
620 return $item;
621 }
622
628 protected function isExistsProductIdInItems($productId)
629 {
630 return (isset($this->items[$productId]));
631 }
632
636 public static function getClassName()
637 {
638 return get_called_class();
639 }
640
641 abstract public function getTransferClassName();
642}
decomposeIntoProvider(Sale\Result $resultProvider, $outputName)
setItemsResultAfterTryShip(PoolQuantity $pool, array $productTryShipList)
static createBuilder($providerClass, $context)
addProductByBasketItem(Sale\BasketItemBase $basketItem)
addProductByShipmentItem(Sale\ShipmentItem $shipmentItem)
static create($providerClass, $context)