|
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
|
|
|
'OrderStep' => 'OrderStep' |
|
18
|
|
|
); |
|
19
|
|
|
|
|
20
|
|
|
private static $indexes = array( |
|
|
|
|
|
|
21
|
|
|
'OrderID' => array( |
|
22
|
|
|
'type' => 'unique', |
|
23
|
|
|
'value' => '"OrderID"' |
|
24
|
|
|
), |
|
25
|
|
|
'Created' => true, |
|
26
|
|
|
'DeferTimeInSeconds' => true |
|
27
|
|
|
); |
|
28
|
|
|
|
|
29
|
|
|
private static $casting = array( |
|
|
|
|
|
|
30
|
|
|
'ToBeProcessedAt' => 'SS_Datetime' |
|
31
|
|
|
); |
|
32
|
|
|
|
|
33
|
|
|
private static $default_sort = array( |
|
|
|
|
|
|
34
|
|
|
'Created' => 'DESC' |
|
35
|
|
|
); |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* standard SS variable. |
|
39
|
|
|
* |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
private static $summary_fields = array( |
|
|
|
|
|
|
43
|
|
|
'Order.Title' => 'Order', |
|
44
|
|
|
'Order.Status.Title' => 'Current Step', |
|
45
|
|
|
'ToBeProcessedAt.Nice' => 'To be processed at', |
|
46
|
|
|
'ToBeProcessedAt.Ago' => 'That is ...', |
|
47
|
|
|
'InProcess.Nice' => 'Currently Running' |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* standard SS variable. |
|
52
|
|
|
* |
|
53
|
|
|
* @var array |
|
54
|
|
|
*/ |
|
55
|
|
|
private static $searchable_fields = array( |
|
|
|
|
|
|
56
|
|
|
'OrderID' => array( |
|
57
|
|
|
'field' => 'NumericField', |
|
58
|
|
|
'title' => 'Order Number', |
|
59
|
|
|
) |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Standard SS method. |
|
65
|
|
|
* |
|
66
|
|
|
* @param Member $member |
|
|
|
|
|
|
67
|
|
|
* |
|
68
|
|
|
* @return bool |
|
69
|
|
|
*/ |
|
70
|
|
|
public function canCreate($member = null) |
|
71
|
|
|
{ |
|
72
|
|
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Standard SS method. |
|
77
|
|
|
* |
|
78
|
|
|
* @param Member $member |
|
|
|
|
|
|
79
|
|
|
* |
|
80
|
|
|
* @return bool |
|
|
|
|
|
|
81
|
|
|
*/ |
|
82
|
|
|
public function canView($member = null) |
|
83
|
|
|
{ |
|
84
|
|
|
if (! $member) { |
|
85
|
|
|
$member = Member::currentUser(); |
|
86
|
|
|
} |
|
87
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
|
|
|
|
|
|
88
|
|
|
if ($extended !== null) { |
|
89
|
|
|
return $extended; |
|
90
|
|
|
} |
|
91
|
|
|
if (Permission::checkMember($member, Config::inst()->get('EcommerceRole', 'admin_permission_code'))) { |
|
92
|
|
|
return true; |
|
93
|
|
|
} |
|
94
|
|
|
//is the member is a shop assistant they can always view it |
|
95
|
|
|
if (EcommerceRole::current_member_is_shop_assistant($member)) { |
|
96
|
|
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return parent::canView($member); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Standard SS method. |
|
104
|
|
|
* |
|
105
|
|
|
* @param Member $member |
|
|
|
|
|
|
106
|
|
|
* |
|
107
|
|
|
* @return bool |
|
108
|
|
|
*/ |
|
109
|
|
|
public function canEdit($member = null) |
|
110
|
|
|
{ |
|
111
|
|
|
return false; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Standard SS method |
|
116
|
|
|
* Queues can be deleted if needed. |
|
117
|
|
|
* |
|
118
|
|
|
* @param Member $member |
|
|
|
|
|
|
119
|
|
|
* |
|
120
|
|
|
* @return bool |
|
|
|
|
|
|
121
|
|
|
*/ |
|
122
|
|
|
public function canDelete($member = null) |
|
123
|
|
|
{ |
|
124
|
|
|
return parent::canDelete($member); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* standard SS variable. |
|
129
|
|
|
* |
|
130
|
|
|
* @var string |
|
131
|
|
|
*/ |
|
132
|
|
|
private static $singular_name = 'Order To Be Processed'; |
|
|
|
|
|
|
133
|
|
|
public function i18n_singular_name() |
|
134
|
|
|
{ |
|
135
|
|
|
return _t('OrderProcessQueue.SINGULAR_NAME', 'Order In Queue'); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* standard SS variable. |
|
140
|
|
|
* |
|
141
|
|
|
* @var string |
|
142
|
|
|
*/ |
|
143
|
|
|
private static $plural_name = 'Orders to be Processed'; |
|
|
|
|
|
|
144
|
|
|
public function i18n_plural_name() |
|
145
|
|
|
{ |
|
146
|
|
|
return _t('OrderProcessQueue.PLURAL_NAME', 'Orders In Queue'); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* META METHOD: Add an order to the job list. |
|
152
|
|
|
* If the order already exists, it will update the seconds and the creation time. |
|
153
|
|
|
* |
|
154
|
|
|
* @param Order $order |
|
155
|
|
|
* @param Int $deferInSeconds |
|
|
|
|
|
|
156
|
|
|
*/ |
|
157
|
|
|
public function AddOrderToQueue($order, $deferTimeInSeconds) |
|
158
|
|
|
{ |
|
159
|
|
|
$filter = array('OrderID' => $order->ID); |
|
160
|
|
|
$existingEntry = OrderProcessQueue::get()->filter($filter)->first(); |
|
161
|
|
|
$filter['Created'] = SS_Datetime::now()->Rfc2822(); |
|
162
|
|
|
$filter['DeferTimeInSeconds'] = $deferTimeInSeconds; |
|
163
|
|
|
if (! $existingEntry) { |
|
164
|
|
|
$existingEntry = OrderProcessQueue::create($filter); |
|
165
|
|
|
$existingEntry->OrderStepID = $order->StatusID; |
|
|
|
|
|
|
166
|
|
|
} else { |
|
167
|
|
|
foreach ($filter as $field => $value) { |
|
168
|
|
|
$existingEntry->$field = $value; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
$existingEntry->write(); |
|
172
|
|
|
|
|
173
|
|
|
return $existingEntry; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* META METHOD |
|
178
|
|
|
* processes the order ... |
|
179
|
|
|
* returns TRUE if SUCCESSFUL and FALSE if it did not run for any reason ... |
|
180
|
|
|
* |
|
181
|
|
|
* |
|
182
|
|
|
* @param Order $order optional |
|
|
|
|
|
|
183
|
|
|
* @return boolean |
|
184
|
|
|
*/ |
|
185
|
|
|
public function process($order = null) |
|
186
|
|
|
{ |
|
187
|
|
|
//find variables |
|
188
|
|
|
if( ! $order) { |
|
189
|
|
|
$order = $this->Order(); |
|
|
|
|
|
|
190
|
|
|
$myQueueObject = $this; |
|
191
|
|
|
} else { |
|
192
|
|
|
$myQueueObject = $this->getQueueObject($order); |
|
193
|
|
|
} |
|
194
|
|
|
//delete if order is gone ... |
|
195
|
|
|
if(! $order) { |
|
196
|
|
|
$myQueueObject->delete(); |
|
197
|
|
|
} |
|
198
|
|
|
//if order has moved already ... delete |
|
199
|
|
|
if( |
|
200
|
|
|
$this->OrderStepID > 0 |
|
|
|
|
|
|
201
|
|
|
&& (int)$order->StatusID !== (int)$this->OrderStepID |
|
|
|
|
|
|
202
|
|
|
) { |
|
203
|
|
|
$myQueueObject->delete(); |
|
204
|
|
|
} |
|
205
|
|
|
if($order && $myQueueObject) { |
|
206
|
|
|
if ($myQueueObject->isReadyToGo()) { |
|
207
|
|
|
$oldOrderStatusID = $order->StatusID; |
|
208
|
|
|
$myQueueObject->InProcess = true; |
|
209
|
|
|
$myQueueObject->write(); |
|
210
|
|
|
$order->tryToFinaliseOrder( |
|
211
|
|
|
$tryAgain = false, |
|
212
|
|
|
$fromOrderQueue = true |
|
213
|
|
|
); |
|
214
|
|
|
$newOrderStatusID = $order->StatusID; |
|
215
|
|
|
if($oldOrderStatusID != $newOrderStatusID) { |
|
216
|
|
|
$myQueueObject->delete(); |
|
217
|
|
|
return true; |
|
218
|
|
|
} else { |
|
219
|
|
|
$myQueueObject->InProcess = false; |
|
220
|
|
|
$myQueueObject->write(); |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
return false; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* META METHOD: returns the queue object if it exists |
|
230
|
|
|
* |
|
231
|
|
|
* @param Order $order |
|
232
|
|
|
* |
|
233
|
|
|
* @return null | OrderProcessQueue |
|
|
|
|
|
|
234
|
|
|
*/ |
|
235
|
|
|
public function getQueueObject($order) |
|
236
|
|
|
{ |
|
237
|
|
|
$filter = array('OrderID' => $order->ID); |
|
238
|
|
|
|
|
239
|
|
|
return OrderProcessQueue::get()->filter($filter)->first(); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* META METHOD: Once you are done, you can remove the item like this ... |
|
244
|
|
|
* |
|
245
|
|
|
* @param Order $order |
|
246
|
|
|
*/ |
|
247
|
|
|
public function removeOrderFromQueue($order) |
|
248
|
|
|
{ |
|
249
|
|
|
$queueEntries = OrderProcessQueue::get()->filter(array('OrderID' => $order->ID)); |
|
250
|
|
|
foreach($queueEntries as $queueEntry) { |
|
251
|
|
|
$queueEntry->delete(); |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* META METHOD: returns a list of orders to be processed |
|
257
|
|
|
* @param int $id force this Order to be processed |
|
258
|
|
|
* @param int $limit total number of orders that can be retrieved at any one time |
|
259
|
|
|
* |
|
260
|
|
|
* @return DataList (of orders) |
|
261
|
|
|
*/ |
|
262
|
|
|
public function OrdersToBeProcessed($id = 0, $limit = 9999) |
|
263
|
|
|
{ |
|
264
|
|
|
|
|
265
|
|
|
//we sort the order randomly so that we get a nice mixture |
|
266
|
|
|
//not always the same ones holding up the process |
|
267
|
|
|
$sql = ' |
|
268
|
|
|
SELECT "OrderID" |
|
269
|
|
|
FROM "OrderProcessQueue" |
|
270
|
|
|
WHERE |
|
271
|
|
|
"InProcess" = 0 |
|
272
|
|
|
AND |
|
273
|
|
|
(UNIX_TIMESTAMP("Created") + "DeferTimeInSeconds") < '.time().' |
|
274
|
|
|
ORDER BY RAND() DESC |
|
275
|
|
|
LIMIT '.$limit.'; |
|
276
|
|
|
'; |
|
277
|
|
|
$rows = DB::query($sql); |
|
278
|
|
|
$orderIDs = array($id => $id); |
|
279
|
|
|
foreach ($rows as $row) { |
|
280
|
|
|
$orderIDs[$row['OrderID']] = $row['OrderID']; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
return Order::get() |
|
284
|
|
|
->filter(array('ID' => $orderIDs)) |
|
285
|
|
|
->sort('RAND()'); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* META METHOD: all orders with a queue object |
|
290
|
|
|
* @param int $id force this Order to be processed |
|
|
|
|
|
|
291
|
|
|
* @param int $limit total number of orders that can be retrieved at any one time |
|
292
|
|
|
* |
|
293
|
|
|
* @return DataList (of orders) |
|
294
|
|
|
*/ |
|
295
|
|
|
public function AllOrdersInQueue($limit = 9999) |
|
296
|
|
|
{ |
|
297
|
|
|
|
|
298
|
|
|
return Order::get() |
|
299
|
|
|
->filter(array('ID' => OrderProcessQueue::get()->column('OrderID'))) |
|
300
|
|
|
->sort('RAND()') |
|
301
|
|
|
->limit($limit); |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* META METHOD: returns a list of orders NOT YET to be processed |
|
306
|
|
|
* @param int $limit total number of orders that can be retrieved at any one time |
|
307
|
|
|
* |
|
308
|
|
|
* @return DataList (of orders) |
|
309
|
|
|
*/ |
|
310
|
|
|
public function OrdersInQueueThatAreNotReady($limit = 9999) |
|
311
|
|
|
{ |
|
312
|
|
|
|
|
313
|
|
|
//we sort the order randomly so that we get a nice mixture |
|
314
|
|
|
//not always the same ones holding up the process |
|
315
|
|
|
$sql = ' |
|
316
|
|
|
SELECT "OrderID" |
|
317
|
|
|
FROM "OrderProcessQueue" |
|
318
|
|
|
WHERE |
|
319
|
|
|
(UNIX_TIMESTAMP("Created") + "DeferTimeInSeconds") >= '.time().' |
|
320
|
|
|
ORDER BY RAND() DESC |
|
321
|
|
|
LIMIT '.$limit.'; |
|
322
|
|
|
'; |
|
323
|
|
|
$rows = DB::query($sql); |
|
324
|
|
|
$orderIDs = array(0 => 0); |
|
325
|
|
|
foreach ($rows as $row) { |
|
326
|
|
|
$orderIDs[$row['OrderID']] = $row['OrderID']; |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
return Order::get() |
|
330
|
|
|
->filter(array('ID' => $orderIDs)) |
|
331
|
|
|
->sort('RAND()'); |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
/** |
|
335
|
|
|
* non-database method of working out if an Order is ready to go. |
|
336
|
|
|
* |
|
337
|
|
|
* @return bool |
|
338
|
|
|
*/ |
|
339
|
|
|
public function isReadyToGo() |
|
340
|
|
|
{ |
|
341
|
|
|
return (strtotime($this->Created) + $this->DeferTimeInSeconds) < time(); |
|
|
|
|
|
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
/** |
|
345
|
|
|
* |
|
346
|
|
|
* casted variable |
|
347
|
|
|
* @return SS_DateTime |
|
348
|
|
|
*/ |
|
349
|
|
|
public function ToBeProcessedAt() |
|
350
|
|
|
{ |
|
351
|
|
|
return $this->getToBeProcessedAt(); |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
/** |
|
355
|
|
|
* |
|
356
|
|
|
* casted variable |
|
357
|
|
|
* @return SS_DateTime |
|
358
|
|
|
*/ |
|
359
|
|
|
public function getToBeProcessedAt() |
|
360
|
|
|
{ |
|
361
|
|
|
return DBField::create_field('SS_Datetime', (strtotime($this->Created) + $this->DeferTimeInSeconds)); |
|
|
|
|
|
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
|
|
365
|
|
|
/** |
|
366
|
|
|
* CMS Fields |
|
367
|
|
|
* @return FieldList |
|
368
|
|
|
*/ |
|
369
|
|
|
public function getCMSFields() |
|
370
|
|
|
{ |
|
371
|
|
|
$fields = parent::getCMSFields(); |
|
372
|
|
|
if($this->exists()) { |
|
373
|
|
|
$fields->addFieldToTab( |
|
374
|
|
|
'Root.Main', |
|
375
|
|
|
ReadonlyField::create( |
|
376
|
|
|
'ToBeProcessedAtCompilations', |
|
377
|
|
|
_t('OrderProcessQueue.TO_BE_PROCESSED', 'To Be Processed'), |
|
378
|
|
|
$this->ToBeProcessedAt->Nice() . ' - ' . $this->getToBeProcessedAt()->Ago() |
|
|
|
|
|
|
379
|
|
|
), |
|
380
|
|
|
'InProcess' |
|
381
|
|
|
); |
|
382
|
|
|
$fields->addFieldToTab( |
|
383
|
|
|
'Root.Main', |
|
384
|
|
|
LiteralField::create( |
|
385
|
|
|
'processQueueNow', |
|
386
|
|
|
'<h2> |
|
387
|
|
|
<a href="/dev/tasks/EcommerceTaskProcessOrderQueue/?id='.$this->OrderID.'" target="_blank">'. |
|
|
|
|
|
|
388
|
|
|
_t('OrderProcessQueue.PROCESS', 'Process now'). |
|
389
|
|
|
'</a> |
|
390
|
|
|
</h2>' |
|
391
|
|
|
) |
|
392
|
|
|
); |
|
393
|
|
|
$fields->replaceField( |
|
394
|
|
|
'OrderID', |
|
395
|
|
|
CMSEditLinkField::create( |
|
396
|
|
|
'OrderID', |
|
397
|
|
|
'Order', |
|
398
|
|
|
$this->Order() |
|
|
|
|
|
|
399
|
|
|
) |
|
400
|
|
|
); |
|
401
|
|
|
} |
|
402
|
|
|
return $fields; |
|
403
|
|
|
} |
|
404
|
|
|
|
|
405
|
|
|
public function requireDefaultRecords() |
|
406
|
|
|
{ |
|
407
|
|
|
parent::requireDefaultRecords(); |
|
408
|
|
|
$errors = OrderProcessQueue::get()->filter(array('OrderID' => 0)); |
|
409
|
|
|
foreach($errors as $error) { |
|
410
|
|
|
DB::alteration_message(' DELETING ROGUE OrderProcessQueue', 'deleted'); |
|
411
|
|
|
$error->delete(); |
|
412
|
|
|
} |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
} |
|
416
|
|
|
|
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.