Bitrix-D7  20.5.0
advsession.php
См. документацию.
1 <?php
2 namespace Bitrix\Seo;
3 
5 use Bitrix\Main;
13 
14 Loc::loadMessages(__FILE__);
15 
16 /**
17  * Class AdvSession
18  *
19  * Events handler for managing users came from yandex.direct ad
20  *
21  * @package Bitrix\Seo
22  **/
24 {
25  const URL_PARAM_CAMPAIGN = 'bxydcampaign';
26  const URL_PARAM_CAMPAIGN_VALUE = '{campaign_id}';
27  const URL_PARAM_BANNER = 'bxydbanner';
28  const URL_PARAM_BANNER_VALUE = '{banner_id}';
29 
30  protected static $orderHandlerCalled = array();
31 
32  public static function checkSession()
33  {
34  $request = Main\Context::getCurrent()->getRequest();
35  if(
36  isset($request[static::URL_PARAM_CAMPAIGN])
37  && isset($request[static::URL_PARAM_BANNER])
38  )
39  {
40  $_SESSION['SEO_ADV'] = array(
41  "ENGINE" => YandexDirect::ENGINE_ID,
42  "CAMPAIGN_ID" => $request[static::URL_PARAM_CAMPAIGN],
43  "BANNER_ID" => $request[static::URL_PARAM_BANNER],
44  );
45  }
46  }
47 
48  public static function isSession()
49  {
50  return isset($_SESSION['SEO_ADV']);
51  }
52 
53  public static function getSession()
54  {
55  return $_SESSION['SEO_ADV'];
56  }
57 
58  public static function onOrderSave($orderId, $orderFields, $orderData, $isNew)
59  {
60  if($isNew)
61  {
62  static::checkSessionOrder($orderId);
63  }
64  }
65 
66  public static function onBasketOrder($orderId)
67  {
68  static::checkSessionOrder($orderId);
69  }
70 
71  public static function onSalePayOrder($orderId, $val)
72  {
73  if($val == 'Y')
74  {
75  static::countSessionOrder($orderId);
76  }
77  }
78 
79  public static function onSaleDeductOrder($orderId, $val)
80  {
81  if($val == 'Y')
82  {
83  static::countSessionOrder($orderId);
84  }
85  }
86  public static function onSaleDeliveryOrder($orderId, $val)
87  {
88  if($val == 'Y')
89  {
90  static::countSessionOrder($orderId);
91  }
92  }
93 
94  public static function onSaleStatusOrder($orderId, $status)
95  {
96  if($status == 'F')
97  {
98  static::countSessionOrder($orderId);
99  }
100  }
101 
102  protected static function checkSessionOrder($orderId)
103  {
104  if(
105  isset($_SESSION['SEO_ADV'])
106  && is_array($_SESSION['SEO_ADV'])
107  && $_SESSION['SEO_ADV']['BANNER_ID'] > 0
108  && !in_array($orderId, static::$orderHandlerCalled)
109  && Main\Loader::includeModule('sale')
110  && Main\Loader::includeModule('catalog')
111  )
112  {
113  static::$orderHandlerCalled[] = $orderId;
114 
115  $banner = null;
116  switch($_SESSION['SEO_ADV']['ENGINE'])
117  {
119  $dbRes = YandexBannerTable::getList(array(
120  'filter' => array(
121  '=XML_ID' => $_SESSION['SEO_ADV']['BANNER_ID'],
122  '=ENGINE.CODE' => YandexDirect::ENGINE_ID,
123  ),
124  'select' => array(
125  'ID', 'CAMPAIGN_ID', 'ENGINE_ID',
126  )
127  ));
128  $banner = $dbRes->fetch();
129  break;
130  }
131 
132  if($banner)
133  {
134  $linkedProductsList = static::getBannerLinkedProducts($banner['ID']);
135 
136  if(count($linkedProductsList) > 0)
137  {
138  $basket = BasketTable::getList(array(
139  'filter' => array(
140  '=ORDER_ID' => $orderId,
141  ),
142  'select' => array('PRODUCT_ID'),
143  ));
144 
145  $addEntry = false;
146  while($item = $basket->fetch())
147  {
148  if(in_array($item['PRODUCT_ID'], $linkedProductsList))
149  {
150  $addEntry = true;
151  break;
152  }
153  else
154  {
155  $productInfo = \CCatalogSKU::GetProductInfo($item['PRODUCT_ID']);
156 
157  if(is_array($productInfo) && in_array($productInfo['ID'], $linkedProductsList))
158  {
159  $addEntry = true;
160  break;
161  }
162  }
163  }
164 
165  if($addEntry)
166  {
167  $entryData = array(
168  'ENGINE_ID' => $banner['ENGINE_ID'],
169  'CAMPAIGN_ID' => $banner['CAMPAIGN_ID'],
170  'BANNER_ID' => $banner['ID'],
171  'ORDER_ID' => $orderId,
172  'SUM' => 0,
173  'PROCESSED' => OrderTable::NOT_PROCESSED,
174  );
175 
176  OrderTable::add($entryData);
177  }
178  }
179  }
180  }
181  }
182 
183  protected static function countSessionOrder($orderId)
184  {
185  if(
186  Main\Loader::includeModule('sale')
187  && Main\Loader::includeModule('catalog')
188  && Main\Loader::includeModule('currency')
189  )
190  {
191  $orderLinks = OrderTable::getList(array(
192  'filter' => array(
193  '=ORDER_ID' => $orderId,
194  '=PROCESSED' => OrderTable::NOT_PROCESSED,
195  ),
196  'select' => array('ID', 'BANNER_ID')
197  ));
198  $orderLink = $orderLinks->fetch();
199  if($orderLink)
200  {
201  $linkedProductsList = static::getBannerLinkedProducts($orderLink['BANNER_ID']);
202  if(count($linkedProductsList) > 0)
203  {
204  $basket = BasketTable::getList(array(
205  'filter' => array(
206  '=ORDER_ID' => $orderId,
207  ),
208  'select' => array('PRODUCT_ID', 'GROSS_PROFIT', 'SUMMARY_PRICE', 'SUMMARY_PURCHASING_PRICE', 'QUANTITY'),
209  ));
210 
211  $sum = 0;
212  while($item = $basket->fetch())
213  {
214  if(in_array($item['PRODUCT_ID'], $linkedProductsList))
215  {
216  $sum += static::getProductProfit($item);
217  }
218  else
219  {
220  $productInfo = \CCatalogSKU::GetProductInfo($item['PRODUCT_ID']);
221 
222  if(is_array($productInfo) && in_array($productInfo['ID'], $linkedProductsList))
223  {
224  $sum += static::getProductProfit($item);
225  }
226  }
227  }
228 
229  OrderTable::update($orderLink['ID'], array(
230  'SUM' => $sum,
231  'PROCESSED' => OrderTable::PROCESSED,
232  ));
233  }
234  }
235  }
236  }
237 
238  protected static function getBannerLinkedProducts($bannerId)
239  {
240  $linkedProductsList = array();
241  $linkedProducts = LinkTable::getList(
242  array(
243  'filter' => array(
244  '=LINK_TYPE' => LinkTable::TYPE_IBLOCK_ELEMENT,
245  '=BANNER_ID' => $bannerId,
246  ),
247  'select' => array(
248  'LINK_ID'
249  )
250  )
251  );
252 
253  while($link = $linkedProducts->fetch())
254  {
255  $linkedProductsList[] = $link['LINK_ID'];
256  }
257 
258  return $linkedProductsList;
259  }
260 
261  // TODO: remove all this math when /sale_refactoring_mince releases
262  protected static function getProductProfit($productInfo)
263  {
264  if($productInfo['GROSS_PROFIT'] <> '')
265  {
266  $profit = doubleval($productInfo['GROSS_PROFIT']);
267  }
268  else
269  {
270  $purchasingCost = 0;
271  if($productInfo['SUMMARY_PURCHASING_PRICE'] <> '')
272  {
273  $purchasingCost = doubleval($productInfo['SUMMARY_PURCHASING_PRICE']);
274  }
275  else
276  {
277  $dbRes = ProductTable::getList(array(
278  'filter' => array(
279  '=ID' => $productInfo['PRODUCT_ID'],
280  ),
281  'select' => array(
282  'ID', 'PURCHASING_PRICE', 'PURCHASING_CURRENCY'
283  )
284  ));
285  $productInfoBase = $dbRes->fetch();
286  if($productInfoBase)
287  {
288  $purchasingCost = $productInfoBase['PURCHASING_PRICE'] * $productInfo['QUANTITY'];
289 
290  $baseCurrency = CurrencyManager::getBaseCurrency();
291  if($baseCurrency != $productInfoBase['PURCHASING_CURRENCY'])
292  {
293  $purchasingCost = \CCurrencyRates::convertCurrency(
294  $purchasingCost,
295  $productInfoBase['PURCHASING_CURRENCY'],
296  $baseCurrency
297  );
298  }
299  }
300  }
301 
302  $profit = doubleval($productInfo['SUMMARY_PRICE'])-$purchasingCost;
303  }
304  return $profit;
305  }
306 }
static getBaseCurrency()
Return base currency.
static getCurrent()
Static method returns current instance of context.
static includeModule($moduleName)
Includes a module by its name.
static loadMessages($file)
Loads language messages for specified file in a lazy way.
Definition: loc.php:67
static getBannerLinkedProducts($bannerId)
Definition: advsession.php:238
static onSaleStatusOrder($orderId, $status)
Definition: advsession.php:94
static onBasketOrder($orderId)
Definition: advsession.php:66
static countSessionOrder($orderId)
Definition: advsession.php:183
static onOrderSave($orderId, $orderFields, $orderData, $isNew)
Definition: advsession.php:58
static checkSessionOrder($orderId)
Definition: advsession.php:102
const URL_PARAM_CAMPAIGN_VALUE
Definition: advsession.php:26
static onSaleDeductOrder($orderId, $val)
Definition: advsession.php:79
static onSalePayOrder($orderId, $val)
Definition: advsession.php:71
static onSaleDeliveryOrder($orderId, $val)
Definition: advsession.php:86
static getProductProfit($productInfo)
Definition: advsession.php:262