Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
basket.php
1<?php
2
3
5
6
17
18class Basket extends Controller
19{
21 {
22 return new ExactParameter(
23 BasketItem::class,
24 'basket',
25 function($className, $id) {
26
27 $r = \Bitrix\Sale\Basket::getList([
28 'select'=>['ORDER_ID'],
29 'filter'=>['ID'=>$id]
30 ]);
31
32 if($row = $r->fetch())
33 {
34 $order = \Bitrix\Sale\Order::load($row['ORDER_ID']);
35 $basket = $order->getBasket()->getItemByBasketCode($id);
36 if($basket instanceof BasketItem)
37 return $basket;
38 }
39 else
40 {
41 $this->addError(new Error(Loc::getMessage('CONTROLLER_ERROR_BASEKT_NOT_EXISTS', ['#ID#'=>$id]), 'BASEKT_NOT_EXISTS'));
42 }
43 return null;
44
45 }
46 );
47 }
48
49 public function getAutoWiredParameters()
50 {
51 return [
52 new \Bitrix\Main\Engine\AutoWire\Parameter(
53 BasketItem::class,
54 function($className, $id) {
55
56 $r = \Bitrix\Sale\Basket::getList([
57 'select'=>['ORDER_ID'],
58 'filter'=>['ID'=>$id]
59 ]);
60
61 if($row = $r->fetch())
62 {
63 $order = \Bitrix\Sale\Order::load($row['ORDER_ID']);
64 $basket = $order->getBasket()->getItemByBasketCode($id);
65 if($basket instanceof BasketItem)
66 return $basket;
67 }
68 else
69 {
70 $this->addError(new Error(Loc::getMessage('CONTROLLER_ERROR_BASEKT_NOT_EXISTS', ['#ID#'=>$id]), 'BASEKT_NOT_EXISTS'));
71 }
72 return null;
73 }
74 )
75 ];
76 }
77
78 //region Actions
79 public function getFieldsAction()
80 {
81 $entity = new \Bitrix\Sale\Rest\Entity\Basket();
82 return ['ITEM'=>$entity->prepareFieldInfos(
83 $entity->getFields()
84 )];
85 }
86
87 public function modifyAction(array $fields)
88 {
89 $builder = $this->getBuilder();
90 $builder->buildEntityBasket($fields);
91
92 if($builder->getErrorsContainer()->getErrorCollection()->count()>0)
93 {
94 $this->addErrors($builder->getErrorsContainer()->getErrors());
95 return null;
96 }
97
98 $order = $builder->getOrder();
99
100 $r = $order->save();
101 if(!$r->isSuccess())
102 {
103 $this->addErrors($r->getErrors());
104 return null;
105 }
106
107 //TODO: return $basket->toArray();
108 return ['ITEMS'=>$this->toArray($order)['ORDER']['BASKET']['ITEMS']];
109 }
110
111 public function addAction(array $fields)
112 {
113 if(isset($fields['BASKET_ITEMS']['ID']))
114 unset($fields['BASKET_ITEMS']['ID']);
115
116 $data = $fields;
117 unset($data['BASKET_ITEMS']);
118 $data['BASKET_ITEMS'] = [$fields['BASKET_ITEMS']];
119
120 $builder = $this->getBuilder(
122 'deleteBaketItemsIfNotExists' => false
123 ])
124 );
125 $builder->buildEntityBasket($data);
126
127 if($builder->getErrorsContainer()->getErrorCollection()->count()>0)
128 {
129 $this->addErrors($builder->getErrorsContainer()->getErrors());
130 return null;
131 }
132
133 $order = $builder->getOrder();
134
135 $idx=0;
136 $collection = $order->getBasket();
138 foreach($collection as $basketItem)
139 {
140 if($basketItem->getId() <= 0)
141 {
142 $idx = $basketItem->getInternalIndex();
143 break;
144 }
145 }
146
147 $r = $order->save();
148 if(!$r->isSuccess())
149 {
150 $this->addErrors($r->getErrors());
151 return null;
152 }
153
154 //TODO: return $basketItem->toArray();
155 return new Page('ITEM', $this->get($order->getBasket()->getItemByIndex($idx)), 1);
156 }
157
158 public function updateAction(BasketItem $basket, array $fields)
159 {
160 $data = [
161 'ORDER'=>[
162 'ID'=>$basket->getCollection()->getOrderId()
163 ],
164 'BASKET'=>[
165 'ITEMS'=>[
166 array_merge(
167 ['ID'=>$basket->getBasketCode()],
168 $fields
169 )
170 ]
171 ]
172 ];
173
174 $builder = $this->getBuilder(
176 'deleteBaketItemsIfNotExists' => false
177 ])
178 );
179
180 $builder->buildEntityBasket($data);
181
182 if($builder->getErrorsContainer()->getErrorCollection()->count()>0)
183 {
184 $this->addErrors($builder->getErrorsContainer()->getErrors());
185 return null;
186 }
187
188 $order = $builder->getOrder();
189
190 $r = $order->save();
191 if(!$r->isSuccess())
192 {
193 $this->addErrors($r->getErrors());
194 return null;
195 }
196
197 //TODO: return $order->toArray();
198 return new Page('ITEM', $this->get($basket), 1);
199 }
200
201 public function getAction(BasketItem $basket)
202 {
203 //TODO: $basketItem->toArray();
204 return ['ITEM' => $this->get($basket)];
205 }
206
207 public function deleteAction(BasketItem $basket)
208 {
210 $basketCollection = $basket->getCollection();
211 $order = $basketCollection->getOrder();
212
213 $r = $basket->delete();
214 if($r->isSuccess())
215 $r = $order->save();
216
217 if(!$r->isSuccess())
218 $this->addErrors($r->getErrors());
219
220 return $r->isSuccess();
221 }
222
223 public function listAction($select=[], $filter, $order=[], PageNavigation $pageNavigation)
224 {
225 $select = empty($select)? ['*']:$select;
226 $order = empty($order)? ['ID'=>'ASC']:$order;
227
228 $items = \Bitrix\Sale\Basket::getList(
229 [
230 'select'=>$select,
231 'filter'=>$filter,
232 'order'=>$order,
233 'offset' => $pageNavigation->getOffset(),
234 'limit' => $pageNavigation->getLimit()
235 ]
236 )->fetchAll();
237
238 return new Page('ITEMS', $items, function() use ($filter)
239 {
240 return count(
241 \Bitrix\Sale\Basket::getList(['filter'=>$filter])->fetchAll()
242 );
243 });
244 }
245
246 public function canBuyAction(BasketItem $basket)
247 {
248 return $basket->canBuy()?'Y':'N';
249 }
250
251 public function getBasePriceAction(BasketItem $basket)
252 {
253 return $basket->getBasketCode();
254 }
255
256 public function getBasePriceWithVatAction(BasketItem $basket)
257 {
258 return $basket->getBasePriceWithVat();
259 }
260
261 public function getCurrencyAction(BasketItem $basket)
262 {
263 return $basket->getCurrency();
264 }
265
266 public function getDefaultPriceAction(BasketItem $basket)
267 {
268 return $basket->getDefaultPrice();
269 }
270
271 public function getDiscountPriceAction(BasketItem $basket)
272 {
273 return $basket->getDiscountPrice();
274 }
275
276 public function getFinalPriceAction(BasketItem $basket)
277 {
278 return $basket->getFinalPrice();
279 }
280
281 public function getInitialPriceAction(BasketItem $basket)
282 {
283 return $basket->getInitialPrice();
284 }
285
286 public function getPriceAction(BasketItem $basket)
287 {
288 return $basket->getPrice();
289 }
290
291 public function getPriceWithVatAction(BasketItem $basket)
292 {
293 return $basket->getPriceWithVat();
294 }
295
296 public function getProductIdAction(BasketItem $basket)
297 {
298 return $basket->getProductId();
299 }
300
301 public function getQuantityAction(BasketItem $basket)
302 {
303 return $basket->getQuantity();
304 }
305
306 public function getReservedQuantityAction(BasketItem $basket)
307 {
308 return $basket->getReservedQuantity();
309 }
310
311 public function getVatAction(BasketItem $basket)
312 {
313 return $basket->getVat();
314 }
315
316 public function getVatRateAction(BasketItem $basket)
317 {
318 return $basket->getVatRate();
319 }
320
321 public function getWeightAction(BasketItem $basket)
322 {
323 return $basket->getWeight();
324 }
325
326 public function isBarcodeMultiAction(BasketItem $basket)
327 {
328 return $basket->isBarcodeMulti();
329 }
330
331 public function isCustomMultiAction(BasketItem $basket)
332 {
333 return $basket->isCustom();
334 }
335
336 public function isCustomPriceAction(BasketItem $basket)
337 {
338 return $basket->isCustomPrice();
339 }
340
341 public function isDelayAction(BasketItem $basket)
342 {
343 return $basket->isDelay();
344 }
345
346 public function isVatInPriceAction(BasketItem $basket)
347 {
348 return $basket->isVatInPrice();
349 }
350
351 public function checkProductBarcodeAction(BasketItem $basket, array $fields)
352 {
353 $r = false;
354 if ($basket)
355 {
356 /*
357 * fields = ['BARCODE'=>..., 'STORE_ID'=>...];
358 * */
359 $r = Provider::checkProductBarcode($basket, $fields);
360 }
361
362 if ($r)
363 return true;
364 else
365 {
366 $this->addError(new Error('','BARCODE_CHECK_FAILED'));
367 return null;
368 }
369
370 }
371
372 //endregion
373
374 protected function get(BasketItem $basketItem, array $fields=[])
375 {
376 $items = $this->toArray($basketItem->getCollection()->getOrder(), $fields)['ORDER']['BASKET']['ITEMS'];
377 foreach ($items as $item)
378 {
379 if($item['ID'] == $basketItem->getId())
380 {
381 return $item;
382 }
383 }
384 return [];
385 }
386
387 static public function prepareFields($fields)
388 {
389 $data = null;
390 Loader::includeModule('catalog');
391
392 if(isset($fields['BASKET']['ITEMS']))
393 {
394 $i=0;
395 foreach ($fields['BASKET']['ITEMS'] as $item)
396 {
397 $item['OFFER_ID'] = $item['PRODUCT_ID'];
398 unset($item['PRODUCT_ID']);
399
400 $item['CUSTOM_PRICE'] = isset($item['PRICE'])? 'Y':'N';
401
402 $item['MODULE'] = isset($item['MODULE'])? $item['MODULE']:null;
403 $item['PRODUCT_PROVIDER_CLASS'] = isset($item['PRODUCT_PROVIDER_CLASS'])? $item['PRODUCT_PROVIDER_CLASS']:null;
404
405 if($item['MODULE'] == 'catalog' && $item['PRODUCT_PROVIDER_CLASS'] === null)
406 {
407 $item['PRODUCT_PROVIDER_CLASS'] = \Bitrix\Catalog\Product\Basket::getDefaultProviderName();
408 }
409
410 $properties = isset($item['PROPERTIES'])? $item['PROPERTIES']:[];
411 foreach ($properties as &$property)
412 {
413 if(isset($property['BASKET_ID']))
414 unset($property['BASKET_ID']);
415 }
416
417 $item['PROPS'] = $properties;
418 unset($item['PROPERTIES']);
419
420 $basketCode = isset($item['ID'])? $item['ID']:'n'.++$i;
421 if(isset($item['ID']))
422 unset($item['ID']);
423
424 $data[$basketCode] = $item;
425 }
426 unset($fields['BASKET']);
427 }
428
429 return ['PRODUCT'=>is_array($data)?$data:[]];
430 }
431
432 protected function checkPermission($name)
433 {
434 if(substr($name,0,6) === 'canbuy')
435 {
436 $name = 'get';
437 }
438
439 parent::checkPermission($name);
440 }
441}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getList(array $parameters=array())
Definition basket.php:37
isCustomPriceAction(BasketItem $basket)
Definition basket.php:336
getReservedQuantityAction(BasketItem $basket)
Definition basket.php:306
getVatRateAction(BasketItem $basket)
Definition basket.php:316
getPriceAction(BasketItem $basket)
Definition basket.php:286
static prepareFields($fields)
Definition basket.php:387
isDelayAction(BasketItem $basket)
Definition basket.php:341
getDiscountPriceAction(BasketItem $basket)
Definition basket.php:271
updateAction(BasketItem $basket, array $fields)
Definition basket.php:158
modifyAction(array $fields)
Definition basket.php:87
getInitialPriceAction(BasketItem $basket)
Definition basket.php:281
getWeightAction(BasketItem $basket)
Definition basket.php:321
checkProductBarcodeAction(BasketItem $basket, array $fields)
Definition basket.php:351
listAction($select=[], $filter, $order=[], PageNavigation $pageNavigation)
Definition basket.php:223
getFinalPriceAction(BasketItem $basket)
Definition basket.php:276
canBuyAction(BasketItem $basket)
Definition basket.php:246
getBasePriceAction(BasketItem $basket)
Definition basket.php:251
getVatAction(BasketItem $basket)
Definition basket.php:311
getProductIdAction(BasketItem $basket)
Definition basket.php:296
isCustomMultiAction(BasketItem $basket)
Definition basket.php:331
isVatInPriceAction(BasketItem $basket)
Definition basket.php:346
isBarcodeMultiAction(BasketItem $basket)
Definition basket.php:326
getPriceWithVatAction(BasketItem $basket)
Definition basket.php:291
getAction(BasketItem $basket)
Definition basket.php:201
getQuantityAction(BasketItem $basket)
Definition basket.php:301
getCurrencyAction(BasketItem $basket)
Definition basket.php:261
getBasePriceWithVatAction(BasketItem $basket)
Definition basket.php:256
getDefaultPriceAction(BasketItem $basket)
Definition basket.php:266