1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
buyerstatistic.php
См. документацию.
1<?php
8namespace Bitrix\Sale;
9
10use Bitrix\Main;
11use Bitrix\Sale\Internals;
12use Bitrix\Sale;
13use Bitrix\Main\Entity\ExpressionField;
14use Bitrix\Main\Localization\Loc;
15
16Loc::loadMessages(__FILE__);
17
19{
26 public static function getList($filter)
27 {
28 return Internals\BuyerStatisticTable::getList($filter);
29 }
30
41 public static function calculate($userId, $currency, $lid)
42 {
43 $result = static::collectUserData($userId, $currency, $lid);
44
45 if (!$result->isSuccess() || $result->hasWarnings())
46 {
47 return $result;
48 }
49
50 $statisticData = static::getList(
51 array(
52 'select' => array('ID'),
53 'filter' => array('=USER_ID' => $userId, '=CURRENCY' => $currency, '=LID' => $lid),
54 'limit' => 1
55 )
56 );
57
58 $buyerStatistic = $statisticData->fetch();
59 if (!$buyerStatistic)
60 {
61 return Internals\BuyerStatisticTable::add($result->getData());
62 }
63
64 return Internals\BuyerStatisticTable::update($buyerStatistic['ID'], $result->getData());
65 }
66
76 protected static function collectUserData($userId, $currency, $lid)
77 {
78 $result = new Result();
79 $userId = (int)$userId;
80 if ($userId <= 0)
81 {
82 $result->addError(new Main\Error('Wrong user id'));
83 return $result;
84 }
85
88 $orderClass = $registry->getOrderClassName();
89
90 $lastOrderDate = null;
91 $lastArchiveDate = null;
92
93 $orderData = $orderClass::getList([
94 'select' => ['DATE_INSERT'],
95 'filter' => ['=USER_ID' => $userId, '=CURRENCY' => $currency, '=LID' => $lid],
96 'order' => ['DATE_INSERT' => 'DESC'],
97 'limit' => 1
98 ]);
99
100 if ($resultOrder = $orderData->fetch())
101 {
102 $lastOrderDate = $resultOrder['DATE_INSERT'];
103 }
104
105 $archiveData = Archive\Manager::getList([
106 'select' => ['DATE_INSERT'],
107 'filter' => ['=USER_ID' => $userId, '=CURRENCY' => $currency, '=LID' => $lid],
108 'order' => ['DATE_INSERT' => 'DESC'],
109 'limit' => 1
110 ]);
111
112 if ($resultOrder = $archiveData->fetch())
113 {
114 $lastArchiveDate = $resultOrder['DATE_INSERT'];
115 }
116
117 if ($lastOrderDate || $lastArchiveDate)
118 {
119 $statistic = array(
120 'USER_ID' => $userId,
121 'CURRENCY' => $currency,
122 'LID' => $lid,
123 'LAST_ORDER_DATE' => ($lastOrderDate) ?: $lastArchiveDate
124 );
125
126 if ($lastOrderDate)
127 {
128 $orderDataCount = $orderClass::getList([
129 'select' => ['FULL_SUM_PAID', 'COUNT_FULL_PAID_ORDER', 'COUNT_PART_PAID_ORDER'],
130 'filter' => [
131 '=USER_ID' => $userId,
132 '=CURRENCY' => $currency,
133 '=LID' => $lid,
134 '>SUM_PAID' => 0
135 ],
136 'group' => ['USER_ID'],
137 'runtime' => [
138 new ExpressionField('COUNT_PART_PAID_ORDER', 'COUNT(1)'),
139 new ExpressionField('COUNT_FULL_PAID_ORDER', 'SUM(CASE WHEN PAYED = \'Y\' THEN 1 ELSE 0 END)'),
140 new ExpressionField('FULL_SUM_PAID', 'SUM(SUM_PAID)')
141 ],
142 ]);
143
144 $countData = $orderDataCount->fetch();
145
146 $statistic['SUM_PAID'] = !empty($countData['FULL_SUM_PAID']) ? $countData['FULL_SUM_PAID'] : "0.0000";
147 $statistic['COUNT_PART_PAID_ORDER'] = !empty($countData['COUNT_PART_PAID_ORDER']) ? $countData['COUNT_PART_PAID_ORDER'] : 0;
148 $statistic['COUNT_FULL_PAID_ORDER'] = !empty($countData['COUNT_FULL_PAID_ORDER']) ? $countData['COUNT_FULL_PAID_ORDER'] : 0;
149 }
150
151 if ($lastArchiveDate)
152 {
153 $archiveDataCount = Archive\Manager::getList([
154 'select' => ['FULL_SUM_PAID', 'COUNT_FULL_PAID_ORDER', 'COUNT_PART_PAID_ORDER'],
155 'filter' => [
156 '=USER_ID' => $userId,
157 '=CURRENCY' => $currency,
158 '=LID' => $lid,
159 '>SUM_PAID' => 0
160 ],
161 'group' => ['USER_ID'],
162 'runtime' => [
163 new ExpressionField('COUNT_PART_PAID_ORDER', 'COUNT(1)'),
164 new ExpressionField('COUNT_FULL_PAID_ORDER', 'SUM(CASE WHEN PAYED = \'Y\' THEN 1 ELSE 0 END)'),
165 new ExpressionField('FULL_SUM_PAID', 'SUM(SUM_PAID)')
166 ],
167 ]);
168
169 $countArchiveData = $archiveDataCount->fetch();
170
171 if ($countArchiveData['FULL_SUM_PAID'] > 0)
172 $statistic['SUM_PAID'] += $countArchiveData['FULL_SUM_PAID'];
173 if ($countArchiveData['COUNT_PART_PAID_ORDER'] > 0)
174 $statistic['COUNT_PART_PAID_ORDER'] += $countArchiveData['COUNT_PART_PAID_ORDER'];
175 if ($countArchiveData['COUNT_FULL_PAID_ORDER'] > 0)
176 $statistic['COUNT_FULL_PAID_ORDER'] += $countArchiveData['COUNT_FULL_PAID_ORDER'];
177 }
178
179 $result->setData($statistic);
180 }
181 else
182 {
183 $result->addWarning(new Main\Error('User doesn\'t have orders' ));
184 }
185
186 return $result;
187 }
188}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
Определения error.php:15
static getList($filter)
Определения buyerstatistic.php:26
static calculate($userId, $currency, $lid)
Определения buyerstatistic.php:41
static getInstance($type)
Определения registry.php:183
const REGISTRY_TYPE_ORDER
Определения registry.php:16
</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
$filter
Определения iblock_catalog_list.php:54
trait Error
Определения error.php:11
$currency
Определения template.php:266