PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/sale/lib/delivery/restrictions/bylocation.php

https://gitlab.com/alexprowars/bitrix
PHP | 295 lines | 217 code | 49 blank | 29 comment | 37 complexity | 1dfe0f319434cfce05bb5a58fab6694a MD5 | raw file
  1. <?php
  2. namespace Bitrix\Sale\Delivery\Restrictions;
  3. use Bitrix\Main\Localization\Loc;
  4. use Bitrix\Sale\Internals\CollectableEntity;
  5. use Bitrix\Sale\Internals\Entity;
  6. use Bitrix\Sale\Location\GroupLocationTable;
  7. use Bitrix\Sale\Location\LocationTable;
  8. use Bitrix\Sale\Order;
  9. use Bitrix\Sale\Shipment;
  10. Loc::loadMessages(__FILE__);
  11. /**
  12. * Class ByLocation
  13. * Restricts delivery by location(s)
  14. * @package Bitrix\Sale\Delivery\Restrictions
  15. */
  16. class ByLocation extends Base
  17. {
  18. public static $easeSort = 200;
  19. public static function getClassTitle()
  20. {
  21. return Loc::getMessage("SALE_DLVR_RSTR_BY_LOCATION_NAME");
  22. }
  23. public static function getClassDescription()
  24. {
  25. return Loc::getMessage("SALE_DLVR_RSTR_BY_LOCATION_DESCRIPT");
  26. }
  27. protected static function getD2LClass()
  28. {
  29. return '\Bitrix\Sale\Delivery\DeliveryLocationTable';
  30. }
  31. /**
  32. * This function should accept only location CODE, not ID, being a part of modern API
  33. * @param string $locationCode
  34. * @param array $restrictionParams
  35. * @param int $deliveryId
  36. * @return bool
  37. */
  38. public static function check($locationCode, array $restrictionParams, $deliveryId = 0)
  39. {
  40. if(intval($deliveryId) <= 0)
  41. return true;
  42. if($locationCode == '')
  43. return false;
  44. try
  45. {
  46. $class = static::getD2LClass();
  47. return $class::checkConnectionExists(
  48. intval($deliveryId),
  49. $locationCode,
  50. array(
  51. 'LOCATION_LINK_TYPE' => 'AUTO'
  52. )
  53. );
  54. }
  55. catch(\Bitrix\Sale\Location\Tree\NodeNotFoundException $e)
  56. {
  57. return false;
  58. }
  59. }
  60. protected static function extractParams(Entity $entity)
  61. {
  62. if ($entity instanceof CollectableEntity)
  63. {
  64. /** @var \Bitrix\Sale\Order $order */
  65. $order = $entity->getCollection()->getOrder();
  66. }
  67. elseif ($entity instanceof Order)
  68. {
  69. /** @var \Bitrix\Sale\Order $order */
  70. $order = $entity;
  71. }
  72. if (!$order)
  73. return '';
  74. if(!$props = $order->getPropertyCollection())
  75. return '';
  76. if(!$locationProp = $props->getDeliveryLocation())
  77. return '';
  78. if(!$locationCode = $locationProp->getValue())
  79. return '';
  80. return $locationCode;
  81. }
  82. protected static function prepareParamsForSaving(array $params = array(), $deliveryId = 0)
  83. {
  84. $class = static::getD2LClass();
  85. if($deliveryId > 0)
  86. {
  87. $arLocation = array();
  88. if(!!\CSaleLocation::isLocationProEnabled())
  89. {
  90. if($params["LOCATION"][$class::DB_LOCATION_FLAG] <> '')
  91. {
  92. $LOCATION1 = explode(':', $params["LOCATION"][$class::DB_LOCATION_FLAG]);
  93. }
  94. if($params["LOCATION"][$class::DB_GROUP_FLAG] <> '')
  95. {
  96. $LOCATION2 = explode(':', $params["LOCATION"][$class::DB_GROUP_FLAG]);
  97. }
  98. }
  99. if (isset($LOCATION1) && is_array($LOCATION1) && count($LOCATION1) > 0)
  100. {
  101. $arLocation[$class::DB_LOCATION_FLAG] = array();
  102. $locationCount = count($LOCATION1);
  103. for ($i = 0; $i<$locationCount; $i++)
  104. if($LOCATION1[$i] <> '')
  105. {
  106. $arLocation[$class::DB_LOCATION_FLAG][] = $LOCATION1[$i];
  107. }
  108. }
  109. if (isset($LOCATION2) && is_array($LOCATION2) && count($LOCATION2) > 0)
  110. {
  111. $arLocation[$class::DB_GROUP_FLAG] = array();
  112. $locationCount = count($LOCATION2);
  113. for ($i = 0; $i<$locationCount; $i++)
  114. if($LOCATION2[$i] <> '')
  115. {
  116. $arLocation[$class::DB_GROUP_FLAG][] = $LOCATION2[$i];
  117. }
  118. }
  119. $class::resetMultipleForOwner($deliveryId, $arLocation);
  120. }
  121. return array();
  122. }
  123. public static function getParamsStructure($deliveryId = 0)
  124. {
  125. $result = array(
  126. "LOCATION" => array(
  127. "TYPE" => "LOCATION_MULTI"
  128. //'LABEL' => Loc::getMessage("SALE_DLVR_RSTR_BY_LOCATION_LOC"),
  129. )
  130. );
  131. if($deliveryId > 0 )
  132. $result["LOCATION"]["DELIVERY_ID"] = $deliveryId;
  133. return $result;
  134. }
  135. public static function save(array $fields, $restrictionId = 0)
  136. {
  137. $fields["PARAMS"] = self::prepareParamsForSaving($fields["PARAMS"], $fields["SERVICE_ID"]);
  138. return parent::save($fields, $restrictionId);
  139. }
  140. public static function delete($restrictionId, $deliveryId = 0)
  141. {
  142. $class = static::getD2LClass();
  143. $class::resetMultipleForOwner($deliveryId);
  144. return parent::delete($restrictionId);
  145. }
  146. /**
  147. * @param Shipment $shipment
  148. * @param array $restrictionFields
  149. * @return array
  150. */
  151. public static function filterServicesArray(Shipment $shipment, array $restrictionFields)
  152. {
  153. if(empty($restrictionFields))
  154. return array();
  155. $shpLocCode = self::extractParams($shipment);
  156. //if location not defined in shipment
  157. if($shpLocCode === '')
  158. return array_keys($restrictionFields);
  159. $res = LocationTable::getList(array(
  160. 'filter' => array('=CODE' => $shpLocCode),
  161. 'select' => array('CODE', 'LEFT_MARGIN', 'RIGHT_MARGIN')
  162. ));
  163. //if location doesn't exists
  164. if(!$shpLocParams = $res->fetch())
  165. return array_keys($restrictionFields);
  166. $result = array();
  167. $srvLocCodesCompat = static::getLocationsCompat($restrictionFields, $shpLocParams['LEFT_MARGIN'], $shpLocParams['RIGHT_MARGIN']);
  168. foreach($srvLocCodesCompat as $locCode => $deliveries)
  169. foreach($deliveries as $deliveryId)
  170. if(!in_array($deliveryId, $result))
  171. $result[] = $deliveryId;
  172. return $result;
  173. }
  174. /**
  175. * @param array $restrictionFields
  176. * @param $leftMargin
  177. * @param $rightMargin
  178. * @return array
  179. */
  180. protected static function getLocationsCompat(array $restrictionFields, $leftMargin, $rightMargin)
  181. {
  182. $result = array();
  183. $groups = array();
  184. $class = static::getD2LClass();
  185. $res = $class::getList(array(
  186. 'filter' => array(
  187. '=DELIVERY_ID' => array_keys($restrictionFields),
  188. array(
  189. 'LOGIC' => 'OR',
  190. array(
  191. 'LOGIC' => 'AND',
  192. '=LOCATION_TYPE' => $class::DB_LOCATION_FLAG,
  193. '<=LOCATION.LEFT_MARGIN' => $leftMargin,
  194. '>=LOCATION.RIGHT_MARGIN' => $rightMargin
  195. ),
  196. array(
  197. 'LOGIC' => 'AND',
  198. '=LOCATION_TYPE' => $class::DB_GROUP_FLAG
  199. )
  200. )
  201. )
  202. ));
  203. while($d2l = $res->fetch())
  204. {
  205. if($d2l['LOCATION_TYPE'] == $class::DB_LOCATION_FLAG)
  206. {
  207. if(!is_array($result[$d2l['LOCATION_CODE']]))
  208. $result[$d2l['LOCATION_CODE']] = array();
  209. if(!in_array($d2l['DELIVERY_ID'] ,$result[$d2l['LOCATION_CODE']]))
  210. $result[$d2l['LOCATION_CODE']][] = $d2l['DELIVERY_ID'];
  211. }
  212. elseif($d2l['LOCATION_TYPE'] == $class::DB_GROUP_FLAG)
  213. {
  214. if(!is_array($groups[$d2l['LOCATION_CODE']]))
  215. $groups[$d2l['LOCATION_CODE']] = array();
  216. if(!in_array($d2l['DELIVERY_ID'] ,$groups[$d2l['LOCATION_CODE']]))
  217. $groups[$d2l['LOCATION_CODE']][] = $d2l['DELIVERY_ID'];
  218. }
  219. }
  220. //groups
  221. if(!empty($groups))
  222. {
  223. $res = GroupLocationTable::getList(array(
  224. 'filter' => array(
  225. '=GROUP.CODE' => array_keys($groups),
  226. '<=LOCATION.LEFT_MARGIN' => $leftMargin,
  227. '>=LOCATION.RIGHT_MARGIN' => $rightMargin
  228. ),
  229. 'select' => array(
  230. 'LOCATION_ID', 'LOCATION_GROUP_ID',
  231. 'LOCATION_CODE' => 'LOCATION.CODE',
  232. 'GROUP_CODE' => 'GROUP.CODE'
  233. )
  234. ));
  235. while($loc = $res->fetch())
  236. {
  237. if(!is_array($result[$loc['LOCATION_CODE']]))
  238. $result[$loc['LOCATION_CODE']] = array();
  239. foreach($groups[$loc['GROUP_CODE']] as $srvId)
  240. if(!in_array($srvId, $result[$loc['LOCATION_CODE']]))
  241. $result[$loc['LOCATION_CODE']][] = $srvId;
  242. }
  243. }
  244. return $result;
  245. }
  246. }