Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
orderbuilderrest.php
1<?php
2
3
5
6
12use Bitrix\Sale;
19
26{
27 public function __construct(SettingsContainer $settings)
28 {
29 parent::__construct($settings);
30 $this->setBasketBuilder(new BasketBuilderRest($this));
31 }
32
33 protected function prepareFields(array $fields)
34 {
35 $fields = array_merge(
36 \Bitrix\Sale\Controller\Order::prepareFields($fields),
37 \Bitrix\Sale\Controller\PropertyValue::prepareFields($fields['ORDER']),
38 \Bitrix\Sale\Controller\BasketItem::prepareFields($fields['ORDER']),
39 \Bitrix\Sale\Controller\Payment::prepareFields($fields['ORDER']),
40 \Bitrix\Sale\Controller\Shipment::prepareFields($fields['ORDER']),
41 \Bitrix\Sale\Controller\TradeBinding::prepareFields($fields['ORDER'])
42 );
43
44 return parent::prepareFields($fields);
45 }
46
47 protected function createEmptyPayment()
48 {
49 if($this->getSettingsContainer()->getItemValue('createDefaultPaymentIfNeed'))
50 {
51 $this->formData["PAYMENT"] = [
52 [
53 'SUM'=>$this->getOrder()->getPrice(),
54 'PAID'=>'N',
55 'PAY_SYSTEM_ID'=>Manager::getInnerPaySystemId()
56 ]
57 ];
58
59 parent::buildPayments();
60 }
61 return $this;
62 }
63
64 protected function createEmptyShipment()
65 {
66 if($this->getSettingsContainer()->getItemValue('createDefaultShipmentIfNeed'))
67 {
68 $this->formData["SHIPMENT"] = [
69 [
70 'DEDUCTED'=>'N',
72 ]
73 ];
74 parent::buildShipments();
75 }
76 return $this;
77 }
78
79 protected function prepareFieldsStatusId($isNew, $item, $defaultFields)
80 {
81 $statusId = '';
82 if($isNew)
83 {
84 if (isset($item['STATUS_ID']))
85 {
86 $statusId = $item['STATUS_ID'];
87 }
88 }
89 else
90 {
91 $statusId = parent::prepareFieldsStatusId($isNew, $item, $defaultFields);
92 }
93
94 return $statusId;
95 }
96
97 protected function removeShipmentItems(\Bitrix\Sale\Shipment $shipment, $products, $idsFromForm)
98 {
99 $result = new Result();
100
101 if(is_array($products))// products, ,
102 {
103 if($this->getSettingsContainer()->getItemValue('deleteShipmentItemIfNotExists'))
104 {
105 $shipmentItemCollection = $shipment->getShipmentItemCollection();
106
107 $shipmentItemIds = [];
108 foreach($products as $items)
109 {
110 if(!isset($items['ORDER_DELIVERY_BASKET_ID']))
111 continue;
112
113 $shipmentItemProduct = $shipmentItemCollection->getItemById($items['ORDER_DELIVERY_BASKET_ID']);
114
115 if ($shipmentItemProduct == null)
116 continue;
117
118 $shipmentItemIds[] = $shipmentItemProduct->getId();
119 }
120
122 foreach ($shipmentItemCollection as $shipmentItem)
123 {
124 if(!in_array($shipmentItem->getId(), $shipmentItemIds))
125 {
126 $r = $shipmentItem->delete();
127 if (!$r->isSuccess())
128 {
129 $this->errorsContainer->addErrors($r->getErrors());
130 $result->addErrors($r->getErrors());
131 }
132 }
133 }
134 }
135 }
136 return $result;
137 }
138
139 protected function prepareDataForSetFields(\Bitrix\Sale\Shipment $shipment, $items)
140 {
141 $result = new Result();
142 $shipmentItemCollection = $shipment->getShipmentItemCollection();
143
144 //only for update shipmentItem, where BASKET_ID is imutable
145 if(isset($items['ORDER_DELIVERY_BASKET_ID']) && intval($items['ORDER_DELIVERY_BASKET_ID']) > 0)
146 {
147 if (!$shipmentItem = $shipmentItemCollection->getItemById($items['ORDER_DELIVERY_BASKET_ID']))
148 {
149 $result->addError( new Error( 'SALE_ORDER_SHIPMENT_BASKET_ORDER_DELIVERY_ID_NOT_FOUND'));
150 }
151 else
152 {
154 $items['BASKET_ID'] = $shipmentItem->getBasketId();
155 }
156 }
157 return $result->setData([$items]);
158 }
159
160 protected function modifyQuantityShipmentItem(ShipmentItem $shipmentItem, array $params)
161 {
162 $r = new Result();
163
164 $basketItem = $shipmentItem->getBasketItem();
166 $basket = $basketItem->getCollection();
168 $order = $basket->getOrder();
169
170 $allAllowedQuantity = $this->getQuantityBasketItemFromShipmentCollection($basketItem);
171
172 $deltaQuantity = $params['AMOUNT'] - $shipmentItem->getQuantity();
173
174 if($deltaQuantity < 0)
175 {
176 $this->setQuantityShipmentItem($shipmentItem, 0, abs($deltaQuantity));
177 }
178 elseif($deltaQuantity > 0)
179 {
180 if($allAllowedQuantity >= $params['AMOUNT'])
181 {
182 $systemShipment = $order->getShipmentCollection()->getSystemShipment();
183 $systemBasketQuantity = $systemShipment->getBasketItemQuantity($basketItem);
184
185 if($systemBasketQuantity >= $deltaQuantity)
186 {
187 $this->setQuantityShipmentItem($shipmentItem, $params['AMOUNT'], $shipmentItem->getQuantity());
188 }
189 else
190 {
191 $needQuantity = $deltaQuantity - $systemBasketQuantity;
192
193 $r = $this->synchronizeQuantityShipmentItems($shipmentItem, $needQuantity);
194 if($r->isSuccess())
195 {
196 $this->setQuantityShipmentItem($shipmentItem, $params['AMOUNT'], $shipmentItem->getQuantity());
197 }
198 }
199 }
200 else
201 {
202 $r->addError(new \Bitrix\Main\Error('Attempt to increase the quantity of goods in shipment to a quantity that exceeds not shipped in the order.'));
203 }
204 }
205 return $r;
206 }
207
208 private function getQuantityBasketItemFromShipmentCollection(BasketItem $basketItem)
209 {
211 $basket = $basketItem->getCollection();
213 $order = $basket->getOrder();
214
215 $allQuantity = 0;
217 foreach ($order->getShipmentCollection() as $shipment)
218 {
219 if($shipment->isShipped())
220 continue;
221
222 $allQuantity += $shipment->getBasketItemQuantity($basketItem);
223 }
224
225 return $allQuantity;
226 }
227
228 protected function setQuantityShipmentItem(ShipmentItem $shipmentItem, $value, $oldValue)
229 {
230 $deltaQuantity = $value - $oldValue;
231
232 if($shipmentItem->getQuantity() + $deltaQuantity == 0)
233 {
234 $r = $shipmentItem->delete();
235 }
236 else
237 {
238 $r = $shipmentItem->setField(
239 "QUANTITY",
240 $shipmentItem->getQuantity() + $deltaQuantity
241 );
242 }
243
244 return $r;
245 }
246
247 public function synchronizeQuantityShipmentItems(ShipmentItem $shipmentItem, $needQuantity)
248 {
249 $result = new Result();
250
251 if(intval($needQuantity) <= 0)
252 {
253 return $result;
254 }
255
256 $basketItem = $shipmentItem->getBasketItem();
258 $shipmentItemCollection = $shipmentItem->getCollection();
259 $parentEntity = $shipmentItemCollection->getShipment();
260
261 foreach ($parentEntity->getCollection() as $shipment)
262 {
264 if($parentEntity->getId() == $shipment->getId())
265 continue;
266
267 if($shipment->isShipped() || $shipment->isSystem())
268 continue;
269
270 $basketQuantity = $shipment->getBasketItemQuantity($basketItem);
271 if(empty($basketQuantity))
272 continue;
273
274 $shipmentItem = $shipment->getShipmentItemCollection()->getItemByBasketCode($basketItem->getBasketCode());
275
276 if($basketQuantity >= $needQuantity)
277 {
278 $this->setQuantityShipmentItem($shipmentItem, 0, $needQuantity);
279 $needQuantity = 0;
280 }
281 else
282 {
283 $this->setQuantityShipmentItem($shipmentItem, 0, $basketQuantity);
284 $needQuantity -= $basketQuantity;
285 }
286
287 if($needQuantity == 0)
288 break;
289 }
290
291 if($needQuantity != 0)
292 $result->addError(new Error('Not enough unallocated goods in shipments'));
293
294 return $result;
295 }
296
297 public function setProperties()
298 {
299 if(!isset($this->formData["PROPERTIES"]))
300 {
301 return $this;
302 }
303
304 $r = $this->removePropertyValues();
305 if($r->isSuccess() == false)
306 {
307 $this->getErrorsContainer()->addErrors($r->getErrors());
308 return $this;
309 }
310
311 $this->formData["PROPERTIES"] = File::getPostWithFiles(
312 $this->formData["PROPERTIES"],
313 $this->settingsContainer->getItemValue('propsFiles')
314 );
315
316 $propCollection = $this->order->getPropertyCollection();
317
318 foreach ($this->formData["PROPERTIES"] as $id=>$value)
319 {
320 if(($propertyValue = $propCollection->getItemByOrderPropertyId($id)))
321 {
322 $propertyValue->setValue($value);
323 }
324 }
325
326 return $this;
327 }
328
329 public function setDiscounts()
330 {
331 return $this;
332 }
333
334 protected function removePropertyValues()
335 {
336 $result = new Result();
337
338 if($this->getSettingsContainer()->getItemValue('deletePropertyValuesIfNotExists'))
339 {
340 $propCollection = $this->order->getPropertyCollection();
342 foreach($propCollection as $propertyValue)
343 {
344 if(is_set($this->formData["PROPERTIES"],$propertyValue->getPropertyId()) == false)
345 {
346 $r = $propertyValue->delete();
347 if (!$r->isSuccess())
348 {
349 $result->addErrors($r->getErrors());
350 }
351 }
352 }
353 }
354
355 return $result;
356 }
357
358 protected function getSettableOrderFields()
359 {
361 $orderClass = $this->getRegistry()->getOrderClassName();
362
363 return $orderClass::getAvailableFields();
364 }
365
366 protected function checkDeliveryRestricted($shipment, $deliveryService, $shipmentFields)
367 {
368 // rest .
369 return true;
370 }
371
372 public function buildEntityShipments(array $fields)
373 {
374 try{
375 $this->initFields($fields)
376 ->delegate()
377 ->createOrder()
378 ->setDiscounts()
379 ->buildShipments()
380 ->setDiscounts()
381 ->finalActions();
382 }
383 catch(BuildingException $e)
384 {
385 return null;
386 }
387
388 return $this->getOrder();
389 }
390
391 public function buildEntityPayments(array $fields)
392 {
393 try{
394 $this->initFields($fields)
395 ->delegate()
396 ->createOrder()
397 ->setDiscounts()
398 ->buildPayments()
399 ->setDiscounts()
400 ->finalActions();
401 }
402 catch(BuildingException $e)
403 {
404 return null;
405 }
406
407 return $this->getOrder();
408 }
409
410 public function buildEntityBasket(array $fields)
411 {
412 try{
413 $this->initFields($fields)
414 ->delegate()
415 ->createOrder()
416 ->setDiscounts() //?
417 ->buildBasket()
418 ->setDiscounts() //?
419 ->finalActions();
420 }
421 catch(BuildingException $e)
422 {
423 return null;
424 }
425
426 return $this->getOrder();
427 }
428
429 public function buildEntityOrder(array $fields)
430 {
431 try{
432 $this->initFields($fields)
433 ->delegate()
434 ->createOrder()
435 ->setDiscounts() //?
436 ->setFields()
437 ->setUser()
438 ->finalActions();
439 }
440 catch(BuildingException $e)
441 {
442 return null;
443 }
444
445 return $this->getOrder();
446 }
447
448 public function buildEntityProperties(array $fields)
449 {
450 try{
451 $this->initFields($fields)
452 ->delegate()
453 ->createOrder()
454 ->setDiscounts() //?
455 ->setProperties()
456 ->setDiscounts() //?
457 ->finalActions();
458 }
459 catch(BuildingException $e)
460 {
461 return null;
462 }
463
464 return $this->getOrder();
465 }
466}
setBasketBuilder(BasketBuilder $basketBuilder)
modifyQuantityShipmentItem(ShipmentItem $shipmentItem, array $params)
prepareDataForSetFields(\Bitrix\Sale\Shipment $shipment, $items)
setQuantityShipmentItem(ShipmentItem $shipmentItem, $value, $oldValue)
checkDeliveryRestricted($shipment, $deliveryService, $shipmentFields)