1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @package ecommercextras |
5
|
|
|
* @author nicolaas[at]sunnysideup.co.nz |
6
|
|
|
*/ |
7
|
|
|
class SearchablePaymentReport extends PaymentsReport |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
protected $title = 'Searchable Payments'; |
10
|
|
|
|
11
|
|
|
protected $description = 'Search all payments'; |
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 getCMSFields() |
41
|
|
|
{ |
42
|
|
|
$fields = parent::getCMSFields(); |
43
|
|
|
$stats[] = "Count: ".$this->statistic("count"); |
|
|
|
|
44
|
|
|
$stats[] = "Sum: ".$this->currencyFormat($this->statistic("sum")); |
45
|
|
|
$stats[] = "Avg: ".$this->currencyFormat($this->statistic("avg")); |
46
|
|
|
$stats[] = "Min: ".$this->currencyFormat($this->statistic("min")); |
47
|
|
|
$stats[] = "Max: ".$this->currencyFormat($this->statistic("max")); |
48
|
|
|
if ($this->statistic("count") > 3) { |
49
|
|
|
$fields->addFieldToTab("Root.Report", new LiteralField("stats", '<h2>Payment Statistics</h2><ul><li>'.implode('</li><li>', $stats).'</li></ul>')); |
50
|
|
|
} |
51
|
|
View Code Duplication |
if ($humanWhere = Session::get("SearchablePaymentReport.humanWhere")) { |
|
|
|
|
52
|
|
|
$fields->addFieldToTab("Root.Report", new LiteralField("humanWhere", "<p>Current Search: ".$humanWhere."</p>"), "ReportDescription"); |
53
|
|
|
$fields->removeByName("ReportDescription"); |
54
|
|
|
$fields->addFieldToTab("Root.Search", new FormAction('clearSearch', 'Clear Search')); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
$paymentStatusList = singleton('Payment')->dbObject('Status')->enumValues(); |
57
|
|
|
$dropDownValues = array(); |
58
|
|
|
foreach ($paymentStatusList as $paymentStatus) { |
59
|
|
|
$dropDownValues[$paymentStatus] = $paymentStatus; |
60
|
|
|
} |
61
|
|
|
$fields->addFieldToTab("Root.Search", new CheckboxSetField("Status", "Payment Status", $dropDownValues)); |
|
|
|
|
62
|
|
|
$fields->addFieldToTab("Root.Search", new NumericField("OrderID", "Order ID")); |
63
|
|
|
$fields->addFieldToTab("Root.Search", new DateField("From", "From...")); |
64
|
|
|
$fields->addFieldToTab("Root.Search", new DropdownTimeField("FromTime", "Start time...", self::get_default_from_time_as_full_date_time(), "H:i a")); |
65
|
|
|
$fields->addFieldToTab("Root.Search", new DateField("Until", "Until...")); |
66
|
|
|
$fields->addFieldToTab("Root.Search", new DropdownTimeField("UntilTime", "End time...", self::get_default_until_time_as_full_date_time(), "H:i a")); |
67
|
|
|
$fields->addFieldToTab("Root.Search", new NumericField("HasMinimumPayment", "Amout at least...")); |
68
|
|
|
$fields->addFieldToTab("Root.Search", new NumericField("HasMaximumPayment", "Amount no more than ...")); |
69
|
|
|
$fields->addFieldToTab("Root.Search", new FormAction('doSearch', 'Apply Search')); |
|
|
|
|
70
|
|
|
|
71
|
|
|
return $fields; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function processform() |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`"; |
|
|
|
|
77
|
|
|
$where = array(); |
78
|
|
|
$having = array(); |
79
|
|
|
$humanWhere = array(); |
80
|
|
|
foreach ($_REQUEST as $key => $value) { |
81
|
|
|
$value = Convert::raw2sql($value); |
82
|
|
|
if ($value) { |
83
|
|
|
switch ($key) { |
84
|
|
|
case "OrderID": |
85
|
|
|
$where[] = ' {$bt}Order{$bt}.{$bt}ID{$bt} = '.intval($value); |
86
|
|
|
$humanWhere[] = ' OrderID equals '.intval($value); |
87
|
|
|
break; |
88
|
|
View Code Duplication |
case "From": |
|
|
|
|
89
|
|
|
$d = new Date("date"); |
90
|
|
|
$d->setValue($value); |
91
|
|
|
$t = new Time("time"); |
92
|
|
|
$cleanTime = trim(preg_replace('/([ap]m)/', "", Convert::raw2sql($_REQUEST["FromTime"]))); |
93
|
|
|
$t->setValue($cleanTime); // |
94
|
|
|
$exactTime = strtotime($d->format("Y-m-d")." ".$t->Nice24()); |
95
|
|
|
$where[] = ' UNIX_TIMESTAMP({$bt}Payment{$bt}.{$bt}Created{$bt}) >= "'.$exactTime.'"'; |
96
|
|
|
$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) |
|
|
|
|
97
|
|
|
break; |
98
|
|
View Code Duplication |
case "Until": |
|
|
|
|
99
|
|
|
$d = new Date("date"); |
100
|
|
|
$d->setValue($value); |
101
|
|
|
$t = new Time("time"); |
102
|
|
|
$cleanTime = trim(preg_replace('/([ap]m)/', "", Convert::raw2sql($_REQUEST["FromTime"]))); |
103
|
|
|
$t->setValue($cleanTime); // |
104
|
|
|
$exactTime = strtotime($d->format("Y-m-d")." ".$t->Nice24()); |
105
|
|
|
$where[] = ' UNIX_TIMESTAMP({$bt}Payment{$bt}.{$bt}Created{$bt}) <= "'.$exactTime.'"'; |
106
|
|
|
$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) |
|
|
|
|
107
|
|
|
|
108
|
|
|
break; |
109
|
|
|
case "Status": |
110
|
|
|
$subWhere = array(); |
111
|
|
|
foreach ($value as $item) { |
112
|
|
|
$subWhere[] = ' {$bt}Payment{$bt}.{$bt}Status{$bt} = "'.$item.'"'; |
113
|
|
|
$humanWhere[] = ' Payment Status equals "'.$item.'"'; |
114
|
|
|
} |
115
|
|
|
if (count($subWhere)) { |
116
|
|
|
$where[] = implode(" OR ", $subWhere); |
117
|
|
|
} |
118
|
|
|
break; |
119
|
|
View Code Duplication |
case "HasMinimumPayment": |
|
|
|
|
120
|
|
|
$having[] = ' Amount > '.intval($value); |
121
|
|
|
$humanWhere[] = ' Payment of at least '.$this->currencyFormat($value); |
122
|
|
|
break; |
123
|
|
View Code Duplication |
case "HasMaximumPayment": |
|
|
|
|
124
|
|
|
$having[] = ' Amount < '.intval($value); |
125
|
|
|
$humanWhere[] = ' Payment of no more than '.$this->currencyFormat($value); |
126
|
|
|
break; |
127
|
|
|
default: |
128
|
|
|
break; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
Session::set("SearchablePaymentReport.having", implode(" AND ", $having)); |
133
|
|
|
Session::set("SearchablePaymentReport.where", implode(" AND", $where)); |
134
|
|
|
Session::set("SearchablePaymentReport.humanWhere", implode(", ", $humanWhere)); |
135
|
|
|
return "ok"; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function getCustomQuery() |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
$bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`"; |
141
|
|
|
//buildSQL($filter = "", $sort = "", $limit = "", $join = "", $restrictClasses = true, $having = "") |
|
|
|
|
142
|
|
|
$where = Session::get("SearchablePaymentReport.where"); |
143
|
|
|
if (trim($where)) { |
144
|
|
|
$where = " ( $where ) AND "; |
145
|
|
|
} |
146
|
|
|
$where .= " 1 = 1"; |
147
|
|
|
$query = singleton('Payment')->buildSQL( |
148
|
|
|
$where, |
149
|
|
|
$sort = "{$bt}Payment{$bt}.{$bt}Created{$bt} DESC", |
150
|
|
|
$limit = "", |
151
|
|
|
$join = " INNER JOIN {$bt}Order{$bt} on {$bt}Order{$bt}.{$bt}ID{$bt} = {$bt}Payment{$bt}.{$bt}OrderID{$bt}" |
152
|
|
|
); |
153
|
|
|
if ($having = Session::get("SearchablePaymentReport.having")) { |
154
|
|
|
$query->having($having); |
155
|
|
|
} |
156
|
|
|
return $query; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
public function getReportField() |
161
|
|
|
{ |
162
|
|
|
$report = parent::getReportField(); |
163
|
|
|
$report->setCustomCsvQuery($this->getExportQuery()); |
164
|
|
|
return $report; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
public function getExportFields() |
169
|
|
|
{ |
170
|
|
|
return array( |
171
|
|
|
"OrderSummary" => "Order Details", |
172
|
|
|
'Amount' => 'Amount', |
173
|
|
|
'Currency' => 'Currency', |
174
|
|
|
'Message' => 'Message', |
175
|
|
|
'IP' => 'Varchar', |
176
|
|
|
'ProxyIP' => 'Varchar' |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function getExportQuery() |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
$bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`"; |
183
|
|
|
//buildSQL($filter = "", $sort = "", $limit = "", $join = "", $restrictClasses = true, $having = "") |
|
|
|
|
184
|
|
|
$where = Session::get("SearchablePaymentReport.where"); |
185
|
|
|
if (trim($where)) { |
186
|
|
|
$where = " ( $where ) AND "; |
187
|
|
|
} |
188
|
|
|
$query = singleton('Payment')->buildSQL( |
189
|
|
|
$where, |
190
|
|
|
$sort = "{$bt}Payment{$bt}.{$bt}Created{$bt} DESC", |
191
|
|
|
$limit = "", |
192
|
|
|
$join = " INNER JOIN {$bt}Order{$bt} on {$bt}Order{$bt}.{$bt}ID{$bt} = {$bt}Payment{$bt}.{$bt}OrderID{$bt}" |
193
|
|
|
); |
194
|
|
|
$fieldArray = $this->getExportFields(); |
195
|
|
View Code Duplication |
if (is_array($fieldArray)) { |
|
|
|
|
196
|
|
|
if (count($fieldArray)) { |
197
|
|
|
foreach ($fieldArray as $key => $field) { |
198
|
|
|
$query->select[] = $key; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
View Code Duplication |
foreach ($query->select as $key=>$value) { |
|
|
|
|
203
|
|
|
if ($value == "OrderSummary") { |
204
|
|
|
$query->select[$key] = "CONCAT({$bt}Order{$bt}.{$bt}ID{$bt}, ' :: ', {$bt}Order{$bt}.{$bt}Created{$bt}, ' :: ', {$bt}Order{$bt}.{$bt}Status{$bt}) AS OrderSummary"; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
if ($having = Session::get("SearchableOrderReport.having")) { |
208
|
|
|
$query->having($having); |
209
|
|
|
} |
210
|
|
|
return $query; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
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.