|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* set the order id number. |
|
5
|
|
|
* |
|
6
|
|
|
* @authors: Nicolaas [at] Sunny Side Up .co.nz |
|
7
|
|
|
* @package: ecommerce |
|
8
|
|
|
* @sub-package: tasks |
|
9
|
|
|
* @inspiration: Silverstripe Ltd, Jeremy |
|
10
|
|
|
**/ |
|
11
|
|
|
class EcommerceTaskOrderItemsPerCustomer extends BuildTask |
|
12
|
|
|
{ |
|
13
|
|
|
protected $title = 'Export all order items to CSV per customer'; |
|
14
|
|
|
|
|
15
|
|
|
protected $description = 'Allows download of all sales items with all details as CSV. Excludes sales made by Admins'; |
|
16
|
|
|
|
|
17
|
|
|
public function run($request) |
|
18
|
|
|
{ |
|
19
|
|
|
//reset time limit |
|
20
|
|
|
set_time_limit(1200); |
|
21
|
|
|
|
|
22
|
|
|
//file data |
|
23
|
|
|
$now = Date('d-m-Y-H-i'); |
|
24
|
|
|
$fileName = "export-$now.csv"; |
|
25
|
|
|
|
|
26
|
|
|
//data object variables |
|
27
|
|
|
$orderStatusSubmissionLog = EcommerceConfig::get('OrderStatusLog', 'order_status_log_class_used_for_submitting_order'); |
|
28
|
|
|
$fileData = ''; |
|
29
|
|
|
$offset = 0; |
|
30
|
|
|
$count = 50; |
|
31
|
|
|
|
|
32
|
|
|
while ( |
|
33
|
|
|
$orders = Order::get() |
|
|
|
|
|
|
34
|
|
|
->sort('"Order"."ID" ASC') |
|
35
|
|
|
->innerJoin('OrderStatusLog', '"Order"."ID" = "OrderStatusLog"."OrderID"') |
|
36
|
|
|
->innerJoin($orderStatusSubmissionLog, "\"$orderStatusSubmissionLog\".\"ID\" = \"OrderStatusLog\".\"ID\"") |
|
37
|
|
|
->leftJoin('Member', '"Member"."ID" = "Order"."MemberID"') |
|
38
|
|
|
->limit($count, $offset) && |
|
39
|
|
|
$ordersCount = $orders->count() |
|
|
|
|
|
|
40
|
|
|
) { |
|
41
|
|
|
$offset = $offset + $count; |
|
42
|
|
|
foreach ($orders as $order) { |
|
|
|
|
|
|
43
|
|
|
if ($order->IsSubmitted()) { |
|
44
|
|
|
$memberIsOK = false; |
|
45
|
|
|
if (!$order->MemberID) { |
|
46
|
|
|
$memberIsOK = true; |
|
47
|
|
|
} elseif (!$order->Member()) { |
|
48
|
|
|
$memberIsOK = true; |
|
49
|
|
|
} elseif ($member = $order->Member()) { |
|
50
|
|
|
$memberIsOK = true; |
|
51
|
|
|
if ($member->IsShopAssistant()) { |
|
52
|
|
|
$memberIsOK = false; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
if ($memberIsOK) { |
|
56
|
|
|
$items = OrderItem::get()->filter(array('OrderID' => $order->ID)); |
|
57
|
|
|
if ($items && $items->count()) { |
|
58
|
|
|
$fileData .= $this->generateExportFileData($order->getOrderEmail(), $order->SubmissionLog()->Created, $items); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
unset($orders); |
|
64
|
|
|
} |
|
65
|
|
|
if ($fileData) { |
|
66
|
|
|
SS_HTTPRequest::send_file($fileData, $fileName, 'text/csv'); |
|
67
|
|
|
} else { |
|
68
|
|
|
user_error('No records found', E_USER_ERROR); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function generateExportFileData($email, $date, $orderItems) |
|
73
|
|
|
{ |
|
74
|
|
|
$separator = ','; |
|
75
|
|
|
$fileData = ''; |
|
76
|
|
|
$columnData = array(); |
|
|
|
|
|
|
77
|
|
|
$exportFields = array( |
|
78
|
|
|
'OrderID', |
|
79
|
|
|
'InternalItemID', |
|
80
|
|
|
'TableTitle', |
|
81
|
|
|
'TableSubTitleNOHTML', |
|
82
|
|
|
'UnitPrice', |
|
83
|
|
|
'Quantity', |
|
84
|
|
|
'CalculatedTotal', |
|
85
|
|
|
); |
|
86
|
|
|
|
|
87
|
|
|
if ($orderItems) { |
|
88
|
|
|
foreach ($orderItems as $item) { |
|
89
|
|
|
$columnData = array(); |
|
90
|
|
|
$columnData[] = '"'.$email.'"'; |
|
91
|
|
|
$columnData[] = '"'.$date.'"'; |
|
92
|
|
|
foreach ($exportFields as $field) { |
|
93
|
|
|
$value = $item->$field; |
|
94
|
|
|
$value = preg_replace('/\s+/', ' ', $value); |
|
95
|
|
|
$value = str_replace(array("\r", "\n"), "\n", $value); |
|
96
|
|
|
$tmpColumnData = '"'.str_replace('"', '\"', $value).'"'; |
|
97
|
|
|
$columnData[] = $tmpColumnData; |
|
98
|
|
|
} |
|
99
|
|
|
$fileData .= implode($separator, $columnData); |
|
100
|
|
|
$fileData .= "\n"; |
|
101
|
|
|
$item->destroy(); |
|
102
|
|
|
unset($item); |
|
103
|
|
|
unset($columnData); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $fileData; |
|
107
|
|
|
} else { |
|
108
|
|
|
return ''; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|