/
TotalOrdersByCountry.php
121 lines (102 loc) · 3.34 KB
/
TotalOrdersByCountry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\commerce\stats;
use Craft;
use craft\commerce\base\Stat;
use craft\db\Table as CraftTable;
use craft\helpers\ArrayHelper;
use yii\db\Expression;
/**
* Total Orders by Country Stat
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0
*/
class TotalOrdersByCountry extends Stat
{
/**
* @inheritdoc
*/
protected string $_handle = 'totalOrdersByCountry';
/**
* @var string Type of stat e.g. 'shipping' or 'billing'.
*/
public string $type = 'shipping';
public int $limit = 5;
/**
* @inheritDoc
*/
public function __construct(string $dateRange = null, string $type = null, $startDate = null, $endDate = null)
{
$this->type = $type ?? $this->type;
parent::__construct($dateRange, $startDate, $endDate);
}
/**
* @inheritDoc
*/
public function getData(): array
{
$query = $this->_createStatQuery();
$query->select([
'countryCode' => ($this->type == 'billing' ? '[[b.countryCode]]' : '[[s.countryCode]]'),
'total' => new Expression('COUNT([[orders.id]])'),
]);
$query->leftJoin(CraftTable::ADDRESSES . ' s', '[[s.id]] = [[orders.shippingAddressId]]');
$query->leftJoin(CraftTable::ADDRESSES . ' b', '[[b.id]] = [[orders.billingAddressId]]');
if ($this->type == 'billing') {
$query->andWhere(['not', ['[[b.countryCode]]' => null]]);
$query->groupBy('[[b.countryCode]]');
} else {
$query->andWhere(['not', ['[[s.countryCode]]' => null]]);
$query->groupBy('[[s.countryCode]]');
}
$query->orderBy(new Expression('COUNT([[orders.id]]) DESC'));
$query->limit($this->limit);
$rows = $query->all();
if (count($rows) < $this->limit) {
return $rows;
}
$countryCodes = ArrayHelper::getColumn($rows, 'countryCode', false);
$otherCountries = $this->_createStatQuery()
->select([
'total' => new Expression('COUNT([[orders.id]])'),
'countryCode' => new Expression('NULL'),
])
->leftJoin(CraftTable::ADDRESSES . ' s', '[[s.id]] = [[orders.shippingAddressId]]')
->leftJoin(CraftTable::ADDRESSES . ' b', '[[b.id]] = [[orders.billingAddressId]]')
->andWhere(['not', [($this->type == 'billing' ? '[[b.countryCode]]' : '[[s.countryCode]]') => $countryCodes]])
->one();
if (empty($otherCountries)) {
return $rows;
}
$otherCountries['name'] = Craft::t('commerce', 'Other countries');
$rows[] = $otherCountries;
return $rows;
}
/**
* @inheritDoc
*/
public function getHandle(): string
{
return $this->_handle . $this->type;
}
/**
* @inheritDoc
*/
public function prepareData($data): mixed
{
if (!empty($data)) {
foreach ($data as &$row) {
if (!$row['countryCode']) {
continue;
}
$row['name'] = Craft::$app->getAddresses()->getCountryRepository()->get($row['countryCode'])->getName();
}
}
return $data;
}
}