|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* After a bug in the saving of orders in the CMS |
|
5
|
|
|
* This "fixer" was introduced to fix older orders |
|
6
|
|
|
* without a submission record. |
|
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 EcommerceTaskArchiveAllOrdersWithItems extends BuildTask |
|
14
|
|
|
{ |
|
15
|
|
|
protected $title = 'Archive all orders with order items and payment and add a submit record.'; |
|
16
|
|
|
|
|
17
|
|
|
protected $description = " |
|
18
|
|
|
This task moves all orders to the 'Archived' (last) Order Step without running any of the tasks in between. |
|
19
|
|
|
NB: It also adds a submit record. |
|
20
|
|
|
This task is basically for orders that never got archived. |
|
21
|
|
|
"; |
|
22
|
|
|
|
|
23
|
|
|
private static $payment_table = 'EcommercePayment'; |
|
24
|
|
|
|
|
25
|
|
|
public function run($request) |
|
26
|
|
|
{ |
|
27
|
|
|
set_time_limit(1200); |
|
28
|
|
|
//IMPORTANT! |
|
29
|
|
|
$lastOrderStep = DataObject::get_one( |
|
30
|
|
|
'OrderStep', |
|
31
|
|
|
'', |
|
32
|
|
|
$cache = true, |
|
33
|
|
|
array('Sort' => 'DESC') |
|
|
|
|
|
|
34
|
|
|
); |
|
35
|
|
|
if ($lastOrderStep) { |
|
36
|
|
|
$joinSQL = ' |
|
37
|
|
|
INNER JOIN "OrderAttribute" ON "Order"."ID" = "OrderAttribute"."OrderID" |
|
38
|
|
|
INNER JOIN "OrderItem" ON "OrderItem"."ID" = "OrderAttribute"."ID" |
|
39
|
|
|
INNER JOIN "'.self::$payment_table.'" ON "'.self::$payment_table.'"."OrderID" = "Order"."ID" |
|
40
|
|
|
'; |
|
41
|
|
|
$whereSQL = 'WHERE "StatusID" <> '.$lastOrderStep->ID.' '; |
|
42
|
|
|
$count = DB::query(" |
|
43
|
|
|
SELECT COUNT (\"Order\".\"ID\") |
|
44
|
|
|
FROM \"Order\" |
|
45
|
|
|
$joinSQL |
|
46
|
|
|
$whereSQL |
|
47
|
|
|
")->value(); |
|
48
|
|
|
$do = DB::query(" |
|
|
|
|
|
|
49
|
|
|
UPDATE \"Order\" |
|
50
|
|
|
$joinSQL |
|
51
|
|
|
SET \"Order\".\"StatusID\" = ".$lastOrderStep->ID." |
|
52
|
|
|
$whereSQL |
|
53
|
|
|
"); |
|
54
|
|
|
if ($count) { |
|
55
|
|
|
DB::alteration_message("NOTE: $count records were updated.", 'created'); |
|
56
|
|
|
} else { |
|
57
|
|
|
DB::alteration_message('No records were updated.'); |
|
58
|
|
|
} |
|
59
|
|
|
} else { |
|
60
|
|
|
DB::alteration_message('Could not find the last order step.', 'deleted'); |
|
61
|
|
|
} |
|
62
|
|
|
$this->createSubmissionLogForArchivedOrders(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function createSubmissionLogForArchivedOrders() |
|
66
|
|
|
{ |
|
67
|
|
|
$lastOrderStep = DataObject::get_one( |
|
68
|
|
|
'OrderStep', |
|
69
|
|
|
'', |
|
70
|
|
|
$cache = true, |
|
71
|
|
|
array('Sort' => 'DESC') |
|
|
|
|
|
|
72
|
|
|
); |
|
73
|
|
|
$submissionLogClassName = EcommerceConfig::get('OrderStatusLog', 'order_status_log_class_used_for_submitting_order'); |
|
74
|
|
|
$obj = $submissionLogClassName::create(); |
|
75
|
|
|
if (!is_a($obj, Object::getCustomClass('OrderStatusLog'))) { |
|
76
|
|
|
user_error('EcommerceConfig::get("OrderStatusLog", "order_status_log_class_used_for_submitting_order") refers to a class that is NOT an instance of OrderStatusLog'); |
|
77
|
|
|
} |
|
78
|
|
|
$orderStatusLogClassName = 'OrderStatusLog'; |
|
79
|
|
|
$offset = 0; |
|
80
|
|
|
$orders = $this->getOrdersForCreateSubmissionLogForArchivedOrders($lastOrderStep, $orderStatusLogClassName, $offset); |
|
81
|
|
|
while ($orders->count()) { |
|
82
|
|
|
foreach ($orders as $order) { |
|
83
|
|
|
$isSubmitted = $submissionLogClassName::get() |
|
84
|
|
|
->Filter(array('OrderID' => $order->ID)) |
|
85
|
|
|
->count(); |
|
86
|
|
|
if (!$isSubmitted) { |
|
87
|
|
|
$obj = $submissionLogClassName::create(); |
|
88
|
|
|
|
|
89
|
|
|
$obj->OrderID = $order->ID; |
|
90
|
|
|
//it is important we add this here so that we can save the 'submitted' version. |
|
91
|
|
|
//this is particular important for the Order Item Links. |
|
92
|
|
|
$obj->write(); |
|
93
|
|
|
$obj->OrderAsHTML = $order->ConvertToHTML(); |
|
94
|
|
|
$obj->write(); |
|
95
|
|
|
DB::alteration_message('creating submission log for Order #'.$obj->OrderID, 'created'); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
$offset += 100; |
|
99
|
|
|
$orders = $this->getOrdersForCreateSubmissionLogForArchivedOrders($lastOrderStep, $orderStatusLogClassName, $offset); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function getOrdersForCreateSubmissionLogForArchivedOrders($lastOrderStep, $orderStatusLogClassName, $offset) |
|
104
|
|
|
{ |
|
105
|
|
|
return Order::get() |
|
106
|
|
|
->filter(array('StatusID' => $lastOrderStep->ID)) |
|
107
|
|
|
->leftJoin($orderStatusLogClassName, "\"$orderStatusLogClassName\".\"OrderID\" = \"Order\".\"ID\"") |
|
108
|
|
|
->where("\"$orderStatusLogClassName\".\"ID\" IS NULL") |
|
109
|
|
|
->limit(100, $offset); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: