
function getInventory() {

    var response = $.ajax({
        type: "POST",
        url: "InventoryBatchAdjust.aspx/getInventories",
        async: false,
        contentType: 'application/json;charset=utf8',
        beforeSend: function () {
            //modalLoad.style.display = "block";
        },
        complete: function (result) {
            // modalLoad.style.display = "none";
        },
        dataType: 'json',
        error: function (jqXHR, textStatus, errorThrown) {
            //console.log(errorThrown);
            console.log("(getInventory()) Request: " + jqXHR.toString() + "\n\nStatus:" + textStatus + "\n\nError: " + errorThrown);
        },
        success: function (result) {
            //console.log(result.d)
            //sessionStorage.setItem('arrData', result.d);
            //generateInventoryWithQOH();
            //loadtblInventory();
        }
    });
    return response.responseJSON.d;
};

function getCategory() {
    var response = $.ajax({
        type: "POST",
        url: "InventoryBatchAdjust.aspx/getCategories",
        async: false,
        contentType: 'application/json;charset=utf8',
        beforeSend: function () { },
        complete: function (result) {
            // modalLoad.style.display = "none";

        },
        dataType: 'json',
        error: function (jqXHR, textStatus, errorThrown) {
           // console.log(errorThrown);
            console.log("(getCategory()) Request: " + jqXHR.toString() + "\n\nStatus:" + textStatus + "\n\nError: " + errorThrown);
        },
        success: function (result) {
            // sessionStorage.setItem('category', result.d);
        }
    });
    return response.responseJSON.d;
}

function getInventoryCostAverage() {
    var response = $.ajax({
        type: "POST",
        url: "InventoryBatchAdjust.aspx/getInventoryCostAverage",
        async: false,
        contentType: 'application/json;charset=utf8',
        beforeSend: function () {
            //modalLoad.style.display = "block";
        },
        complete: function (result) {
            // modalLoad.style.display = "none";
        },
        dataType: 'json',
        error: function (jqXHR, textStatus, errorThrown) {  console.log("(getInventoryCostAverage()) Request: " + jqXHR.toString() + "\n\nStatus:" + textStatus + "\n\nError: " + errorThrown); },
        success: function (result) { }
    });
    return response.responseJSON.d;
}

function getStockAdjustGroupUnit() {
    var response = $.ajax({
        type: "POST",
        url: "InventoryBatchAdjust.aspx/getStockAdjustGroupUnit",
        async: false,
        contentType: 'application/json;charset=utf8',
        beforeSend: function () {
            //modalLoad.style.display = "block";
        },
        complete: function (result) {
            // modalLoad.style.display = "none";
        },
        dataType: 'json',
        error: function (jqXHR, textStatus, errorThrown) {
            console.log(errorThrown);
        },
        success: function (result) {
            //sessionStorage.setItem('arrGroupUnit', result.d)
        }
    });
    return response.responseJSON.d;
}

function getStockAdjustUnit() {
    var response = $.ajax({
        type: "POST",
        url: "InventoryBatchAdjust.aspx/getStockAdjustUnit",
        async: false,
        contentType: 'application/json;charset=utf8',
        beforeSend: function () {
            //modalLoad.style.display = "block";
        },
        complete: function (result) {
            // modalLoad.style.display = "none";
        },
        dataType: 'json',
        error: function (jqXHR, textStatus, errorThrown) {
            console.log(errorThrown);
        },
        success: function (result) {
            //sessionStorage.setItem('arrUnit', result.d)
        }
    });
    return response.responseJSON.d;
}

function getInventoryStockAdjustDetail() {
    var response = $.ajax({
        type: "POST",
        url: "InventoryBatchAdjust.aspx/getInventoryCostAverageDetail",
        async: false,
        contentType: 'application/json;charset=utf8',
        beforeSend: function () {
            //modalLoad.style.display = "block";
        },
        complete: function (result) {
            // modalLoad.style.display = "none";

        },
        dataType: 'json',
        error: function (jqXHR, textStatus, errorThrown) {
            //console.log(errorThrown);
            console.log("(getInventoryStockAdjustDetail()) Request: " + jqXHR.toString() + "\n\nStatus:" + textStatus + "\n\nError: " + errorThrown);
        },
        success: function (result) {
        }
    });
    return response.responseJSON.d;
}


