Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
delivery.php
1<?php
3
9
10Loc::loadMessages(__FILE__);
11
12class Delivery extends Base\Restriction
13{
14 public static $easeSort = 200;
15
19 public static function getClassTitle()
20 {
21 return Loc::getMessage("SALE_COMPANY_RULES_BY_DLV_TITLE");
22 }
23
27 public static function getClassDescription()
28 {
29 return Loc::getMessage("SALE_COMPANY_RULES_BY_DLV_DESC");
30 }
31
38 public static function check($params, array $restrictionParams, $serviceId = 0)
39 {
40 if ((int)$serviceId <= 0)
41 return true;
42
43 if (!$params)
44 return true;
45
46 $deliveryIds = self::getDeliveryByCompanyId($serviceId);
47
48 if (empty($deliveryIds))
49 return true;
50
51 $diff = array_diff($params, $deliveryIds);
52
53 return empty($diff);
54 }
55
61 protected static function extractParams(Sale\Internals\Entity $entity)
62 {
63 $result = array();
64
66 $shipmentCollection = null;
67
68 if ($entity instanceof Sale\Shipment)
69 {
70 $shipmentCollection = $entity->getCollection();
71 }
72 elseif ($entity instanceof Sale\Payment)
73 {
75 $paymentCollection = $entity->getCollection();
76 if ($paymentCollection)
77 {
79 $order = $paymentCollection->getOrder();
80 if ($order)
81 $shipmentCollection = $order->getShipmentCollection();
82 }
83 }
84 elseif ($entity instanceOf Sale\Order)
85 {
86 $shipmentCollection = $entity->getShipmentCollection();
87 }
88
89 if ($shipmentCollection !== null)
90 {
92 foreach ($shipmentCollection as $shipment)
93 {
94 if ($shipment->isSystem())
95 {
96 continue;
97 }
98
99 if ($deliveryId = $shipment->getDeliveryId())
100 {
101 $result[] = $deliveryId;
102 }
103 }
104 }
105
106 return $result;
107 }
108
112 protected static function getDeliveryList()
113 {
114 $result = array();
115
116 $serviceList = array();
117 $dbRes = Sale\Delivery\Services\Table::getList(array('select' => array('ID', 'NAME', 'PARENT_ID')));
118 while ($service = $dbRes->fetch())
119 $serviceList[$service['ID']] = $service;
120
121 foreach ($serviceList as $service)
122 {
123 if ((int)$service['PARENT_ID'] > 0)
124 $name = $serviceList[$service['PARENT_ID']]['NAME'].': '.$service['NAME'].' ['.$service['ID'].']';
125 else
126 $name = $service['NAME'].' ['.$service['ID'].']';
127
128 $result[$service['ID']] = $name;
129 }
130
131 return $result;
132 }
133
138 public static function getParamsStructure($entityId = 0)
139 {
140 $result = array(
141 "DELIVERY" => array(
142 "TYPE" => "ENUM",
143 'MULTIPLE' => 'Y',
144 "LABEL" => Loc::getMessage("SALE_COMPANY_RULES_BY_DLV"),
145 "OPTIONS" => self::getDeliveryList()
146 )
147 );
148
149 return $result;
150 }
151
157 protected static function getDeliveryByCompanyId($companyId = 0)
158 {
159 $result = array();
160 if ($companyId == 0)
161 return $result;
162
163 $dbRes = CompanyServiceTable::getList(
164 array(
165 'select' => array('SERVICE_ID'),
166 'filter' => array(
167 'COMPANY_ID' => $companyId,
168 'SERVICE_TYPE' => Sale\Services\Company\Restrictions\Manager::SERVICE_TYPE_SHIPMENT)
169 )
170 );
171
172 while ($data = $dbRes->fetch())
173 $result[] = $data['SERVICE_ID'];
174
175 return $result;
176 }
177
183 public static function save(array $fields, $restrictionId = 0)
184 {
185 $serviceIds = $fields["PARAMS"];
186 $fields["PARAMS"] = array();
187
188 if ($restrictionId > 0)
189 {
190 $dbRes = CompanyServiceTable::getList(
191 array(
192 'select' => array('SERVICE_ID'),
193 'filter' => array(
194 'SERVICE_TYPE' => Sale\Services\Company\Restrictions\Manager::SERVICE_TYPE_SHIPMENT,
195 'COMPANY_ID' => $fields['SERVICE_ID']
196 )
197 )
198 );
199
200 while($data = $dbRes->fetch())
201 {
202 $key = array_search($data['SERVICE_ID'], $serviceIds['DELIVERY']);
203 if (!$key)
204 {
205 CompanyServiceTable::delete(array('COMPANY_ID' => $fields['SERVICE_ID'], 'SERVICE_ID' => $data['SERVICE_ID'], 'SERVICE_TYPE' => Sale\Services\Company\Restrictions\Manager::SERVICE_TYPE_SHIPMENT));
206 }
207 else
208 {
209 unset($serviceIds['DELIVERY'][$key]);
210 }
211 }
212 }
213
214 $result = parent::save($fields, $restrictionId);
215
216 $addFields = array('COMPANY_ID' => $fields['SERVICE_ID'], 'SERVICE_TYPE' => Sale\Services\Company\Restrictions\Manager::SERVICE_TYPE_SHIPMENT);
217 foreach ($serviceIds['DELIVERY'] as $id)
218 {
219 $addFields['SERVICE_ID'] = $id;
220 CompanyServiceTable::add($addFields);
221 }
222
223 return $result;
224 }
225
231 public static function prepareParamsValues(array $paramsValues, $entityId = 0)
232 {
233 return array("DELIVERY" => self::getDeliveryByCompanyId($entityId));
234 }
235
242 public static function delete($restrictionId, $entityId = 0)
243 {
244 $dbRes = CompanyServiceTable::getList(
245 array(
246 'select' => array('SERVICE_ID'),
247 'filter' => array(
248 'SERVICE_TYPE' => Sale\Services\Company\Restrictions\Manager::SERVICE_TYPE_SHIPMENT,
249 'COMPANY_ID' => $entityId
250 )
251 )
252 );
253
254 while ($data = $dbRes->fetch())
255 {
256 CompanyServiceTable::delete(array('COMPANY_ID' => $entityId, 'SERVICE_ID' => $data['SERVICE_ID'], 'SERVICE_TYPE' => Sale\Services\Company\Restrictions\Manager::SERVICE_TYPE_SHIPMENT));
257 }
258
259 return parent::delete($restrictionId);
260 }
261}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static prepareParamsValues(array $paramsValues, $entityId=0)
Definition delivery.php:231
static check($params, array $restrictionParams, $serviceId=0)
Definition delivery.php:38
static save(array $fields, $restrictionId=0)
Definition delivery.php:183