Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
order.php
1<?
3
13
14Loc::loadMessages(__FILE__);
15
16class Order
17{
25 public static function create(array $params)
26 {
27 if(empty($params['CURRENCY']))
28 throw new ArgumentNullException('params[CURRENCY]');
29
30 if($params['SITE_ID'] == '')
31 throw new ArgumentNullException('params[SITE_ID]');
32
33 if(intval($params['PERSON_TYPE_ID']) <= 0)
34 throw new ArgumentNullException('params[PERSON_TYPE_ID]');
35
36 if(empty($params['CART_ITEMS']))
37 throw new ArgumentNullException('params[CART_ITEMS]');
38
39 if(empty($params['IS_ACCEPT_OLD_PRICE']))
40 $params['IS_ACCEPT_OLD_PRICE'] = \CSaleYMHandler::NOT_ACCEPT_OLD_PRICE;
41
42 $result = new Result();
43 $currencyList = \Bitrix\Currency\CurrencyManager::getCurrencyList();
44
45 if($params['CURRENCY'] == 'RUR' && empty($currencyList['RUR']) && !empty($currencyList['RUB']))
46 $currency = 'RUB';
47 else
48 $currency = $params['CURRENCY'];
49
50 if(empty($currencyList[$currency]))
51 {
52 $result->addError(new Error(Loc::getMessage('SALE_YMARKET_ORDER_CURRENCY_NOT_SUPPORTED', array('#CURRENCY#' => $currency))));
53 return $result;
54 }
55
56 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
58 $orderClass = $registry->getOrderClassName();
59
60 $order = $orderClass::create($params['SITE_ID'], \CSaleUser::GetAnonymousUserID(), $currency);
62 $res = $order->setPersonTypeId(intval($params['PERSON_TYPE_ID']));
63
64 if (!$res->isSuccess())
65 $result->addErrors($res->getErrors());
66
67 if(!empty($params['PROPERTIES']))
68 {
69 $propCollection = $order->getPropertyCollection();
70 $res = $propCollection->setValuesFromPost(array('PROPERTIES' => $params['PROPERTIES']), array());
71
72 if (!$res->isSuccess())
73 $result->addErrors($res->getErrors());
74 }
75
76 $fUserId = $order->getUserId() > 0 ? \Bitrix\Sale\Fuser::getIdByUserId($order->getUserId()) : null;
77 $isStartField = $order->isStartField();
78
80 $basketClass = $registry->getBasketClassName();
81
82 $basket = $basketClass::create($params['SITE_ID']);
83 $res = $order->setBasket($basket);
84
85 if (!$res->isSuccess())
86 $result->addErrors($res->getErrors());
87
88 $basket->setFUserId($fUserId);
89 $discount = $order->getDiscount();
90
91 //Hello from discounts
92 //todo: sortByColumn($products, array("BASE_PRICE" => SORT_DESC, "PRICE" => SORT_DESC), '', null, true);
93
94 Loader::includeModule('catalog');
95
96 $itemsMap = array();
97
98 foreach ($params['CART_ITEMS'] as $itemKey => $item)
99 {
100 $basketItemFields = array(
101 'PRODUCT_ID' => $item['offerId'],
102 'QUANTITY' => $item['count'],
103 'PRODUCT_PROVIDER_CLASS' => '\Bitrix\Catalog\Product\CatalogProvider'
104 );
105
106 $context = array(
107 'SITE_ID' => $params['SITE_ID'],
108 'CURRENCY' => $params['CURRENCY'],
109 );
110
111 if ($order->getUserId() > 0)
112 {
113 $context['USER_ID'] = $order->getUserId();
114 }
115
116 $basketItem = null;
117
118 $res = Catalog\Product\Basket::addProductToBasket($basket, $basketItemFields, $context);
119 $resultData = $res->getData();
120
121 if (!empty($resultData['BASKET_ITEM']))
122 {
124 $basketItem = $resultData['BASKET_ITEM'];
125 }
126
127 if (!$res->isSuccess())
128 {
129 if ($basketItem)
130 {
131 $basketItem->setField("QUANTITY", 0);
132 }
133 }
134
135 if ($basketItem)
136 {
137 $itemsMap[$basketItem->getBasketCode()] = $itemKey;
138 }
139 }
140
141 /*
142 * todo: check if we still need this
143 if($arProduct["CURRENCY"] != $currency && \Bitrix\Main\Loader::includeModule('currency'))
144 {
145 $price = \CCurrencyRates::convertCurrency(
146 $arProduct["PRICE"],
147 $arProduct["CURRENCY"],
148 $currency
149 );
150 }
151 */
152
153 $r = $basket->refreshData();
154 if (!$r->isSuccess())
155 {
156 return $r;
157 }
158
160 foreach($basket as $basketItem)
161 {
162 $basketCode = $basketItem->getBasketCode();
163 $item = $params['CART_ITEMS'][$itemsMap[$basketCode]];
164
165 $basketItem->setField("NAME", $item['offerName']);
166//
167// if ($discount instanceof \Bitrix\Sale\Discount)
168// $discount->setBasketItemData($basketCode, $providerData[$basketCode]);
169 }
170
171 if($basket->count() == 0)
172 {
173 $result->addError(new Error(Loc::getMessage('SALE_YMARKET_ORDER_PRODUCTS_NOT_AVAILABLE')));
174 return $result;
175 }
176
177 if ($isStartField)
178 {
179 $hasMeaningfulFields = $order->hasMeaningfulField();
180 $res = $order->doFinalAction($hasMeaningfulFields);
181
182 if (!$res->isSuccess())
183 $result->addErrors($res->getErrors());
184 }
185
186 $result->setData(
187 array(
188 'ORDER' => $order,
189 'ITEMS_MAP' => $itemsMap
190 )
191 );
192
193 return $result;
194 }
195
196 public static function createShipment(\Bitrix\Sale\Order &$order, $deliveryId = 0, $deliveryPrice = false)
197 {
198 $shipments = $order->getShipmentCollection();
199
201 if($shipments->count() > 0)
202 foreach ($shipments as $shipment)
203 if(!$shipment->isSystem())
204 $shipment->delete();
205
206 $shipment = $shipments->createItem();
207
208 if(intval($deliveryId) > 0)
209 {
211 if($dlvSrv = \Bitrix\Sale\Delivery\Services\Manager::getObjectById($deliveryId))
212 $dlvName = $dlvSrv->getNameWithParent();
213 else
214 $dlvName = 'Not found ('.$deliveryId.')';
215
216 $shipment->setField('DELIVERY_ID', $deliveryId);
217 $shipment->setField('DELIVERY_NAME', $dlvName);
218 }
219
220 if($deliveryPrice !== false)
221 {
222 $shipment->setBasePriceDelivery(floatval($deliveryPrice), true);
223 }
224
225 $basket = $order->getBasket();
226
227 if($basket)
228 {
229 $shipmentItemCollection = $shipment->getShipmentItemCollection();
230 $basketItems = $basket->getBasketItems();
231
233 foreach ($basketItems as $basketItem)
234 {
236 $shipmentItem = $shipmentItemCollection->createItem($basketItem);
237 $shipmentItem->setQuantity($basketItem->getField('QUANTITY'));
238 }
239 }
240
241 return $shipment;
242 }
243 public static function createPayment(\Bitrix\Sale\Order &$order, $paySystemId = 0)
244 {
245 $payments = $order->getPaymentCollection();
246
248 if($payments->count() > 0)
249 {
250 foreach ($payments as $payment)
251 {
252 if($payment->isPaid())
253 $payment->setPaid("N");
254
255 $payment->delete();
256 }
257 }
258
259 $payment = $payments->createItem();
260
261 if(intval($paySystemId) > 0)
262 {
263 $psName = 'Not found ('.$paySystemId.')';
264
265 if($ps = PaySystem\Manager::getById($paySystemId))
266 $psName = $ps['NAME'];
267
268 $payment->setField('PAY_SYSTEM_ID', $paySystemId);
269 $payment->setField('PAY_SYSTEM_NAME', $psName);
270 }
271
272 $payment->setField("SUM", $order->getPrice());
273
274 return $payment;
275 }
276}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29