Subversion Repositories SmartDukaan

Rev

Rev 35555 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

var grnhistory = grnhistory || {};
grnhistory.searchText = "";
grnhistory.searchType = "";
grnhistory.startTime = "0";
grnhistory.endTime = "0";

$(function () {
        $(document).on('click', ".new_grn", function () {
                loadNewGrn("main-content");
        });

        $(document).on('click', ".grn_history", function () {
                loadGrnHistory("main-content", "", "", moment().format(moment.HTML5_FMT.DATETIME_LOCAL_SECONDS), moment().format(moment.HTML5_FMT.DATETIME_LOCAL_SECONDS));
        });

        $(document).on('click', ".pending_grn", function () {
                loadPendingGrn("main-content");
        });

        $(document).on('click', "td.startGrnSerialized", function () {
                var invoiceNumber = $(this).attr('invoiceNumber');
                var quantity = $(this).attr('quantity');
                var displayName = $(this).attr('displayName');
                var itemId = $(this).attr('itemId');
                console.log(quantity);
                $(".modal-body .grnInvoiceNumber>span").text(invoiceNumber);
                $(".modal-body .grnProductInfo>span").text(displayName);
                $("#scanModel .invoiceNumber").val(invoiceNumber);
                $("#scanModel .itemId").val(itemId);
                $("#grnImeiInformation").empty();
                var qty = parseInt(quantity);
                var divCode = '<div class="row">CONTENT</div><p></p>';
                doGetAjaxRequestHandler(`/purchase/get-imeis?invoiceNumber=${invoiceNumber}&itemId=${itemId}`, function (data) {
                        let response = data.response;
                        let index = 0;
                        while (index < response.length) {
                                let currentRow = response.length - index < 4 ? response.length - index : 4;
                                let rowArr = [];
                                for (let i = 0; i < currentRow; i++) {
                                        let checkedDiv =
                                                `<div class="col-sm-3">
                                                <label class="checkbox-inline">
                                                        <input type="checkbox" value="${response[index]}"> ${response[index]}
                                                </label>
                                        </div>`;
                                        rowArr.push(checkedDiv);
                                        index++;
                                }
                                $("#grnImeiInformation").append(divCode.replace("CONTENT", rowArr.join("")));
                        }
                });
        });
});

$(document).on('click', "td.startGrnNonSerialized", function () {
        var invoiceNumber = $(this).attr('invoiceNumber');
        var quantity = $(this).attr('quantity');
        var displayName = $(this).attr('displayName');
        var itemId = $(this).attr('itemId');
        console.log(quantity);
        $("#scanNonSerializedModel .modal-body .grnInvoiceNumber>span").text(invoiceNumber);
        $("#scanNonSerializedModel .modal-body .grnProductInfo>span").text(displayName);
        $("#scanNonSerializedModel .invoiceNumber").val(invoiceNumber);
        $("#scanNonSerializedModel .itemId").val(itemId);
        $("#grnImeiInformation").empty();
        var qty = parseInt(quantity);
        var divCode = '<div class="row">CONTENT</div><p></p>';
        var innerDiv = '<div class="col-sm-3">' +
                '<div class="input-group">' +
                '<input type="text" class="form-control">' +
                '</div>' +
                '</div>';
        while (qty > 0) {
                var divText = "";
                if (qty < 4) {
                        var cp = qty;
                        for (var i = 0; i < cp; i++) {
                                divText = divText + innerDiv;
                                qty = qty - 1;
                        }
                } else {
                        for (var i = 0; i < 4; i++) {
                                divText = divText + innerDiv;
                                qty = qty - 1;
                        }
                }
                divText = divCode.replace("CONTENT", divText);
                $("#grnImeiInformation")
                        .append(divText)
        }
});

