Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
basketreservationhistoryservice.php
1<?php
2
4
15use Exception;
16
21{
22 public function __construct()
23 {
24 Loader::includeModule('catalog');
25 }
26
33 private function roundQuantity(float $quantity): float
34 {
35 $precision = 6;
36 return round($quantity, $precision, PHP_ROUND_HALF_DOWN);
37 }
38
45 public function getQuantityByReservation(int $reservationId): float
46 {
47 $total = 0.0;
48
50 'select' => [
51 'QUANTITY',
52 ],
53 'filter' => [
54 '=RESERVATION_ID' => $reservationId,
55 ],
56 ]);
57 foreach ($rows as $row)
58 {
59 $total += (float)$row['QUANTITY'];
60 }
61
62 return $total;
63 }
64
74 public function getAvailableCountForBasketItems(array $basketItemFilter): array
75 {
76 $basketItems =
77 BasketTable::getList([
78 'select' => [
79 'ID',
80 'PRODUCT_ID',
81 ],
82 'filter' => $basketItemFilter,
83 ])
84 ->fetchAll()
85 ;
86
87 $basket2productIds = array_column($basketItems, 'PRODUCT_ID', 'ID');
88 if (empty($basket2productIds))
89 {
90 return [];
91 }
92
93 $calculator = new AvailableQuantityCalculator();
94
95 $rows = StoreProductTable::getList([
96 'select' => [
97 'PRODUCT_ID',
98 'STORE_ID',
99 'AMOUNT',
100 ],
101 'filter' => [
102 '=PRODUCT_ID' => $basket2productIds,
103 ],
104 ]);
105 foreach ($rows as $row)
106 {
107 $calculator->setStoreQuantity($row['STORE_ID'], $row['PRODUCT_ID'], $row['AMOUNT']);
108 }
109
110 $reservationsRows = BasketReservationHistoryTable::getList([
111 'select' => [
112 'RESERVATION_ID',
113 'QUANTITY',
114 'STORE_ID' => 'RESERVATION.STORE_ID',
115 'BASKET_ID' => 'RESERVATION.BASKET_ID',
116 'PRODUCT_ID' => 'RESERVATION.BASKET.PRODUCT_ID',
117 ],
118 'filter' => [
119 '!RESERVATION.STORE_ID' => null,
120 '=RESERVATION.BASKET.PRODUCT_ID' => $basket2productIds,
121 ],
122 'order' => [
123 'DATE_RESERVE' => 'ASC',
124 ],
125 ]);
126
127 foreach ($reservationsRows as $row)
128 {
129 $calculator->addReservationHistory(
130 $row['STORE_ID'],
131 $row['PRODUCT_ID'],
132 $row['BASKET_ID'],
133 $row['QUANTITY']
134 );
135 }
136
137 return $calculator->getQuantityForBatch($basket2productIds);
138 }
139
148 public function getAvailableCountForOrder(int $orderId): array
149 {
150 return $this->getAvailableCountForBasketItems([
151 '=ORDER_ID' => $orderId,
152 ]);
153 }
154
178 public function getAvailableCountForBasketItem(int $basketId, int $storeId): float
179 {
180 $basketItem = BasketTable::getRow([
181 'select' => [
182 'PRODUCT_ID',
183 ],
184 'filter' => [
185 '=ID' => $basketId,
186 ],
187 ]);
188 if (!$basketItem || !$basketItem['PRODUCT_ID'])
189 {
190 return 0.0;
191 }
192
193 $productId = (int)$basketItem['PRODUCT_ID'];
194 $storeQuantityRow = StoreProductTable::getRow([
195 'select' => [
196 'AMOUNT',
197 ],
198 'filter' => [
199 '=STORE_ID' => $storeId,
200 '=PRODUCT_ID' => $productId,
201 ],
202 ]);
203 if (!$storeQuantityRow)
204 {
205 return 0.0;
206 }
207
208 $calculator = new AvailableQuantityCalculator();
209 $calculator->setStoreQuantity($storeId, $productId, $storeQuantityRow['AMOUNT']);
210
211 $reservationsRows = BasketReservationHistoryTable::getList([
212 'select' => [
213 'RESERVATION_ID',
214 'QUANTITY',
215 'BASKET_ID' => 'RESERVATION.BASKET_ID',
216 ],
217 'filter' => [
218 '=RESERVATION.STORE_ID' => $storeId,
219 '=RESERVATION.BASKET.PRODUCT_ID' => $productId,
220 ],
221 'order' => [
222 'DATE_RESERVE' => 'ASC',
223 ],
224 ]);
225 foreach ($reservationsRows as $row)
226 {
227 $calculator->addReservationHistory(
228 $storeId,
229 $productId,
230 $row['BASKET_ID'],
231 $row['QUANTITY']
232 );
233 }
234
235 return $calculator->getQuantityForItem($productId, $basketId, $storeId);
236 }
237
244 public function add(array $fields): Result
245 {
247 }
248
256 private function addQuantity(int $reservationId, float $quantity): Result
257 {
258 return $this->add([
259 'RESERVATION_ID' => $reservationId,
260 'DATE_RESERVE' => new DateTime(),
261 'QUANTITY' => $quantity,
262 ]);
263 }
264
271 public function addByReservation(int $reservationId): Result
272 {
273 $reservation = BasketReservationTable::getRowById($reservationId);
274 if (!$reservation)
275 {
276 throw new Exception('Reservation not found');
277 }
278
279 return $this->addQuantity($reservationId, (float)$reservation['QUANTITY']);
280 }
281
289 public function update(int $id, array $fields): Result
290 {
291 return BasketReservationHistoryTable::update($id, $fields);
292 }
293
300 public function updateByReservation(int $reservationId): Result
301 {
302 $reservation = BasketReservationTable::getRowById($reservationId);
303 if (!$reservation)
304 {
305 throw new Exception('Reservation not found');
306 }
307
308 $reservationQuantity = $this->roundQuantity($reservation['QUANTITY']);
309 $historyQuantity = $this->roundQuantity($this->getQuantityByReservation($reservationId));
310
311 if ($reservationQuantity !== $historyQuantity)
312 {
313 return $this->addQuantity($reservationId, $reservationQuantity - $historyQuantity);
314 }
315
316 return new Result();
317 }
318
325 public function delete(int $id): Result
326 {
328 }
329
338 public function deleteByReservation(int $reservationId): Result
339 {
340 $result = new DeleteResult();
341
343 'select' => [
344 'ID',
345 ],
346 'filter' => [
347 '=RESERVATION_ID' => $reservationId,
348 ],
349 ]);
350 foreach ($rows as $row)
351 {
352 $deleteResult = BasketReservationHistoryTable::delete($row['ID']);
353 foreach ($deleteResult->getErrors() as $err)
354 {
355 $result->addError($err);
356 }
357 }
358
359 return $result;
360 }
361}
static getList(array $parameters=array())
static update($primary, array $data)