|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This class provides a bunch of Meta Objects |
|
4
|
|
|
* that do not interact with the object at hand, but rather with the datalist as a whole. |
|
5
|
|
|
* |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
class OrderProcessQueue extends DataObject |
|
|
|
|
|
|
9
|
|
|
{ |
|
10
|
|
|
private static $db = array( |
|
|
|
|
|
|
11
|
|
|
'DeferTimeInSeconds' => 'Int', |
|
12
|
|
|
'InProcess' => 'Boolean' |
|
13
|
|
|
); |
|
14
|
|
|
|
|
15
|
|
|
private static $has_one = array( |
|
|
|
|
|
|
16
|
|
|
'Order' => 'Order' |
|
17
|
|
|
); |
|
18
|
|
|
|
|
19
|
|
|
private static $indexes = array( |
|
|
|
|
|
|
20
|
|
|
'OrderID' => array( |
|
21
|
|
|
'type' => 'unique', |
|
22
|
|
|
'value' => '"OrderID"' |
|
23
|
|
|
), |
|
24
|
|
|
'Created' => true, |
|
25
|
|
|
'DeferTimeInSeconds' => true |
|
26
|
|
|
); |
|
27
|
|
|
|
|
28
|
|
|
private static $casting = array( |
|
|
|
|
|
|
29
|
|
|
'ToBeProcessedAt' => 'SS_Datetime' |
|
30
|
|
|
); |
|
31
|
|
|
|
|
32
|
|
|
private static $default_sort = array( |
|
|
|
|
|
|
33
|
|
|
'Created' => 'DESC' |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* standard SS variable. |
|
38
|
|
|
* |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
private static $summary_fields = array( |
|
|
|
|
|
|
42
|
|
|
'Order.Title' => 'Order', |
|
43
|
|
|
'Order.Status.Title' => 'Current Step', |
|
44
|
|
|
'ToBeProcessedAt.Nice' => 'To be processed at', |
|
45
|
|
|
'ToBeProcessedAt.Ago' => 'That is ...', |
|
46
|
|
|
'InProcess.Nice' => 'Currently Running' |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* standard SS variable. |
|
51
|
|
|
* |
|
52
|
|
|
* @var array |
|
53
|
|
|
*/ |
|
54
|
|
|
private static $searchable_fields = array( |
|
|
|
|
|
|
55
|
|
|
'OrderID' => array( |
|
56
|
|
|
'field' => 'NumericField', |
|
57
|
|
|
'title' => 'Order Number', |
|
58
|
|
|
) |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Standard SS method. |
|
64
|
|
|
* |
|
65
|
|
|
* @param Member $member |
|
|
|
|
|
|
66
|
|
|
* |
|
67
|
|
|
* @return bool |
|
68
|
|
|
*/ |
|
69
|
|
|
public function canCreate($member = null) |
|
70
|
|
|
{ |
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Standard SS method. |
|
76
|
|
|
* |
|
77
|
|
|
* @param Member $member |
|
|
|
|
|
|
78
|
|
|
* |
|
79
|
|
|
* @return bool |
|
|
|
|
|
|
80
|
|
|
*/ |
|
81
|
|
|
public function canView($member = null) |
|
82
|
|
|
{ |
|
83
|
|
|
if (! $member) { |
|
84
|
|
|
$member = Member::currentUser(); |
|
85
|
|
|
} |
|
86
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
|
|
|
|
|
|
87
|
|
|
if ($extended !== null) { |
|
88
|
|
|
return $extended; |
|
89
|
|
|
} |
|
90
|
|
|
if (Permission::checkMember($member, Config::inst()->get('EcommerceRole', 'admin_permission_code'))) { |
|
91
|
|
|
return true; |
|
92
|
|
|
} |
|
93
|
|
|
//is the member is a shop assistant they can always view it |
|
94
|
|
|
if (EcommerceRole::current_member_is_shop_assistant($member)) { |
|
95
|
|
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return parent::canView($member); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Standard SS method. |
|
103
|
|
|
* |
|
104
|
|
|
* @param Member $member |
|
|
|
|
|
|
105
|
|
|
* |
|
106
|
|
|
* @return bool |
|
107
|
|
|
*/ |
|
108
|
|
|
public function canEdit($member = null) |
|
109
|
|
|
{ |
|
110
|
|
|
return false; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Standard SS method |
|
115
|
|
|
* logs can never be deleted... |
|
116
|
|
|
* |
|
117
|
|
|
* @param Member $member |
|
|
|
|
|
|
118
|
|
|
* |
|
119
|
|
|
* @return bool |
|
120
|
|
|
*/ |
|
121
|
|
|
public function canDelete($member = null) |
|
122
|
|
|
{ |
|
123
|
|
|
return false; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* standard SS variable. |
|
128
|
|
|
* |
|
129
|
|
|
* @var string |
|
130
|
|
|
*/ |
|
131
|
|
|
private static $singular_name = 'Order To Be Processed'; |
|
|
|
|
|
|
132
|
|
|
public function i18n_singular_name() |
|
133
|
|
|
{ |
|
134
|
|
|
return _t('OrderProcessQueue.SINGULAR_NAME', 'Order In Queue'); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* standard SS variable. |
|
139
|
|
|
* |
|
140
|
|
|
* @var string |
|
141
|
|
|
*/ |
|
142
|
|
|
private static $plural_name = 'Orders to be Processed'; |
|
|
|
|
|
|
143
|
|
|
public function i18n_plural_name() |
|
144
|
|
|
{ |
|
145
|
|
|
return _t('OrderProcessQueue.PLURAL_NAME', 'Orders In Queue'); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* META METHOD: Add an order to the job list. |
|
151
|
|
|
* If the order already exists, it will update the seconds and the creation time. |
|
152
|
|
|
* |
|
153
|
|
|
* @param Order $order [description] |
|
154
|
|
|
* @param Int $deferInSeconds [description] |
|
|
|
|
|
|
155
|
|
|
*/ |
|
156
|
|
|
public function AddOrderToQueue($order, $deferTimeInSeconds) |
|
157
|
|
|
{ |
|
158
|
|
|
$filter = array('OrderID' => $order->ID); |
|
159
|
|
|
$existingEntry = OrderProcessQueue::get()->filter($filter)->first(); |
|
160
|
|
|
$filter['Created'] = SS_Datetime::now()->Rfc2822(); |
|
161
|
|
|
$filter['DeferTimeInSeconds'] = $deferTimeInSeconds; |
|
162
|
|
|
if (! $existingEntry) { |
|
163
|
|
|
$existingEntry = OrderProcessQueue::create($filter); |
|
164
|
|
|
} else { |
|
165
|
|
|
foreach ($filter as $field => $value) { |
|
166
|
|
|
$existingEntry->$field = $value; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
$existingEntry->write(); |
|
170
|
|
|
|
|
171
|
|
|
return $existingEntry; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* processes the order ... |
|
176
|
|
|
* |
|
177
|
|
|
* @param Order $order |
|
178
|
|
|
*/ |
|
179
|
|
|
public function process($order) |
|
180
|
|
|
{ |
|
181
|
|
|
$queueObjectSingleton = Injector::inst()->get('OrderProcessQueue'); |
|
182
|
|
|
$myQueueObject = $queueObjectSingleton->getQueueObject($order); |
|
183
|
|
|
if ($myQueueObject && $myQueueObject->isReadyToGo()) { |
|
184
|
|
|
$myQueueObject->InProcess = true; |
|
185
|
|
|
$myQueueObject->write(); |
|
186
|
|
|
$order->tryToFinaliseOrder( |
|
187
|
|
|
$tryAgain = false, |
|
188
|
|
|
$fromOrderQueue = true |
|
189
|
|
|
); |
|
190
|
|
|
$myQueueObject->delete(); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* META METHOD: returns the queue object if it exists |
|
196
|
|
|
* |
|
197
|
|
|
* @param Order $order |
|
198
|
|
|
* |
|
199
|
|
|
* @return null | OrderProcessQueue |
|
|
|
|
|
|
200
|
|
|
*/ |
|
201
|
|
|
public function getQueueObject($order) |
|
202
|
|
|
{ |
|
203
|
|
|
$filter = array('OrderID' => $order->ID); |
|
204
|
|
|
|
|
205
|
|
|
return OrderProcessQueue::get()->filter($filter)->first(); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* META METHOD: Once you are done, you can remove the item like this ... |
|
210
|
|
|
* |
|
211
|
|
|
* @param Order $order |
|
212
|
|
|
*/ |
|
213
|
|
|
public function removeOrderFromQueue($order) |
|
214
|
|
|
{ |
|
215
|
|
|
$filter = array('OrderID' => $order->ID); |
|
216
|
|
|
$existingEntries = OrderProcessQueue::get()->filter($filter); |
|
217
|
|
|
$existingEntries->removeAll(); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* META METHOD: returns a list of orders to be processed |
|
222
|
|
|
* @param int $id force this Order to be processed |
|
223
|
|
|
* @param int $limit total number of orders that can be retrieved at any one time |
|
224
|
|
|
* @return DataList (of orders) |
|
225
|
|
|
*/ |
|
226
|
|
|
public function OrdersToBeProcessed($id = 0, $limit = 9999) |
|
227
|
|
|
{ |
|
228
|
|
|
$sql = ' |
|
229
|
|
|
SELECT "OrderID" |
|
230
|
|
|
FROM "OrderProcessQueue" |
|
231
|
|
|
WHERE |
|
232
|
|
|
"InProcess" = 0 |
|
233
|
|
|
AND |
|
234
|
|
|
(UNIX_TIMESTAMP("Created") + "DeferTimeInSeconds") < '.time().' |
|
235
|
|
|
ORDER BY "Created" DESC |
|
236
|
|
|
LIMIT '.$limit.'; |
|
237
|
|
|
'; |
|
238
|
|
|
$rows = DB::query($sql); |
|
239
|
|
|
$orderIDs = array($id => $id); |
|
240
|
|
|
foreach ($rows as $row) { |
|
241
|
|
|
$orderIDs[$row['OrderID']] = $row['OrderID']; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
return Order::get() |
|
245
|
|
|
->filter(array('ID' => $orderIDs)); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* non-database method of working out if an Order is ready to go. |
|
250
|
|
|
* |
|
251
|
|
|
* @return bool |
|
252
|
|
|
*/ |
|
253
|
|
|
public function isReadyToGo() |
|
254
|
|
|
{ |
|
255
|
|
|
return (strtotime($this->Created) + $this->DeferTimeInSeconds) < time(); |
|
|
|
|
|
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* |
|
260
|
|
|
* casted variable |
|
261
|
|
|
* @return SS_DateTime |
|
262
|
|
|
*/ |
|
263
|
|
|
public function ToBeProcessedAt() |
|
264
|
|
|
{ |
|
265
|
|
|
return $this->getToBeProcessedAt(); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* |
|
270
|
|
|
* casted variable |
|
271
|
|
|
* @return SS_DateTime |
|
272
|
|
|
*/ |
|
273
|
|
|
public function getToBeProcessedAt() |
|
274
|
|
|
{ |
|
275
|
|
|
return DBField::create_field('SS_Datetime', (strtotime($this->Created) + $this->DeferTimeInSeconds)); |
|
|
|
|
|
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* CMS Fields |
|
281
|
|
|
* @return FieldList |
|
282
|
|
|
*/ |
|
283
|
|
|
public function getCMSFields() |
|
284
|
|
|
{ |
|
285
|
|
|
$fields = parent::getCMSFields(); |
|
286
|
|
|
if($this->exists()) { |
|
287
|
|
|
$fields->addFieldToTab( |
|
288
|
|
|
'Root.Main', |
|
289
|
|
|
ReadonlyField::create( |
|
290
|
|
|
'ToBeProcessedAtCompilations', |
|
291
|
|
|
_t('OrderProcessQueue.TO_BE_PROCESSED', 'To Be Processed'), |
|
292
|
|
|
$this->ToBeProcessedAt->Nice() . ' - ' . $this->getToBeProcessedAt()->Ago() |
|
|
|
|
|
|
293
|
|
|
), |
|
294
|
|
|
'InProcess' |
|
295
|
|
|
); |
|
296
|
|
|
$fields->addFieldToTab( |
|
297
|
|
|
'Root.Main', |
|
298
|
|
|
LiteralField::create( |
|
299
|
|
|
'processQueueNow', |
|
300
|
|
|
'<h2> |
|
301
|
|
|
<a href="/dev/tasks/EcommerceTaskProcessOrderQueue/?id='.$this->OrderID.'" target="_blank">'. |
|
|
|
|
|
|
302
|
|
|
_t('OrderProcessQueue.PROCESS', 'Process now'). |
|
303
|
|
|
'</a> |
|
304
|
|
|
</h2>' |
|
305
|
|
|
) |
|
306
|
|
|
); |
|
307
|
|
|
} |
|
308
|
|
|
return $fields; |
|
309
|
|
|
} |
|
310
|
|
|
} |
|
311
|
|
|
|
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.