Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
shipment.php
1<?php
2
3
5
6
12use Bitrix\Sale;
15
16class Shipment extends Controller
17{
18 public function getPrimaryAutoWiredParameter()
19 {
20 return new ExactParameter(
21 Sale\Shipment::class,
22 'shipment',
23 function($className, $id) {
24
25 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
26
28 $shipmentClass = $registry->getShipmentClassName();
29
30 $r = $shipmentClass::getList([
31 'select'=>['ORDER_ID'],
32 'filter'=>['ID'=>$id],
33 ]);
34
35 if($row = $r->fetch())
36 {
38 $orderClass = $registry->getOrderClassName();
39
40 $order = $orderClass::load($row['ORDER_ID']);
41 $shipment = $order->getShipmentCollection()->getItemById($id);
42 if ($shipment)
43 {
44 return $shipment;
45 }
46 }
47 else
48 {
49 $this->addError(new Error('shipment is not exists', 201140400001));
50 }
51 return null;
52 }
53 );
54 }
55
56 //region Actions
57 public function getFieldsAction()
58 {
59 $entity = new \Bitrix\Sale\Rest\Entity\Shipment();
60 return ['SHIPMENT'=>$entity->prepareFieldInfos(
61 $entity->getFields()
62 )];
63 }
64
65 public function modifyAction($fields)
66 {
67 $builder = $this->getBuilder();
68 $builder->buildEntityShipments($fields);
69
70 if($builder->getErrorsContainer()->getErrorCollection()->count()>0)
71 {
72 $this->addErrors($builder->getErrorsContainer()->getErrors());
73 return null;
74 }
75
76 $order = $builder->getOrder();
77
78 $r = $order->save();
79 if(!$r->isSuccess())
80 {
81 $this->addErrors($r->getErrors());
82 return null;
83 }
84 elseif ($r->hasWarnings())
85 {
86 $this->addErrors($r->getWarnings());
87 return null;
88 }
89
90 //TODO: return $shipment->toArray();
91 return ['SHIPMENTS'=>$this->toArray($order)['ORDER']['SHIPMENTS']];
92 }
93
94 public function addAction(array $fields)
95 {
96 $data = [];
97
98 $data['ORDER']['ID'] = $fields['ORDER_ID'];
99 $data['ORDER']['SHIPMENTS'] = [$fields];
100
101 $builder = $this->getBuilder(
103 'deleteShipmentIfNotExists' => false,
104 'deleteShipmentItemIfNotExists' => false,
105 ])
106 );
107 $builder->buildEntityShipments($data);
108
109 if($builder->getErrorsContainer()->getErrorCollection()->count()>0)
110 {
111 $this->addErrors($builder->getErrorsContainer()->getErrors());
112 return null;
113 }
114
115 $order = $builder->getOrder();
116
117 $idx=0;
118 $collection = $order->getShipmentCollection();
120 foreach($collection as $shipment)
121 {
122 if($shipment->getId() <= 0)
123 {
124 $idx = $shipment->getInternalIndex();
125 break;
126 }
127 }
128
129 $r = $order->save();
130 if(!$r->isSuccess())
131 {
132 $this->addErrors($r->getErrors());
133 return null;
134 }
135 elseif ($r->hasWarnings())
136 {
137 $this->addErrors($r->getWarnings());
138 return null;
139 }
140
142 $entity = $order->getShipmentCollection()->getItemByIndex($idx);
143 return ['SHIPMENT'=>$this->get($entity)];
144 }
145
146 public function updateAction(\Bitrix\Sale\Shipment $shipment, array $fields)
147 {
148 $data = [];
149
150 $fields['ID'] = $shipment->getId();
151 $fields['ORDER_ID'] = $shipment->getParentOrderId();
152
153 $data['ORDER']['ID'] = $fields['ORDER_ID'];
154 $data['ORDER']['SHIPMENTS'] = [$fields];
155
156 $builder = $this->getBuilder(
157 new SettingsContainer([
158 'deleteShipmentIfNotExists' => false,
159 'deleteShipmentItemIfNotExists' => false,
160 ])
161 );
162 $builder->buildEntityShipments($data);
163
164 if($builder->getErrorsContainer()->getErrorCollection()->count()>0)
165 {
166 $this->addErrors($builder->getErrorsContainer()->getErrors());
167 return null;
168 }
169
170 $order = $builder->getOrder();
171
172 $r = $order->save();
173 if(!$r->isSuccess())
174 {
175 $this->addErrors($r->getErrors());
176 return null;
177 }
178 elseif($r->hasWarnings())
179 {
180 $this->addErrors($r->getWarnings());
181 return null;
182 }
183
185 $entity = $order->getShipmentCollection()->getItemById($shipment->getId());
186 return ['SHIPMENT'=>$this->get($entity)];
187 }
188
189 public function deleteAction(\Bitrix\Sale\Shipment $shipment)
190 {
191 $r = $shipment->delete();
192 return $this->save($shipment, $r);
193 }
194
195 public function getAction(\Bitrix\Sale\Shipment $shipment)
196 {
197 return ['SHIPMENT'=>$this->get($shipment)];
198 }
199
200 public function listAction(PageNavigation $pageNavigation, array $select = [], array $filter = [], array $order = []): Page
201 {
202 $select = empty($select)? ['*']:$select;
203 $order = empty($order)? ['ID'=>'ASC']:$order;
204 $filter['!SYSTEM'] = 'Y';
205
206 $runtime = [
207 new \Bitrix\Main\Entity\ReferenceField(
208 'STATUS_TABLE',
209 '\Bitrix\Sale\Internals\StatusTable',
210 array('=this.STATUS_ID' => 'ref.ID')
211 ),
212 new \Bitrix\Main\Entity\ReferenceField(
213 'DELIVERY',
214 '\Bitrix\Sale\Delivery\Services\Table',
215 array('=this.DELIVERY_ID' => 'ref.ID')
216 ),
217 ];
218
219 $shipments = \Bitrix\Sale\Shipment::getList(
220 [
221 'select' => $select,
222 'filter' => $filter,
223 'order' => $order,
224 'offset' => $pageNavigation->getOffset(),
225 'limit' => $pageNavigation->getLimit(),
226 'runtime' => $runtime,
227 ]
228 )->fetchAll();
229
230 return new Page('SHIPMENTS', $shipments, function() use ($select, $filter, $runtime)
231 {
232 return count(
233 \Bitrix\Sale\Shipment::getList(['select'=>$select, 'filter'=>$filter, 'runtime'=>$runtime])->fetchAll()
234 );
235 });
236 }
237
238 public function getAllowDeliveryDateAction(\Bitrix\Sale\Shipment $shipment)
239 {
240 return $shipment->getAllowDeliveryDate();
241 }
242
244 {
245 return $shipment->getAllowDeliveryUserId();
246 }
247
248 public function getCompanyIdAction(\Bitrix\Sale\Shipment $shipment)
249 {
250 return $shipment->getCompanyId();
251 }
252
253 public function getCurrencyAction(\Bitrix\Sale\Shipment $shipment)
254 {
255 return $shipment->getCurrency();
256 }
257
258 public function getDeliveryIdAction(\Bitrix\Sale\Shipment $shipment)
259 {
260 return $shipment->getDeliveryId();
261 }
262
263 public function getDeliveryNameAction(\Bitrix\Sale\Shipment $shipment)
264 {
265 return $shipment->getDeliveryName();
266 }
267
268 public function getParentOrderIdAction(\Bitrix\Sale\Shipment $shipment)
269 {
270 return $shipment->getParentOrderId();
271 }
272
273 public function getPersonTypeIdAction(\Bitrix\Sale\Shipment $shipment)
274 {
275 return $shipment->getPersonTypeId();
276 }
277
278 public function getPriceAction(\Bitrix\Sale\Shipment $shipment)
279 {
280 return $shipment->getPrice();
281 }
282
283 public function getShippedDateAction(\Bitrix\Sale\Shipment $shipment)
284 {
285 return $shipment->getShippedDate();
286 }
287
288 public function getShippedUserIdAction(\Bitrix\Sale\Shipment $shipment)
289 {
290 return $shipment->getShippedUserId();
291 }
292
293 public function getStoreIdAction(\Bitrix\Sale\Shipment $shipment)
294 {
295 return $shipment->getStoreId();
296 }
297
298 public function getUnshipReasonAction(\Bitrix\Sale\Shipment $shipment)
299 {
300 $shipment->getUnshipReason();
301 }
302
303 public function getVatRateAction(\Bitrix\Sale\Shipment $shipment)
304 {
305 return $shipment->getVatRate();
306 }
307
308 public function getVatSumAction(\Bitrix\Sale\Shipment $shipment)
309 {
310 return $shipment->getVatSum();
311 }
312
313 public function getWeightAction(\Bitrix\Sale\Shipment $shipment)
314 {
315 return $shipment->getWeight();
316 }
317
318 public function isAllowDeliveryAction(\Bitrix\Sale\Shipment $shipment)
319 {
320 return $shipment->isAllowDelivery()? 'Y':'N';
321 }
322
323 public function isCanceledAction(\Bitrix\Sale\Shipment $shipment)
324 {
325 return $shipment->isCanceled()? 'Y':'N';
326 }
327
328 public function isCustomPriceAction(\Bitrix\Sale\Shipment $shipment)
329 {
330 return $shipment->isCustomPrice()? 'Y':'N';
331 }
332
333 public function isEmptyAction(\Bitrix\Sale\Shipment $shipment)
334 {
335 return $shipment->isEmpty()? 'Y':'N';
336 }
337
338 public function isMarkedAction(\Bitrix\Sale\Shipment $shipment)
339 {
340 return $shipment->isMarked()? 'Y':'N';
341 }
342
343 public function isReservedAction(\Bitrix\Sale\Shipment $shipment)
344 {
345 return $shipment->isReserved()? 'Y':'N';
346 }
347
348 public function isShippedAction(\Bitrix\Sale\Shipment $shipment)
349 {
350 return $shipment->isShipped()? 'Y':'N';
351 }
352
353 public function setBasePriceDeliveryAction(\Bitrix\Sale\Shipment $shipment, $value, $custom=false)
354 {
355 $r=$shipment->setBasePriceDelivery($value, $custom);
356 return $this->save($shipment, $r);
357 }
358
359 public function setShippedAction(\Bitrix\Sale\Shipment $shipment, $value)
360 {
361 $r = $shipment->setField('DEDUCTED', $value);
362 return $this->save($shipment, $r);
363 }
364 //endregion
365
366 private function save(\Bitrix\Sale\Shipment $shipment, Result $r)
367 {
368 if(!$r->isSuccess())
369 {
370 $this->addErrors($r->getErrors());
371 return null;
372 }
373 else
374 {
376 $collection = $shipment->getCollection();
377 $r = $collection->getOrder()->save();
378 if(!$r->isSuccess())
379 {
380 $this->addErrors($r->getErrors());
381 return null;
382 }
383 }
384
385 return $r->isSuccess();
386 }
387
388 protected function get(\Bitrix\Sale\Shipment $shipment, array $fields=[])
389 {
390 $shipments = $this->toArray($shipment->getCollection()->getOrder(), $fields)['ORDER']['SHIPMENTS'];
391 foreach ($shipments as $item)
392 {
393 if($item['ID']==$shipment->getId())
394 {
395 return $item;
396 }
397 }
398 return [];
399 }
400
401 static public function prepareFields($fields)
402 {
403 $data=null;
404
405 if(isset($fields["SHIPMENTS"]) && is_array($fields["SHIPMENTS"]))
406 {
407 foreach($fields['SHIPMENTS'] as $k=>$shipmentFormData)
408 {
409 $data[$k] = $shipmentFormData;
410 if(isset($shipmentFormData['SHIPMENT_ITEMS']))
411 {
412 unset($data[$k]['SHIPMENT_ITEMS']);
413
414 $i=0;
415 foreach($shipmentFormData['SHIPMENT_ITEMS'] as $item)
416 {
417 $shipmentItem = [];
418
419 if(isset($item['ID']) && intval($item['ID'])>0)
420 {
421 $shipmentItem['ORDER_DELIVERY_BASKET_ID'] = intval($item['ID']);
422 }
423 else
424 {
425 if(isset($item['BASKET_ID']))
426 $shipmentItem['BASKET_CODE'] = $item['BASKET_ID'];
427 }
428
429 if(isset($item['XML_ID']))
430 $shipmentItem['XML_ID'] = $item['XML_ID'];
431
432 $shipmentItem['AMOUNT'] = $item['QUANTITY'];
433
434 //$basketCode = $item['BASKET_ID'];
435 //unset($item['BASKET_ID']);
436
437 //region fill Id - ShipmentItemStore
438 $storesInfo = isset($item['BARCODE_INFO'])? $item['BARCODE_INFO']:[];
439 foreach($storesInfo as &$storeInfo)
440 {
441 if(isset($storeInfo['BARCODE']))
442 {
443 foreach ($storeInfo['BARCODE'] as &$barCode)
444 $barCode['ID'] = isset($barCode['ID'])?$barCode['ID']:0;
445 }
446 }
447 //endregion
448/*
449 $data[$k]['PRODUCT'][$basketCode] = [
450 'AMOUNT'=>$item['QUANTITY'],
451 'BASKET_CODE'=>$basketCode,
452 'XML_ID'=>$item['XML_ID'],
453 'BARCODE_INFO'=> $storesInfo
454 ];
455*/
456 $data[$k]['PRODUCT'][] = $shipmentItem + ['BARCODE_INFO'=> $storesInfo];
457 }
458 }
459 else
460 {
461 $data[$k]['PRODUCT'] = false;
462 }
463 }
464 }
465
466 return is_array($data)?['SHIPMENT'=>$data]:[];
467 }
468
469 protected function checkPermissionEntity($name)
470 {
471 if($name == 'getallowdeliverydate'
472 || $name == 'getallowdeliveryuserid'
473 || $name == 'getcompanyid'
474 || $name == 'getcurrency'
475 || $name == 'getdeliveryid'
476 || $name == 'getdeliveryname'
477 || $name == 'getparentorderid'
478 || $name == 'getpersontypeid'
479 || $name == 'getprice'
480 || $name == 'getphippeddate'
481 || $name == 'getstoreid'
482 || $name == 'getunshipreason'
483 || $name == 'getvatrate'
484 || $name == 'getvatsum'
485 || $name == 'getweight'
486 || $name == 'getshippeddate'
487 || $name == 'getshippeduserid'
488 || $name == 'isallowdelivery'
489 || $name == 'iscanceled'
490 || $name == 'iscustomprice'
491 || $name == 'isempty'
492 || $name == 'ismarked'
493 || $name == 'isreserved'
494 || $name == 'isshipped'
495 )
496 {
497 $r = $this->checkReadPermissionEntity();
498 }
499 elseif($name == 'setbasepricedelivery'
500 || $name == 'setshipped'
501 )
502 {
503 $r = $this->checkModifyPermissionEntity();
504 }
505 else
506 {
507 $r = parent::checkPermissionEntity($name);
508 }
509 return $r;
510 }
511}
getWeightAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:313
isMarkedAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:338
setBasePriceDeliveryAction(\Bitrix\Sale\Shipment $shipment, $value, $custom=false)
Definition shipment.php:353
getAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:195
getAllowDeliveryDateAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:238
getAllowDeliveryUserIdAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:243
getCompanyIdAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:248
getVatRateAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:303
getShippedUserIdAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:288
getVatSumAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:308
isCanceledAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:323
getUnshipReasonAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:298
isCustomPriceAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:328
listAction(PageNavigation $pageNavigation, array $select=[], array $filter=[], array $order=[])
Definition shipment.php:200
isEmptyAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:333
getStoreIdAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:293
getPersonTypeIdAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:273
isShippedAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:348
deleteAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:189
isReservedAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:343
getDeliveryIdAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:258
getCurrencyAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:253
getDeliveryNameAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:263
getParentOrderIdAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:268
setShippedAction(\Bitrix\Sale\Shipment $shipment, $value)
Definition shipment.php:359
getPriceAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:278
isAllowDeliveryAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:318
getShippedDateAction(\Bitrix\Sale\Shipment $shipment)
Definition shipment.php:283
static getList(array $parameters)