Subversion Repositories SmartDukaan

Rev

Rev 22090 | Rev 22245 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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

$(function() {
        $(".new_grn").live('click', function() {
                loadNewGrn("main-content");
        });
        
        $(".good_inventory").live('click', function() {
                loadGoodInventory("main-content","");
        });
        
        $(".catalog").live('click', function() {
                loadCatalog("main-content","");
        });
        
        $(".bad_inventory").live('click', function() {
                loadBadInventory("main-content","");
        });

        $(".grn_history").live('click', function() {
                loadGrnHistory("main-content","","","0","0");
        });
        
        $(".cart_icon_header").live('click', function() {
                loadCart("main-content");
        });
        
        $(".cart-checkout").live('click', function() {
                checkout("main-content");
        });
        
        //cart_icon_header
        
        $("#purchase-reference-submit").live('submit', function(){
                var formData = new FormData($(this)[0]);

                jQuery.ajax({
                        url: context+"/purchase",
                        type: 'POST',
                        data: formData,
                        async: false,
                        success: function (data) {
                                $('#main-content').html(data);
                        },
                        error : function() {
                                alert("OOPS!!!Failed to do changes.Try Again.",'ERROR');
                        },
                        cache: false,
                        contentType: false,
                        processData: false
                });
                return false;
        });

        $("td.startGrnSerialized").live('click', 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)
                }
        });

        $("td.startGrnNonSerialized").live('click', 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)
                }
        });
        
        $("#grnSubmitSerialized").live('click', 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});
                jQuery.ajax({
                        url: context+"/scanSerialized",
                        type: 'POST',
                        data: postData,
                        contentType:'application/json',
                        async: false,
                        success: function (data) {
                                alert("Purchase booked successfully");
                                $('#scanModel').modal('hide');
                                $("#scanModel").on("hidden.bs.modal", function () {
                                        $("#purchase-reference-submit").submit();
                                });
                        },
                        error : function() {
                                alert("OOPS!!!Failed to do changes.Try Again.",'ERROR');
                        },
                        cache: false,
                        processData: false
                });
                return false;
        });
        
        $("#grnNonSerializedSubmit").live('click', 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)});
                jQuery.ajax({
                        url: context+"/scanNonSerialized",
                        type: 'POST',
                        data: postData,
                        contentType:'application/json',
                        async: false,
                        success: function (data) {
                                alert("Purchase booked successfully");
                                $('#scanNonSerializedModel').modal('hide');
                                $("#scanNonSerializedModel").on("hidden.bs.modal", function () {
                                        $("#purchase-reference-submit").submit();
                                });
                        },
                        error : function() {
                                alert("OOPS!!!Failed to do changes.Try Again.",'ERROR');
                        },
                        cache: false,
                        processData: false
                });
                return false;
        });

        $("#grnImeiInformation :input").live('change paste keyup mouseup', 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")});
                                }
                        }
                }
        });
        
        $("#good-inventory-paginated .next").live('click', function() {
                var start = $( "#good-inventory-paginated .start" ).text();
                var end = $( "#good-inventory-paginated .end" ).text();
                var searchText = $("#good-inventory-search-text").val();
                if (typeof (searchText) == "undefined" || !searchText){
                        searchText = "";
                }
                getNextItems(start,end,searchText);
                $("#good-inventory-paginated .next").blur();
                
    });
        
        $("#good-inventory-paginated .previous").live('click', function() {
                var start = $( "#good-inventory-paginated .start" ).text();
                var end =  $( "#good-inventory-paginated .end" ).text();
                var searchText = $("#good-inventory-search-text").val();
                if (typeof (searchText) == "undefined" || !searchText){
                        searchText = "";
                }
                var pre = end - 20;
                getPreviousItems(start,end,pre,searchText);
                $("#good-inventory-paginated .previous").blur();
    });
        
        $("#good-inventory-search-button").live('click', function() {
                var searchText = $("#good-inventory-search-text").val();
                if (typeof (searchText) == "undefined" || !searchText){
                        searchText = "";
                }
                loadGoodInventorySearchInfo(searchText);
    });
        
        $("#good-inventory-search-text").live("keyup", function(e) {
                var keyCode = e.keyCode || e.which;
        if(keyCode == 13){
                $("#good-inventory-search-button").click();
        }
        });
        
        $("#catalog-paginated .next").live('click', function() {
                var start = $( "#catalog-paginated .start" ).text();
                var end = $( "#catalog-paginated .end" ).text();
                var searchText = $("#catalog-search-text").val();
                if (typeof (searchText) == "undefined" || !searchText){
                        searchText = "";
                }
                getNextCatalogItems(start,end,searchText);
                $("#catalog-paginated .next").blur();
    });
        
        $("#catalog-paginated .previous").live('click', function() {
                var start = $( "#catalog-paginated .start" ).text();
                var end =  $( "#catalog-paginated .end" ).text();
                var searchText = $("#catalog-search-text").val();
                if (typeof (searchText) == "undefined" || !searchText){
                        searchText = "";
                }
                var pre = end - 20;
                getPreviousCatalogItems(start,end,pre,searchText);
                $("#catalog-paginated .previous").blur();
    });
        
        $("#catalog-search-button").live('click', function() {
                var searchText = $("#catalog-search-text").val();
                if (typeof (searchText) == "undefined" || !searchText){
                        searchText = "";
                }
                loadCatalogSearchInfo(searchText);
    });
        
        $("#catalog-search-text").live("keyup", function(e) {
                var keyCode = e.keyCode || e.which;
        if(keyCode == 13){
                $("#catalog-search-button").click();
        }
        });
        
        $("#grn-history-search-button").live('click', function() {
                grnhistory.searchText = $("#grn-history-search-text").val();
                grnhistory.searchType = "purchaseReference";
                grnhistory.startTime = "0";
                grnhistory.endTime = "0";
                if (typeof (grnhistory.searchText) == "undefined" || !grnhistory.searchText){
                        grnhistory.searchText = "";
                }
                loadGrnHistorySearchInfo(grnhistory.searchText,grnhistory.searchType,grnhistory.startTime,grnhistory.endTime);
    });
        
        $("#grn-history-search-text").live("keyup", function(e) {
                var keyCode = e.keyCode || e.which;
        if(keyCode == 13){
                $("#grn-history-search-button").click();
        }
        });
        
        $("#grn-history-paginated .next").live('click', function() {
                var start = $( "#grn-history-paginated .start" ).text();
                var end = $( "#grn-history-paginated .end" ).text();
                getGrnHistoryNextItems(start,end,grnhistory.searchText,grnhistory.searchType,grnhistory.startTime,grnhistory.endTime);
                $("#grn-history-paginated .next").blur();
                
    });
        
        $("#grn-history-paginated .previous").live('click', function() {
                var start = $( "#grn-history-paginated .start" ).text();
                var end =  $( "#grn-history-paginated .end" ).text();
                var pre = end - 20;
                getGrnHistoryPreviousItems(start,end,pre,grnhistory.searchText,grnhistory.searchType,grnhistory.startTime,grnhistory.endTime);
                $("#grn-history-paginated .previous").blur();
    });
        
        $("#grn-history-date-button").live('click', function() {
                var dateRange = $('#reportrange span').text();
                grnhistory.startTime = new Date(dateRange.split("-")[0]).getTime();
                grnhistory.endTime = new Date(dateRange.split("-")[1]).getTime();
                grnhistory.searchType = "dateFilter";
                grnhistory.searchText = "";
                loadGrnHistorySearchInfo(grnhistory.searchText,grnhistory.searchType,grnhistory.startTime,grnhistory.endTime);
    });
        
        //grn-details
        
        $(".grn-details").live('click',function(){
                var purchaseId = $(this).attr('data');
                console.log(purchaseId);
                loadGrnDetails(purchaseId,"grn-details-container");
        });
        
        $(".pending-grn").live('click',function(){
                var purchaseReference = $(this).attr('data');
                console.log(purchaseReference);
                loadPendingGrnDetails(purchaseReference,"main-content");
        });
        
        //add-to-bag
        
        $(".add-to-bag").live('click', function() {
                var itemId = $(this).attr('data');
                $("#bagModel .itemId").val(itemId);
                $("#bagModel .quantity").val(0);
                jQuery.ajax({
                        url: context+"/checkItemAvailability/?itemId="+itemId,
                        type: 'GET',
                        contentType:'application/json',
                        async: false,
                        success: function (data) {
                                var obj = JSON.parse(data);
                                console.log(obj);
                                console.log(obj.displayName);
                                $(".modal-body h4.bagModalProductInfo.modelHeaderCustom > span").text(obj.displayName);
                                $(".modal-body h4.bagModalAvailability.modelHeaderCustom > span").text(obj.availability);
                                $("#bagModel .availability").val(obj.availability);
                        },
                        error : function() {
                                alert("OOPS!!!Failed to do changes.Try Again.",'ERROR');
                        },
                        cache: false,
                        processData: false
                });
        });
        
        //addToBagButton
        
        $("#addToBagButton").live('click', function() {
                var itemId = $(this).attr('data');
                var itemId = $("#bagModel .itemId").val();
                var tempAvailability = $("#bagModel .availability").val();
                var quantity = $("#bagModel .quantity").val();
                
                if (!quantity || parseInt(quantity) <= 0 || isNaN(parseInt(quantity)) || quantity.indexOf('.') != -1){
                        alert("Please enter valid quantity");
                        return;
                }
                
                jQuery.ajax({
                        url: context+"/checkItemAvailability/?itemId="+itemId,
                        type: 'GET',
                        contentType:'application/json',
                        async: false,
                        success: function (data) {
                                var obj = JSON.parse(data);
                                console.log(obj);
                                if (obj.availability < parseInt(quantity) || obj.availability == 0){
                                        alert("You don't have enough inventory of this item");
                                        return;
                                }
                                var obj = {'itemId':obj.itemId,'displayName':obj.displayName,'quantity':parseInt(quantity),'iconUrl':obj.iconUrl};
                                console.log(obj);
                                addItemInLocalStorage(obj);
                                alert("Item added to bag successfully");
                                $('#bagModel').modal('hide');
                        },
                        error : function() {
                                alert("OOPS!!!Failed to do changes.Try Again.",'ERROR');
                        },
                        cache: false,
                        processData: false
                });
        });
        
        $(".cart-trash").live('click',function(){
                var itemId = $(this).attr('data');
                removeItemFromLocalStorage(itemId);
                $('div[itemId="' + itemId + '"]').remove();
                if ($('#cart-details').html().trim() == ""){
                        $('#cart-details-empty').show();
                        $('#cart-checkout').hide();
                }
        });
        
        $(".cart-qty").live('change paste keyup mouseup', function() {
                var itemId = $(this).attr('data');
                var maxQty = $(this).attr('max');
                var qty = $(this).val();
                if (!qty){
                        qty = 0;
                }
                if(parseInt(qty) < 0){
                        alert("Invalid quantity");
                        $(this).val(0);
                        qty = 0;
                }
                if (parseInt(qty) > parseInt(maxQty)){
                        alert("You don't have availability of "+qty);
                        $(this).val(maxQty);
                        qty = maxQty;
                }
                changeQuantityInLocalStorage(itemId, qty);
        });
        
        $("form#cd input.unitPrice").live('change paste keyup mouseup', function() {
                var itemId = $(this).attr('itemId');
                var qty = $(this).attr('quantity');
                var unitPrice = $(this).val();
                if (!unitPrice){
                        unitPrice = 0;
                }
                if(parseFloat(unitPrice) < 0){
                        alert("Invalid unit price");
                        $(this).val(0);
                        unitPrice = 0;
                }
                var netPayableAmount = 0;
                $("form#cd input.totalPrice").each(function(){
                        if ($(this).attr("itemId") === itemId){
                                $(this).val(parseFloat(unitPrice)*parseInt(qty));
                        }
                        netPayableAmount = netPayableAmount + parseFloat($(this).val()); 
                });
                $("form#cd input.netPayableAmount").val(netPayableAmount);
        });

});