Skip to content

Commit

Permalink
Fixed deletd orders being used in widget calculations fixes #1490
Browse files Browse the repository at this point in the history
  • Loading branch information
nfourtythree committed Jun 9, 2020
1 parent bfa6653 commit 8b08fae
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
### Added
- Added the `craft\commerce\services\Sales::EVENT_AFTER_DELETE_SALE` event.

### Fixed
- Fixed a bug where reporting widgets were including deleted orders in calculations. ([#1490](https://github.com/craftcms/commerce/issues/1490))

## 3.1.7 - 2020-06-02

### Fixed
Expand Down
4 changes: 3 additions & 1 deletion src/base/Stat.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,11 @@ protected function _createStatQuery()
{
return (new Query)
->from(Table::ORDERS . ' orders')
->innerJoin('{{%elements}} elements', '[[elements.id]] = [[orders.id]]')
->where(['>=', 'dateOrdered', Db::prepareDateForDb($this->_startDate)])
->andWhere(['<=', 'dateOrdered', Db::prepareDateForDb($this->_endDate)])
->andWhere(['isCompleted' => 1]);
->andWhere(['isCompleted' => 1])
->andWhere(['elements.dateDeleted' => null]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/stats/AverageOrderTotal.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class AverageOrderTotal extends Stat
public function getData()
{
$query = $this->_createStatQuery();
$query->select([new Expression('ROUND(SUM([[total]]) / COUNT([[id]]), 4) as averageOrderTotal')]);
$query->select([new Expression('ROUND(SUM([[total]]) / COUNT([[orders.id]]), 4) as averageOrderTotal')]);

return $query->scalar();
}
Expand Down
2 changes: 1 addition & 1 deletion src/stats/RepeatCustomers.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function getData()
->count();

$repeatRows = $this->_createStatQuery()
->select([new Expression('COUNT([[id]])')])
->select([new Expression('COUNT([[orders.id]])')])
->groupBy('customerId')
->column();

Expand Down
6 changes: 3 additions & 3 deletions src/stats/TopCustomers.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ public function getData()
$topCustomers = $this->_createStatQuery()
->select([
new Expression('SUM([[total]]) as total'),
new Expression('ROUND((SUM([[total]]) / COUNT([[id]])), 4) as average'),
new Expression('ROUND((SUM([[total]]) / COUNT([[orders.id]])), 4) as average'),
'customerId',
'[[orders.email]] as email',
new Expression('COUNT([[id]]) as count'),
new Expression('COUNT([[orders.id]]) as count'),
])
->groupBy(['[[orders.customerId]]', '[[orders.email]]'])
->limit($this->limit);

if ($this->type == 'average') {
$topCustomers->orderBy(new Expression('ROUND((SUM([[total]]) / COUNT([[id]])), 4) DESC'));
$topCustomers->orderBy(new Expression('ROUND((SUM([[total]]) / COUNT([[orders.id]])), 4) DESC'));
} else {
$topCustomers->orderBy(new Expression('SUM([[total]]) DESC'));
}
Expand Down
4 changes: 2 additions & 2 deletions src/stats/TotalOrders.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ class TotalOrders extends Stat
public function getData()
{
$query = $this->_createStatQuery();
$query->select([new Expression('COUNT([[id]]) as total')]);
$query->select([new Expression('COUNT([[orders.id]]) as total')]);

$chartData = $this->_createChartQuery([
new Expression('COUNT([[id]]) as total'),
new Expression('COUNT([[orders.id]]) as total'),
], [
'total' => 0,
]);
Expand Down
2 changes: 1 addition & 1 deletion src/stats/TotalRevenue.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function getData()
return $this->_createChartQuery(
[
new Expression('SUM([[total]]) as revenue'),
new Expression('COUNT([[id]]) as count'),
new Expression('COUNT([[orders.id]]) as count'),
],
[
'revenue' => 0,
Expand Down

0 comments on commit 8b08fae

Please sign in to comment.