Subversion Repositories munaweb

Rev

Rev 7 | Rev 12 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
<!DOCTYPE html>
2
<html lang="en">
3
 
4
<head>
5
    <title>Order Reports</title>
6
    <meta charset="UTF-8">
7
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
8
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
9
    <link rel="icon" href="favicon.ico" type="image/x-icon">
10
 
11
    <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
12
    <link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
13
    <link rel="stylesheet" href="css/style.css">
14
    <script src="js/XMLWriter.js"></script>
15
    <script src="js/lodash.min.js"></script>
16
    <script src="js/datetimepicker_css.js"></script>
17
    <script src="js/moment.js"></script>
18
    <script src="js/moment-timezone.js"></script>
19
    <script src="js/muna-tools.js"></script>
20
</head>
21
 
22
<body onload="return initConfig();">
23
    <div class="w3-main">
24
        <div class="w3-container w3-padding w3-margin w3-card-4">
25
            <div class="w3-container w3-gray" style="height:100px">
26
                <img class="w3-image" src="images/MUNA%20-%20Logo%20100x100.png" alt="MUNA Trading Logo" style="height:100px">
27
                <p id="connected" class="w3-xxlarge w3-right">Order Reports
28
                    <input id="login" type="button" class="w3-btn w3-large w3-green w3-margin w3-round-large w3-ripple w3-right w3-hide" onclick="eBayLogin();" value="Login" />
29
                </p>
30
            </div>
31
 
32
            <div class="w3-row">
7 - 33
                <form id="searchForm" class="w3-container w3-light-grey w3-padding w3-small" onsubmit="return getOrders();">
34
                    <div class="w3-container w3-card-2 w3-cell w3-padding">
35
                        <h4>Report Range</h4>
36
                        <div class="w3-row">
37
                            <div class="w3-half w3-padding">
38
                                <label for="startDate" class="w3-small">On or After <img src="images2/cal.gif" alt="Calendar Picker" onclick="javascript:NewCssCal('startDate','yyyyMMdd','arrow',false,'24',false,'past')" style="cursor:pointer" /></label>
39
                                <input id="startDate" name="startDate" class="w3-input w3-border-0" style="height:3em" type="text" required>
2 - 40
                            </div>
7 - 41
                            <div class="w3-half w3-padding">
42
                                <label for="endDate" class="w3-small">On or Before <img src="images2/cal.gif" alt="Calendar Picker" onclick="javascript:NewCssCal('endDate','yyyyMMdd','arrow',false,'24',false,'past')" style="cursor:pointer" /></label>
43
                                <input id="endDate" name="endDate" class="w3-input w3-border-0" style="height:3em" type="text" required>
44
                            </div>
2 - 45
                        </div>
6 - 46
                    </div>
7 - 47
                    <div class="w3-container w3-card-2 w3-cell w3-padding">
48
                        <p>
49
                            <input id="shippingList" class="w3-radio" type="radio" style="height:1em" name="rptType" value="shippingList" checked>
50
                            <label for="shippingList">Awaiting Shipment</label></p>
51
                        <p>
52
                            <input id="orderList" class="w3-radio" type="radio" style="height:1em" name="rptType" value="orderList">
53
                            <label for="orderList">Orders</label></p>
54
                        <p>
55
                            <input id="feeList" class="w3-radio" type="radio" style="height:1em" name="rptType" value="feeList">
56
                            <label for="feeList">eBay Fees and Credits</label></p>
57
                        <p>
58
                            <input id="trackingList" class="w3-radio" type="radio" style="height:1em" name="rptType" value="trackingList">
59
                            <label for="trackingList">Tracking Status</label></p>
60
                        <p>
61
                            <input id="customerList" class="w3-radio" type="radio" style="height:1em" name="rptType" value="customerList">
62
                            <label for="customerList">eBay Customer Export</label></p>
63
                        <input id="printButton" type="button" class="w3-btn w3-large w3-black w3-margin w3-round-large w3-ripple w3-right w3-hide" onclick="printData();" value="Print" />
64
                        <input id="dlOrderListButton" type="button" class="w3-btn w3-large w3-black w3-margin w3-round-large w3-ripple w3-right w3-hide" onclick="exportTableToCSV('orderTable', 'orders.csv');" value="CSV" />
65
                        <input id="dlFeeListButton" type="button" class="w3-btn w3-large w3-black w3-margin w3-round-large w3-ripple w3-right w3-hide" onclick="exportTableToCSV('feeTable', 'fees.csv');" value="CSV" />
66
                        <input id="dlCustomerListButton" type="button" class="w3-btn w3-large w3-black w3-margin w3-round-large w3-ripple w3-right w3-hide" onclick="exportTableToCSV('customerTable', 'customers.csv');" value="CSV" />
67
                        <input id="startButton" type="button" class="w3-btn w3-large w3-red w3-margin w3-round-large w3-ripple w3-right" onclick="getOrders();" value="Start" />
68
                    </div>
69
                    <div id="summary" class="w3-container w3-cell w3-padding"></div>
70
                </form>
71
            </div>
72
            <div id="progressBarDiv" class="w3-container w3-padding w3-margin w3-card-4 w3-hide">
73
                <h2 id="progressBarHeader"></h2>
74
                <div class="w3-light-grey">
75
                    <div id="progressBar" class="w3-container w3-green w3-center" style="width:0%">0%</div>
6 - 76
                </div>
2 - 77
            </div>
7 - 78
            <div id="results" class="w3-container w3-padding w3-card-4 w3-hide"></div>
79
            <div id="printTable">
80
                <div id="logging" class="w3-container w3-padding"></div>
81
            </div>
82
            <div id="modals"></div>
2 - 83
        </div>
84
 
85
        <footer class="w3-container w3-center w3-border-top w3-margin">
86
            Copyright &#169; 2018 MUNA Trading. All rights reserved.
87
        </footer>
88
 
89
    </div>
90
 
91
    <script>
92
 
93
// Globals
94
var pagesToProcess = 0;
6 - 95
var pagesProcessed = 0;
96
var maxPagesToProcess = 0;
2 - 97
var shippingsToProcess = 0;
6 - 98
var shippingsProcessed = 0;
2 - 99
var thumbnailsToProcess = 0;
6 - 100
var thumbnailsProcessed = 0;
101
var maxThumbnailsToProcess = 0;
2 - 102
var trackingNumbersToProcess = 0;
6 - 103
var trackingNumbersProcessed = 0;
104
var maxTrackingNumbersToProcess = 0;
2 - 105
var html = [];
106
var url;
107
var trackingList = [];
108
var trackingListDedupe = [];
109
 
110
var itemsSold;
111
var grossSales;
112
var shippingRcvd;
113
var returnCost;
114
var shippingCost;
115
var eBayFees;
116
var PayPalFees;
117
var VATaxGross;
118
var VATaxExempt;
119
var VATaxAmount;
120
var totalAmount;
121
 
122
// Initialize Configuration Variables
123
function initConfig() {
6 - 124
    eBayAuthToken = readCookie();
125
    if (eBayAuthToken.length > 0) {
126
        connected();
127
    }
2 - 128
 
6 - 129
    if (eBayAuthTokenFlag === false) {
130
        var x = document.getElementById("login");
131
        if (x.className.indexOf("w3-show") == -1) {
132
            x.className += " w3-show";
133
        }
134
    }
2 - 135
 
6 - 136
    var currentDate = moment();
7 - 137
    var weekDayName =  moment().format('dddd');
138
    var yesterday = moment().subtract((weekDayName == "Monday" ? 2 : 1), "days");
2 - 139
 
7 - 140
    x = document.getElementById("startDate");
141
    if (x.value.length < 1) {
142
        x.value = yesterday.format('YYYY-MM-DD');
6 - 143
    }
7 - 144
 
145
    x = document.getElementById("endDate");
146
    if (x.value.length < 1) {
147
        x.value = currentDate.format('YYYY-MM-DD');
6 - 148
    }
2 - 149
}
150
 
151
function requireNewLogin() {
6 - 152
    // dummy
2 - 153
}
154
 
155
function getOrders() {
6 - 156
    var x;
2 - 157
 
6 - 158
    itemsSold = 0;
159
    grossSales = 0.0;
160
    shippingRcvd = 0.0;
161
    returnCost = 0.0;
162
    shippingCost = 0.0;
163
    eBayFees = 0.0;
164
    PayPalFees = 0.0;
165
    VATaxGross = 0.0;
166
    VATaxExempt = 0.0;
167
    VATaxAmount = 0.0;
2 - 168
 
6 - 169
    document.getElementById("summary").innerHTML = '';
170
    document.getElementById("results").innerHTML = '';
171
    document.getElementById("logging").innerHTML = '';
2 - 172
 
6 - 173
    var sDate = moment(document.getElementById("startDate").value, 'YYYY-MM-DD', true);
174
    var eDate = moment(document.getElementById("endDate").value, 'YYYY-MM-DD', true);
2 - 175
 
6 - 176
    if (!sDate.isValid()) {
177
        document.getElementById("startDate").value = '';
178
        return;
179
    }
2 - 180
 
6 - 181
    if (!eDate.isValid()) {
182
        document.getElementById("endDate").value = '';
183
        return;
184
    }
2 - 185
 
6 - 186
    if (moment(document.getElementById("startDate").value).isAfter(document.getElementById("endDate").value, 'day')) {
187
        document.getElementById("endDate").value = '';
188
        return;
189
    }
2 - 190
 
6 - 191
    x = document.getElementById("printButton");
192
    x.className = x.className.replace(" w3-show", "");
193
    x = document.getElementById("dlOrderListButton");
194
    x.className = x.className.replace(" w3-show", "");
195
    x = document.getElementById("dlFeeListButton");
196
    x.className = x.className.replace(" w3-show", "");
197
    x = document.getElementById("dlCustomerListButton");
198
    x.className = x.className.replace(" w3-show", "");
7 - 199
    x = document.getElementById("summary");
200
    x.className = x.className.replace("  w3-card-2", "");
2 - 201
 
6 - 202
    x = document.getElementById("results");
203
    if (x.className.indexOf("w3-show") == -1) {
204
        x.className += " w3-show";
205
    }
2 - 206
 
6 - 207
    pagesToProcess = 1;
208
    pagesProcessed = 0;
209
    maxPagesToProcess = 1;
2 - 210
 
6 - 211
    if (getRadioValue('rptType') == 'feeList') {
212
        document.getElementById("results").innerHTML = '<p><strong>Retrieving Fees and Credits...</strong></p>';
213
        initProgressBar("Retrieving eBay Fees and Credits...");
2 - 214
 
6 - 215
        retrieveFees(1);
216
    } else {
217
        document.getElementById("results").innerHTML = '<p><strong>Retrieving Orders...</strong></p>';
218
        document.getElementById("modals").innerHTML = '';
2 - 219
 
6 - 220
        retrieveeBayOrders(1);
221
        initProgressBar("Retrieving Orders...");
2 - 222
 
6 - 223
        function checkpagesToProcess() {
224
            if (pagesToProcess > 0) {
225
                window.setTimeout(checkpagesToProcess, 100); // wait 100 milliseconds
226
            } else {
227
                endProgressBar();
2 - 228
 
6 - 229
                if (getRadioValue('rptType') == 'shippingList') {
230
                    retrieveShopifyShippings(1);
231
                } else if (getRadioValue('rptType') == 'trackingList') {
232
                    retrieveShopifyTrackings(1);
233
                } else if (getRadioValue('rptType') == 'customerList') {
234
                    sortTable("customerTable", 1);
235
                    x = document.getElementById("results");
236
                    x.innerHTML = '<p><strong>Report Finished!</strong></p>';
237
                    if (!x.className.includes("process-errors")) {
238
                        setTimeout(function() {
239
                            x.className = x.className.replace(" w3-show", "");
240
                        }, 3000);
241
                    }
242
                } else {
243
                    retrieveShopifyOrders(1);
244
                }
245
            }
246
        }
2 - 247
 
6 - 248
        checkpagesToProcess();
249
 
250
        if (getRadioValue('rptType') == 'shippingList') {
251
            x = document.getElementById("printButton");
252
            if (x.className.indexOf("w3-show") == -1) {
253
                x.className += " w3-show";
254
            }
255
        } else if (getRadioValue('rptType') == 'orderList') {
256
            x = document.getElementById("dlOrderListButton");
257
            if (x.className.indexOf("w3-show") == -1) {
258
                x.className += " w3-show";
259
            }
260
        } else if (getRadioValue('rptType') == 'customerList') {
261
            x = document.getElementById("dlCustomerListButton");
262
            if (x.className.indexOf("w3-show") == -1) {
263
                x.className += " w3-show";
264
            }
265
        }
266
    }
2 - 267
}
268
 
