Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
helper.php
1<?php
2
4
5use Bitrix\Main\Entity\EventResult;
12
13Loc::loadMessages(__FILE__);
14
20class Helper
21{
31
32 public static function getProductById($productId, $quantity, $siteId)
33 {
34 $result = array();
35
36 if(\CModule::IncludeModule('catalog'))
37 {
38 //TODO: change to new provider
39 if ($productProvider = \CSaleBasket::GetProductProvider(array(
40 "MODULE" => "catalog",
41 "PRODUCT_PROVIDER_CLASS" => "CCatalogProductProvider"))
42 )
43 {
44 global $USER;
45 $bTmpUserCreated = false;
46 if (!\CCatalog::IsUserExists())
47 {
48 $bTmpUserCreated = true;
49 if (isset($USER))
50 {
51 $USER_TMP = $USER;
52 unset($USER);
53 }
54
55 $USER = new \CUser();
56 }
57
58 $result = $productProvider::GetProductData(array(
59 "PRODUCT_ID" => $productId,
60 "RENEWAL" => "N",
61 "QUANTITY" => $quantity,
62 "SITE_ID" => $siteId
63 ));
64
65 $result["MODULE"] = "catalog";
66 $result["PRODUCT_PROVIDER_CLASS"] = "CCatalogProductProvider";
67 $dbIblockElement = \CIBlockElement::GetList(array(), array("ID" => $productId), false, false, array('XML_ID', 'IBLOCK_EXTERNAL_ID'));
68
69 if($IblockElement = $dbIblockElement->Fetch())
70 {
71 if($IblockElement["XML_ID"] <> '')
72 $result["PRODUCT_XML_ID"] = $IblockElement["XML_ID"];
73
74 if($IblockElement["IBLOCK_EXTERNAL_ID"] <> '')
75 $result["CATALOG_XML_ID"] = $IblockElement["IBLOCK_EXTERNAL_ID"];
76 }
77
78 if ($bTmpUserCreated)
79 {
80 unset($USER);
81 if (isset($USER_TMP))
82 {
83 $USER = $USER_TMP;
84 unset($USER_TMP);
85 }
86 }
87 }
88 }
89 else
90 {
91 throw new \Bitrix\Main\SystemException("Can't include module \"Catalog\"!");
92 }
93
94 return $result;
95 }
96
101 public static function getPersonTypesList($siteId)
102 {
103 $dbResultList = \CSalePersonType::GetList(
104 "NAME",
105 "ASC",
106 array(
107 "LID" => $siteId,
108 "ACTIVE" => "Y"
109 )
110 );
111
112 $arPersonTypes = array();
113 while ($arPT = $dbResultList->Fetch())
114 $arPersonTypes[$arPT['ID']] = $arPT['NAME'];
115
116 return $arPersonTypes;
117 }
118
123 public static function getDeliveryList($siteId)
124 {
125 $arDeliveryFilter = array(
126 "LID" => $siteId,
127 "ACTIVE" => "Y"
128 );
129
130 $dbDeliveryList = \CSaleDelivery::GetList(
131 array("NAME" => "ASC"),
132 $arDeliveryFilter,
133 false,
134 false,
135 array("ID", "NAME")
136 );
137
138 $arDeliveryList=array();
139 while ($arDelivery = $dbDeliveryList->Fetch())
140 $arDeliveryList[$arDelivery["ID"]] = $arDelivery["NAME"];
141
142 return $arDeliveryList;
143 }
144
149 public static function getOrderPropsList($personTypeId)
150 {
151 if(intval($personTypeId) <= 0)
152 throw new ArgumentNullException('personTypeId');
153
154 $res = OrderPropsTable::getList(array(
155 'filter' => array('=PERSON_TYPE_ID' => $personTypeId),
156 'order' => array('SORT' => 'ASC', 'NAME' => 'ASC'),
157 'select' => array('ID', 'CODE', 'NAME')
158 ));
159
160 return $res->fetchAll();
161 }
162
166 public static function getRequiredOrderProps()
167 {
168 return array(
169 "FIO",
170 "EMAIL",
171 "PHONE",
172 "ZIP",
173 "CITY",
174 "ADDRESS",
175 );
176 }
177
178
185 public static function makeSelectorFromPaySystems($selectName, $psIdValue, $personTypeId)
186 {
187 static $arPaySystems = array();
188
189 if(!isset($arPaySystems[$personTypeId]))
190 {
191 $arPaySystems[$personTypeId] = array();
192 $dbResultList = \CSalePaySystem::GetList(
193 array("NAME" => "ASC"),
194 array(
195 "ACTIVE" => "Y",
196 "PSA_PERSON_TYPE_ID" => $personTypeId,
197 ),
198 false,
199 false,
200 array("ID", "NAME")
201 );
202
203 while($arPS = $dbResultList->Fetch())
204 $arPaySystems[$personTypeId][$arPS['ID']] = $arPS['NAME'];
205 }
206
207 $result = '<select name="'.$selectName.'">'.
208 '<option value="">'.Loc::getMessage("SALE_EBAY_HLP_NOT_USE").'</option>';
209
210 foreach ($arPaySystems[$personTypeId] as $psId => $psName)
211 {
212 $result.= '<option value="'.
213 $psId.'"'.
214 ($psIdValue == $psId ? ' selected ': '').'>'.
215 htmlspecialcharsbx($psName).
216 '</option>';
217 }
218
219 $result .= '</select>';
220
221 return $result;
222 }
223
231 public static function getSelectHtml($name, array $data, $selected = "", $bShowNotUse = true)
232 {
233 if(!is_array($data) || empty($data))
234 return "";
235
236 $result = '<select name="'.htmlspecialcharsbx($name).'">';
237
238 if($bShowNotUse)
239 $result .= '<option value="">'.GetMessage("SALE_YM_NOT_USE").'</option>';
240
241 foreach($data as $value => $title)
242 $result .= '<option value="'.htmlspecialcharsbx($value).'"'.($selected == $value ? " selected" : "").'>'.htmlspecialcharsbx($title).'</option>';
243
244 $result .= '</select>';
245
246 return $result;
247 }
248
253 public static function makeAdditionalOrderProps(array $params)
254 {
255 $psId = intval($params["PAY_SYSTEM"]);
256
257 $result = array();
258
259 $arPropFilter = array(
260 "PERSON_TYPE_ID" => $params["PERSON_TYPE"],
261 "ACTIVE" => "Y"
262 );
263
264 if ($psId != 0)
265 {
266 $arPropFilter["RELATED"]["PAYSYSTEM_ID"] = $psId;
267 $arPropFilter["RELATED"]["TYPE"] = "WITH_NOT_RELATED";
268 }
269
270 if ($params["DELIVERY"] <> '')
271 {
272 $arPropFilter["RELATED"]["DELIVERY_ID"] = $params["DELIVERY"];
273 $arPropFilter["RELATED"]["TYPE"] = "WITH_NOT_RELATED";
274 }
275
276 $dbOrderProps = \CSaleOrderProps::GetList(
277 array(),
278 $arPropFilter,
279 false,
280 false,
281 array("ID", "CODE")
282 );
283
284 while ($arOrderProps = $dbOrderProps->Fetch())
285 {
286 foreach (self::getOrderProps() as $prop)
287 {
288 if ($arOrderProps["CODE"] == $params["ORDER_PROPS_MAP"][$prop] && isset($params[$prop]))
289 $result[$arOrderProps["ID"]] = $params[$prop];
290 }
291 }
292
293 return $result;
294 }
295
299 public static function getBitrixStatuses($siteId)
300 {
301 $result = array(
302 "CANCELED" => Loc::getMessage("SALE_EBAY_HLP_FLAG_CANCELED"),
303 "ALLOW_DELIVERY" => Loc::getMessage("SALE_EBAY_HLP_FLAG_DELIVERY"),
304 "PAYED" => Loc::getMessage("SALE_EBAY_HLP_FLAG_PAYED"),
305 "DEDUCTED" => Loc::getMessage("SALE_EBAY_HLP_FLAG_DEDUCTED"),
306 );
307
308 if ($siteId == '')
309 throw new ArgumentNullException("siteId");
310
311 $dbRes = SiteTable::getList(array(
312 'filter' => array(
313 'LID' => $siteId,
314 ),
315 'select' => array("LANGUAGE_ID"),
316 ));
317
318 if ($site = $dbRes->fetch())
319 $langId = $site["LANGUAGE_ID"];
320 else
321 throw new SystemException("Site with id: \"" . $siteId . "\" not found!");
322
323 $dbRes = StatusLangTable::getList(array(
324 'filter' => array(
325 'LID' => $langId,
326 'STATUS.TYPE' => 'O',
327 ),
328 'order' => array(
329 "STATUS.SORT" => "ASC",
330 "NAME" => "ASC",
331 ),
332 'select' => array(
333 "ID" => "STATUS.ID", "NAME",
334 ),
335 ));
336
337 while ($row = $dbRes->fetch())
338 $result[$row['ID']] = Loc::getMessage("SALE_EBAY_HLP_STATUS") . " " . $row['NAME'] . ' [' . $row['ID'] . ']';
339
340 return $result;
341 }
342
350 public static function getBitrixCategoryPropsHtml($selectName, $iblockId, $bitrixCategoryId = 0, $value = '')
351 {
352 $catProps = self::getPropertiesList($iblockId, $bitrixCategoryId);
353
354 $resultHtml =
355 ' <select name="' . $selectName . '">
356 <option value=""></option>
357 <option value="">------------------</option>
358 <option value="">' . Loc::getMessage("SALE_EBAY_HLP_CATEGORY_PROPS") . '</option>
359 <option value="">------------------</option>';
360
361 foreach ($catProps as $propId => $prop)
362 $resultHtml .= '<option value="' . $propId . '"' . ($value == $propId ? ' selected' : '') . '>' . htmlspecialcharsbx($prop["NAME"]) . '</option>';
363
364 $arOffers = \CCatalogSKU::GetInfoByProductIBlock($iblockId);
365 if ($arOffers)
366 {
367 $catProps2 = self::getPropertiesList($arOffers["IBLOCK_ID"], $bitrixCategoryId);
368
369 $resultHtml .= '
370 <option value="">------------------</option>
371 <option value="">' . Loc::getMessage("SALE_EBAY_HLP_OFFERS_PROPS") . '</option>
372 <option value="">------------------</option>
373 ';
374
375 foreach ($catProps2 as $propId => $prop)
376 $resultHtml .= '<option value="' . $propId . '"' . ($value == $propId ? ' selected' : '') . '>' . htmlspecialcharsbx($prop["NAME"]) . '</option>';
377 }
378
379 $resultHtml .= ' </select>';
380
381 return $resultHtml;
382 }
383
389 public static function getPropertiesList($iblockId, $sectionId = 0)
390 {
391 $result = \CIBlockSectionPropertyLink::GetArray($iblockId, $sectionId);
392
393 $rsProps = \CIBlockProperty::GetList(array(
394 "SORT" => "ASC",
395 'ID' => 'ASC',
396 ), array(
397 "IBLOCK_ID" => $iblockId,
398 "CHECK_PERMISSIONS" => "N",
399 "ACTIVE" => "Y",
400 ));
401
402 while ($arProp = $rsProps->Fetch())
403 if (isset($result[$arProp["ID"]]))
404 $result[$arProp["ID"]]["NAME"] = $arProp["NAME"];
405
406 return $result;
407 }
408
409
410 public function notifyNewOrder($newOrderId, $siteId, $buyerEmail = "", $buyerFio = "")
411 {
412 if ($newOrderId == '')
413 throw new ArgumentNullException("newOrderId");
414
415 if ($siteId == '')
416 throw new ArgumentNullException("siteId");
417
418 global $DB;
419
420 $strOrderList = "";
421 $baseLangCurrency = \CSaleLang::GetLangCurrency($siteId);
422 $orderNew = \CSaleOrder::GetByID($newOrderId);
423 $orderNew["BASKET_ITEMS"] = array();
424
425 $dbBasketTmp = \CSaleBasket::GetList(
426 array("SET_PARENT_ID" => "DESC", "TYPE" => "DESC", "NAME" => "ASC"),
427 array("ORDER_ID" => $newOrderId),
428 false,
429 false,
430 array(
431 "ID", "PRICE", "QUANTITY", "NAME",
432 )
433 );
434
435 while ($arBasketTmp = $dbBasketTmp->GetNext())
436 $orderNew["BASKET_ITEMS"][] = $arBasketTmp;
437
438 $orderNew["BASKET_ITEMS"] = getMeasures($orderNew["BASKET_ITEMS"]);
439
440 foreach ($orderNew["BASKET_ITEMS"] as $val)
441 {
442 if (\CSaleBasketHelper::isSetItem($val))
443 continue;
444
445 $measure = (isset($val["MEASURE_TEXT"])) ? $val["MEASURE_TEXT"] : GetMessage("SALE_YMH_SHT");
446 $strOrderList .= $val["NAME"] . " - " . $val["QUANTITY"] . " " . $measure . " x " . SaleFormatCurrency($val["PRICE"], $baseLangCurrency);
447 $strOrderList .= "</br>";
448 }
449
450 //send mail
451 $arFields = array(
452 "ORDER_ID" => $orderNew["ACCOUNT_NUMBER"],
453 "ORDER_DATE" => Date($DB->DateFormatToPHP(\CLang::GetDateFormat("SHORT", $siteId))),
454 "ORDER_USER" => $buyerFio,
455 "PRICE" => SaleFormatCurrency($orderNew["PRICE"], $baseLangCurrency),
456 "BCC" => \COption::GetOptionString("sale", "order_email", "order@" . $_SERVER['SERVER_NAME']),
457 "EMAIL_TO" => array("PAYER_NAME" => $buyerFio, "USER_EMAIL" => $buyerEmail),
458 "ORDER_LIST" => $strOrderList,
459 "SALE_EMAIL" => \COption::GetOptionString("sale", "order_email", "order@" . $_SERVER['SERVER_NAME']),
460 "DELIVERY_PRICE" => $orderNew["DELIVERY_PRICE"],
461 );
462
463 $eventName = "SALE_NEW_ORDER";
464
465 $bSend = true;
466
467 foreach (GetModuleEvents("sale", "OnOrderNewSendEmail", true) as $arEvent)
468 if (ExecuteModuleEventEx($arEvent, array($newOrderId, &$eventName, &$arFields)) === false)
469 $bSend = false;
470
471 $emailSendRes = false;
472
473 if ($bSend)
474 {
475 $event = new \CEvent;
476 $emailSendRes = $event->Send($eventName, $siteId, $arFields, "N");
477 }
478
479 $pushSendResult = \CSaleMobileOrderPush::send("ORDER_CREATED", array("ORDER" => $orderNew));
480
481 return $emailSendRes && $pushSendResult;
482 }
483
484 public static function onAfterUpdateShipment(\Bitrix\Main\Event $event)
485 {
486 $result = new EventResult();
487 $data = $event->getParameter('fields');
488
489 if (!isset($data["TRACKING_NUMBER"]) && !isset($data["DELIVERY_NAME"]))
490 return $result;
491
492 $primary = $event->getParameter('id');
493
494 $dbRes = OrderTable::getList(array(
495 'select' => array(
496 '*',
497 'SITE_ID' => 'ORDER.LID',
498 'TRADING_PLATFORM_CODE' => 'TRADING_PLATFORM.CODE',
499 'TRADING_PLATFORM_CLASS' => 'TRADING_PLATFORM.CLASS',
500 'DELIVERY_NAME' => 'SHIPMENT.DELIVERY.NAME',
501 'DELIVERY_ID' => 'SHIPMENT.DELIVERY_ID',
502 'TRACKING_NUMBER' => 'SHIPMENT.TRACKING_NUMBER',
503 ),
504 'filter' => array(
505 '=SHIPMENT.ID' => $primary['ID'],
506 ),
507 'runtime' => array(
508 'SHIPMENT' => array(
509 'data_type' => 'Bitrix\Sale\Internals\ShipmentTable',
510 'reference' => array(
511 '=this.ORDER_ID' => 'ref.ORDER_ID',
512 ),
513 ),
514 ),
515 ));
516
517 if ($platformOrder = $dbRes->fetch())
518 {
519 if (class_exists($platformOrder['TRADING_PLATFORM_CLASS']) && is_subclass_of($platformOrder['TRADING_PLATFORM_CLASS'], '\Bitrix\Sale\TradingPlatform\Platform'))
520 {
521 if ($platform = call_user_func($platformOrder['TRADING_PLATFORM_CLASS'] . '::getInstance'))
522 {
523 $result = $platform->onAfterUpdateShipment(
524 $event,
525 array_merge(
526 $platformOrder,
527 array(
528 'TRACKING_NUMBER' => isset($data["TRACKING_NUMBER"]) ? $data["TRACKING_NUMBER"] : $platformOrder["TRACKING_NUMBER"],
529 'DELIVERY_NAME' => isset($data["DELIVERY_NAME"]) ? $data["DELIVERY_NAME"] : $platformOrder["DELIVERY_NAME"],
530 'DELIVERY_ID' => isset($data["DELIVERY_ID"]) ? $data["DELIVERY_ID"] : $platformOrder["DELIVERY_ID"],
531 )
532 )
533 );
534 }
535 }
536 }
537
538 return $result;
539 }
540
541 public static function getIblocksIds($withSku = false)
542 {
543 if (!\Bitrix\Main\Loader::includeModule('catalog'))
544 throw new SystemException('Module catalog is not installed');
545
546// save result in STATIC - for multiple used
547 if (!isset(self::$arIBlockIDs))
548 {
549 self::$arIBlockIDs = array();
550
551// get CATALOG IBLOCKS from SKU iblocks
552 $rsCatalogs = \CCatalog::GetList(
553 array(),
554 array('!PRODUCT_IBLOCK_ID' => 0),
555 false,
556 false,
557 array('IBLOCK_ID', 'PRODUCT_IBLOCK_ID', 'NAME')
558 );
559 while ($arCatalog = $rsCatalogs->Fetch())
560 {
561 $arCatalog['PRODUCT_IBLOCK_ID'] = intval($arCatalog['PRODUCT_IBLOCK_ID']);
562 if (0 < $arCatalog['PRODUCT_IBLOCK_ID'])
563 self::$arIBlockIDs[$arCatalog['PRODUCT_IBLOCK_ID']] = array(
564 "IBLOCK_ID" => $arCatalog['PRODUCT_IBLOCK_ID'],
565 "NAME" => '',
566 );
567
568// get SKU IBLOCKS if needed
569 if ($withSku)
570 {
571 $arCatalog['IBLOCK_ID'] = intval($arCatalog['IBLOCK_ID']);
572 if (0 < $arCatalog['IBLOCK_ID'])
573 self::$arIBlockIDs[$arCatalog['IBLOCK_ID']] = array(
574 "IBLOCK_ID" => $arCatalog['IBLOCK_ID'],
575 "NAME" => $arCatalog['NAME'],
576 );
577 }
578 }
579
580// get CATALOG IBLOCKS
581 $rsCatalogs = \CCatalog::GetList(
582 array(),
583 array('PRODUCT_IBLOCK_ID' => 0),
584 false,
585 false,
586 array('IBLOCK_ID', 'NAME')
587 );
588 while ($arCatalog = $rsCatalogs->Fetch())
589 {
590 $arCatalog['IBLOCK_ID'] = intval($arCatalog['IBLOCK_ID']);
591 if (0 < $arCatalog['IBLOCK_ID'])
592 self::$arIBlockIDs[$arCatalog['IBLOCK_ID']] = array(
593 "IBLOCK_ID" => $arCatalog['IBLOCK_ID'],
594 "NAME" => $arCatalog['NAME'],
595 );
596 }
597
598 if (empty(self::$arIBlockIDs))
599 self::$arIBlockIDs[-1] = true;
600 }
601
602 return self::$arIBlockIDs;
603 }
604
605 public static function getDefaultFeedIntervals()
606 {
607
608 return array(
609 "PRODUCT" => 30, //in MINUTES
610 "INVENTORY" => 30, //in MINUTES
611 "IMAGE" => 30, //in MINUTES
612 "ORDER" => 30, //in MINUTES
613 "ORDER_ACK" => 15, //in MINUTES
614 "SHIPMENT" => 30, //in MINUTES
615 "PRODUCTS" => 24, //in HOURS
616 "ALBUMS" => 24, //in HOURS
617 "ALL" => 24 //in HOURS
618 );
619 }
620}
static includeModule($moduleName)
Definition loader.php:69
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getList(array $parameters=array())
notifyNewOrder($newOrderId, $siteId, $buyerEmail="", $buyerFio="")
Definition helper.php:410
static getIblocksIds($withSku=false)
Definition helper.php:541
static onAfterUpdateShipment(\Bitrix\Main\Event $event)
Definition helper.php:484
static getBitrixCategoryPropsHtml($selectName, $iblockId, $bitrixCategoryId=0, $value='')
Definition helper.php:350
static getBitrixStatuses($siteId)
Definition helper.php:299
static getProductById($productId, $quantity, $siteId)
Definition helper.php:32
static getSelectHtml($name, array $data, $selected="", $bShowNotUse=true)
Definition helper.php:231
static getOrderPropsList($personTypeId)
Definition helper.php:149
static makeSelectorFromPaySystems($selectName, $psIdValue, $personTypeId)
Definition helper.php:185
static makeAdditionalOrderProps(array $params)
Definition helper.php:253
static getPropertiesList($iblockId, $sectionId=0)
Definition helper.php:389
static getPersonTypesList($siteId)
Definition helper.php:101