-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoCsv.php
134 lines (113 loc) · 5.62 KB
/
toCsv.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
<?php
$csvPath = "../ebayOrder.csv";
//Checks, if the csv already exists
if(file_exists($csvPath)){
echo $now." CSV file was not processed yet!";
exit();
}
//Stores all information
$list = array ( );
//Headline
array_push($list, array('Verkaufsprotokollnummer', 'Mitgliedsname', 'Vollstaendiger Name des Kaeufers', 'E-Mail des Kaeufers', 'Kaeuferadresse 1', 'Kaeuferadresse 2',
'Ort des Kaeufers', 'Staat des Kaeufers', 'Postleitzahl des Kaeufers', 'Land des Kaeufers',
'Bestellnummer', 'Artikelnummer', 'Transaktions-ID', 'Artikelbezeichnung', 'Stueckzahl',
'Verkaufspreis', 'Inklusive Mehrwertsteuersatz', 'Verpackung und Versand', 'Versicherung', 'Gesamtpreis',
'Zahlungsmethode', 'PayPal Transaktions-ID', 'Rechnungsnummer', 'CreatedTime', 'LastModifiedTime', 'ExternalTransactionTime', 'PaymentTime', 'Versanddatum',
'Versandservice', 'Abgegebene Bewertungen', 'Erhaltene Bewertungen', 'Notizzettel', 'Bestandseinheit', 'Private Notizen', 'Produktkennung-Typ', 'Produktkennung-Wert', 'Produktkennung-Wert 2',
'Variantendetails', 'Produktreferenznummer', 'Verwendungszweck', 'Sendungsnummer', 'eBay Plus', 'Nebenkosten', 'Land', 'Telefon', 'From', 'To', 'Now'));
if ($entries == 0) {
echo $now." NO new orders from " . $CreateTimeFrom . " to " . $CreateTimeTo;
}
else{
$orders = $response->OrderArray->Order;
if ($orders != null) {
echo $now. " - " . sizeof($orders) . " new orders get parsed. ( " . $CreateTimeFrom . " to " . $CreateTimeTo . " )";
foreach ($orders as $order) {
/*
echo "<pre>";
var_dump($order);
echo "</pre>";
*/
$shippingAddress = $order->ShippingAddress;
$ShippingServiceSelected = $order->ShippingServiceSelected;
$externalTransaction = $order->ExternalTransaction;
$checkoutmessage = $order->BuyerCheckoutMessage;
$checkoutmessage = preg_replace('/\s+/', ' ', trim($checkoutmessage)); //Our ERP-system has problems with \r\n in messages. This removes them.
$transactions = $order->TransactionArray;
if ($transactions) {
// iterate through each transaction for the order
$i = 0;
foreach ($transactions->Transaction as $transaction) {
$title = $transaction->Item->Title;
$quantity = $transaction->QuantityPurchased;
$price = $transaction->TransactionPrice;
$fees = (doubleval($price) + doubleval($ShippingServiceSelected->ShippingServiceCost)) / doubleval($quantity) * 0.11;
$paymentID = $order->ExternalTransaction->ExternalTransactionID;
//For users who put their street number in the second address field
if(strlen($shippingAddress->Street2) < 5){
$street01 = $shippingAddress->Street1 . " " . $shippingAddress->Street2;
$street02 = "";
}
else{
$street01 = $shippingAddress->Street1;
$street02 = $shippingAddress->Street2;
}
//Packs with multiple items?
if(strpos(strtolower($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
}
$quantity *= intval($strpostitle); //Get "real" quantity
$price = doubleval($price) / doubleval($strpostitle); //Get "real" price
$fees = $fees / doubleval($strpostitle) + 0.01; //Get "real" fees
}
/*
if($quantity > 1 || $i == 1){
$fees = 0;
}
$i = 1; //Fees should only be imported once
*/
//On some transactions the paymentID gets set to "SIS". We don't want this.
if($paymentID == "SIS"){
$paymentID = "";
}
/*
//Check if the customer has a "StaticAlias" Address
if(!isset($transaction->Buyer->StaticAlias) || $transaction->Buyer->StaticAlias == "" || $transaction->Buyer->StaticAlias == null){
$email = $transaction->Buyer->Email;
}
else{
$email = $transaction->Buyer->StaticAlias;
}
*/
$email = $transaction->Buyer->Email;
array_push($list, array($order->OrderID, $order->BuyerUserID, $shippingAddress->Name, $email, $street01, $street02,
$shippingAddress->CityName, $shippingAddress->StateOrProvince, $shippingAddress->PostalCode, $shippingAddress->CountryName,
$transaction->OrderLineItemID, $transaction->Item->SKU, $transaction->TransactionID, $transaction->Item->Title, $quantity,
$price, $order->ShippingDetails->SalesTax->SalesTaxAmount, $ShippingServiceSelected->ShippingServiceCost, "0,00", $order->AmountPaid,
$order->CheckoutStatus->PaymentMethod, $paymentID, '',
$order->CreatedTime, $order->CheckoutStatus->LastModifiedTime, $externalTransaction->ExternalTransactionTime, $order->PaidTime, '',
$ShippingServiceSelected->ShippingService, 'Nein', '', '', $transaction->Item->SKU, $checkoutmessage, '', '', '',
'', '', '', '', 'Nein', $fees, $shippingAddress->Country, $shippingAddress->Phone, $CreateTimeFrom, $CreateTimeTo, $now));
}
}
}
}else{
echo $now." NO new orders.";
}
//Write the transactions to file
$csvWriter = fopen($csvPath, 'w');
for ($i = 0; $i < count($list); $i++) {
fputcsv($csvWriter, $list[$i], ';');
}
fclose($csvWriter);
$CreateTimeToDT->modify("+1 second");
$CreateTimeToAddOneSec = $CreateTimeToDT->format("Y-m-d\TH:i:s.000\Z");
//var_dump($CreateTimeToAddOneSec);
$lastWriter = fopen('last.txt', 'w+');
fwrite($lastWriter, $CreateTimeToAddOneSec);
fclose($lastWriter);
}
?>