$(document).on('click', "#grnSubmitSerialized", function () {
        let imeis = [];
        $("#grnImeiInformation :input:checked").each(function () {
                var input = $(this).val().trim();
                imeis.push(input);
        });
        let invoiceNumber = $("#scanModel .invoiceNumber").val();
        let itemId = $("#scanModel .itemId").val();
        let postData = JSON.stringify({
                "itemId": parseInt(itemId),
                "invoiceNumber": invoiceNumber,
                "serialNumbers": imeis
        });
        doPostAjaxRequestWithJsonHandler(context + "/scanSerialized", postData, function (response) {
                bootbox.alert("Purchase booked successfully", function () {
                        $('#scanModel').on('hidden.bs.modal', function (event) {
                                $("#purchase-reference-submit-button").click();
                                $(this).off(event);
                        });
                        $('#scanModel').modal('hide');
                });
        });
        return false;
});

$(document).on('click', "#grnNonSerializedSubmit", function () {
        var error = false;
        var errorText = "";
        if (error) {
                return false;
        }
        var invoiceNumber = $("#scanNonSerializedModel .invoiceNumber").val();
        var itemId = $("#scanNonSerializedModel .itemId").val();
        var quantity = $("#scanNonSerializedModel .quantity").val();
        var postData = JSON.stringify({
                "itemId": parseInt(itemId),
                "invoiceNumber": invoiceNumber,
                "quantity": parseInt(quantity)
        });
        doPostAjaxRequestWithJsonHandler(context + "/scanNonSerialized", postData, function (response) {
                bootbox.alert("Purchase booked successfully", function () {
                        $('#scanNonSerializedModel').on('hidden.bs.modal', function (event) {
                                $(this).off(event);
                                $("#purchase-reference-submit-button").click();
                        });
                        $('#scanNonSerializedModel').modal('hide');
                });
        });
        return false;
});

$(document).on('change', "#grnImeiInformation :input", function () {
        if ($(this).val().trim() != "") {
                console.log('The text box really changed this time');
                $('#grnImeiInformation :input').removeClass("border-highlight");
                if (findDuplicateSerialNumbers($(this).val().trim()) > 0) {
                        var inputs = $("#grnImeiInformation :input[value=" + $(this).val().trim() + "]");
                        if (inputs.length > 1) {
                                inputs.each(function () {
                                        $(this).addClass("border-highlight")
                                });
                        }
                }
        }
});


$(document).on('paste', "#grnImeiInformation :input", function () {
        if ($(this).val().trim() != "") {
                console.log('paste event');
                $(this).next("input .form-control").focus();
        }
});

$(document).on("keyup", "#airwayBillOrInvoiceNumberText", function (e) {
        var keyCode = e.keyCode || e.which;
        if (keyCode == 13) {
                $("#purchase-reference-submit-button").click();
        }
});

$(document).on('click', "#grn-history-search-button", function () {
        grnhistory.searchText = $("#grn-history-search-text").val();
        grnhistory.searchType = "purchaseReference";
        grnhistory.startTime = startDate;
        grnhistory.endTime = endDate;
        if (typeof (grnhistory.searchText) == "undefined" || !grnhistory.searchText) {
                grnhistory.searchText = "";
        }
        loadGrnHistorySearchInfo(grnhistory.searchText, grnhistory.searchType, grnhistory.startTime, grnhistory.endTime);
});

$(document).on('click', "#grn-history-download-button", function () {
        grnhistory.searchText = $("#grn-history-search-text").val();
        grnhistory.searchType = "purchaseReference";
        grnhistory.startTime = startDate;
        grnhistory.endTime = endDate;
        if (typeof (grnhistory.searchText) == "undefined" || !grnhistory.searchText) {
                grnhistory.searchText = "";
        }
        downloadPurchaseInvoices(grnhistory.searchText, grnhistory.searchType, grnhistory.startTime, grnhistory.endTime);
});

$(document).on("keyup", "#grn-history-search-text", function (e) {
        var keyCode = e.keyCode || e.which;
        if (keyCode == 13) {
                $("#grn-history-search-button").click();
        }
});

$(document).on('click', "#grn-history-paginated .next", function () {
        var params = {};
        params['purchaseReference'] = grnhistory.searchText;
        params['searchType'] = grnhistory.searchType;
        params['startTime'] = grnhistory.startTime;
        params['endTime'] = grnhistory.endTime;
        loadPaginatedNextItems('/getPaginatedGrnHistory', params, 'grn-history-paginated', 'grn-history-table', null);
        $(this).blur();

});

