-
Notifications
You must be signed in to change notification settings - Fork 0
/
getOrders.php
171 lines (137 loc) · 6.7 KB
/
getOrders.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
<?php
require_once 'inc/config.php';
$execTime = getCurrentDateTimeOtto();
//Check, if the csv file was already processed
if(file_exists($csvPathOrders)){
echo $execTime . " - CSV file was not processed yet!";
}
else{
//Read date of last operation
$lastDate = file_get_contents('inc/lastDate.txt');
//$lastDate = "1970-01-01T01:00:00+02:00";
//Check if new orders exist
$orders = getOrders($url, $accessToken, $lastDate);
//Make csv out of the api request
$csv = convertOrdersToCsv($orders, $lastDate);
//Write orders and last date to file. Display error message, if there are no new orders
logMe($execTime . " - Searched orders from " . $lastDate);
if($csv == null){
}
else{
echo PHP_EOL . PHP_EOL;
$csv = generateHeadline() . PHP_EOL . $csv;
logMe($csv);
writeToCsv($csvPathOrders, $csv);
writeLastDate($execTime);
}
}
function getOrders($url, $accessToken, $lastDate){
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
$requestUrl = $url . '/v4/orders?fromDate=' . urlencode($lastDate) .'&fulfillmentStatus=PROCESSABLE';
//echo $requestUrl;
curl_setopt($ch, CURLOPT_URL, $requestUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$headers = array();
$headers[] = 'Authorization: Bearer ' . $accessToken;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
/*
echo "<pre>";
var_dump(json_decode($result, true));
echo "</pre>";
*/
return json_decode($result, true);
}
/*
* Format
* Bestellnr [0]; Bestelldatum [1]; E-Mail [2]; Artikelnr [3]; Menge[4]; Preis [5];
* V-Firma1 [6]; V-Strasse [7]; V-Firma2 [8]; V-PLZ [9]; V-Ort [10]; V-LKZ [11];
* R-Firma1 [12]; R-Strasse [13]; R-Firma2 [14]; R-PLZ [15]; R-Ort [16]; R-LKZ [17];
* Telefonnr [18]; Zahlungsart [19]; Zahlungsnr [20]; Porto [21]; Nebenkosten [22];
* LastModified [23]; PositionItemId [24]
*/
function convertOrdersToCsv($orders, $lastDate){
if(!isset($orders["resources"][0])){
return null;
}
else{
$csv = "";
foreach($orders["resources"] as &$value){
foreach($value["positionItems"] as &$item){
$processDateTime = new DateTime($item["processableDate"]);
$lastDateTime = new DateTime($lastDate);
echo $value["orderNumber"] . PHP_EOL;
echo "LastDateTime: " . $lastDateTime->format('Y-m-d\TH:i:s.000+0000') . PHP_EOL;
echo "ProcessDateTime: " . $processDateTime->format('Y-m-d\TH:i:s.000+0000') . PHP_EOL . PHP_EOL;
//Only import new orders
if($processDateTime > $lastDateTime){
$quantity = 1;
$price = $item["itemValueGrossPrice"]["amount"];
$title = strtolower($item["product"]["productTitle"]);
$fees = $price * 0.15;
//"Pack" articles (multiple articles in one listing)
if(strpos($title, 'er pack')){
$strpostitle = substr($title, 0, strpos(strtolower($title), "er pack")); //Cut everything after "er Pack"
$lastspace = strrpos($strpostitle, ' '); //Search for last space
if($lastspace > 0){
$strpostitle = substr($strpostitle, $lastspace, strlen($strpostitle)); //Cut everything before last space
}
$strpostitle = preg_replace("/[^0-9]/", "", $strpostitle); //Remove every non number
$quantity *= intval($strpostitle); //Get "real" quantity
$price = doubleval($price) / doubleval($strpostitle); //Get "real" price
$fees = $fees / doubleval($strpostitle) + 0.01; //Get "real" fees
}
$csv .= $value["orderNumber"] . ';';
$csv .= $value["orderDate"] . ';';
$csv .= '' . ';'; //Mail does not exist
$csv .= $item["product"]["sku"] . ';';
$csv .= $quantity . ';';
$csv .= $price . ';';
$csv .= $value["deliveryAddress"]["firstName"] . " " . $value["deliveryAddress"]["lastName"] . ';';
$csv .= $value["deliveryAddress"]["street"] . " " . $value["deliveryAddress"]["houseNumber"] . ';';
if(isset($value["deliveryAddress"]["addition"])){ $csv .= $value["deliveryAddress"]["addition"] . ';'; } else { $csv .= ';'; }; //Not every customer has a value in addition
$csv .= $value["deliveryAddress"]["zipCode"] . ';';
$csv .= $value["deliveryAddress"]["city"] . ';';
$csv .= $value["deliveryAddress"]["countryCode"] . ';';
$csv .= $value["invoiceAddress"]["firstName"] . " " . $value["invoiceAddress"]["lastName"] . ';';
$csv .= $value["invoiceAddress"]["street"] . " " . $value["invoiceAddress"]["houseNumber"] . ';';
if(isset($value["invoiceAddress"]["addition"])){ $csv .= $value["invoiceAddress"]["addition"] . ';'; } else { $csv .= ';'; }; //Not every customer has a value in addition
$csv .= $value["invoiceAddress"]["zipCode"] . ';';
$csv .= $value["invoiceAddress"]["city"] . ';';
$csv .= $value["invoiceAddress"]["countryCode"] . ';';
if(isset($value["deliveryAddress"]["phoneNumber"])){ $csv .= $value["deliveryAddress"]["phoneNumber"] . ';'; } else { $csv .= ';'; } //Not every customer has a phone number
$csv .= $value["payment"]["paymentMethod"] . ';';
$csv .= $value["salesOrderId"] . ';';
$csv .= $value["initialDeliveryFees"][0]["deliveryFeeAmount"]["amount"] . ';';
$csv .= $fees . ';';
$csv .= $value["lastModifiedDate"] . ';';
$csv .= $item["positionItemId"] . ';';
$csv .= $item["processableDate"] . ';';
$csv .= $value["lastModifiedDate"] . ';';
$csv .= $lastDate . ';';
$csv .= $title . PHP_EOL;
}
}
}
return $csv;
}
}
function generateHeadline(){
return 'Bestellnr [0]; Bestelldatum [1]; E-Mail [2]; Artikelnr [3]; Menge[4]; Preis [5]; V-Firma1 [6]; V-Strasse [7]; V-Firma2 [8]; V-PLZ [9]; V-Ort [10]; V-LKZ [11]; L-Firma1 [12]; L-Strasse [13]; L-Firma2 [14]; L-PLZ [15]; L-Ort [16]; L-LKZ [17]; Telefonnr [18]; Zahlungsart [19]; Zahlungsnr [20]; Porto [21]; Nebenkosten [22]; Last Modified [23]; PositionItemId [24]; ProcessableDate [25]; LastModDate [26]; LastTime [27]; Title [28]';
}
function writeToCsv($csvPath, $csv){
$file = $csvPath;
//Write upload id to file
file_put_contents($file, $csv);
}
function writeLastDate($execTime){
$file = 'inc/lastDate.txt';
//Write upload id to file
file_put_contents($file, $execTime);
}