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