-
Notifications
You must be signed in to change notification settings - Fork 2
/
orders.php
executable file
·248 lines (169 loc) · 5.81 KB
/
orders.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
require_once 'common.php';
class Orders {
// filepaths for ftp and local directory
const LOCAL = '/tmp/orders.csv';
const REMOTE = '/Output/orders.csv';
public static function download($local, $remote) {
try {
$ftp = new Ftp;
$ftp->connect(FTP_HOST);
$ftp->login(FTP_USER, FTP_PASS);
$ftp->pasv(true);
$ftp->get($local, $remote, Ftp::BINARY);
return true;
} catch (FtpException $e) {
//throw new Exception($e->getMessage());
Mage::log($e->getMessage());
// return true, we'll create the file if not found / exists
return true;
}
}
public static function export() {
// download csv file from ftp
self::download(__DIR__ . self::LOCAL, FTP_PATH . self::REMOTE);
// write (merge) data
$parser = new Parser(__DIR__ . self::LOCAL);
$parser->write(self::flatOrderArr());
// upload from remote and save to specified directory
try {
self::upload(__DIR__ . self::LOCAL, FTP_PATH . self::REMOTE);
} catch (Exception $e) {
Mage::log($e->getMessage());
mail(ADMIN_EMAIL, 'CLASSIC FTP ERROR [ORDERS UPLOAD]', "ERROR: \n" . json_encode($e->getMessage()) . "\n\nRERUN ORDER DATA: \n" . json_encode(self::flatOrderArr()));
return false;
}
fwrite(STDOUT, " ++ ORDER SYNC COMPLETE \n");
return true;
}
public static function upload($local, $remote) {
try {
$ftp = new Ftp;
$ftp->connect(FTP_HOST);
$ftp->login(FTP_USER, FTP_PASS);
$ftp->pasv(true);
$ftp->put($remote, $local, Ftp::BINARY);
// remove local file
unlink($local);
return true;
} catch (FtpException $e) {
throw new Exception($e->getMessage());
Mage::log($e->getMessage());
return false;
}
}
public static function mapShippingAgent($agent) {
if (stristr($agent, 'UPS')) {
// ups
return 'UPSN';
} else if (stristr($agent, 'USPS')) {
// usps
return 'USPOSTAL';
}
return null;
}
public static function mapShippingService($service) {
if (stristr($service, 'UPS Three-Day Select')) {
// ups 3 day select
return '3 DAY SELECT';
} else if (stristr($service, 'UPS Next Day Air')) {
// ups next day air
return 'NEXT DAY AIR';
} else if (stristr($service, 'UPS Second Day Air')) {
// ups 2nd day air
return '2ND DAY AIR';
} else if (stristr($service, 'UPS Ground')) {
// ups ground
return 'GROUND';
} else if (stristr($service, 'Priority Mail')) {
// usps priority
return 'PRIORITY-PARCEL';
}
return null;
}
public static function buildRow($order, $item, $address, $shipping) {
$arr = array(
'Magento Order ID' => $order['increment_id'],
'Order Date' => date('m/d/Y', strtotime($order['created_at'])),
'Line Item SKU' => $item['sku'],
'Line Item Qty' => round($item['qty_ordered']),
'Line Item Subtotal' => $item['row_total'] * $item['qty_ordered'], // row total without tax
'Line Item Total' => $item['row_total_incl_tax'], // row total with tax
'Tax' => $order['tax_amount'],
'Ship To Email' => isset($address['email']) ? trim(strtolower($address['email'])) : trim(strtolower($order['customer_email'])),
'Ship To Name' => trim(ucwords(strtolower($address['firstname']))) . ' ' . trim(ucwords(strtolower($address['lastname']))), // new
'Ship To Address 1' => trim(ucwords(strtolower($address['street']))),
'Ship To Address 2' => '',
'Ship To City' => trim(ucwords(strtolower($address['city']))),
'Ship To State' => trim(ucwords(strtolower($address['region']))),
'Ship To Zip' => trim($address['postcode']),
'Ship To Phone' => (!empty($address['telephone'])) ? $address['telephone'] : null, // new
'Shipping Agent' => self::mapShippingAgent($shipping['agent']),
'Shipping Service' => self::mapShippingService($shipping['service'])
);
return $arr;
}
public static function orderCollection() {
$orders = Mage::getModel('sales/order')->getCollection()
->addFieldToFilter('status', 'processing') // products that have not been submitted for packaging
// ->addFieldToFilter('created_at', array(
// 'from' => strtotime('-1 day', time()),
// 'to' => time(),
// 'datetime' => true
// ))
;
return $orders;
}
public static function orderData($order) {
return $order->getData();
}
public static function flatOrderArr() {
$collection = self::orderCollection();
$arr = array();
foreach ($collection as $obj) {
// get order specific data
$order = self::orderData($obj);
$address = self::orderShippingAddress($obj);
$shipping = self::orderShippingMethod($obj);
$items = self::orderItems($obj);
// for each of the items, create a flat row
foreach ($items as $item) {
// build array
$arr[] = self::buildRow($order, $item, $address, $shipping);
}
// set order to pending
self::setOrderToPending($order['increment_id']);
}
return $arr;
}
public static function setOrderToPending($order_id) {
// get order
$order = Mage::getModel('sales/order')->loadByIncrementId($order_id);
// set status to submitted (processing: submitted)
$order->setStatus('submitted');
// save
$order->save();
return true;
}
public static function orderItems($obj) {
$items = $obj->getAllVisibleItems();
$data = array();
foreach ($items as $item) {
$data[] = $item->getData();
}
return $data;
}
public static function orderShippingAddress($obj) {
$address = Mage::getModel('sales/order_address')
->load($obj->getShippingAddressId())
->getData();
return $address;
}
public static function orderShippingMethod($obj) {
$shipping = array(
'agent' => $obj->getShippingMethod(),
'service' => $obj->getShippingDescription()
);
return $shipping;
}
}