$(document).on('click', "#grn-history-paginated .previous", function () {
        var params = {};
        params['purchaseReference'] = grnhistory.searchText;
        params['searchType'] = grnhistory.searchType;
        params['startTime'] = grnhistory.startTime;
        params['endTime'] = grnhistory.endTime;
        loadPaginatedPreviousItems('/getPaginatedGrnHistory', params, 'grn-history-paginated', 'grn-history-table', null);
        $(this).blur();

});

$(document).on('click', "#grn-history-date-button", function () {
        var dateRange = $('#reportrange span').text();
        grnhistory.startTime = getDatesFromPicker($("#grnDateRange")).startDate;
        grnhistory.endTime = getDatesFromPicker($("#grnDateRange")).endDate;
        grnhistory.searchType = "dateFilter";
        grnhistory.searchText = "";
        loadGrnHistorySearchInfo(grnhistory.searchText, grnhistory.searchType, grnhistory.startTime, grnhistory.endTime);
});

$(document).on('click', ".grn-details", function () {
        var purchaseId = $(this).attr('data');
        console.log(purchaseId);
        loadGrnDetails(purchaseId, "grn-details-container");
});

$(document).on('click', ".pending-grn-details", function () {
        console.log("pending-grn-details clicked");
        var orderId = $(this).attr('data');
        console.log("orderId : " + orderId);
        loadPendingGrnDetails(orderId, "pending-grn-details-container");
});

$(document).on('click', ".pending-grn", function () {
        var purchaseReference = $(this).attr('data');
        console.log(purchaseReference);
        loadPendingGrnDetails(purchaseReference, "main-content");
});


function loadNewGrn(domId, invoiceNumber) {
        //var response = doGetAjaxRequest(context+"/purchase");
        doGetAjaxRequestHandler(context + "/purchase", function (response) {
                $('#' + domId).html(response);
                if (invoiceNumber != undefined) {
                        $("#airwayBillOrInvoiceNumberText").val(invoiceNumber);
                        $("#purchase-reference-submit-button").click();
                }
        });

}

function getPurchaseByInvoiceNumber() {
        var airwayBillOrInvoiceNumber = $('#airwayBillOrInvoiceNumberText').val();
        doGetAjaxRequestHandler(context + "/purchaseByInvoiceNumber?airwayBillOrInvoiceNumber=" + airwayBillOrInvoiceNumber, function (response) {
                $('#main-content').html(response);
        });
}

function loadPendingGrn(domId) {
        doGetAjaxRequestHandler(context + "/pendingGrn", function (response) {
                $('#' + domId).html(response);
        });
}

function loadGrnHistory(domId, purchase_reference, searchType, startTime, endTime) {
        doGetAjaxRequestHandler(context + "/grnHistory?purchaseReference=" + purchase_reference + "&searchType=" + searchType
                + "&startTime=" + startTime
                + "&endTime=" + endTime, function (response) {
                $('#' + domId).html(response);
        });
}

function loadGrnHistorySearchInfo(search_text, searchType, startTime, endTime) {
        loadGrnHistory("main-content", search_text, searchType, startTime, endTime);
}

function downloadPurchaseInvoice(search_text, searchType, startTime, endTime) {
        purchaseInvoice_url = context + "/grnHistory?purchaseReference=" + purchase_reference + "&searchType=" + searchType
                + "&startTime=" + startTime + "&endTime=" + endTime;
}

function findDuplicateSerialNumbers(value) {
        var result = $("#grnImeiInformation :input[value='" + value + "']").length - 1;
        return result;
}

function loadGrnDetails(purchaseId, domId) {
        doGetAjaxRequestHandler(context + "/grnHistoryDetailByPurchaseId?purchaseId=" + purchaseId, function (response) {
                $('#' + domId).html(response);
                window.dispatchEvent(new Event('resize'));
        });
}

function loadPendingGrnDetails(orderId, domId) {
        doGetAjaxRequestHandler(context + "/pendingGrnDetails?orderId=" + orderId, function (response) {
                $('#' + domId).html(response);
                window.dispatchEvent(new Event('resize'));
        });

}