269
function retrieveeBayOrders(pageNumber) {
6 - 270
    var i;
271
    var xml;
272
    var authErrorFlag = false;
2 - 273
 
6 - 274
    if (eBayAuthTokenFlag === false) {
275
        return;
276
    }
2 - 277
 
6 - 278
    var valueStartDate = moment(document.getElementById('startDate').value + " 00:00:00", "YYYY-MM-DD HH:mm:ss").tz("UTC").toISOString();
279
    var valueEndDate = moment(document.getElementById('endDate').value + " 23:59:59", "YYYY-MM-DD HH:mm:ss").tz("UTC").toISOString();
2 - 280
 
6 - 281
    var xw = new XMLWriter('UTF-8', '1.0');
282
    var xhr = new XMLHttpRequest();
2 - 283
 
6 - 284
    xw.writeStartDocument();
285
    xw.writeStartElement("GetOrdersRequest");
286
    xw.writeAttributeString('xmlns', 'urn:ebay:apis:eBLBaseComponents');
2 - 287
 
6 - 288
    xw.writeStartElement('RequesterCredentials');
289
    xw.writeElementString('eBayAuthToken', eBayAuthToken);
290
    xw.writeEndElement(); /* RequesterCredentials */
2 - 291
 
6 - 292
    xw.writeElementString('CreateTimeFrom', valueStartDate);
293
    xw.writeElementString('CreateTimeTo', valueEndDate);
2 - 294
 
6 - 295
    xw.writeElementString('IncludeFinalValueFee', 'true');
296
    xw.writeElementString('OrderRole', 'Seller');
297
    xw.writeElementString('OrderStatus', 'All');
2 - 298
 
6 - 299
    xw.writeStartElement('Pagination');
300
    xw.writeElementString('EntriesPerPage', configXmlRequestEntriesPerPage);
301
    xw.writeElementString('PageNumber', pageNumber.toString());
302
    xw.writeEndElement(); /* Pagination */
2 - 303
 
6 - 304
    xw.writeElementString('DetailLevel', 'ReturnAll');
2 - 305
 
6 - 306
    xw.writeElementString('ErrorLanguage', 'en_US');
307
    xw.writeElementString('Version', configeBayTradingVersion);
308
    xw.writeElementString('WarningLevel', configWarningLevel);
2 - 309
 
6 - 310
    xw.writeEndElement(); /* GetOrdersRequest */
311
    xw.writeEndDocument();
2 - 312
 
6 - 313
    xhr.open('POST', configProxyUrl, true);
314
    xhr.setRequestHeader('Content-Type', 'text/xml');
315
    xhr.setRequestHeader('X-EBAY-API-APP-NAME', configAppid);
316
    xhr.setRequestHeader('X-EBAY-API-COMPATIBILITY-LEVEL', configeBayTradingVersion);
317
    xhr.setRequestHeader('X-EBAY-API-CALL-NAME', 'GetOrders');
318
    xhr.setRequestHeader('X-EBAY-API-SITEID', '0');
319
    xhr.setRequestHeader('X-EBAY-API-DEV-NAME', '');
320
    xhr.setRequestHeader('X-EBAY-API-CERT-NAME', '');
321
    xhr.setRequestHeader('X-Proxy-URL', configServiceEndpoint);
2 - 322
 
6 - 323
    xml = xw.flush();
324
    xw.close();
2 - 325
 
6 - 326
    xhr.onload = () => {
327
        var obj = XMLparse(xhr.responseXML);
2 - 328
 
6 - 329
        var returnCode = obj.Ack;
2 - 330
 
6 - 331
        var x = document.getElementById("results");
332
        if (x.className.indexOf("w3-show") == -1) {
333
            x.className += " w3-show";
334
        }
2 - 335
 
6 - 336
        if (obj.Message) {
337
            x.innerHTML += "<p>" + obj.Message + "</p>";
338
        }
2 - 339
 
6 - 340
        if (returnCode == 'Success' || (returnCode == 'Warning' && obj.Errors.ErrorCode == '21917182')) {
341
            if (getRadioValue('rptType') == 'shippingList') {
342
                createShipmentTable(obj, pageNumber);
343
            } else if (getRadioValue('rptType') == 'trackingList') {
344
                createTrackingTable(obj, pageNumber);
345
            } else if (getRadioValue('rptType') == 'customerList') {
346
                createCustomerTable(obj, pageNumber);
347
            } else {
348
                createOrderTable(obj, pageNumber);
349
            }
2 - 350
 
6 - 351
            x.innerHTML += '<p><strong>' + returnCode + ' (Orders ' + pageNumber + ' / ' + obj.PaginationResult.TotalNumberOfPages + ')</strong></p>';
2 - 352
 
6 - 353
            if (getRadioValue('rptType') == 'orderList') {
354
                printSummary();
355
            }
2 - 356
 
6 - 357
            if (obj.HasMoreOrders == 'true') {
358
                ++pagesToProcess;
359
                ++maxPagesToProcess;
360
                updateProgressBar(maxPagesToProcess, pagesProcessed);
361
                retrieveeBayOrders(pageNumber + 1);
362
            }
2 - 363
 
6 - 364
            document.getElementById("logging").innerHTML = html.join('');
365
            --pagesToProcess;
366
            ++pagesProcessed;
367
            updateProgressBar(maxPagesToProcess, pagesProcessed);
368
        } else {
369
            x.className += " process-errors";
370
            x.innerHTML += "<p><strong>" + returnCode + ":</strong></p>";
2 - 371
 
6 - 372
            var errors = getJsonArray(obj.Errors);
373
            x.innerHTML += "<p>";
374
            for (i = 0; i < errors.length; i++) {
375
                x.innerHTML += errors[i].SeverityCode + " (" + errors[i].ErrorCode + "): " + escapeHtml(errors[i].LongMessage) + "<br/>";
376
                if (errors[i].LongMessage.includes('Auth')) {
377
                    authErrorFlag = true;
378
                }
379
            }
380
            x.innerHTML += "</p>";
2 - 381
 
6 - 382
            if (authErrorFlag === true) {
383
                eBayAuthTokenFlag = false;
384
                x = document.getElementById("login");
385
                if (x.className.indexOf("w3-show") == -1) {
386
                    x.className += " w3-show";
387
                }
388
            }
389
        }
390
    };
2 - 391
 
6 - 392
    xhr.send(xml);
2 - 393
}
394
 
395
function retrieveShopifyOrders(page) {
6 - 396
    var sDate = moment(document.getElementById("startDate").value + " 00:00:00").format("YYYY-MM-DDTHH:mm:ssZ");
397
    var eDate = moment(document.getElementById("endDate").value + " 23:59:59").format("YYYY-MM-DDTHH:mm:ssZ");
2 - 398
 
6 - 399
    var xhttp = new XMLHttpRequest();
400
    xhttp.onreadystatechange = function() {
401
        if (this.readyState == 4 && this.status == 200) {
402
            var json = JSON.parse(this.responseText);
403
            if (json.orders.length > 0) {
404
                retrieveShopifyOrder(json);
405
            } else {
406
                var x = document.getElementById("results");
407
                x.innerHTML += '<p><strong>Report Finished!</strong></p>';
408
                if (!x.className.includes("process-errors")) {
409
                    setTimeout(function() {
410
                        x.className = x.className.replace(" w3-show", "");
411
                    }, 3000);
412
                }
413
            }
414
        }
415
    };
2 - 416
 
6 - 417
    xhttp.open("GET", configProxyUrl, true);
418
    xhttp.setRequestHeader("X-Proxy-Url", encodeURI(configShopifyUrl + configShopifyOrdersUrl + '?page=' + ((page - 1) * configShopifyOrderLimit) + '&limit=' + configShopifyOrderLimit + '&status=any&created_at_min=' + sDate + '&created_at_max=' + eDate));
419
    xhttp.send();
2 - 420
}
421
 
422
function retrieveShopifyOrder(json) {
6 - 423
    var i, j;
424
    var title;
425
    var quantity;
426
    var itemId;
427
    var trackingNumber;
428
    var trackingStatus;
429
    var shipmentState;
430
    var shipmentDate;
2 - 431
 
6 - 432
    for (i = 0; i < json.orders.length; i++) {
433
        html.push('<tr>');
434
        html.push(tableCell(json.orders[i].created_at.substr(0, 10)));
435
        if (json.orders[i].fulfillment_status !== null) {
436
            html.push(tableCell(json.orders[i].fulfillment_status));
437
        } else {
438
            html.push(tableCell('created'));
439
        }
440
        html.push(tableCell(json.orders[i].id + ' (' + json.orders[i].name + ')'));
2 - 441
 
6 - 442
        title = '';
443
        quantity = '';
444
        itemId = '';
445
        for (j = 0; j < json.orders[i].line_items.length; j++) {
446
            itemsSold++;
447
            if (j > 0) {
448
                title += '<br/>';
449
                quantity += '<br/>';
450
                itemId += '<br/>';
451
            }
452
            title += json.orders[i].line_items[j].title;
453
            quantity += json.orders[i].line_items[j].quantity;
454
            itemId += json.orders[i].line_items[j].product_id === null ? '' : json.orders[i].line_items[j].product_id;
455
        }
2 - 456
 
6 - 457
        html.push(tableCell(title));
458
        html.push(tableCell(itemId));
459
        html.push(tableCell('Shopify'));
460
        html.push(tableCell(quantity));
461
        html.push(tableCell('$' + json.orders[i].total_price_usd));
462
        html.push(tableCell('$0.00'));
463
        html.push(tableCell('N/A'));
464
        html.push(tableCell('$0.00'));
465
        html.push(tableCellLabel('Fee' + json.orders[i].id));
466
        html.push(tableCell(json.orders[i].total_tax));
2 - 467
 
6 - 468
        if (json.orders[i].shipping_address !== undefined) {
469
            shipmentState = json.orders[i].shipping_address.province_code;
470
        } else {
471
            shipmentState = configTaxStateId;
472
        }
473
        html.push(tableCell(shipmentState));
474
        html.push(tableCellLabel('PaidDate' + json.orders[i].id));
475
        html.push(tableCell(json.orders[i].payment_gateway_names !== undefined ? json.orders[i].payment_gateway_names.join(', ') : ''));
476
        html.push(tableCellLabel('TransactionId' + json.orders[i].id));
2 - 477
 
6 - 478
        shipmentDate = '';
479
        trackingNumber = '';
480
        trackingStatus = '';
481
        for (j = 0; j < json.orders[i].fulfillments.length; j++) {
482
            if (j > 0) {
483
                trackingNumber += '<br/>';
484
                trackingStatus += '<br/>';
485
                shipmentDate += '<br/>';
486
            }
2 - 487
 
6 - 488
            if (json.orders[i].fulfillments[j].tracking_number !== null) {
489
                trackingNumber = json.orders[i].fulfillments[j].tracking_company + ' ' + json.orders[i].fulfillments[j].tracking_number;
490
                trackingStatus = json.orders[i].fulfillments[j].shipment_status;
491
                shipmentDate += json.orders[i].fulfillments[j].created_at.substr(0, 10);
492
            }
493
        }
2 - 494
 
6 - 495
        html.push(tableCell(shipmentDate));
496
        html.push(tableCell(trackingNumber));
497
        html.push(tableCell(trackingStatus));
498
        html.push('</tr>');
2 - 499
 
6 - 500
        grossSales += Number(json.orders[i].total_price_usd);
501
        VATaxAmount += Number(json.orders[i].total_tax);
502
    }
2 - 503
 
6 - 504
    // xxxxx last steps in the chain
505
    printOrderTotals();
506
    document.getElementById("logging").innerHTML = html.join('');
2 - 507
 
6 - 508
    sortTable("orderTable", 1);
509
    document.getElementById("results").innerHTML += '<p><strong>Retrieving Shipping Costs...</strong></p>';
510
    findShippingCosts();
2 - 511
}
512
 
513
function retrieveShopifyShippings(page) {
6 - 514
    var xhttp = new XMLHttpRequest();
515
    xhttp.onreadystatechange = function() {
516
        if (this.readyState == 4 && this.status == 200) {
517
            //window.alert(this.responseText);
518
            var json = JSON.parse(this.responseText);
519
            if (json.orders[0] !== undefined) {
520
                retrieveShopifyShipping(json);
521
            }
2 - 522
 
6 - 523
            // xxxxx last steps in the chain
524
            printShipmentTotals();
525
            document.getElementById("logging").innerHTML = html.join('');
2 - 526
 
6 - 527
            sortTable("shippingTable", 1);
528
            findThumbNails();
529
        }
530
    };
2 - 531
 
6 - 532
    xhttp.open("GET", configProxyUrl, true);
533
    xhttp.setRequestHeader("X-Proxy-Url", encodeURI(configShopifyUrl + configShopifyOrdersUrl + '?page=' + ((page - 1) * configShopifyOrderLimit) + '&limit=' + configShopifyOrderLimit + '&fulfillment_status=unshipped'));
534
    xhttp.send();
2 - 535
}
536
 
537
function retrieveShopifyShipping(json) {
6 - 538
    var i;
2 - 539
 
6 - 540
    for (i = 0; i < json.orders.length; i++) {
541
        totalAmount += parseFloat(json.orders[i].total_price);
2 - 542
 
6 - 543
        for (j = 0; j < json.orders[i].line_items.length; j++) {
544
            html.push('<tr>');
2 - 545
 
6 - 546
            html.push(tableCellSKU(json.orders[i].line_items[j].sku, json.orders[i].line_items[j].title));
547
            html.push(tableCell(json.orders[i].line_items[j].quantity));
548
            if (json.orders[i].line_items[j].product_id !== null) {
549
                html.push(tableCellAndLabel(json.orders[i].line_items[j].product_id, 'PictureURL' + json.orders[i].line_items[j].product_id));
550
            } else {
551
                html.push(tableCell(''));
552
            }
553
            html.push(tableCell(json.orders[i].line_items[j].title));
554
            html.push(tableCell(json.orders[i].name));
555
            html.push(tableCellDate(json.orders[i].created_at.substr(0, 10)));
556
            html.push(tableCell('$' + parseFloat(json.orders[i].line_items[j].price).toFixed(2)));
557
            html.push(tableCellHidden('Shopify'));
2 - 558
 
6 - 559
            itemsSold += Number(json.orders[i].line_items[j].quantity);
2 - 560
 
6 - 561
            html.push('</tr>');
562
        }
563
    }
2 - 564
}
565
 