function bearertoken() {
  let url = readWebConfig();

    let token;
    var settings = {
        "url": url + "/Users/authenticate",
        "method": "POST",
        "async": false,
        "timeout": 0,
        "headers": {
            "Content-Type": "application/json"
        },
        "data": JSON.stringify({
            "username": "test",
            "password": "test"
        }),
    };

    $.ajax(settings).done(function (response) {
        token = response['token'];
    });
    return token;
}

function getICAD(){

    let url = readWebConfig();
    let token = bearertoken();
    let db = loadDB();
    //console.log(url);
    //console.log(token);
    //console.log(db);
    var icad;
    var settings = {
        //"url": "https://www.suntoyo.online/api/da1/InventoryCostAverageDetail/GetInventoryCostAverageDetail",
        "url": url + "/api/" + db + "/InventoryCostAverageDetail/GetInventoryCostAverageDetail",
        "method": "GET",
        "async": false,
        "timeout": 0,
        "headers": {
                "Authorization": token
            },
    };

    $.ajax(settings).done(function (response) {
        icad = response
    }).fail(function (jqXHR, textStatus, errorThrown) {
        console.log("error" + errorThrown);
        return;
    });
     return JSON.parse(icad);
}

function getInventories(){
    let url = readWebConfig();
    let token = bearertoken();
    let db = loadDB();
    var inventories; 

    var settings = {
        "url": url + "/api/" + db + "/DFDownload/GetInventory",
        "method": "GET",
        "timeout": 0,
        "async": false,
        "headers": {
        "Authorization": token
        },
    };

    $.ajax(settings).done(function (response) {
        inventories = response
    }).fail(function (jqXHR, textStatus, errorThrown) {
        console.log("error");
    });
     return JSON.parse(inventories);
}

function getCategories(){
  
    let url = readWebConfig();
    let token = bearertoken();
    let db = loadDB();
    var categories; 
    var settings = {
        "url": url + "/api/" + db + "/DFDownload/GetInventoryCategories",
        "method": "GET",
        "timeout": 0,
        "async": false,
        "beforeSend":function( xhr ) {
              //$('#loadingmodal').removeClass('d-none').addClass('d-block');
        },
        "headers": {
        "Authorization": token
        },
    };

    $.ajax(settings).done(function (response) {
        //$('#loadingmodal').modal('hide')
        categories = response  
        //$('#loadingmodal').removeClass('d-block').addClass('d-none');
    }).fail(function (jqXHR, textStatus, errorThrown) {
        console.log("error");
    })  
    return JSON.parse(categories);
}

function getICA() {
    let url = readWebConfig();
    let token = bearertoken();
    let db = loadDB();
    let stockadjustdetails;
    var settings = {
        "url": url + "/api/" + db + "/DFDownload/GetInventoryCostAverage",
        "method": "GET",
        "async": false,
        "timeout": 0,
        "headers": {
            "Authorization": token
        },
    }; $.ajax(settings).done(function (response) {
      
        stockadjustdetails = response;
    }).fail(function (jqXHR, textStatus, errorThrown) {
        console.log("error");
    });
    return JSON.parse(stockadjustdetails);
}

function getBatchID1() {
    let token = bearertoken();
    let db = loadDB();
    let batchid;
    var newbatchid;
    let batchidlength = '000000';
    var settings = {
        "url": url + "/api/" + db + "/InventoryCostAverageDetail/GetInventoryCostAverageDetail",
        "method": "GET",
        "async": false,
        "timeout": 0,
        "headers": {
            "Authorization": token
        },
    };

    $.ajax(settings).done(function (response) {
        let data = JSON.parse(response);
        data.sort(function (a, b) {
            return a['batchid'].localeCompare(b['batchid'])
        });

        //console.log(data)

        if (data.length > 0) {
            let lastbatchid = data.at(-1);
            let batchid = parseInt(lastbatchid['batchid']) + 1;

            newbatchid = batchidlength + batchid;
            newbatchid = newbatchid.substr(newbatchid.length - batchidlength.length);
        } else {
            newbatchid = batchidlength + 1;
            newbatchid = newbatchid.substr(newbatchid.length - batchidlength.length);
        }

    }).fail(function (jqXHR, textStatus, errorThrown) {
        console.log("error");
    }).always(function (jqXHR, textStatus, errorThrown) {
    })

    return newbatchid;
}

