Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
basketitem.php
1<?php
2
3
5
6
12use Bitrix\Sale;
15
17{
18 public function getPrimaryAutoWiredParameter()
19 {
20 return new ExactParameter(
21 Sale\BasketItem::class,
22 'basketItem',
23 function($className, $id) {
24 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
25
27 $basketClass = $registry->getBasketClassName();
28
29 $r = $basketClass::getList([
30 'select'=>['ORDER_ID'],
31 'filter'=>['ID'=>$id]
32 ]);
33
34 if($row = $r->fetch())
35 {
37 $orderClass = $registry->getOrderClassName();
38
39 $order = $orderClass::load($row['ORDER_ID']);
40 $basket = $order->getBasket()->getItemByBasketCode($id);
41 if ($basket instanceof \Bitrix\Sale\BasketItem)
42 {
43 return $basket;
44 }
45 }
46 else
47 {
48 $this->addError(new Error('basket item is not exists', 200140400001));
49 }
50 return null;
51
52 }
53 );
54 }
55
56 //region Actions
57 public function getFieldsAction()
58 {
59 $entity = new \Bitrix\Sale\Rest\Entity\BasketItem();
60 return ['BASKET_ITEM'=>$entity->prepareFieldInfos(
61 $entity->getFields()
62 )];
63 }
64
66 {
67 $entity = new \Bitrix\Sale\Rest\Entity\BasketItem();
68 return ['BASKET_ITEM'=>$entity->prepareFieldInfos(
69 $entity->getFieldsCatalogProduct()
70 )];
71 }
72
73 public function modifyAction(array $fields)
74 {
75 $builder = $this->getBuilder();
76 $builder->buildEntityBasket($fields);
77
78 if($builder->getErrorsContainer()->getErrorCollection()->count()>0)
79 {
80 $this->addErrors($builder->getErrorsContainer()->getErrors());
81 return null;
82 }
83
84 $order = $builder->getOrder();
85
86 $r = $order->save();
87 if(!$r->isSuccess())
88 {
89 $this->addErrors($r->getErrors());
90 return null;
91 }
92
93 //TODO: return $basket->toArray();
94 return ['BASKET_ITEMS'=>$this->toArray($order)['ORDER']['BASKET_ITEMS']];
95 }
96
97 public function addAction(array $fields)
98 {
99 $data = [];
100
101 $data['ORDER']['ID'] = $fields['ORDER_ID'];
102 $data['ORDER']['BASKET_ITEMS'] = [$fields];
103
104 $r = $this->addValidate($fields);
105 if($r->isSuccess())
106 {
107 $builder = $this->getBuilder(
109 'deleteBasketItemsIfNotExists' => false
110 ])
111 );
112 $builder->buildEntityBasket($data);
113
114 if($builder->getErrorsContainer()->getErrorCollection()->count()>0)
115 {
116 $this->addErrors($builder->getErrorsContainer()->getErrors());
117 return null;
118 }
119
120 $order = $builder->getOrder();
121
122 $idx=0;
123 $collection = $order->getBasket();
125 foreach($collection as $basketItem)
126 {
127 if($basketItem->getId() <= 0)
128 {
129 $idx = $basketItem->getInternalIndex();
130 break;
131 }
132 }
133
134 $r = $order->save();
135 }
136
137 if(!$r->isSuccess())
138 {
139 $this->addErrors($r->getErrors());
140 return null;
141 }
142
144 $entity = $order->getBasket()->getItemByIndex($idx);
145 return new Page('BASKET_ITEM', $this->get($entity), 1);
146 }
147
148 public function addCatalogProductAction(array $fields)
149 {
150 $fields['MODULE'] = 'catalog';
151 $fields['PRODUCT_PROVIDER_CLASS'] = '\Bitrix\Catalog\Product\CatalogProvider';
152
153 return $this->addAction($fields);
154 }
155
156 public function updateAction(\Bitrix\Sale\BasketItem $basketItem, array $fields)
157 {
158 $data = [];
159
160 $fields['ID'] = $basketItem->getBasketCode();
161 $fields['ORDER_ID'] = $basketItem->getCollection()->getOrderId();
162
163 $data['ORDER']['ID'] = $fields['ORDER_ID'];
164 $data['ORDER']['BASKET_ITEMS'] = [$fields];
165
166 $builder = $this->getBuilder(
168 'deleteBasketItemsIfNotExists' => false
169 ])
170 );
171
172 $builder->buildEntityBasket($data);
173
174 if($builder->getErrorsContainer()->getErrorCollection()->count()>0)
175 {
176 $this->addErrors($builder->getErrorsContainer()->getErrors());
177 return null;
178 }
179
180 $order = $builder->getOrder();
181
182 $r = $order->save();
183 if(!$r->isSuccess())
184 {
185 $this->addErrors($r->getErrors());
186 return null;
187 }
188
190 $entity = $order->getBasket()->getItemById($basketItem->getId());
191 return new Page('BASKET_ITEM', $this->get($entity), 1);
192 }
193
194 public function updateCatalogProductAction(\Bitrix\Sale\BasketItem $basketItem, array $fields)
195 {
196 return $this->updateAction($basketItem, $fields);
197 }
198
199 public function getAction(\Bitrix\Sale\BasketItem $basketItem)
200 {
201 //TODO: $basketItem->toArray();
202 return ['BASKET_ITEM' => $this->get($basketItem)];
203 }
204
205 public function deleteAction(\Bitrix\Sale\BasketItem $basketItem)
206 {
208 $basketCollection = $basketItem->getCollection();
209 $order = $basketCollection->getOrder();
210
211 $r = $basketItem->delete();
212 if($r->isSuccess())
213 $r = $order->save();
214
215 if(!$r->isSuccess())
216 $this->addErrors($r->getErrors());
217
218 return $r->isSuccess();
219 }
220
221 public function listAction(PageNavigation $pageNavigation, array $select = [], array $filter = [], array $order = []): Page
222 {
223 $select = empty($select) ? ['*'] : $select;
224 $order = empty($order)? ['ID' => 'ASC'] : $order;
225
226 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
227
229 $basketClass = $registry->getBasketClassName();
230
231 $items = $basketClass::getList(
232 [
233 'select' => $select,
234 'filter' => $filter,
235 'order' => $order,
236 'offset' => $pageNavigation->getOffset(),
237 'limit' => $pageNavigation->getLimit(),
238 ]
239 )->fetchAll();
240
241 return new Page('BASKET_ITEMS', $items, function() use ($filter)
242 {
243 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
244
246 $basketClass = $registry->getBasketClassName();
247
248 return count(
249 $basketClass::getList(['filter'=>$filter])->fetchAll()
250 );
251 });
252 }
253
254 public function canBuyAction(\Bitrix\Sale\BasketItem $basketItem)
255 {
256 return $basketItem->canBuy()?'Y':'N';
257 }
258
259 public function getBasePriceAction(\Bitrix\Sale\BasketItem $basketItem)
260 {
261 return $basketItem->getBasePrice();
262 }
263
264 public function getBasePriceWithVatAction(\Bitrix\Sale\BasketItem $basketItem)
265 {
266 return $basketItem->getBasePriceWithVat();
267 }
268
269 public function getCurrencyAction(\Bitrix\Sale\BasketItem $basketItem)
270 {
271 return $basketItem->getCurrency();
272 }
273
274 public function getDefaultPriceAction(\Bitrix\Sale\BasketItem $basketItem)
275 {
276 return $basketItem->getDefaultPrice();
277 }
278
279 public function getDiscountPriceAction(\Bitrix\Sale\BasketItem $basketItem)
280 {
281 return $basketItem->getDiscountPrice();
282 }
283
284 public function getFinalPriceAction(\Bitrix\Sale\BasketItem $basketItem)
285 {
286 return $basketItem->getFinalPrice();
287 }
288
289 public function getInitialPriceAction(\Bitrix\Sale\BasketItem $basketItem)
290 {
291 return $basketItem->getInitialPrice();
292 }
293
294 public function getPriceAction(\Bitrix\Sale\BasketItem $basketItem)
295 {
296 return $basketItem->getPrice();
297 }
298
299 public function getPriceWithVatAction(\Bitrix\Sale\BasketItem $basketItem)
300 {
301 return $basketItem->getPriceWithVat();
302 }
303
304 public function getProductIdAction(\Bitrix\Sale\BasketItem $basketItem)
305 {
306 return $basketItem->getProductId();
307 }
308
309 public function getQuantityAction(\Bitrix\Sale\BasketItem $basketItem)
310 {
311 return $basketItem->getQuantity();
312 }
313
314 public function getReservedQuantityAction(\Bitrix\Sale\BasketItem $basketItem)
315 {
316 return $basketItem->getReservedQuantity();
317 }
318
319 public function getVatAction(\Bitrix\Sale\BasketItem $basketItem)
320 {
321 return $basketItem->getVat();
322 }
323
324 public function getVatRateAction(\Bitrix\Sale\BasketItem $basketItem)
325 {
326 return $basketItem->getVatRate();
327 }
328
329 public function getWeightAction(\Bitrix\Sale\BasketItem $basketItem)
330 {
331 return $basketItem->getWeight();
332 }
333
334 public function isBarcodeMultiAction(\Bitrix\Sale\BasketItem $basketItem)
335 {
336 return $basketItem->isBarcodeMulti()? 'Y':'N';
337 }
338
339 public function isCustomMultiAction(\Bitrix\Sale\BasketItem $basketItem)
340 {
341 return $basketItem->isCustom()? 'Y':'N';
342 }
343
344 public function isCustomPriceAction(\Bitrix\Sale\BasketItem $basketItem)
345 {
346 return $basketItem->isCustomPrice()? 'Y':'N';
347 }
348
349 public function isDelayAction(\Bitrix\Sale\BasketItem $basketItem)
350 {
351 return $basketItem->isDelay()? 'Y':'N';
352 }
353
354 public function isVatInPriceAction(\Bitrix\Sale\BasketItem $basketItem)
355 {
356 return $basketItem->isVatInPrice()? 'Y':'N';
357 }
358
359 /*public function checkProductBarcodeAction(\Bitrix\Sale\BasketItem $basketItem, array $fields)
360 {
361 $r = false;
362 if ($basketItem)
363 {
364 $r = Provider::checkProductBarcode($basketItem, $fields);
365 }
366
367 if ($r)
368 return true;
369 else
370 {
371 $this->addError(new Error('barcode check failed',200150000010));
372 return null;
373 }
374
375 }*/
376
377 //endregion
378
379 protected function get(\Bitrix\Sale\BasketItem $basketItem, array $fields=[])
380 {
381 $items = $this->toArray($basketItem->getCollection()->getOrder(), $fields)['ORDER']['BASKET_ITEMS'];
382 foreach ($items as $item)
383 {
384 if($item['ID'] == $basketItem->getId())
385 {
386 return $item;
387 }
388 }
389 return [];
390 }
391
392 static public function prepareFields($fields)
393 {
394 $data = null;
395 Loader::includeModule('catalog');
396
397 if(isset($fields['BASKET_ITEMS']))
398 {
399 $i=0;
400 foreach ($fields['BASKET_ITEMS'] as $item)
401 {
402 if(isset($item['PRODUCT_ID']))
403 {
404 $item['OFFER_ID'] = $item['PRODUCT_ID'];
405 unset($item['PRODUCT_ID']); // need for builder
406 }
407
408 /*$properties = isset($item['PROPERTIES'])? $item['PROPERTIES']:[];
409 foreach ($properties as &$property)
410 {
411 if(isset($property['BASKET_ID']))
412 unset($property['BASKET_ID']);
413 }
414
415 $item['PROPS'] = $properties;*/
416
417 $item['PROPS'] = isset($item['PROPERTIES'])? $item['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_ITEMS']);
427 }
428
429 return is_array($data)?['PRODUCT'=>$data]:[];
430 }
431
432 protected function checkPermissionEntity($name)
433 {
434 if($name == 'canbuy'
435 || $name == 'getbaseprice'
436 || $name == 'getbasepricewithvat'
437 || $name == 'getcurrency'
438 || $name == 'getdefaultprice'
439 || $name == 'getdiscountprice'
440 || $name == 'getfinalprice'
441 || $name == 'getinitialprice'
442 || $name == 'getprice'
443 || $name == 'getpricewithvat'
444 || $name == 'getproductid'
445 || $name == 'getquantity'
446 || $name == 'getreservedquantity'
447 || $name == 'getvat'
448 || $name == 'getvatrate'
449 || $name == 'getweight'
450 || $name == 'isbarcodemulti'
451 || $name == 'iscustommulti'
452 || $name == 'iscustomprice'
453 || $name == 'isdelay'
454 || $name == 'isvatinprice'
455 || $name == 'getfieldscatalogproduct'
456 )
457 {
458 $r = $this->checkReadPermissionEntity();
459 }
460 elseif ($name == 'addcatalogproduct' || $name == 'updatecatalogproduct')
461 {
462 $r = $this->checkCreatePermissionEntity();
463 }
464 else
465 {
466 $r = parent::checkPermissionEntity($name);
467 }
468
469 return $r;
470 }
471
472 protected function addValidate($fields)
473 {
474 $result = new Result();
475
476 if(isset($fields['ORDER_ID']) == false || intval($fields['ORDER_ID'])<=0)
477 {
478 $result->addError(new Error('Required fields: fields[ORDER_ID]'));
479 }
480 else
481 {
482 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
484 $orderClass = $registry->getOrderClassName();
485
486 $order = $orderClass::load($fields['ORDER_ID']);
487 if($order->getCurrency() <> $fields['CURRENCY'])
488 {
489 $result->addError(new Error('Currency must be the currency of the order'));
490 }
491 }
492
493 return $result;
494
495 }
496}
canBuyAction(\Bitrix\Sale\BasketItem $basketItem)
getWeightAction(\Bitrix\Sale\BasketItem $basketItem)
getBasePriceAction(\Bitrix\Sale\BasketItem $basketItem)
getDefaultPriceAction(\Bitrix\Sale\BasketItem $basketItem)
getProductIdAction(\Bitrix\Sale\BasketItem $basketItem)
updateCatalogProductAction(\Bitrix\Sale\BasketItem $basketItem, array $fields)
getReservedQuantityAction(\Bitrix\Sale\BasketItem $basketItem)
isDelayAction(\Bitrix\Sale\BasketItem $basketItem)
getAction(\Bitrix\Sale\BasketItem $basketItem)
getDiscountPriceAction(\Bitrix\Sale\BasketItem $basketItem)
getFinalPriceAction(\Bitrix\Sale\BasketItem $basketItem)
isCustomPriceAction(\Bitrix\Sale\BasketItem $basketItem)
isVatInPriceAction(\Bitrix\Sale\BasketItem $basketItem)
isBarcodeMultiAction(\Bitrix\Sale\BasketItem $basketItem)
isCustomMultiAction(\Bitrix\Sale\BasketItem $basketItem)
getInitialPriceAction(\Bitrix\Sale\BasketItem $basketItem)
getPriceAction(\Bitrix\Sale\BasketItem $basketItem)
getVatRateAction(\Bitrix\Sale\BasketItem $basketItem)
getQuantityAction(\Bitrix\Sale\BasketItem $basketItem)
getCurrencyAction(\Bitrix\Sale\BasketItem $basketItem)
getPriceWithVatAction(\Bitrix\Sale\BasketItem $basketItem)
getBasePriceWithVatAction(\Bitrix\Sale\BasketItem $basketItem)
getVatAction(\Bitrix\Sale\BasketItem $basketItem)