Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
bylocation.php
1<?php
3
11
12Loc::loadMessages(__FILE__);
13
19class ByLocation extends Base
20{
21 public static $easeSort = 200;
22
23 public static function getClassTitle()
24 {
25 return Loc::getMessage("SALE_DLVR_RSTR_BY_LOCATION_NAME");
26 }
27
28 public static function getClassDescription()
29 {
30 return Loc::getMessage("SALE_DLVR_RSTR_BY_LOCATION_DESCRIPT");
31 }
32
33 protected static function getD2LClass()
34 {
35 return '\Bitrix\Sale\Delivery\DeliveryLocationTable';
36 }
37
45 public static function check($locationCode, array $restrictionParams, $deliveryId = 0)
46 {
47 if(intval($deliveryId) <= 0)
48 return true;
49
50 if($locationCode == '')
51 return false;
52
53 try
54 {
55 $class = static::getD2LClass();
56 return $class::checkConnectionExists(
57 intval($deliveryId),
58 $locationCode,
59 array(
60 'LOCATION_LINK_TYPE' => 'AUTO'
61 )
62 );
63 }
64 catch(\Bitrix\Sale\Location\Tree\NodeNotFoundException $e)
65 {
66 return false;
67 }
68 }
69
70 protected static function extractParams(Entity $entity)
71 {
72 if ($entity instanceof CollectableEntity)
73 {
75 $order = $entity->getCollection()->getOrder();
76 }
77 elseif ($entity instanceof Order)
78 {
80 $order = $entity;
81 }
82
83 if (!$order)
84 return '';
85
86
87 if(!$props = $order->getPropertyCollection())
88 return '';
89
90 if(!$locationProp = $props->getDeliveryLocation())
91 return '';
92
93 if(!$locationCode = $locationProp->getValue())
94 return '';
95
96 return $locationCode;
97 }
98
99 protected static function prepareParamsForSaving(array $params = array(), $deliveryId = 0)
100 {
101 $class = static::getD2LClass();
102 if($deliveryId > 0)
103 {
104 $arLocation = array();
105
106 if(!!\CSaleLocation::isLocationProEnabled())
107 {
108 if($params["LOCATION"][$class::DB_LOCATION_FLAG] <> '')
109 {
110 $LOCATION1 = explode(':', $params["LOCATION"][$class::DB_LOCATION_FLAG]);
111 }
112
113 if($params["LOCATION"][$class::DB_GROUP_FLAG] <> '')
114 {
115 $LOCATION2 = explode(':', $params["LOCATION"][$class::DB_GROUP_FLAG]);
116 }
117 }
118
119 if (isset($LOCATION1) && is_array($LOCATION1) && count($LOCATION1) > 0)
120 {
121 $arLocation[$class::DB_LOCATION_FLAG] = array();
122 $locationCount = count($LOCATION1);
123
124 for ($i = 0; $i<$locationCount; $i++)
125 if($LOCATION1[$i] <> '')
126 {
127 $arLocation[$class::DB_LOCATION_FLAG][] = $LOCATION1[$i];
128 }
129 }
130
131 if (isset($LOCATION2) && is_array($LOCATION2) && count($LOCATION2) > 0)
132 {
133 $arLocation[$class::DB_GROUP_FLAG] = array();
134 $locationCount = count($LOCATION2);
135
136 for ($i = 0; $i<$locationCount; $i++)
137 if($LOCATION2[$i] <> '')
138 {
139 $arLocation[$class::DB_GROUP_FLAG][] = $LOCATION2[$i];
140 }
141
142 }
143
144 $class::resetMultipleForOwner($deliveryId, $arLocation);
145 }
146
147 return array();
148 }
149
150 public static function getParamsStructure($deliveryId = 0)
151 {
152
153 $result = array(
154 "LOCATION" => array(
155 "TYPE" => "LOCATION_MULTI"
156 //'LABEL' => Loc::getMessage("SALE_DLVR_RSTR_BY_LOCATION_LOC"),
157 )
158 );
159
160 if($deliveryId > 0 )
161 $result["LOCATION"]["DELIVERY_ID"] = $deliveryId;
162
163 return $result;
164 }
165
166 public static function save(array $fields, $restrictionId = 0)
167 {
168 $fields["PARAMS"] = self::prepareParamsForSaving($fields["PARAMS"], $fields["SERVICE_ID"]);
169 return parent::save($fields, $restrictionId);
170 }
171
172 public static function delete($restrictionId, $deliveryId = 0)
173 {
174 $class = static::getD2LClass();
175 $class::resetMultipleForOwner($deliveryId);
176 return parent::delete($restrictionId);
177 }
178
184 public static function filterServicesArray(Shipment $shipment, array $restrictionFields)
185 {
186 if(empty($restrictionFields))
187 return array();
188
189 $shpLocCode = self::extractParams($shipment);
190
191 //if location not defined in shipment
192 if($shpLocCode === '')
193 return array_keys($restrictionFields);
194
195 $res = LocationTable::getList(array(
196 'filter' => array('=CODE' => $shpLocCode),
197 'select' => array('CODE', 'LEFT_MARGIN', 'RIGHT_MARGIN')
198 ));
199
200 //if location doesn't exists
201 if(!$shpLocParams = $res->fetch())
202 return array_keys($restrictionFields);
203
204 $result = array();
205 $srvLocCodesCompat = static::getLocationsCompat($restrictionFields, $shpLocParams['LEFT_MARGIN'], $shpLocParams['RIGHT_MARGIN']);
206
207 foreach($srvLocCodesCompat as $locCode => $deliveries)
208 foreach($deliveries as $deliveryId)
209 if(!in_array($deliveryId, $result))
210 $result[] = $deliveryId;
211
212 return $result;
213 }
214
221 protected static function getLocationsCompat(array $restrictionFields, $leftMargin, $rightMargin)
222 {
223 $result = array();
224 $groups = array();
225 $class = static::getD2LClass();
226
227 $res = $class::getList(array(
228 'filter' => array(
229 '=DELIVERY_ID' => array_keys($restrictionFields),
230 array(
231 'LOGIC' => 'OR',
232 array(
233 'LOGIC' => 'AND',
234 '=LOCATION_TYPE' => $class::DB_LOCATION_FLAG,
235 '<=LOCATION.LEFT_MARGIN' => $leftMargin,
236 '>=LOCATION.RIGHT_MARGIN' => $rightMargin
237 ),
238 array(
239 'LOGIC' => 'AND',
240 '=LOCATION_TYPE' => $class::DB_GROUP_FLAG
241 )
242 )
243 )
244 ));
245
246 while($d2l = $res->fetch())
247 {
248 if($d2l['LOCATION_TYPE'] == $class::DB_LOCATION_FLAG)
249 {
250 if (!isset($result[$d2l['LOCATION_CODE']]))
251 {
252 $result[$d2l['LOCATION_CODE']] = [];
253 }
254
255 if(!in_array($d2l['DELIVERY_ID'] ,$result[$d2l['LOCATION_CODE']]))
256 $result[$d2l['LOCATION_CODE']][] = $d2l['DELIVERY_ID'];
257 }
258 elseif($d2l['LOCATION_TYPE'] == $class::DB_GROUP_FLAG)
259 {
260 if (!isset($groups[$d2l['LOCATION_CODE']]))
261 {
262 $groups[$d2l['LOCATION_CODE']] = [];
263 }
264
265 if(!in_array($d2l['DELIVERY_ID'] ,$groups[$d2l['LOCATION_CODE']]))
266 $groups[$d2l['LOCATION_CODE']][] = $d2l['DELIVERY_ID'];
267 }
268 }
269
270 //groups
271 if(!empty($groups))
272 {
273 $res = GroupLocationTable::getList(array(
274 'filter' => array(
275 '=GROUP.CODE' => array_keys($groups),
276 '<=LOCATION.LEFT_MARGIN' => $leftMargin,
277 '>=LOCATION.RIGHT_MARGIN' => $rightMargin
278 ),
279 'select' => array(
280 'LOCATION_ID', 'LOCATION_GROUP_ID',
281 'LOCATION_CODE' => 'LOCATION.CODE',
282 'GROUP_CODE' => 'GROUP.CODE'
283 )
284 ));
285
286 while($loc = $res->fetch())
287 {
288 if(!is_array($result[$loc['LOCATION_CODE']]))
289 $result[$loc['LOCATION_CODE']] = array();
290
291 foreach($groups[$loc['GROUP_CODE']] as $srvId)
292 if(!in_array($srvId, $result[$loc['LOCATION_CODE']]))
293 $result[$loc['LOCATION_CODE']][] = $srvId;
294 }
295 }
296
297 return $result;
298 }
299}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static filterServicesArray(Shipment $shipment, array $restrictionFields)
static check($locationCode, array $restrictionParams, $deliveryId=0)
static save(array $fields, $restrictionId=0)
static getLocationsCompat(array $restrictionFields, $leftMargin, $rightMargin)
static prepareParamsForSaving(array $params=array(), $deliveryId=0)