566
function retrieveShopifyTrackings(page) {
6 - 567
    var sDate = moment(document.getElementById("startDate").value + " 00:00:00").format("YYYY-MM-DDTHH:mm:ssZ");
568
    var eDate = moment(document.getElementById("endDate").value + " 23:59:59").format("YYYY-MM-DDTHH:mm:ssZ");
2 - 569
 
6 - 570
    var xhttp = new XMLHttpRequest();
571
    xhttp.onreadystatechange = function() {
572
        if (this.readyState == 4 && this.status == 200) {
573
            //window.alert(this.responseText);
574
            var json = JSON.parse(this.responseText);
575
            if (json.orders[0] !== undefined) {
576
                retrieveShopifyTracking(json);
577
            }
2 - 578
 
6 - 579
            // xxxxx last in the chain
580
            printTrackingTotals();
581
            document.getElementById("logging").innerHTML = html.join('');
582
            sortTable("trackingTable", 1);
583
            document.getElementById("results").innerHTML += '<p><strong>Retrieving Shipping Costs...</strong></p>';
584
            findShippingCosts();
585
        }
586
    };
2 - 587
 
6 - 588
    xhttp.open("GET", configProxyUrl, true);
589
    xhttp.setRequestHeader("X-Proxy-Url", encodeURI(configShopifyUrl + configShopifyOrdersUrl + '?page=' + ((page - 1) * configShopifyOrderLimit) + '&limit=' + configShopifyOrderLimit + '&fulfillment_status=shipped&status=closed&created_at_min=' + sDate + '&created_at_max=' + eDate));
590
    xhttp.send();
2 - 591
}
592
 
593
function retrieveShopifyTracking(json) {
6 - 594
    var i, j;
595
    var title;
2 - 596
 
6 - 597
    for (i = 0; i < json.orders.length; i++) {
598
        if (json.orders[i].shipping_lines[0] === undefined) {
599
            continue;
600
        }
2 - 601
 
6 - 602
        html.push('<tr>');
603
        html.push(tableCell(json.orders[i].created_at.substr(0, 10)));
2 - 604
 
6 - 605
        title = '';
606
        for (j = 0; j < json.orders[i].line_items.length; j++) {
607
            if (j > 0) {
608
                title += '<br/>';
609
            }
610
            title += json.orders[i].line_items[j].title;
611
        }
2 - 612
 
6 - 613
        html.push(tableCell(title));
614
        html.push(tableCell('N/A'));
615
        html.push(tableCellAndLabel(json.orders[i].shipping_address.city + ', ' + json.orders[i].shipping_address.province_code + ' ' + json.orders[i].shipping_address.zip, 'DeliveryAddress' + json.orders[i].fulfillments[0].tracking_number));
616
        html.push(tableCell(json.orders[i].fulfillments[0].created_at.substr(0, 10))); // best guess
617
        html.push(tableCell(json.orders[i].fulfillments[0].created_at.substr(0, 10)));
618
        html.push(tableCellAndLabel(json.orders[i].fulfillments[0].tracking_company + ' ' + json.orders[i].fulfillments[0].tracking_number, json.orders[i].fulfillments[0].tracking_number));
619
        html.push(tableCell(getCarrierService(json.orders[i].fulfillments[0].tracking_company, json.orders[i].fulfillments[0].tracking_number, true)));
620
        html.push(tableCellLabel('DeliveryDate' + json.orders[i].fulfillments[0].tracking_number));
621
        html.push(tableCellLabel('DeliveryDays' + json.orders[i].fulfillments[0].tracking_number));
622
        html.push(tableCellLabel('DeliveryStatus' + json.orders[i].fulfillments[0].tracking_number));
2 - 623
 
6 - 624
        html.push('</tr>');
625
    }
2 - 626
}
627
 
628
function createOrderTable(obj, pageNumber) {
6 - 629
    var i;
630
    var title;
631
    var trackingNumber;
632
    var quantity;
633
    var finalValueFee;
634
    var finalValueFeeAmount;
635
    var itemId;
636
    var taxAmount;
637
    var payAmount;
2 - 638
 
6 - 639
    if (pageNumber == 1) {
640
        document.getElementById("logging").innerHTML = '';
641
        html = [];
2 - 642
 
6 - 643
        html.push('<h3>Order List from ' + document.getElementById("startDate").value + ' to ' + document.getElementById("endDate").value + '</h3>');
644
        html.push('<div class="w3-responsive">');
645
        html.push('<table id="orderTable" class="w3-table-all w3-tiny w3-hoverable">');
646
        html.push('<thead>');
647
        html.push('<tr>');
2 - 648
 
6 - 649
        html.push(tableHeader('Order Date'));
650
        html.push(tableHeader('Order Status'));
651
        html.push(tableHeader('Order Id'));
652
        html.push(tableHeader('Ttitle'));
653
        html.push(tableHeader('Item Id'));
654
        html.push(tableHeader('Sales Channel'));
655
        html.push(tableHeader('Quantity Purchased'));
656
        html.push(tableHeader('Payment Amount'));
657
        html.push(tableHeader('Shipping Received'));
658
        html.push(tableHeader('Shipping Cost'));
659
        html.push(tableHeader('eBay Final Value Fee'));
660
        html.push(tableHeader('Payment Processor Fee'));
661
        html.push(tableHeader('Sales Tax Amount'));
662
        html.push(tableHeader('Ship To State'));
663
        html.push(tableHeader('Paid Date'));
664
        html.push(tableHeader('Payment Methods'));
2 - 665
 
6 - 666
        html.push(tableHeader('Payment Transaction ID'));
667
        html.push(tableHeader('Shipped Date'));
668
        html.push(tableHeader('Shipping Tracking Number'));
669
        html.push(tableHeader('Delivery Status'));
2 - 670
 
6 - 671
        html.push('</tr>');
672
        html.push('</thead>');
673
        html.push('<tbody>');
674
    }
2 - 675
 
6 - 676
    var orders = getJsonArray(obj.OrderArray);
677
    for (var entry = 0; entry < orders.length; entry++) {
678
        html.push('<tr>');
2 - 679
 
6 - 680
        html.push(tableCellDate(orders[entry].CreatedTime));
681
        html.push(tableCell(orders[entry].OrderStatus));
682
        html.push(tableCell(orders[entry].OrderID));
2 - 683
 
6 - 684
        title = '';
685
        trackingNumber = '';
686
        quantity = '';
687
        finalValueFee = '';
688
        finalValueFeeAmount = 0.0;
689
        itemId = '';
2 - 690
 
6 - 691
        var transactions = getJsonArray(orders[entry].TransactionArray);
692
        for (i = 0; i < transactions.length; i++) {
693
            itemsSold++;
694
            if (i > 0) {
695
                title += '<br/>';
696
                quantity += '<br/>';
697
                itemId += '<br/>';
698
            }
699
            title += transactions[i].Item.Title;
700
            quantity += transactions[i].QuantityPurchased;
701
            finalValueFeeAmount += parseFloat(transactions[i].FinalValueFee.text);
702
            eBayFees += parseFloat(transactions[i].FinalValueFee.text);
703
            itemId += transactions[i].Item.ItemID;
704
        }
705
        html.push(tableCell(title));
706
        html.push(tableCell(itemId));
707
        finalValueFee = '$' + finalValueFeeAmount.toFixed(2);
2 - 708
 
6 - 709
        var trackingDetails = getJsonArray(transactions[0].ShippingDetails.ShipmentTrackingDetails);
710
        for (i = 0; i < trackingDetails.length; i++) {
711
            trackingNumber += (i > 0 ? '<br/>' : '') + trackingDetails[i].ShippingCarrierUsed + ' ' + trackingDetails[i].ShipmentTrackingNumber;
712
        }
2 - 713
 
6 - 714
        html.push(tableCell('eBay'));
715
        html.push(tableCell(quantity));
716
        html.push(tableCell('$' + parseFloat(orders[entry].AmountPaid.text).toFixed(2)));
717
        grossSales += parseFloat(orders[entry].AmountPaid.text);
2 - 718
 
6 - 719
        html.push(tableCell('$' + parseFloat(orders[entry].ShippingServiceSelected.ShippingServiceCost.text).toFixed(2)));
720
        shippingRcvd += parseFloat(orders[entry].ShippingServiceSelected.ShippingServiceCost.text);
2 - 721
 
6 - 722
        html.push(tableCellLabel('ShippingCost' + orders[entry].OrderID));
723
        html.push(tableCell(finalValueFee));
2 - 724
 
6 - 725
        if (orders[entry].ExternalTransaction !== undefined && orders[entry].ExternalTransaction.FeeOrCreditAmount !== undefined) {
726
            html.push(tableCell('$' + parseFloat(orders[entry].ExternalTransaction.FeeOrCreditAmount.text).toFixed(2)));
727
            PayPalFees += parseFloat(orders[entry].ExternalTransaction.FeeOrCreditAmount.text);
728
        } else {
729
            html.push(tableCell(''));
730
        }
2 - 731
 
6 - 732
        if (orders[entry].ShippingAddress.StateOrProvince == configTaxStateId && orders[entry].ShippingDetails.SalesTax.SalesTaxAmount.text == "0.0") {
733
            payAmount = parseFloat(orders[entry].AmountPaid.text);
734
            taxAmount = payAmount - (payAmount / (1.00 + (configTaxRate / 100)));
735
            html.push(tableCell('$' + taxAmount.toFixed(2)));
736
            VATaxAmount += taxAmount;
737
        } else {
738
            html.push(tableCell('$' + parseFloat(orders[entry].ShippingDetails.SalesTax.SalesTaxAmount.text).toFixed(2)));
739
            VATaxAmount += parseFloat(orders[entry].ShippingDetails.SalesTax.SalesTaxAmount.text);
740
        }
741
        html.push(tableCell(orders[entry].ShippingAddress.StateOrProvince));
742
        VATaxGross += (parseFloat(orders[entry].AmountPaid.text) - parseFloat(orders[entry].ShippingDetails.SalesTax.SalesTaxAmount.text));
743
        if (orders[entry].ShippingAddress.StateOrProvince != configTaxStateId) {
744
            VATaxExempt += parseFloat(orders[entry].AmountPaid.text);
745
        }
2 - 746
 
6 - 747
        if (orders[entry].PaidTime !== undefined) {
748
            html.push(tableCellDate(orders[entry].PaidTime));
749
        } else {
750
            html.push(tableCell(''));
751
        }
752
        html.push(tableCell(orders[entry].PaymentMethods));
2 - 753
 
6 - 754
        if (orders[entry].ExternalTransaction !== undefined && orders[entry].ExternalTransaction.ExternalTransactionID !== undefined) {
755
            html.push(tableCell(orders[entry].ExternalTransaction.ExternalTransactionID));
756
        } else {
757
            html.push(tableCell(''));
758
        }
2 - 759
 
6 - 760
        if (orders[entry].ShippedTime === undefined) {
761
            html.push(tableCell(''));
762
        } else {
763
            html.push(tableCellDate(orders[entry].ShippedTime));
764
        }
765
        html.push(tableCell(trackingNumber));
766
        html.push(tableCellLabel('Delivery' + orders[entry].OrderID));
767
 
768
        html.push('</tr>');
769
    }
2 - 770
}
771
 
772
function printOrderTotals() {
6 - 773
    html.push('</tbody>');
774
    html.push('<tfoot>');
775
    html.push('<tr>');
776
    html.push(tableCell('<strong>Totals</strong>'));
777
    html.push(tableCell(''));
778
    html.push(tableCell(''));
779
    html.push(tableCell(''));
780
    html.push(tableCell(''));
781
    html.push(tableCell(''));
782
    html.push(tableCell('<strong>' + itemsSold + '</strong>'));
783
    html.push(tableCell('<strong>$' + grossSales.toFixed(2) + '</strong>'));
784
    html.push(tableCell('<strong>$' + shippingRcvd.toFixed(2) + '</strong>'));
785
    html.push(tableCellLabel('ShippingTotal'));
786
    html.push(tableCell('<strong>$' + eBayFees.toFixed(2) + '</strong>'));
787
    html.push(tableCell('<strong>$' + PayPalFees.toFixed(2) + '</strong>'));
788
    html.push(tableCell('<strong>$' + VATaxAmount.toFixed(2) + '</strong>'));
789
    html.push(tableCell(''));
790
    html.push(tableCell(''));
791
    html.push(tableCell(''));
792
    html.push(tableCell(''));
793
    html.push(tableCell(''));
794
    html.push(tableCell(''));
795
    html.push(tableCell(''));
796
    html.push('</tr>');
797
    html.push('</tfoot>');
798
    html.push('</table>');
799
    html.push('</div>');
2 - 800
}
801
 
