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