1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
conversionhandlers.php
См. документацию.
1<?php
2
3namespace Bitrix\Sale\Internals;
4
5use Bitrix\Conversion\Utils;
6use Bitrix\Conversion\DayContext;
7use Bitrix\Main;
8use Bitrix\Main\Loader;
9use Bitrix\Main\Type\Date;
10use Bitrix\Main\Type\DateTime;
11use Bitrix\Main\Localization\Loc;
12use Bitrix\Sale;
13
16{
17 private static float $onBeforeBasketAddQuantity = 0;
18
19 public static function onGetCounterTypes(): array
20 {
21 return [
22 'sale_cart_add_day' => [
23 'MODULE' => 'sale',
24 'NAME' => 'Added to cart goals',
25 'GROUP' => 'day',
26 ],
27 'sale_cart_add' => [
28 'MODULE' => 'sale',
29 'NAME' => 'Added to cart total',
30 ],
31 'sale_cart_sum_add' => [
32 'MODULE' => 'sale',
33 'NAME' => 'Sum added to cart',
34 ],
35
36 'sale_order_add_day' => [
37 'MODULE' => 'sale',
38 'NAME' => 'Placed orders goals',
39 'GROUP' => 'day',
40 ],
41 'sale_order_add' => [
42 'MODULE' => 'sale',
43 'NAME' => 'Placed orders total',
44 ],
45 'sale_order_sum_add' => [
46 'MODULE' => 'sale',
47 'NAME' => 'Sum placed orders',
48 ],
49
50 'sale_payment_add_day' => [
51 'MODULE' => 'sale',
52 'NAME' => 'Payments a day goals',
53 'GROUP' => 'day',
54 ],
55 'sale_payment_add' => [
56 'MODULE' => 'sale',
57 'NAME' => 'Payments a day total',
58 ],
59 'sale_payment_sum_add' => [
60 'MODULE' => 'sale',
61 'NAME' => 'Added payment sum',
62 ],
63 ];
64 }
65
66 public static function onGetRateTypes(): array
67 {
68 $scale = [0.5, 1, 1.5, 2, 5];
69
70 $format = [
71 'SUM' => function ($value, $format = null)
72 {
73 return Utils::formatToBaseCurrency($value, $format);
74 },
75 ];
76
77 $units = [
78 'SUM' => Utils::getBaseCurrencyUnit(), // TODO deprecated
79 ];
80
81 return [
82 'sale_payment' => [
83 'NAME' => Loc::getMessage('SALE_CONVERSION_RATE_PAYMENT_NAME'),
84 'SCALE' => $scale,
85 'FORMAT' => $format,
86 'UNITS' => $units,
87 'MODULE' => 'sale',
88 'SORT' => 1100,
89 'COUNTERS' => [
90 'conversion_visit_day',
91 'sale_payment_add_day',
92 'sale_payment_add',
93 'sale_payment_add_cmpfb',
94 'sale_payment_sum_add',
95 ],
96 'CALCULATE' => function (array $counters)
97 {
98 $denominator = (int)($counters['conversion_visit_day'] ?? 0);
99 $numerator = (int)($counters['sale_payment_add_day'] ?? 0);
100 $quantity = (float)($counters['sale_payment_add'] ?? 0) + (float)($counters['sale_payment_add_cmpfb'] ?? 0);
101 $sum = (float)($counters['sale_payment_sum_add'] ?? 0);
102
103 return [
104 'DENOMINATOR' => $denominator,
105 'NUMERATOR' => $numerator,
106 'QUANTITY' => $quantity,
107 'RATE' => $denominator ? $numerator / $denominator : 0,
108 'SUM' => $sum,
109 ];
110 },
111 ],
112
113 'sale_order' => [
114 'NAME' => Loc::getMessage('SALE_CONVERSION_RATE_ORDER_NAME'),
115 'SCALE' => $scale,
116 'FORMAT' => $format,
117 'UNITS' => $units,
118 'MODULE' => 'sale',
119 'SORT' => 1200,
120 'COUNTERS' => [
121 'conversion_visit_day',
122 'sale_order_add_day',
123 'sale_order_add',
124 'sale_order_add_cmpfb',
125 'sale_order_sum_add',
126 ],
127 'CALCULATE' => function (array $counters)
128 {
129 $denominator = (int)($counters['conversion_visit_day'] ?? 0);
130 $numerator = (int)($counters['sale_order_add_day'] ?? 0);
131 $quantity = (float)($counters['sale_order_add'] ?? 0) + (float)($counters['sale_order_add_cmpfb'] ?? 0);
132 $sum = (float)($counters['sale_order_sum_add'] ?? 0);
133
134 return [
135 'DENOMINATOR' => $denominator,
136 'NUMERATOR' => $numerator,
137 'QUANTITY' => $quantity,
138 'RATE' => $denominator ? $numerator / $denominator : 0,
139 'SUM' => $sum,
140 ];
141 },
142 ],
143
144 'sale_cart' => [
145 'NAME' => Loc::getMessage('SALE_CONVERSION_RATE_CART_NAME'),
146 'SCALE' => $scale,
147 'FORMAT' => $format,
148 'UNITS' => $units,
149 'MODULE' => 'sale',
150 'SORT' => 1300,
151 'COUNTERS' => [
152 'conversion_visit_day',
153 'sale_cart_add_day',
154 'sale_cart_add',
155 'sale_cart_add_cmpfb',
156 'sale_cart_sum_add',
157 ],
158 'CALCULATE' => function (array $counters)
159 {
160 $denominator = (int)($counters['conversion_visit_day'] ?? 0);
161 $numerator = (int)($counters['sale_cart_add_day'] ?? 0);
162 $quantity = (float)($counters['sale_cart_add'] ?? 0) + (float)($counters['sale_cart_add_cmpfb'] ?? 0);
163 $sum = (float)($counters['sale_cart_sum_add'] ?? 0);
164
165 return [
166 'DENOMINATOR' => $denominator,
167 'NUMERATOR' => $numerator,
168 'QUANTITY' => $quantity,
169 'RATE' => $denominator ? $numerator / $denominator : 0,
170 'SUM' => $sum,
171 ];
172 },
173 ],
174 ];
175 }
176
177 public static function onGenerateInitialData(Date $from, Date $to): array
178 {
179 $data = [];
180
181 // 1. Payments
182
184 [],
185 [
186 'PAYED' => 'Y',
187 'CANCELED' => 'N',
188 '>=DATE_PAYED' => $from,
189 '<=DATE_PAYED' => $to,
190 ],
191 false,
192 false,
193 [
194 'LID',
195 'DATE_PAYED',
196 'PRICE',
197 'CURRENCY',
198 ]
199 );
200
201 while ($row = $result->Fetch())
202 {
203 $day = new DateTime($row['DATE_PAYED']);
204 $sum = Utils::convertToBaseCurrency($row['PRICE'], $row['CURRENCY']);
205
206 if ($counters =& $data[$row['LID']][$day->format('Y-m-d')])
207 {
208 $counters['sale_payment_add_day'] += 1;
209 $counters['sale_payment_sum_add'] += $sum;
210 }
211 else
212 {
213 $counters = [
214 'sale_payment_add_day' => 1,
215 'sale_payment_sum_add' => $sum,
216 ];
217 }
218 }
219
220 // 2. Orders
221
223 [],
224 [
225 'CANCELED' => 'N',
226 '>=DATE_INSERT' => $from,
227 '<=DATE_INSERT' => $to,
228 ],
229 false,
230 false,
231 [
232 'LID',
233 'DATE_INSERT',
234 'PRICE',
235 'CURRENCY',
236 ]
237 );
238
239 while ($row = $result->Fetch())
240 {
241 $day = new DateTime($row['DATE_INSERT']);
242 $sum = Utils::convertToBaseCurrency($row['PRICE'], $row['CURRENCY']);
243
244 if ($counters =& $data[$row['LID']][$day->format('Y-m-d')])
245 {
246 $counters['sale_order_add_day'] += 1;
247 $counters['sale_order_sum_add'] += $sum;
248 }
249 else
250 {
251 $counters = [
252 'sale_order_add_day' => 1,
253 'sale_order_sum_add' => $sum,
254 ];
255 }
256 }
257
258 // 3. Cart
259
261 [],
262 [
263 '>=DATE_INSERT' => $from,
264 '<=DATE_INSERT' => $to,
265 ],
266 false,
267 false,
268 [
269 'LID',
270 'DATE_INSERT',
271 'PRICE',
272 'CURRENCY',
273 'QUANTITY',
274 ]
275 );
276
277 while ($row = $result->Fetch())
278 {
279 $day = new DateTime($row['DATE_INSERT']);
280 $sum = Utils::convertToBaseCurrency($row['PRICE'] * $row['QUANTITY'], $row['CURRENCY']);
281
282 if ($counters =& $data[$row['LID']][$day->format('Y-m-d')])
283 {
284 $counters['sale_cart_add_day'] += 1;
285 $counters['sale_cart_sum_add'] += $sum;
286 }
287 else
288 {
289 $counters = [
290 'sale_cart_add_day' => 1,
291 'sale_cart_sum_add' => $sum,
292 ];
293 }
294 }
295
296 // Result
297
298 unset($counters);
299
300 $result = [];
301
302 foreach ($data as $siteId => $dayCounters)
303 {
304 $result[] = [
305 'ATTRIBUTES' => ['conversion_site' => $siteId],
306 'DAY_COUNTERS' => $dayCounters,
307 ];
308 }
309
310 return $result;
311 }
312
313 // Cart Counters
314
315 // Events can be stacked!!!
316 // 1) OnBeforeBasketAdd -> OnBasketAdd
317 // 2) OnBeforeBasketAdd -> OnBeforeBasketUpdate -> OnBasketUpdate -> OnBasketAdd
318 // 3) and other variations with mixed arguments as well, sick!!!
319
320 public static function onSaleBasketItemSaved(Main\Event $event): void
321 {
322 if (!$event->getParameter('IS_NEW'))
323 {
324 return;
325 }
326
327 $basketItem = $event->getParameter('ENTITY');
328
329 if ($basketItem instanceof Sale\BasketItem)
330 {
331 $price = $basketItem->getPrice();
332 $quantity = $basketItem->getQuantity();
333 $currency = $basketItem->getCurrency();
334
335 if ($quantity && Loader::includeModule('conversion'))
336 {
337 $context = DayContext::getSiteInstance($basketItem->getField('LID'));
338
339 $context->addDayCounter('sale_cart_add_day', 1);
340 $context->addCounter(new Date(), 'sale_cart_add', 1);
341
342 if ($price * $quantity && $currency)
343 {
344 $context->addCurrencyCounter('sale_cart_sum_add', $price * $quantity, $currency);
345 }
346 }
347 }
348 }
349
350 public static function onBeforeBasketAdd(/*array*/ $fields): void
351 {
352 self::$onBeforeBasketAddQuantity = (float)($fields['QUANTITY'] ?? 0);
353 }
354
355 public static function onBasketAdd($id, /*array*/ $fields): void
356 {
357 if (is_array($fields)
358 && isset($fields['PRICE'], $fields['QUANTITY'], $fields['CURRENCY'])
359 && self::$onBeforeBasketAddQuantity
360 && Loader::includeModule('conversion'))
361 {
362 $context = DayContext::getSiteInstance($fields['LID']);
363 $context->addDayCounter('sale_cart_add_day', 1);
364 $context->addCounter(new Date(), 'sale_cart_add' , 1);
365 $context->addCurrencyCounter(
366 'sale_cart_sum_add',
367 $fields['PRICE'] * self::$onBeforeBasketAddQuantity,
368 $fields['CURRENCY']
369 );
370 }
371
372 self::$onBeforeBasketAddQuantity = 0;
373 }
374
375 //static private $onBeforeBasketUpdate = 0;
376
377 public static function onBeforeBasketUpdate($id, /*array*/ $fields = null) // null hack/fix 4 sale 15
378 {
379 /*self::$onBeforeBasketUpdate =
380
381 Loader::includeModule('conversion')
382 && ($intId = (int) $id) > 0
383 && $intId == $id
384 && ($row = \CSaleBasket::GetByID($id))
385
386 ? $row['PRICE'] * $row['QUANTITY'] : 0;*/
387 }
388
389 public static function onBasketUpdate($id, /*array*/ $fields)
390 {
391 /*if (Loader::includeModule('conversion')
392 && is_array($fields)
393 && isset($fields['PRICE'], $fields['QUANTITY'], $fields['CURRENCY']))
394 {
395 $context = DayContext::getInstance();
396
397 $newSum = $fields['PRICE'] * $fields['QUANTITY'];
398
399 // add item to cart
400 if ($newSum > self::$onBeforeBasketUpdate)
401 {
402 $context->addCurrencyCounter('sale_cart_sum_add', $newSum - self::$onBeforeBasketUpdate, $fields['CURRENCY']);
403 }
404 // remove item from cart
405 elseif ($newSum < self::$onBeforeBasketUpdate)
406 {
407 $context->addCurrencyCounter('sale_cart_sum_rem', self::$onBeforeBasketUpdate - $newSum, $fields['CURRENCY']);
408 }
409 }
410
411 self::$onBeforeBasketUpdate = 0;*/
412 }
413
414 //static private $onBeforeBasketDeleteSum = 0;
415 //static private $onBeforeBasketDeleteCurrency; // TODO same to all other
416
417 public static function onBeforeBasketDelete($id)
418 {
419 /*self::$onBeforeBasketDeleteSum =
420
421 Loader::includeModule('conversion')
422 && ($intId = (int) $id) > 0
423 && $intId == $id
424 && ($row = \CSaleBasket::GetByID($id))
425 && (self::$onBeforeBasketDeleteCurrency = $row['CURRENCY'])
426
427 ? $row['PRICE'] * $row['QUANTITY'] : 0;*/
428 }
429
430 public static function onBasketDelete($id)
431 {
432 /*if (Loader::includeModule('conversion') && self::$onBeforeBasketDeleteSum > 0)
433 {
434 $context = DayContext::getInstance();
435 $context->addCurrencyCounter('sale_cart_sum_rem', self::$onBeforeBasketDeleteSum, self::$onBeforeBasketDeleteCurrency);
436 }
437
438 self::$onBeforeBasketDeleteSum = 0;*/
439 }
440
441 // Order Counters
442
443 public static function onSaleOrderSaved(Main\Event $event): void
444 {
445 if (!$event->getParameter('IS_NEW'))
446 {
447 return;
448 }
449
450 $order = $event->getParameter('ENTITY');
451
452 if ($order instanceof Sale\Order)
453 {
454 $price = $order->getPrice();
455 $currency = $order->getCurrency();
456
457 if (Loader::includeModule('conversion'))
458 {
459 $context = DayContext::getSiteInstance($order->getField('LID'));
460
461 $context->addDayCounter('sale_order_add_day', 1);
462 $context->addCounter(new Date(), 'sale_order_add', 1);
463 $context->attachEntityItem('sale_order', $order->getId());
464
465 if ($price && $currency)
466 {
467 $context->addCurrencyCounter('sale_order_sum_add', $price, $currency);
468 }
469 }
470 }
471 }
472
473 public static function onOrderAdd($id, array $fields): void
474 {
475 if (Loader::includeModule('conversion'))
476 {
477 $context = DayContext::getSiteInstance($fields['LID']);
478 $context->addDayCounter('sale_order_add_day', 1);
479 $context->addCounter(new Date(), 'sale_order_add', 1);
480 $context->addCurrencyCounter('sale_order_sum_add', $fields['PRICE'], $fields['CURRENCY']);
481 $context->attachEntityItem('sale_order', $id);
482 }
483 }
484
485 // Payment Counters
486
487 public static function onSaleOrderPaid(Main\Event $event): void
488 {
489 $order = $event->getParameter('ENTITY');
490 if (Loader::includeModule('conversion') && $order instanceof Sale\Order)
491 {
492 self::updatePaidOrderConversion(
493 $order->getId(),
494 $order->getPrice(),
495 $order->getCurrency(),
496 Date::createFromText($order->getField('DATE_PAYED')),
497 $order->isPaid()
498 );
499 }
500 }
501
502 public static function onSalePayOrder($id, $paid): void
503 {
504 if (Loader::includeModule('conversion') && ($row = \CSaleOrder::GetById($id)))
505 {
506 self::updatePaidOrderConversion(
507 $id,
508 $row['PRICE'],
509 $row['CURRENCY'],
510 new Date($row['DATE_PAYED'], 'Y-m-d H:i:s'),
511 $paid === 'Y'
512 );
513 }
514 }
515
526 private static function updatePaidOrderConversion($orderId, $price, $currency, $day, $isPaid): void
527 {
528 $context = DayContext::getEntityItemInstance('sale_order', $orderId);
529 $isAdminSection = defined('ADMIN_SECTION') && ADMIN_SECTION === true;
530
531 if ($isPaid)
532 {
533 $currentDate = new Date();
534 if ($isAdminSection)
535 {
536 $context->addCounter($currentDate, 'sale_payment_add_day', 1);
537 }
538 else
539 {
540 $context->addDayCounter('sale_payment_add_day', 1);
541 }
542
543 $context->addCounter($currentDate, 'sale_payment_add', 1);
544 $context->addCurrencyCounter('sale_payment_sum_add', $price, $currency);
545 unset($currentDate);
546 }
547 else
548 {
549 if ($isAdminSection)
550 {
551 $context->subCounter($day, 'sale_payment_add_day', 1);
552 }
553 else
554 {
555 $context->subDayCounter($day, 'sale_payment_add_day', 1);
556 }
557
558 $context->subCounter($day, 'sale_payment_add', 1);
559 $context->subCurrencyCounter($day, 'sale_payment_sum_add', $price, $currency);
560 }
561 }
562}
$sum
Определения checkout.php:6
static getBaseCurrencyUnit()
Определения utils.php:47
static formatToBaseCurrency($value, $format=null)
Определения utils.php:28
static convertToBaseCurrency($value, $currency)
Определения utils.php:10
Определения date.php:9
static onBeforeBasketUpdate($id, $fields=null)
Определения conversionhandlers.php:377
static onSaleOrderPaid(Main\Event $event)
Определения conversionhandlers.php:487
static onSalePayOrder($id, $paid)
Определения conversionhandlers.php:502
static onGenerateInitialData(Date $from, Date $to)
Определения conversionhandlers.php:177
static onBasketUpdate($id, $fields)
Определения conversionhandlers.php:389
static onBasketAdd($id, $fields)
Определения conversionhandlers.php:355
static onOrderAdd($id, array $fields)
Определения conversionhandlers.php:473
static onSaleBasketItemSaved(Main\Event $event)
Определения conversionhandlers.php:320
static onSaleOrderSaved(Main\Event $event)
Определения conversionhandlers.php:443
static onBeforeBasketAdd( $fields)
Определения conversionhandlers.php:350
static GetList($arOrder=array(), $arFilter=array(), $arGroupBy=false, $arNavStartParams=false, $arSelectFields=array())
Определения basket.php:33
static GetList($arOrder=array("ID"=>"DESC"), $arFilter=array(), $arGroupBy=false, $arNavStartParams=false, $arSelectFields=array(), $arOptions=array())
Определения order.php:339
$data['IS_AVAILABLE']
Определения .description.php:13
$orderId
Определения payment.php:5
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$context
Определения csv_new_setup.php:223
$siteId
Определения ajax.php:8
Определения buffer.php:3
$order
Определения payment.php:8
$event
Определения prolog_after.php:141
const ADMIN_SECTION
Определения rss.php:2
$currency
Определения template.php:266
$counters
Определения options.php:100
$fields
Определения yandex_run.php:501