802
function createCustomerTable(obj, pageNumber) {
6 - 803
    var i;
804
    var firstName;
805
    var lastName;
806
    var emailAddress;
2 - 807
 
6 - 808
    if (pageNumber == 1) {
809
        document.getElementById("logging").innerHTML = '';
810
        html = [];
2 - 811
 
6 - 812
        html.push('<h3>Customer List from ' + document.getElementById("startDate").value + ' to ' + document.getElementById("endDate").value + '</h3>');
813
        html.push('<div class="w3-responsive">');
814
        html.push('<table id="customerTable" class="w3-table-all w3-tiny w3-hoverable">');
815
        html.push('<thead>');
816
        html.push('<tr>');
2 - 817
 
6 - 818
        html.push(tableHeader('First Name'));
819
        html.push(tableHeader('Last Name'));
820
        html.push(tableHeader('Email'));
821
        html.push(tableHeader('Company'));
822
        html.push(tableHeader('Address1'));
823
        html.push(tableHeader('Address2'));
824
        html.push(tableHeader('City'));
825
        html.push(tableHeader('Province'));
826
        html.push(tableHeader('Province Code'));
827
        html.push(tableHeader('Country'));
828
        html.push(tableHeader('Country Code'));
829
        html.push(tableHeader('Zip'));
830
        html.push(tableHeader('Phone'));
831
        html.push(tableHeader('Accepts Marketing'));
832
        html.push(tableHeader('Total Spent'));
833
        html.push(tableHeader('Total Orders'));
834
        html.push(tableHeader('Tags'));
835
        html.push(tableHeader('Note'));
836
        html.push(tableHeader('Tax Exempt'));
2 - 837
 
6 - 838
        html.push('</tr>');
839
        html.push('</thead>');
840
        html.push('<tbody>');
841
    }
2 - 842
 
6 - 843
    var orders = getJsonArray(obj.OrderArray);
844
    for (var entry = 0; entry < orders.length; entry++) {
845
        html.push('<tr>');
2 - 846
 
6 - 847
        firstName = '';
848
        lastName = '';
849
        emailAddress = '';
2 - 850
 
6 - 851
        var transactions = getJsonArray(orders[entry].TransactionArray);
852
        for (i = 0; i < transactions.length; i++) {
853
            firstName = transactions[i].Buyer.UserFirstName;
854
            lastName = transactions[i].Buyer.UserLastName;
855
            emailAddress = transactions[i].Buyer.Email;
856
        }
2 - 857
 
6 - 858
        html.push(tableCell(firstName.toProperCase()));
859
        html.push(tableCell(lastName.toProperCase()));
860
        html.push(tableCell(emailAddress != 'Invalid Request' ? emailAddress : ""));
861
        html.push(tableCell(''));
862
        html.push(tableCell(orders[entry].ShippingAddress.Street1.toProperCase()));
863
        html.push(tableCell(orders[entry].ShippingAddress.Street2.toProperCase()));
864
        html.push(tableCell(orders[entry].ShippingAddress.CityName.toProperCase()));
865
        html.push(tableCell(''));
866
        html.push(tableCell(orders[entry].ShippingAddress.StateOrProvince));
867
        html.push(tableCell(orders[entry].ShippingAddress.CountryName.toProperCase()));
868
        html.push(tableCell(orders[entry].ShippingAddress.Country));
869
        html.push(tableCell(orders[entry].ShippingAddress.PostalCode));
870
        html.push(tableCell(orders[entry].ShippingAddress.Phone));
871
        html.push(tableCell('no'));
872
        html.push(tableCell(''));
873
        html.push(tableCell(''));
874
        html.push(tableCell('ebay'));
875
        html.push(tableCell(orders[entry].BuyerUserID));
876
        html.push(tableCell('no'));
2 - 877
 
6 - 878
        html.push('</tr>');
879
    }
2 - 880
 
881
 
6 - 882
    if (obj.HasMoreOrders != 'true') {
883
        html.push('</tbody>');
884
        html.push('<tfoot>');
885
        html.push('<tr>');
886
        html.push(tableCell(''));
887
        html.push('</tr>');
888
        html.push('</tfoot>');
889
        html.push('</table>');
890
        html.push('</div>');
891
    }
2 - 892
}
893
 
894
function createShipmentTable(obj, pageNumber) {
6 - 895
    var i;
2 - 896
 
6 - 897
    if (pageNumber == 1) {
898
        totalAmount = 0.00;
899
        itemsSold = 0;
2 - 900
 
6 - 901
        document.getElementById("logging").innerHTML = '';
902
        html = [];
2 - 903
 
6 - 904
        html.push('<h3>Awaiting Shipment List</h3>');
905
        html.push('<table id="shippingTable" class="w3-table-all w3-small">');
906
        html.push('<thead>');
907
        html.push('<tr>');
2 - 908
 
6 - 909
        html.push(tableHeader('SKU'));
910
        html.push(tableHeader('Quantity'));
911
        html.push(tableHeader('Thumbnail'));
912
        html.push(tableHeader('Ttitle'));
913
        html.push(tableHeader('Item Id'));
914
        html.push(tableHeader('Date'));
915
        html.push(tableHeader('Payment Amount'));
916
        html.push(tableHeaderHidden('Sales Channel'));
2 - 917
 
6 - 918
        html.push('</tr>');
919
        html.push('</thead>');
920
        html.push('<tbody>');
921
    }
2 - 922
 
6 - 923
    var orders = getJsonArray(obj.OrderArray);
924
    for (var entry = 0; entry < orders.length; entry++) {
925
        if (orders[entry].OrderStatus == 'Completed' && orders[entry].ShippedTime === undefined && orders[entry].PaidTime !== undefined) {
926
            var transactions = getJsonArray(orders[entry].TransactionArray);
927
            for (i = 0; i < transactions.length; i++) {
928
                itemsSold++;
2 - 929
 
10 - 930
                var title = getJsonValue(transactions[i].Item.Title);
931
                var buyerMessage = getJsonValue(orders[entry].BuyerCheckoutMessage);
932
                if (buyerMessage.length > 0) {
933
                    title += '<br/><br/>Buyer Message: ' + buyerMessage;
934
                }
935
 
6 - 936
                html.push('<tr>');
2 - 937
 
6 - 938
                html.push(tableCellSKU(transactions[i].Item.SKU, transactions[i].Item.Title));
939
                html.push(tableCell(transactions[i].QuantityPurchased));
940
                html.push(tableCellLabel('PictureURL' + transactions[i].Item.ItemID));
10 - 941
                html.push(tableCell(title));
6 - 942
                html.push(tableCell(transactions[i].Item.ItemID));
943
                html.push(tableCellDate(orders[entry].CreatedTime));
10 - 944
                html.push(tableCell('$' + parseFloat(orders[entry].AmountPaid.text).toFixed(2) + (transactions.length > 1 ?'*' : '')));
6 - 945
                if (i === 0) {
946
                    totalAmount += parseFloat(orders[entry].AmountPaid.text);
947
                }
948
                html.push(tableCellHidden('eBay'));
2 - 949
 
6 - 950
                html.push('</tr>');
951
            }
952
        }
953
    }
2 - 954
}
955
 
956
function printShipmentTotals() {
6 - 957
    html.push('</tbody>');
958
    html.push('<tfoot>');
959
    html.push('<tr>');
960
    html.push(tableCell('<strong>Totals</strong>'));
961
    html.push(tableCell(''));
962
    html.push(tableCell(''));
963
    html.push(tableCell(''));
964
    html.push(tableCell(''));
965
    html.push(tableCell('<strong>' + itemsSold + '</strong>'));
966
    html.push(tableCell('<strong>$' + totalAmount.toFixed(2) + '</strong>'));
967
    html.push(tableCellHidden(''));
968
    html.push('</tr>');
969
    html.push('</tfoot>');
970
    html.push('</table>');
2 - 971
}
972
 
973
function createTrackingTable(obj, pageNumber) {
6 - 974
    var i;
975
    var title;
976
    var trackingNumber;
977
    var service;
978
    var quantity;
979
    var html_temp;
980
    var finalValueFee;
981
    var finalValueFeeAmount;
982
    var itemId;
2 - 983
 
6 - 984
    if (pageNumber == 1) {
985
        document.getElementById("logging").innerHTML = '';
986
        html = [];
987
        trackingList = [];
988
        trackingListDedupe = [];
2 - 989
 
6 - 990
        html.push('<h3>Tracking Number Status from ' + document.getElementById("startDate").value + ' to ' + document.getElementById("endDate").value + '</h3>');
991
        html.push('<div class="w3-responsive">');
992
        html.push('<table id="trackingTable" class="w3-table-all w3-tiny w3-hoverable">');
993
        html.push('<thead>');
994
        html.push('<tr>');
2 - 995
 
6 - 996
        html.push(tableHeader('Order Date'));
997
        html.push(tableHeader('Ttitle'));
998
        html.push(tableHeaderHidden('Order Id'));
999
        html.push(tableHeader('Shipping Cost'));
1000
        html.push(tableHeader('Shipping Address'));
1001
        html.push(tableHeader('Paid Date'));
1002
        html.push(tableHeader('Shipped Date'));
1003
        html.push(tableHeader('Shipping Tracking Number'));
1004
        html.push(tableHeader('Service'));
1005
        html.push(tableHeader('Delivery Date'));
1006
        html.push(tableHeader('Duration (Days)'));
1007
        html.push(tableHeader('Delivery Status'));
2 - 1008
 
6 - 1009
        html.push('</tr>');
1010
        html.push('</thead>');
1011
        html.push('<tbody>');
1012
    }
2 - 1013
 
6 - 1014
    var orders = getJsonArray(obj.OrderArray);
1015
    for (var entry = 0; entry < orders.length; entry++) {
1016
        html_temp = [];
2 - 1017
 
6 - 1018
        html_temp.push('<tr>');
2 - 1019
 
6 - 1020
        html_temp.push(tableCellDate(orders[entry].CreatedTime));
2 - 1021
 
6 - 1022
        title = '';
1023
        trackingNumber = '';
1024
        service = '';
1025
        quantity = '';
1026
        finalValueFee = '';
1027
        finalValueFeeAmount = 0.0;
1028
        itemId = '';
2 - 1029
 
6 - 1030
        var transactions = getJsonArray(orders[entry].TransactionArray);
1031
        for (title = '', i = 0; i < transactions.length; i++) {
1032
            if (i > 0) {
1033
                title += '<br/>';
1034
                quantity += '<br/>';
1035
                itemId += '<br/>';
1036
            }
1037
            title += transactions[i].Item.Title;
1038
        }
1039
        html_temp.push(tableCell(title));
2 - 1040
 
6 - 1041
        var trackingDetails = getJsonArray(transactions[0].ShippingDetails.ShipmentTrackingDetails);
1042
        // only last 1
1043
        for (i = 0; i < trackingDetails.length; i++) {
1044
            trackingNumber = trackingDetails[i].ShippingCarrierUsed + ' ' + trackingDetails[i].ShipmentTrackingNumber;
1045
            service = getCarrierService(trackingDetails[i].ShippingCarrierUsed, trackingDetails[i].ShipmentTrackingNumber, true);
1046
        }
2 - 1047
 
6 - 1048
        html_temp.push(tableCellHidden(orders[entry].OrderID));
1049
        html_temp.push(tableCellLabel('ShippingCost' + orders[entry].OrderID));
1050
        html_temp.push(tableCellAndLabel(orders[entry].ShippingAddress.CityName + ', ' + orders[entry].ShippingAddress.StateOrProvince + ' ' + orders[entry].ShippingAddress.PostalCode, 'DeliveryAddress' + trackingNumber.substr(trackingNumber.indexOf(' ') + 1)));
2 - 1051
 
6 - 1052
        if (orders[entry].PaidTime !== undefined) {
1053
            html_temp.push(tableCellDate(orders[entry].PaidTime));
1054
        } else {
1055
            html_temp.push(tableCell(''));
1056
        }
2 - 1057
 
6 - 1058
        if (orders[entry].ShippedTime === undefined) {
1059
            html_temp.push(tableCellLabel('ShippedDate' + trackingNumber.substr(trackingNumber.indexOf(' ') + 1)));
1060
        } else {
1061
            html_temp.push(tableCellDate(orders[entry].ShippedTime));
1062
        }
2 - 1063
 
6 - 1064
        html_temp.push(tableCellAndLabel(trackingNumber, trackingNumber.substr(trackingNumber.indexOf(' ') + 1)));
1065
        html_temp.push(tableCell(service));
1066
        html_temp.push(tableCellLabel('DeliveryDate' + trackingNumber.substr(trackingNumber.indexOf(' ') + 1)));
1067
        html_temp.push(tableCellLabel('DeliveryDays' + trackingNumber.substr(trackingNumber.indexOf(' ') + 1)));
1068
        html_temp.push(tableCellLabel('DeliveryStatus' + trackingNumber.substr(trackingNumber.indexOf(' ') + 1)));
2 - 1069
 
6 - 1070
        html_temp.push('</tr>');
1071
 
1072
        if (trackingListDedupe.indexOf(trackingNumber.substr(trackingNumber.indexOf(' ') + 1)) == -1) {
1073
            html.push(html_temp.join(''));
1074
            trackingListDedupe.push(trackingNumber.substr(trackingNumber.indexOf(' ') + 1));
1075
        }
1076
    }
2 - 1077
}
1078
 
