Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
orderarchivecollection.php
1<?php
3
8
9Loc::loadMessages(__FILE__);
10
16{
17 public function loadFromDB(array $parameters)
18 {
19 $result = new Sale\Result();
20
21 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
23 $orderClass = $registry->getOrderClassName();
24
25 $ordersList = $orderClass::loadByFilter($parameters);
26
27 foreach ($ordersList as $order)
28 {
29 $newItem = new OrderArchiveItem($order);
30 $this->addItem($newItem);
31 }
32
33 if ($this->isEmpty())
34 {
35 $result->setData(array("count" => null));
36 $result->addWarning(new Main\Error(Loc::getMessage("ARCHIVE_ORDER_NOT_FOUND")));
37 return $result;
38 }
39
40 $this->fillItemsData();
41 return $result;
42 }
43
44 private function addItem(OrderArchiveItem $item)
45 {
46 $this->collection[$item->getId()] = $item;
47 }
48
49 private function fillItemsData()
50 {
51 $idList = $this->getItemIds();
52 if (empty($idList))
53 return;
54
55 $idListChunks = array_chunk($idList, 999);
56 foreach ($idListChunks as $idOrdersList)
57 {
58 $sortedOrderProperties = $this->collectOrderProperties($idOrdersList);
59 $sortedPayments = $this->collectPayments($idOrdersList);
60 $sortedShipments = $this->collectShipments($idOrdersList);
61 $sortedBasketItems = $this->collectBaskets($idOrdersList);
62 $sortedDataDiscount = $this->collectDiscountData($idOrdersList);
63
64 foreach ($idOrdersList as $orderId)
65 {
67 $item = $this->getItemById($orderId);
68 if (empty($item))
69 {
70 continue;
71 }
72
73 $properties = $sortedOrderProperties[$orderId] ? $sortedOrderProperties[$orderId] : [];
74 $item->addOrderDataField('PROPERTIES', $properties);
75 $payment = $sortedPayments[$orderId] ? $sortedPayments[$orderId] : [];
76 $item->addOrderDataField('PAYMENT', $payment);
77 $shipment = $sortedShipments[$orderId] ? $sortedShipments[$orderId] : [];
78 $item->addOrderDataField('SHIPMENT', $shipment);
79 $discount = $sortedDataDiscount[$orderId] ? $sortedDataDiscount[$orderId] : [];
80 $item->addOrderDataField('DISCOUNT', $discount);
81 $basketItems = $sortedBasketItems[$orderId] ? $sortedBasketItems[$orderId] : [];
82 $item->addBasketDataFields($basketItems);
83 }
84 }
85 }
86
87 private function getItemById($id)
88 {
89 return $this->collection[$id];
90 }
91
92 private function getItemIds()
93 {
94 return array_keys($this->collection);
95 }
96
104 private function collectOrderProperties($orderIds)
105 {
106 $sortedOrderProperties = [];
107 $orderProperties = Internals\OrderPropsValueTable::getList(
108 array(
109 "order" => array("ORDER_ID"),
110 "filter" => array("=ORDER_ID" => $orderIds)
111 )
112 );
113
114 while ($property = $orderProperties->fetch())
115 {
116 $sortedOrderProperties[$property['ORDER_ID']][$property['ID']] = $property;
117 }
118 return $sortedOrderProperties;
119 }
120
129 private function collectPayments(array $orderIds)
130 {
131 $sortedPayments = [];
132 $payments = Sale\Payment::getList(
133 array(
134 "order" => array("ORDER_ID"),
135 "filter" => array("=ORDER_ID" => $orderIds)
136 )
137 );
138
139 while ($payment = $payments->fetch())
140 {
141 $sortedPayments[$payment['ORDER_ID']][$payment['ID']] = $payment;
142 }
143
144 return $sortedPayments;
145 }
146
155 private function collectShipments(array $orderIds)
156 {
157 $shipmentItemsList = [];
158 $sortedShipments = [];
159
160 $shipments = Sale\Shipment::getList(
161 array(
162 "order" => array("ORDER_ID"),
163 "filter" => array("=ORDER_ID" => $orderIds, "SYSTEM" => 'N')
164 )
165 );
166
167 while ($shipment = $shipments->fetch())
168 {
169 $shipmentItemsList[$shipment['ID']] = $shipment;
170 }
171
172 if (!empty($shipmentItemsList))
173 {
174 $shipmentsItems = Sale\ShipmentItem::getList(
175 array(
176 "order" => array("ORDER_DELIVERY_ID"),
177 "filter" => array("ORDER_DELIVERY_ID" => array_keys($shipmentItemsList))
178 )
179 );
180
181 while ($shipmentsItem = $shipmentsItems->fetch())
182 {
183 $shipmentItemsList[$shipmentsItem['ORDER_DELIVERY_ID']]["SHIPMENT_ITEM"][] = $shipmentsItem;
184 }
185 }
186
187 foreach ($shipmentItemsList as $item)
188 {
189 $sortedShipments[$item['ORDER_ID']][$item['ID']] = $item;
190 }
191
192 return $sortedShipments;
193 }
194
203 private function collectBaskets(array $orderIds)
204 {
205 $sortedBasketItems = [];
206 $basketItemsList = [];
207
208 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
209
211 $basketClassName = $registry->getBasketClassName();
212
213 $basketItems = $basketClassName::getList(
214 array(
215 "order" => array("ORDER_ID"),
216 "filter" => array("=ORDER_ID" => $orderIds)
217 )
218 );
219
220 while ($element = $basketItems->fetch())
221 {
222 $basketItemsList[$element['ID']] = $element;
223 }
224
225 if (!empty($basketItemsList))
226 {
227 $basketProperties = Internals\BasketPropertyTable::getList(
228 array(
229 "filter" => array("BASKET_ID" => array_keys($basketItemsList))
230 )
231 );
232
233 while ($property = $basketProperties->fetch())
234 {
235 $basketItemsList[$property["BASKET_ID"]]['PROPERTY_ITEMS'][] = $property;
236 }
237
238 $basketProperties = Sale\ShipmentItemStore::getList(
239 array(
240 "filter" => array("=BASKET_ID" => array_keys($basketItemsList))
241 )
242 );
243
244 while ($property = $basketProperties->fetch())
245 {
246 $basketItemsList[$property["BASKET_ID"]]['SHIPMENT_BARCODE_ITEMS'][$property['ORDER_DELIVERY_BASKET_ID']] = $property;
247 }
248 }
249
250 foreach ($basketItemsList as $basketItem)
251 {
252 $sortedBasketItems[$basketItem['ORDER_ID']][$basketItem['ID']] = $basketItem;
253 }
254
255 return $sortedBasketItems;
256 }
257
265 private function collectDiscountData(array $orderIds)
266 {
267 $sortedDataDiscount = [];
268 $discountList = [];
269
270 $couponList = $this->collectCoupons($orderIds);
271 $sortedDiscountRules = $this->collectRules($orderIds);
272
273 $dataIterator = Internals\OrderDiscountDataTable::getList(
274 array(
275 'select' => array('*'),
276 'filter' => array('=ORDER_ID' => $orderIds)
277 )
278 );
279
280 while ($dataDiscount = $dataIterator->fetch())
281 {
282 $discountList[$dataDiscount['ORDER_ID']][$dataDiscount['ID']] = $dataDiscount;
283 }
284
285 foreach ($orderIds as $orderId)
286 {
287 $sortedDataDiscount[$orderId] = [
288 'ORDER_DATA' => isset($discountList[$orderId]) ? $discountList[$orderId] : [],
289 'COUPON_LIST' => isset($couponList[$orderId]) ? $couponList[$orderId] : [],
290 'RULES_DATA' => isset($sortedDiscountRules[$orderId]) ? $sortedDiscountRules[$orderId] : []
291 ];
292 }
293
294 return $sortedDataDiscount;
295 }
296
304 private function collectCoupons($orderIds)
305 {
306 $couponList = [];
307
308 $couponsIterator = Internals\OrderCouponsTable::getList(array(
309 'select' => array(
310 '*',
311 'MODULE_ID' => 'ORDER_DISCOUNT.MODULE_ID',
312 'DISCOUNT_ID' => 'ORDER_DISCOUNT.DISCOUNT_ID',
313 'DISCOUNT_NAME' => 'ORDER_DISCOUNT.NAME',
314 'DISCOUNT_DESCR' => 'ORDER_DISCOUNT.ACTIONS_DESCR',
315 ),
316 'filter' => array('=ORDER_ID' => $orderIds),
317 'order' => array('ID' => 'ASC')
318 ));
319
320 while ($coupon = $couponsIterator->fetch())
321 {
322 foreach ($coupon['DISCOUNT_DESCR'] as $discountDescriptionArray)
323 {
324 foreach ($discountDescriptionArray as $descriptionList)
325 {
326 if (is_array($descriptionList))
327 {
328 $coupon['DISCOUNT_SIZE'] = Sale\Discount\Formatter::formatRow($descriptionList);
329 }
330 }
331 }
332
333 $couponList[$coupon['ORDER_ID']][$coupon['COUPON']] = $coupon;
334 }
335
336 return $couponList;
337 }
338
346 private function collectRules($orderIds)
347 {
348 $sortedRules =
349 $discountList =
350 $rulesList = [];
351
352 $ruleIterator = Internals\OrderRulesTable::getList(array(
353 'filter' => array('=ORDER_ID' => $orderIds),
354 'order' => array('ID' => 'ASC'),
355 'select' => ['*', 'RULE_DESCR' => 'DESCR.DESCR', 'RULE_DESCR_ID' => 'DESCR.ID']
356 ));
357
358 while ($rule = $ruleIterator->fetch())
359 {
360 $discountList[] = $rule['ORDER_DISCOUNT_ID'];
361 $rulesList[$rule['ID']] = $rule;
362 }
363
364 $discountList = array_unique($discountList);
365
366 if (!empty($discountList))
367 {
368 $discountIterator = Internals\OrderDiscountTable::getList(array(
369 'filter' => array('@ID' => $discountList),
370 ));
371
372 while ($discount = $discountIterator->fetch())
373 {
374 $discountList[$discount['ID']] = $discount;
375 }
376 }
377
378 foreach ($rulesList as $id => $rule)
379 {
380 $rule["DISCOUNT_DATA"] = $discountList[$rule['ORDER_DISCOUNT_ID']] ? $discountList[$rule['ORDER_DISCOUNT_ID']] : array();
381 $sortedRules[$rule['ORDER_ID']][$id] = $rule;
382 }
383
384 return $sortedRules;
385 }
386
392 public function deleteItem($index)
393 {
394 if (!isset($this->collection[$index]))
395 throw new Main\ArgumentOutOfRangeException("Collection item index wrong");
396
397 unset($this->collection[$index]);
398 }
399}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29