Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
orderfacade.php
1<?php
2
3namespace Bitrix\Sale;
4
8
9Loc::loadMessages(__FILE__);
10
16{
27 public static function payOrder($id)
28 {
29 $result = new Result();
30
33 $orderClassName = $registry->getOrderClassName();
34
35 $order = $orderClassName::load($id);
36 if (!$order)
37 {
38 $result->addError(new Error(Loc::getMessage('SALE_GROUP_ACTION_ERR_ORDER_NOT_FOUND')));
39 return $result;
40 }
41
42 $collection = $order->getPaymentCollection();
44 foreach ($collection as $payment)
45 {
46 if (!$payment->isPaid())
47 {
48 $r = $payment->setPaid('Y');
49 if (!$r->isSuccess())
50 {
51 $result->addErrors($r->getErrors());
52 return $result;
53 }
54 }
55 }
56
57 if (!$order->isPaid())
58 {
59 $payment = static::createFinalPayment($order);
60 if ($payment === null)
61 {
62 $result->addError(
63 new Error(
64 Loc::getMessage('SALE_GROUP_ACTION_ERR_PAYMENT_CREATE')
65 )
66 );
67 return $result;
68 }
69 }
70
71 $r = $order->save();
72 if (!$r->isSuccess())
73 {
74 $result->addErrors($r->getErrors());
75 }
76
77 return $result;
78 }
79
88 public static function cancelPayOrder($id)
89 {
90 $result = new Result();
91
94 $orderClassName = $registry->getOrderClassName();
95
96 $order = $orderClassName::load($id);
97 if (!$order)
98 {
99 $result->addError(new Error(Loc::getMessage('SALE_GROUP_ACTION_ERR_ORDER_NOT_FOUND')));
100 return $result;
101 }
102
103 $collection = $order->getPaymentCollection();
105 foreach ($collection as $payment)
106 {
107 if ($payment->isPaid())
108 {
109 $r = $payment->setPaid('N');
110 if (!$r->isSuccess())
111 {
112 $result->addErrors($r->getErrors());
113 return $result;
114 }
115 }
116 }
117
118 $r = $order->save();
119 if (!$r->isSuccess())
120 {
121 $result->addErrors($r->getErrors());
122 }
123
124 return $result;
125 }
126
138 public static function deductOrder($id)
139 {
140 $result = new Result();
141
144 $orderClassName = $registry->getOrderClassName();
145
146 $order = $orderClassName::load($id);
147 if (!$order)
148 {
149 $result->addError(new Error(Loc::getMessage('SALE_GROUP_ACTION_ERR_ORDER_NOT_FOUND')));
150 return $result;
151 }
152
153 $collection = $order->getShipmentCollection()->getNotSystemItems();
154
156 foreach ($collection as $shipment)
157 {
158 if (!$shipment->isShipped())
159 {
160 $r = $shipment->setField('DEDUCTED', 'Y');
161 if (!$r->isSuccess())
162 {
163 $result->addErrors($r->getErrors());
164 return $result;
165 }
166 }
167 }
168
169 if (!$order->isShipped())
170 {
171 $shipment = static::createFinalShipment($order);
172 if ($shipment === null)
173 {
174 $result->addError(
175 new Error(
176 Loc::getMessage('SALE_GROUP_ACTION_ERR_SHIPMENT_CREATE')
177 )
178 );
179 return $result;
180 }
181 }
182
183 $r = $order->save();
184 if (!$r->isSuccess())
185 {
186 $result->addErrors($r->getErrors());
187 }
188
189 return $result;
190 }
191
201 public static function cancelDeductOrder($id)
202 {
203 $result = new Result();
204
207 $orderClassName = $registry->getOrderClassName();
208
209 $order = $orderClassName::load($id);
210 if (!$order)
211 {
212 $result->addError(new Error(Loc::getMessage('SALE_GROUP_ACTION_ERR_ORDER_NOT_FOUND')));
213 return $result;
214 }
215
216 $collection = $order->getShipmentCollection()->getNotSystemItems();
217
219 foreach ($collection as $shipment)
220 {
221 if ($shipment->isShipped())
222 {
223 $r = $shipment->setField('DEDUCTED', 'N');
224 if (!$r->isSuccess())
225 {
226 $result->addErrors($r->getErrors());
227 return $result;
228 }
229 }
230 }
231
232 $r = $order->save();
233 if (!$r->isSuccess())
234 {
235 $result->addErrors($r->getErrors());
236 }
237
238 return $result;
239 }
240
250 protected static function createFinalPayment(Order $order)
251 {
252 $price = $order->getPrice();
253 $paidSum = $order->getPaymentCollection()->getPaidSum();
254
255 $payment = $order->getPaymentCollection()->createItem();
256 $payment->setField('SUM', $price - $paidSum);
257
258 $paySystemId = static::getPaySystemId($payment);
259 if ($paySystemId === 0)
260 {
261 return null;
262 }
263
264 $service = Sale\PaySystem\Manager::getObjectById($paySystemId);
265 $payment->setPaySystemService($service);
266
267 $payment->setPaid('Y');
268
269 return $payment;
270 }
271
283 protected static function createFinalShipment(Order $order)
284 {
285 $collection = $order->getShipmentCollection();
286
287 $deliveryId = static::getDeliveryId();
288 if ((int)$deliveryId == 0)
289 {
290 return null;
291 }
292
293 $delivery = Sale\Delivery\Services\Manager::getObjectById($deliveryId);
294 $shipment = $collection->createItem($delivery);
295
296 $itemCollection = $shipment->getShipmentItemCollection();
297
298 $system = $collection->getSystemShipment();
299 $systemItemCollection = $system->getShipmentItemCollection();
300
302 foreach ($systemItemCollection as $shipmentItem)
303 {
304 $item = $itemCollection->createItem($shipmentItem->getBasketItem());
305 $item->setQuantity($shipmentItem->getQuantity());
306 }
307
308 $shipment->setField('DEDUCTED', 'Y');
309
310 return $shipment;
311 }
312
319 protected static function getPaySystemId(Payment $payment)
320 {
321 $paySystemList = Sale\PaySystem\Manager::getListWithRestrictions($payment);
322 foreach ($paySystemList as $paySystem)
323 {
324 if ((int)$paySystem['ID'] === (int)Sale\PaySystem\Manager::getInnerPaySystemId())
325 {
326 continue;
327 }
328
329 return $paySystem['ID'];
330 }
331
332 return 0;
333 }
334
339 protected static function getDeliveryId()
340 {
341 return Sale\Delivery\Services\Manager::getEmptyDeliveryServiceId();
342 }
343}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static createFinalPayment(Order $order)
static getPaySystemId(Payment $payment)
static getInstance($type)
Definition registry.php:183