28 return Internals\BuyerStatisticTable::getList($filter);
41 public static function calculate($userId, $currency, $lid)
43 $result = static::collectUserData($userId, $currency, $lid);
45 if (!$result->isSuccess() || $result->hasWarnings())
50 $statisticData = static::getList(
52 'select' => array(
'ID'),
53 'filter' => array(
'=USER_ID' => $userId,
'=CURRENCY' => $currency,
'=LID' => $lid),
58 $buyerStatistic = $statisticData->fetch();
61 return Internals\BuyerStatisticTable::add($result->getData());
64 return Internals\BuyerStatisticTable::update($buyerStatistic[
'ID'], $result->getData());
76 protected static function collectUserData($userId, $currency, $lid)
79 $userId = (int)$userId;
82 $result->addError(
new Main\
Error(
'Wrong user id'));
88 $orderClass = $registry->getOrderClassName();
90 $lastOrderDate =
null;
91 $lastArchiveDate =
null;
93 $orderData = $orderClass::getList([
94 'select' => [
'DATE_INSERT'],
95 'filter' => [
'=USER_ID' => $userId,
'=CURRENCY' => $currency,
'=LID' => $lid],
96 'order' => [
'DATE_INSERT' =>
'DESC'],
100 if ($resultOrder = $orderData->fetch())
102 $lastOrderDate = $resultOrder[
'DATE_INSERT'];
105 $archiveData = Archive\Manager::getList([
106 'select' => [
'DATE_INSERT'],
107 'filter' => [
'=USER_ID' => $userId,
'=CURRENCY' => $currency,
'=LID' => $lid],
108 'order' => [
'DATE_INSERT' =>
'DESC'],
112 if ($resultOrder = $archiveData->fetch())
114 $lastArchiveDate = $resultOrder[
'DATE_INSERT'];
117 if ($lastOrderDate || $lastArchiveDate)
120 'USER_ID' => $userId,
121 'CURRENCY' => $currency,
123 'LAST_ORDER_DATE' => ($lastOrderDate) ? $lastOrderDate : $lastArchiveDate
128 $orderDataCount = $orderClass::getList([
129 'select' => [
'FULL_SUM_PAID',
'COUNT_FULL_PAID_ORDER',
'COUNT_PART_PAID_ORDER'],
131 '=USER_ID' => $userId,
132 '=CURRENCY' => $currency,
136 'group' => [
'USER_ID'],
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)')
144 $countData = $orderDataCount->fetch();
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;
151 if ($lastArchiveDate)
153 $archiveDataCount = Archive\Manager::getList([
154 'select' => [
'FULL_SUM_PAID',
'COUNT_FULL_PAID_ORDER',
'COUNT_PART_PAID_ORDER'],
156 '=USER_ID' => $userId,
157 '=CURRENCY' => $currency,
161 'group' => [
'USER_ID'],
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)')
169 $countArchiveData = $archiveDataCount->fetch();
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'];
179 $result->setData($statistic);
183 $result->addWarning(
new Main\
Error(
'User doesn\'t have orders' ));
static loadMessages($file)