Subversion Repositories SmartDukaan

Rev

Rev 27754 | Rev 30599 | Go to most recent revision | 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";
dateRangeCallback(moment(), moment());

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

        $(document).on('click', ".grn_history", function() {
                loadGrnHistory("main-content","","",startDate,endDate);
        });
        
        $(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>';
                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', "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(){
                var imeis = [];
                var error = false;
                var errorText = "";
                $("#grnImeiInformation :input").each(function(){
                        var input = $(this).val().trim();
                        if (imeis.indexOf(input) !=-1 || !input){
                                error = true;
                                if (!input){
                                        $(this).addClass("border-highlight");
                                }
                        }
                        imeis.push(input);
                        
                });
                if (error){
                        return false;
                }
                var invoiceNumber = $("#scanModel .invoiceNumber").val();
                var itemId = $("#scanModel .itemId").val();
                var 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 = startDate;
                grnhistory.endTime = 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'));
        });
        
}