TSI Arrivals

script for calculating TSI arrivals

// ==UserScript==
// @name         TSI Arrivals
// @description  script for calculating TSI arrivals
// @version      1.85
// @author       milcz
// @match        https://trans-logistics-eu.amazon.com/fmc/execution/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=amazon.com
// @grant        GM.xmlHttpRequest



// @namespace https://greasyfork.org/users/967551
// ==/UserScript==



function getUnits() {

    //set current time
    const time = new Date()
    const lastMonth = new Date(time - 864000000)
    const nextMonth = new Date(Number(time) + 864000000)

    console.log(lastMonth)

    let lastD = lastMonth.getDate()
    if (lastD < 10) {
    lastD = '0' + lastD
    }
    let lastM = lastMonth.getMonth() + 1
    if (lastM < 10) {
    lastM = '0' + lastM
    }
    let lastY = lastMonth.getFullYear()

    let nextD = nextMonth.getDate()
    if (nextD < 10) {
    nextD = '0' + nextD
    }
    let nextM = nextMonth.getMonth() + 1
    if (nextM < 10) {
    nextM = '0' + nextM
    }
    let nextY = nextMonth.getFullYear()

    let startDate = `${lastY}-${lastM}-${lastD}`
    let endDate = `${nextY}-${nextM}-${nextD}`

    console.log(startDate, endDate)


    GM.xmlHttpRequest({
        method: "GET",
        url: 'https://afttransshipmenthub-eu.aka.amazon.com/inbound/getTransferManifestsByDateAndSourceWarehouse/?startDate=' + startDate + '&endDate=' + endDate + '&warehouseId=LCY2&_=1662724122233',
        responseType: 'json',
        headers: {
            "User-Agent": "Mozilla/5.0",    // If not specified, navigator.userAgent will be used.
        },
        onload: function update(response) {
            const vrids = document.getElementsByClassName('vr-audit-dialog');
            const headingParent = document.getElementsByTagName('thead')[0].firstChild;
            const heading = document.createElement('th');
            heading.setAttribute('id', 'units-container');

            if (!document.getElementById('units-container')) {
                headingParent.insertBefore(heading, headingParent.children[2])
                heading.innerHTML = 'Units';
            }


            let sum = 0;
            for (let vrid of vrids) {
                let items = 6000;
                let sourceFc = vrid.parentElement.parentElement.childNodes[7].innerHTML.slice(0, 4);
                const vridParent = vrid.parentElement.parentElement;
                for (let manifest of response.response) {

                    if (vrid.innerHTML == manifest.trailerId) {
                        items = manifest.numberOfItems;
                        if (manifest.numberOfReactiveItems != 0 ) {
                          vrid.parentElement.parentElement.style.setProperty("color", "red", "important");
                        }
                        break
                    } else if (sourceFc == 'BHX1') {
                        items = 6103
                    } else if (sourceFc == 'BHX4') {
                        items = 24591
                    } else if (sourceFc == 'BRS2') {
                        items = 6488
                    } else if (sourceFc == 'EDI4') {
                        items = 2919
                    } else if (sourceFc == 'EMA1') {
                        items = 4618
                    } else if (sourceFc == 'EUK5') {
                        items = 6589
                    } else if (sourceFc == 'LBA4') {
                        items = 25735
                    } else if (sourceFc == 'LTN4') {
                        items = 5725
                    } else if (sourceFc == 'MXP5') {
                        items = 6409
                    } else if (sourceFc == 'BCN1') {
                        items = 8890
                    } else if (sourceFc == 'BLQ1') {
                        items = 11943
                    } else if (sourceFc == 'BRS1') {
                        items = 8000
                    } else if (sourceFc == 'CWL1') {
                        items = 3010
                    } else if (sourceFc == 'FRA7') {
                        items = 16878
                    } else if (sourceFc == 'GLA1') {
                        items = 7841
                    } else if (sourceFc == 'KAL2') {
                        items = 8790
                    } else if (sourceFc == 'LCY3') {
                        items = 5445
                    } else if (sourceFc == 'LTN1') {
                        items = 5597
                    } else if (sourceFc == 'MAN3') {
                        items = 8000
                    } else if (sourceFc == 'MME1') {
                        items = 8000
                    } else if (sourceFc == 'ORY1') {
                        items = 2015
                    } else if (sourceFc == 'ORY4') {
                        items = 8570
                    } else if (sourceFc == 'SNN4') {
                        items = 7145
                    } else if (sourceFc == 'TRN1') {
                        items = 3472
                    } else if (sourceFc == 'VEEI') {
                        items = 2605
                    } else if (sourceFc == 'XBH7') {
                        items = 10821
                    } else if (sourceFc == 'XUK1') {
                        items = 13819
                    } else if (sourceFc == 'XUKP') {
                        items = 5476
                    } else if (sourceFc == 'XUKM') {
                        items = 5000
                    } else if (sourceFc == 'ZAZ1') {
                        items = 9338
                    }

                }
                const unitsContainer = document.createElement('td')
                vridParent.insertBefore(unitsContainer, vridParent.children[2])
                unitsContainer.innerHTML = items.toLocaleString('en-US');
                sum = sum + items;

            }


            const hook = document.getElementById('fmc-execution-plans-vrs_paginate');
            if (!document.getElementById('total-units')) {
                const container = document.createElement('div');
                container.setAttribute('id', 'total-units');
                hook.insertBefore(container, hook.firstChild)
                container.innerHTML = 'Total arrivals: ' + '<b>' + sum.toLocaleString('en-US') + '</b>'
            }

            const sortButtons = document.getElementsByTagName('th');
            for (let button of sortButtons) {
                button.addEventListener('click', getUnits)
            }

        }
    }

    );


}
getUnits()


const refreshButton = document.getElementById('a-autoid-13');
refreshButton.addEventListener('click', getUnits)