Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
saleproviderbase.php
1<?php
2
3namespace Bitrix\Sale;
4
6
11abstract class SaleProviderBase
12{
13 public const EMPTY_STORE_ID = 0;
14
15 public const SUMMMARY_PRODUCT_LIST = 'PRODUCT_DATA_LIST';
16
17 public const FLAT_PRICE_LIST = 'PRICE_LIST';
18 public const FLAT_AVAILABLE_QUANTITY_LIST = 'AVAILABLE_QUANTITY_LIST';
19 public const FLAT_RESERVED_QUANTITY_LIST = 'RESERVED_QUANTITY_LIST';
20 public const FLAT_QUANTITY_LIST = 'QUANTITY_LIST';
21
22 public const STORE_AVAILABLE_QUANTITY_LIST = 'AVAILABLE_QUANTITY_LIST_BY_STORE';
23 public const STORE_RESERVED_QUANTITY_LIST = 'RESERVED_QUANTITY_LIST_BY_STORE';
24 public const STORE_QUANTITY_LIST = 'QUANTITY_LIST_BY_STORE';
25
26
27 protected $context = [];
28
34 public function __construct(array $context = array())
35 {
36 if (!empty($context))
37 {
38 $this->context = $context;
39 }
40 }
41
45 protected function getContext()
46 {
47 return $this->context;
48 }
49
55 abstract public function getProductData(array $products);
56
62 abstract public function getCatalogData(array $products);
63
69 abstract public function tryShip(array $products);
70
76 public function isNeedShip(array $products)
77 {
78 $result = new Sale\Result();
79 $result->setData([
80 'IS_NEED_SHIP' => [],
81 ]);
82 return $result;
83 }
84
90 abstract public function tryUnship(array $products);
91
97 abstract public function ship(array $products);
98
104 abstract public function unship(array $products);
105
111 abstract public function getBundleItems(array $products);
112
118 abstract public function reserve(array $products);
119
125 abstract public function getAvailableQuantity(array $products);
126
132 abstract public function deliver(array $products);
133
139 abstract public function viewProduct(array $products);
140
146 abstract public function getProductListStores(array $products);
147
153 abstract public function checkBarcode(array $items);
154
160 abstract public function getAvailableQuantityAndPrice(array $products);
161
162 public function getAvailableQuantityByStore(array $products): Result
163 {
164 $result = $this->getAvailableQuantity($products);
165 if (!$result->isSuccess())
166 {
167 return $result;
168 }
169
170 $data = $result->getData();
171 if (empty($data) || empty($data[self::FLAT_AVAILABLE_QUANTITY_LIST]))
172 {
173 return $result;
174 }
175 if (!is_array($data[self::FLAT_AVAILABLE_QUANTITY_LIST]))
176 {
177 $result->setData([]);
178
179 return $result;
180 }
181
182 $quantityByStore = [];
183
184 foreach ($products as $productId => $item)
185 {
186 $quantityByStore[$productId] = $this->distributeQuantityByStore($item['QUANTITY_LIST_BY_STORE'], $data[self::FLAT_AVAILABLE_QUANTITY_LIST][$productId]);
187 }
188
189 $result->setData([
190 self::STORE_AVAILABLE_QUANTITY_LIST => $quantityByStore
191 ]);
192
193 return $result;
194 }
195
196 private function distributeQuantityByStore($needQuantityList, $availableQuantity) : array
197 {
198 $result = [];
199
200 foreach ($needQuantityList as $quantityByStore)
201 {
202 foreach ($quantityByStore as $storeId => $quantity)
203 {
204 if (abs($quantity) < abs($availableQuantity))
205 {
206 $result[$storeId] = $quantity;
207 $availableQuantity -= $quantity;
208 }
209 else
210 {
211 $result[$storeId] = $availableQuantity;
212 $availableQuantity = 0;
213 }
214 }
215 }
216
217 return $result;
218 }
219
220 public function getAvailableQuantityAndPriceByStore(array $products): Result
221 {
222 $result = $this->getAvailableQuantityAndPrice($products);
223
224 if (!$result->isSuccess())
225 {
226 return $result;
227 }
228
229 $data = $result->getData();
230 if (empty($data) || empty($data[self::SUMMMARY_PRODUCT_LIST]))
231 {
232 return $result;
233 }
234 if (!is_array($data[self::SUMMMARY_PRODUCT_LIST]))
235 {
236 $result->setData([]);
237
238 return $result;
239 }
240
241 $summary = $data[self::SUMMMARY_PRODUCT_LIST];
242
243 $priceList = (
244 !empty($summary[self::FLAT_PRICE_LIST])
245 && is_array($summary[self::FLAT_PRICE_LIST])
246 )
247 ? $summary[self::FLAT_PRICE_LIST]
248 : []
249 ;
250 $quantityList = (
251 !empty($summary[self::FLAT_AVAILABLE_QUANTITY_LIST])
252 && is_array($summary[self::FLAT_AVAILABLE_QUANTITY_LIST])
253 )
254 ? $this->getFilledDefaultStore($summary[self::FLAT_AVAILABLE_QUANTITY_LIST])
255 : []
256 ;
257
258 $result->setData([
259 self::SUMMMARY_PRODUCT_LIST => [
260 self::FLAT_PRICE_LIST => $priceList,
261 self::STORE_AVAILABLE_QUANTITY_LIST => $quantityList,
262 ],
263 ]);
264 unset($quantityList, $priceList, $summary, $data);
265
266 return $result;
267 }
268
269 protected function getFilledDefaultStore(array $quantityList): array
270 {
271 $result = [];
272 $storeId = static::getDefaultStoreId();
273 foreach (array_keys($quantityList) as $productId)
274 {
275 $result[$productId] = [
276 $storeId => $quantityList[$productId],
277 ];
278 }
279
280 return $result;
281 }
282
283 public static function getDefaultStoreId(): int
284 {
286 }
287}
tryShip(array $products)
getFilledDefaultStore(array $quantityList)
getAvailableQuantityByStore(array $products)
getAvailableQuantityAndPrice(array $products)
__construct(array $context=array())
getProductListStores(array $products)
getAvailableQuantity(array $products)
getCatalogData(array $products)
getAvailableQuantityAndPriceByStore(array $products)
tryUnship(array $products)
viewProduct(array $products)
deliver(array $products)
reserve(array $products)
getBundleItems(array $products)
getProductData(array $products)