17 public function loadFromDB(array $parameters)
23 $orderClass = $registry->getOrderClassName();
25 $ordersList = $orderClass::loadByFilter($parameters);
27 foreach ($ordersList as $order)
30 $this->addItem($newItem);
35 $result->setData(array(
"count" =>
null));
40 $this->fillItemsData();
46 $this->collection[$item->
getId()] = $item;
49 private function fillItemsData()
51 $idList = $this->getItemIds();
55 $idListChunks = array_chunk($idList, 999);
56 foreach ($idListChunks as $idOrdersList)
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);
64 foreach ($idOrdersList as $orderId)
67 $item = $this->getItemById($orderId);
73 $properties = $sortedOrderProperties[$orderId] ? $sortedOrderProperties[$orderId] : [];
75 $payment = $sortedPayments[$orderId] ? $sortedPayments[$orderId] : [];
77 $shipment = $sortedShipments[$orderId] ? $sortedShipments[$orderId] : [];
79 $discount = $sortedDataDiscount[$orderId] ? $sortedDataDiscount[$orderId] : [];
81 $basketItems = $sortedBasketItems[$orderId] ? $sortedBasketItems[$orderId] : [];
87 private function getItemById($id)
89 return $this->collection[$id];
92 private function getItemIds()
94 return array_keys($this->collection);
104 private function collectOrderProperties($orderIds)
106 $sortedOrderProperties = [];
107 $orderProperties = Internals\OrderPropsValueTable::getList(
109 "order" => array(
"ORDER_ID"),
110 "filter" => array(
"=ORDER_ID" => $orderIds)
114 while ($property = $orderProperties->fetch())
116 $sortedOrderProperties[$property[
'ORDER_ID']][$property[
'ID']] = $property;
118 return $sortedOrderProperties;
129 private function collectPayments(array $orderIds)
131 $sortedPayments = [];
132 $payments = Sale\Payment::getList(
134 "order" => array(
"ORDER_ID"),
135 "filter" => array(
"=ORDER_ID" => $orderIds)
139 while ($payment = $payments->fetch())
141 $sortedPayments[$payment[
'ORDER_ID']][$payment[
'ID']] = $payment;
144 return $sortedPayments;
155 private function collectShipments(array $orderIds)
157 $shipmentItemsList = [];
158 $sortedShipments = [];
160 $shipments = Sale\Shipment::getList(
162 "order" => array(
"ORDER_ID"),
163 "filter" => array(
"=ORDER_ID" => $orderIds,
"SYSTEM" =>
'N')
167 while ($shipment = $shipments->fetch())
169 $shipmentItemsList[$shipment[
'ID']] = $shipment;
172 if (!empty($shipmentItemsList))
174 $shipmentsItems = Sale\ShipmentItem::getList(
176 "order" => array(
"ORDER_DELIVERY_ID"),
177 "filter" => array(
"ORDER_DELIVERY_ID" => array_keys($shipmentItemsList))
181 while ($shipmentsItem = $shipmentsItems->fetch())
183 $shipmentItemsList[$shipmentsItem[
'ORDER_DELIVERY_ID']][
"SHIPMENT_ITEM"][] = $shipmentsItem;
187 foreach ($shipmentItemsList as $item)
189 $sortedShipments[$item[
'ORDER_ID']][$item[
'ID']] = $item;
192 return $sortedShipments;
203 private function collectBaskets(array $orderIds)
205 $sortedBasketItems = [];
206 $basketItemsList = [];
211 $basketClassName = $registry->getBasketClassName();
213 $basketItems = $basketClassName::getList(
215 "order" => array(
"ORDER_ID"),
216 "filter" => array(
"=ORDER_ID" => $orderIds)
220 while ($element = $basketItems->fetch())
222 $basketItemsList[$element[
'ID']] = $element;
225 if (!empty($basketItemsList))
227 $basketProperties = Internals\BasketPropertyTable::getList(
229 "filter" => array(
"BASKET_ID" => array_keys($basketItemsList))
233 while ($property = $basketProperties->fetch())
235 $basketItemsList[$property[
"BASKET_ID"]][
'PROPERTY_ITEMS'][] = $property;
238 $basketProperties = Sale\ShipmentItemStore::getList(
240 "filter" => array(
"=BASKET_ID" => array_keys($basketItemsList))
244 while ($property = $basketProperties->fetch())
246 $basketItemsList[$property[
"BASKET_ID"]][
'SHIPMENT_BARCODE_ITEMS'][$property[
'ORDER_DELIVERY_BASKET_ID']] = $property;
250 foreach ($basketItemsList as $basketItem)
252 $sortedBasketItems[$basketItem[
'ORDER_ID']][$basketItem[
'ID']] = $basketItem;
255 return $sortedBasketItems;
265 private function collectDiscountData(array $orderIds)
267 $sortedDataDiscount = [];
270 $couponList = $this->collectCoupons($orderIds);
271 $sortedDiscountRules = $this->collectRules($orderIds);
273 $dataIterator = Internals\OrderDiscountDataTable::getList(
275 'select' => array(
'*'),
276 'filter' => array(
'=ORDER_ID' => $orderIds)
280 while ($dataDiscount = $dataIterator->fetch())
282 $discountList[$dataDiscount[
'ORDER_ID']][$dataDiscount[
'ID']] = $dataDiscount;
285 foreach ($orderIds as $orderId)
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] : []
294 return $sortedDataDiscount;
304 private function collectCoupons($orderIds)
308 $couponsIterator = Internals\OrderCouponsTable::getList(array(
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',
316 'filter' => array(
'=ORDER_ID' => $orderIds),
317 'order' => array(
'ID' =>
'ASC')
320 while ($coupon = $couponsIterator->fetch())
322 foreach ($coupon[
'DISCOUNT_DESCR'] as $discountDescriptionArray)
324 foreach ($discountDescriptionArray as $descriptionList)
326 if (is_array($descriptionList))
328 $coupon[
'DISCOUNT_SIZE'] = Sale\Discount\Formatter::formatRow($descriptionList);
333 $couponList[$coupon[
'ORDER_ID']][$coupon[
'COUPON']] = $coupon;
346 private function collectRules($orderIds)
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']
358 while ($rule = $ruleIterator->fetch())
360 $discountList[] = $rule[
'ORDER_DISCOUNT_ID'];
361 $rulesList[$rule[
'ID']] = $rule;
364 $discountList = array_unique($discountList);
366 if (!empty($discountList))
368 $discountIterator = Internals\OrderDiscountTable::getList(array(
369 'filter' => array(
'@ID' => $discountList),
372 while ($discount = $discountIterator->fetch())
374 $discountList[$discount[
'ID']] = $discount;
378 foreach ($rulesList as $id => $rule)
380 $rule[
"DISCOUNT_DATA"] = $discountList[$rule[
'ORDER_DISCOUNT_ID']] ? $discountList[$rule[
'ORDER_DISCOUNT_ID']] : array();
381 $sortedRules[$rule[
'ORDER_ID']][$id] = $rule;
394 if (!isset($this->collection[$index]))
397 unset($this->collection[$index]);