1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Nicolaas [at] sunnysideup.co.nz |
4
|
|
|
* @package ecommercextras |
5
|
|
|
*/ |
6
|
|
|
class PaymentsReport extends SS_Report |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
protected $title = 'All Payments'; |
9
|
|
|
|
10
|
|
|
protected static $payment_array = array(); |
11
|
|
|
|
12
|
|
|
protected $description = 'Show all payments'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Return a {@link ComplexTableField} that shows |
16
|
|
|
* all Order instances that are not printed. That is, |
17
|
|
|
* Order instances with the property "Printed" value |
18
|
|
|
* set to "0". |
19
|
|
|
* |
20
|
|
|
* @return ComplexTableField |
21
|
|
|
*/ |
22
|
|
|
public function getReportField() |
23
|
|
|
{ |
24
|
|
|
// Get the fields used for the table columns |
25
|
|
|
$fields = array( |
26
|
|
|
'Created' => 'Time', |
27
|
|
|
'Amount' => 'Amount', |
28
|
|
|
'Currency' => 'Currency', |
29
|
|
|
'Message' => 'Note', |
30
|
|
|
'IP' => 'Purchaser IP', |
31
|
|
|
'ProxyIP' => 'Purchaser Proxy' |
32
|
|
|
); |
33
|
|
|
$fields['ChangeStatus'] = ''; |
34
|
|
|
|
35
|
|
|
$table = new TableListField( |
36
|
|
|
'Payments', |
37
|
|
|
'Payment', |
38
|
|
|
$fields |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
// Customise the SQL query for Order, because we don't want it querying |
42
|
|
|
// all the fields. Invoice and Printed are dummy fields that just have some |
43
|
|
|
// text in them, which would be automatically queried if we didn't specify |
44
|
|
|
// a custom query. |
45
|
|
|
|
46
|
|
|
$table->setCustomQuery($this->getCustomQuery()); |
47
|
|
|
|
48
|
|
|
// Set the links to the Invoice and Print fields allowing a user to view |
49
|
|
|
// another template for viewing an Order instance |
50
|
|
|
$table->setFieldFormatting(array( |
51
|
|
|
'ChangeStatus' => '<a href=\"#\" class=\"statusDropdownChange\" rel=\"$ID\">$Status</a><span class=\"outcome\"></span>' |
52
|
|
|
)); |
53
|
|
|
|
54
|
|
|
$table->setFieldCasting(array( |
55
|
|
|
'Amount' => 'Currency->Nice' |
56
|
|
|
)); |
57
|
|
|
|
58
|
|
|
$table->setPermissions(array( |
59
|
|
|
'edit', |
60
|
|
|
'show', |
61
|
|
|
'export', |
62
|
|
|
)); |
63
|
|
|
$table->setPageSize(250); |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
$table->setFieldListCsv($this->getExportFields()); |
67
|
|
|
|
68
|
|
|
$table->setCustomCsvQuery($this->getExportQuery()); |
69
|
|
|
|
70
|
|
|
//$tableField->removeCsvHeader(); |
|
|
|
|
71
|
|
|
|
72
|
|
|
return $table; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
public function getCustomQuery() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
if ("PaymentsReport" == $this->class) { |
|
|
|
|
78
|
|
|
//user_error('Please implement getCustomQuery() on ' . $this->class, E_USER_ERROR); |
|
|
|
|
79
|
|
|
} else { |
80
|
|
|
//buildSQL($filter = "", $sort = "", $limit = "", $join = "", $restrictClasses = true, $having = "") |
|
|
|
|
81
|
|
|
$query = singleton('Payment')->buildSQL('', 'Payment.Created DESC'); |
82
|
|
|
$query->groupby[] = 'Payment.ID'; |
83
|
|
|
return $query; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getExportFields() |
88
|
|
|
{ |
89
|
|
|
if ("PaymentsReport" == $this->class) { |
|
|
|
|
90
|
|
|
//user_error('Please implement getExportFields() on ' . $this->class, E_USER_ERROR); |
|
|
|
|
91
|
|
|
} else { |
92
|
|
|
return array("Order.ID" => "Order ID", "Order.Total" => "Order Total"); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
View Code Duplication |
public function getExportQuery() |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
if ("PaymentsReport" == $this->class) { |
|
|
|
|
99
|
|
|
//user_error('Please implement getExportFields() on ' . $this->class, E_USER_ERROR); |
|
|
|
|
100
|
|
|
} else { |
101
|
|
|
$query = singleton('Payment')->buildSQL('', 'Payment.Created DESC'); |
102
|
|
|
$query->groupby[] = 'Payment.ID'; |
103
|
|
|
return $query; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
View Code Duplication |
protected function statistic($type) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
if (!count(self::$payment_array)) { |
110
|
|
|
$data = $this->getCustomQuery()->execute(); |
111
|
|
|
if ($data) { |
112
|
|
|
$array = array(); |
|
|
|
|
113
|
|
|
foreach ($data as $row) { |
114
|
|
|
if ($row["Amount"] && $row["Status"] == "Success") { |
115
|
|
|
self::$payment_array[] = $row["Amount"]; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
if (count(self::$payment_array)) { |
121
|
|
|
switch ($type) { |
122
|
|
|
case "count": |
123
|
|
|
return count(self::$payment_array); |
124
|
|
|
break; |
|
|
|
|
125
|
|
|
case "sum": |
126
|
|
|
return array_sum(self::$payment_array); |
127
|
|
|
break; |
|
|
|
|
128
|
|
|
case "avg": |
129
|
|
|
return array_sum(self::$payment_array) / count(self::$payment_array); |
130
|
|
|
break; |
|
|
|
|
131
|
|
|
case "min": |
132
|
|
|
asort(self::$payment_array); |
133
|
|
|
foreach (self::$payment_array as $item) { |
134
|
|
|
return $item; |
135
|
|
|
} |
136
|
|
|
break; |
137
|
|
|
case "max": |
138
|
|
|
arsort(self::$payment_array); |
139
|
|
|
foreach (self::$payment_array as $item) { |
140
|
|
|
return $item; |
141
|
|
|
} |
142
|
|
|
break; |
143
|
|
|
default: |
144
|
|
|
user_error("Wrong statistic type speficied in PaymentsReport::statistic", E_USER_ERROR); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
return -1; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function processform() |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
if ("PaymentsReport" == $this->class) { |
|
|
|
|
153
|
|
|
//user_error('Please implement processform() on ' . $this->class, E_USER_ERROR); |
|
|
|
|
154
|
|
|
} else { |
155
|
|
|
die($_REQUEST); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
protected function currencyFormat($v) |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
$c = new Currency("currency"); |
|
|
|
|
163
|
|
|
$c->setValue($v); |
164
|
|
|
return $c->Nice(); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
class PaymentsReport_Handler extends Controller |
|
|
|
|
169
|
|
|
{ |
170
|
|
|
public function processform() |
|
|
|
|
171
|
|
|
{ |
172
|
|
|
$ClassName = Director::URLParam("ID"); |
|
|
|
|
173
|
|
|
$object = new $ClassName; |
174
|
|
|
return $object->processform(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
|
178
|
|
View Code Duplication |
public function setstatus() |
|
|
|
|
179
|
|
|
{ |
180
|
|
|
$id = $this->urlParams['ID']; |
181
|
|
|
if (!is_numeric($id)) { |
182
|
|
|
return "could not update payment status"; |
183
|
|
|
} |
184
|
|
|
$payment = DataObject::get_by_id('Payment', $id); |
185
|
|
|
if ($payment) { |
186
|
|
|
$oldStatus = $payment->Status; |
187
|
|
|
$newStatus = $this->urlParams['OtherID']; |
188
|
|
|
if ($oldStatus != $newStatus) { |
189
|
|
|
$payment->Status = $newStatus; |
190
|
|
|
$payment->write(); |
191
|
|
|
$orderLog = new OrderStatusLog(); |
192
|
|
|
$orderLog->OrderID = $orderLog->OrderID; |
193
|
|
|
$orderLog->Status = "Payment status changed from ".$oldStatus." to ".$newStatus."."; |
194
|
|
|
$orderLog->Note = "Payment changed from ".$oldStatus." to ".$newStatus."."; |
195
|
|
|
$orderLog->write(); |
196
|
|
|
} else { |
197
|
|
|
return "no change"; |
198
|
|
|
} |
199
|
|
|
} else { |
200
|
|
|
return "payment not found"; |
201
|
|
|
} |
202
|
|
|
return "updated to ".$newStatus; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
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.