1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
controller.php
См. документацию.
1<?php
2
3
4namespace Bitrix\Sale\Controller;
5
6
7use Bitrix\Main\Engine;
8use Bitrix\Main\Error;
9use Bitrix\Main\Loader;
10use Bitrix\Main\ModuleManager;
11use Bitrix\Main\NotImplementedException;
12use Bitrix\Rest\RestException;
13use Bitrix\Sale\Helpers\Order\Builder\SettingsContainer;
14use Bitrix\Sale\Rest\ModificationFieldsBase;
15use Bitrix\Sale\Result;
16use Bitrix\Sale\TradeBindingEntity;
17/*
18 * Error code notation x(category1) xxx(category2) xxx(code category) xxxxx(code) - 2 000 403 00010
19 * category1:
20 * Intrnalizer - 1
21 * Controller - 2
22 * Externalazer - 3
23 * */
25{
26 protected function isCrmModuleInstalled()
27 {
28 return ModuleManager::isModuleInstalled('crm');
29 }
30
32 {
33 $r = $this->checkPermission($action->getName());
34 if($r->isSuccess())
35 {
36 if($this->isCrmModuleInstalled() && Loader::includeModule('crm'))
37 {
38 $internalizer = \Bitrix\Crm\Order\Rest\Internalizer::buildByAction($action, [], $this->getScope());
39 }
40 else
41 {
43 }
44
45 $r = $internalizer->process();
46
47 if($r->isSuccess())
48 {
49 $action->setArguments($r->getData()['data']);
50 return parent::processBeforeAction($action);
51 }
52 else
53 {
54 $this->addErrors($r->getErrors());
55 return null;
56 }
57 }
58 else
59 {
60 $this->addErrors($r->getErrors());
61 return null;
62 }
63 }
64
66 {
67 $externalizer = null;
68 if($this->errorCollection->count()==0)
69 {
70 if($result instanceof Engine\Response\DataType\Page || is_array($result))
71 {
73 [$result->getId()=>$result->getItems()]:$result;
74
75 if($this->isCrmModuleInstalled() && Loader::includeModule('crm'))
76 {
77 $externalizer = \Bitrix\Crm\Order\Rest\Externalizer::buildByAction($action, $data, $this->getScope());
78 }
79 else
80 {
82 }
83 }
84 }
85 else
86 {
87 return parent::processAfterAction($action, $result);
88 }
89
90 if($externalizer instanceof ModificationFieldsBase)
91 {
92 if($this->getScope() == Engine\Controller::SCOPE_REST)
93 {
94 // nothing
95 }
96 else if($this->getScope() == Engine\Controller::SCOPE_AJAX)
97 {
98 $externalizer->setFormat([
101 ]);
102 }
103
104 if($this->getScope() == Engine\Controller::SCOPE_REST)
105 {
106 return $result instanceof Engine\Response\DataType\Page ?
107 $externalizer->getPage($result):$externalizer;
108 }
109 else if($this->getScope() == Engine\Controller::SCOPE_AJAX)
110 {
111 return $externalizer->toArray();
112 }
113 }
114
115 return parent::processAfterAction($action, $result);
116 }
117
118 protected static function getApplication()
119 {
121 global $APPLICATION;
122
123 return $APPLICATION;
124 }
125
126 protected static function getNavData($start, $bORM = false)
127 {
128 if($start >= 0)
129 {
130 return ($bORM ?
131 ['limit' => \IRestService::LIST_LIMIT, 'offset' => intval($start)]
132 :['nPageSize' => \IRestService::LIST_LIMIT, 'iNumPage' => intval($start / \IRestService::LIST_LIMIT) + 1]
133 );
134 }
135 else
136 {
137 return ($bORM ?
138 ['limit' => \IRestService::LIST_LIMIT]
139 :['nTopCount' => \IRestService::LIST_LIMIT]
140 );
141 }
142 }
143
145 {
147
148 return ($this->isCrmModuleInstalled() && Loader::includeModule('crm'))
149 ? new \Bitrix\Crm\Order\Builder\OrderBuilderRest($settings)
150 : new \Bitrix\Sale\Helpers\Order\Builder\OrderBuilderRest($settings);
151 }
152
153 protected function getSettingsContainerDefault()
154 {
155 return new SettingsContainer([
156 'deleteClientsIfNotExists' => true,
157 'deleteTradeBindingIfNotExists' => true,
158 'deletePaymentIfNotExists' => true,
159 'deleteShipmentIfNotExists' => true,
160 'deleteShipmentItemIfNotExists' => true,
161 'deletePropertyValuesIfNotExists' => true,
162 'createDefaultPaymentIfNeed' => false,
163 'createDefaultShipmentIfNeed' => false,
164 'createUserIfNeed' => SettingsContainer::SET_ANONYMOUS_USER,
165 'cacheProductProviderData' => false,
166 'propsFiles' => $this->getFielsPropertyValuesFromRequest(),
167 'acceptableErrorCodes' => []
168 ]);
169 }
170
172 {
173 $orderProperties = [];
174
175 if (isset($this->request->getFileList()['fields']['PROPERTIES']))
176 {
177 foreach ($this->request->getFileList()['fields']['PROPERTIES'] as $orderPropId => $arFileData)
178 {
179 if (is_array($arFileData))
180 {
181 foreach ($arFileData as $paramName => $value)
182 {
183 if (is_array($value))
184 {
185 foreach ($value as $nIndex => $val)
186 {
187 if ($arFileData["name"][$nIndex] <> '')
188 {
189 $orderProperties[$orderPropId][$nIndex][$paramName] = $val;
190 }
191 }
192 }
193 else
194 {
195 $orderProperties[$orderPropId][$paramName] = $value;
196 }
197 }
198 }
199 }
200 }
201
202 return $orderProperties;
203 }
204
205 protected function toArray(\Bitrix\Sale\Order $order, array $fields=[])
206 {
207 //добавляем те поля, к существующим полям сущности, которые у неё отсутствуют
208 $fields = array_merge($fields, $this->getAdditionalFields($order));
209
210 if($this->isCrmModuleInstalled() && Loader::includeModule('crm'))
211 {
212 $director = new \Bitrix\Crm\Order\Rest\Normalizer\Director();
213 $normalizer = new \Bitrix\Crm\Order\Rest\Normalizer\ObjectNormalizer($fields);
214 }
215 else
216 {
217 $director = new \Bitrix\Sale\Rest\Normalizer\Director();
218 $normalizer = new \Bitrix\Sale\Rest\Normalizer\ObjectNormalizer($fields);
219 }
220
221 return $director->normalize($normalizer, $order);
222 }
223
224 private function getAdditionalFields(\Bitrix\Sale\Order $order)
225 {
226 $ixInternal = [];
227 //region fill internal Index
228 foreach (\Bitrix\Sale\PersonType::getList(['select'=>['ID', 'XML_ID']]) as $row)
229 {
230 $ixInternal['personType'][$row['ID']] = $row['XML_ID'];
231 }
232
233 foreach (\Bitrix\Sale\OrderStatus::getList(['select'=>['ID', 'XML_ID']]) as $row)
234 {
235 $ixInternal['orderStatus'][$row['ID']] = $row['XML_ID'];
236 }
237
238 foreach (\Bitrix\Sale\Property::getList(['select'=>['ID', 'XML_ID']])->fetchAll() as $row)
239 {
240 $ixInternal['properties'][$row['ID']] = $row['XML_ID'];
241 }
242
243 foreach (\Bitrix\Sale\PaySystem\Manager::getList(['select'=>['ID', 'XML_ID', 'IS_CASH']])->fetchAll() as $row)
244 {
245 $ixInternal['paySystems'][$row['ID']]['XML_ID'] = $row['XML_ID'];
246 $ixInternal['paySystems'][$row['ID']]['IS_CASH'] = $row['IS_CASH'];
247 }
248
249 foreach (\Bitrix\Sale\Delivery\Services\Manager::getActiveList() as $row)
250 {
251 $ixInternal['deliverySystems'][$row['ID']] = $row['XML_ID'];
252 }
253
254 foreach (\Bitrix\Sale\DeliveryStatus::getList(['select'=>['ID', 'XML_ID']]) as $row)
255 {
256 $ixInternal['deliveryStatus'][$row['ID']] = $row['XML_ID'];
257 }
258
259 foreach (\Bitrix\Sale\TradingPlatformTable::getList(['select'=>['ID', 'XML_ID']])->fetchAll() as $row)
260 {
261 $ixInternal['tradingPlatform'][$row['ID']] = $row['XML_ID'];
262 }
263 //endregion
264
265 $r['ORDER'][$order->getInternalId()] = [
266 'PERSON_TYPE_XML_ID' => $ixInternal['personType'][$order->getPersonTypeId()] ?? null,
267 'STATUS_XML_ID' => $ixInternal['orderStatus'][$order->getField('STATUS_ID')] ?? null,
268 ];
269
270 foreach ($order->getPropertyCollection() as $property)
271 {
272 $r['PROPERTIES'][$property->getInternalIndex()] = [
273 'ORDER_PROPS_XML_ID' => $ixInternal['properties'][$property->getPropertyId()] ?? null,
274 ];
275 }
276
277 foreach ($order->getPaymentCollection() as $payment)
278 {
279 $r['PAYMENTS'][$payment->getInternalIndex()] = [
280 'PAY_SYSTEM_XML_ID' => $ixInternal['paySystems'][$payment->getPaymentSystemId()]['XML_ID'] ?? null,
281 'PAY_SYSTEM_IS_CASH' => $ixInternal['paySystems'][$payment->getPaymentSystemId()]['IS_CASH'] ?? null,
282 ];
283 }
284
286 foreach ($order->getShipmentCollection() as $shipment)
287 {
288 $shipmentIndex = $shipment->getInternalIndex();
289 $r['SHIPMENTS'][$shipmentIndex] = [
290 'DELIVERY_XML_ID' => $ixInternal['deliverySystems'][$shipment->getDeliveryId()] ?? null,
291 'STATUS_XML_ID' => $ixInternal['deliveryStatus'][$shipment->getField('STATUS_ID')] ?? null,
292 ];
293 }
294
296 foreach ($order->getTradeBindingCollection() as $binding)
297 {
298 if ($binding->getTradePlatform() !== null)
299 {
300 $r['TRADE_BINDINGS'][$binding->getInternalIndex()] = [
301 'TRADING_PLATFORM_XML_ID' => $ixInternal['tradingPlatform'][$binding->getTradePlatform()->getId()] ?? null,
302 ];
303 }
304 }
305
306 return $r;
307 }
308
309 private function checkPermission($name)
310 {
311 $r = new Result();
312
313 if($name == 'add')
314 {
315 $r = $this->checkCreatePermissionEntity();
316 }
317 elseif ($name == 'update')
318 {
319 $r = $this->checkUpdatePermissionEntity();
320 }
321 elseif ($name == 'list')
322 {
323 $r = $this->checkReadPermissionEntity();
324 }
325 elseif ($name == 'getfields'){}
326 elseif ($name == 'get')
327 {
328 $r = $this->checkReadPermissionEntity();
329 }
330 elseif ($name == 'delete')
331 {
332 $r = $this->checkDeletePermissionEntity();
333 }
334 elseif ($name == 'modify')
335 {
336 $r = $this->checkModifyPermissionEntity();
337 }
338 elseif ($name == 'tryadd')
339 {
340 $r = $this->checkCreatePermissionEntity();
341 }
342 elseif ($name == 'tryupdate')
343 {
344 $r = $this->checkUpdatePermissionEntity();
345 }
346 elseif ($name == 'trymodify')
347 {
348 $r = $this->checkModifyPermissionEntity();
349 }
350 else
351 {
352 $r = $this->checkPermissionEntity($name);
353 }
354
355 return $r;
356 }
357
358 protected function checkReadPermissionEntity()
359 {
360 $r = new Result();
361
362 $saleModulePermissions = self::getApplication()->GetGroupRight("sale");
363 if ($saleModulePermissions == "D")
364 {
365 $r->addError(new Error('Access Denied', 200040300010));
366 }
367 return $r;
368 }
369
370 protected function checkModifyPermissionEntity()
371 {
372 $r = new Result();
373
374 $saleModulePermissions = self::getApplication()->GetGroupRight("sale");
375 if ($saleModulePermissions < "W")
376 {
377 $r->addError(new Error('Access Denied', 200040300020));
378 }
379 return $r;
380 }
381
382 protected function checkCreatePermissionEntity()
383 {
384 return $this->checkModifyPermissionEntity();
385 }
386
387 protected function checkUpdatePermissionEntity()
388 {
389 return $this->checkModifyPermissionEntity();
390 }
391
392 protected function checkDeletePermissionEntity()
393 {
394 return $this->checkModifyPermissionEntity();
395 }
396
397 protected function checkPermissionEntity($name)
398 {
399 throw new NotImplementedException('Check permission entity. The method '.$name.' is not implemented.');
400 }
401}
global $APPLICATION
Определения include.php:80
addErrors(array $errors)
Определения controller.php:1083
Определения error.php:15
toArray(\Bitrix\Sale\Order $order, array $fields=[])
Определения controller.php:205
static getApplication()
Определения controller.php:118
processBeforeAction(Engine\Action $action)
Определения controller.php:31
checkPermissionEntity($name)
Определения controller.php:397
getFielsPropertyValuesFromRequest()
Определения controller.php:171
processAfterAction(Engine\Action $action, $result)
Определения controller.php:65
static getNavData($start, $bORM=false)
Определения controller.php:126
getBuilder(SettingsContainer $settings=null)
Определения controller.php:144
static getList(array $parameters=[])
Определения persontype.php:100
static buildByAction(Action $action, $data=[], $scope='')
Определения modificationfieldsbase.php:24
static getList(array $parameters=array())
Определения statusbase.php:29
const LIST_LIMIT
Определения rest.php:897
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$start
Определения get_search.php:9
$name
Определения menu_edit.php:35
Определения action.php:3
Определения aliases.php:54
$payment
Определения payment.php:14
$order
Определения payment.php:8
$settings
Определения product_settings.php:43
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$saleModulePermissions
Определения tools.php:21
$val
Определения options.php:1793
$action
Определения file_dialog.php:21
$fields
Определения yandex_run.php:501