Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
shipmentitem.php
1<?php
2
3
5
6
9use Bitrix\Main\Entity\ExpressionField;
13use Bitrix\Sale;
17
19{
20 public function getPrimaryAutoWiredParameter()
21 {
22 return new ExactParameter(
23 Sale\ShipmentItem::class,
24 'shipmentItem',
25 function($className, $id) {
26
27 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
28
30 $shipmentItemClass = $registry->getShipmentItemClassName();
31
32 $si = $shipmentItemClass::getList([
33 'select'=>['ORDER_DELIVERY_ID'],
34 'filter'=>['ID'=>$id]
35 ]);
36
37 if($siRow = $si->fetch())
38 {
40 $shipmentClass = $registry->getShipmentClassName();
41
42 $r = $shipmentClass::getList([
43 'select'=>['ORDER_ID'],
44 'filter'=>['ID'=>$siRow['ORDER_DELIVERY_ID']]
45 ]);
46
47 if($row = $r->fetch())
48 {
50 $orderClass = $registry->getOrderClassName();
51
52 $order = $orderClass::load($row['ORDER_ID']);
54 $shipment = $order->getShipmentCollection()->getItemById($siRow['ORDER_DELIVERY_ID']);
55 $shipmentItem = $shipment->getShipmentItemCollection()->getItemById($id);
56
57 if ($shipmentItem)
58 {
59 return $shipmentItem;
60 }
61 }
62 }
63
64 $this->addError(new Error('shipment item is not exists', 201240400001));
65 return null;
66 }
67 );
68 }
69
70 //region Actions
71 public function getFieldsAction()
72 {
73 $entity = new \Bitrix\Sale\Rest\Entity\ShipmentItem();
74 return ['SHIPMENT_ITEM'=>$entity->prepareFieldInfos(
75 $entity->getFields()
76 )];
77 }
78
79 public function getAction(\Bitrix\Sale\ShipmentItem $shipmentItem)
80 {
81 return ['SHIPMENT_ITEM'=>$this->get($shipmentItem)];
82 }
83
84 public function listAction(PageNavigation $pageNavigation, array $select = [], array $filter = [], array $order = []): Page
85 {
86 $select = empty($select) ? ['*'] : $select;
87 $order = empty($order) ? ['ID' => 'ASC'] : $order;
88
89 $shipmentItems = \Bitrix\Sale\ShipmentItem::getList(
90 [
91 'select' => $select,
92 'filter' => $filter,
93 'order' => $order,
94 'offset' => $pageNavigation->getOffset(),
95 'limit' => $pageNavigation->getLimit(),
96 ]
97 )->fetchAll();
98
99 return new Page('SHIPMENT_ITEMS', $shipmentItems, function() use ($filter)
100 {
101 return (int) \Bitrix\Sale\ShipmentItem::getList([
102 'select' => ['CNT'],
103 'filter'=>$filter,
104 'runtime' => [
105 new ExpressionField('CNT', 'COUNT(ID)')
106 ]
107 ])->fetch()['CNT'];
108 });
109 }
110
111 public function addAction(array $fields)
112 {
113 $result = new Result();
114
115 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
116
117
118 $basketId = $fields['BASKET_ID'];
119 $shipmentId = $fields['ORDER_DELIVERY_ID'];
120
121 unset($fields['ORDER_DELIVERY_ID'], $fields['BASKET_ID']);
122
124 $basketClass = $registry->getBasketClassName();
125
126 $r = $basketClass::getList([
127 'select'=>['ORDER_ID'],
128 'filter'=>['ID'=>$basketId]
129 ]);
130
131 if($row = $r->fetch())
132 {
134 $orderClass = $registry->getOrderClassName();
135
136 $order = $orderClass::load($row['ORDER_ID']);
137 $basketItem = $order->getBasket()->getItemByBasketCode($basketId);
138 if($basketItem instanceof BasketItem)
139 {
141 $collection = $order->getShipmentCollection();
142 $shipment = $collection->getItemById($shipmentId);
143 if($shipment instanceof \Bitrix\Sale\Shipment)
144 {
145 $shipmentItemCollection = $shipment->getShipmentItemCollection();
146 if($shipmentItemCollection->isExistBasketItem($basketItem) == false)
147 {
149 $shipmentItem = $shipmentItemCollection->createItem($basketItem);
150 $result = $shipmentItem->setFields($fields);
151 if($result->isSuccess() && $result->hasWarnings() == false)
152 {
153 $r = $this->save($shipmentItem);
154 if(!$r->isSuccess())
155 {
156 $result->addErrors($r->getErrors());
157 }
158 }
159 }
160 else
161 {
162 $result->addError(new Error('Duplicate entry for key [basketId, orderDeliveryId]', 201250000001));
163 }
164 }
165 else
166 {
167 $result->addError(new Error('shipment not exists', 201240400002));
168 }
169 }
170 }
171 else
172 {
173 $result->addError(new Error('shipment not exists', 201240400003));
174 }
175
176 if(!$result->isSuccess())
177 {
178 $this->addErrors($result->getErrors());
179 return null;
180 }
181 elseif($result->hasWarnings())
182 {
183 $this->addErrors($result->getWarnings());
184 return null;
185 }
186 else
187 {
188 return ['SHIPMENT_ITEM'=>$this->get($shipmentItem)];
189 }
190 }
191
192 public function updateAction(\Bitrix\Sale\ShipmentItem $shipmentItem, array $fields)
193 {
194 $r = $shipmentItem->setFields($fields);
195
196 if($r->isSuccess() == false)
197 {
198 $this->addErrors($r->getErrors());
199 return null;
200 }
201 elseif($r->hasWarnings())
202 {
203 $this->addErrors($r->getWarnings());
204 return null;
205 }
206 else
207 {
208 $r = $this->save($shipmentItem);
209 if(!$r->isSuccess())
210 {
211 $this->addErrors($r->getErrors());
212 return null;
213 }
214 else
215 {
216 return ['SHIPMENT_ITEM'=>$this->get($shipmentItem)];
217 }
218 }
219 }
220
221 public function deleteAction(\Bitrix\Sale\ShipmentItem $shipmentItem)
222 {
223 $r = $shipmentItem->delete();
224
225 if($r->isSuccess() == false)
226 {
227 $this->addErrors($r->getErrors());
228 return null;
229 }
230 elseif($r->hasWarnings())
231 {
232 $this->addErrors($r->getWarnings());
233 return null;
234 }
235 else
236 {
237 $r = $this->save($shipmentItem);
238 if(!$r->isSuccess())
239 {
240 $this->addErrors($r->getErrors());
241 return null;
242 }
243 else
244 {
245 return true;
246 }
247 }
248 }
249 //endregion
250
251 protected function get(\Bitrix\Sale\ShipmentItem $shipmentItem, array $fields=[])
252 {
254 $collectionShipmentItems = $shipmentItem->getCollection();
256 $shipment = $collectionShipmentItems->getShipment();
258 $collectionShipments = $shipment->getCollection();
259 $order = $collectionShipments->getOrder();
260
261 $shipments = $this->toArray($order, $fields)['ORDER']['SHIPMENTS'];
262 foreach ($shipments as $shipment)
263 {
264 foreach ($shipment['SHIPMENT_ITEMS'] as $item)
265 {
266 if($item['ID']==$shipmentItem->getId())
267 {
268 return $item;
269 }
270 }
271 }
272 return [];
273 }
274
275 private function save(\Bitrix\Sale\ShipmentItem $shipmentItem)
276 {
277 $result = new Result();
279 $collectionShipmentItems = $shipmentItem->getCollection();
281 $shipment = $collectionShipmentItems->getShipment();
283 $collectionShipments = $shipment->getCollection();
284 $order = $collectionShipments->getOrder();
285
286 $r = $order->save();
287 if(!$r->isSuccess())
288 {
289 $result->addErrors($r->getErrors());
290 }
291 elseif($r->hasWarnings())
292 {
293 $result->addErrors($r->getWarnings());
294 }
295 return $result;
296 }
297}
getAction(\Bitrix\Sale\ShipmentItem $shipmentItem)
deleteAction(\Bitrix\Sale\ShipmentItem $shipmentItem)
updateAction(\Bitrix\Sale\ShipmentItem $shipmentItem, array $fields)
listAction(PageNavigation $pageNavigation, array $select=[], array $filter=[], array $order=[])
static getList(array $parameters)