Bitrix-D7  20.0.0
Класс BuyerStatistic

Открытые статические члены

static getList ($filter)
 Executes the query and returns selection by parameters of the query. Подробнее...
 
static calculate ($userId, $currency, $lid)
 Fill statistic for user for certain site and currency. Подробнее...
 

Защищенные статические члены

static collectUserData ($userId, $currency, $lid)
 Collect statistic (last order date, count of full paid orders, count of partially paid orders and sum of paid payments) for user, certain site and currency. Подробнее...
 

Подробное описание

См. определение в файле buyerstatistic.php строка 18

Методы

◆ calculate()

static calculate (   $userId,
  $currency,
  $lid 
)
static

Fill statistic for user for certain site and currency.

The function'll update values if user entry exists or it'll create new if user entry doesn't exist

Аргументы
$userId
$currency
$lid
Возвращает
Main\Result

См. определение в файле buyerstatistic.php строка 41

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  $id = $buyerStatistic['ID'];
60 
61  if (empty($id))
62  return Internals\BuyerStatisticTable::add($result->getData());
63  else
64  return Internals\BuyerStatisticTable::update($id, $result->getData());
65  }

◆ collectUserData()

static collectUserData (   $userId,
  $currency,
  $lid 
)
staticprotected

Collect statistic (last order date, count of full paid orders, count of partially paid orders and sum of paid payments) for user, certain site and currency.

Аргументы
$userId
$currency
$lid
Возвращает
Result

См. определение в файле buyerstatistic.php строка 76

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 
87  /** @var Sale\Order $orderClass */
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) ? $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  }

◆ getList()

static getList (   $filter)
static

Executes the query and returns selection by parameters of the query.

This function is an alias to the Query object functions

Возвращает
Main\DB\Result
Исключения

См. определение в файле buyerstatistic.php строка 26

27  {
28  return Internals\BuyerStatisticTable::getList($filter);
29  }

Объявления и описания членов класса находятся в файле:
Bitrix\Sale\Registry\REGISTRY_TYPE_ORDER
const REGISTRY_TYPE_ORDER
Definition: sale/lib/registry.php:16
Bitrix\Sale\Registry\getInstance
static getInstance($type)
Definition: sale/lib/registry.php:161
Bitrix\Sale\Archive\Manager\getList
static getList(array $parameters=array())
Definition: sale/lib/archive/manager.php:205
Sale
Class RestHandler.