Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
advsession.php
1<?php
2namespace Bitrix\Seo;
3
15
16Loc::loadMessages(__FILE__);
17
26{
27 const URL_PARAM_CAMPAIGN = 'bxydcampaign';
28 const URL_PARAM_CAMPAIGN_VALUE = '{campaign_id}';
29 const URL_PARAM_BANNER = 'bxydbanner';
30 const URL_PARAM_BANNER_VALUE = '{banner_id}';
31
32 private const DATA_INDEX = 'SEO_ADV';
33
34 protected static $orderHandlerCalled = array();
35
36 public static function checkSession()
37 {
38 $request = Main\Context::getCurrent()->getRequest();
39 if(
40 isset($request[static::URL_PARAM_CAMPAIGN])
41 && isset($request[static::URL_PARAM_BANNER])
42 )
43 {
44 $session = self::getSessionStorage();
45 $session?->set(
46 self::DATA_INDEX,
47 [
48 'ENGINE' => YandexDirect::ENGINE_ID,
49 'CAMPAIGN_ID' => $request[static::URL_PARAM_CAMPAIGN],
50 'BANNER_ID' => $request[static::URL_PARAM_BANNER],
51 ]
52 );
53 }
54 }
55
56 public static function isSession()
57 {
58 $session = self::getSessionStorage();
59 if ($session === null)
60 {
61 return false;
62 }
63 $data = $session->get(self::DATA_INDEX);
64 unset($session);
65
66 return !empty($data) && is_array($data);
67 }
68
69 public static function getSession()
70 {
71 $session = self::getSessionStorage();
72 if ($session === null)
73 {
74 return null;
75 }
76 $data = $session->get(self::DATA_INDEX);
77 unset($session);
78
79 if (!empty($data) && is_array($data))
80 {
81 return $data;
82 }
83
84 return null;
85 }
86
87 public static function onOrderSave($orderId, $orderFields, $orderData, $isNew)
88 {
89 if($isNew)
90 {
91 static::checkSessionOrder($orderId);
92 }
93 }
94
95 public static function onBasketOrder($orderId)
96 {
97 static::checkSessionOrder($orderId);
98 }
99
100 public static function onSalePayOrder($orderId, $val)
101 {
102 if($val == 'Y')
103 {
104 static::countSessionOrder($orderId);
105 }
106 }
107
108 public static function onSaleDeductOrder($orderId, $val)
109 {
110 if($val == 'Y')
111 {
112 static::countSessionOrder($orderId);
113 }
114 }
115 public static function onSaleDeliveryOrder($orderId, $val)
116 {
117 if($val == 'Y')
118 {
119 static::countSessionOrder($orderId);
120 }
121 }
122
123 public static function onSaleStatusOrder($orderId, $status)
124 {
125 if($status == 'F')
126 {
127 static::countSessionOrder($orderId);
128 }
129 }
130
131 protected static function checkSessionOrder($orderId)
132 {
133 if (in_array($orderId, static::$orderHandlerCalled))
134 {
135 return;
136 }
137 if (!(
138 Main\Loader::includeModule('sale')
139 && Main\Loader::includeModule('catalog')
140 ))
141 {
142 return;
143 }
144
145 $data = self::getSession();
146 if ($data === null)
147 {
148 return;
149 }
150
151 $bannerId = (string)($data['BANNER_ID'] ?? '');
152 if ($bannerId !== '')
153 {
154 static::$orderHandlerCalled[] = $orderId;
155 $banner = null;
156 $engine = (string)($data['ENGINE'] ?? '');
157
158 switch ($engine)
159 {
160 case YandexDirect::ENGINE_ID:
161 $dbRes = YandexBannerTable::getList(array(
162 'filter' => array(
163 '=XML_ID' => $bannerId,
164 '=ENGINE.CODE' => YandexDirect::ENGINE_ID,
165 ),
166 'select' => array(
167 'ID', 'CAMPAIGN_ID', 'ENGINE_ID',
168 )
169 ));
170 $banner = $dbRes->fetch();
171 break;
172 }
173
174 if ($banner)
175 {
176 $linkedProductsList = static::getBannerLinkedProducts($banner['ID']);
177
178 if (count($linkedProductsList) > 0)
179 {
180 $basket = BasketTable::getList(array(
181 'filter' => array(
182 '=ORDER_ID' => $orderId,
183 ),
184 'select' => array('PRODUCT_ID'),
185 ));
186
187 $addEntry = false;
188 while ($item = $basket->fetch())
189 {
190 if (in_array($item['PRODUCT_ID'], $linkedProductsList))
191 {
192 $addEntry = true;
193 break;
194 }
195 else
196 {
197 $productInfo = \CCatalogSKU::GetProductInfo($item['PRODUCT_ID']);
198
199 if (is_array($productInfo) && in_array($productInfo['ID'], $linkedProductsList))
200 {
201 $addEntry = true;
202 break;
203 }
204 }
205 }
206
207 if ($addEntry)
208 {
209 $entryData = array(
210 'ENGINE_ID' => $banner['ENGINE_ID'],
211 'CAMPAIGN_ID' => $banner['CAMPAIGN_ID'],
212 'BANNER_ID' => $banner['ID'],
213 'ORDER_ID' => $orderId,
214 'SUM' => 0,
215 'PROCESSED' => OrderTable::NOT_PROCESSED,
216 );
217
218 OrderTable::add($entryData);
219 }
220 }
221 }
222 }
223 }
224
225 protected static function countSessionOrder($orderId)
226 {
227 if(
228 Main\Loader::includeModule('sale')
229 && Main\Loader::includeModule('catalog')
230 && Main\Loader::includeModule('currency')
231 )
232 {
233 $orderLinks = OrderTable::getList(array(
234 'filter' => array(
235 '=ORDER_ID' => $orderId,
236 '=PROCESSED' => OrderTable::NOT_PROCESSED,
237 ),
238 'select' => array('ID', 'BANNER_ID')
239 ));
240 $orderLink = $orderLinks->fetch();
241 if($orderLink)
242 {
243 $linkedProductsList = static::getBannerLinkedProducts($orderLink['BANNER_ID']);
244 if(count($linkedProductsList) > 0)
245 {
246 $basket = BasketTable::getList(array(
247 'filter' => array(
248 '=ORDER_ID' => $orderId,
249 ),
250 'select' => array('PRODUCT_ID', 'GROSS_PROFIT', 'SUMMARY_PRICE', 'SUMMARY_PURCHASING_PRICE', 'QUANTITY'),
251 ));
252
253 $sum = 0;
254 while($item = $basket->fetch())
255 {
256 if(in_array($item['PRODUCT_ID'], $linkedProductsList))
257 {
258 $sum += static::getProductProfit($item);
259 }
260 else
261 {
262 $productInfo = \CCatalogSKU::GetProductInfo($item['PRODUCT_ID']);
263
264 if(is_array($productInfo) && in_array($productInfo['ID'], $linkedProductsList))
265 {
266 $sum += static::getProductProfit($item);
267 }
268 }
269 }
270
271 OrderTable::update($orderLink['ID'], array(
272 'SUM' => $sum,
273 'PROCESSED' => OrderTable::PROCESSED,
274 ));
275 }
276 }
277 }
278 }
279
280 protected static function getBannerLinkedProducts($bannerId)
281 {
282 $linkedProductsList = array();
283 $linkedProducts = LinkTable::getList(
284 array(
285 'filter' => array(
286 '=LINK_TYPE' => LinkTable::TYPE_IBLOCK_ELEMENT,
287 '=BANNER_ID' => $bannerId,
288 ),
289 'select' => array(
290 'LINK_ID'
291 )
292 )
293 );
294
295 while($link = $linkedProducts->fetch())
296 {
297 $linkedProductsList[] = $link['LINK_ID'];
298 }
299
300 return $linkedProductsList;
301 }
302
303 // TODO: remove all this math when /sale_refactoring_mince releases
304 protected static function getProductProfit($productInfo)
305 {
306 if($productInfo['GROSS_PROFIT'] <> '')
307 {
308 $profit = doubleval($productInfo['GROSS_PROFIT']);
309 }
310 else
311 {
312 $purchasingCost = 0;
313 if($productInfo['SUMMARY_PURCHASING_PRICE'] <> '')
314 {
315 $purchasingCost = doubleval($productInfo['SUMMARY_PURCHASING_PRICE']);
316 }
317 else
318 {
319 $dbRes = ProductTable::getList(array(
320 'filter' => array(
321 '=ID' => $productInfo['PRODUCT_ID'],
322 ),
323 'select' => array(
324 'ID', 'PURCHASING_PRICE', 'PURCHASING_CURRENCY'
325 )
326 ));
327 $productInfoBase = $dbRes->fetch();
328 if($productInfoBase)
329 {
330 $purchasingCost = $productInfoBase['PURCHASING_PRICE'] * $productInfo['QUANTITY'];
331
332 $baseCurrency = CurrencyManager::getBaseCurrency();
333 if($baseCurrency != $productInfoBase['PURCHASING_CURRENCY'])
334 {
335 $purchasingCost = \CCurrencyRates::convertCurrency(
336 $purchasingCost,
337 $productInfoBase['PURCHASING_CURRENCY'],
338 $baseCurrency
339 );
340 }
341 }
342 }
343
344 $profit = doubleval($productInfo['SUMMARY_PRICE'])-$purchasingCost;
345 }
346 return $profit;
347 }
348
356 private static function getSessionStorage(): ?Session
357 {
359 $session = Application::getInstance()->getSession();
360 if (!$session->isAccessible())
361 {
362 return null;
363 }
364
365 return $session;
366 }
367}
static loadMessages($file)
Definition loc.php:64
static getBannerLinkedProducts($bannerId)
static onSaleStatusOrder($orderId, $status)
static onBasketOrder($orderId)
static countSessionOrder($orderId)
static onOrderSave($orderId, $orderFields, $orderData, $isNew)
static checkSessionOrder($orderId)
static onSaleDeductOrder($orderId, $val)
static onSalePayOrder($orderId, $val)
static onSaleDeliveryOrder($orderId, $val)
static getProductProfit($productInfo)