Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
order.php
1<?php
2
3
5
6
12
13class Order extends Base
14{
15 public function getFields()
16 {
17 return [
18 'PERSON_TYPE_ID'=>[
19 'TYPE'=>self::TYPE_INT,
20 'ATTRIBUTES'=>[
23 ]
24 ],
25 'USER_ID'=>[
26 'TYPE'=>self::TYPE_INT,
27 'ATTRIBUTES'=>[
29 ]
30 ],
31 'CURRENCY'=>[
32 'TYPE'=>self::TYPE_STRING,
33 'ATTRIBUTES'=>[
36 ]
37 ],
38 'LID'=>[
39 'TYPE'=>self::TYPE_STRING,
40 'ATTRIBUTES'=>[
43 ]
44 ],
45 'PERSON_TYPE_XML_ID'=>[
46 'TYPE'=>self::TYPE_STRING,
47 'ATTRIBUTES'=>[Attributes::ReadOnly]
48 ],
49 'STATUS_XML_ID'=>[
50 'TYPE'=>self::TYPE_STRING,
51 'ATTRIBUTES'=>[Attributes::ReadOnly]
52 ],
53 'ACCOUNT_NUMBER'=>[
54 'TYPE'=>self::TYPE_STRING,
55 'ATTRIBUTES'=>[Attributes::ReadOnly]
56 ],
57 'ID'=>[
58 'TYPE'=>self::TYPE_INT,
59 'ATTRIBUTES'=>[Attributes::ReadOnly]
60 ],
61 'CANCELED'=>[
62 'TYPE'=>self::TYPE_CHAR
63 ],
64 'DATE_CANCELED'=>[
65 'TYPE'=>self::TYPE_DATETIME,
66 'ATTRIBUTES'=>[Attributes::ReadOnly]
67 ],
68 'DEDUCTED'=>[
69 'TYPE'=>self::TYPE_CHAR,
70 'ATTRIBUTES'=>[Attributes::ReadOnly]
71 ],
72 'EMP_CANCELED_ID'=>[
73 'TYPE'=>self::TYPE_INT
74 ],
75 'REASON_CANCELED'=>[
76 'TYPE'=>self::TYPE_STRING
77 ],
78 'STATUS_ID'=>[
79 'TYPE'=>self::TYPE_CHAR
80 ],
81 'DATE_STATUS'=>[
82 'TYPE'=>self::TYPE_DATETIME,
83 'ATTRIBUTES'=>[Attributes::ReadOnly]
84 ],
85 'EMP_STATUS_ID'=>[
86 'TYPE'=>self::TYPE_INT
87 ],
88 'MARKED'=>[
89 'TYPE'=>self::TYPE_CHAR
90 ],
91 'DATE_MARKED'=>[
92 'TYPE'=>self::TYPE_DATETIME,
93 'ATTRIBUTES'=>[Attributes::ReadOnly]
94 ],
95 'EMP_MARKED_ID'=>[
96 'TYPE'=>self::TYPE_INT
97 ],
98 'REASON_MARKED'=>[
99 /*??*/'TYPE'=>self::TYPE_STRING
100 ],
101 'PRICE'=>[
102 'TYPE'=>self::TYPE_FLOAT
103 ],
104 'PAYED'=>[
105 'TYPE'=>self::TYPE_CHAR,
106 'ATTRIBUTES'=>[Attributes::ReadOnly]
107 ],
108 'DISCOUNT_VALUE'=>[
109 'TYPE'=>self::TYPE_FLOAT
110 ],
111 'DATE_INSERT'=>[
112 'TYPE'=>self::TYPE_DATETIME
113 ],
114 'DATE_UPDATE'=>[
115 'TYPE'=>self::TYPE_DATETIME,
116 'ATTRIBUTES'=>[Attributes::ReadOnly]
117 ],
118 'USER_DESCRIPTION'=>[
119 'TYPE'=>self::TYPE_STRING
120 ],
121 'ADDITIONAL_INFO'=>[
122 'TYPE'=>self::TYPE_STRING
123 ],
124 'COMMENTS'=>[
125 'TYPE'=>self::TYPE_STRING
126 ],
127 'TAX_VALUE'=>[
128 /*??*/'TYPE'=>self::TYPE_FLOAT,
129 'ATTRIBUTES'=>[Attributes::ReadOnly]
130 ],
131 'RECURRING_ID'=>[
132 'TYPE'=>self::TYPE_CHAR
133 ],
134 'LOCKED_BY'=>[
135 'TYPE'=>self::TYPE_CHAR
136 ],
137 'DATE_LOCK'=>[
138 'TYPE'=>self::TYPE_DATETIME,
139 'ATTRIBUTES'=>[Attributes::ReadOnly]
140 ],
141 'RECOUNT_FLAG'=>[
142 'TYPE'=>self::TYPE_CHAR
143 ],
144 'AFFILIATE_ID'=>[
145 'TYPE'=>self::TYPE_INT
146 ],
147 'DELIVERY_DOC_NUM'=>[
148 'TYPE'=>self::TYPE_STRING,
149 'ATTRIBUTES'=>[Attributes::Hidden]
150 ],
151 'DELIVERY_DOC_DATE'=>[
152 'TYPE'=>self::TYPE_DATETIME,
153 'ATTRIBUTES'=>[Attributes::Hidden]
154 ],
155 'UPDATED_1C'=>[
156 'TYPE'=>self::TYPE_CHAR
157 ],
158 'STORE_ID'=>[
159 'TYPE'=>self::TYPE_INT,
160 'ATTRIBUTES'=>[Attributes::Hidden]
161 ],
162 'ORDER_TOPIC'=>[
163 'TYPE'=>self::TYPE_STRING
164 ],
165 'RESPONSIBLE_ID'=>[
166 'TYPE'=>self::TYPE_INT
167 ],
168 'DATE_BILL'=>[
169 'TYPE'=>self::TYPE_DATETIME,
170 'ATTRIBUTES'=>[Attributes::Hidden]
171 ],
172 'DATE_PAY_BEFORE'=>[
173 'TYPE'=>self::TYPE_DATETIME,
174 'ATTRIBUTES'=>[Attributes::Hidden]
175 ],
176 'XML_ID'=>[
177 'TYPE'=>self::TYPE_STRING
178 ],
179 'ID_1C'=>[
180 'TYPE'=>self::TYPE_STRING
181 ],
182 'VERSION_1C'=>[
183 'TYPE'=>self::TYPE_STRING
184 ],
185 'VERSION'=>[
186 'TYPE'=>self::TYPE_INT,
187 'ATTRIBUTES'=>[Attributes::ReadOnly]
188 ],
189 'EXTERNAL_ORDER'=>[
190 'TYPE'=>self::TYPE_CHAR
191 ],
192 'COMPANY_ID'=>[
193 'TYPE'=>self::TYPE_INT
194 ],
195 //region List fields
196 'PAYMENTS'=>['TYPE'=>self::TYPE_LIST, 'ATTRIBUTES'=>[Attributes::Hidden]],
197 'SHIPMENTS'=>['TYPE'=>self::TYPE_LIST, 'ATTRIBUTES'=>[Attributes::Hidden]],
198 'PROPERTY_VALUES'=>['TYPE'=>self::TYPE_LIST, 'ATTRIBUTES'=>[Attributes::Hidden]],
199 'BASKET_ITEMS'=>['TYPE'=>self::TYPE_LIST, 'ATTRIBUTES'=>[Attributes::Hidden]],
200 //endregion
201 ];
202 }
203
204 public function internalizeFieldsModify($fields, $fieldsInfo=[])
205 {
206 $result = [];
207
208 $payment = new Payment();
209 $shipment = new Shipment();
210 $basketItem = new BasketItem();
211 $propertyValue = new PropertyValue();
212 $tradeBinding = new TradeBinding();
213
214
215 $listFieldsInfoAdd = $this->getListFieldInfo($this->getFields(), ['filter'=>['ignoredAttributes'=>[Attributes::Hidden, Attributes::ReadOnly]]]);
216 $listFieldsInfoUpdate = $this->getListFieldInfo($this->getFields(), ['filter'=>['ignoredAttributes'=>[Attributes::Hidden, Attributes::ReadOnly, Attributes::Immutable], 'skipFields'=>['ID']]]);
217
218 $result['ORDER'] = $this->internalizeFields($fields['ORDER'],
219 $this->isNewItem($fields['ORDER'])? $listFieldsInfoAdd:$listFieldsInfoUpdate
220 );
221
222 if(isset($fields['ORDER']['BASKET_ITEMS']))
223 {
224 $result['ORDER']['BASKET_ITEMS'] = $basketItem->internalizeFieldsModify($fields)['ORDER']['BASKET_ITEMS'];
225 }
226
227 if(isset($fields['ORDER']['PROPERTY_VALUES']))
228 {
229 $result['ORDER']['PROPERTY_VALUES'] = $propertyValue->internalizeFieldsModify($fields)['ORDER']['PROPERTY_VALUES'];
230 }
231
232 if(isset($fields['ORDER']['PAYMENTS']))
233 {
234 $result['ORDER']['PAYMENTS'] = $payment->internalizeFieldsModify($fields)['ORDER']['PAYMENTS'];
235 }
236
237 if(isset($fields['ORDER']['SHIPMENTS']))
238 {
239 $result['ORDER']['SHIPMENTS'] = $shipment->internalizeFieldsModify($fields)['ORDER']['SHIPMENTS'];
240 }
241
242 if(isset($fields['ORDER']['TRADE_BINDINGS']))
243 {
244 $result['ORDER']['TRADE_BINDINGS'] = $tradeBinding->internalizeFieldsModify($fields)['ORDER']['TRADE_BINDINGS'];
245 }
246
247 return $result;
248 }
249
250 public function convertKeysToSnakeCaseArguments($name, $arguments)
251 {
252 if($name == 'import')
253 {
254 if(isset($arguments['fields']))
255 {
256 $fields = $arguments['fields'];
257 if(!empty($fields))
258 $arguments['fields'] = $this->convertKeysToSnakeCaseFields($fields);
259 }
260 }
261 return $arguments;
262 }
263
264 public function internalizeArguments($name, $arguments)
265 {
266 if($name == 'getdeliveryidlist'
267 || $name == 'getpayments'
268 || $name == 'getpaysystemidlist'
269 || $name == 'getprintedchecks'
270 || $name == 'getshipments'
271 || $name == 'getbasket'
272 || $name == 'getpropertyvalues'
273 || $name == 'getcurrency'
274 || $name == 'getdateinsert'
275 || $name == 'getapplydiscount'
276 || $name == 'getpersontypeid'
277 || $name == 'getprice'
278 || $name == 'getproperties'
279 || $name == 'getsiteid'
280 || $name == 'getsumpaid'
281 || $name == 'gettaxlist'
282 || $name == 'gettaxlocation'
283 || $name == 'gettaxprice'
284 || $name == 'gettaxvalue'
285 || $name == 'getuserid'
286 || $name == 'getvatrate'
287 || $name == 'getvatsum'
288 || $name == 'iscanceled'
289 || $name == 'isexternal'
290 || $name == 'ismarked'
291 || $name == 'ispaid'
292 || $name == 'isshipped'
293 || $name == 'isusedvat'
294 || $name == 'importdelete'
295 ){}
296 elseif($name == 'import')
297 {
298 $fields = $arguments['fields'];
299 if(!empty($fields))
300 $arguments['fields'] = $this->internalizeFieldsImport($fields);
301 }
302 else
303 {
304 parent::internalizeArguments($name, $arguments);
305 }
306
307 return $arguments;
308 }
309
310 protected function internalizeFieldsImport($fields)
311 {
312 $result = [];
313
314 $payment = new Payment();
315 $shipment = new Shipment();
316 $shipmentItem = new ShipmentItem();
317 $basketItem = new BasketItem();
318 $tradeBinding = new TradeBinding();
319 $propertyValue = new PropertyValue();
320 $basketProperties = new BasketProperties();
321
322
323 if(isset($fields['ORDER']))
324 {
325 $result['ORDER'] = $this->internalizeFields($fields['ORDER'], $this->getFields());//only for importAction PERSON_TYPE_XML_ID, STATUS_XML_ID
326 }
327
328 if(isset($fields['ORDER']['BASKET_ITEMS']))
329 {
330 $result['ORDER']['BASKET_ITEMS'] = $basketItem->internalizeListFields($fields['ORDER']['BASKET_ITEMS']);
331
332 foreach ($fields['ORDER']['BASKET_ITEMS'] as $k=>$items)
333 {
334 if(isset($items['PROPERTIES']))
335 {
336 $result['ORDER']['BASKET_ITEMS'][$k]['PROPERTIES'] = $basketProperties->internalizeListFields($items['PROPERTIES']);
337 }
338 }
339 }
340
341 if(isset($fields['ORDER']['PROPERTY_VALUES']))
342 {
343 $result['ORDER']['PROPERTY_VALUES'] = $propertyValue->internalizeListFields($fields['ORDER']['PROPERTY_VALUES']);//only for importAction ORDER_PROPS_XML_ID
344 }
345
346 if(isset($fields['ORDER']['PAYMENTS']))
347 {
348 $result['ORDER']['PAYMENTS'] = $payment->internalizeListFields($fields['ORDER']['PAYMENTS']);//only for importAction PAY_SYSTEMS_XML_ID
349 }
350
351 if(isset($fields['ORDER']['SHIPMENTS']))
352 {
353 $result['ORDER']['SHIPMENTS'] = $shipment->internalizeListFields($fields['ORDER']['SHIPMENTS']);//only for importAction DELIVERY_XML_ID, STATUS_XML_ID
354
355 foreach ($fields['ORDER']['SHIPMENTS'] as $k=>$items)
356 {
357 if(isset($items['SHIPMENT_ITEMS']))
358 {
359 $result['ORDER']['SHIPMENTS'][$k]['SHIPMENT_ITEMS'] = $shipmentItem->internalizeListFields($items['SHIPMENT_ITEMS']);
360 }
361 }
362 }
363
364 if(isset($fields['ORDER']['TRADE_BINDINGS']))
365 {
366 $result['ORDER']['TRADE_BINDINGS'] = $tradeBinding->internalizeListFields($fields['ORDER']['TRADE_BINDINGS']);//only for importAction TRADING_PLATFORM_XML_ID
367 }
368
369 return $result;
370 }
371
372 protected function getRewritedFields()
373 {
374 return [
375 'PERSON_TYPE_XML_ID'=>[
376 'REFERENCE_FIELD'=>'PERSON_TYPE.XML_ID'
377 ],
378 'STATUS_XML_ID'=>[
379 'REFERENCE_FIELD'=>'STATUS_TABLE.XML_ID'
380 ]
381 ];
382 }
383
384 public function externalizeFields($fields)
385 {
386 $basketItem = new \Bitrix\Sale\Rest\Entity\BasketItem();
387 $payment = new \Bitrix\Sale\Rest\Entity\Payment();
388 $shipment = new \Bitrix\Sale\Rest\Entity\Shipment();
389 $shipmentItem = new \Bitrix\Sale\Rest\Entity\ShipmentItem();
390 $tradeBinding = new TradeBinding();
391 $propertyValue = new PropertyValue();
392 $basketProperties = new \Bitrix\Sale\Rest\Entity\BasketProperties();
393
394 $result = parent::externalizeFields($fields);
395
396 if(isset($fields['PROPERTY_VALUES']) && count($fields['PROPERTY_VALUES'])>0)
397 {
398 $result['PROPERTY_VALUES'] = $propertyValue->externalizeListFields($fields['PROPERTY_VALUES']);
399 }
400
401 if(isset($fields['BASKET_ITEMS']) && count($fields['BASKET_ITEMS'])>0)
402 {
403 foreach ($fields['BASKET_ITEMS'] as $k=>$item)
404 {
405 $result['BASKET_ITEMS'][$k] = $basketItem->externalizeFields($item);
406 //$result['BASKET_ITEMS'][$k]['PROPERTIES'] = $basketProperties->externalizeListFields($item['PROPERTIES']);
407 }
408 }
409
410 if(isset($fields['PAYMENTS']) && count($fields['PAYMENTS'])>0)
411 {
412 $result['PAYMENTS'] = $payment->externalizeListFields($fields['PAYMENTS']);
413 }
414
415 if(isset($fields['SHIPMENTS']) && count($fields['SHIPMENTS'])>0)
416 {
417 foreach($fields['SHIPMENTS'] as $k=>$item)
418 {
419 $result['SHIPMENTS'][$k] = $shipment->externalizeFields($item);
420 /*if(isset($item['SHIPMENT_ITEMS']))
421 {
422 $data['SHIPMENTS'][$k]['SHIPMENT_ITEMS'] = $shipmentItem->externalizeListFields($item['SHIPMENT_ITEMS']);
423 }*/
424 }
425 }
426
427 if(isset($fields['TRADE_BINDINGS']) && count($fields['TRADE_BINDINGS'])>0)
428 $result['TRADE_BINDINGS'] = $tradeBinding->externalizeListFields($fields['TRADE_BINDINGS']);
429
430 return $result;
431 }
432
433 public function externalizeFieldsModify($fields)
434 {
435 return $this->externalizeFields($fields);
436 }
437
438 public function externalizeFieldsTryModify($fields)
439 {
440 $result = parent::externalizeFieldsTryModify($fields);
441
442 if(isset($fields['PAYMENTS']))
443 {
444 foreach ($fields['PAYMENTS'] as $k=>$payment)
445 {
446 if(isset($payment['LIST_PAY_SYSTEM_WITH_RESTRICTIONS']))
447 {
448 $result['PAYMENTS'][$k]['LIST_PAY_SYSTEM_WITH_RESTRICTIONS'] = $payment['LIST_PAY_SYSTEM_WITH_RESTRICTIONS'];
449 }
450 }
451 }
452
453 if(isset($fields['SHIPMENTS']))
454 {
455 foreach ($fields['SHIPMENTS'] as $k=>$shipments)
456 {
457 if(isset($shipments['LIST_DELIIVERY_SERVICES_RESTRICTIONS']))
458 {
459 $result['SHIPMENTS'][$k]['LIST_DELIIVERY_SERVICES_RESTRICTIONS'] = $shipments['LIST_DELIIVERY_SERVICES_RESTRICTIONS'];
460 }
461 }
462 }
463
464 return $result;
465 }
466
467 public function externalizeResult($name, $fields)
468 {
469 if($name == 'getdeliveryidlist'
470 || $name == 'getpaysystemidlist'
471 || $name == 'getprintedchecks'
472 ){}
473 elseif ($name == 'getbasket')
474 {
475 $basketItem = new BasketItem();
476 $fields = $basketItem->externalizeListFields($fields);
477 }
478 elseif ($name == 'getpayments')
479 {
480 $payment = new Payment();
481 $fields = $payment->externalizeListFields($fields);
482 }
483 elseif ($name == 'getshipments')
484 {
485 $shipment = new Shipment();
486 $fields = $shipment->externalizeListFields($fields);
487 }
488 elseif ($name == 'getpropertyvalues')
489 {
490 $propertyValue = new PropertyValue();
491 $fields = $propertyValue->externalizeListFields($fields);
492 }
493 elseif($name == 'import')
494 {
495 $fields = $this->externalizeFieldsModify($fields);
496 }
497 else
498 {
499 $fields = parent::externalizeResult($name, $fields);
500 }
501
502 return $fields;
503 }
504
505 public function checkRequiredFieldsModify($fields)
506 {
507 $r = new Result();
508
509 $payment = new Payment();
510 $shipment = new Shipment();
511 $basketItem = new BasketItem();
512 $propertyValue = new PropertyValue();
513 $tradeBinding = new TradeBinding();
514
515 $listFieldsInfoAdd = $this->getListFieldInfo($this->getFields(), ['filter'=>['ignoredAttributes'=>[Attributes::Hidden, Attributes::ReadOnly]]]);
516 $listFieldsInfoUpdate = $this->getListFieldInfo($this->getFields(), ['filter'=>['ignoredAttributes'=>[Attributes::Hidden, Attributes::ReadOnly, Attributes::Immutable], 'skipFields'=>['ID']]]);
517
518 $required = $this->checkRequiredFields($fields['ORDER'],
519 $this->isNewItem($fields['ORDER'])? $listFieldsInfoAdd:$listFieldsInfoUpdate
520 );
521 if($required->isSuccess() == false)
522 {
523 $r->addError(new Error(implode(', ', $required->getErrorMessages()).'.'));
524 }
525
526 $required = $propertyValue->checkRequiredFieldsModify($fields);
527 if($required->isSuccess() == false)
528 {
529 $r->addError(new Error(implode(', ', $required->getErrorMessages())));
530 }
531
532 if(isset($fields['ORDER']['BASKET_ITEMS']))
533 {
534 $required = $basketItem->checkRequiredFieldsModify($fields);
535 if($required->isSuccess() == false)
536 {
537 $r->addError(new Error(implode(', ', $required->getErrorMessages())));
538 }
539 }
540
541 if(isset($fields['ORDER']['PAYMENTS']))
542 {
543 $required = $payment->checkRequiredFieldsModify($fields);
544 if($required->isSuccess() == false)
545 {
546 $r->addError(new Error(implode(', ', $required->getErrorMessages())));
547 }
548 }
549
550 if(isset($fields['ORDER']['SHIPMENTS']))
551 {
552 $required = $shipment->checkRequiredFieldsModify($fields);
553 if($required->isSuccess() == false)
554 {
555 $r->addError(new Error(implode(', ', $required->getErrorMessages())));
556 }
557 }
558
559 if(isset($fields['ORDER']['TRADE_BINDINGS']))
560 {
561 $required = $tradeBinding->checkRequiredFieldsModify($fields);
562 if($required->isSuccess() == false)
563 {
564 $r->addError(new Error(implode(', ', $required->getErrorMessages())));
565 }
566 }
567
568 return $r;
569 }
570}
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)
Definition order.php:264
externalizeFieldsTryModify($fields)
Definition order.php:438
convertKeysToSnakeCaseArguments($name, $arguments)
Definition order.php:250
internalizeFieldsModify($fields, $fieldsInfo=[])
Definition order.php:204
externalizeResult($name, $fields)
Definition order.php:467