1079
function printTrackingTotals() {
6 - 1080
    html.push('</tbody>');
1081
    html.push('<tfoot class="w3-hide">');
1082
    html.push('<tr>');
1083
    html.push(tableCell(''));
1084
    html.push('</tr>');
1085
    html.push('</tfoot>');
1086
    html.push('</table>');
1087
    html.push('</div>');
2 - 1088
}
1089
 
1090
function getCarrierService(carrier, trackingNumber, flag) {
6 - 1091
    if (flag && trackingList.indexOf(trackingNumber) == -1) {
1092
        trackingList.push(trackingNumber);
1093
    }
2 - 1094
 
6 - 1095
    if (carrier == "USPS") {
1096
        if (trackingNumber.startsWith("9449")) {
1097
            return ("Media Mail");
1098
        } else if (trackingNumber.startsWith("9450")) {
1099
            return ("Media Mail, Insured");
1100
        } else if (trackingNumber.startsWith("94001")) {
1101
            return ("First Class");
1102
        } else if (trackingNumber.startsWith("9416")) {
1103
            return ("First Class, Insured");
1104
        } else if (trackingNumber.startsWith("92055")) {
1105
            return ("Priority Mail");
1106
        } else if (trackingNumber.startsWith("94073")) {
1107
            return ("Certified Mail");
1108
        } else if (trackingNumber.startsWith("93033")) {
1109
            return ("Collect on Delivery");
1110
        } else if (trackingNumber.startsWith("82")) {
1111
            return ("Global Express");
1112
        } else if (trackingNumber.startsWith("EC")) {
1113
            return ("Priority Mail Express International");
1114
        } else if (trackingNumber.startsWith("92701") || trackingNumber.startsWith("EA")) {
1115
            return ("Priority Mail Express");
1116
        } else if (trackingNumber.startsWith("CP")) {
1117
            return ("Priority Mail  International");
1118
        } else if (trackingNumber.startsWith("92088")) {
1119
            return ("Registered Mail");
1120
        } else if (trackingNumber.startsWith("92021")) {
1121
            return ("Signature Confirmation");
1122
        } else if (trackingNumber.startsWith("9461") || trackingNumber.startsWith("9462")) {
1123
            return ("Parcel Select Ground");
1124
        } else if (trackingNumber.startsWith("92021")) {
1125
            return ("Signature Confirmation");
1126
        }
1127
    } else if (carrier == "UPS") {
1128
        if (trackingNumber.startsWith("1Z")) {
1129
            var service = trackingNumber.substr(8, 2);
2 - 1130
 
6 - 1131
            if (service == '01') {
1132
                return ('Next Day Air ("Red")');
1133
            } else if (service == '02') {
1134
                return ('UPS United States Second Day Air ("Blue")');
1135
            } else if (service == '03') {
1136
                return ('Ground');
1137
            } else if (service == '12') {
1138
                return ('Third Day Select');
1139
            } else if (service == '13') {
1140
                return ('Next Day Air Saver ("Red Saver")');
1141
            } else if (service == '15') {
1142
                return ('Next Day Air Early A.M.');
1143
            } else if (service == '22') {
1144
                return ('Ground - Returns Plus - Three Pickup Attempts');
1145
            } else if (service == '32') {
1146
                return ('Next Day Air Early A.M. - COD');
1147
            } else if (service == '33') {
1148
                return ('Next Day Air Early A.M. - Saturday Delivery, COD');
1149
            } else if (service == '41') {
1150
                return ('Next Day Air Early A.M. - Saturday Delivery');
1151
            } else if (service == '42') {
1152
                return ('Ground - Signature Required');
1153
            } else if (service == '44') {
1154
                return ('Next Day Air - Saturday Delivery');
1155
            } else if (service == '66') {
1156
                return ('Worldwide Express');
1157
            } else if (service == '72') {
1158
                return ('Ground - Collect on Delivery');
1159
            } else if (service == '78') {
1160
                return ('Ground - Returns Plus - One Pickup Attempt');
1161
            } else if (service == '90') {
1162
                return ('Ground - Returns - UPS Prints and Mails Label');
1163
            } else if (service == 'A0') {
1164
                return ('Next Day Air Early A.M. - Adult Signature Required');
1165
            } else if (service == 'A1') {
1166
                return ('Next Day Air Early A.M. - Saturday Delivery, Adult Signature Required');
1167
            } else if (service == 'A2') {
1168
                return ('Next Day Air - Adult Signature Required');
1169
            } else if (service == 'A8') {
1170
                return ('Ground - Adult Signature Required');
1171
            } else if (service == 'A9') {
1172
                return ('Next Day Air Early A.M. - Adult Signature Required, COD');
1173
            } else if (service == 'AA') {
1174
                return ('Next Day Air Early A.M. - Saturday Delivery, Adult Signature Required, COD');
1175
            }
1176
        }
1177
    }
2 - 1178
 
6 - 1179
    return ("");
2 - 1180
}
1181
 
1182
function retrieveFees(pageNumber) {
6 - 1183
    var i;
1184
    var xml;
1185
    var authErrorFlag = false;
2 - 1186
 
6 - 1187
    if (eBayAuthTokenFlag === false) {
1188
        return;
1189
    }
2 - 1190
 
6 - 1191
    var valueStartDate = document.getElementById('startDate').value + "T00:00:00.000Z";
1192
    var valueEndDate = document.getElementById('endDate').value + "T23:59:59.999Z";
2 - 1193
 
6 - 1194
    var xw = new XMLWriter('UTF-8', '1.0');
1195
    var xhr = new XMLHttpRequest();
2 - 1196
 
6 - 1197
    xw.writeStartDocument();
1198
    xw.writeStartElement("GetAccountRequest");
1199
    xw.writeAttributeString('xmlns', 'urn:ebay:apis:eBLBaseComponents');
2 - 1200
 
6 - 1201
    xw.writeStartElement('RequesterCredentials');
1202
    xw.writeElementString('eBayAuthToken', eBayAuthToken);
1203
    xw.writeEndElement(); /* RequesterCredentials */
2 - 1204
 
6 - 1205
    xw.writeElementString('AccountEntrySortType', 'AccountEntryCreatedTimeAscending');
1206
    xw.writeElementString('AccountHistorySelection', 'BetweenSpecifiedDates');
1207
    xw.writeElementString('BeginDate', valueStartDate);
1208
    xw.writeElementString('EndDate', valueEndDate);
1209
    xw.writeElementString('ExcludeBalance', 'true');
1210
    xw.writeElementString('ExcludeSummary', 'true');
2 - 1211
 
6 - 1212
    xw.writeStartElement('Pagination');
1213
    xw.writeElementString('EntriesPerPage', configXmlRequestEntriesPerPage);
1214
    xw.writeElementString('PageNumber', pageNumber.toString());
1215
    xw.writeEndElement(); /* Pagination */
2 - 1216
 
6 - 1217
    xw.writeElementString('ErrorLanguage', 'en_US');
1218
    xw.writeElementString('Version', configeBayTradingVersion);
1219
    xw.writeElementString('WarningLevel', configWarningLevel);
2 - 1220
 
6 - 1221
    xw.writeEndElement(); /* GetAccountRequest */
1222
    xw.writeEndDocument();
2 - 1223
 
6 - 1224
    xhr.open('POST', configProxyUrl, true);
1225
    xhr.setRequestHeader('Content-Type', 'text/xml');
1226
    xhr.setRequestHeader('X-EBAY-API-APP-NAME', configAppid);
1227
    xhr.setRequestHeader('X-EBAY-API-COMPATIBILITY-LEVEL', configeBayTradingVersion);
1228
    xhr.setRequestHeader('X-EBAY-API-CALL-NAME', 'GetAccount');
1229
    xhr.setRequestHeader('X-EBAY-API-SITEID', '0');
1230
    xhr.setRequestHeader('X-EBAY-API-DEV-NAME', '');
1231
    xhr.setRequestHeader('X-EBAY-API-CERT-NAME', '');
1232
    xhr.setRequestHeader('X-Proxy-URL', configServiceEndpoint);
2 - 1233
 
6 - 1234
    xml = xw.flush();
1235
    xw.close();
2 - 1236
 
6 - 1237
    xhr.onload = () => {
1238
        var obj = XMLparse(xhr.responseXML);
2 - 1239
 
6 - 1240
        var returnCode = obj.Ack;
2 - 1241
 
6 - 1242
        var x = document.getElementById("results");
1243
        if (x.className.indexOf("w3-show") == -1) {
1244
            x.className += " w3-show";
1245
        }
2 - 1246
 
6 - 1247
        if (obj.Message) {
1248
            x.innerHTML += "<p>" + obj.Message + "</p>";
1249
        }
2 - 1250
 
6 - 1251
        if (returnCode == 'Success' || (returnCode == 'Warning' && obj.Errors.ErrorCode == '21917182')) {
1252
            createFeeTable(obj, pageNumber);
2 - 1253
 
6 - 1254
            x.innerHTML += '<p><strong>' + returnCode + ' (Fees/Credits ' + pageNumber + ' / ' + obj.PaginationResult + ')</strong></p>';
2 - 1255
 
6 - 1256
            if (obj.HasMoreEntries == 'true') {
1257
                ++pagesToProcess;
1258
                ++maxPagesToProcess;
1259
                updateProgressBar(maxPagesToProcess, pagesProcessed);
1260
                retrieveFees(pageNumber + 1);
1261
            }
2 - 1262
 
6 - 1263
            document.getElementById("logging").innerHTML = html.join('');
2 - 1264
 
6 - 1265
            --pagesToProcess;
1266
            ++pagesProcessed;
1267
            updateProgressBar(maxPagesToProcess, pagesProcessed);
1268
        } else {
1269
            x.className += " process-errors";
1270
            x.innerHTML += "<p><strong>" + returnCode + ":</strong></p>";
2 - 1271
 
6 - 1272
            var errors = getJsonArray(obj.Errors);
1273
            x.innerHTML += "<p>";
1274
            for (i = 0; i < errors.length; i++) {
1275
                x.innerHTML += errors[i].SeverityCode + " (" + errors[i].ErrorCode + "): " + escapeHtml(errors[i].LongMessage) + "<br/>";
1276
                if (errors[i].LongMessage.includes('Auth')) {
1277
                    authErrorFlag = true;
1278
                }
1279
            }
1280
            x.innerHTML += "</p>";
2 - 1281
 
6 - 1282
            if (authErrorFlag === true) {
1283
                eBayAuthTokenFlag = false;
1284
                x = document.getElementById("login");
1285
                if (x.className.indexOf("w3-show") == -1) {
1286
                    x.className += " w3-show";
1287
                }
1288
            }
2 - 1289
 
6 - 1290
            --pagesToProcess;
1291
            ++pagesProcessed;
1292
            updateProgressBar(maxPagesToProcess, pagesProcessed);
1293
        }
1294
    };
2 - 1295
 
6 - 1296
    xhr.send(xml);
2 - 1297
 
6 - 1298
    function checkpagesToProcess() {
1299
        if (pagesToProcess > 0) {
1300
            window.setTimeout(checkpagesToProcess, 100); // wait 100 milliseconds
1301
        } else {
1302
            sortTable("feeTable", 1);
1303
            x = document.getElementById("results");
1304
            x.innerHTML += '<p><strong>Report Finished!</strong></p>';
2 - 1305
 
6 - 1306
            endProgressBar();
1307
 
1308
            var y = document.getElementById("dlFeeListButton");
1309
            if (y.className.indexOf("w3-show") == -1) {
1310
                y.className += " w3-show";
1311
            }
1312
 
1313
            if (!x.className.includes("process-errors")) {
1314
                setTimeout(function() {
1315
                    x.className = x.className.replace(" w3-show", "");
1316
                }, 3000);
1317
            }
1318
        }
1319
    }
1320
 
1321
    if (pageNumber == 1) {
1322
        checkpagesToProcess();
1323
    }
2 - 1324
}
1325
 
