Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
automatic_profile.php
1<?php
2
4
10
11Loc::loadMessages(__FILE__);
12
19{
20 protected $profileId = "";
21 protected $oldConfig;
22 protected $parentSid;
24 protected $parentAutomatic = null;
25 protected $parentHandlerInitParams = array();
26 protected $profileConfig = null;
27 protected $profileOldConfig = null;
28 protected $profileOldConfigActualized = false;
29
30 protected static $isProfile = true;
31
32 public function __construct(array $initParams)
33 {
34 if(!isset($initParams["PARENT_ID"]))
35 throw new ArgumentNullException('initParams["PARENT_ID"]');
36
37 $this->parentAutomatic = Manager::getObjectById($initParams["PARENT_ID"]);
38
39 if(!$this->parentAutomatic || !($this->parentAutomatic instanceof Automatic))
40 throw new SystemException("Can't initialize AutomaticProfile's id: ".$initParams["ID"]." parent Automatic parent_id: ".$initParams["PARENT_ID"]);
41
42 $this->parentSid = $this->parentAutomatic->getSid();
43
44 if($this->parentSid == '')
45 throw new SystemException("Can't determine AutomaticProfile's SID. profile id: ".$initParams["ID"]." parent Automatic id: ".$initParams["PARENT_ID"]);
46
47 $this->parentHandlerInitParams = $this->parentAutomatic->getHandlerInitParams($this->parentSid);
48
49 if($this->parentHandlerInitParams === false)
50 throw new SystemException("Can't get init services params of Automatic delivery service with sid: ".$this->parentSid);
51
52 parent::__construct($initParams);
53
54 if(isset($initParams['PROFILE_ID']) && $initParams['PROFILE_ID'] <> '')
55 $this->profileId = $initParams['PROFILE_ID'];
56 elseif(isset($this->config["MAIN"]["PROFILE_ID"]))
57 $this->profileId = $this->config["MAIN"]["PROFILE_ID"];
58
59 if($this->profileId <> '' && !array_key_exists($this->profileId, $this->parentHandlerInitParams["PROFILES"]))
60 throw new SystemException("Profile \"".$this->profileId."\" is not part of Automatic delivery service with sid: ".$this->parentSid);
61
62 if($this->name == '' && isset($this->parentHandlerInitParams['PROFILES'][$this->profileId]['TITLE']))
63 $this->name = $this->parentHandlerInitParams['PROFILES'][$this->profileId]['TITLE'];
64
65 if($this->description == '' && isset($this->parentHandlerInitParams['PROFILES'][$this->profileId]['DESCRIPTION']))
66 $this->description = $this->parentHandlerInitParams['PROFILES'][$this->profileId]['DESCRIPTION'];
67
68 if(!empty($this->parentHandlerInitParams["PROFILES"][$this->profileId]["TRACKING_CLASS_NAME"]))
69 $this->trackingClass = $this->parentHandlerInitParams["PROFILES"][$this->profileId]["TRACKING_CLASS_NAME"];
70
71 $this->inheritParams();
72 }
73
74 protected function inheritParams()
75 {
76 if($this->name == '') $this->name = $this->parentAutomatic->getName();
77 if(intval($this->logotip) <= 0) $this->logotip = $this->parentAutomatic->getLogotip();
78 if($this->description == '') $this->description = $this->parentAutomatic->getDescription();
79 if($this->trackingClass == '') $this->trackingClass = $this->parentAutomatic->getTrackingClass();
80
81 $parentTP = $this->parentAutomatic->getTrackingParams();
82
83 if(is_array($parentTP) && !empty($parentTP))
84 {
85 if(empty($this->trackingParams) || !is_array($this->trackingParams))
86 {
87 $this->trackingParams = $parentTP;
88 }
89 else
90 {
91 foreach($this->trackingParams as $k => $v)
92 if(empty($v) && !empty($parentTP[$k]))
93 $this->trackingParams[$k] = $parentTP[$k];
94 }
95 }
96 }
97
104 public function getOldConfig($actualizeProfiles = false)
105 {
106 if($this->profileOldConfig === null)
107 {
108 $own = Automatic::createConfig($this->parentHandlerInitParams, $this->config["MAIN"]["OLD_SETTINGS"] ?? '');
109 $parent = $this->getParentService()->getOldConfig();
110
111 $profileOldConfig = array(
112 "CONFIG" => array_merge(
113 isset($parent["CONFIG"]) && is_array($parent["CONFIG"]) ? $parent["CONFIG"] : array(),
114 isset($own["CONFIG"]) && is_array($own["CONFIG"]) ? $own["CONFIG"] : array()
115 ),
116 "CONFIG_GROUPS" =>
117 isset($parent["CONFIG_GROUPS"]) && is_array($parent["CONFIG"])? $parent["CONFIG_GROUPS"] : array()
118 );
119
120 if(isset($own["CONFIG"]) && is_array($own["CONFIG"]))
121 foreach($own["CONFIG"] as $k => $v)
122 if(empty($v["GROUP"]) || $v["GROUP"] != $this->profileId)
123 $profileOldConfig["CONFIG"][$k] = $parent["CONFIG"][$k];
124
125 $this->profileOldConfig = $profileOldConfig;
126 }
127
128 if($actualizeProfiles && !$this->profileOldConfigActualized)
129 {
130 $this->profileOldConfig = $this->actualizeProfilesConfig($this->profileOldConfig);
131 $this->profileOldConfigActualized = true;
132 }
133
135 }
136
141 protected function actualizeProfilesConfig(array $oldConfig)
142 {
143 $profiles = Manager::getByParentId($this->parentId);
144 $actualizedCodes = array();
145
146 foreach($profiles as $id => $fields)
147 {
148 if($id == $this->id)
149 continue;
150
151 if($fields['CODE'] <> '')
152 {
153 if($fields['CODE'] == $this->code)
154 continue;
155
156 if(in_array($fields['CODE'], $actualizedCodes))
157 continue;
158 }
159
161 $service = Manager::getPooledObject($fields);
162
163 if($service)
164 {
165 $config = $service->getConfig();
166 $serviceProfileId = $config['MAIN']['ITEMS']['PROFILE_ID']['VALUE'];
167
168 if($serviceProfileId == $this->profileId)
169 continue;
170
171 $profileOldConfig = $service->getOldConfig();
172
173 foreach($profileOldConfig['CONFIG'] as $k => $v)
174 if(isset($v['GROUP']) && $v['GROUP'] == $serviceProfileId)
175 $oldConfig['CONFIG'][$k] = $v;
176
177 $actualizedCodes[] = $fields['CODE'];
178 }
179 }
180
181 return $oldConfig;
182 }
183
184 public function getConfig()
185 {
186 if($this->profileConfig === null)
187 {
188 $configStructure = $this->getConfigStructure();
189 $profileConfig = array();
190
191 foreach($configStructure as $key => $configSection)
192 $profileConfig[$key] = $this->glueValuesToConfig($configSection, isset($this->config[$key]) ? $this->config[$key] : array());
193
194 if($this->profileId <> '')
195 {
196 $oldConfig = Automatic::createConfig($this->parentHandlerInitParams, $this->config["MAIN"]["OLD_SETTINGS"] ?? '');
198
199 foreach($newConfig as $groupId => $groupParams)
200 if($groupId != $this->profileId)
201 unset($newConfig[$groupId]);
202
203 $profileConfig= array_merge($this->config, $profileConfig, $newConfig);
204 }
205
206 $this->profileConfig = $profileConfig;
207 }
208
210 }
211
212 public static function getClassTitle()
213 {
214 return Loc::getMessage("SALE_DLVR_HANDL_AUTP_NAME");
215 }
216
217 public static function getClassDescription()
218 {
219 return Loc::getMessage("SALE_DLVR_HANDL_AUTP_DESCRIPTION");
220 }
221
222 protected function calculateConcrete(\Bitrix\Sale\Shipment $shipment)
223 {
224 $result = $this->parentAutomatic->calculateProfile($this->profileId, $this->getOldConfig(true), $shipment);
225
226 $result->setDeliveryPrice(
227 $result->getPrice() + $this->getMarginPrice($result->getPrice())
228 );
229
230 return $result;
231 }
232
233 protected function getMarginPrice($price)
234 {
235 if($this->config["MAIN"]["MARGIN_TYPE"] == "%")
236 $marginPrice = $price * floatval($this->config["MAIN"]["MARGIN_VALUE"]) / 100;
237 else
238 $marginPrice = floatval($this->config["MAIN"]["MARGIN_VALUE"]);
239
240 return $marginPrice;
241 }
242
243 protected function getConfigStructure()
244 {
245 static $profiles = null;
246
247 if($profiles === null)
248 {
249 $profiles = array("" => "");
250
251 foreach($this->parentHandlerInitParams["PROFILES"] as $profileId => $profileParams)
252 if($profileParams["TITLE"] <> '')
253 $profiles[$profileId] = $profileParams["TITLE"]." [".$profileId."]";
254 }
255
256 $result = array(
257 "MAIN" => array(
258 "TITLE" => Loc::getMessage("SALE_DLVR_HANDL_AUTP_CONF_MAIN_TITLE"),
259 "DESCRIPTION" => Loc::getMessage("SALE_DLVR_HANDL_AUTP_CONF_MAIN_DESCR"),
260 "ITEMS" => array (
261 "PROFILE_ID" => array(
262 "TYPE" => "STRING",
263 "NAME" => "PROFILE_ID",
264 "HIDDEN" => true,
265 "DEFAULT" => $this->profileId
266 ),
267 "PROFILE_NAME" => array(
268 "TYPE" => "STRING",
269 "NAME" => Loc::getMessage("SALE_DLVR_HANDL_AUTP_CONF_MAIN_PROFILE_ID"),
270 "DEFAULT" => $profiles[$this->profileId] ?? '',
271 "READONLY" => true
272 )
273 )
274 )
275 );
276
277 $serviceCurrency = $this->currency;
278
279 if(\Bitrix\Main\Loader::includeModule('currency'))
280 {
281 $currencyList = CurrencyManager::getCurrencyList();
282
283 if (isset($currencyList[$this->currency]))
284 $serviceCurrency = $currencyList[$this->currency];
285
286 unset($currencyList);
287 }
288
289 $marginTypes = array(
290 "%" => "%",
291 "CURRENCY" => $serviceCurrency
292 );
293
294 $result["MAIN"]["ITEMS"]["MARGIN_VALUE"] = array(
295 "TYPE" => "STRING",
296 "NAME" => Loc::getMessage("SALE_DLVR_HANDL_AUT_MARGIN_VALUE"),
297 "DEFAULT" => 0
298 );
299
300 $result["MAIN"]["ITEMS"]["MARGIN_TYPE"] = array(
301 "TYPE" => "ENUM",
302 "NAME" => Loc::getMessage("SALE_DLVR_HANDL_AUT_MARGIN_TYPE"),
303 "DEFAULT" => "%",
304 "OPTIONS" => $marginTypes
305 );
306
307 $configProfileIds = array_keys($this->parentHandlerInitParams["PROFILES"]);
308
309 if($this->profileId <> '' && in_array($this->profileId, $configProfileIds))
310 {
311 $oldAutoConfig = $this->parentAutomatic->getOldConfig();
312
313 if($oldAutoConfig && isset($oldAutoConfig["CONFIG_GROUPS"]) && is_array($oldAutoConfig["CONFIG_GROUPS"]))
314 {
315 foreach($oldAutoConfig["CONFIG_GROUPS"] as $key => $groupId)
316 if($this->profileId != $groupId)
317 unset($oldAutoConfig["CONFIG_GROUPS"][$key]);
318
319 foreach($oldAutoConfig["CONFIG"] as $key => $params)
320 {
321 if (!isset($params["CONFIG"]))
322 {
323 unset($oldAutoConfig["CONFIG"][$key]);
324 }
325 elseif ($this->profileId != $params["CONFIG"])
326 {
327 unset($oldAutoConfig["CONFIG"][$key]);
328 }
329 }
330 }
331
333
334 if(!empty($oldConfig))
335 {
336 if(isset($oldConfig["CONFIG_GROUPS"]["MAIN"]))
337 {
338 $oldConfig["CONFIG_GROUPS"]["MAIN_OLD"] = $oldConfig["CONFIG_GROUPS"]["MAIN"];
339 unset($oldConfig["CONFIG_GROUPS"]["MAIN"]);
340 }
341
342 $result = array_merge($result, $oldConfig);
343 }
344 }
345
346 return $result;
347 }
348
349 public function prepareFieldsForSaving(array $fields)
350 {
351 $parentAutoConfig = $this->parentAutomatic->getConfigValues();
352
353 if(isset($fields["CONFIG"]) && is_array($fields["CONFIG"]))
354 $fields["CONFIG"] = array_merge($parentAutoConfig, $fields["CONFIG"]);
355
356 $configMain = $fields["CONFIG"]["MAIN"];
358
359 if (isset($handler["DBSETSETTINGS"]) && is_callable($handler["DBSETSETTINGS"]))
360 {
361 $oldSettings = $fields["CONFIG"];
362 unset($oldSettings["MAIN"]);
363
364 if(is_array($oldSettings))
365 $oldSettings = Automatic::convertNewSettingsToOld($oldSettings);
366
367 if (!$strOldSettings = call_user_func($handler["DBSETSETTINGS"], $oldSettings))
368 throw new SystemException("Can't save delivery services's old settings");
369 }
370 else
371 {
372 $strOldSettings = "";
373 }
374
375 $strOldSettings = serialize($strOldSettings);
376
377 $fields["CONFIG"] = array(
378 "MAIN" => $configMain
379 );
380
381 $fields["CONFIG"]["MAIN"]["OLD_SETTINGS"] = $strOldSettings;
382 $fields = parent::prepareFieldsForSaving($fields);
383 $fields["CODE"] = $this->parentAutomatic->getSid().":".$this->profileId;
384
385 if(!empty($fields['TRACKING_PARAMS']) && is_array($fields['TRACKING_PARAMS']))
386 {
387 $parentTP = $this->parentAutomatic->getTrackingParams();
388
389 foreach($fields['TRACKING_PARAMS'] as $k => $v)
390 {
391 if (!empty($parentTP[$k]) && $v == $parentTP[$k])
392 {
393 $fields['TRACKING_PARAMS'][$k] = '';
394 }
395 }
396 }
397
398 return $fields;
399 }
400
401 public function getParentService()
402 {
404 }
405
406 public function isCompatible(Shipment $shipment)
407 {
408 return $this->parentAutomatic->isProfileCompatible($this->profileId, $this->getOldConfig(true), $shipment);
409 }
410
411 public static function isProfile()
412 {
413 return self::$isProfile;
414 }
415
419 protected function getProfileType(): string
420 {
421 return (string)$this->profileId;
422 }
423
424 public function getAdditionalInfoShipmentEdit(Shipment $shipment)
425 {
426 return $this->parentAutomatic->getAdditionalInfoShipmentEdit($shipment);
427 }
428
429 public function processAdditionalInfoShipmentEdit(Shipment $shipment, array $requestData)
430 {
431 return $this->parentAutomatic->processAdditionalInfoShipmentEdit($shipment, $requestData);
432 }
433
434 public function getAdditionalInfoShipmentView(Shipment $shipment)
435 {
436 return $this->parentAutomatic->getAdditionalInfoShipmentView($shipment);
437 }
438}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static convertNewSettingsToOld(array $newSettings=array())
static createConfig($initHandlerParams, $settings, $siteId=false)
calculateConcrete(\Bitrix\Sale\Shipment $shipment)
processAdditionalInfoShipmentEdit(Shipment $shipment, array $requestData)
static getObjectById($deliveryId)
Definition manager.php:438
static getPooledObject(array $fields)
Definition manager.php:424