Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
helper.php
1<?php
2
4
14
15Loc::loadMessages(__FILE__);
16
22class Helper
23{
29 public static function checkEnveronment()
30 {
31 $result = new Result();
32
33 if(!extension_loaded('ssh2'))
34 $result->addError( new Error(Loc::getMessage("SALE_EBAY_HLP_CHECK_ERROR_SSH2")));
35
36 if(!extension_loaded('SimpleXML'))
37 $result->addError( new Error(Loc::getMessage("SALE_EBAY_HLP_CHECK_ERROR_SIMPLEXML")));
38
39 return $result;
40 }
41
45 public static function getSftpPath()
46 {
47 return $_SERVER["DOCUMENT_ROOT"]."/bitrix/tradingplatforms/ebay/sftp";
48 }
49
54 public static function createFeedFileStructure()
55 {
56 $sftpDir = self::getSftpPath();
57 $directory = new Directory($sftpDir);
58
59 if(!$directory->isExists())
60 $directory->create();
61
62 foreach(array("product", "inventory", "image", "order-ack", "shipment") as $feedType)
63 {
64 $feedDir = new Directory($sftpDir."/".$feedType);
65
66 if(!$feedDir->isExists())
67 $feedDir->create();
68
69 foreach(array("xml", "tmp", "zip") as $stage)
70 {
71 $stageDir = new Directory($sftpDir."/".$feedType."/".$stage);
72
73 if(!$stageDir->isExists())
74 $stageDir->create();
75 }
76 }
77
78 return true;
79 }
80
85 public static function installEvents()
86 {
87 $dbEvent = \CEventMessage::GetList("id", "asc", Array("EVENT_NAME" => "SALE_EBAY_ERROR"));
88
89 if(!($dbEvent->Fetch()))
90 {
91 $langs = \CLanguage::GetList();
92 while($lang = $langs->Fetch())
93 {
94 $lid = $lang["LID"];
95 $obEventType = new \CEventType;
96 $obEventType->Add(array(
97 "EVENT_NAME" => "SALE_EBAY_ERROR",
98 "NAME" => Loc::getMessage("SALE_EBAY_HLP_EVNT_TYPE_ERROR"),
99 "LID" => $lid,
100 "DESCRIPTION" =>"
101 #ERROR_TYPE# - ".Loc::getMessage("SALE_EBAY_HLP_EVNT_TYPE_ERROR_TYPE")."
102 #ERROR_DETAILS# - ".Loc::getMessage("SALE_EBAY_HLP_EVNT_TYPE_ERROR_DETAIL")."
103 #EMAIL_FROM# - ".Loc::getMessage("SALE_EBAY_HLP_EVNT_TYPE_FROM")."
104 #EMAIL_TO# - ".Loc::getMessage("SALE_EBAY_HLP_EVNT_TYPE_TO")."
105 #BCC# - ".Loc::getMessage("SALE_EBAY_HLP_EVNT_TYPE_BCC")
106 ));
107
108 $arSites = array();
109 $sites = \CSite::GetList('', '', Array("LANGUAGE_ID"=>$lid));
110 while ($site = $sites->Fetch())
111 $arSites[] = $site["LID"];
112
113 if(count($arSites) > 0)
114 {
115 $arr = array();
116 $arr["ACTIVE"] = "Y";
117 $arr["EVENT_NAME"] = "SALE_EBAY_ERROR";
118 $arr["LID"] = $arSites;
119 $arr["EMAIL_FROM"] = "#DEFAULT_EMAIL_FROM#";
120 $arr["EMAIL_TO"] = "#EMAIL_TO#";
121 $arr["BCC"] = "#BCC#";
122 $arr["SUBJECT"] = "#SITE_NAME# ".Loc::getMessage("SALE_EBAY_EVNT_MSG_SBUJ']").".";
123 $arr["BODY_TYPE"] = "text";
124 $arr["MESSAGE"] =
125 Loc::getMessage("SALE_EBAY_EVNT_MSG_INFO_SITE")." #SITE_NAME#.\n\n".
126 Loc::getMessage("SALE_EBAY_EVNT_MSG").":\n\n".
127 "#ERROR_TYPE#\n\n".
128 "#ERROR_DETAILS#";
129
130 $obTemplate = new \CEventMessage;
131 $obTemplate->Add($arr);
132 }
133 }
134 }
135
136 return true;
137 }
138
142 public static function getEbayOrderStatuses()
143 {
144 return array(
145 "Active",
146 "Completed",
147 "Canceled",
148 "Inactive"
149 );
150 }
151
158 public static function onSaleDeductOrder($orderId, $val)
159 {
160 if($val != "Y")
161 return false;
162
163 $order = \CSaleOrder::GetByID($orderId);
164
165 if($order["XML_ID"] == '' || mb_substr($order["XML_ID"], 0, 4) != Ebay::TRADING_PLATFORM_CODE)
166 return false;
167
168 $ebayOrderId = mb_substr($order["XML_ID"], mb_strlen(Ebay::TRADING_PLATFORM_CODE) + 1);
169
170 $shipmentInfo = array();
171 $trackingInfo = array();
172
173 if($order["TRACKING_NUMBER"] <> '')
174 {
175 $ebayDelivery = "Other";
176 $ebay = \Bitrix\Sale\TradingPlatform\Ebay\Ebay::getInstance();
177 $settings = $ebay->getSettings();
178
179 if(isset($settings[$order["LID"]]["DELIVERY"]) && is_array($settings[$order["LID"]]["DELIVERY"]))
180 {
181 foreach($settings[$order["LID"]]["DELIVERY"] as $eDelivery => $bDelivery)
182 {
183 if($bDelivery == $order["DELIVERY_ID"])
184 {
185 $ebayDelivery = $eDelivery;
186 break;
187 }
188 }
189 }
190
191 $trackingInfo = array(
192 "SERVICE" => $ebayDelivery,
193 "NUMBER" => $order["TRACKING_NUMBER"]
194 );
195 }
196
197 $orderLineItemIds = array();
198
199 $dbBasket = \CSaleBasket::GetList(
200 array(),
201 array("ORDER_ID" => $orderId),
202 false,
203 false,
204 array("XML_ID")
205 );
206
207 while ($arBasket = $dbBasket->GetNext())
208 $orderLineItemIds[] = $arBasket["XML_ID"];
209
210 foreach($orderLineItemIds as $orderLineItemId)
211 {
212 $tmpShipmentInfo = array(
213 "ORDER_ID" => $ebayOrderId,
214 "ORDER_LINE_ITEM_ID" => $orderLineItemId,
215 );
216
217 if(!empty($trackingInfo))
218 $tmpShipmentInfo["TRACKING"] = $trackingInfo;
219
220 $shipmentInfo[] = $tmpShipmentInfo;
221 }
222
223 $ebayFeed = Manager::createFeed("SHIPMENT", $order["LID"]);
224 $ebayFeed->setSourceData($shipmentInfo);
225 $ebayFeed->exchangeData("");
226
227 return true;
228 }
229
235 public static function OnEventLogGetAuditTypes()
236 {
237 $prefix = 'eBay: ';
238
239 $result = array(
240 "EBAY_FEED_ERROR" => Loc::getMessage("SALE_EBAY_AT_FEED_ERROR"),
241 "EBAY_AGENT_FEED_STARTED" => Loc::getMessage("SALE_EBAY_AT_AGENT_FEED_STARTED"),
242 "EBAY_FEED_CREATED" => Loc::getMessage("SALE_EBAY_AT_FEED_CREATED"),
243 "EBAY_DATA_PROCESSOR_ORDER_ERROR" => Loc::getMessage("SALE_EBAY_AT_DATA_PROCESSOR_ORDER_ERROR"),
244 "EBAY_DATA_PROCESSOR_ORDER_PROCESSED" => Loc::getMessage("SALE_EBAY_AT_DATA_PROCESSOR_ORDER_PROCESSED"),
245 "EBAY_DATA_PROCESSOR_SFTPQUEUE_SEND" => Loc::getMessage("SALE_EBAY_AT_DATA_PROCESSOR_SFTPQUEUE_SEND"),
246 "EBAY_DATA_SOURCE_ORDERFILE_RECEIVED" => Loc::getMessage("SALE_EBAY_AT_DATA_SOURCE_ORDERFILE_RECEIVED"),
247 "EBAY_DATA_PROCESSOR_ORDER_CREATED" => Loc::getMessage("SALE_EBAY_AT_DATA_PROCESSOR_ORDER_CREATED"),
248 "EBAY_DATA_PROCESSOR_ORDER_PROCESSING" => Loc::getMessage("SALE_EBAY_AT_DATA_PROCESSOR_ORDER_PROCESSING"),
249 "EBAY_DATA_SOURCE_RESULTS_RECEIVED" => Loc::getMessage("SALE_EBAY_AT_DATA_SOURCE_RESULTS_RECEIVED"),
250 "EBAY_DATA_SOURCE_RESULTS_ERROR" => Loc::getMessage("SALE_EBAY_AT_DATA_SOURCE_RESULTS_ERROR"),
251 "EBAY_AGENT_ADDING_RESULT" => Loc::getMessage("SALE_EBAY_AT_AGENT_ADDING_RESULT"),
252 "EBAY_FEED_RESULTS_ERROR" => Loc::getMessage("SALE_EBAY_AT_FEED_RESULTS_ERROR"),
253 "EBAY_POLICY_REQUEST_ERROR" => Loc::getMessage("SALE_EBAY_AT_POLICY_REQUEST_ERROR"),
254 "EBAY_POLICY_REQUEST_HTTP_ERROR" => Loc::getMessage("SALE_EBAY_AT_POLICY_REQUEST_HTTP_ERROR"),
255 "EBAY_DATA_PROCESSOR_ORDER_SKIPPED" => Loc::getMessage("SALE_EBAY_AT_DATA_PROCESSOR_ORDER_SKIPPED"),
256 "EBAY_DATA_PROCESSOR_ORDER_ALREADY_EXIST" => Loc::getMessage("SALE_EBAY_AT_DATA_PROCESSOR_ORDER_ALREADY_EXIST"),
257 "EBAY_DATA_PROCESSOR_ORDER_PROCESSING_TRANSACTION_ITEM_NOT_FOUND" => Loc::getMessage("SALE_EBAY_AT_DATA_PROCESSOR_ORDER_PROCESSING_TR_NOT_FOUND"),
258 "EBAY_DATA_PROCESSOR_ORDER_PROCESSING_TRANSACTION_ITEM_SKU_NOT_FOUND" => Loc::getMessage("SALE_EBAY_AT_DATA_PROCESSOR_ORDER_PROCESSING_TRANSACTION_ITEM_SKU_NOT_FOUND"),
259 "EBAY_DATA_PROCESSOR_ORDER_TRANSACTION_ITEM_CREATE_ERROR" => Loc::getMessage("SALE_EBAY_AT_DATA_PROCESSOR_ORDER_TRANSACTION_ITEM_CREATE_ERROR"),
260 "EBAY_DATA_PROCESSOR_ORDER_CREATE_ERROR_SET_BASKET" => Loc::getMessage("SALE_EBAY_AT_DATA_PROCESSOR_ORDER_CREATE_ERROR_SET_BASKET"),
261 "EBAY_DATA_PROCESSOR_ORDER_CANCELING_ERROR" => Loc::getMessage("SALE_EBAY_AT_DATA_PROCESSOR_ORDER_CANCELING_ERROR"),
262 "EBAY_DATA_PROCESSOR_ORDER_DEDUCTIOING_ERROR" => Loc::getMessage("SALE_EBAY_AT_DATA_PROCESSOR_ORDER_DEDUCTIOING_ERROR"),
263 "EBAY_DATA_PROCESSOR_ORDER_CHANGE_STATUS_ERROR" => Loc::getMessage("SALE_EBAY_AT_DATA_PROCESSOR_ORDER_CHANGE_STATUS_ERROR"),
264 "EBAY_DATA_PROCESSOR_ORDER_SAVE_ERROR" => Loc::getMessage("SALE_EBAY_AT_DATA_PROCESSOR_ORDER_SAVE_ERROR"),
265 "EBAY_DATA_PROCESSOR_ORDER_CORR_SAVE_ERROR" => Loc::getMessage("SALE_EBAY_AT_DATA_PROCESSOR_ORDER_CORR_SAVE_ERROR"),
266 "EBAY_DATA_PROCESSOR_SFTPQUEUE_FLUSHING" => Loc::getMessage("SALE_EBAY_AT_DATA_PROCESSOR_SFTPQUEUE_FLUSHING"),
267 "EBAY_SFTP_TOKEN_EXP" => Loc::getMessage("SALE_EBAY_AT_SFTP_TOKEN_EXP"),
268 );
269
270 array_walk($result, function(&$value, $key, $prefix) {
271 $value = $prefix.$value;
272 },
273 $prefix
274 );
275
276 return $result;
277 }
278
284 public static function getSftp($siteId)
285 {
286 if($siteId == '')
287 throw new ArgumentNullException("siteId");
288
289 static $sftp = array();
290
291 if(!isset($sftp[$siteId]))
292 {
293 $ebay = \Bitrix\Sale\TradingPlatform\Ebay\Ebay::getInstance();
294 $settings = $ebay->getSettings();
295 $host = isset($settings[$siteId]["SFTP_HOST"]) ? $settings[$siteId]["SFTP_HOST"] : "mip.ebay.com";
296 $port = isset($settings[$siteId]["SFTP_PORT"]) ? $settings[$siteId]["SFTP_PORT"] : 22;
297 $fingerprint = $settings[$siteId]["SFTP_HOST_FINGERPRINT"] <> '' ? $settings[$siteId]["SFTP_HOST_FINGERPRINT"] : "DD1FEE728C2E1FF2AACC2724929C3CF1";
298
299 if(!empty($settings[$siteId]["SFTP_TOKEN_EXP"]) && date('c') > date($settings[$siteId]["SFTP_TOKEN_EXP"]))
300 {
301 Ebay::log(Logger::LOG_LEVEL_ERROR, "EBAY_SFTP_TOKEN_EXP", 'SFTP token', Loc::getMessage('SALE_EBAY_AT_SFTP_TOKEN_EXP_MESSAGE'), $siteId);
302 throw new SystemException(Loc::getMessage('SALE_EBAY_AT_SFTP_TOKEN_EXP_MESSAGE'));
303 }
304 else
305 {
306 $sftp[$siteId] = new Sftp(
307 $settings[$siteId]["SFTP_LOGIN"],
308 $settings[$siteId]["SFTP_PASS"],
309 $host,
310 $port,
311 $fingerprint
312 );
313 }
314 }
315
316 return $sftp[$siteId];
317 }
318
327 public static function getEbayCategoryVariations($ebayCategoryId, $siteId, $localInfoOnly = false)
328 {
329 $result = array();
330
331 $categoriesVarResult = CategoryVariationTable::getList( array(
332 'select' => array('ID', 'NAME', 'REQUIRED'),
333 'order' => array('NAME' =>'ASC'),
334 'filter' => array("=CATEGORY_ID" => $ebayCategoryId),
335 'group' => array('NAME')
336 ));
337
338 while($var = $categoriesVarResult->fetch())
339 $result[$var['ID']] = $var;
340
341 if(empty($result) && !$localInfoOnly)
342 {
343 $categories = new \Bitrix\Sale\TradingPlatform\Ebay\Api\Categories($siteId);
344 $rfrCount = $categories->refreshVariationsTableData(array($ebayCategoryId));
345
346 if(intval($rfrCount) > 0)
347 $result = self::getEbayCategoryVariations($ebayCategoryId, $siteId, true);
348 }
349
350 return $result;
351 }
352}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static log($level, $type, $itemId, $description, $siteId)
Definition ebay.php:147