1326
function createFeeTable(obj, pageNumber) {
6 - 1327
    var i;
2 - 1328
 
6 - 1329
    if (pageNumber == 1) {
1330
        totalAmount = 0;
2 - 1331
 
6 - 1332
        document.getElementById("logging").innerHTML = '';
1333
        html = [];
2 - 1334
 
6 - 1335
        html.push('<h3>Fees and Credits List from ' + document.getElementById("startDate").value + ' to ' + document.getElementById("endDate").value + '</h3>');
1336
        html.push('<table id="feeTable" class="w3-table-all w3-small">');
1337
        html.push('<thead>');
1338
        html.push('<tr>');
2 - 1339
 
6 - 1340
        html.push(tableHeader('Item ID'));
1341
        html.push(tableHeader('Title'));
1342
        html.push(tableHeader('Date'));
1343
        html.push(tableHeader('Fee Type'));
1344
        html.push(tableHeader('Amount'));
2 - 1345
 
6 - 1346
        html.push('</tr>');
1347
        html.push('</thead>');
1348
        html.push('<tbody>');
1349
    }
2 - 1350
 
6 - 1351
    for (i = 0; i < obj.AccountEntries.length; i++) {
1352
        if ((!obj.AccountEntries[i].Description.includes('Payment') && !obj.AccountEntries[i].Description.includes('Final Value Fee') && obj.AccountEntries[i].GrossDetailAmount.text != '0.0' && obj.AccountEntries[i].NetDetailAmount.text != '0.0') ||
1353
            (obj.AccountEntries[i].Description.includes('Final Value Fee') && obj.AccountEntries[i].NetDetailAmount.text.substr(0, 1) == '-')) {
1354
            html.push('<tr>');
2 - 1355
 
6 - 1356
            html.push(tableCell(obj.AccountEntries[i].ItemID));
1357
            html.push(tableCell(obj.AccountEntries[i].Title));
1358
            html.push(tableCellDate(obj.AccountEntries[i].Date));
1359
            html.push(tableCell(obj.AccountEntries[i].Description));
1360
            html.push(tableCell('$' + parseFloat(obj.AccountEntries[i].GrossDetailAmount.text).toFixed(2)));
1361
            totalAmount += parseFloat(obj.AccountEntries[i].GrossDetailAmount.text);
2 - 1362
 
6 - 1363
            html.push('</tr>');
1364
        }
1365
    }
2 - 1366
 
6 - 1367
    if (obj.HasMoreEntries != 'true') {
1368
        html.push('</tbody>');
1369
        html.push('<tfoot>');
1370
        html.push('<tr>');
1371
        html.push(tableCell('<strong>Totals</strong>'));
1372
        html.push(tableCell(''));
1373
        html.push(tableCell(''));
1374
        html.push(tableCell(''));
1375
        html.push(tableCell('<strong>$' + totalAmount.toFixed(2) + '</strong>'));
1376
        html.push('</tr>');
1377
        html.push('</tfoot>');
1378
        html.push('</table>');
1379
    }
2 - 1380
}
1381
 
1382
 
1383
function tableCellDate(utcDate) {
6 - 1384
    var localDate = new Date(utcDate);
1385
    return ('<td>' + localDate.yyyymmdd() + '</td>');
2 - 1386
}
1387
 
1388
function tableCellSKU(str, title) {
6 - 1389
    if (str === undefined) {
1390
        str = '';
1391
    }
2 - 1392
 
6 - 1393
    if (str.indexOf(" - ") > 0) {
1394
        str = str.substr(0, str.indexOf(" - "));
1395
    }
2 - 1396
 
6 - 1397
    if (str.startsWith("Batch")) {
1398
        var batchNo = Number(str.substr(6));
1399
        str = "Batch " + pad(batchNo, 2);
1400
    }
2 - 1401
 
6 - 1402
    if (title.includes('(DVD') || title.includes('(HD') || title.includes('(Blu')) {
1403
        str = 'DVD ' + str;
1404
    } else if (title.includes('(CD')) {
1405
        str = 'CD ' + str;
1406
    } else if (title.includes('Hardcover') || title.includes('Paperback')) {
1407
        str = 'Book ' + str;
1408
    }
2 - 1409
 
6 - 1410
    return ('<td>' + str + '</td>');
2 - 1411
}
1412
 
1413
function sortTable(tableName, column) {
6 - 1414
    var table, rows, switching, i, x, y, shouldSwitch;
1415
    --column; // column parameter starts with 1
1416
    table = document.getElementById(tableName);
1417
    switching = true;
1418
    /*Make a loop that will continue until
1419
    no switching has been done:*/
1420
    while (switching) {
1421
        //start by saying: no switching is done:
1422
        switching = false;
1423
        rows = table.getElementsByTagName("TR");
1424
        /*Loop through all table rows (except the
1425
        first, which contains table headers, and the last
1426
        which contains the table footers):*/
1427
        for (i = 1; i < (rows.length - 2); i++) {
1428
            //start by saying there should be no switching:
1429
            shouldSwitch = false;
1430
            /*Get the two elements you want to compare,
1431
            one from current row and one from the next:*/
1432
            x = rows[i].getElementsByTagName("TD")[column];
1433
            y = rows[i + 1].getElementsByTagName("TD")[column];
1434
            //check if the two rows should switch place:
1435
            if (x !== undefined && x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
1436
                //if so, mark as a switch and break the loop:
1437
                shouldSwitch = true;
1438
                break;
1439
            }
1440
        }
1441
        if (shouldSwitch) {
1442
            /*If a switch has been marked, make the switch
1443
            and mark that a switch has been done:*/
1444
            rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
1445
            switching = true;
1446
        }
1447
    }
2 - 1448
}
1449
 
1450
function printData() {
6 - 1451
    var printContent = document.getElementById("printTable");
2 - 1452
 
6 - 1453
    var num;
1454
    var cssReference = [];
1455
    var uniqueName = new Date();
2 - 1456
 
6 - 1457
    var windowName = 'Print' + uniqueName.getTime();
1458
    var printWindow = window.open(num, windowName, 'left=50000,top=50000,width=0,height=0');
1459
    cssReference[0] = printWindow.document.createElement("link");
1460
    cssReference[0].href = "https://www.w3schools.com/w3css/4/w3.css";
1461
    cssReference[0].rel = "stylesheet";
1462
    cssReference[0].type = "text/css";
1463
    cssReference[1] = printWindow.document.createElement("link");
1464
    cssReference[1].href = "css/style.css";
1465
    cssReference[1].rel = "stylesheet";
1466
    cssReference[1].type = "text/css";
2 - 1467
 
6 - 1468
    printWindow.document.write(printContent.outerHTML);
1469
    printWindow.document.getElementsByTagName('head')[0].appendChild(cssReference[0]);
1470
    printWindow.document.getElementsByTagName('head')[0].appendChild(cssReference[1]);
2 - 1471
 
6 - 1472
    printWindow.document.close();
1473
    printWindow.focus();
1474
    printWindow.print();
1475
    printWindow.close();
2 - 1476
}
1477
 
1478
function printSummary() {
6 - 1479
    var profit = grossSales + shippingRcvd - shippingCost - eBayFees - PayPalFees - VATaxAmount;
2 - 1480
 
7 - 1481
    var x = document.getElementById("summary");
1482
    if (x.className.indexOf(" w3-card-2") == -1) {
1483
        x.className += "  w3-card-2";
1484
    }
1485
 
1486
    x.innerHTML = '<h2>Profit: ' + profit.toFixed(2) + '</h3>';
2 - 1487
}
1488
 
1489
function findThumbNails() {
6 - 1490
    var i;
1491
    var x = document.getElementById("shippingTable");
2 - 1492
 
6 - 1493
    thumbnailsToProcess = 0;
1494
    thumbnailsProcessed = 0;
1495
    maxThumbnailsToProcess = 0;
1496
    initProgressBar("");
2 - 1497
 
6 - 1498
    for (i = 1; i < (x.rows.length - 1); i++) {
1499
        if (x.rows[i].cells[7].innerHTML == 'eBay' && x.rows[i].cells[2] !== undefined && x.rows[i].cells[2].innerHTML.length < 1) {
1500
            ++thumbnailsToProcess;
1501
            ++maxThumbnailsToProcess;
1502
            updateProgressBar(maxThumbnailsToProcess, thumbnailsProcessed);
1503
            getThumbNail(x.rows[i].cells[4].innerHTML);
1504
        } else if (x.rows[i].cells[7].innerHTML == 'Shopify' && x.rows[i].cells[2] !== undefined && x.rows[i].cells[2].innerHTML.length > 0) {
1505
            ++thumbnailsToProcess;
1506
            ++maxThumbnailsToProcess;
1507
            updateProgressBar(maxThumbnailsToProcess, thumbnailsProcessed);
1508
            getShopifyThumbNail(x.rows[i].cells[2].innerHTML);
1509
        }
1510
    }
2 - 1511
 
6 - 1512
    function checkthumbnailsToProcess() {
1513
        if (thumbnailsToProcess > 0) {
1514
            window.setTimeout(checkthumbnailsToProcess, 100); // wait 100 milliseconds
1515
        } else {
1516
            endProgressBar();
1517
            x = document.getElementById("results");
1518
            x.innerHTML += '<p><strong>Report Finished!</strong></p>';
1519
            if (!x.className.includes("process-errors")) {
1520
                setTimeout(function() {
1521
                    x.className = x.className.replace(" w3-show", "");
1522
                }, 3000);
1523
            }
1524
        }
1525
    }
2 - 1526
 
6 - 1527
    checkthumbnailsToProcess();
2 - 1528
}
1529
 
1530
function getThumbNail(itemId) {
6 - 1531
    // Construct the getSingleItem request
1532
    url = configeBayShopping + "?";
1533
    url += "callname=GetSingleItem";
1534
    url += "&responseencoding=JSON";
1535
    url += "&appid=" + configAppid;
1536
    url += "&version=" + configeBayShoppingVersion;
1537
    url += "&ItemID=";
1538
    url += itemId;
2 - 1539
 
6 - 1540
    var xhttp = new XMLHttpRequest();
2 - 1541
 
6 - 1542
    xhttp.onreadystatechange = function() {
1543
        if (this.readyState == 4) {
1544
            _cb_getThumbNail(JSON.parse(this.responseText));
1545
        }
1546
    };
2 - 1547
 
6 - 1548
    xhttp.open("GET", configProxyUrl, true);
1549
    xhttp.setRequestHeader("X-Proxy-Url", encodeURI(url));
1550
    xhttp.send();
2 - 1551
}
1552
 
1553
function _cb_getThumbNail(root) {
6 - 1554
    var ack = root.Ack;
2 - 1555
 
6 - 1556
    if (ack == 'Failure' || ack == 'PartialFailure') {
1557
        return;
1558
    }
2 - 1559
 
6 - 1560
    var JsonObj = typeof root.Item != 'object' ? JSON.parse(root.Item) : root.Item;
1561
    var ItemID = JsonObj.ItemID;
1562
    var PictureURL = getJsonArray(JsonObj.PictureURL);
2 - 1563
 
6 - 1564
    document.getElementById('PictureURL' + ItemID).innerHTML = '<img src="' + PictureURL[0] + '" style="max-height:100px">';
1565
    --thumbnailsToProcess;
1566
    ++thumbnailsProcessed;
1567
    updateProgressBar(maxThumbnailsToProcess, thumbnailsProcessed);
2 - 1568
}
1569
 
1570
function getShopifyThumbNail(productId) {
6 - 1571
    var xhttp = new XMLHttpRequest();
1572
    xhttp.onreadystatechange = function() {
1573
        if (this.readyState == 4 && this.status == 200) {
1574
            //window.alert(this.responseText);
1575
            var json = JSON.parse(this.responseText);
1576
            if (json.product !== undefined) {
1577
                document.getElementById('PictureURL' + productId).innerHTML = '<img src="' + json.product.image.src + '" style="max-height:100px">';
1578
            }
1579
            --thumbnailsToProcess;
1580
            ++thumbnailsProcessed;
1581
            updateProgressBar(maxThumbnailsToProcess, thumbnailsProcessed);
1582
        }
1583
    };
2 - 1584
 
6 - 1585
    xhttp.open("GET", configProxyUrl, true);
1586
    xhttp.setRequestHeader("X-Proxy-Url", encodeURI(configShopifyUrl + 'products/' + productId + '.json?fields=image'));
1587
    xhttp.send();
2 - 1588
}
1589
 
1590
function findShippingCosts() {
6 - 1591
    var i;
1592
    var x;
2 - 1593
 
6 - 1594
    shippingsToProcess = 0;
1595
    shippingsProcessed = 0;
1596
    maxShippingsToProcess = 0;
1597
    initProgressBar("Retrieving Shipping Costs...");
2 - 1598
 
6 - 1599
    if (getRadioValue('rptType') == 'trackingList') {
1600
        x = document.getElementById("trackingTable");
1601
    } else {
1602
        x = document.getElementById("orderTable");
1603
    }
2 - 1604
 
6 - 1605
    for (i = (x.rows.length - 1); i > 0; i--) {
1606
        if (getRadioValue('rptType') == 'trackingList') {
1607
            if (x.rows[i].cells[3] !== undefined && x.rows[i].cells[3].innerHTML.length < 1) {
1608
                if (x.rows[i].cells[7] !== undefined && x.rows[i].cells[7].innerHTML.length < 1) {
1609
                    document.getElementById("trackingTable").deleteRow(i);
1610
                } else {
1611
                    ++shippingsToProcess;
1612
                    ++maxShippingsToProcess;
1613
                    updateProgressBar(maxShippingsToProcess, shippingsProcessed);
1614
                    getShippingCost(x.rows[i].cells[2].innerHTML);
1615
                }
1616
            }
1617
        } else {
1618
            if (x.rows[i].cells[5].innerHTML == 'eBay' && x.rows[i].cells[9] !== undefined && x.rows[i].cells[9].innerHTML.length < 1) {
1619
                ++shippingsToProcess;
1620
                ++maxShippingsToProcess;
1621
                updateProgressBar(maxShippingsToProcess, shippingsProcessed);
1622
                getShippingCost(x.rows[i].cells[2].innerHTML);
1623
            } else if (x.rows[i].cells[5].innerHTML == 'Shopify') {
1624
                ++shippingsToProcess;
1625
                ++maxShippingsToProcess;
1626
                updateProgressBar(maxShippingsToProcess, shippingsProcessed);
1627
                getPaymentTransaction(x.rows[i].cells[2].innerHTML);
1628
            }
1629
        }
1630
    }
2 - 1631
 
6 - 1632
    function checkshippingsToProcess() {
1633
        if (shippingsToProcess > 0) {
1634
            window.setTimeout(checkshippingsToProcess, 100); // wait 100 milliseconds
1635
        } else {
1636
            endProgressBar();
1637
            x = document.getElementById("results");
1638
            if (getRadioValue('rptType') != 'trackingList') {
1639
                document.getElementById("ShippingTotal").innerHTML = '<strong>$' + shippingCost.toFixed(2) + '</strong>';
1640
                x.innerHTML += '<p><strong>Report Finished!</strong></p>';
1641
                if (!x.className.includes("process-errors")) {
1642
                    setTimeout(function() {
1643
                        x.className = x.className.replace(" w3-show", "");
1644
                    }, 3000);
1645
                }
1646
            } else {
1647
                x.innerHTML += '<p><strong>Retrieving Carrier Tracking...</strong></p>';
1648
                getTrackingDetails();
1649
            }
1650
        }
1651
    }
1652
 
1653
    checkshippingsToProcess();
2 - 1654
}
1655
 
