Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
manager.php
1<?php
2
4
14
15Loc::loadMessages(__FILE__);
16
18{
20 protected $items = array();
21 protected static $classes = null;
22 protected static $cachedFields = array();
23
27
28 const STORE_PICKUP_CODE = 'BITRIX_STORE_PICKUP';
29 const STORE_PICKUP_CLASS = '\Bitrix\Sale\Delivery\ExtraServices\Store';
30
32 protected $isClone = false;
33
41 public function __construct($initParam, $currency = "", $values = array(), array $additionalParams = array())
42 {
43 if(!is_array($initParam)) // deliveryId
44 {
45 if(intval($initParam) <= 0) //new delivery service
46 return;
47
48 $itemsParams = self::getExtraServicesList($initParam);
49
50 }
51 else // params array.
52 {
53 $itemsParams = $initParam;
54 }
55
56 if(empty($itemsParams))
57 return;
58
59 if(!empty($this->items))
60 sortByColumn($itemsParams, array("SORT" => SORT_ASC, "NAME" => SORT_ASC), '', null, true);
61
62 foreach($itemsParams as $params)
63 {
64 if($currency === "" && !empty($params["CURRENCY"]))
65 $currency = $params["CURRENCY"];
66
67 $this->addItem($params, $currency, isset($values[$params["ID"]]) ? $values[$params["ID"]] : null, $additionalParams);
68 }
69 }
70
74 public static function getClassesList()
75 {
76 if(static::$classes === null)
77 self::initClassesList();
78
79 return static::$classes;
80 }
81
87 public static function initClassesList()
88 {
89 if(static::$classes !== null)
90 return true;
91
92 $classes = array(
93 '\Bitrix\Sale\Delivery\ExtraServices\Enum' => 'lib/delivery/extra_services/enum.php',
94 '\Bitrix\Sale\Delivery\ExtraServices\Store' => 'lib/delivery/extra_services/store.php',
95 '\Bitrix\Sale\Delivery\ExtraServices\Quantity' => 'lib/delivery/extra_services/quantity.php',
96 '\Bitrix\Sale\Delivery\ExtraServices\Checkbox' => 'lib/delivery/extra_services/checkbox.php'
97 );
98
99 \Bitrix\Main\Loader::registerAutoLoadClasses('sale', $classes);
100 Services\Manager::getHandlersList();
101 unset($classes['\Bitrix\Sale\Delivery\ExtraServices\Store']);
102 $event = new Event('sale', 'onSaleDeliveryExtraServicesClassNamesBuildList');
103 $event->send();
104 $resultList = $event->getResults();
105
106 if (is_array($resultList) && !empty($resultList))
107 {
108 $customClasses = array();
109
110 foreach ($resultList as $eventResult)
111 {
113 if ($eventResult->getType() != EventResult::SUCCESS)
114 continue;
115
116 $params = $eventResult->getParameters();
117
118 if(!empty($params) && is_array($params))
119 $customClasses = array_merge($customClasses, $params);
120 }
121
122 if(!empty($customClasses))
123 {
124 \Bitrix\Main\Loader::registerAutoLoadClasses(null, $customClasses);
125 $classes = array_merge($customClasses, $classes);
126 }
127 }
128
129 static::$classes = array_merge(array_keys($classes));
130
131 return static::$classes;
132 }
133
137 public function getItems()
138 {
139 return $this->items;
140 }
141
146 public function getItem($id)
147 {
148 return (isset($this->items[$id]) ? $this->items[$id] : null);
149 }
150
155 public function getItemByCode($code)
156 {
157 foreach($this->items as $item)
158 if($item->getCode() == $code)
159 return $item;
160
161 return null;
162 }
163
168 public function getTotalCostShipment(Shipment $shipment = null)
169 {
170 $result = 0;
171
172 foreach($this->items as $itemId => $item)
173 $result += $item->getCostShipment($shipment);
174
175 return $result;
176 }
177
183 public static function prepareParamsToSave($params)
184 {
185 if(isset($params["RIGHTS"]))
186 {
187 $params["RIGHTS"] =
188 (isset($params["RIGHTS"][self::RIGHTS_ADMIN_IDX]) ? $params["RIGHTS"][self::RIGHTS_ADMIN_IDX] : "Y").
189 (isset($params["RIGHTS"][self::RIGHTS_MANAGER_IDX]) ? $params["RIGHTS"][self::RIGHTS_MANAGER_IDX] : "Y").
190 (isset($params["RIGHTS"][self::RIGHTS_CLIENT_IDX]) ? $params["RIGHTS"][self::RIGHTS_CLIENT_IDX] : "Y");
191 }
192
193 if(!isset($params["CLASS_NAME"]) || $params["CLASS_NAME"] == '' || !class_exists($params["CLASS_NAME"]))
194 return $params;
195
196 if(!isset($params["ACTIVE"]))
197 $params["ACTIVE"] = "Y";
198
199 if(isset($params["CLASS_NAME_DISABLED"]))
200 unset($params["CLASS_NAME_DISABLED"]);
201
202 if(is_callable($params["CLASS_NAME"]."::prepareParamsToSave"))
203 $params = $params["CLASS_NAME"]::prepareParamsToSave($params);
204
205 return $params;
206 }
207
216 public static function getAdminParamsControl($className, $name, array $params)
217 {
218 if($className == '')
219 throw new ArgumentNullException("className");
220
221 if(!is_callable($className.'::getAdminParamsControl'))
222 throw new SystemException('"'.$className.'::getAdminParamsControl" does not exist!');
223
224 return $className::getAdminParamsControl($name, $params);
225 }
226
236 public function addItem($params, $currency, $value = null, array $additionalParams = array())
237 {
238 if($params["CLASS_NAME"] === '' )
239 return false;
240
241 if(!isset($params["CLASS_NAME"]))
242 throw new ArgumentNullException("params[\"CLASS_NAME\"]");
243
244 if(!class_exists($params["CLASS_NAME"]))
245 return false;
246
247 if(!is_subclass_of($params["CLASS_NAME"], Base::class))
248 {
249 throw new \Bitrix\Main\SystemException(
250 'Class "' . $params["CLASS_NAME"] . '" is not a subclass of the \Bitrix\Sale\Delivery\ExtraServices\Base'
251 );
252 }
253
254 $item = new $params["CLASS_NAME"]($params["ID"], $params, $currency, $value, $additionalParams);
255 $this->items[$params["ID"]] = $item;
256 return $params["ID"];
257 }
258
262 public function setValues(array $values = array())
263 {
264 foreach($values as $eSrvId => $value)
265 {
266 $item = $this->getItem($eSrvId);
267
268 if($item)
269 $item->setValue($value);
270 }
271 }
272
277 public function setOperationCurrency($currency)
278 {
279 foreach($this->items as $itemId => $item)
280 $item->setOperatingCurrency($currency);
281 }
282
288 public static function getValuesForShipment($shipmentId, $deliveryId)
289 {
290 $result = array();
291
292 if(intval($shipmentId) > 0 && intval($deliveryId) > 0)
293 {
294 $dbRes = ShipmentExtraServiceTable::getList(array(
295 'filter' => array(
296 '=SHIPMENT_ID' => $shipmentId,
297 '!=ID' => self::getStoresValueId($deliveryId)
298 )
299 ));
300
301 while($row = $dbRes->fetch())
302 $result[$row["EXTRA_SERVICE_ID"]] = $row["VALUE"];
303 }
304
305 return $result;
306 }
307
316 public static function saveValuesForShipment($shipmentId, $extraServices)
317 {
318 $result = new Result();
319
320 if(intval($shipmentId) <= 0)
321 throw new ArgumentNullException("shipmentId");
322
323 $exist = array();
324
325 $dbRes = ShipmentExtraServiceTable::getList(array(
326 'filter' => array(
327 '=SHIPMENT_ID' => $shipmentId
328 )
329 ));
330
331 while($row = $dbRes->fetch())
332 $exist[$row["EXTRA_SERVICE_ID"]] = $row["ID"];
333
334 if(is_array($extraServices))
335 {
336 foreach($extraServices as $extraServiceId => $value)
337 {
338 if(array_key_exists($extraServiceId, $exist))
339 {
340 $res = ShipmentExtraServiceTable::update($exist[$extraServiceId], array("VALUE" => $value));
341 }
342 else
343 {
344 $res = ShipmentExtraServiceTable::add(array(
345 "EXTRA_SERVICE_ID" => $extraServiceId,
346 "SHIPMENT_ID" => $shipmentId,
347 "VALUE" => $value
348 ));
349 }
350
351 if($res->isSuccess())
352 unset($exist[$extraServiceId]);
353 else
354 foreach($res->getErrors() as $error)
355 $result->addError($error);
356
357 }
358 }
359
360 foreach($exist as $extraServiceId => $value)
361 {
362 $res = ShipmentExtraServiceTable::delete($extraServiceId);
363
364 if(!$res->isSuccess())
365 foreach($res->getErrors() as $error)
366 $result->addError($error);
367 }
368
369 return $result;
370 }
371
379 public static function getObjectsForShipment(int $shipmentId, int $deliveryId, string $currency): array
380 {
381 $result = [];
382
383 $extraServiceValuesList = ShipmentExtraServiceTable::getList(
384 [
385 'filter' => [
386 '=SHIPMENT_ID' => $shipmentId,
387 '!=ID' => self::getStoresValueId($deliveryId)
388 ],
389 'select' => [
390 '*',
391 'EXTRA_SERVICE',
392 ]
393 ]
394 );
395
396 while ($extraServiceValue = $extraServiceValuesList->fetchObject())
397 {
398 $extraService = $extraServiceValue->getExtraService();
399
400 $className = $extraService->getClassName();
401
403 $extraServiceInstance = new $className(
404 $extraService->getId(),
405 [
406 'NAME' => $extraService->getName(),
407 'CODE' => $extraService->getCode(),
408 'INIT_VALUE' => $extraService->getInitValue(),
409 'PARAMS' => $extraService->getParams()
410 ],
411 $currency,
412 $extraServiceValue->getValue()
413 );
414
415 if (!$extraServiceInstance instanceof Base)
416 {
417 throw new SystemException(
418 sprintf(
419 'Object is not of expected type: %s',
420 Base::class
421 )
422 );
423 }
424
425 $result[$extraServiceValue['EXTRA_SERVICE_ID']] = $extraServiceInstance;
426 }
427
428 return $result;
429 }
430
436 public static function getStoreIdForShipment($shipmentId, $deliveryId)
437 {
438 $result = 0;
439
440 if(intval($shipmentId) > 0 && intval($deliveryId) > 0)
441 {
442 $storeFields = self::getStoresFields($deliveryId);
443
444 if(!empty($storeFields))
445 {
446 $dbRes = ShipmentExtraServiceTable::getList(array(
447 'filter' => array(
448 '=SHIPMENT_ID' => $shipmentId,
449 '=EXTRA_SERVICE_ID' => $storeFields['ID']
450 )
451 ));
452
453 if($row = $dbRes->fetch())
454 $result = $row["VALUE"];
455 }
456 }
457
458 return $result;
459 }
460
470 public static function saveStoreIdForShipment($shipmentId, $deliveryId, $storeId)
471 {
472 if(intval($shipmentId) <= 0)
473 throw new ArgumentNullException("shipmentId");
474
475 $result = new Result();
476
477 if(intval($deliveryId) <= 0)
478 return $result;
479
480 $storeFields = self::getStoresFields($deliveryId, false);
481
482 if(isset($storeFields['ID']))
483 {
484 $dbRes = ShipmentExtraServiceTable::getList(array(
485 'filter' => array(
486 '=SHIPMENT_ID' => $shipmentId,
487 '=EXTRA_SERVICE_ID' => $storeFields['ID']
488 )
489 ));
490
491 $storeRowId = 0;
492
493 if($row = $dbRes->fetch())
494 $storeRowId = $row["ID"];
495
496 if($storeRowId > 0)
497 {
498 $res = ShipmentExtraServiceTable::update($storeRowId, array("VALUE" => $storeId));
499 }
500 else
501 {
502 $res = ShipmentExtraServiceTable::add(array(
503 "EXTRA_SERVICE_ID" => $storeFields['ID'],
504 "SHIPMENT_ID" => $shipmentId,
505 "VALUE" => $storeId
506 ));
507 }
508
509 if(!$res->isSuccess())
510 foreach($res->getErrors() as $error)
511 $result->addError($error);
512 }
513
514 return $result;
515 }
516
521 protected static function getStoresValueId($deliveryId)
522 {
523 $fields = self::getStoresFields($deliveryId);
524
525 if(isset($fields["ID"]))
526 $result = $fields["ID"];
527 else
528 $result = 0;
529
530 return $result;
531 }
532
538 public static function getStoresFields($deliveryId, $onlyActive = true)
539 {
540 if(intval($deliveryId) <= 0)
541 return array();
542
543 $result = self::getExtraServicesList($deliveryId, true);
544
545 if(
546 $onlyActive
547 && (empty($result['ACTIVE']) || $result['ACTIVE'] !== 'Y')
548 )
549 {
550 return [];
551 }
552
553 return $result;
554 }
555
560 public static function getStoresList($deliveryId)
561 {
562 $stores = self::getStoresFields($deliveryId);
563 return isset($stores["PARAMS"]["STORES"]) ? $stores["PARAMS"]["STORES"] : array();
564 }
565
571 public static function deleteStores($deliveryId)
572 {
573 $storesFields = self::getStoresFields($deliveryId, false);
574
575 if(empty($storesFields['ID']))
576 return new Result();
577
578 $result = Table::delete($storesFields['ID']);
579
580 if($result->isSuccess())
581 unset(static::$cachedFields[$deliveryId][$storesFields['ID']]);
582
583 return $result;
584 }
585
591 public static function setStoresUnActive($deliveryId)
592 {
593 if(intval($deliveryId) <= 0)
594 return new Result();
595
596 $storesFields = self::getStoresFields($deliveryId);
597
598 if(empty($storesFields['ID']))
599 return new Result();
600
601 $result = Table::update(
602 $storesFields['ID'],
603 array(
604 "ACTIVE" => "N"
605 )
606 );
607
608 if($result->isSuccess())
609 static::$cachedFields[$deliveryId][$storesFields['ID']]["ACTIVE"] = "N";
610
611 return $result;
612 }
613
620 public static function saveStores($deliveryId, array $storesList)
621 {
622 $result = new Result();
623 $storesFields = self::getStoresFields($deliveryId, false);
624
625 if(!empty($storesFields['ID']))
626 {
627 $res = Table::update(
628 $storesFields["ID"],
629 array(
630 "ACTIVE" => "Y",
631 "PARAMS" => array(
632 "STORES" => $storesList
633 )
634 )
635 );
636 }
637 else
638 {
639 $res = Table::add(
640 array(
641 "CODE" => self::STORE_PICKUP_CODE,
642 "NAME" => Loc::getMessage("DELIVERY_SERVICE_MANAGER_ES_NAME"),
643 "DESCRIPTION" => Loc::getMessage("DELIVERY_SERVICE_MANAGER_ES_DESCRIPTION"),
644 "CLASS_NAME" => self::STORE_PICKUP_CLASS,
645 "DELIVERY_ID" => $deliveryId,
646 "RIGHTS" => "YYY",
647 "ACTIVE" => "Y",
648 "PARAMS" => array(
649 "STORES" => $storesList
650 )
651 )
652 );
653 }
654
655 if(!$res->isSuccess())
656 $result->addErrors($res->getErrors());
657
658 return $result;
659 }
660
667 public static function getExtraServicesList($deliveryId, $stores = false)
668 {
669 if(intval($deliveryId) <= 0)
670 return array();
671
672 if(!isset(static::$cachedFields[$deliveryId]))
673 {
674 $srv = Services\Manager::getById($deliveryId);
675
676 if(!empty($srv['PARENT_ID']))
677 {
678 self::prepareData(array($deliveryId, $srv['PARENT_ID']));
679 static::$cachedFields[$deliveryId] = static::$cachedFields[$deliveryId] + static::$cachedFields[$srv['PARENT_ID']];
680 }
681 else
682 {
683 self::prepareData(array($deliveryId));
684 }
685 }
686
687 $result = array();
688
689 foreach(static::$cachedFields[$deliveryId] as $id => $es)
690 {
691 if($es['CLASS_NAME'] == self::STORE_PICKUP_CLASS)
692 {
693 if($stores)
694 return $es;
695
696 continue;
697 }
698
699 if(!$stores)
700 $result[$id] = $es;
701 }
702
703 return $result;
704 }
705
712 public static function prepareData(array $servicesIds)
713 {
714 if(empty($servicesIds))
715 return;
716
717 foreach($servicesIds as $id)
718 {
719 $srv = Services\Manager::getById($id);
720
721 if(!empty($srv['PARENT_ID']) && !in_array($id, $servicesIds))
722 $servicesIds[] = $id;
723 }
724
725 $ids = array_diff($servicesIds, array_keys(static::$cachedFields));
726
727 $dbRes = Table::getList(array(
728 'filter' => array(
729 '=DELIVERY_ID' => $ids,
730 array(
731 "LOGIC" => "OR",
732 "=ACTIVE" => "Y",
733 "=CLASS_NAME" => self::STORE_PICKUP_CLASS
734 )
735 ),
736 "order" => array(
737 "SORT" =>"ASC",
738 "NAME" => "ASC"
739 ),
740 "select" => array("*", "CURRENCY" => "DELIVERY_SERVICE.CURRENCY")
741 ));
742
743 while($es = $dbRes->fetch())
744 {
745 if(!isset(static::$cachedFields[$es['DELIVERY_ID']]))
746 static::$cachedFields[$es['DELIVERY_ID']] = array();
747
748 static::$cachedFields[$es['DELIVERY_ID']][$es["ID"]] = $es;
749 }
750
751 foreach($ids as $deliveryId)
752 if(!isset(static::$cachedFields[$deliveryId]))
753 static::$cachedFields[$deliveryId] = array();
754 }
755
762 public function createClone(\SplObjectStorage $cloneEntity)
763 {
764 if ($this->isClone() && $cloneEntity->contains($this))
765 {
766 return $cloneEntity[$this];
767 }
768
769 $extraServiceClone = clone $this;
770 $extraServiceClone->isClone = true;
771
772 if (!$cloneEntity->contains($this))
773 {
774 $cloneEntity[$this] = $extraServiceClone;
775 }
776
777 return $extraServiceClone;
778 }
779
783 public function isClone()
784 {
785 return $this->isClone;
786 }
787
793 public function getTotalCost()
794 {
795 $result = 0;
796
797 foreach($this->items as $itemId => $item)
798 $result += $item->getCost();
799
800 return $result;
801 }
802}
803
804\Bitrix\Sale\Delivery\ExtraServices\Manager::initClassesList();
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
__construct($initParam, $currency="", $values=array(), array $additionalParams=array())
Definition manager.php:41
createClone(\SplObjectStorage $cloneEntity)
Definition manager.php:762
static saveValuesForShipment($shipmentId, $extraServices)
Definition manager.php:316
getTotalCostShipment(Shipment $shipment=null)
Definition manager.php:168
static getValuesForShipment($shipmentId, $deliveryId)
Definition manager.php:288
static saveStoreIdForShipment($shipmentId, $deliveryId, $storeId)
Definition manager.php:470
static getStoreIdForShipment($shipmentId, $deliveryId)
Definition manager.php:436
addItem($params, $currency, $value=null, array $additionalParams=array())
Definition manager.php:236
static prepareData(array $servicesIds)
Definition manager.php:712
static getAdminParamsControl($className, $name, array $params)
Definition manager.php:216
static getExtraServicesList($deliveryId, $stores=false)
Definition manager.php:667
static saveStores($deliveryId, array $storesList)
Definition manager.php:620
static getStoresFields($deliveryId, $onlyActive=true)
Definition manager.php:538