function getBatchID() {
    //console.log(JSON.parse(getInventoryStockAdjustDetail()))
    let getInventoryStockAdjustDetailList = JSON.parse(getInventoryStockAdjustDetail())
    //let token = bearertoken();
    //let db = loadDB();
    let batchid;
    var newbatchid;
    let batchidlength = '000000';
    if (getInventoryStockAdjustDetailList.length > 0) {
        batchid = getInventoryStockAdjustDetailList.slice(-1)[0]['batchid']
        batchid = parseInt(batchid) + 1;
        newbatchid = batchidlength + batchid;
        newbatchid = newbatchid.substring(newbatchid.length - batchidlength.length);
    } else {
        newbatchid = batchidlength + 1;
        newbatchid = newbatchid.substr(newbatchid.length - batchidlength.length);
    }
    return newbatchid;
    //var settings = {
    //    "url": url + "/api/" + db + "/InventoryCostAverageDetail/GetInventoryCostAverageDetail",
    //    "method": "GET",
    //    "async": false,
    //    "timeout": 0,
    //    "headers": {
    //        "Authorization": token
    //    },
    //};

    //$.ajax(settings).done(function (response) {
    //    let data = JSON.parse(response);
    //    data.sort(function (a, b) {
    //        return a['batchid'].localeCompare(b['batchid'])
    //    });

    //    //console.log(data)

    //    if (data.length > 0) {
    //        let lastbatchid = data.at(-1);
    //        let batchid = parseInt(lastbatchid['batchid']) + 1;

    //        newbatchid = batchidlength + batchid;
    //        newbatchid = newbatchid.substr(newbatchid.length - batchidlength.length);
    //    } else {
    //        newbatchid = batchidlength + 1;
    //        newbatchid = newbatchid.substr(newbatchid.length - batchidlength.length);
    //    }

    //}).fail(function (jqXHR, textStatus, errorThrown) {
    //    console.log("error");
    //}).always(function (jqXHR, textStatus, errorThrown) {
    //})

    //return newbatchid;
}

function getGroupUnit() {
    let url = readWebConfig();
    let token = bearertoken();
    let db = loadDB();
    var groupunit;
    var settings = {
        "url": url + "/api/" + db + "/StockAdjustGroupUnit/GetStockAdjustGroupUnit",
        "method": "GET",
        "timeout": 0,
        "async": false,
        "headers": {
            "Authorization": token
        },
    };

    $.ajax(settings).done(function (response) {
        groupunit = response
    }).fail(function (jqXHR, textStatus, errorThrown) {
        console.log("error");
    });
    return JSON.parse(groupunit);
}

function getUnit() {
    let url = readWebConfig();
    let token = bearertoken();
    let db = loadDB();
    var unit;
    var settings = {
        "url": url + "/api/" + db + "/StockAdjustUnit/GetStockAdjustUnit",
        "method": "GET",
        "timeout": 0,
        "async": false,
        "headers": {
            "Authorization": token
        },
    };

    $.ajax(settings).done(function (response) {
        unit = response
    }).fail(function (jqXHR, textStatus, errorThrown) {
        console.log("error");
    });
    return JSON.parse(unit);
}

function getDateTime() {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();
    if (month.toString().length == 1) {
        month = '0' + month;
    }
    if (day.toString().length == 1) {
        day = '0' + day;
    }
    if (hour.toString().length == 1) {
        hour = '0' + hour;
    }
    if (minute.toString().length == 1) {
        minute = '0' + minute;
    }
    if (second.toString().length == 1) {
        second = '0' + second;
    }
    var dateTime = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
    return dateTime;

}


