Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
reservedproductcleaner.php
1<?
2namespace Bitrix\Sale\Helpers;
3
11
13{
14 private const RECORD_LIMIT = 100;
15
16 protected static $moduleId = "sale";
17
18 public function execute(array &$result)
19 {
20 $processedRecords = 0;
21
22 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
24 $orderClass = $registry->getOrderClassName();
25
26 $days_ago = (int) Option::get("sale", "product_reserve_clear_period");
27
28 if ($days_ago > 0)
29 {
30 global $USER;
31
32 if (!is_object($USER))
33 {
34 $USER = new \CUser;
35 }
36
37 $date = new DateTime();
38 $parameters = [
39 'select' => [
40 'ORDER_ID' => 'ORDER.ID',
41 'ID',
42 'BASKET_ID'
43 ],
44 'filter' => [
45 '>QUANTITY' => 0,
46 '<=DATE_RESERVE_END' => $date,
47 '=ORDER.PAYED' => 'N',
48 '=ORDER.CANCELED' => 'N',
49 ],
50 'runtime' => [
51 new Main\Entity\ReferenceField(
52 'BASKET',
53 Sale\Internals\BasketTable::class,
54 [
55 '=this.BASKET_ID' => 'ref.ID',
56 ],
57 ['join_type' => 'inner']
58 ),
59 new Main\Entity\ReferenceField(
60 'ORDER',
61 Sale\Internals\OrderTable::class,
62 [
63 '=this.BASKET.ORDER_ID' => 'ref.ID',
64 ],
65 ['join_type' => 'inner']
66 ),
67 ],
68 'limit' => self::RECORD_LIMIT,
69 ];
70
71 $orderList = [];
72 $res = Sale\ReserveQuantityCollection::getList($parameters);
73 while ($data = $res->fetch())
74 {
75 if (!isset($orderList[$data['ORDER_ID']]))
76 {
77 $orderList[$data['ORDER_ID']] = [];
78 }
79
80 if (!isset($orderList[$data['ORDER_ID']][$data['BASKET_ID']]))
81 {
82 $orderList[$data['ORDER_ID']][$data['BASKET_ID']] = [];
83 }
84
85 $orderList[$data['ORDER_ID']][$data['BASKET_ID']][] = $data['ID'];
86 }
87
88 foreach ($orderList as $orderId => $basketItemIds)
89 {
90 $order = $orderClass::load($orderId);
91 if (!$order)
92 {
93 continue;
94 }
95
96 $basket = $order->getBasket();
97 foreach ($basketItemIds as $basketItemId => $reserveIds)
98 {
100 $basketItem = $basket->getItemById($basketItemId);
101 if (!$basketItem)
102 {
103 continue;
104 }
105
106 foreach ($reserveIds as $reserveId)
107 {
109 $reserveCollection = $basketItem->getReserveQuantityCollection();
110 if (!$reserveCollection)
111 {
112 continue;
113 }
114
115 $reserve = $reserveCollection->getItemById($reserveId);
116 if (!$reserve)
117 {
118 continue;
119 }
120
121 $reserve->delete();
122
123 $processedRecords++;
124 }
125 }
126
127 $r = $order->save();
128 if (!$r->isSuccess())
129 {
130 $errorText = (string)$order->getField('REASON_MARKED');
131 if ($errorText !== '')
132 {
133 $errorText .= "\n";
134 }
135
136 foreach($r->getErrorMessages() as $error)
137 {
138 if ((string)$error !== '')
139 {
140 $errorText .= $error."\n";
141 }
142 }
143
144 Sale\Internals\OrderTable::update($order->getId(), [
145 "MARKED" => "Y",
146 "REASON_MARKED" => $errorText
147 ]);
148 }
149 }
150
151 // crutch for #120087
152 if (!is_object($USER) || $USER->GetID() <= 0)
153 {
154 ORM\Entity::destroy(Sale\Internals\OrderTable::getEntity());
155 }
156 }
157
158 if ($processedRecords < self::RECORD_LIMIT)
159 {
161 }
162
164 }
165}
execute(array &$option)