PageRenderTime 62ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/sale/lib/rest/synchronization/manager.php

https://gitlab.com/alexprowars/bitrix
PHP | 274 lines | 227 code | 46 blank | 1 comment | 14 complexity | e96797b85a4b018332ddd75003268781 MD5 | raw file
  1. <?php
  2. namespace Bitrix\Sale\Rest\Synchronization;
  3. use Bitrix\Main\Config\Option;
  4. use Bitrix\Main\Error;
  5. use Bitrix\Main\HttpRequest;
  6. use Bitrix\Main\Localization\Loc;
  7. use Bitrix\Sale\Result;
  8. Loc::loadMessages(__FILE__);
  9. class Manager
  10. {
  11. protected $action;
  12. protected $handlerExecuted;
  13. protected $client;
  14. protected $clientOAuth;
  15. protected $clientId;
  16. protected $clientSecret;
  17. protected $serviceUrl;
  18. protected $oauthKey;
  19. protected $accessToken;
  20. protected $refreshToken;
  21. /** @var HttpRequest */
  22. protected $request;
  23. protected static $instance = null;
  24. const B24_APP_GRANT_TYPE = 'refresh_token';
  25. const ACTION_UNDEFINED = 'undefined';
  26. const ACTION_DELETED = 'deleted';
  27. const ACTION_IMPORT = 'import';
  28. const END_POINT = '/bitrix/services/sale/synchronizer/push.php';
  29. public static function getInstance()
  30. {
  31. if(self::$instance === null)
  32. {
  33. self::$instance = new static();
  34. }
  35. return self::$instance;
  36. }
  37. public function isActive()
  38. {
  39. return Option::get("sale", "config_external_is_active")=='Y';
  40. }
  41. public function activate()
  42. {
  43. Option::set("sale", "config_external_is_active", 'Y');
  44. }
  45. public function deactivate()
  46. {
  47. Option::set("sale", "config_external_is_active", 'N');
  48. }
  49. public function pushHandlerExecuted($name)
  50. {
  51. $this->handlerExecuted[$name];
  52. }
  53. public function isExecutedHandler($name)
  54. {
  55. return is_set($this->handlerExecuted, $name);
  56. }
  57. public function checkDefaultSettings()
  58. {
  59. $result = new Result();
  60. $siteId='';
  61. $r = \CSite::GetList();
  62. while ($row = $r->fetch())
  63. if($row['ID']==$this->getDefaultSiteId())
  64. $siteId=$row['ID'];
  65. if($siteId=='')
  66. $result->addError(new Error(Loc::getMessage('MAN_ERROR_EMPTY_FIELD_SITE')));
  67. $deliverySystemId=0;
  68. foreach(\Bitrix\Sale\Delivery\Services\Manager::getActiveList() as $row)
  69. if($row['ID']==$this->getDefaultDeliverySystemId())
  70. $deliverySystemId = $row['ID'];
  71. if($deliverySystemId==0)
  72. $result->addError(new Error(Loc::getMessage('MAN_ERROR_EMPTY_FIELD_DELIVERY_SERVICES')));
  73. if(count(\Bitrix\Sale\PaySystem\Manager::getList(['filter'=>['ID'=>$this->getDefaultPaySystemId()]]))<=0)
  74. $result->addError(new Error(Loc::getMessage('MAN_ERROR_EMPTY_FIELD_PAY_SYSTEM')));
  75. if(count(\Bitrix\Sale\PersonType::getList(['filter'=>['ID'=>$this->getDefaultPersonTypeId()]]))<=0)
  76. $result->addError(new Error(Loc::getMessage('MAN_ERROR_EMPTY_FIELD_PERSON_TYPE')));
  77. if(count(\Bitrix\Sale\OrderStatus::getList(['filter'=>['ID'=>$this->getDefaultOrderStatusId()]]))<=0)
  78. $result->addError(new Error('MAN_ERROR_EMPTY_FIELD_ORDER_STATUS'));
  79. if(count(\Bitrix\Sale\DeliveryStatus::getList(['filter'=>['ID'=>$this->getDefaultDeliveryStatusId()]]))<=0)
  80. $result->addError(new Error('MAN_ERROR_EMPTY_FIELD_DELIVERY_STATUS'));
  81. $catalogList=[];
  82. if(\Bitrix\Main\Loader::includeModule('catalog'))
  83. {
  84. $catalogList = \Bitrix\Catalog\CatalogIblockTable::getList([
  85. 'select' => ['IBLOCK_ID', 'IBLOCK.NAME'],
  86. 'filter' => ['=IBLOCK.ACTIVE'=>'Y']
  87. ])->fetchAll();
  88. }
  89. if(!count($catalogList)>0)
  90. $result->addError(new Error(Loc::getMessage('MAN_ERROR_CATALOGS')));
  91. return $result;
  92. }
  93. public function getClient()
  94. {
  95. if(!$this->client)
  96. {
  97. $this->client = new Client(
  98. $this->getClientId(),
  99. $this->getClientSecret(),
  100. $this->getSchemeServiceUrl().'://'.$this->getServiceUrl()
  101. );
  102. }
  103. return $this->client;
  104. }
  105. public function setSchemeServiceUrl($code)
  106. {
  107. Option::set("sale", "config_external_scheme_service_url", $code);
  108. }
  109. public function getSchemeServiceUrl()
  110. {
  111. return Option::get("sale", "config_external_scheme_service_url", false);
  112. }
  113. public function setServiceUrl($code)
  114. {
  115. Option::set("sale", "config_external_service_url", $code);
  116. }
  117. public function getServiceUrl()
  118. {
  119. return Option::get("sale", "config_external_service_url", false);
  120. }
  121. public function getClientId()
  122. {
  123. return 'app.5c05614270fdc0.60242739';
  124. }
  125. public function getClientSecret()
  126. {
  127. return 'cvdpAuyaHdC9ngJHctyRwu2xFtZamw85P3CWV8mIg7ESBfrVIa';
  128. }
  129. public function setAccessToken($accessToken)
  130. {
  131. Option::set("sale", "config_external_access_token", $accessToken);
  132. }
  133. public function getAccessToken()
  134. {
  135. return Option::get("sale", "config_external_access_token", false);
  136. }
  137. public function setRefreshToken($refreshToken)
  138. {
  139. Option::set("sale", "config_external_refresh_token", $refreshToken);
  140. }
  141. public function getRefreshToken()
  142. {
  143. return Option::get("sale", "config_external_refresh_token", false);
  144. }
  145. public function setOauthKey($key)
  146. {
  147. Option::set("sale", "config_external_oauth_key", $key);
  148. }
  149. public function getOauthKey()
  150. {
  151. return Option::get("sale", "config_external_oauth_key", "");
  152. }
  153. public function setAction($action)
  154. {
  155. $this->action = $action;
  156. }
  157. public function getAction()
  158. {
  159. return $this->action;
  160. }
  161. public function getDefaultDeliverySystemId()
  162. {
  163. return (int)Option::get("sale", "config_external_default_delivery_system_id");
  164. }
  165. public function setDefaultDeliverySystemId($code)
  166. {
  167. Option::set("sale", "config_external_default_delivery_system_id", $code);
  168. }
  169. public function getDefaultPaySystemId()
  170. {
  171. return (int)Option::get("sale", "config_external_default_pay_system_id");
  172. }
  173. public function setDefaultPaySystemId($code)
  174. {
  175. Option::set("sale", "config_external_default_pay_system_id", $code);
  176. }
  177. public function getDefaultSiteId()
  178. {
  179. return Option::get("sale", "config_external_default_site_id");
  180. }
  181. public function setDefaultSiteId($code)
  182. {
  183. Option::set("sale", "config_external_default_site_id", $code);
  184. }
  185. public function getDefaultPersonTypeId()
  186. {
  187. return (int)Option::get("sale", "config_external_default_person_type_id");
  188. }
  189. public function setDefaultPersonTypeId($code)
  190. {
  191. Option::set("sale", "config_external_default_person_type_id", $code);
  192. }
  193. public function getDefaultOrderStatusId()
  194. {
  195. return Option::get("sale", "config_external_default_order_status_id");
  196. }
  197. public function setDefaultOrderStatusId($code)
  198. {
  199. Option::set("sale", "config_external_default_order_status_id", $code);
  200. }
  201. public function getDefaultDeliveryStatusId()
  202. {
  203. return Option::get("sale", "config_external_default_delivery_status_id");
  204. }
  205. public function setDefaultDeliveryStatusId($code)
  206. {
  207. Option::set("sale", "config_external_default_delivery_status_id", $code);
  208. }
  209. public function getTradePlatformsXmlId($siteId)
  210. {
  211. $r = unserialize(Option::get("sale", "config_external_trade_platforms_xml_id"), ['allowed_classes' => false]);
  212. return $r[$siteId];
  213. }
  214. public function setTradePlatformsXmlId($siteId, $code)
  215. {
  216. $r = unserialize(Option::get("sale", "config_external_trade_platforms_xml_id"), ['allowed_classes' => false]);
  217. $r[$siteId] = $code;
  218. Option::set("sale", "config_external_trade_platforms_xml_id", serialize($r));
  219. }
  220. public function isMarked()
  221. {
  222. return Option::get("sale", "config_external_order_marked", 'N') == 'Y';
  223. }
  224. public function marked($code)
  225. {
  226. Option::set("sale", "config_external_order_marked", $code);
  227. }
  228. }