function grnPartial(invoiceNumber) {
        let serialNumbers = $("#all-imeis").tagsinput('items');
        if (serialNumbers.length == 0) {
                alert("Pls add imeis");
                $("#all-imeis").focus();
                return false;
        }
        let imeisJson = {
                serialNumbers: serialNumbers,
                invoiceNumber: invoiceNumber
        }
        doPostAjaxRequestWithJsonHandler(`${context}/purchase/grn-imeis`, JSON.stringify(imeisJson), function (data) {
                let response = data.response;
                if (response) {
                        alert("Purchase booked successfully");
                        $("#purchase-reference-submit-button").click();
                }
        });
}

$(document).on('click', ".hid-allocation", function () {
        doGetAjaxRequestHandler(context + "/pur-sale-ratio/stock/hid-allocation",
                function (response) {
                        $('#' + 'main-content').html(response);
                });
});

$(document).on('click', '.partner-hid-allocation', function () {
        partnerHidAllocationList();
});

function partnerHidAllocationList() {
        if (typeof partnerId != "undefined") {
                doGetAjaxRequestHandler(context + "/pur-sale-ratio/stock/hid-catalog-allocation-list?fofoId=" + partnerId,
                        function (response) {
                                $('.hid-allocation-conatiner').html(response);
                        });
        } else {
                doGetAjaxRequestHandler(context + "/pur-sale-ratio/stock/hid-catalog-allocation-list?fofoId=0",
                        function (response) {
                                $('.hid-allocation-conatiner').html(response);
            });
    }

}

var table = $('.hidCatalogAllocationList').DataTable();
$(document).on('dblclick', '.hidCatalogAllocationList tbody tr', function () {
    var data = table.row(this).data();
    $(this).find(":input").attr('disabled', false).show();
});

$(document).on('click', ".generate-allocation-request", function () {
    var catalogid = $(this).data('catalogid');
    const self = this;
    if (confirm("Are you sure you want to generate a request for allocation limit")) {
        var requestedAllocation = $(this).closest('tr').find('input[name="allocation-limit"]').val();

        if (!requestedAllocation) {
            alert("Please enter a requested allocation before submitting.");
            return;
        }

        doGetAjaxRequestHandler(`${context}/pur-sale-ratio/generate-allocation-request?catalogId=${catalogid}&fofoId=${partnerId}&requestedAllocation=${requestedAllocation}`, function (response) {
            if (response) {
                alert("Allocation request has been sent.");
                partnerHidAllocationList();
            }
        });
        }
});

$(document).on('click', '.edit-allocation', function () {
        var $row = $(this).closest('tr'); // Get the closest table row
        var $input = $row.find('.reuested_allocation'); // Find the input inside the row
        $input.prop('disabled', false).focus(); // Enable input and focus on it
});

$(document).on('click', ".allocation-request-edit", function () {
        var $row = $(this).closest('tr');
        var allocationId = $(this).data('id');
        var requestedAllocation = $row.find('.reuested_allocation').val();

        if (!requestedAllocation) {
                alert("Please enter a requested allocation before submitting.");
                return;
        }

        if (confirm("Are you sure you want to update the allocation?")) {
                doGetAjaxRequestHandler(`${context}/pur-sale-ratio/edit-allocation-request?allocationId=${allocationId}&requestedAllocation=${requestedAllocation}`, function (response) {
                        if (response) {
                                alert("Allocation request has been sent.");
                                loadRequestedAllocations();
                        }
                });
        }
});


$(document).on('click', ".reuested-allocations", function () {
    loadRequestedAllocations();
});

$(document).on('click', ".allocation-request-approve-reject", function () {
    var id = $(this).data('id');
    var status = $(this).data('status');
    doGetAjaxRequestHandler(context + `/pur-sale-ratio/allocation-request-approve-reject?id=${id}&status=${status}`,
        function (response) {
            loadRequestedAllocations();
        });
});

function loadRequestedAllocations() {
    doGetAjaxRequestHandler(context + "/pur-sale-ratio/requested-allocations",
        function (response) {
            $('#' + 'main-content').html(response);
        });
}