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