Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
synchronizer.php
1<?php
2
3
5
6
7use Bitrix\Crm\Timeline\OrderController;
16use Bitrix\Sale;
17
18class Synchronizer extends Engine\Controller
19{
20 protected function isB24()
21 {
22 return ModuleManager::isModuleInstalled('crm');
23 }
24
25 public function getDefaultSettingsAction()
26 {
27 return $this->getSettings();
28 }
29
30 public function setDefaultSettingsAction()
31 {
32 $r = new Result();
33 $manager = new \Bitrix\Sale\Rest\Synchronization\Manager();
34
35
36
37 $personId = \Bitrix\Sale\PersonType::getList(['select'=>['ID', 'NAME'],'order'=>'ID', 'limit'=>1])->fetch()['ID'];
38
39 if((int)$personId>0)
40 {
41 $manager->setDefaultPersonTypeId($personId);
42 }
43 else
44 {
45 $r->addError(new Error('person type not found'));
46 }
47
48 $ps = Manager::getList(
49 [
50 'select'=>['ID'],
51 'filter'=>['!ID'=>Manager::getInnerPaySystemId(), 'ENTITY_REGISTRY_TYPE'=>'ORDER'],
52 'order'=>['ID'=>'ASC'],
53 'limit'=>1
54 ]
55 )->fetchAll();
56
57 $paySystemId = isset($ps[0])? $ps[0]['ID']:0;
58 if((int)$paySystemId>0)
59 {
60 $manager->setDefaultPaySystemId($paySystemId);
61 }
62 else
63 {
64 $r->addError(new Error('paysystem not found'));
65 }
66
68 if((int)$deliverySystemId>0)
69 {
70 $manager->setDefaultDeliverySystemId($deliverySystemId);
71 }
72 else
73 {
74 $r->addError(new Error('deliverysystem not found'));
75 }
76
77 $manager->setDefaultSiteId(SITE_ID);
78 $manager->setDefaultDeliveryStatusId(DeliveryStatus::getInitialStatus());
79 $manager->setDefaultOrderStatusId(OrderStatus::getInitialStatus());
80
81 if($r->isSuccess())
82 {
83 $manager->activate();
84
85 return true;
86 }
87 else
88 {
89 $manager->deactivate();
90 $this->addErrors($r->getErrors());
91
92 return null;
93 }
94 }
95
96 public function isActiveAction()
97 {
98 $instance = \Bitrix\Sale\Rest\Synchronization\Manager::getInstance();
99 return $instance->isActive();
100 }
101
102 protected function getSettings()
103 {
104 $manager = new \Bitrix\Sale\Rest\Synchronization\Manager();
105
106 $internal = [];
107 foreach(\Bitrix\Sale\PersonType::getList(['select'=>['ID', 'NAME']]) as $row)
108 $internal['PERSON_TYPE'][$row['ID']] = $row;
109
110 foreach(\Bitrix\Sale\PaySystem\Manager::getList(['select'=>['ID', 'NAME']])->fetchAll() as $row)
111 $internal['PAY_SYSTEMS'][$row['ID']] = $row;
112
113 foreach(\Bitrix\Sale\Delivery\Services\Manager::getActiveList() as $row)
114 $internal['DELIVERY_SYSTEMS'][$row['ID']] = $row;
115
116 $r = \CSite::GetList();
117 while ($row = $r->fetch())
118 $internal['SITES'][$row['ID']] = $row;
119
120 foreach(OrderStatus::getList(['select' => ['*', 'NAME' => 'Bitrix\Sale\Internals\StatusLangTable:STATUS.NAME'],
121 'filter' => [
122 '=Bitrix\Sale\Internals\StatusLangTable:STATUS.LID' => LANGUAGE_ID
123 ]]) as $row)
124 $internal['ORDER_STATUSES'][$row['ID']] = $row;
125
126 foreach(DeliveryStatus::getList(['select' => ['*', 'NAME' => 'Bitrix\Sale\Internals\StatusLangTable:STATUS.NAME'],
127 'filter' => [
128 '=Bitrix\Sale\Internals\StatusLangTable:STATUS.LID' => LANGUAGE_ID
129 ]]) as $row)
130 $internal['DELIVERY_STATUSES'][$row['ID']] = $row;
131
132 $catalogList = [];
133 if(\Bitrix\Main\Loader::includeModule('catalog'))
134 {
135 $r = \Bitrix\Catalog\CatalogIblockTable::getList([
136 'select' => ['IBLOCK_ID', 'IBLOCK.NAME'],
137 'filter' => ['=IBLOCK.ACTIVE'=>'Y']]);
138
139 while($row = $r->fetch())
140 $catalogList[] = ['id'=>$row['IBLOCK_ID'], 'name'=>$row['CATALOG_CATALOG_IBLOCK_IBLOCK_NAME']];
141 }
142
143 $site=[];
144 if(isset($internal['SITES'][$manager->getDefaultSiteId()]))
145 $site = $internal['SITES'][$manager->getDefaultSiteId()];
146
147 $paySystem=[];
148 if(isset($internal['PAY_SYSTEMS'][$manager->getDefaultPaySystemId()]))
149 $paySystem = $internal['PAY_SYSTEMS'][$manager->getDefaultPaySystemId()];
150
151 $deliverySystem=[];
152 if(isset($internal['DELIVERY_SYSTEMS'][$manager->getDefaultDeliverySystemId()]))
153 $deliverySystem = $internal['DELIVERY_SYSTEMS'][$manager->getDefaultDeliverySystemId()];
154
155 $personType=[];
156 if(isset($internal['PERSON_TYPE'][$manager->getDefaultPersonTypeId()]))
157 $personType = $internal['PERSON_TYPE'][$manager->getDefaultPersonTypeId()];
158
159 $orderStatuses=[];
160 if(isset($internal['ORDER_STATUSES'][$manager->getDefaultOrderStatusId()]))
161 $orderStatuses = $internal['ORDER_STATUSES'][$manager->getDefaultOrderStatusId()];
162
163 $deliveryStatus=[];
164 if(isset($internal['DELIVERY_STATUSES'][$manager->getDefaultDeliveryStatusId()]))
165 $deliveryStatus = $internal['DELIVERY_STATUSES'][$manager->getDefaultDeliveryStatusId()];
166
167
168 return [
169 'synchronizer'=>[
170 'isActive'=>$manager->isActive() && $manager->checkDefaultSettings()->isSuccess()?'Y':'N',
171 'site'=>count($site)>0? ['id'=>$site['ID'], 'name'=>$site['NAME']]:[],
172 'paySystem'=>count($paySystem)>0? ['id'=>$paySystem['ID'], 'name'=>$paySystem['NAME']]:[],
173 'deliverySystem'=>count($deliverySystem)>0? ['id'=>$deliverySystem['ID'], 'name'=>$deliverySystem['NAME']]:[],
174 'personType'=>count($personType)>0? ['id'=>$personType['ID'], 'name'=>$personType['NAME']]:[],
175 'orderStatus'=>count($orderStatuses)>0? ['id'=>$orderStatuses['ID'], 'name'=>$orderStatuses['NAME']]:[],
176 'deliveryStatus'=>count($deliveryStatus)>0? ['id'=>$deliveryStatus['ID'], 'name'=>$deliveryStatus['NAME']]:[],
177 'catalogs'=>$catalogList
178 ],
179 ];
180 }
181
182 public function addTimelineAfterOrderModifyAction($orderId, array $params)
183 {
184 if ($this->isB24())
185 {
186 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
187
189 $orderClass = $registry->getOrderClassName();
190
192 $order = $orderClass::load($orderId);
193 if($order)
194 {
195 OrderController::getInstance()->afterModifyExternalEntity($order->getId(), ['TYPE'=>$params['type'], 'MESSAGE'=>$params['message']]);
196 }
197 }
198 }
199}
static getList(array $parameters=[])
static getList(array $parameters=array())