Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
orderbasketshipment.php
1<?php
2
4
14use Bitrix\Main\Entity\EntityError;
16
17Loc::loadMessages(__FILE__);
18
19require_once __DIR__.'/../../../../general/admin_tool.php';
20
22{
23 protected $shipment = null;
24 protected static $useStoreControl = null;
25 protected $systemJsObjName = 'BX.Sale.Admin.SystemShipmentBasketObj';
26
33 public function __construct(Shipment $shipment, $jsObjName = "", $idPrefix = "")
34 {
35 self::$useStoreControl = Configuration::useStoreControl();
36 $order = $shipment->getCollection()->getOrder();
37 $this->visibleColumns = $this->getVisibleColumns('sale_shipment_basket');
38 parent::__construct($order, $jsObjName, $idPrefix);
39
40 $this->shipment = $shipment;
41 $this->data = array();
42 $this->mode = self::VIEW_MODE;
43 }
44
45 public function getEdit($defTails = false)
46 {
47 $result = '
48 <script>
49 function searchProductByBarcode(_this)
50 {
51 event = window.event;
52 if(event.keyCode == 13)
53 {
54 '.$this->jsObjName.'.checkProductByBarcode(_this.nextElementSibling);
55 event.preventDefault();
56 }
57 }
58 </script>
59 <div class="adm-s-gray-title" style="padding-right: 2px;">
60 '.Loc::getMessage("SALE_ORDER_SHIPMENT_BASKET_COMPOSITION").'
61 <div class="adm-s-gray-title-btn-container">
62 <span
63 class="adm-btn adm-btn-green adm-btn-add"
64 onClick="'.$this->systemJsObjName.'.addProductSearch();"
65 >'.
66 Loc::getMessage("SALE_ORDER_BASKET_PRODUCT_ADD").
67 '</span>
68 </div>
69 <div class="adm-s-gray-title-btn-container" style="margin-right: 25px;">
70 <span class="adm-bus-order-find-by-barcode" onclick="BX.Sale.Admin.GeneralShipment.findProductByBarcode(this);">'.Loc::getMessage('SALE_ORDER_SHIPMENT_BASKET_FIND_BY_BARCODE').'</span>
71 </div>
72 <div class="adm-s-gray-title-btn-container" style="margin-right: 25px; display: none;">
73 <input type="text" style="width: 150px;" onkeypress="searchProductByBarcode(this);">
74 <span class="adm-btn" onclick="'.$this->jsObjName.'.checkProductByBarcode(this);">'.Loc::getMessage('SALE_ORDER_SHIPMENT_BASKET_FIND').'</span>
75 </div>
76 <div class="clb"></div>
77 </div>';
78
79 $result .= '
80 <div class="adm-s-order-table-ddi">
81 <table class="adm-s-order-table-ddi-table" style="width: 100%;" id="'.$this->idPrefix.'sale_order_edit_product_table">
82 <thead>
83 <tr>
84 <td class="adm-s-order-table-context-menu-column">
85 <span class="adm-s-order-table-title-icon"
86 title="'.Loc::getMessage("SALE_ORDER_BASKET_SETTINGS_BUTTON_TITLE").'"
87 onclick="'.$this->jsObjName.'.onHeadMenu(this);"
88 >
89 </span>
90 </td>
91 <td></td>';
92
93 foreach($this->visibleColumns as $id => $name)
94 if ($id == 'STORE')
95 $result .= "<td style='width: 200px'>".$name."</td>";
96 else
97 $result .= "<td>".$name."</td>";
98
99 $result .= '</tr>
100 </thead>
101 </table>
102 </div>';
103
104 $result .= '<div class="adm-list-table-footer" id="b_sale_order_shipment_footer" style="margin-top: -5px; padding-top: 10px;">
105 <span class="adm-selectall-wrap" style="margin-top: 5px; font-weight: bold;">'.Loc::getMessage('SALE_ORDER_SHIPMENT_BASKET_GOODS_ALL').': </span><span class="adm-selectall-wrap" style="margin-top: 5px; font-weight: bold;" id="'.$this->idPrefix.'_count">0</span>
106 <span class="adm-selectall-wrap" style="margin-top: 5px; font-weight: bold;">'.Loc::getMessage('SALE_ORDER_SHIPMENT_BASKET_GOODS_SELECTED').': </span><span class="adm-selectall-wrap" style="margin-top: 5px; font-weight: bold;" id="'.$this->idPrefix.'_selected_count">0</span>
107 <span class="adm-selectall-wrap" style="margin-top: 5px; font-weight: bold;">'.str_replace(array('#CURRENT_PAGE#', '#COUNT_PAGE#'), array('1', '1'),Loc::getMessage('SALE_ORDER_SHIPMENT_BASKET_PAGE')).'</span>
108 </div>';
109
110 $result .= '<div class="adm-list-table-footer" id="b_sale_order_shipment_footer">
111 <span class="adm-selectall-wrap">
112 <input type="checkbox" class="adm-checkbox adm-designed-checkbox" name="action_target" value="selected" id="action_target" '.($this->shipment->isShipped() ? 'disabled' : '').'>
113 <label for="action_target" class="adm-checkbox adm-designed-checkbox-label"></label>
114 <label title="'.Loc::getMessage('SALE_ORDER_SHIPMENT_BASKET_APPLY').'" for="action_target" class="adm-checkbox-label">'.Loc::getMessage('SALE_ORDER_SHIPMENT_BASKET_APPLY_FOR_ALL').'</label>
115 </span>
116
117 <span class="adm-table-item-edit-wrap adm-table-item-edit-single">
118 <a href="javascript:void(0);" class="adm-table-btn-delete adm-edit-disable" hidefocus="true" title="" id="action_delete_button" onclick="'.$this->jsObjName.'.groupMoveToSystemBasket(this);"></a>
119 </span>
120 </div>';
121
122 return $result;
123 }
124
125 public function getView($index = 0)
126 {
127 $exceptionFields = array('QUANTITY', 'REMAINING_QUANTITY');
128 foreach ($exceptionFields as $field)
129 {
130 if (array_key_exists($field, $this->visibleColumns))
131 unset($this->visibleColumns[$field]);
132 }
133
134 $result = '
135 <div class="adm-s-order-table-ddi">
136 <table class="adm-s-order-table-ddi-table" style="width: 100%;" id="'.$this->idPrefix.'_'.$index.'">
137 <thead>
138 <tr>';
139
140 foreach ($this->visibleColumns as $id => $name)
141 {
142 if ($id == 'STORE')
143 $result .= "<td style='width: 200px'>".$name."</td>";
144 else
145 $result .= "<td>".$name."</td>";
146 }
147 $result .= '</tr>
148 </thead>
149 </table>
150 </div>';
151
152 $result .= $this->getViewScript($index, $this->visibleColumns);
153 return $result;
154 }
155
156 protected static function getDefaultVisibleColumns()
157 {
158 $columnName = array(
159 "NUMBER" => Loc::getMessage("SALE_ORDER_BASKET_SETTINGS_COL_NUMBER"),
160 "IMAGE" => Loc::getMessage("SALE_ORDER_BASKET_SETTINGS_COL_IMAGE"),
161 "NAME" => Loc::getMessage("SALE_ORDER_BASKET_SETTINGS_COL_NAME"),
162 "PROPS" => Loc::getMessage("SALE_ORDER_BASKET_SETTINGS_COL_PROPS"),
163 "QUANTITY" => Loc::getMessage("SALE_ORDER_BASKET_SETTINGS_COL_QUANTITY"),
164 "AMOUNT" => Loc::getMessage("SALE_ORDER_BASKET_SETTINGS_COL_AMOUNT")
165 );
166
167 if (self::$useStoreControl)
168 {
169 $columnName = array_merge(
170 $columnName,
171 array(
172 "STORE" => Loc::getMessage("SALE_ORDER_BASKET_SETTINGS_COL_STORE"),
173 "CUR_AMOUNT" => Loc::getMessage("SALE_ORDER_BASKET_SETTINGS_COL_CUR_AMOUNT"),
174 "REMAINING_QUANTITY" => Loc::getMessage("SALE_ORDER_BASKET_SETTINGS_COL_REMAINING_QUANTITY")
175 )
176 );
177 }
178
179 $columnName = array_merge(
180 $columnName,
181 array(
182 "BARCODE" => Loc::getMessage("SALE_ORDER_BASKET_SETTINGS_COL_BARCODE"),
183 )
184 );
185
186 return $columnName;
187 }
188
192 protected static function getDefaultUnShippedVisibleColumns()
193 {
194 return array(
195 "NUMBER" => Loc::getMessage("SALE_ORDER_BASKET_SETTINGS_COL_NUMBER"),
196 "IMAGE" => Loc::getMessage("SALE_ORDER_BASKET_SETTINGS_COL_IMAGE"),
197 "NAME" => Loc::getMessage("SALE_ORDER_BASKET_SETTINGS_COL_NAME"),
198 "PROPS" => Loc::getMessage("SALE_ORDER_BASKET_SETTINGS_COL_PROPS"),
199 "AMOUNT" => Loc::getMessage("SALE_ORDER_BASKET_SETTINGS_COL_QUANTITY")
200 );
201 }
202
207 public function prepareData(array $inParams = array())
208 {
209 $result = null;
211 $shipmentCollection = $this->shipment->getCollection();
212 $shipmentItemCollection = $this->shipment->getShipmentItemCollection();
213 $result = $this->getProductsInfo($shipmentItemCollection);
214 $result = $this->getOffersSkuParams($result);
215 $result['UNSHIPPED_PRODUCTS'] = array();
216
217 if ($this->shipment->getId() > 0)
218 {
220 $systemShipment = $shipmentCollection->getSystemShipment();
221 $systemItemsCollection = $systemShipment->getShipmentItemCollection();
222
223 $systemCollectionProduct = $this->getProductsInfo($systemItemsCollection);
224 $systemCollectionProduct = $this->getOffersSkuParams($systemCollectionProduct);
225 $result['UNSHIPPED_PRODUCTS'] = array_diff_key($systemCollectionProduct['ITEMS'], $result['ITEMS']);
226 }
227
228 return $result;
229 }
230
236 public function getProductsInfo($shipmentItemCollection)
237 {
239 $shipment = $shipmentItemCollection->getShipment();
240 $systemShipmentItemCollection = null;
241 $systemShipmentItem = null;
242
243 $result = array(
244 "ITEMS" => array()
245 );
246
247 if (!$shipment->isSystem())
248 {
250 $systemShipment = $shipment->getCollection()->getSystemShipment();
251 $systemShipmentItemCollection = $systemShipment->getShipmentItemCollection();
252 }
253
254 $items = array();
255
256 $catalogProductsIds = array();
257
259 foreach($shipmentItemCollection as $item)
260 {
261 $basketItem = $item->getBasketItem();
262
263 if ($basketItem && $basketItem->getField("MODULE") == "catalog")
264 $catalogProductsIds[] = $basketItem->getProductId();
265 }
266
267 if(!empty($catalogProductsIds))
268 $catalogProductsFields = self::getProductsData($catalogProductsIds, $this->order->getSiteId(), $this->visibleColumns, $this->order->getUserId());
269
271 foreach($shipmentItemCollection as $item)
272 {
273 $params = array();
274
275 $basketItem = $item->getBasketItem();
276 if ($basketItem)
277 {
278 $systemShipmentItem = null;
279 if ($systemShipmentItemCollection)
280 {
282 $systemShipmentItem = $systemShipmentItemCollection->getItemByBasketCode($basketItem->getBasketCode());
283 }
284
285 $productId = $basketItem->getProductId();
286
287 if ($basketItem->getField("MODULE") === "catalog" && !empty($catalogProductsFields[$productId]))
288 {
289 $params = $catalogProductsFields[$productId];
290 }
291
292 if (intval($basketItem->getField("MEASURE_CODE")) > 0)
293 {
294 $params["MEASURE_CODE"] = intval($basketItem->getField("MEASURE_CODE"));
295 }
296 elseif (!isset($params["MEASURE_CODE"]))
297 {
298 $params["MEASURE_CODE"] = 0;
299 }
300
301 if (!empty($basketItem->getField("MEASURE_NAME")))
302 {
303 $params["MEASURE_TEXT"] = $basketItem->getField("MEASURE_NAME");
304 }
305 elseif(!isset($params["MEASURE_TEXT"]))
306 {
307 $params["MEASURE_TEXT"] = "";
308 }
309
310 if ($basketItem->isBundleParent())
311 {
312 $params["BASE_ELEMENTS_QUANTITY"] = $basketItem->getBundleBaseQuantity();
313 if (!isset($params['IS_SET_ITEM']))
314 $params['IS_SET_ITEM'] = 'N';
315 if (!isset($params['IS_SET_PARENT']))
316 $params['IS_SET_PARENT'] = 'Y';
317 if (!isset($params['OLD_PARENT_ID']))
318 $params['OLD_PARENT_ID'] = '';
319 }
320 $params["BASKET_ID"] = $basketItem->getId();
321 $params["PRODUCT_PROVIDER_CLASS"] = $basketItem->getProvider();
322 $params["NAME"] = $basketItem->getField("NAME");
323 $params["MODULE"] = $basketItem->getField("MODULE");
324 $params['TYPE'] = (int)$basketItem->getField('TYPE');
325 $params['BARCODE_INFO'] = [];
326
327 $itemStoreCollection = $item->getShipmentItemStoreCollection();
328 if ($itemStoreCollection)
329 {
331 foreach ($itemStoreCollection as $barcode)
332 {
333 $storeId = $barcode->getStoreId();
334 if (!isset($params['BARCODE_INFO'][$storeId]))
335 {
336 $params['BARCODE_INFO'][$storeId] = [];
337 }
338
339 $params['BARCODE_INFO'][$storeId][] = [
340 'ID' => $barcode->getId(),
341 'BARCODE' => $barcode->getBarcode(),
342 'MARKING_CODE' => $barcode->getMarkingCode(),
343 'QUANTITY' => $barcode->getQuantity(),
344 ];
345 }
346 }
347
348 if (!$shipment->isSystem())
349 $params['ORDER_DELIVERY_BASKET_ID'] = $item->getId();
350
351 $systemItemQuantity = ($systemShipmentItem) ? $systemShipmentItem->getQuantity() : 0;
352 $params["QUANTITY"] = floatval($item->getQuantity() + $systemItemQuantity);
353 $params["AMOUNT"] = floatval($item->getQuantity());
354 $params["PRICE"] = $basketItem->getPrice();
355 $params["CURRENCY"] = $basketItem->getCurrency();
356 $params["PROPS"] = array();
357
359 foreach($basketItem->getPropertyCollection() as $property)
360 {
361 $params["PROPS"][] = array(
362 "VALUE" => $property->getField("VALUE"),
363 "NAME" => $property->getField("NAME"),
364 "CODE" => $property->getField("CODE"),
365 "SORT" => $property->getField("SORT")
366 );
367 }
368
369 if(\Bitrix\Main\Loader::includeModule("catalog"))
370 {
371 $productInfo = \CCatalogSku::GetProductInfo($productId);
372 if ($productInfo)
373 {
374 $params["OFFERS_IBLOCK_ID"] = $productInfo["OFFER_IBLOCK_ID"];
375 $params["IBLOCK_ID"] = $productInfo["IBLOCK_ID"];
376 $params["PRODUCT_ID"] = $productInfo["ID"];
377 }
378 }
379
380 $params["PARENT_BASKET_ID"] = $basketItem->getParentBasketItemId() ?? 0;
381
382 //If product became bundle, but in saved order it is a simple product.
383 if ($basketItem->getBasketCode() == intval($basketItem->getBasketCode()) && !$basketItem->isBundleParent() && !empty($params['SET_ITEMS']))
384 {
385 unset($params['SET_ITEMS'], $params['OLD_PARENT_ID']);
386 $params['IS_SET_PARENT'] = 'N';
387 }
388
389 $params['IS_SUPPORTED_MARKING_CODE'] = $basketItem->isSupportedMarkingCode() ? 'Y' : 'N';
390 }
391 else
392 {
393 $systemShipmentItem = null;
394 if ($systemShipmentItemCollection)
395 {
397 $systemShipmentItem = $systemShipmentItemCollection->getItemByBasketId($item->getBasketId());
398 }
399
400 $systemItemQuantity = ($systemShipmentItem) ? $systemShipmentItem->getQuantity() : 0;
401 $params = array(
402 'NAME' => Loc::getMessage("SALE_ORDER_SHIPMENT_BASKET_WRONG_BASKET_ITEM"),
403 'QUANTITY' => floatval($item->getQuantity() + $systemItemQuantity),
404 'AMOUNT' => floatval($item->getQuantity()),
405 'BASKET_ID' => $item->getBasketId(),
406 );
407 }
408
409 $items[$params['BASKET_ID']] = $params;
410 }
411
412 $bundleChild = array();
413 foreach ($items as $basketId => $item)
414 {
415 $parentBasketId = $item['PARENT_BASKET_ID'];
416 if ($parentBasketId > 0)
417 {
418 $item['IS_SET_ITEM'] = 'Y';
419 $item['OLD_PARENT_ID'] = $items[$parentBasketId]['OLD_PARENT_ID'];
420 $bundleChild[$parentBasketId][] = $item;
421 unset($items[$basketId]);
422 }
423 }
424
425 foreach ($items as $basketId => $item)
426 {
427 if (isset($bundleChild[$basketId]))
428 $items[$basketId]['SET_ITEMS'] = $bundleChild[$basketId];
429 }
430
431 $result['ITEMS'] = $items;
432
433 return $result;
434 }
435
441 public function modifyFromRequest($data, $request)
442 {
443 // recovery on delete
444 foreach ($data['ITEMS'] as $code => $item)
445 {
446 $basketCode = $item['BASKET_ID'];
447
448 if (!isset($request[$basketCode]))
449 {
450 $data['UNSHIPPED_PRODUCTS'][$code] = $data['ITEMS'][$code];
451 unset($data['ITEMS'][$code]);
452 }
453 }
454
455 // recovery on add
456 foreach ($data['UNSHIPPED_PRODUCTS'] as $code => $item)
457 {
458 $basketCode = $item['BASKET_ID'];
459
460 if (isset($request[$basketCode]))
461 {
462 $data['ITEMS'][$code] = $data['UNSHIPPED_PRODUCTS'][$code];
463 unset($data['UNSHIPPED_PRODUCTS'][$code]);
464 }
465 }
466
467 // recovery barcode info
468 if ($request)
469 {
470 foreach ($request as $basketCode => $product)
471 {
472 $basket = $this->order->getBasket();
474 $basketItem = $basket->getItemById($product['BASKET_ID']);
475 if ($basketItem && $basketItem->isBundleChild())
476 {
477 $parentBasketItem = $basketItem->getParentBasketItem();
478 foreach ($data['ITEMS'][$parentBasketItem->getId()]['SET_ITEMS'] as $id => $setItem)
479 {
480 if ($setItem['PRODUCT_ID'] == $product['PRODUCT_ID'])
481 {
482 $item = &$data['ITEMS'][$parentBasketItem->getId()]['SET_ITEMS'][$id];
483 break;
484 }
485 }
486 }
487 else
488 {
489 $item = &$data['ITEMS'][$product['BASKET_ID']];
490 }
491 $item['AMOUNT'] = $product['AMOUNT'];
492 $item['QUANTITY'] = $product['QUANTITY'];
493 if ($product['BARCODE_INFO'])
494 {
495 foreach ($product['BARCODE_INFO'] as $id => $info)
496 {
497 $storeId = $info['STORE_ID'];
498 if (!isset($item['BARCODE_INFO'][$storeId]))
499 {
500 $item['BARCODE_INFO'][$storeId] = array();
501 if ($basketItem->isBundleParent())
502 {
503 $item['BARCODE_INFO'][$storeId][0] = array();
504 $item['BARCODE_INFO'][$storeId][0]['QUANTITY'] = $info['QUANTITY'];
505 }
506 if ($info['BARCODE'])
507 {
508 foreach ($info['BARCODE'] as $barcode)
509 {
510 $item['BARCODE_INFO'][$storeId][] = array(
511 'ID' => (int)$barcode['ID'],
512 'BARCODE' => (string)$barcode['VALUE'],
513 'MARKING_CODE' => (string)$barcode['MARKING_CODE'],
514 'QUANTITY' => ($basketItem->isbarcodeMulti()) ? 1 : $info['QUANTITY']
515 );
516 }
517 }
518 }
519 else
520 {
521 foreach ($item['BARCODE_INFO'][$storeId] as &$barcodeInfo)
522 {
523 if ($info['BARCODE'])
524 {
525 $barcode = array_shift($info['BARCODE']);
526 $barcodeInfo['ID'] = (int)$barcode['ID'];
527 $barcodeInfo['BARCODE'] = (string)$barcode['VALUE'];
528 $barcodeInfo['MARKING_CODE'] = (string)$barcode['MARKING_CODE'];
529 }
530 $barcodeInfo['QUANTITY'] = ($basketItem->isbarcodeMulti()) ? 1 : $info['QUANTITY'];
531 }
532 unset($barcodeInfo);
533 }
534 }
535 }
536 }
537 }
538 unset($item);
539
540 return $data;
541 }
542
543 public function getScripts($recoveryData = array())
544 {
545 if(!static::$jsInited)
546 {
547 \Bitrix\Main\Page\Asset::getInstance()->addJs("/bitrix/js/sale/admin/order_basket.js");
548 \Bitrix\Main\Page\Asset::getInstance()->addJs("/bitrix/js/sale/admin/order_shipment_basket.js");
549 \Bitrix\Main\UI\Extension::load('sale.admin_order');
550 static::$jsInited = true;
551 }
552 $data = $this->prepareData();
553
554 if (!empty($recoveryData))
555 {
556 $data = $this->modifyFromRequest($data, $recoveryData['1']['PRODUCT']);
557 }
558
559 $keys = array_merge(array_keys($data["ITEMS"]), array_keys($data["UNSHIPPED_PRODUCTS"]));
560
561 $result = '
562 <script type="text/javascript">
563 BX.message({
564 SALE_ORDER_BASKET_ROW_SETTINGS: "'.\CUtil::JSEscape(Loc::getMessage("SALE_ORDER_BASKET_ROW_SETTINGS")).'",
565 SALE_ORDER_BASKET_PROD_MENU_ADD: "'.\CUtil::JSEscape(Loc::getMessage("SALE_ORDER_BASKET_PROD_MENU_ADD")).'",
566 SALE_ORDER_BASKET_PROD_MENU_DELETE: "'.\CUtil::JSEscape(Loc::getMessage("SALE_ORDER_BASKET_PROD_MENU_DELETE")).'",
567 SALE_ORDER_BASKET_TURN: "'.\CUtil::JSEscape(Loc::getMessage("SALE_ORDER_BASKET_TURN")).'",
568 SALE_ORDER_BASKET_EXPAND: "'.\CUtil::JSEscape(Loc::getMessage("SALE_ORDER_BASKET_EXPAND")).'",
569 SALE_ORDER_BASKET_NO_PICTURE: "'.\CUtil::JSEscape(Loc::getMessage("SALE_ORDER_BASKET_NO_PICTURE")).'",
570 SALE_ORDER_SHIPMENT_BASKET_SELECTED_PRODUCTS_DEL: "'.\CUtil::JSEscape(Loc::getMessage("SALE_ORDER_SHIPMENT_BASKET_SELECTED_PRODUCTS_DEL")).'",
571 SALE_ORDER_SHIPMENT_BASKET_ALL_PRODUCTS_DEL: "'.\CUtil::JSEscape(Loc::getMessage("SALE_ORDER_SHIPMENT_BASKET_ALL_PRODUCTS_DEL")).'",
572 SALE_ORDER_SHIPMENT_BASKET_ADD_NEW_STORE: "'.\CUtil::JSEscape(Loc::getMessage("SALE_ORDER_SHIPMENT_BASKET_ADD_NEW_STORE")).'",
573 SALE_ORDER_SHIPMENT_BASKET_BARCODE: "'.\CUtil::JSEscape(Loc::getMessage("SALE_ORDER_SHIPMENT_BASKET_BARCODE")).'",
574 SALE_ORDER_SHIPMENT_BASKET_CLOSE: "'.\CUtil::JSEscape(Loc::getMessage("SALE_ORDER_SHIPMENT_BASKET_CLOSE")).'",
575 SALE_ORDER_SHIPMENT_BASKET_ADD: "'.\CUtil::JSEscape(Loc::getMessage("SALE_ORDER_SHIPMENT_BASKET_ADD")).'",
576 SALE_ORDER_SHIPMENT_BASKET_NO_PRODUCTS: "'.\CUtil::JSEscape(Loc::getMessage("SALE_ORDER_SHIPMENT_BASKET_NO_PRODUCTS")).'",
577 SALE_ORDER_SHIPMENT_BASKET_BARCODE_ALREADY_USED: "'.\CUtil::JSEscape(Loc::getMessage("SALE_ORDER_SHIPMENT_BASKET_BARCODE_ALREADY_USED")).'",
578 SALE_ORDER_SHIPMENT_BASKET_BARCODE: "'.\CUtil::JSEscape(Loc::getMessage("SALE_ORDER_SHIPMENT_BASKET_BARCODE")).'",
579 SALE_ORDER_SHIPMENT_BASKET_BARCODE_CLOSE: "'.\CUtil::JSEscape(Loc::getMessage("SALE_ORDER_SHIPMENT_BASKET_BARCODE_CLOSE")).'",
580 SALE_ORDER_SHIPMENT_BASKET_BARCODE_ENTER: "'.\CUtil::JSEscape(Loc::getMessage("SALE_ORDER_SHIPMENT_BASKET_BARCODE_ENTER")).'",
581 SALE_ORDER_SHIPMENT_BASKET_ERROR_ALREADY_SHIPPED: "'.\CUtil::JSEscape(Loc::getMessage("SALE_ORDER_SHIPMENT_BASKET_ERROR_ALREADY_SHIPPED")).'",
582 SALE_ORDER_SHIPMENT_BASKET_ERROR_NOT_FOUND: "'.\CUtil::JSEscape(Loc::getMessage("SALE_ORDER_SHIPMENT_BASKET_ERROR_NOT_FOUND")).'",
583 SALE_ORDER_SHIPMENT_BASKET_BARCODE_EMPTY: "'.\CUtil::JSEscape(Loc::getMessage("SALE_ORDER_SHIPMENT_BASKET_BARCODE_EMPTY")).'",
584 });
585
586 BX.ready(function(){
587 '.$this->jsObjName.' = new BX.Sale.Admin.ShipmentBasketEdit({
588 tableId: "'.$this->idPrefix.'sale_order_edit_product_table",
589 productsOrder: '.\CUtil::PhpToJSObject($keys).',
590 idPrefix: "'.$this->idPrefix.'",
591 products: '.\CUtil::PhpToJSObject($data["ITEMS"]).',
592 orderId: '.((int)$this->order->getId()).',
593 visibleColumns: '.\CUtil::PhpToJSObject($this->visibleColumns).',
594 objName: "'.$this->jsObjName.'",
595 isShipped: "'.$this->shipment->isShipped().'",
596 totalBlockFields: {
597 PRICE_DELIVERY_DISCOUNT: {
598 id: "'.$this->idPrefix.'sale_order_edit_basket_price_delivery_discount",
599 value: "'.roundEx(floatval(0), SALE_VALUE_PRECISION).'",
600 type: "currency"
601 }
602 },
603 dataForRecovery: '.\CUtil::PhpToJSObject($recoveryData).',
604 useStoreControl: "'.self::$useStoreControl.'"
605 });
606
607 '.$this->systemJsObjName.' = new BX.Sale.Admin.SystemShipmentBasketEdit({
608 tableId: "unshipped",
609 productsOrder: '.\CUtil::PhpToJSObject($keys).',
610 idPrefix: "del",
611 products: '.\CUtil::PhpToJSObject($data["UNSHIPPED_PRODUCTS"]).',
612 visibleColumns: '.\CUtil::PhpToJSObject(self::getDefaultUnShippedVisibleColumns()).',
613 objName: "'.$this->systemJsObjName.'"
614 });
615
616 '.$this->jsObjName.'.link = '.$this->systemJsObjName.';
617 '.$this->systemJsObjName.'.link = '.$this->jsObjName.';
618 });';
619
620 $result .= $this->settingsDialog->getScripts();
621 $result .= '</script>';
622
623 return $result;
624 }
625
626 public function getViewScript($index, $visibleColumns)
627 {
628 \Bitrix\Main\Page\Asset::getInstance()->addJs("/bitrix/js/sale/admin/order_shipment_basket.js");
629 \Bitrix\Main\UI\Extension::load('sale.admin_order');
630
631 $data = $this->prepareData();
632
633 return '<script>
634 BX.message({
635 SALE_ORDER_SHIPMENT_VIEW_BASKET_NO_PRODUCTS: "'.\CUtil::JSEscape(Loc::getMessage("SALE_ORDER_SHIPMENT_VIEW_BASKET_NO_PRODUCTS")).'",
636 SALE_ORDER_SHIPMENT_BASKET_BARCODE: "'.\CUtil::JSEscape(Loc::getMessage("SALE_ORDER_SHIPMENT_BASKET_BARCODE")).'",
637 SALE_ORDER_SHIPMENT_BASKET_BARCODE_CLOSE: "'.\CUtil::JSEscape(Loc::getMessage("SALE_ORDER_SHIPMENT_BASKET_BARCODE_CLOSE")).'",
638 SALE_ORDER_SHIPMENT_BASKET_BARCODE_EMPTY: "'.\CUtil::JSEscape(Loc::getMessage("SALE_ORDER_SHIPMENT_BASKET_BARCODE_EMPTY")).'",
639 });
640
641 BX.ready(function()
642 {
643 var viewBasket_'.$index.' = new BX.Sale.Admin.ShipmentBasket({
644 idPrefix: "'.$this->idPrefix.'",
645 productsOrder: '.\CUtil::PhpToJSObject(array_keys($data["ITEMS"])).',
646 tableId: "'.$this->idPrefix.'_'.$index.'",
647 products: '.\CUtil::PhpToJSObject($data["ITEMS"]).',
648 visibleColumns: '.\CUtil::PhpToJSObject($visibleColumns).',
649 useStoreControl: "'.self::$useStoreControl.'"
650 });
651 });
652 </script>';
653 }
654
655 public static function updateData(Order &$order, &$shipment, $shipmentBasket)
656 {
658 $result = new Result();
659 $shippingItems = array();
660 $idsFromForm = array();
661 $basket = $order->getBasket();
662 $shipmentItemCollection = $shipment->getShipmentItemCollection();
663 if (is_null(self::$useStoreControl))
664 self::$useStoreControl = Configuration::useStoreControl();
665
666 if(is_array($shipmentBasket))
667 {
668 // PREPARE DATA FOR SET_FIELDS
669 foreach ($shipmentBasket as $items)
670 {
671 $items['QUANTITY'] = floatval(str_replace(',', '.', $items['QUANTITY'] ?? 0));
672 $items['AMOUNT'] = floatval(str_replace(',', '.', $items['AMOUNT']));
673 if (isset($items['BASKET_ID']) && $items['BASKET_ID'] > 0)
674 {
675 if (!$basketItem = $basket->getItemById($items['BASKET_ID']))
676 {
677 $result->addError(new ResultError(
679 'SALE_ORDER_SHIPMENT_BASKET_BASKET_ITEM_NOT_FOUND',
680 [
681 '#BASKET_ITEM_ID#' => $items['BASKET_ID'],
682 ]
683 ),
684 'PROVIDER_UNRESERVED_SHIPMENT_ITEM_WRONG_BASKET_ITEM')
685 );
686 return $result;
687 }
689 $basketCode = $basketItem->getBasketCode();
690 }
691 else
692 {
693 $basketCode = $items['BASKET_CODE'];
694 if(!$basketItem = $basket->getItemByBasketCode($basketCode))
695 {
696 $result->addError(new ResultError(
698 'SALE_ORDER_SHIPMENT_BASKET_BASKET_ITEM_NOT_FOUND',
699 [
700 '#BASKET_ITEM_ID#' => $items['BASKET_ID'],
701 ]
702 ),
703 'PROVIDER_UNRESERVED_SHIPMENT_ITEM_WRONG_BASKET_ITEM')
704 );
705 return $result;
706 }
707 }
708
709 $tmp = array(
710 'BASKET_CODE' => $basketCode,
711 'AMOUNT' => $items['AMOUNT'],
712 'ORDER_DELIVERY_BASKET_ID' => $items['ORDER_DELIVERY_BASKET_ID'] ?? null,
713 );
714 $idsFromForm[$basketCode] = array();
715
716 if (!empty($items['BARCODE_INFO']) && (self::$useStoreControl || $basketItem->isSupportedMarkingCode()))
717 {
718 foreach ($items['BARCODE_INFO'] as $item)
719 {
720 if ($basketItem->isBundleParent())
721 {
722 $shippingItems[] = $tmp;
723 continue;
724 }
725
726 $tmp['BARCODE'] = array(
727 'ORDER_DELIVERY_BASKET_ID' => $items['ORDER_DELIVERY_BASKET_ID'],
728 'STORE_ID' => (int)$item['STORE_ID'],
729 'QUANTITY' => ($basketItem->isBarcodeMulti() || $basketItem->isSupportedMarkingCode()) ? 1 : $item['QUANTITY']
730 );
731
732 $barcodeCount = 0;
733 if (!empty($item['BARCODE']) && is_array($item['BARCODE']))
734 {
735 foreach ($item['BARCODE'] as $barcode)
736 {
737 if(self::$useStoreControl && ($barcodeCount >= $items['QUANTITY']))
738 {
739 break;
740 }
741
742 $barcode['ID'] = (int)$barcode['ID'];
743 $idsFromForm[$basketCode]['BARCODE_IDS'][$barcode['ID']] = true;
744
745 if ($barcode['ID'] > 0)
746 $tmp['BARCODE']['ID'] = $barcode['ID'];
747 else
748 unset($tmp['BARCODE']['ID']);
749
750 $tmp['BARCODE']['BARCODE'] = $barcode['VALUE'];
751 $tmp['BARCODE']['MARKING_CODE'] = $barcode['MARKING_CODE'];
752 $shippingItems[] = $tmp;
753 $barcodeCount++;
754 }
755 }
756 elseif (!$basketItem->isBarcodeMulti() && !$basketItem->isSupportedMarkingCode())
757 {
758 $shippingItems[] = $tmp;
759 continue;
760 }
761
762 if ($basketItem->isBarcodeMulti() || $basketItem->isSupportedMarkingCode())
763 {
764 while ($barcodeCount < $item['QUANTITY'])
765 {
766 unset($tmp['BARCODE']['ID']);
767 $tmp['BARCODE']['BARCODE'] = '';
768 $tmp['BARCODE']['MARKING_CODE'] = '';
769 $shippingItems[] = $tmp;
770 $barcodeCount++;
771 }
772 }
773 }
774 }
775 else
776 {
777 $shippingItems[] = $tmp;
778 }
779 }
780 }
781
782
783 // DELETE FROM COLLECTION
785 foreach ($shipmentItemCollection as $shipmentItem)
786 {
787 if (!array_key_exists($shipmentItem->getBasketCode(), $idsFromForm))
788 {
790 $r = $shipmentItem->delete();
791 if (!$r->isSuccess())
792 $result->addErrors($r->getErrors());
793 }
794
795 $shipmentItemStoreCollection = $shipmentItem->getShipmentItemStoreCollection();
796 if ($shipmentItemStoreCollection)
797 {
799 foreach ($shipmentItemStoreCollection as $shipmentItemStore)
800 {
801 $shipmentItemId = $shipmentItemStore->getId();
802 if (!isset($idsFromForm[$shipmentItem->getBasketCode()]['BARCODE_IDS'][$shipmentItemId]))
803 {
804 $delResult = $shipmentItemStore->delete();
805 if (!$delResult->isSuccess())
806 $result->addErrors($delResult->getErrors());
807 }
808 }
809 }
810 }
811
812 $isStartField = $shipmentItemCollection->isStartField();
813
814 // SET DATA
815 foreach ($shippingItems as $shippingItem)
816 {
817 if ((int)$shippingItem['ORDER_DELIVERY_BASKET_ID'] <= 0)
818 {
819 $basketCode = $shippingItem['BASKET_CODE'];
821 $basketItem = $order->getBasket()->getItemByBasketCode($basketCode);
822
824 $shipmentItem = $shipmentItemCollection->createItem($basketItem);
825 if ($shipmentItem === null)
826 {
827 $result->addError(
828 new EntityError(
829 Loc::getMessage('SALE_ORDER_SHIPMENT_BASKET_ERROR_ALREADY_SHIPPED')
830 )
831 );
832 return $result;
833 }
834 unset($shippingItem['BARCODE']['ORDER_DELIVERY_BASKET_ID']);
835 }
836 else
837 {
838 $shipmentItem = $shipmentItemCollection->getItemById($shippingItem['ORDER_DELIVERY_BASKET_ID']);
839 $basketItem = $shipmentItem->getBasketItem();
840 }
841
842 if ($shippingItem['AMOUNT'] <= 0)
843 {
844 $result->addError(
845 new EntityError(
846 Loc::getMessage('SALE_ORDER_SHIPMENT_BASKET_ERROR_QUANTITY', array('#BASKET_ITEM#' => $basketItem->getField('NAME'))),
847 'BASKET_ITEM_'.$basketItem->getBasketCode()
848 )
849 );
850 continue;
851 }
852
853 if ($shipmentItem->getQuantity() < $shippingItem['AMOUNT'])
854 {
855 $order->setMathActionOnly(true);
856 $setFieldResult = $shipmentItem->setField('QUANTITY', $shippingItem['AMOUNT']);
857 $order->setMathActionOnly(false);
858
859 if (!$setFieldResult->isSuccess())
860 $result->addErrors($setFieldResult->getErrors());
861 }
862
863 if (!empty($shippingItem['BARCODE']) && (self::$useStoreControl || $basketItem->isSupportedMarkingCode()))
864 {
865 $barcode = $shippingItem['BARCODE'];
866
868 $shipmentItemStoreCollection = $shipmentItem->getShipmentItemStoreCollection();
869 if ($shipmentItemStoreCollection)
870 {
871 if (!$basketItem->isBarcodeMulti() && !$basketItem->isSupportedMarkingCode())
872 {
874 $r = $shipmentItemStoreCollection->setBarcodeQuantityFromArray($shipmentBasket[$basketItem->getId()]);
875 if(!$r->isSuccess())
876 {
877 $result->addErrors($r->getErrors());
878 }
879 }
880
881 if (isset($barcode['ID']) && intval($barcode['ID']) > 0)
882 {
884 if ($shipmentItemStore = $shipmentItemStoreCollection->getItemById($barcode['ID']))
885 {
886 unset($barcode['ID']);
887 $setFieldResult = $shipmentItemStore->setFields($barcode);
888
889 if (!$setFieldResult->isSuccess())
890 $result->addErrors($setFieldResult->getErrors());
891 }
892 }
893 else
894 {
895 $shipmentItemStore = $shipmentItemStoreCollection->createItem($basketItem);
896 $setFieldResult = $shipmentItemStore->setFields($barcode);
897 if (!$setFieldResult->isSuccess())
898 $result->addErrors($setFieldResult->getErrors());
899 }
900 }
901 }
902
903 $setFieldResult = $shipmentItem->setField('QUANTITY', $shippingItem['AMOUNT']);
904 if (!$setFieldResult->isSuccess())
905 $result->addErrors($setFieldResult->getErrors());
906 }
907
908 if ($isStartField)
909 {
910 $hasMeaningfulFields = $shipmentItemCollection->hasMeaningfulField();
911
913 $r = $shipmentItemCollection->doFinalAction($hasMeaningfulFields);
914 if (!$r->isSuccess())
915 {
916 $result->addErrors($r->getErrors());
917 }
918 }
919
920 return $result;
921 }
922}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
getOffersSkuParams(array $productsParams, array $visibleColumns=array())
static getProductsData(array $productsIds, $siteId, array $fields=array(), $userId=0)
__construct(Shipment $shipment, $jsObjName="", $idPrefix="")