1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverShop\Reports; |
4
|
|
|
|
5
|
|
|
use SilverShop\Page\Product; |
6
|
|
|
use SilverShop\SQLQueryList\SQLQueryList; |
7
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
8
|
|
|
use SilverStripe\ORM\DataObject; |
9
|
|
|
use SilverStripe\ORM\Queries\SQLSelect; |
10
|
|
|
|
11
|
|
|
class ProductReport extends ShopPeriodReport |
12
|
|
|
{ |
13
|
|
|
protected $title = 'Products'; |
14
|
|
|
|
15
|
|
|
protected $description = 'Understand which products are performing, and which aren\'t.'; |
16
|
|
|
|
17
|
|
|
protected $dataClass = Product::class; |
18
|
|
|
|
19
|
|
|
protected $periodfield = '"SilverShop_Order"."Created"'; |
20
|
|
|
|
21
|
|
|
public function columns() |
22
|
|
|
{ |
23
|
|
|
return [ |
24
|
|
|
'Title' => [ |
25
|
|
|
'title' => 'Title', |
26
|
|
|
'formatting' => '<a href=\"admin/catalog/Product/EditForm/field/Product/item/$ID/edit\" target=\"_new\">$Title</a>', |
27
|
|
|
], |
28
|
|
|
'BasePrice' => 'Price', |
29
|
|
|
'Quantity' => 'Quantity', |
30
|
|
|
'Sales' => 'Sales', |
31
|
|
|
]; |
32
|
|
|
} |
33
|
|
|
|
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
|
|
|
|
54
|
|
|
public function query($params) |
55
|
|
|
{ |
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
|
|
|
if ($start && $end) { |
69
|
|
|
$whereClue = sprintf( |
70
|
|
|
'DATE("o"."Placed") BETWEEN DATE(\'%s\') AND DATE(\'%s\')', |
71
|
|
|
$start, |
72
|
|
|
$end |
73
|
|
|
); |
74
|
|
|
} elseif ($start) { |
75
|
|
|
$whereClue = sprintf( |
76
|
|
|
'DATE("o"."Placed") > DATE(\'%s\')', |
77
|
|
|
$start |
78
|
|
|
); |
79
|
|
|
} elseif ($end) { |
80
|
|
|
$whereClue = sprintf( |
81
|
|
|
'DATE("o"."Placed") <= DATE(\'%s\')', |
82
|
|
|
$end |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$completedStatus = '\'' . implode('\', \'', [ |
87
|
|
|
'Unpaid', 'Paid', 'Processing', 'Sent', 'Complete' |
88
|
|
|
]) . '\''; |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
$query->setSelect( |
92
|
|
|
[ |
93
|
|
|
'"SiteTree"."ID"', |
94
|
|
|
'"SiteTree"."Title"', |
95
|
|
|
'"SilverShop_Product"."BasePrice"', |
96
|
|
|
] |
97
|
|
|
) |
98
|
|
|
->selectField( |
99
|
|
|
sprintf( |
100
|
|
|
'( |
101
|
|
|
SELECT |
102
|
|
|
SUM(soi."Quantity") |
103
|
|
|
FROM |
104
|
|
|
"SilverShop_Product_OrderItem" spo, |
105
|
|
|
"SilverShop_OrderItem" soi, |
106
|
|
|
"SilverShop_OrderAttribute" soa, |
107
|
|
|
"SilverShop_Order" o |
108
|
|
|
WHERE |
109
|
|
|
spo.ProductID = "SilverShop_Product"."ID" |
110
|
|
|
AND spo.ID = soi.ID |
111
|
|
|
AND soi.ID = spo.ID |
112
|
|
|
AND spo.ID = soa.ID |
113
|
|
|
AND soa.OrderID = o.ID |
114
|
|
|
AND o.Status IN (%s) |
115
|
|
|
AND %s |
116
|
|
|
)', |
117
|
|
|
$completedStatus, |
118
|
|
|
$whereClue |
119
|
|
|
), |
120
|
|
|
'Quantity' |
121
|
|
|
) |
122
|
|
|
->selectField( |
123
|
|
|
sprintf( |
124
|
|
|
'( |
125
|
|
|
SELECT |
126
|
|
|
SUM(soa."CalculatedTotal") |
127
|
|
|
FROM |
128
|
|
|
"SilverShop_Product_OrderItem" spo, |
129
|
|
|
"SilverShop_OrderItem" soi, |
130
|
|
|
"SilverShop_OrderAttribute" soa, |
131
|
|
|
"SilverShop_Order" o |
132
|
|
|
WHERE |
133
|
|
|
spo.ProductID = "SilverShop_Product"."ID" |
134
|
|
|
AND spo.ID = soi.ID |
135
|
|
|
AND soi.ID = spo.ID |
136
|
|
|
AND spo.ID = soa.ID |
137
|
|
|
AND soa.OrderID = o.ID |
138
|
|
|
AND o.Status IN (%s) |
139
|
|
|
AND %s |
140
|
|
|
)', |
141
|
|
|
$completedStatus, |
142
|
|
|
$whereClue |
143
|
|
|
), |
144
|
|
|
'Sales' |
145
|
|
|
) |
146
|
|
|
; |
147
|
|
|
|
148
|
|
|
$query->addInnerJoin('SiteTree', '"SilverShop_Product"."ID" = "SiteTree"."ID"'); |
149
|
|
|
$query->setOrderBy('Quantity DESC'); |
150
|
|
|
return $query; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|