1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* @description: |
6
|
|
|
* |
7
|
|
|
* |
8
|
|
|
* @authors: Nicolaas [at] Sunny Side Up .co.nz |
9
|
|
|
* @package: ecommerce |
10
|
|
|
* @sub-package: tasks |
11
|
|
|
* @inspiration: Silverstripe Ltd, Jeremy |
12
|
|
|
**/ |
13
|
|
|
class EcommerceTaskProcessOrderQueue extends BuildTask |
14
|
|
|
{ |
15
|
|
|
protected $sendEmails = true; |
16
|
|
|
|
17
|
|
|
protected $limit = 1; |
18
|
|
|
|
19
|
|
|
protected $title = 'Process The Order Queue'; |
20
|
|
|
|
21
|
|
|
protected $description = 'Go through order queue and try to finalise all the orders in it.'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
*@return int - number of carts destroyed |
|
|
|
|
25
|
|
|
**/ |
26
|
|
|
public function run($request) |
27
|
|
|
{ |
28
|
|
|
//as this may run every minute, we have to limit it to fifty seconds. |
29
|
|
|
set_time_limit(50); |
30
|
|
|
$now = microtime(true); |
31
|
|
|
//IMPORTANT! |
32
|
|
|
if (! $this->sendEmails) { |
33
|
|
|
Config::inst()->update('Email', 'send_all_emails_to', 'no-one@localhost'); |
34
|
|
|
Email::set_mailer(new Ecommerce_Dummy_Mailer()); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
$id = intval($request->getVar('id')) - 0; |
37
|
|
|
$queueObjectSingleton = Injector::inst()->get('OrderProcessQueue'); |
38
|
|
|
$ordersinQueue = $queueObjectSingleton->OrdersToBeProcessed($id); |
39
|
|
|
if ($ordersinQueue->count() == 0) { |
40
|
|
|
echo 'No orders in queue'; |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
echo '<h3>There are '.$ordersinQueue->count().' in the queue, processing '.$this->limit.' now</h3>'; |
44
|
|
|
if ($id) { |
45
|
|
|
echo '<h3>FORCING Order with ID: '.$id.'</h3>'; |
46
|
|
|
$ordersinQueue = $ordersinQueue->filter(array('ID' => $id)); |
47
|
|
|
} |
48
|
|
|
$this->tryToFinaliseOrders($ordersinQueue); |
49
|
|
|
echo '<hr />'; |
50
|
|
|
echo '<hr />'; |
51
|
|
|
echo 'PROCESSED IN: '.round(((microtime(true) - $now) / 1), 5).' seconds'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* |
57
|
|
|
* @param DataList $orders orders to be processsed. |
58
|
|
|
*/ |
59
|
|
|
protected function tryToFinaliseOrders($orders) |
60
|
|
|
{ |
61
|
|
|
//limit orders |
62
|
|
|
$orders = $orders->limit($this->limit); |
63
|
|
|
//we sort randomly so it is less likely we get stuck with the same ones |
64
|
|
|
$orders = $orders->sort('RAND()'); |
65
|
|
|
$queueObjectSingleton = Injector::inst()->get('OrderProcessQueue'); |
66
|
|
|
foreach ($orders as $order) { |
67
|
|
|
echo '<hr />Processing order: '.$order->ID; |
68
|
|
|
$outcome = $queueObjectSingleton->process($order); |
69
|
|
|
if ($outcome === true) { |
70
|
|
|
echo '<br />... Order moved successfully.<hr />'; |
71
|
|
|
} else { |
72
|
|
|
echo '<br />... '.$outcome.'<hr />'; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.