|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Dynamic\Foxy\Extension\Purchasable; |
|
6
|
|
|
use Dynamic\FoxyStripe\Foxy\Transaction; |
|
7
|
|
|
use SilverStripe\Forms\DateField; |
|
8
|
|
|
use SilverStripe\ORM\DataObject; |
|
9
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLVarchar; |
|
10
|
|
|
use SilverStripe\ORM\ValidationException; |
|
11
|
|
|
use SilverStripe\Security\Member; |
|
12
|
|
|
use SilverStripe\Security\Permission; |
|
13
|
|
|
use SilverStripe\Security\PermissionProvider; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class Order |
|
17
|
|
|
* @package Dynamic\Foxy\Model |
|
18
|
|
|
* |
|
19
|
|
|
* @property \SilverStripe\ORM\FieldType\DBInt StoreID |
|
20
|
|
|
* @property \SilverStripe\ORM\FieldType\DBInt OrderID |
|
21
|
|
|
* @property \SilverStripe\ORM\FieldType\DBVarchar Email |
|
22
|
|
|
* @property \SilverStripe\ORM\FieldType\DBDatetime TransactionDate |
|
23
|
|
|
* @property \SilverStripe\ORM\FieldType\DBCurrency ProductTotal |
|
24
|
|
|
* @property \SilverStripe\ORM\FieldType\DBCurrency TaxTotal |
|
25
|
|
|
* @property \SilverStripe\ORM\FieldType\DBCurrency ShippingTotal |
|
26
|
|
|
* @property \SilverStripe\ORM\FieldType\DBCurrency OrderTotal |
|
27
|
|
|
* @property \SilverStripe\ORM\FieldType\DBVarchar ReceiptURL |
|
28
|
|
|
* @property \SilverStripe\ORM\FieldType\DBVarchar OrderStatus |
|
29
|
|
|
* @property \SilverStripe\ORM\FieldType\DBText Response |
|
30
|
|
|
* |
|
31
|
|
|
* @property int MemberID |
|
32
|
|
|
* @method Member Member |
|
33
|
|
|
*/ |
|
34
|
|
|
class Order extends DataObject implements PermissionProvider |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
private static $db = [ |
|
|
|
|
|
|
40
|
|
|
'StoreID' => 'Int', |
|
41
|
|
|
'OrderID' => 'Int', |
|
42
|
|
|
'Email' => 'Varchar(255)', |
|
43
|
|
|
'TransactionDate' => 'DBDatetime', |
|
44
|
|
|
'ProductTotal' => 'Currency', |
|
45
|
|
|
'TaxTotal' => 'Currency', |
|
46
|
|
|
'ShippingTotal' => 'Currency', |
|
47
|
|
|
'OrderTotal' => 'Currency', |
|
48
|
|
|
'ReceiptURL' => 'Varchar(255)', |
|
49
|
|
|
'OrderStatus' => 'Varchar(255)', |
|
50
|
|
|
'Response' => 'Text', |
|
51
|
|
|
]; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var array |
|
55
|
|
|
*/ |
|
56
|
|
|
private static $has_one = [ |
|
|
|
|
|
|
57
|
|
|
'Member' => Member::class, |
|
58
|
|
|
]; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
private static $singular_name = 'Order'; |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var string |
|
67
|
|
|
*/ |
|
68
|
|
|
private static $plural_name = 'Orders'; |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var string |
|
72
|
|
|
*/ |
|
73
|
|
|
private static $description = 'Orders from FoxyCart Datafeed'; |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @var string |
|
77
|
|
|
*/ |
|
78
|
|
|
private static $default_sort = 'TransactionDate DESC, ID DESC'; |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @var array |
|
82
|
|
|
*/ |
|
83
|
|
|
private static $summary_fields = [ |
|
|
|
|
|
|
84
|
|
|
'OrderID', |
|
85
|
|
|
'TransactionDate.Nice', |
|
86
|
|
|
'Email', |
|
87
|
|
|
'ProductTotal.Nice', |
|
88
|
|
|
'ShippingTotal.Nice', |
|
89
|
|
|
'TaxTotal.Nice', |
|
90
|
|
|
'OrderTotal.Nice', |
|
91
|
|
|
'ReceiptLink', |
|
92
|
|
|
]; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @var array |
|
96
|
|
|
*/ |
|
97
|
|
|
private static $searchable_fields = [ |
|
|
|
|
|
|
98
|
|
|
'OrderID', |
|
99
|
|
|
'TransactionDate' => [ |
|
100
|
|
|
'field' => DateField::class, |
|
101
|
|
|
'filter' => 'PartialMatchFilter', |
|
102
|
|
|
], |
|
103
|
|
|
'Email', |
|
104
|
|
|
'OrderTotal' |
|
105
|
|
|
]; |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @var array |
|
109
|
|
|
*/ |
|
110
|
|
|
private static $casting = [ |
|
|
|
|
|
|
111
|
|
|
'ReceiptLink' => 'HTMLVarchar', |
|
112
|
|
|
]; |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @var array |
|
116
|
|
|
*/ |
|
117
|
|
|
private static $indexes = [ |
|
|
|
|
|
|
118
|
|
|
'OrderID' => true, // make unique |
|
119
|
|
|
]; |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @var string |
|
123
|
|
|
*/ |
|
124
|
|
|
private static $table_name = 'FoxyOrder'; |
|
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @var |
|
128
|
|
|
*/ |
|
129
|
|
|
private $transaction; |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return mixed |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function getTransaction() |
|
135
|
|
|
{ |
|
136
|
|
|
if (!$this->transaction) { |
|
137
|
|
|
$this->setTransaction(); |
|
138
|
|
|
} |
|
139
|
|
|
return $this->transaction; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return $this |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function setTransaction() |
|
146
|
|
|
{ |
|
147
|
|
|
if ($this->Response) { |
|
148
|
|
|
$this->transaction = Transaction::create($this->OrderID, urldecode($this->Response)); |
|
149
|
|
|
} else { |
|
150
|
|
|
$this->transaction = false; |
|
151
|
|
|
} |
|
152
|
|
|
return $this; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param bool $includerelations |
|
157
|
|
|
* |
|
158
|
|
|
* @return array|string |
|
159
|
|
|
*/ |
|
160
|
|
|
public function fieldLabels($includerelations = true) |
|
161
|
|
|
{ |
|
162
|
|
|
$labels = parent::fieldLabels(); |
|
163
|
|
|
$labels['StoreID'] = _t(__CLASS__ . '.StoreID', 'Store ID#'); |
|
164
|
|
|
$labels['OrderID'] = _t(__CLASS__ . '.OrderID', 'Order ID#'); |
|
165
|
|
|
$labels['TransactionDate'] = _t(__CLASS__ . '.TransactionDate', 'Date'); |
|
166
|
|
|
$labels['TransactionDate.NiceUS'] = _t(__CLASS__ . '.TransactionDate', 'Date'); |
|
167
|
|
|
$labels['Email'] = _t(__CLASS__ . '.Email', 'Email'); |
|
168
|
|
|
$labels['ProductTotal.Nice'] = _t(__CLASS__ . '.ProductTotal', 'Sub Total'); |
|
169
|
|
|
$labels['TaxTotal.Nice'] = _t(__CLASS__ . '.TaxTotal', 'Tax'); |
|
170
|
|
|
$labels['ShippingTotal.Nice'] = _t(__CLASS__ . '.ShippingTotal', 'Shipping'); |
|
171
|
|
|
$labels['OrderTotal'] = _t(__CLASS__ . '.OrderTotal', 'Total'); |
|
172
|
|
|
$labels['OrderTotal.Nice'] = _t(__CLASS__ . '.OrderTotal', 'Total'); |
|
173
|
|
|
$labels['ReceiptLink'] = _t(__CLASS__ . '.ReceiptLink', 'Invoice'); |
|
174
|
|
|
$labels['Details.ProductID'] = _t(__CLASS__ . '.Details.ProductID', 'Product'); |
|
175
|
|
|
return $labels; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @return mixed |
|
180
|
|
|
*/ |
|
181
|
|
|
public function ReceiptLink() |
|
182
|
|
|
{ |
|
183
|
|
|
return $this->getReceiptLink(); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @return mixed |
|
188
|
|
|
*/ |
|
189
|
|
|
public function getReceiptLink() |
|
190
|
|
|
{ |
|
191
|
|
|
$obj = DBHTMLVarchar::create(); |
|
192
|
|
|
$obj->setValue( |
|
193
|
|
|
'<a href="' . $this->ReceiptURL . '" target="_blank" class="cms-panel-link action external-link">view</a>' |
|
194
|
|
|
); |
|
195
|
|
|
return $obj; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
|
200
|
|
|
*/ |
|
201
|
|
|
protected function onBeforeWrite() |
|
202
|
|
|
{ |
|
203
|
|
|
parent::onBeforeWrite(); |
|
204
|
|
|
$this->parseOrder(); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @return bool |
|
209
|
|
|
* |
|
210
|
|
|
* @throws ValidationException |
|
211
|
|
|
*/ |
|
212
|
|
|
public function parseOrder() |
|
213
|
|
|
{ |
|
214
|
|
|
if ($this->getTransaction() && $this->getTransaction()->exists()) { |
|
215
|
|
|
$this->parseOrderInfo(); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
$this->extend('updateParseOrder', $this); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @param $response |
|
223
|
|
|
*/ |
|
224
|
|
|
public function parseOrderInfo() |
|
225
|
|
|
{ |
|
226
|
|
|
$transaction = $this->getTransaction()->getTransaction(); |
|
227
|
|
|
|
|
228
|
|
|
// Record transaction data from FoxyCart Datafeed: |
|
229
|
|
|
$this->StoreID = (int)$transaction->store_id; |
|
|
|
|
|
|
230
|
|
|
$this->Email = (string)$transaction->customer_email; |
|
|
|
|
|
|
231
|
|
|
$this->TransactionDate = (string)$transaction->transaction_date; |
|
|
|
|
|
|
232
|
|
|
$this->ProductTotal = (float)$transaction->product_total; |
|
|
|
|
|
|
233
|
|
|
$this->TaxTotal = (float)$transaction->tax_total; |
|
|
|
|
|
|
234
|
|
|
$this->ShippingTotal = (float)$transaction->shipping_total; |
|
|
|
|
|
|
235
|
|
|
$this->OrderTotal = (float)$transaction->order_total; |
|
|
|
|
|
|
236
|
|
|
$this->ReceiptURL = (string)$transaction->receipt_url; |
|
|
|
|
|
|
237
|
|
|
$this->OrderStatus = (string)$transaction->status; |
|
|
|
|
|
|
238
|
|
|
|
|
239
|
|
|
$this->extend('handleOrderInfo', $order, $response); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* @return array |
|
244
|
|
|
*/ |
|
245
|
|
|
public function providePermissions() |
|
246
|
|
|
{ |
|
247
|
|
|
return [ |
|
248
|
|
|
'MANAGE_FOXY_ORDERS' => [ |
|
249
|
|
|
'name' => _t( |
|
250
|
|
|
__CLASS__ . '.PERMISSION_MANAGE_ORDERS_DESCRIPTION', |
|
251
|
|
|
'Manage orders' |
|
252
|
|
|
), |
|
253
|
|
|
'category' => _t( |
|
254
|
|
|
Purchasable::class . '.PERMISSIONS_CATEGORY', |
|
255
|
|
|
'Foxy' |
|
256
|
|
|
), |
|
257
|
|
|
'help' => _t( |
|
258
|
|
|
__CLASS__ . '.PERMISSION_MANAGE_ORDERS_HELP', |
|
259
|
|
|
'Manage orders and view recipts' |
|
260
|
|
|
), |
|
261
|
|
|
'sort' => 400, |
|
262
|
|
|
], |
|
263
|
|
|
]; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* @param bool $member |
|
268
|
|
|
* |
|
269
|
|
|
* @return bool|int |
|
270
|
|
|
*/ |
|
271
|
|
|
public function canView($member = null) |
|
272
|
|
|
{ |
|
273
|
|
|
return Permission::checkMember($member, 'MANAGE_FOXY_ORDERS'); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* @param null $member |
|
|
|
|
|
|
278
|
|
|
* |
|
279
|
|
|
* @return bool |
|
280
|
|
|
*/ |
|
281
|
|
|
public function canEdit($member = null) |
|
282
|
|
|
{ |
|
283
|
|
|
return false; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* @param null $member |
|
|
|
|
|
|
288
|
|
|
* |
|
289
|
|
|
* @return bool |
|
290
|
|
|
*/ |
|
291
|
|
|
public function canDelete($member = null) |
|
292
|
|
|
{ |
|
293
|
|
|
return false; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* @param null $member |
|
|
|
|
|
|
298
|
|
|
* @param array $context |
|
299
|
|
|
* |
|
300
|
|
|
* @return bool |
|
301
|
|
|
*/ |
|
302
|
|
|
public function canCreate($member = null, $context = []) |
|
303
|
|
|
{ |
|
304
|
|
|
return false; |
|
305
|
|
|
} |
|
306
|
|
|
} |
|
307
|
|
|
|