1656
function getShippingCost(OrderId) {
6 - 1657
    var i;
1658
    var xml;
1659
    var correlationId;
1660
    var shippingCostStr;
1661
    var deliveryStatus;
1662
    var authErrorFlag = false;
2 - 1663
 
6 - 1664
    var xw = new XMLWriter('UTF-8', '1.0');
1665
    var xhr = new XMLHttpRequest();
2 - 1666
 
6 - 1667
    xw.writeStartDocument();
1668
    xw.writeStartElement("GetSellingManagerSaleRecordRequest");
1669
    xw.writeAttributeString('xmlns', 'urn:ebay:apis:eBLBaseComponents');
2 - 1670
 
6 - 1671
    xw.writeStartElement('RequesterCredentials');
1672
    xw.writeElementString('eBayAuthToken', eBayAuthToken);
1673
    xw.writeEndElement(); /* RequesterCredentials */
2 - 1674
 
6 - 1675
    xw.writeElementString('OrderID', OrderId);
1676
    xw.writeElementString('MessageID', OrderId);
2 - 1677
 
6 - 1678
    xw.writeElementString('ErrorLanguage', 'en_US');
1679
    xw.writeElementString('Version', configeBayTradingVersion);
1680
    xw.writeElementString('WarningLevel', configWarningLevel);
2 - 1681
 
6 - 1682
    xw.writeEndElement(); /* GetSellingManagerSaleRecordRequest */
1683
    xw.writeEndDocument();
2 - 1684
 
6 - 1685
    xhr.open('POST', configProxyUrl, true);
1686
    xhr.setRequestHeader('Content-Type', 'text/xml');
1687
    xhr.setRequestHeader('X-EBAY-API-APP-NAME', configAppid);
1688
    xhr.setRequestHeader('X-EBAY-API-COMPATIBILITY-LEVEL', configeBayTradingVersion);
1689
    xhr.setRequestHeader('X-EBAY-API-CALL-NAME', 'GetSellingManagerSaleRecord');
1690
    xhr.setRequestHeader('X-EBAY-API-SITEID', '0');
1691
    xhr.setRequestHeader('X-EBAY-API-DEV-NAME', '');
1692
    xhr.setRequestHeader('X-EBAY-API-CERT-NAME', '');
1693
    xhr.setRequestHeader('X-Proxy-URL', configServiceEndpoint);
2 - 1694
 
6 - 1695
    xml = xw.flush();
1696
    xw.close();
2 - 1697
 
6 - 1698
    xhr.onload = () => {
1699
        var obj = XMLparse(xhr.responseXML);
2 - 1700
 
6 - 1701
        var returnCode = obj.Ack;
2 - 1702
 
6 - 1703
        var x = document.getElementById("results");
1704
        if (x.className.indexOf("w3-show") == -1) {
1705
            x.className += " w3-show";
1706
        }
2 - 1707
 
6 - 1708
        if (obj.Message) {
1709
            x.innerHTML += "<p>" + obj.Message + "</p>";
1710
        }
2 - 1711
 
6 - 1712
        if (returnCode == 'Success' || (returnCode == 'Warning' && obj.Errors.ErrorCode == '21917182')) {
1713
            correlationId = obj.CorrelationID;
1714
            deliveryStatus = '';
1715
            shippingCostStr = '';
2 - 1716
 
6 - 1717
            if (obj.SellingManagerSoldOrder.ActualShippingCost !== undefined &&
1718
                obj.SellingManagerSoldOrder.ActualShippingCost.text != '0.0' &&
1719
                document.getElementById('ShippingCost' + correlationId).innerHTML.length < 1) {
1720
                shippingCostStr = '$' + parseFloat(obj.SellingManagerSoldOrder.ActualShippingCost.text).toFixed(2);
1721
                shippingCost += parseFloat(obj.SellingManagerSoldOrder.ActualShippingCost.text);
1722
                document.getElementById('ShippingCost' + correlationId).innerHTML = shippingCostStr;
1723
                if (getRadioValue('rptType') == 'orderList') {
1724
                    printSummary();
1725
                }
1726
            }
2 - 1727
 
6 - 1728
            if (getRadioValue('rptType') != 'trackingList') {
1729
                var soldTransactions = getJsonArray(obj.SellingManagerSoldOrder.SellingManagerSoldTransaction);
1730
                for (i = 0; i < soldTransactions.length; i++) {
1731
                    if (soldTransactions[i].Shipment.DeliveryStatus !== undefined) {
1732
                        deliveryStatus += (i > 0 ? '<br/>' : '') + soldTransactions[i].Shipment.DeliveryStatus;
1733
                    }
1734
                }
1735
                document.getElementById('Delivery' + correlationId).innerHTML = deliveryStatus;
1736
            }
1737
        } else {
1738
            x.className += " process-errors";
1739
            x.innerHTML += "<p><strong>" + returnCode + ":</strong></p>";
2 - 1740
 
6 - 1741
            var errors = getJsonArray(obj.Errors);
1742
            x.innerHTML += "<p>";
1743
            for (i = 0; i < errors.length; i++) {
1744
                x.innerHTML += errors[i].SeverityCode + " (" + errors[i].ErrorCode + "): " + escapeHtml(errors[i].LongMessage) + "<br/>";
1745
                if (errors[i].LongMessage.includes('Auth')) {
1746
                    authErrorFlag = true;
1747
                }
1748
            }
1749
            x.innerHTML += "</p>";
2 - 1750
 
6 - 1751
            if (authErrorFlag === true) {
1752
                eBayAuthTokenFlag = false;
1753
                x = document.getElementById("login");
1754
                if (x.className.indexOf("w3-show") == -1) {
1755
                    x.className += " w3-show";
1756
                }
1757
            }
1758
        }
2 - 1759
 
6 - 1760
        --shippingsToProcess;
1761
        ++shippingsProcessed;
1762
        updateProgressBar(maxShippingsToProcess, shippingsProcessed);
1763
    };
2 - 1764
 
6 - 1765
    xhr.send(xml);
2 - 1766
}
1767
 
1768
function getPaymentTransaction(fullId) {
6 - 1769
    var id = fullId.substr(0, fullId.indexOf(' '));
1770
    var i;
1771
    var transFee = 0.00;
1772
    var transFeeStr = '';
1773
    var transDate = '';
1774
    var transId = '';
2 - 1775
 
6 - 1776
    var xhttp = new XMLHttpRequest();
1777
    xhttp.onreadystatechange = function() {
1778
        if (this.readyState == 4 && this.status == 200) {
1779
            //window.alert(this.responseText);
1780
            var json = JSON.parse(this.responseText);
10 - 1781
            var transactions = getJsonArray(json.transactions);
1782
            for (i = 0; i < transactions.length; i++) {
1783
                transDate = getJsonValue(transactions[i].created_at).substr(0, 10);
1784
                document.getElementById('PaidDate' + id).innerHTML = transDate;
2 - 1785
 
10 - 1786
                var gateway = getJsonValue(transactions[i].gateway);
1787
                if (gateway == 'shopify_payments') {
1788
                    transId = getJsonValue(transactions[i].id);
1789
                    transFee = Number(getJsonValue(transactions[i].receipt.fee_amount)) / 100;
1790
                } else if (gateway == 'paypal') {
1791
                    transId = getJsonValue(transactions[i].authorization);
1792
                    transFee = Number(getJsonValue(transactions[i].receipt.fee_amount));
1793
                } else if (gateway == 'manual') {
1794
                    transId = getJsonValue(json.transactions[i].id);
1795
                }
2 - 1796
 
10 - 1797
                document.getElementById('TransactionId' + id).innerHTML = transId;
2 - 1798
 
10 - 1799
                transFeeStr = '$' + parseFloat(transFee).toFixed(2);
1800
                PayPalFees += transFee;
1801
                document.getElementById('Fee' + id).innerHTML = transFeeStr;
6 - 1802
            }
2 - 1803
 
6 - 1804
            --shippingsToProcess;
1805
            ++shippingsProcessed;
1806
            updateProgressBar(maxShippingsToProcess, shippingsProcessed);
1807
        }
1808
    };
2 - 1809
 
6 - 1810
    xhttp.open("GET", configProxyUrl, true);
1811
    xhttp.setRequestHeader("X-Proxy-Url", encodeURI(configShopifyUrl + configShopifyTransactionsUrl1 + id + configShopifyTransactionsUrl2));
1812
    xhttp.send();
2 - 1813
}
1814
 
1815
function getTrackingDetails() {
6 - 1816
    var i;
1817
    var x;
2 - 1818
 
6 - 1819
    trackingNumbersToProcess = 0;
1820
    trackingNumbersProcessed = 0;
1821
    maxTrackingNumbersToProcess = 0;
1822
    initProgressBar("Retrieving Carrier Tracking...");
2 - 1823
 
6 - 1824
    for (i = 0; i < trackingList.length; i++) {
1825
        ++trackingNumbersToProcess;
1826
        ++maxTrackingNumbersToProcess;
1827
        if (trackingList[i].substr(0, 2) == '1Z') {
1828
            getUPSTracking(trackingList[i]);
1829
        } else {
1830
            getUSPSTracking(trackingList[i]);
1831
        }
1832
    }
2 - 1833
 
6 - 1834
    function checkTrackingsToProcess() {
1835
        if (trackingNumbersToProcess > 0) {
1836
            window.setTimeout(checkTrackingsToProcess, 100); // wait 100 milliseconds
1837
        } else {
1838
            endProgressBar();
1839
            x = document.getElementById("results");
1840
            x.innerHTML += '<p><strong>Report Finished!</strong></p>';
1841
            if (!x.className.includes("process-errors")) {
1842
                setTimeout(function() {
1843
                    x.className = x.className.replace(" w3-show", "");
1844
                }, 3000);
1845
            }
1846
        }
1847
    }
2 - 1848
 
6 - 1849
    checkTrackingsToProcess();
2 - 1850
}
1851
 
