Subversion Repositories SmartDukaan

Rev

Rev 33661 | Blame | Compare with Previous | Last modification | View Log | RSS feed

var retailerName;
var fofoId;
var detailPoId;
$(function () {
    $(document).on('click', ".suggested-monthly-po", function () {
        getAllOpenPo("main-content");
    });


    $(document).on('click', "#suggested-monthly-po .podetail", function () {
        var poId = $(this).data("poid");
        fofoId = $(this).data("fofoid");
        detailPoId = poId;
        retailerName = $(this).find('td:nth-child(1) span').text().trim();

        $(this).addClass("active-tr");
        $(".podetail").not(this).removeClass("active-tr");

        getPoDetailOrder(poId, "suggested-monthly-po-detail");
    });

});

function getAllOpenPo(domId) {
    doGetAjaxRequestHandler(context + "/getMonthlySuggestedPo", function (response) {
        $('#' + domId).html(response);

        // Destroy the existing DataTable instance if it exists
        if ($.fn.DataTable.isDataTable('#suggested-monthly-po')) {
            $('#suggested-monthly-po').DataTable().destroy();
        }

        // Initialize DataTable
        var table = $('#suggested-monthly-po').DataTable({
            "bPaginate": true,
            "pageLength": 25,
            "bLengthChange": true,
            "bFilter": true,
            "bInfo": false,
            "bAutoWidth": false
        });


        table.page('first').draw('page');


        var firstVisibleRow = $("#suggested-monthly-po tbody tr:visible:first");
        if (firstVisibleRow.length > 0) {
            firstVisibleRow.trigger('click');
        }
    });
}


function getPoDetailOrder(poId, detailContainer) {
    var url = context + "/monthlyPo/orderByPoId?poId=" + poId;
    doGetAjaxRequestHandler(url, function (response) {
        $('#' + detailContainer).html(response);
        $("#selected-po").text(poId);
    });
}

function getPoDetailOrder(poId, detailContainer) {
    var url = context + "/monthlyPo/orderByPoId?poId=" + poId;
    doGetAjaxRequestHandler(url, function (response) {

        $('#' + detailContainer).html(response);

        $("#selected-po").text(poId);

        calculateAndDisplayTotalPrice();
    });
}

function calculateAndDisplayTotalPrice() {
    var totalPrice = 0;

    $('#suggested-monthly-po-detail tbody tr').each(function () {
        var itemPrice = parseFloat($(this).find('td:eq(3)').text());
        var quantity = parseFloat($(this).find('td:eq(2)').text());

        totalPrice += itemPrice * quantity;
    });

    $('.totalPoPrice').text('Total PO Price: ' + totalPrice.toFixed(2));
}


$(function () {
    $(document).on('click', "#approveOrderBtn", function () {
        // Show confirmation alert
        var userConfirmed = confirm("Are you sure you want to approve this order?");

        // Proceed only if the user confirms
        if (userConfirmed) {
            var bulkOrderModels = [];
            $('#suggested-monthly-po-detail tbody tr').each(function (index) {
                var row = $(this);

                var bulkOrder = {
                    rowIndex: index + 1,
                    fofoId: fofoId,
                    partnerName: retailerName,
                    itemId: row.find('td:nth-child(1)').text().trim(),
                    description: row.find('td:nth-child(2)').text().trim(),
                    quantity: row.find('td:nth-child(3)').text().trim(),
                    itemPrice: row.find('td:nth-child(4)').text().trim()
                };

                bulkOrderModels.push(bulkOrder);
            });

            console.log("bulk order models", bulkOrderModels);

            // Send the request only after confirmation
            doPostAjaxRequestWithJsonHandler(context + `/monthlyPo/approve?poId=${detailPoId}`, JSON.stringify(bulkOrderModels), function (response) {
                if (response) {
                    getAllOpenPo("main-content");
                }
            });
        } else {
            // User canceled the action, do nothing
            console.log("User canceled the approval.");
        }
    });


    $(document).on('click', "#cancelOrderBtn", function () {
        var userConfirmed = confirm("Are you sure you want to cancel this order?");

        // Proceed only if the user confirms
        if (userConfirmed) {
            doPostAjaxRequestHandler(context + `/monthlyPo/cancelPo?poId=${detailPoId}`, function (response) {
                if (response) {
                    getAllOpenPo("main-content");
                }
            });
        } else {
            console.log("User canceled the approval.");
        }
    });
});