Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
base.php
1<?php
3
13
14Loc::loadMessages(__FILE__);
15
16/* Inputs for deliveries */
17require_once __DIR__.'/../inputs.php';
18
24abstract class Base
25{
26 protected $handlerCode = 'UNDEFINED';
27
28 protected $id = 0;
29 protected $name = "";
30 protected $code = "";
31 protected $vatId = 0;
32 protected $sort = 100;
33 protected $logotip = 0;
34 protected $parentId = 0;
35 protected $currency = "";
36 protected $active = false;
37 protected $description = "";
38 protected $config = array();
39 protected $restricted = false;
40 protected $trackingClass = "";
42 protected $deliveryRequestHandler = null;
43
44 protected $extraServices = array();
45 protected $trackingParams = array();
46 protected $allowEditShipment = array();
47
48 protected static $isProfile = false;
49 protected static $canHasProfiles = false;
50 protected static $isCalculatePriceImmediately = false;
51 protected static $whetherAdminExtraServicesShow = false;
52
53 const EVENT_ON_CALCULATE = "onSaleDeliveryServiceCalculate";
54
55 public const TAG_PROFITABLE = 'profitable';
56
58 protected $isClone = false;
59
66 public function __construct(array $initParams)
67 {
68 $initParams = $this->prepareFieldsForUsing($initParams);
69
70 if(isset($initParams["PARENT_ID"]))
71 $this->parentId = $initParams["PARENT_ID"];
72 else
73 $this->parentId = 0;
74
75 if(!isset($initParams["ACTIVE"]))
76 $initParams["ACTIVE"] = "N";
77
78 if(!isset($initParams["NAME"]))
79 $initParams["NAME"] = "";
80
81 if(!isset($initParams["CONFIG"]) || !is_array($initParams["CONFIG"]))
82 $initParams["CONFIG"] = array();
83
84 if(!is_array($initParams["CONFIG"]))
85 throw new \Bitrix\Main\ArgumentTypeException("CONFIG", "array");
86
87 $this->active = $initParams["ACTIVE"] == "Y";
88 $this->name = $initParams["NAME"];
89 $this->config = $initParams["CONFIG"];
90
91 if(isset($initParams["ID"]) )
92 $this->id = $initParams["ID"];
93
94 if(isset($initParams["DESCRIPTION"]))
95 $this->description = $initParams["DESCRIPTION"];
96
97 if(isset($initParams["CODE"]))
98 $this->code = $initParams["CODE"];
99
100 if(isset($initParams["SORT"]))
101 $this->sort = $initParams["SORT"];
102
103 if(isset($initParams["LOGOTIP"]))
104 $this->logotip = $initParams["LOGOTIP"];
105
106 if(isset($initParams["CURRENCY"]))
107 $this->currency = $initParams["CURRENCY"];
108
109 if(isset($initParams["ALLOW_EDIT_SHIPMENT"]))
110 $this->allowEditShipment = $initParams["ALLOW_EDIT_SHIPMENT"];
111
112 if(isset($initParams["VAT_ID"]))
113 $this->vatId = intval($initParams["VAT_ID"]);
114
115 if(isset($initParams["RESTRICTED"]))
116 $this->restricted = $initParams["RESTRICTED"];
117
118 $this->trackingParams =
119 isset($initParams["TRACKING_PARAMS"]) && is_array($initParams["TRACKING_PARAMS"])
120 ? $initParams["TRACKING_PARAMS"]
121 : []
122 ;
123
124 if(isset($initParams["EXTRA_SERVICES"]))
125 $this->extraServices = new \Bitrix\Sale\Delivery\ExtraServices\Manager($initParams["EXTRA_SERVICES"], $this->currency);
126 elseif($this->id > 0)
127 $this->extraServices = new \Bitrix\Sale\Delivery\ExtraServices\Manager($this->id, $this->currency);
128 else
129 $this->extraServices = new \Bitrix\Sale\Delivery\ExtraServices\Manager(array(), $this->currency);
130 }
131
135 public function getHandlerCode(): string
136 {
137 return (string)$this->handlerCode;
138 }
139
143 public function getServiceCode(): string
144 {
145 if (
146 static::isProfile()
147 && ($parentService = $this->getParentService())
148 && ($parentServiceHandlerCode = $parentService->getHandlerCode())
149 && ($profileType = $this->getProfileType())
150 )
151 {
152 return $parentServiceHandlerCode . '_' . $profileType;
153 }
154
155 return $this->getHandlerCode();
156 }
157
165 public function calculate(\Bitrix\Sale\Shipment $shipment = null, $extraServices = array()) // null for compability with old configurable services api
166 {
167 $result = new Delivery\CalculationResult();
168
169 if ($shipment && !$shipment->getCollection())
170 {
171 $result->addError(new Error('\Bitrix\Sale\Delivery\Services\Base::calculate() can\'t calculate empty shipment!'));
172 return $result;
173 }
174
175 if ($shipment)
176 {
177 $result = $this->calculateConcrete($shipment);
178
179 if (empty($extraServices))
180 {
181 $extraServices = $shipment->getExtraServices();
182 }
183
184 $this->extraServices->setValues($extraServices);
185 $this->extraServices->setOperationCurrency($shipment->getCurrency());
186 $extraServicePrice = $this->extraServices->getTotalCostShipment($shipment);
187
188 if ((float)$extraServicePrice > 0)
189 {
190 $result->setExtraServicesPrice($extraServicePrice);
191 }
192 }
193
194 $eventParams = [
195 'RESULT' => $result,
196 'SHIPMENT' => $shipment,
197 'DELIVERY_ID' => $this->id,
198 ];
199
200 $event = new Event('sale', self::EVENT_ON_CALCULATE, $eventParams);
201 $event->send();
202 $resultList = $event->getResults();
203
204 if (is_array($resultList) && !empty($resultList))
205 {
206 foreach ($resultList as &$eventResult)
207 {
208 if ($eventResult->getType() != EventResult::SUCCESS)
209 {
210 continue;
211 }
212
213 $params = $eventResult->getParameters();
214
215 if (isset($params['RESULT']))
216 {
217 $result = $params['RESULT'];
218 }
219 }
220 }
221
222 return $result;
223 }
224
228 public function getExtraServices()
229 {
230 return $this->extraServices;
231 }
232
236 public function getCurrency()
237 {
238 return $this->currency;
239 }
240
245 protected static function calculateShipmentPrice(\Bitrix\Sale\Shipment $shipment)
246 {
247 $result = 0;
248
249 foreach($shipment->getShipmentItemCollection() as $shipmentItem)
250 {
252 $basketItem = $shipmentItem->getBasketItem();
253
254 if(!$basketItem)
255 continue;
256
257 if($basketItem->isBundleChild())
258 continue;
259
260 $result += $basketItem->getPrice();
261 }
262
263 return $result;
264 }
265
270 public static function getClassTitle()
271 {
272 return "";
273 }
274
279 public static function getClassDescription()
280 {
281 return "";
282 }
283
287 public static function getDefaultVatRate(): ?float
288 {
289 return null;
290 }
291
296 protected function calculateConcrete(\Bitrix\Sale\Shipment $shipment)
297 {
298 return (new Delivery\CalculationResult())
299 ->addError(
300 new Error(
301 Loc::getMessage('SALE_DLVR_BASE_DELIVERY_PRICE_CALC_ERROR'),
302 'DELIVERY_CALCULATION'
303 ));
304 }
305
311 public function prepareFieldsForSaving(array $fields)
312 {
313 $strError = "";
314 $structure = $fields["CLASS_NAME"]::getConfigStructure();
315
316 foreach($structure as $key1 => $rParams)
317 {
318 foreach($rParams["ITEMS"] as $key2 => $iParams)
319 {
320 if ($iParams["TYPE"] == "DELIVERY_SECTION")
321 {
322 continue;
323 }
324
325 $value = $fields["CONFIG"][$key1][$key2] ?? null;
326
327 $errors = \Bitrix\Sale\Internals\Input\Manager::getRequiredError($iParams, $value);
328
329 if (empty($errors))
330 {
331 $errors = \Bitrix\Sale\Internals\Input\Manager::getError($iParams, $value);
332 }
333
334 if (!empty($errors))
335 {
336 $strError .= Loc::getMessage("SALE_DLVR_BASE_FIELD")." \"".$iParams["NAME"]."\": ".implode("<br>\n", $errors)."<br>\n";
337 }
338 }
339 }
340
341 if ($strError != "")
342 {
343 throw new SystemException($strError);
344 }
345
346 if(mb_strpos($fields['CLASS_NAME'], '\\') !== 0)
347 {
348 $fields['CLASS_NAME'] = '\\'.$fields['CLASS_NAME'];
349 }
350
351 return $fields;
352 }
353
359 protected function getConfigStructure()
360 {
361 return array();
362 }
363
369 protected function glueValuesToConfig(array $confStructure, $confValues = array())
370 {
371 if(!is_array($confValues))
372 $confValues = array();
373
374 if(isset($confStructure["ITEMS"]) && is_array($confStructure["ITEMS"]))
375 {
376 $confStructure["ITEMS"] = $this->glueValuesToConfig($confStructure["ITEMS"], $confValues);
377 }
378 else
379 {
380 foreach($confStructure as $itemKey => $itemParams)
381 {
382 if(isset($confStructure[$itemKey]["VALUE"]))
383 continue;
384
385 if(isset($itemParams["ITEMS"]) && is_array($itemParams["ITEMS"]))
386 $confStructure[$itemKey]["ITEMS"] = $this->glueValuesToConfig($itemParams["ITEMS"], $confValues[$itemKey]);
387 elseif(isset($confValues[$itemKey]))
388 $confStructure[$itemKey]["VALUE"] = $confValues[$itemKey];
389 elseif(!isset($itemParams["VALUE"]) && isset($itemParams["DEFAULT"]))
390 $confStructure[$itemKey]["VALUE"] = $itemParams["DEFAULT"];
391 }
392 }
393
394 return $confStructure;
395 }
396
401 public function getConfig()
402 {
403 $configStructure = $this->getConfigStructure();
404
405 if(!is_array($configStructure))
406 throw new SystemException ("Method getConfigStructure() must return an array!");
407
408 foreach($configStructure as $key => $configSection)
409 $configStructure[$key] = $this->glueValuesToConfig($configSection, isset($this->config[$key]) ? $this->config[$key] : array());
410
411 return $configStructure;
412 }
413
417 public function getConfigValues()
418 {
419 return $this->config;
420 }
421
425 public static function getAdminFieldsList()
426 {
427 return Table::getMap();
428 }
429
434 public static function whetherAdminRestrictionsShow()
435 {
436 return true;
437 }
438
442 public static function canHasChildren()
443 {
444 return false;
445 }
446
450 public static function canHasProfiles()
451 {
452 return self::$canHasProfiles;
453 }
454
458 public static function getChildrenClassNames()
459 {
460 return array();
461 }
462
466 public function getId()
467 {
468 return $this->id;
469 }
470
474 public function getCode()
475 {
476 return $this->code;
477 }
478
482 public function getName()
483 {
484 return $this->name;
485 }
486
490 public function getDescription()
491 {
492 return $this->description;
493 }
494
498 public function getParentId()
499 {
500 return $this->parentId;
501 }
502
506 public function getSort()
507 {
508 return $this->sort;
509 }
510
514 public function getNameWithParent()
515 {
516 $result = $this->name;
517
518 if($parent = $this->getParentService())
519 $result = $parent->getName()." (".$result.")";
520
521 return $result;
522 }
523
527 public function getLogotip()
528 {
529 return $this->logotip;
530 }
531
535 public function getLogotipPath()
536 {
537 $logo = $this->getLogotip();
538 return intval($logo) > 0 ? \CFile::GetPath($logo) : "";
539 }
540
546 public function getParentService()
547 {
548 $result = null;
549
550 if(intval($this->parentId) > 0)
551 $result = Manager::getObjectById($this->parentId);
552
553 return $result;
554 }
555
560 public function prepareFieldsForUsing(array $fields)
561 {
562 return $fields;
563 }
564
569 {
570 return array();
571
572 /*
573 exapmple for concrete handlers
574 return array(
575 "ZAPALECH" => array(
576 "NAME" => "extra service name",
577 "SORT" => 50,
578 "RIGHTS" => "YYY",
579 "ACTIVE" => "Y",
580 "CLASS_NAME" => '\Bitrix\Sale\Delivery\ExtraServices\Checkbox',
581 "DESCRIPTION" => "Extra service description",
582 "PARAMS" => array("PRICE" => 2000)
583 )
584 );
585 */
586 }
587
591 public static function whetherAdminExtraServicesShow()
592 {
593 return self::$whetherAdminExtraServicesShow;
594 }
595
600 public static function onBeforeAdd(array &$fields = array()): \Bitrix\Main\Result
601 {
602 return new \Bitrix\Main\Result();
603 }
604
610 public static function onAfterAdd($serviceId, array $fields = array())
611 {
612 return true;
613 }
614
620 public static function onBeforeUpdate($serviceId, array &$fields = array())
621 {
622 return true;
623 }
624
630 public static function onAfterUpdate($serviceId, array $fields = array())
631 {
632 return true;
633 }
634
639 public static function onAfterDelete($serviceId)
640 {
641 return true;
642 }
643
648 public function isCompatible(Shipment $shipment)
649 {
650 return true;
651 }
652
660 public function getCompatibleExtraServiceIds(Shipment $shipment): ?array
661 {
662 return null;
663 }
664
668 public function getProfilesList()
669 {
670 return array();
671 }
672
676 public static function isProfile()
677 {
678 return self::$isProfile;
679 }
680
684 protected function getProfileType(): string
685 {
686 return '';
687 }
688
692 public function getTrackingClass()
693 {
694 return $this->trackingClass;
695 }
696
700 public function setTrackingClass($class)
701 {
702 $this->trackingClass = $class;
703 }
704
708 public function getTrackingParams()
709 {
710 return $this->trackingParams;
711 }
712
716 public function isTrackingInherited()
717 {
718 return false;
719 }
720
725 {
726 return self::$isCalculatePriceImmediately;
727 }
728
732 public function isRestricted()
733 {
734 return $this->restricted;
735 }
736
740 public static function onGetBusinessValueConsumers()
741 {
742 return array();
743 }
744
748 public static function onGetBusinessValueGroups()
749 {
750 return array();
751 }
752
756 public static function isInstalled()
757 {
758 return true;
759 }
760
761 public static function install()
762 {
763 return true;
764 }
765
766 public static function unInstall()
767 {
768 return true;
769 }
770
774 public function isAllowEditShipment()
775 {
776 return $this->allowEditShipment != 'N';
777 }
778
785 public function getAdminMessage()
786 {
787 return array();
788 }
789
794 public function execAdminAction()
795 {
796 return new Result();
797 }
798
803 public function getAdditionalInfoShipmentEdit(Shipment $shipment)
804 {
805 return array();
806 }
807
813 public function processAdditionalInfoShipmentEdit(Shipment $shipment, array $requestData)
814 {
815 return $shipment;
816 }
817
822 public function getAdditionalInfoShipmentView(Shipment $shipment)
823 {
824 return array();
825 }
826
831 public function getAdditionalInfoShipmentPublic(Shipment $shipment)
832 {
833 return array();
834 }
835
842 public function createClone(\SplObjectStorage $cloneEntity)
843 {
844 if ($this->isClone() && $cloneEntity->contains($this))
845 {
846 return $cloneEntity[$this];
847 }
848
849 $deliveryServiceClone = clone $this;
850 $deliveryServiceClone->isClone = true;
851
852 if (!$cloneEntity->contains($this))
853 {
854 $cloneEntity[$this] = $deliveryServiceClone;
855 }
856
858 if ($extraServices = $this->getExtraServices())
859 {
860 if (!$cloneEntity->contains($extraServices))
861 {
862 $cloneEntity[$extraServices] = $extraServices->createClone($cloneEntity);
863 }
864
865 if ($cloneEntity->contains($extraServices))
866 {
867 $deliveryServiceClone->extraServices = $cloneEntity[$extraServices];
868 }
869 }
870
871 return $deliveryServiceClone;
872 }
873
877 public function isClone()
878 {
879 return $this->isClone;
880 }
881
886 public static function getSupportedServicesList()
887 {
888 return array();
889 }
890
894 public function getAdminAdditionalTabs()
895 {
896 return array();
897 }
898
902 public function getVatId()
903 {
904 return $this->vatId;
905 }
906
910 public function setVatId($vatId)
911 {
912 $this->vatId = $vatId;
913 }
914
919 {
920 return $this->deliveryRequestHandler;
921 }
922
923 public function createProfileObject($fields)
924 {
925 return Manager::createObject($fields);
926 }
927
933 public static function isHandlerCompatible()
934 {
935 // Actually only configurable are fully compatible with all languages
936 return in_array(
938 ['', 'ru', 'kz', 'by', 'ua'],
939 true
940 );
941 }
942
946 public function getTags(): array
947 {
948 return [];
949 }
950}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
prepareFieldsForUsing(array $fields)
Definition base.php:560
static onBeforeAdd(array &$fields=array())
Definition base.php:600
static onBeforeUpdate($serviceId, array &$fields=array())
Definition base.php:620
getCompatibleExtraServiceIds(Shipment $shipment)
Definition base.php:660
calculate(\Bitrix\Sale\Shipment $shipment=null, $extraServices=array())
Definition base.php:165
getAdditionalInfoShipmentPublic(Shipment $shipment)
Definition base.php:831
isCompatible(Shipment $shipment)
Definition base.php:648
__construct(array $initParams)
Definition base.php:66
prepareFieldsForSaving(array $fields)
Definition base.php:311
static onAfterAdd($serviceId, array $fields=array())
Definition base.php:610
glueValuesToConfig(array $confStructure, $confValues=array())
Definition base.php:369
static onAfterDelete($serviceId)
Definition base.php:639
calculateConcrete(\Bitrix\Sale\Shipment $shipment)
Definition base.php:296
static onAfterUpdate($serviceId, array $fields=array())
Definition base.php:630
getAdditionalInfoShipmentEdit(Shipment $shipment)
Definition base.php:803
processAdditionalInfoShipmentEdit(Shipment $shipment, array $requestData)
Definition base.php:813
getAdditionalInfoShipmentView(Shipment $shipment)
Definition base.php:822