1852
function getUSPSTracking(searchNumber) {
6 - 1853
    var j;
1854
    var str;
1855
    var url;
1856
    var trackingNumber;
1857
    var summary;
1858
    var detail;
1859
    var deliveryDate;
1860
    var createDate;
1861
    var duration;
1862
    var lastUpdate;
1863
    var lastEvent;
1864
    var footer;
2 - 1865
 
6 - 1866
    var xhttp = new XMLHttpRequest();
1867
    xhttp.onreadystatechange = function() {
1868
        if (this.readyState == 4 && this.status == 200) {
1869
            var obj = XMLparse(this.responseXML);
2 - 1870
 
6 - 1871
            if (obj.TrackSummary === undefined) {
1872
                --trackingNumbersToProcess;
1873
                ++trackingNumbersProcessed;
1874
                updateProgressBar(maxTrackingNumbersToProcess, trackingNumbersProcessed);
1875
                return;
1876
            }
2 - 1877
 
6 - 1878
            trackingNumber = obj.ID;
1879
            summary = formatUSPSTrackingLine(obj.TrackSummary);
1880
            detail = '';
1881
            footer = '';
1882
            deliveryDate = '';
1883
            duration = '';
1884
            lastUpdate = summary.substr(8, 10);
1885
            lastEvent = obj.TrackSummary.Event;
2 - 1886
 
6 - 1887
            if (document.getElementById('DeliveryStatus' + trackingNumber) === null) {
1888
                --trackingNumbersToProcess;
1889
                ++trackingNumbersProcessed;
1890
                updateProgressBar(maxTrackingNumbersToProcess, trackingNumbersProcessed);
1891
                return;
1892
            }
2 - 1893
 
6 - 1894
            if (obj.TrackDetail !== undefined && obj.TrackDetail.length > 0) {
1895
                for (j = 0; j < obj.TrackDetail.length; j++) {
1896
                    detail += formatUSPSTrackingLine(obj.TrackDetail[j]);
1897
                }
2 - 1898
 
6 - 1899
                createDate = moment(obj.TrackDetail[(obj.TrackDetail.length - 1)].EventDate, "MMMM DD, YYYY").format('YYYY-MM-DD');
1900
                footer = 'Enroute since ' + createDate + ' (' + moment().diff(createDate, 'days') + ' days)';
2 - 1901
 
6 - 1902
                if (summary.includes("Delivered")) {
1903
                    deliveryDate = summary.substr(8, 10);
1904
                    duration = moment(deliveryDate).diff(createDate, 'days');
1905
                    document.getElementById('DeliveryStatus' + trackingNumber).style.color = "green";
1906
                    footer = 'Delivered in ' + moment(deliveryDate).to(createDate, 'days');
1907
                } else if (moment().diff(lastUpdate, 'days') > 3) {
1908
                    document.getElementById('DeliveryStatus' + trackingNumber).style.color = "red";
1909
                }
1910
            }
2 - 1911
 
6 - 1912
            document.getElementById('DeliveryStatus' + trackingNumber).innerHTML = lastEvent;
1913
            document.getElementById('DeliveryDays' + trackingNumber).innerHTML = duration;
1914
            document.getElementById('DeliveryDate' + trackingNumber).innerHTML = deliveryDate;
2 - 1915
 
6 - 1916
            str = document.getElementById(trackingNumber).innerHTML;
1917
            if (!str.startsWith('<button')) {
1918
                document.getElementById(trackingNumber).innerHTML = '<button onclick="document.getElementById(\'mod' + trackingNumber + '\').style.display=\'block\'" class="w3-button w3-black">' + str + '</button>';
2 - 1919
 
6 - 1920
                str = '  <div id="mod' + trackingNumber + '" class="w3-modal">';
1921
                str += '    <div class="w3-modal-content w3-card-4">';
1922
                str += '      <header class="w3-container w3-black"> ';
1923
                str += '        <span onclick="document.getElementById(\'mod' + trackingNumber + '\').style.display=\'none\'"';
1924
                str += '        class="w3-button w3-display-topright">&times;</span>';
1925
                str += '        <h3>' + trackingNumber + ' (USPS ' + getCarrierService("USPS", trackingNumber, false) + ')</h3>';
1926
                str += '      </header>';
1927
                str += '      <div class="w3-container">';
1928
                str += '         <table class="w3-table-all w3-small">';
1929
                str += '			<tr>';
1930
                str += '			<th>DATE</th>';
1931
                str += '			<th>TIME</th>';
1932
                str += '			<th>STATUS</th>';
1933
                str += '			<th>LOCATION</th>';
1934
                str += '			</tr>';
1935
                str += summary;
1936
                str += detail;
1937
                str += '         </table>';
1938
                str += '      </div>';
1939
                str += '      <footer class="w3-container w3-black">';
1940
                str += '        <p>' + document.getElementById('DeliveryAddress' + trackingNumber).innerHTML + ' - ' + footer + '</p>';
1941
                str += '      </footer>';
1942
                str += '    </div>';
1943
                str += '  </div>';
2 - 1944
 
6 - 1945
                document.getElementById("modals").innerHTML += str;
1946
            }
2 - 1947
 
6 - 1948
            --trackingNumbersToProcess;
1949
            ++trackingNumbersProcessed;
1950
            updateProgressBar(maxTrackingNumbersToProcess, trackingNumbersProcessed);
1951
        }
1952
    };
2 - 1953
 
6 - 1954
    url = configUSPSUrl + '?API=TrackV2&XML=';
1955
    url += '<TrackFieldRequest USERID="';
1956
    url += configUSPSUserId;
1957
    url += '">';
1958
    url += '<TrackID ID="' + searchNumber + '"></TrackID>';
1959
    url += '</TrackFieldRequest>';
1960
    url = url.replace(/ /g, '%20');
2 - 1961
 
6 - 1962
    xhttp.open("GET", configProxyUrl, true);
1963
    xhttp.setRequestHeader("X-Proxy-Url", encodeURI(url));
1964
    xhttp.send();
2 - 1965
}
1966
 
1967
function formatUSPSTrackingLine(t) {
6 - 1968
    var str = '';
2 - 1969
 
6 - 1970
    if (t !== undefined) {
1971
        str = '<tr>';
2 - 1972
 
6 - 1973
        str += '<td>' + moment(t.EventDate, "MMMM DD, YYYY").format('YYYY-MM-DD') + '</td>';
2 - 1974
 
6 - 1975
        str += '<td>';
1976
        if (t.EventTime.length > 0) {
1977
            str += ' ' + moment(t.EventTime, "hh:mm a").format('HH:mm');
1978
        }
1979
        str += '</td>';
2 - 1980
 
6 - 1981
        str += '<td>' + t.Event + '</td>';
2 - 1982
 
6 - 1983
        str += '<td>';
1984
        if (t.EventCity.length > 0) {
1985
            str += t.EventCity;
1986
            if (t.EventState.length > 0) {
1987
                str += ", " + t.EventState + ' ' + t.EventZIPCode;
1988
            }
1989
        }
1990
        str += '</td>';
2 - 1991
 
6 - 1992
        str += '</tr>';
1993
    }
2 - 1994
 
6 - 1995
    return (str);
2 - 1996
}
1997
 
1998
function getUPSTracking(trackingNumber) {
6 - 1999
    var xhr = new XMLHttpRequest();
2 - 2000
 
6 - 2001
    var xml = '<?xml version="1.0"?>';
2002
    xml += '<AccessRequest xml:lang="en-US">';
2003
    xml += '<AccessLicenseNumber>' + configUPSAccessKey + '</AccessLicenseNumber>';
2004
    xml += '<UserId>' + configUPSUsername + '</UserId>';
2005
    xml += '<Password>' + configUPSPassword + '</Password>';
2006
    xml += '</AccessRequest>';
2007
    xml += '<?xml version="1.0"?>';
2008
    xml += '<TrackRequest xml:lang="en-US">';
2009
    xml += '<Request>';
2010
    xml += '<TransactionReference>';
2011
    xml += '<CustomerContext>' + 'MUNA Trading' + '</CustomerContext>';
2012
    xml += '</TransactionReference>';
2013
    xml += '<RequestAction>Track</RequestAction>';
2014
    xml += '<RequestOption>1</RequestOption>';
2015
    xml += '</Request>';
2016
    xml += '<TrackingNumber>' + trackingNumber + '</TrackingNumber>';
2017
    xml += '</TrackRequest>';
2 - 2018
 
6 - 2019
    xhr.onload = () => {
2020
        var obj = XMLparse(xhr.responseXML);
2 - 2021
 
6 - 2022
        if (obj.Response.ResponseStatusDescription != 'Success') {
2023
            --trackingNumbersToProcess;
2024
            ++trackingNumbersProcessed;
2025
            updateProgressBar(maxTrackingNumbersToProcess, trackingNumbersProcessed);
2026
            return;
2027
        }
2 - 2028
 
6 - 2029
        trackingNumber = obj.Shipment.ShipmentIdentificationNumber;
2030
        summary = formatUPSTrackingLine(obj.Shipment.Package.Activity[0]);
2031
        detail = '';
2032
        footer = '';
2033
        deliveryDate = '';
2034
        duration = '';
2035
        lastUpdate = moment(obj.Shipment.Package.Activity[0].Status.Date, "YYYYMMDD").format('YYYY-MM-DD');
2036
        lastEvent = obj.Shipment.Package.Activity[0].Status.StatusType.Description;
2 - 2037
 
6 - 2038
        if (document.getElementById('DeliveryStatus' + trackingNumber) === null) {
2039
            --trackingNumbersToProcess;
2040
            ++trackingNumbersProcessed;
2041
            updateProgressBar(maxTrackingNumbersToProcess, trackingNumbersProcessed);
2042
            return;
2043
        }
2 - 2044
 
6 - 2045
        if (obj.Shipment.Package.Activity !== undefined && obj.Shipment.Package.Activity.length > 0) {
2046
            for (j = 0; j < obj.Shipment.Package.Activity.length; j++) {
2047
                detail += formatUPSTrackingLine(obj.Shipment.Package.Activity[j]);
2048
            }
2 - 2049
 
6 - 2050
            createDate = moment(obj.Shipment.PickupDate, "YYYYMMDD").format('YYYY-MM-DD');
2051
            footer = 'Enroute since ' + createDate + ' (' + moment().diff(createDate, 'days') + ' days)';
2 - 2052
 
6 - 2053
            if (obj.Shipment.Package.DeliveryIndicator == 'Y') {
2054
                deliveryDate = moment(obj.Shipment.Package.DeliveryDate, "YYYYMMDD").format('YYYY-MM-DD');
2055
                duration = moment(deliveryDate).diff(createDate, 'days');
2056
                document.getElementById('DeliveryStatus' + trackingNumber).style.color = "green";
2057
                footer = 'Delivered in ' + moment(deliveryDate).to(createDate, 'days');
2058
            } else if (moment().diff(lastUpdate, 'days') > 3) {
2059
                document.getElementById('DeliveryStatus' + trackingNumber).style.color = "red";
2060
            }
2061
        }
2 - 2062
 
6 - 2063
        document.getElementById('ShippedDate' + trackingNumber).innerHTML = createDate;
2064
        document.getElementById('DeliveryStatus' + trackingNumber).innerHTML = lastEvent;
2065
        document.getElementById('DeliveryDays' + trackingNumber).innerHTML = duration;
2066
        document.getElementById('DeliveryDate' + trackingNumber).innerHTML = deliveryDate;
2 - 2067
 
6 - 2068
        str = document.getElementById(trackingNumber).innerHTML;
2069
        if (!str.startsWith('<button')) {
2070
            document.getElementById(trackingNumber).innerHTML = '<button onclick="document.getElementById(\'mod' + trackingNumber + '\').style.display=\'block\'" class="w3-button w3-black">' + str + '</button>';
2 - 2071
 
6 - 2072
            str = '  <div id="mod' + trackingNumber + '" class="w3-modal">';
2073
            str += '    <div class="w3-modal-content w3-card-4">';
2074
            str += '      <header class="w3-container w3-black"> ';
2075
            str += '        <span onclick="document.getElementById(\'mod' + trackingNumber + '\').style.display=\'none\'"';
2076
            str += '        class="w3-button w3-display-topright">&times;</span>';
2077
            str += '        <h3>' + trackingNumber + ' (UPS ' + getCarrierService("UPS", trackingNumber, false) + ')</h3>';
2078
            str += '      </header>';
2079
            str += '      <div class="w3-container">';
2080
            str += '         <table class="w3-table-all w3-small">';
2081
            str += '			<tr>';
2082
            str += '			<th>DATE</th>';
2083
            str += '			<th>TIME</th>';
2084
            str += '			<th>STATUS</th>';
2085
            str += '			<th>LOCATION</th>';
2086
            str += '			</tr>';
2087
            str += summary;
2088
            str += detail;
2089
            str += '         </table>';
2090
            str += '      </div>';
2091
            str += '      <footer class="w3-container w3-black">';
2092
            str += '        <p>' + document.getElementById('DeliveryAddress' + trackingNumber).innerHTML + ' - ' + footer + '</p>';
2093
            str += '      </footer>';
2094
            str += '    </div>';
2095
            str += '  </div>';
2 - 2096
 
6 - 2097
            document.getElementById("modals").innerHTML += str;
2098
        }
2 - 2099
 
6 - 2100
        --trackingNumbersToProcess;
2101
        ++trackingNumbersProcessed;
2102
        updateProgressBar(maxTrackingNumbersToProcess, trackingNumbersProcessed);
2103
    };
2 - 2104
 
6 - 2105
    xhr.open('POST', configProxyUrl, true);
2106
    xhr.setRequestHeader('Content-Type', 'text/xml');
2107
    xhr.setRequestHeader('X-Proxy-URL', configUPSUrl);
2108
    xhr.send(xml);
2 - 2109
}
2110
 
2111
function formatUPSTrackingLine(t) {
6 - 2112
    var str = '';
2 - 2113
 
6 - 2114
    if (t !== undefined) {
2115
        str = '<tr>';
2 - 2116
 
6 - 2117
        str += '<td>' + moment(t.Date, "YYYYMMDD").format('YYYY-MM-DD') + '</td>';
2 - 2118
 
6 - 2119
        str += '<td>';
2120
        if (t.Time.length > 0) {
2121
            str += ' ' + moment(t.Time, "HHmmss").format('HH:mm');
2122
        }
2123
        str += '</td>';
2 - 2124
 
6 - 2125
        str += '<td>' + t.Status.StatusType.Description + '</td>';
2 - 2126
 
6 - 2127
        str += '<td>';
2128
        if (t.ActivityLocation.Address.City !== undefined) {
2129
            str += t.ActivityLocation.Address.City;
2130
            if (t.ActivityLocation.Address.StateProvinceCode !== undefined) {
2131
                str += ", " + t.ActivityLocation.Address.StateProvinceCode;
2132
            }
2133
        }
2134
        str += '</td>';
2 - 2135
 
6 - 2136
        str += '</tr>';
2137
    }
2 - 2138
 
6 - 2139
    return (str);
2 - 2140
}
2141
 
2142
 
2143
function connected() {
6 - 2144
    var x;
2 - 2145
 
6 - 2146
    eBayAuthTokenFlag = true;
2147
    document.getElementById("connected").innerHTML += " (Connected)";
2 - 2148
 
6 - 2149
    x = document.getElementById("startButton");
2150
    x.className = x.className.replace(" w3-red", " w3-black");
2 - 2151
 
6 - 2152
    x = document.getElementById("login");
2153
    x.className = x.className.replace(" w3-show", "");
2 - 2154
 
6 - 2155
    x = document.getElementById("results");
2156
    x.innerHTML = "";
2157
    x.className = x.className.replace(" w3-show", "");
2 - 2158
}
2159
 
2160
    </script>
2161
 
2162
</body>
2163
</html>