Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
basketitem.php
1<?php
2
3
5
6
11
12class BasketItem extends Base
13{
14 public function getFields()
15 {
16 return $this->getFieldsInfoItem() + $this->getCustomProductFieldsInfo();
17 }
18
19 public function getFieldsCatalogProduct()
20 {
21 return $this->getFieldsInfoItem() + $this->getFieldsInfoCatalogProduct();
22 }
23
24 private function getFieldsInfoItem()
25 {
26 return [
27 'ORDER_ID'=>[
28 'TYPE'=>self::TYPE_STRING,
29 'ATTRIBUTES'=>[
30 Attributes::Immutable,
31 Attributes::Required
32 ]
33 ],
34 'ID'=>[
35 'TYPE'=>self::TYPE_STRING,
36 'ATTRIBUTES'=>[Attributes::ReadOnly]
37 ],
38 'FUSER_ID'=>[
39 'TYPE'=>self::TYPE_INT,
40 'ATTRIBUTES'=>[Attributes::ReadOnly]
41 ],
42 'LID'=>[
43 'TYPE'=>self::TYPE_STRING,
44 'ATTRIBUTES'=>[Attributes::ReadOnly]
45 ],
46 'SORT'=>[
47 'TYPE'=>self::TYPE_INT
48 ],
49 'PRODUCT_ID'=>[
50 'TYPE'=>self::TYPE_INT,
51 'ATTRIBUTES'=>[
52 Attributes::Immutable,
53 Attributes::Required
54 ]
55 ],
56 'PRICE'=>[
57 'TYPE'=>self::TYPE_FLOAT
58 ],
59 'CUSTOM_PRICE'=>[
60 'TYPE'=>self::TYPE_CHAR,
61 'ATTRIBUTES'=>[Attributes::ReadOnly]
62 ],
63 'CURRENCY'=>[
64 'TYPE'=>self::TYPE_STRING,
65 'ATTRIBUTES'=>[
66 Attributes::Required,
67 Attributes::Immutable
68 ]
69 ],
70 'QUANTITY'=>[
71 'TYPE'=>self::TYPE_FLOAT,
72 'ATTRIBUTES'=>[Attributes::Required]
73 ],
74 /*&&*/'SUBSCRIBE'=>[
75 'TYPE'=>self::TYPE_CHAR
76 ],
77 /*&&*/'RECOMMENDATION'=>[
78 'TYPE'=>self::TYPE_STRING
79 ],
80 'XML_ID'=>[
81 'TYPE'=>self::TYPE_STRING
82 ],
83 'DATE_INSERT'=>[
84 'TYPE'=>self::TYPE_DATETIME,
85 'ATTRIBUTES'=>[Attributes::ReadOnly]
86 ],
87 'DATE_UPDATE'=>[
88 'TYPE'=>self::TYPE_DATETIME,
89 'ATTRIBUTES'=>[Attributes::ReadOnly]
90 ],
91 'DATE_REFRESH'=>[
92 'TYPE'=>self::TYPE_DATETIME,
93 'ATTRIBUTES'=>[Attributes::ReadOnly],
94 ],
95 'PROPERTIES'=>[
96 'TYPE'=>self::TYPE_LIST,
97 'ATTRIBUTES'=>[Attributes::Hidden]
98 ]
99 ];
100 }
101
102 private function getFieldsInfoCatalogProduct()
103 {
104 return [
105 'NAME'=>[
106 'TYPE'=>self::TYPE_STRING,
107 'ATTRIBUTES'=>[Attributes::ReadOnly]
108 ],
109 'PRODUCT_PRICE_ID'=>[
110 'TYPE'=>self::TYPE_INT,
111 'ATTRIBUTES'=>[Attributes::ReadOnly]
112 ],
113 'PRICE_TYPE_ID'=>[
114 'TYPE'=>self::TYPE_INT,
115 'ATTRIBUTES'=>[Attributes::ReadOnly]
116 ],
117 'DETAIL_PAGE_URL'=>[
118 'TYPE'=>self::TYPE_STRING,
119 'ATTRIBUTES'=>[Attributes::ReadOnly]
120 ],
121 'BASE_PRICE'=>[
122 'TYPE'=>self::TYPE_FLOAT,
123 'ATTRIBUTES'=>[Attributes::ReadOnly]
124 ],
125 'DISCOUNT_PRICE'=>[
126 'TYPE'=>self::TYPE_FLOAT,
127 'ATTRIBUTES'=>[Attributes::ReadOnly]
128 ],
129 'WEIGHT'=>[
130 'TYPE'=>self::TYPE_FLOAT,
131 'ATTRIBUTES'=>[Attributes::ReadOnly]
132 ],
133 'DIMENSIONS'=>[
134 'TYPE'=>self::TYPE_STRING,
135 'ATTRIBUTES'=>[Attributes::ReadOnly]
136 ],
137 'MEASURE_CODE'=>[
138 'TYPE'=>self::TYPE_STRING,
139 'ATTRIBUTES'=>[Attributes::ReadOnly]
140 ],
141 'MEASURE_NAME'=>[
142 'TYPE'=>self::TYPE_STRING,
143 'ATTRIBUTES'=>[Attributes::ReadOnly]
144 ],
145 'CAN_BUY'=>[
146 'TYPE'=>self::TYPE_CHAR,
147 'ATTRIBUTES'=>[Attributes::ReadOnly]
148 ],
149 'NOTES'=>[
150 'TYPE'=>self::TYPE_STRING,
151 'ATTRIBUTES'=>[Attributes::ReadOnly]
152 ],
153 'VAT_RATE'=>[
154 'TYPE'=>self::TYPE_FLOAT,
155 'ATTRIBUTES'=>[Attributes::ReadOnly]
156 ],
157 'VAT_INCLUDED'=>[
158 'TYPE'=>self::TYPE_CHAR,
159 'ATTRIBUTES'=>[Attributes::ReadOnly]
160 ],
161 'BARCODE_MULTI'=>[
162 'TYPE'=>self::TYPE_CHAR,
163 'ATTRIBUTES'=>[Attributes::ReadOnly]
164 ],
165 'TYPE'=>[
166 'TYPE'=>self::TYPE_STRING,
167 'ATTRIBUTES'=>[Attributes::ReadOnly]
168 ],
169 'SET_PARENT_ID'=>[
170 'TYPE'=>self::TYPE_STRING,
171 'ATTRIBUTES'=>[Attributes::ReadOnly]
172 ],
173 'DISCOUNT_NAME'=>[
174 'TYPE'=>self::TYPE_STRING,
175 'ATTRIBUTES'=>[Attributes::ReadOnly]
176 ],
177 'DISCOUNT_VALUE'=>[
178 'TYPE'=>self::TYPE_STRING,
179 'ATTRIBUTES'=>[Attributes::ReadOnly]
180 ],
181 'DISCOUNT_COUPON'=>[
182 'TYPE'=>self::TYPE_STRING,
183 'ATTRIBUTES'=>[Attributes::ReadOnly]
184 ],
185 'CATALOG_XML_ID'=>[
186 'TYPE'=>self::TYPE_STRING,
187 'ATTRIBUTES'=>[Attributes::ReadOnly]
188 ],
189 'PRODUCT_XML_ID'=>[
190 'TYPE'=>self::TYPE_STRING,
191 'ATTRIBUTES'=>[Attributes::ReadOnly]
192 ],
193 'PRODUCT_PROVIDER_CLASS'=>[
194 'TYPE'=>self::TYPE_STRING,
195 'ATTRIBUTES'=>[Attributes::ReadOnly]
196 ],
197 'MODULE'=>[
198 'TYPE'=>self::TYPE_STRING,
199 'ATTRIBUTES'=>[Attributes::ReadOnly]
200 ],
201 ];
202 }
203
204 private function getCustomProductFieldsInfo()
205 {
206 return [
207 'NAME'=>[
208 'TYPE'=>self::TYPE_STRING
209 ],
210 'PRODUCT_PRICE_ID'=>[
211 'TYPE'=>self::TYPE_INT
212 ],
213 'PRICE_TYPE_ID'=>[
214 'TYPE'=>self::TYPE_INT
215 ],
216 'DETAIL_PAGE_URL'=>[
217 'TYPE'=>self::TYPE_STRING
218 ],
219 'BASE_PRICE'=>[
220 'TYPE'=>self::TYPE_FLOAT
221 ],
222 'DISCOUNT_PRICE'=>[
223 'TYPE'=>self::TYPE_FLOAT
224 ],
225 'WEIGHT'=>[
226 'TYPE'=>self::TYPE_FLOAT
227 ],
228 'DIMENSIONS'=>[
229 'TYPE'=>self::TYPE_STRING
230 ],
231 'MEASURE_CODE'=>[
232 'TYPE'=>self::TYPE_STRING
233 ],
234 'MEASURE_NAME'=>[
235 'TYPE'=>self::TYPE_STRING
236 ],
237 'CAN_BUY'=>[
238 'TYPE'=>self::TYPE_CHAR
239 ],
240 /*??*/'NOTES'=>[
241 'TYPE'=>self::TYPE_STRING
242 ],
243 'VAT_RATE'=>[
244 'TYPE'=>self::TYPE_FLOAT
245 ],
246 'VAT_INCLUDED'=>[
247 'TYPE'=>self::TYPE_CHAR
248 ],
249 'BARCODE_MULTI'=>[
250 'TYPE'=>self::TYPE_CHAR
251 ],
252 'TYPE'=>[
253 'TYPE'=>self::TYPE_STRING
254 ],
255 'SET_PARENT_ID'=>[
256 'TYPE'=>self::TYPE_STRING
257 ],
258 /*??*/'DISCOUNT_NAME'=>[
259 'TYPE'=>self::TYPE_STRING
260 ],
261 /*??*/'DISCOUNT_VALUE'=>[
262 'TYPE'=>self::TYPE_STRING
263 ],
264 /*??*/'DISCOUNT_COUPON'=>[
265 'TYPE'=>self::TYPE_STRING
266 ],
267 'CATALOG_XML_ID'=>[
268 'TYPE'=>self::TYPE_STRING,
269 'ATTRIBUTES'=>[Attributes::Immutable]
270 ],
271 'PRODUCT_XML_ID'=>[
272 'TYPE'=>self::TYPE_STRING,
273 'ATTRIBUTES'=>[Attributes::Immutable]
274 ],
275 'PRODUCT_PROVIDER_CLASS'=>[
276 'TYPE'=>self::TYPE_STRING,
277 'ATTRIBUTES'=>[Attributes::Immutable]
278 ],
279 'MODULE'=>[
280 'TYPE'=>self::TYPE_STRING,
281 'ATTRIBUTES'=>[Attributes::Immutable]
282 ],
283 ];
284 }
285
286 public function convertKeysToSnakeCaseArguments($name, $arguments)
287 {
288 if($name == 'getfieldscatalogproduct'){}
289 elseif ($name == 'addcatalogproduct'
290 || $name == 'updatecatalogproduct'
291 || $name == 'modifycatalogproduct')
292 {
293 if(isset($arguments['fields']))
294 {
295 $fields = $arguments['fields'];
296 if(!empty($fields))
297 $arguments['fields'] = $this->convertKeysToSnakeCaseFields($fields);
298 }
299 }
300 else
301 {
302 $arguments = parent::convertKeysToSnakeCaseArguments($name, $arguments);
303 }
304
305 return $arguments;
306 }
307
308 public function checkArguments($name, $arguments)
309 {
310 $r = new Result();
311
312 if($name == 'getfieldscatalogproduct'){}
313 elseif($name == 'addcatalogproduct')
314 {
315 $fields = $arguments['fields'];
316 $fieldsInfo = $this->getListFieldInfo($this->getFieldsCatalogProduct(), ['filter'=>['ignoredAttributes'=>[Attributes::Hidden, Attributes::ReadOnly]]]);
317
318 if(!empty($fields))
319 {
320 $required = $this->checkRequiredFields($fields, $fieldsInfo);
321 if(!$required->isSuccess())
322 $r->addError(new Error('Required fields: '.implode(', ', $required->getErrorMessages())));
323 }
324 }
325 elseif($name == 'updatecatalogproduct')
326 {
327 $fields = $arguments['fields'];
328 $fieldsInfo = $this->getListFieldInfo($this->getFieldsCatalogProduct(), ['filter'=>['ignoredAttributes'=>[Attributes::Hidden, Attributes::ReadOnly, Attributes::Immutable]]]);
329
330 if(!empty($fields))
331 {
332 $required = $this->checkRequiredFields($fields, $fieldsInfo);
333 if(!$required->isSuccess())
334 $r->addError(new Error('Required fields: '.implode(', ', $required->getErrorMessages())));
335 }
336 }
337 else
338 {
339 $r = parent::checkArguments($name, $arguments);
340 }
341
342 return $r;
343 }
344
345 public function internalizeArguments($name, $arguments)
346 {
347 if($name == 'canbuy'
348 || $name == 'getbaseprice'
349 || $name == 'getbasepricewithvat'
350 || $name == 'getcurrency'
351 || $name == 'getdefaultprice'
352 || $name == 'getdiscountprice'
353 || $name == 'getfinalprice'
354 || $name == 'getinitialprice'
355 || $name == 'getprice'
356 || $name == 'getpricewithvat'
357 || $name == 'getproductid'
358 || $name == 'getquantity'
359 || $name == 'getreservedquantity'
360 || $name == 'getvat'
361 || $name == 'getvatrate'
362 || $name == 'getweight'
363 || $name == 'isbarcodemulti'
364 || $name == 'iscustommulti'
365 || $name == 'iscustomprice'
366 || $name == 'isdelay'
367 || $name == 'isvatinprice'
368 || $name == 'getfieldscatalogproduct'
369 ){}
370 elseif($name == 'addcatalogproduct')
371 {
372 $fields = $arguments['fields'];
373 $fieldsInfo = $this->getListFieldInfo($this->getFieldsCatalogProduct(), ['filter'=>['ignoredAttributes'=>[Attributes::Hidden, Attributes::ReadOnly]]]);
374
375 if(!empty($fields))
376 {
377 $arguments['fields'] = $this->internalizeFields($fields, $fieldsInfo);
378 }
379 }
380 elseif($name == 'updatecatalogproduct')
381 {
382 $fields = $arguments['fields'];
383 $fieldsInfo = $this->getListFieldInfo($this->getFieldsCatalogProduct(), ['filter'=>['ignoredAttributes'=>[Attributes::Hidden, Attributes::ReadOnly, Attributes::Immutable]]]);
384
385 if(!empty($fields))
386 {
387 $arguments['fields'] = $this->internalizeFields($fields, $fieldsInfo);
388 }
389 }
390 elseif($name == 'modifycatalogproduct'){}
391 else
392 {
393 parent::internalizeArguments($name, $arguments);
394 }
395
396 return $arguments;
397 }
398
399 public function externalizeResult($name, $fields)
400 {
401 if($name == 'getfieldscatalogproduct'){}
402 elseif($name == 'addcatalogproduct'
403 || $name == 'updatecatalogproduct')
404 {
405 $fields = $this->externalizeFields($fields);
406 }
407 else
408 {
409 parent::externalizeResult($name, $fields);
410 }
411
412 return $fields;
413 }
414
415 protected function isNewItem($fields)
416 {
417 if(isset($fields['ID']) === false)
418 {
419 return true;
420 }
421 else
422 {
423 return BasketBuilderRest::isBasketItemNew($fields['ID']);
424 }
425 }
426
427 public function internalizeFieldsModify($fields, $fieldsInfo=[])
428 {
429 $result = [];
430 $basketProperties = new BasketProperties();
431
432 $fieldsInfo = empty($fieldsInfo)? $this->getFields():$fieldsInfo;
433 $listFieldsInfoAdd = $this->getListFieldInfo($fieldsInfo, ['filter'=>['ignoredAttributes'=>[Attributes::Hidden, Attributes::ReadOnly], 'ignoredFields'=>['ORDER_ID']]]);
434 $listFieldsInfoUpdate = $this->getListFieldInfo($fieldsInfo, ['filter'=>['ignoredAttributes'=>[Attributes::Hidden, Attributes::ReadOnly, Attributes::Immutable], 'skipFields'=>['ID']]]);
435
436 if(isset($fields['ORDER']['ID']))
437 $result['ORDER']['ID'] = (int)$fields['ORDER']['ID'];
438
439 if(isset($fields['ORDER']['BASKET_ITEMS']))
440 {
441 foreach ($fields['ORDER']['BASKET_ITEMS'] as $k=>$item)
442 {
443 $result['ORDER']['BASKET_ITEMS'][$k] = $this->internalizeFields($item,
444 $this->isNewItem($item)? $listFieldsInfoAdd:$listFieldsInfoUpdate
445 );
446
447 // n1 - ref shipmentItem.basketId
448 if($this->isNewItem($item) && isset($item['ID']))
449 $result['ORDER']['BASKET_ITEMS'][$k]['ID'] = $item['ID'];
450
451 if(isset($item['PROPERTIES']))
452 {
453 $result['ORDER']['BASKET_ITEMS'][$k]['PROPERTIES'] = $basketProperties->internalizeFieldsModify(['BASKET_ITEM'=>['PROPERTIES'=>$item['PROPERTIES']]])['BASKET_ITEM']['PROPERTIES'];
454 }
455 }
456 }
457
458 return $result;
459 }
460
461 public function externalizeFields($fields)
462 {
463 $basketProperties = new \Bitrix\Sale\Rest\Entity\BasketProperties();
464
465 $result = parent::externalizeFields($fields);
466
467 if(isset($fields['PROPERTIES']))
468 $result['PROPERTIES'] = $basketProperties->externalizeListFields($fields['PROPERTIES']);
469
470 return $result;
471 }
472
473 public function externalizeFieldsModify($fields)
474 {
475 return $this->externalizeListFields($fields);
476 }
477
478 public function checkFieldsModify($fields)
479 {
480 $r = new Result();
481
482 $emptyFields = [];
483 if(!isset($fields['ORDER']['ID']))
484 {
485 $emptyFields[] = '[order][id]';
486 }
487 if(!isset($fields['ORDER']['BASKET_ITEMS']) || !is_array($fields['ORDER']['BASKET_ITEMS']))
488 {
489 $emptyFields[] = '[order][basketItems][]';
490 }
491
492 if(count($emptyFields)>0)
493 {
494 $r->addError(new Error('Required fields: '.implode(', ', $emptyFields)));
495 }
496 else
497 {
498 $r = parent::checkFieldsModify($fields);
499 }
500
501 return $r;
502 }
503
504 public function checkRequiredFieldsModify($fields)
505 {
506 $r = new Result();
507
508 $basketProperties = new BasketProperties();
509
510 $listFieldsInfoAdd = $this->getListFieldInfo($this->getFields(), ['filter'=>['ignoredAttributes'=>[Attributes::Hidden, Attributes::ReadOnly], 'ignoredFields'=>['ORDER_ID']]]);
511 $listFieldsInfoUpdate = $this->getListFieldInfo($this->getFields(), ['filter'=>['ignoredAttributes'=>[Attributes::Hidden, Attributes::ReadOnly, Attributes::Immutable]]]);
512
513 foreach ($fields['ORDER']['BASKET_ITEMS'] as $k=>$item)
514 {
515 $required = $this->checkRequiredFields($item,
516 $this->isNewItem($item)? $listFieldsInfoAdd:$listFieldsInfoUpdate
517 );
518 if(!$required->isSuccess())
519 {
520 $r->addError(new Error('[basketItems]['.$k.'] - '.implode(', ', $required->getErrorMessages()).'.'));
521 }
522
523 if(isset($item['PROPERTIES']))
524 {
525 $requiredProperties = $basketProperties->checkRequiredFieldsModify(['BASKET_ITEM'=>['PROPERTIES'=>$item['PROPERTIES']]]);
526 if(!$requiredProperties->isSuccess())
527 {
528 $requiredPropertiesFields = [];
529 foreach ($requiredProperties->getErrorMessages() as $errorMessage)
530 {
531 $requiredPropertiesFields[] = '[basketItems]['.$k.']'.$errorMessage;
532 }
533 $r->addError(new Error(implode( ' ', $requiredPropertiesFields)));
534 }
535
536 }
537 }
538 return $r;
539 }
540}
getListFieldInfo(array $fieldsInfo, $params=[])
Definition base.php:62
internalizeFields($fields, array $fieldsInfo)
Definition base.php:281
convertKeysToSnakeCaseFields($fields)
Definition base.php:101
checkRequiredFields($fields, array $fieldsInfo, $params=[])
Definition base.php:676
internalizeArguments($name, $arguments)
convertKeysToSnakeCaseArguments($name, $arguments)
internalizeFieldsModify($fields, $fieldsInfo=[])