1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @package ecommercextras |
5
|
|
|
* @author nicolaas[at]sunnysideup.co.nz |
6
|
|
|
*/ |
7
|
|
|
class SearchableOrderReport extends SalesReport |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
protected $title = 'Searchable Orders'; |
10
|
|
|
|
11
|
|
|
protected $description = 'Search all orders'; |
12
|
|
|
|
13
|
|
|
protected static $default_from_time = "11:00 am"; |
14
|
|
|
public static function set_default_from_time($v) |
15
|
|
|
{ |
16
|
|
|
self::$default_from_time = $v; |
17
|
|
|
} |
18
|
|
|
public static function get_default_from_time() |
19
|
|
|
{ |
20
|
|
|
return self::$default_from_time; |
21
|
|
|
} |
22
|
|
|
public static function get_default_from_time_as_full_date_time() |
23
|
|
|
{ |
24
|
|
|
return date("Y-m-d", time()) . " " . date("H:i", strtotime(self::get_default_from_time())); |
25
|
|
|
} |
26
|
|
|
protected static $default_until_time = "10:00 pm"; |
27
|
|
|
public static function set_default_until_time($v) |
28
|
|
|
{ |
29
|
|
|
self::$default_until_time = $v; |
30
|
|
|
} |
31
|
|
|
public static function get_default_until_time() |
32
|
|
|
{ |
33
|
|
|
return self::$default_until_time; |
34
|
|
|
} |
35
|
|
|
public static function get_default_until_time_as_full_date_time() |
36
|
|
|
{ |
37
|
|
|
return date("Y-m-d", time()) . " " . date("H:i", strtotime(self::get_default_until_time())); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function parameterFields() |
41
|
|
|
{ |
42
|
|
|
//$fields = parent::getCMSFields(); |
|
|
|
|
43
|
|
|
$fields =new FieldSet(); |
44
|
|
|
$stats[] = "Count: ".$this->statistic("count"); |
|
|
|
|
45
|
|
|
$stats[] = "Sum: ".$this->currencyFormat($this->statistic("sum")); |
46
|
|
|
$stats[] = "Avg: ".$this->currencyFormat($this->statistic("avg")); |
47
|
|
|
$stats[] = "Min: ".$this->currencyFormat($this->statistic("min")); |
48
|
|
|
$stats[] = "Max: ".$this->currencyFormat($this->statistic("max")); |
49
|
|
|
if ($this->statistic("count") > 3) { |
50
|
|
|
$fields->push(new LiteralField("stats", '<h2>Payment Statistics</h2><ul><li>'.implode('</li><li>', $stats).'</li></ul>')); |
51
|
|
|
} |
52
|
|
View Code Duplication |
if ($humanWhere = Session::get("SearchableOrderReport.humanWhere")) { |
|
|
|
|
53
|
|
|
$fields->push(new LiteralField("humanWhere", "<p>Current Search: ".$humanWhere."</p>"), "ReportDescription"); |
54
|
|
|
$fields->removeByName("ReportDescription"); |
55
|
|
|
$fields->push(new FormAction('clearSearch', 'Clear Search')); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
$fields->push(new CheckboxSetField("Status", "Order Status", OrderDecorator::get_order_status_options())); |
|
|
|
|
58
|
|
|
$fields->push(new NumericField("OrderID", "Order ID")); |
59
|
|
|
$fields->push(new DateField("From", "From...")); |
60
|
|
|
$fields->push(new TimeField("FromTime", "Start time...", self::get_default_from_time_as_full_date_time(), "H:i a")); |
|
|
|
|
61
|
|
|
$fields->push(new DateField("Until", "Until...")); |
62
|
|
|
$fields->push(new TimeField("UntilTime", "End time...", self::get_default_until_time_as_full_date_time(), "H:i a")); |
|
|
|
|
63
|
|
|
$fields->push(new TextField("Email", "Email")); |
64
|
|
|
$fields->push(new TextField("FirstName", "First Name")); |
65
|
|
|
$fields->push(new TextField("Surname", "Surname")); |
66
|
|
|
$fields->push(new NumericField("HasMinimumPayment", "Has Minimum Payment of ...")); |
67
|
|
|
$fields->push(new NumericField("HasMaximumPayment", "Has Maximum Payment of ...")); |
68
|
|
|
$fields->push(new FormAction('doSearch', 'Apply Search')); |
|
|
|
|
69
|
|
|
$fields->push(new LiteralField('doExport', '<a href="SalesReport_Handler/fullsalesexport/">export all details (do a search first to limit results)</a>')); |
70
|
|
|
return $fields; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function processform() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`"; |
76
|
|
|
$where = array(); |
77
|
|
|
$having = array(); |
78
|
|
|
$humanWhere = array(); |
79
|
|
|
foreach ($_REQUEST as $key => $value) { |
80
|
|
|
$value = Convert::raw2sql($value); |
81
|
|
|
if ($value) { |
82
|
|
|
switch ($key) { |
83
|
|
|
case "OrderID": |
84
|
|
|
$where[] = " {$bt}Order{$bt}.{$bt}ID{$bt} = ".intval($value); |
85
|
|
|
$humanWhere[] = ' OrderID equals '.intval($value); |
86
|
|
|
break; |
87
|
|
View Code Duplication |
case "From": |
|
|
|
|
88
|
|
|
$d = new Date("date"); |
89
|
|
|
$d->setValue($value); |
90
|
|
|
$t = new Time("time"); |
91
|
|
|
$cleanTime = trim(preg_replace('/([ap]m)/', "", Convert::raw2sql($_REQUEST["FromTime"]))); |
92
|
|
|
$t->setValue($cleanTime); // |
93
|
|
|
$exactTime = strtotime($d->format("Y-m-d")." ".$t->Nice24()); |
94
|
|
|
$where[] = " UNIX_TIMESTAMP({$bt}Order{$bt}.{$bt}Created{$bt}) >= '".$exactTime."'"; |
95
|
|
|
$humanWhere[] = ' Order on or after '.Date("r", $exactTime);//r = Example: Thu, 21 Dec 2000 16:01:07 +0200 // also consider: l jS \of F Y H:i Z(e) |
|
|
|
|
96
|
|
|
break; |
97
|
|
View Code Duplication |
case "Until": |
|
|
|
|
98
|
|
|
$d = new Date("date"); |
99
|
|
|
$d->setValue($value); |
100
|
|
|
$t = new Time("time"); |
101
|
|
|
$cleanTime = trim(preg_replace('/([ap]m)/', "", Convert::raw2sql($_REQUEST["FromTime"]))); |
102
|
|
|
$t->setValue($cleanTime); // |
103
|
|
|
$exactTime = strtotime($d->format("Y-m-d")." ".$t->Nice24()); |
104
|
|
|
$where[] = " UNIX_TIMESTAMP({$bt}Order{$bt}.{$bt}Created{$bt}) <= '".$exactTime."'"; |
105
|
|
|
$humanWhere[] = ' Order before or on '.Date("r", $exactTime);//r = Example: Thu, 21 Dec 2000 16:01:07 +0200 // also consider: l jS \of F Y H:i Z(e) |
|
|
|
|
106
|
|
|
break; |
107
|
|
View Code Duplication |
case "Email": |
|
|
|
|
108
|
|
|
$where[] = " {$bt}Member{$bt}.{$bt}Email{$bt} = '".$value."'"; |
109
|
|
|
$humanWhere[] = ' Customer Email equals "'.$value.'"'; |
110
|
|
|
break; |
111
|
|
View Code Duplication |
case "FirstName": |
|
|
|
|
112
|
|
|
$where[] = " {$bt}Member{$bt}.{$bt}FirstName{$bt} LIKE '%".$value."%'"; |
113
|
|
|
$humanWhere[] = ' Customer First Name equals '.$value.'"'; |
114
|
|
|
break; |
115
|
|
View Code Duplication |
case "Surname": |
|
|
|
|
116
|
|
|
$where[] = " {$bt}Member{$bt}.{$bt}Surname{$bt} LIKE '%".$value."%'"; |
117
|
|
|
$humanWhere[] = ' Customer Surname equals "'.$value.'"'; |
118
|
|
|
break; |
119
|
|
|
case "Status": |
120
|
|
|
$subWhere = array(); |
121
|
|
|
foreach ($value as $item) { |
122
|
|
|
$subWhere[] = " {$bt}Order{$bt}.{$bt}Status{$bt} = '".$item."'"; |
123
|
|
|
$humanWhere[] = ' Order Status equals "'.$item.'"'; |
124
|
|
|
} |
125
|
|
|
if (count($subWhere)) { |
126
|
|
|
$where[] = implode(" OR ", $subWhere); |
127
|
|
|
} |
128
|
|
|
break; |
129
|
|
View Code Duplication |
case "HasMinimumPayment": |
|
|
|
|
130
|
|
|
$having[] = ' RealPayments > '.intval($value); |
131
|
|
|
$humanWhere[] = ' Real Payment of at least '.$this->currencyFormat($value); |
132
|
|
|
break; |
133
|
|
View Code Duplication |
case "HasMaximumPayment": |
|
|
|
|
134
|
|
|
$having[] = ' RealPayments < '.intval($value); |
135
|
|
|
$humanWhere[] = ' Real Payment of no more than '.$this->currencyFormat($value); |
136
|
|
|
break; |
137
|
|
|
//this has been included for SearchableProductSalesReport |
138
|
|
|
case "Product": |
139
|
|
|
$where[] = " IF(ProductVariationsForVariations.Title IS NOT NULL, CONCAT(ProductSiteTreeForVariations.Title,' : ', ProductVariationsForVariations.Title), IF(SiteTreeForProducts.Title IS NOT NULL, SiteTreeForProducts.Title, OrderAttribute.ClassName)) LIKE '%".$value."%'"; |
140
|
|
|
$humanWhere[] = ' Product includes the phrase '.$value.'"'; |
141
|
|
|
break; |
142
|
|
|
|
143
|
|
|
default: |
144
|
|
|
break; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
return $this->saveProcessedForm($having, $where, $humanWhere); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
protected function saveProcessedForm($having, $where, $humanWhere) |
152
|
|
|
{ |
153
|
|
|
Session::set("SearchableOrderReport.having", implode(" AND ", $having)); |
154
|
|
|
Session::set("SearchableOrderReport.where", implode(" AND", $where)); |
155
|
|
|
Session::set("SearchableOrderReport.humanWhere", implode(", ", $humanWhere)); |
156
|
|
|
return "ok"; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function getReportField() |
160
|
|
|
{ |
161
|
|
|
$report = parent::getReportField(); |
162
|
|
|
$report->setCustomCsvQuery($this->getExportQuery()); |
163
|
|
|
return $report; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function getCustomQuery() |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
$bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`"; |
169
|
|
|
//buildSQL($filter = "", $sort = "", $limit = "", $join = "", $restrictClasses = true, $having = "") |
|
|
|
|
170
|
|
|
$where = Session::get("SearchableOrderReport.where"); |
171
|
|
|
if (trim($where)) { |
172
|
|
|
$where = " ( $where ) AND "; |
173
|
|
|
} |
174
|
|
|
$where .= "({$bt}Payment{$bt}.{$bt}Status{$bt} = 'Success' OR {$bt}Payment{$bt}.{$bt}Status{$bt} = 'Pending' OR {$bt}Payment{$bt}.{$bt}Status{$bt} IS NULL)"; |
175
|
|
|
$query = singleton('Order')->buildSQL( |
176
|
|
|
$where, |
177
|
|
|
$sort = "{$bt}Order{$bt}.{$bt}Created{$bt} DESC", |
178
|
|
|
$limit = "", |
179
|
|
|
$join = " |
180
|
|
|
INNER JOIN {$bt}Member{$bt} ON {$bt}Member{$bt}.{$bt}ID{$bt} = {$bt}Order{$bt}.{$bt}MemberID{$bt} |
181
|
|
|
LEFT JOIN Payment ON {$bt}Payment{$bt}.{$bt}OrderID{$bt} = {$bt}Order{$bt}.{$bt}ID{$bt} |
182
|
|
|
" |
183
|
|
|
); |
184
|
|
|
$query->select[] = "SUM({$bt}Payment{$bt}.{$bt}Amount{$bt}) RealPayments"; |
185
|
|
|
if ($having = Session::get("SearchableOrderReport.having")) { |
186
|
|
|
$query->having($having); |
187
|
|
|
} |
188
|
|
|
$query->groupby("{$bt}Order{$bt}.{$bt}ID{$bt}"); |
189
|
|
|
return $query; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function getExportFields() |
193
|
|
|
{ |
194
|
|
|
return array( |
195
|
|
|
"OrderSummary" => "Order Details", |
196
|
|
|
"RealPayments" => "Payments", |
197
|
|
|
"Payment.Message" => "Reference", |
198
|
|
|
"Member.FirstName" => "Customer first name", |
199
|
|
|
"Member.Surname" => "Customer last name", |
200
|
|
|
"Member.HomePhone" => "Customer home phone", |
201
|
|
|
"Member.MobilePhone" => "Customer mobile phone", |
202
|
|
|
"Member.Email" => "Customer phone", |
203
|
|
|
"Member.Address" => "Customer address 1", |
204
|
|
|
"Member.AddressLine2" => "Customer address 2", |
205
|
|
|
"Member.City" => "Customer City", |
206
|
|
|
); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function getExportQuery() |
|
|
|
|
210
|
|
|
{ |
211
|
|
|
$bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`"; |
212
|
|
|
//buildSQL($filter = "", $sort = "", $limit = "", $join = "", $restrictClasses = true, $having = "") |
|
|
|
|
213
|
|
|
$where = Session::get("SearchableOrderReport.where"); |
214
|
|
|
if (trim($where)) { |
215
|
|
|
$where = " ( $where ) AND "; |
216
|
|
|
} |
217
|
|
|
$where .= "({$bt}Payment{$bt}.{$bt}Status{$bt} = 'Success' OR {$bt}Payment{$bt}.{$bt}Status{$bt} = 'Pending' OR {$bt}Payment{$bt}.{$bt}Status{$bt} IS NULL)"; |
218
|
|
|
$query = singleton('Order')->buildSQL( |
219
|
|
|
$where, |
220
|
|
|
$sort = "{$bt}Order{$bt}.{$bt}Created{$bt} DESC", |
221
|
|
|
$limit = "", |
222
|
|
|
$join = " |
223
|
|
|
INNER JOIN {$bt}Member{$bt} ON {$bt}Member{$bt}.{$bt}ID{$bt} = {$bt}Order{$bt}.{$bt}MemberID{$bt} |
224
|
|
|
LEFT JOIN Payment ON {$bt}Payment{$bt}.{$bt}OrderID{$bt} = {$bt}Order{$bt}.{$bt}ID{$bt} |
225
|
|
|
"// |
226
|
|
|
); |
227
|
|
|
$fieldArray = $this->getExportFields(); |
228
|
|
View Code Duplication |
if (is_array($fieldArray)) { |
|
|
|
|
229
|
|
|
if (count($fieldArray)) { |
230
|
|
|
foreach ($fieldArray as $key => $field) { |
231
|
|
|
$query->select[] = $key; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
foreach ($query->select as $key=>$value) { |
236
|
|
|
if ($value == "RealPayments") { |
237
|
|
|
$query->select[$key] = "SUM(IF(Payment.Status = 'Success',{$bt}Payment{$bt}.{$bt}Amount{$bt}, 0)) RealPayments"; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
View Code Duplication |
foreach ($query->select as $key=>$value) { |
|
|
|
|
241
|
|
|
if ($value == "OrderSummary") { |
242
|
|
|
$query->select[$key] = "CONCAT({$bt}Order{$bt}.{$bt}ID{$bt}, ' :: ', {$bt}Order{$bt}.{$bt}Created{$bt}, ' :: ', {$bt}Order{$bt}.{$bt}Status{$bt}) AS OrderSummary"; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
$query->groupby("{$bt}Order{$bt}.{$bt}ID{$bt}"); |
246
|
|
|
if ($having = Session::get("SearchableOrderReport.having")) { |
247
|
|
|
$query->having($having); |
248
|
|
|
} |
249
|
|
|
return $query; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
protected function currencyFormat($v) |
|
|
|
|
253
|
|
|
{ |
254
|
|
|
$c = new Currency("currency"); |
|
|
|
|
255
|
|
|
$c->setValue($v); |
256
|
|
|
return $c->Nice(); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.