Skip to content

Commit 02d6353

Browse files
authored
fixed the sales report so it returns the number of sales and revenue … (#777)
* fixed the sales report so it returns the number of sales and revenue generated during a certain period of time. * default report value is 1
1 parent 3daa277 commit 02d6353

File tree

1 file changed

+112
-20
lines changed

1 file changed

+112
-20
lines changed

src/Reports/ProductReport.php

Lines changed: 112 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
namespace SilverShop\Reports;
44

55
use SilverShop\Page\Product;
6+
use SilverShop\SQLQueryList\SQLQueryList;
67
use SilverStripe\CMS\Model\SiteTree;
8+
use SilverStripe\ORM\DataObject;
9+
use SilverStripe\ORM\Queries\SQLSelect;
710

811
class ProductReport extends ShopPeriodReport
912
{
@@ -13,7 +16,7 @@ class ProductReport extends ShopPeriodReport
1316

1417
protected $dataClass = Product::class;
1518

16-
protected $periodfield = '"SiteTree"."Created"';
19+
protected $periodfield = '"SilverShop_Order"."Created"';
1720

1821
public function columns()
1922
{
@@ -23,35 +26,124 @@ public function columns()
2326
'formatting' => '<a href=\"admin/catalog/Product/EditForm/field/Product/item/$ID/edit\" target=\"_new\">$Title</a>',
2427
),
2528
'BasePrice' => 'Price',
26-
'Created' => 'Created',
2729
'Quantity' => 'Quantity',
2830
'Sales' => 'Sales',
2931
);
3032
}
3133

34+
35+
public function sourceRecords($params)
36+
{
37+
$list = SQLQueryList::create($this->query($params));
38+
$self = $this;
39+
$list->setOutputClosure(
40+
function ($row) use ($self) {
41+
$row['BasePrice'] = $self->formatMoney($row['BasePrice']);
42+
$row['Sales'] = $self->formatMoney($row['Sales']);
43+
return new $self->dataClass($row);
44+
}
45+
);
46+
return $list;
47+
}
48+
49+
private function formatMoney($money)
50+
{
51+
return number_format($money, 2);
52+
}
53+
3254
public function query($params)
3355
{
34-
$query = parent::query($params);
35-
$query->selectField($this->periodfield, 'FilterPeriod')
36-
->addSelect(
37-
[
38-
'"SilverShop_Product"."ID"',
39-
'"SiteTree"."ClassName"',
56+
//convert dates to correct format
57+
$fields = $this->parameterFields();
58+
$fields->setValues($params);
59+
$start = $fields->fieldByName('StartPeriod')->dataValue();
60+
$end = $fields->fieldByName('EndPeriod')->dataValue();
61+
62+
63+
$table = DataObject::getSchema()->tableName($this->dataClass);
64+
$query = new SQLSelect();
65+
$query->setFrom('"' . $table . '"');
66+
67+
$whereClue = '1';
68+
$filterperiod = $this->periodfield;
69+
if ($start && $end) {
70+
$whereClue = sprintf(
71+
'DATE("o"."Placed") BETWEEN DATE(\'%s\') AND DATE(\'%s\')',
72+
$start,
73+
$end
74+
);
75+
} elseif ($start) {
76+
$whereClue = sprintf(
77+
'DATE("o"."Placed") > DATE(\'%s\')',
78+
$start
79+
);
80+
} elseif ($end) {
81+
$whereClue = sprintf(
82+
'DATE("o"."Placed") <= DATE(\'%s\')',
83+
$end
84+
);
85+
}
86+
87+
$completedStatus = '\'' . implode('\', \'', [
88+
'Unpaid', 'Paid', 'Processing', 'Sent', 'Complete'
89+
]) . '\'';
90+
91+
92+
$query->setSelect(
93+
[
94+
'"SiteTree"."ID"',
4095
'"SiteTree"."Title"',
4196
'"SilverShop_Product"."BasePrice"',
42-
'"SiteTree"."Created"',
43-
]
44-
)
45-
->selectField('COUNT("SilverShop_OrderItem"."Quantity")', 'Quantity')
46-
->selectField('SUM("SilverShop_OrderAttribute"."CalculatedTotal")', 'Sales');
47-
$query->addInnerJoin('SiteTree', '"SilverShop_Product"."ID" = "SiteTree"."ID"');
48-
$query->addLeftJoin('SilverShop_Product_OrderItem', 'SilverShop_Product.ID = "SilverShop_Product_OrderItem"."ProductID"');
49-
$query->addLeftJoin('SilverShop_OrderItem', '"SilverShop_Product_OrderItem"."ID" = "SilverShop_OrderItem"."ID"');
50-
$query->addLeftJoin('SilverShop_OrderAttribute', '"SilverShop_Product_OrderItem"."ID" = "SilverShop_OrderAttribute"."ID"');
51-
$query->addLeftJoin('SilverShop_Order', '"SilverShop_OrderAttribute"."OrderID" = "SilverShop_Order"."ID"');
52-
$query->addGroupby('"SilverShop_Product"."ID"');
53-
$query->addWhere('"SilverShop_Order"."Paid" IS NOT NULL OR "SilverShop_Product_OrderItem"."ID" IS NULL');
97+
]
98+
)
99+
->selectField(
100+
sprintf(
101+
'(
102+
SELECT
103+
SUM(soi."Quantity")
104+
FROM
105+
"SilverShop_Product_OrderItem" spo,
106+
"SilverShop_OrderItem" soi,
107+
"SilverShop_OrderAttribute" soa,
108+
"SilverShop_Order" o
109+
WHERE
110+
spo.ProductID = "SilverShop_Product"."ID"
111+
AND spo.ID = soi.ID
112+
AND soi.ID = spo.ID
113+
AND spo.ID = soa.ID
114+
AND soa.OrderID = o.ID
115+
AND o.Status IN (%s)
116+
AND %s
117+
)',
118+
$completedStatus,
119+
$whereClue
120+
), 'Quantity')
121+
->selectField(
122+
sprintf(
123+
'(
124+
SELECT
125+
SUM(soa."CalculatedTotal")
126+
FROM
127+
"SilverShop_Product_OrderItem" spo,
128+
"SilverShop_OrderItem" soi,
129+
"SilverShop_OrderAttribute" soa,
130+
"SilverShop_Order" o
131+
WHERE
132+
spo.ProductID = "SilverShop_Product"."ID"
133+
AND spo.ID = soi.ID
134+
AND soi.ID = spo.ID
135+
AND spo.ID = soa.ID
136+
AND soa.OrderID = o.ID
137+
AND o.Status IN (%s)
138+
AND %s
139+
)',
140+
$completedStatus,
141+
$whereClue
142+
), 'Sales')
143+
;
54144

145+
$query->addInnerJoin('SiteTree', '"SilverShop_Product"."ID" = "SiteTree"."ID"');
146+
$query->setOrderBy('Quantity DESC');
55147
return $query;
56148
}
57149
}

0 commit comments

Comments
 (0)