PageRenderTime 26ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/sale/lib/delivery/services/automatic_profile.php

https://gitlab.com/alexprowars/bitrix
PHP | 426 lines | 408 code | 12 blank | 6 comment | 6 complexity | 89a8d62acfd6fa9f718e13e2093d1147 MD5 | raw file
  1. <?php
  2. namespace Bitrix\Sale\Delivery\Services;
  3. use Bitrix\Currency\CurrencyManager;
  4. use Bitrix\Main\SystemException;
  5. use Bitrix\Main\Localization\Loc;
  6. use Bitrix\Main\ArgumentNullException;
  7. use Bitrix\Sale\Shipment;
  8. Loc::loadMessages(__FILE__);
  9. /**
  10. * Class AutomaticProfile
  11. * Adapter for old delivery services profile to work with new API.
  12. * @package Bitrix\Sale\Delivery\Services
  13. */
  14. class AutomaticProfile extends Base
  15. {
  16. protected $profileId = "";
  17. protected $oldConfig;
  18. protected $parentSid;
  19. /** @var Automatic|null $parentAutomatic */
  20. protected $parentAutomatic = null;
  21. protected $parentHandlerInitParams = array();
  22. protected $profileConfig = null;
  23. protected $profileOldConfig = null;
  24. protected $profileOldConfigActualized = false;
  25. protected static $isProfile = true;
  26. public function __construct(array $initParams)
  27. {
  28. if(!isset($initParams["PARENT_ID"]))
  29. throw new ArgumentNullException('initParams["PARENT_ID"]');
  30. $this->parentAutomatic = Manager::getObjectById($initParams["PARENT_ID"]);
  31. if(!$this->parentAutomatic || !($this->parentAutomatic instanceof Automatic))
  32. throw new SystemException("Can't initialize AutomaticProfile's id: ".$initParams["ID"]." parent Automatic parent_id: ".$initParams["PARENT_ID"]);
  33. $this->parentSid = $this->parentAutomatic->getSid();
  34. if($this->parentSid == '')
  35. throw new SystemException("Can't determine AutomaticProfile's SID. profile id: ".$initParams["ID"]." parent Automatic id: ".$initParams["PARENT_ID"]);
  36. $this->parentHandlerInitParams = $this->parentAutomatic->getHandlerInitParams($this->parentSid);
  37. if($this->parentHandlerInitParams === false)
  38. throw new SystemException("Can't get init services params of Automatic delivery service with sid: ".$this->parentSid);
  39. parent::__construct($initParams);
  40. if(isset($initParams['PROFILE_ID']) && $initParams['PROFILE_ID'] <> '')
  41. $this->profileId = $initParams['PROFILE_ID'];
  42. elseif(isset($this->config["MAIN"]["PROFILE_ID"]))
  43. $this->profileId = $this->config["MAIN"]["PROFILE_ID"];
  44. if($this->profileId <> '' && !array_key_exists($this->profileId, $this->parentHandlerInitParams["PROFILES"]))
  45. throw new SystemException("Profile \"".$this->profileId."\" is not part of Automatic delivery service with sid: ".$this->parentSid);
  46. if($this->name == '' && isset($this->parentHandlerInitParams['PROFILES'][$this->profileId]['TITLE']))
  47. $this->name = $this->parentHandlerInitParams['PROFILES'][$this->profileId]['TITLE'];
  48. if($this->description == '' && isset($this->parentHandlerInitParams['PROFILES'][$this->profileId]['DESCRIPTION']))
  49. $this->description = $this->parentHandlerInitParams['PROFILES'][$this->profileId]['DESCRIPTION'];
  50. if(!empty($this->parentHandlerInitParams["PROFILES"][$this->profileId]["TRACKING_CLASS_NAME"]))
  51. $this->trackingClass = $this->parentHandlerInitParams["PROFILES"][$this->profileId]["TRACKING_CLASS_NAME"];
  52. $this->inheritParams();
  53. }
  54. protected function inheritParams()
  55. {
  56. if($this->name == '') $this->name = $this->parentAutomatic->getName();
  57. if(intval($this->logotip) <= 0) $this->logotip = $this->parentAutomatic->getLogotip();
  58. if($this->description == '') $this->description = $this->parentAutomatic->getDescription();
  59. if($this->trackingClass == '') $this->trackingClass = $this->parentAutomatic->getTrackingClass();
  60. $parentTP = $this->parentAutomatic->getTrackingParams();
  61. if(is_array($parentTP) && !empty($parentTP))
  62. {
  63. if(empty($this->trackingParams) || !is_array($this->trackingParams))
  64. {
  65. $this->trackingParams = $parentTP;
  66. }
  67. else
  68. {
  69. foreach($this->trackingParams as $k => $v)
  70. if(empty($v) && !empty($parentTP[$k]))
  71. $this->trackingParams[$k] = $parentTP[$k];
  72. }
  73. }
  74. }
  75. /**
  76. * @param bool $actualizeProfiles
  77. * @return array Old config.
  78. * @internal
  79. */
  80. public function getOldConfig($actualizeProfiles = false)
  81. {
  82. if($this->profileOldConfig === null)
  83. {
  84. $own = Automatic::createConfig($this->parentHandlerInitParams, $this->config["MAIN"]["OLD_SETTINGS"]);
  85. $parent = $this->getParentService()->getOldConfig();
  86. $profileOldConfig = array(
  87. "CONFIG" => array_merge(
  88. isset($parent["CONFIG"]) && is_array($parent["CONFIG"]) ? $parent["CONFIG"] : array(),
  89. isset($own["CONFIG"]) && is_array($own["CONFIG"]) ? $own["CONFIG"] : array()
  90. ),
  91. "CONFIG_GROUPS" =>
  92. isset($parent["CONFIG_GROUPS"]) && is_array($parent["CONFIG"])? $parent["CONFIG_GROUPS"] : array()
  93. );
  94. if(isset($own["CONFIG"]) && is_array($own["CONFIG"]))
  95. foreach($own["CONFIG"] as $k => $v)
  96. if(empty($v["GROUP"]) || $v["GROUP"] != $this->profileId)
  97. $profileOldConfig["CONFIG"][$k] = $parent["CONFIG"][$k];
  98. $this->profileOldConfig = $profileOldConfig;
  99. }
  100. if($actualizeProfiles && !$this->profileOldConfigActualized)
  101. {
  102. $this->profileOldConfig = $this->actualizeProfilesConfig($this->profileOldConfig);
  103. $this->profileOldConfigActualized = true;
  104. }
  105. return $this->profileOldConfig;
  106. }
  107. /**
  108. * @param array $oldConfig
  109. * @return array
  110. */
  111. protected function actualizeProfilesConfig(array $oldConfig)
  112. {
  113. $profiles = Manager::getByParentId($this->parentId);
  114. $actualizedCodes = array();
  115. foreach($profiles as $id => $fields)
  116. {
  117. if($id == $this->id)
  118. continue;
  119. if($fields['CODE'] <> '')
  120. {
  121. if($fields['CODE'] == $this->code)
  122. continue;
  123. if(in_array($fields['CODE'], $actualizedCodes))
  124. continue;
  125. }
  126. /** @var \Bitrix\Sale\Delivery\Services\AutomaticProfile $service */
  127. $service = Manager::getPooledObject($fields);
  128. if($service)
  129. {
  130. $config = $service->getConfig();
  131. $serviceProfileId = $config['MAIN']['ITEMS']['PROFILE_ID']['VALUE'];
  132. if($serviceProfileId == $this->profileId)
  133. continue;
  134. $profileOldConfig = $service->getOldConfig();
  135. foreach($profileOldConfig['CONFIG'] as $k => $v)
  136. if(isset($v['GROUP']) && $v['GROUP'] == $serviceProfileId)
  137. $oldConfig['CONFIG'][$k] = $v;
  138. $actualizedCodes[] = $fields['CODE'];
  139. }
  140. }
  141. return $oldConfig;
  142. }
  143. public function getConfig()
  144. {
  145. if($this->profileConfig === null)
  146. {
  147. $configStructure = $this->getConfigStructure();
  148. $profileConfig = array();
  149. foreach($configStructure as $key => $configSection)
  150. $profileConfig[$key] = $this->glueValuesToConfig($configSection, isset($this->config[$key]) ? $this->config[$key] : array());
  151. if($this->profileId <> '')
  152. {
  153. $oldConfig = Automatic::createConfig($this->parentHandlerInitParams, $this->config["MAIN"]["OLD_SETTINGS"]);
  154. $newConfig = Automatic::convertOldConfigToNew($oldConfig);
  155. foreach($newConfig as $groupId => $groupParams)
  156. if($groupId != $this->profileId)
  157. unset($newConfig[$groupId]);
  158. $profileConfig= array_merge($this->config, $profileConfig, $newConfig);
  159. }
  160. $this->profileConfig = $profileConfig;
  161. }
  162. return $this->profileConfig;
  163. }
  164. public static function getClassTitle()
  165. {
  166. return Loc::getMessage("SALE_DLVR_HANDL_AUTP_NAME");
  167. }
  168. public static function getClassDescription()
  169. {
  170. return Loc::getMessage("SALE_DLVR_HANDL_AUTP_DESCRIPTION");
  171. }
  172. protected function calculateConcrete(\Bitrix\Sale\Shipment $shipment)
  173. {
  174. $result = $this->parentAutomatic->calculateProfile($this->profileId, $this->getOldConfig(true), $shipment);
  175. $result->setDeliveryPrice(
  176. $result->getPrice() + $this->getMarginPrice($result->getPrice())
  177. );
  178. return $result;
  179. }
  180. protected function getMarginPrice($price)
  181. {
  182. if($this->config["MAIN"]["MARGIN_TYPE"] == "%")
  183. $marginPrice = $price * floatval($this->config["MAIN"]["MARGIN_VALUE"]) / 100;
  184. else
  185. $marginPrice = floatval($this->config["MAIN"]["MARGIN_VALUE"]);
  186. return $marginPrice;
  187. }
  188. protected function getConfigStructure()
  189. {
  190. static $profiles = null;
  191. if($profiles === null)
  192. {
  193. $profiles = array("" => "");
  194. foreach($this->parentHandlerInitParams["PROFILES"] as $profileId => $profileParams)
  195. if($profileParams["TITLE"] <> '')
  196. $profiles[$profileId] = $profileParams["TITLE"]." [".$profileId."]";
  197. }
  198. $result = array(
  199. "MAIN" => array(
  200. "TITLE" => Loc::getMessage("SALE_DLVR_HANDL_AUTP_CONF_MAIN_TITLE"),
  201. "DESCRIPTION" => Loc::getMessage("SALE_DLVR_HANDL_AUTP_CONF_MAIN_DESCR"),
  202. "ITEMS" => array (
  203. "PROFILE_ID" => array(
  204. "TYPE" => "STRING",
  205. "NAME" => "PROFILE_ID",
  206. "HIDDEN" => true,
  207. "DEFAULT" => $this->profileId
  208. ),
  209. "PROFILE_NAME" => array(
  210. "TYPE" => "STRING",
  211. "NAME" => Loc::getMessage("SALE_DLVR_HANDL_AUTP_CONF_MAIN_PROFILE_ID"),
  212. "DEFAULT" => $profiles[$this->profileId],
  213. "READONLY" => true
  214. )
  215. )
  216. )
  217. );
  218. $serviceCurrency = $this->currency;
  219. if(\Bitrix\Main\Loader::includeModule('currency'))
  220. {
  221. $currencyList = CurrencyManager::getCurrencyList();
  222. if (isset($currencyList[$this->currency]))
  223. $serviceCurrency = $currencyList[$this->currency];
  224. unset($currencyList);
  225. }
  226. $marginTypes = array(
  227. "%" => "%",
  228. "CURRENCY" => $serviceCurrency
  229. );
  230. $result["MAIN"]["ITEMS"]["MARGIN_VALUE"] = array(
  231. "TYPE" => "STRING",
  232. "NAME" => Loc::getMessage("SALE_DLVR_HANDL_AUT_MARGIN_VALUE"),
  233. "DEFAULT" => 0
  234. );
  235. $result["MAIN"]["ITEMS"]["MARGIN_TYPE"] = array(
  236. "TYPE" => "ENUM",
  237. "NAME" => Loc::getMessage("SALE_DLVR_HANDL_AUT_MARGIN_TYPE"),
  238. "DEFAULT" => "%",
  239. "OPTIONS" => $marginTypes
  240. );
  241. $configProfileIds = array_keys($this->parentHandlerInitParams["PROFILES"]);
  242. if($this->profileId <> '' && in_array($this->profileId, $configProfileIds))
  243. {
  244. $oldAutoConfig = $this->parentAutomatic->getOldConfig();
  245. if($oldAutoConfig && isset($oldAutoConfig["CONFIG_GROUPS"]) && is_array($oldAutoConfig["CONFIG_GROUPS"]))
  246. {
  247. foreach($oldAutoConfig["CONFIG_GROUPS"] as $key => $groupId)
  248. if($this->profileId != $groupId)
  249. unset($oldAutoConfig["CONFIG_GROUPS"][$key]);
  250. foreach($oldAutoConfig["CONFIG"] as $key => $params)
  251. if($this->profileId != $params["CONFIG"])
  252. unset($oldAutoConfig["CONFIG"][$key]);
  253. }
  254. $oldConfig = Automatic::convertOldConfigToNew($oldAutoConfig);
  255. if(!empty($oldConfig))
  256. {
  257. if(isset($oldConfig["CONFIG_GROUPS"]["MAIN"]))
  258. {
  259. $oldConfig["CONFIG_GROUPS"]["MAIN_OLD"] = $oldConfig["CONFIG_GROUPS"]["MAIN"];
  260. unset($oldConfig["CONFIG_GROUPS"]["MAIN"]);
  261. }
  262. $result = array_merge($result, $oldConfig);
  263. }
  264. }
  265. return $result;
  266. }
  267. public function prepareFieldsForSaving(array $fields)
  268. {
  269. $parentAutoConfig = $this->parentAutomatic->getConfigValues();
  270. if(isset($fields["CONFIG"]) && is_array($fields["CONFIG"]))
  271. $fields["CONFIG"] = array_merge($parentAutoConfig, $fields["CONFIG"]);
  272. $configMain = $fields["CONFIG"]["MAIN"];
  273. $handler = $this->parentHandlerInitParams;
  274. if (isset($handler["DBSETSETTINGS"]) && is_callable($handler["DBSETSETTINGS"]))
  275. {
  276. $oldSettings = $fields["CONFIG"];
  277. unset($oldSettings["MAIN"]);
  278. if(is_array($oldSettings))
  279. $oldSettings = Automatic::convertNewSettingsToOld($oldSettings);
  280. if (!$strOldSettings = call_user_func($handler["DBSETSETTINGS"], $oldSettings))
  281. throw new SystemException("Can't save delivery services's old settings");
  282. }
  283. else
  284. {
  285. $strOldSettings = "";
  286. }
  287. $strOldSettings = serialize($strOldSettings);
  288. $fields["CONFIG"] = array(
  289. "MAIN" => $configMain
  290. );
  291. $fields["CONFIG"]["MAIN"]["OLD_SETTINGS"] = $strOldSettings;
  292. $fields = parent::prepareFieldsForSaving($fields);
  293. $fields["CODE"] = $this->parentAutomatic->getSid().":".$this->profileId;
  294. if(!empty($fields['TRACKING_PARAMS']) && is_array($fields['TRACKING_PARAMS']))
  295. {
  296. $parentTP = $this->parentAutomatic->getTrackingParams();
  297. foreach($fields['TRACKING_PARAMS'] as $k => $v)
  298. if(!empty($parentTP[$k]) && $v == $parentTP[$k])
  299. $fields['TRACKING_PARAMS'][$k] ='';
  300. }
  301. return $fields;
  302. }
  303. public function getParentService()
  304. {
  305. return $this->parentAutomatic;
  306. }
  307. public function isCompatible(Shipment $shipment)
  308. {
  309. return $this->parentAutomatic->isProfileCompatible($this->profileId, $this->getOldConfig(true), $shipment);
  310. }
  311. public static function isProfile()
  312. {
  313. return self::$isProfile;
  314. }
  315. /**
  316. * @inheritDoc
  317. */
  318. protected function getProfileType(): string
  319. {
  320. return (string)$this->profileId;
  321. }
  322. public function getAdditionalInfoShipmentEdit(Shipment $shipment)
  323. {
  324. return $this->parentAutomatic->getAdditionalInfoShipmentEdit($shipment);
  325. }
  326. public function processAdditionalInfoShipmentEdit(Shipment $shipment, array $requestData)
  327. {
  328. return $this->parentAutomatic->processAdditionalInfoShipmentEdit($shipment, $requestData);
  329. }
  330. public function getAdditionalInfoShipmentView(Shipment $shipment)
  331. {
  332. return $this->parentAutomatic->getAdditionalInfoShipmentView($shipment);
  333. }
  334. }