Subversion Repositories SmartDukaan

Rev

Rev 24406 | Rev 24556 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

$(function() {
        $("a.allocation_po").live('click', function(){
                currentFofoId=0;
                counterSize='';
                loadIndent('main-content');
        });
        $("button.mk_pause_button").live('click', function(){
                var clickedButton = $(this);
                var description = clickedButton.data("description");
                var catalogId = clickedButton.data("id");
                getColorsForItems(catalogId, 0, "Active/pause " + description, function(itemIds){
                        console.log(itemIds);
                        if(itemIds!=null) {
                                bootbox.confirm("Confirm Update " + clickedButton.data("description") + "?", function(result){
                                        if(result) {
                                                coloredItems.forEach(function(item){
                                                        item.active = itemIds.indexOf(item.id) >= 0;
                                                });
                                                doPostAjaxRequestWithJsonHandler(context+"/indent/confirm-pause/", JSON.stringify(coloredItems), function(response) {
                                                        if(response) {
                                                                bootbox.alert("Item/s  Updated Successfully");
                                                        } else {
                                                                bootbox.alert("Could not pause item");
                                                        }
                                                });
                                        }
                                });
                        }
                });
        });
        $("button.raise_po").live('click', function(){
                if (confirm("Please confirm the PO")) {
                        raisePO();
                } else {
                        return;
                }
        });
        $("li.mk_counter_size").live('click', function(){
                if($(this).hasClass('active')){
                        return;
                }
                currentFofoId = 0;
                counterSize=$(this).find('a').html();
                loadIndent('main-content');
        });
        $("li.brandLi").live('click', function(){
                if(!$(this).hasClass('active')) {
                        $("table.brandPerformance").toggle();
                        $("li.brandLi").removeClass('active');
                        $(this).addClass('active');
                }
        });
        $("a.create_indent").live('click', function() {
                currentFofoId=0;
                counterSize='';
                loadIndent("main-content");
        });
        $("button.create_indent").live('click', function() {
                loadIndent("main-content");
        });
        
        $("button.download_indent").live('click', function(){
                downloadIndent();
        })

        $("#entire-catalog-table input").live({
                change : function() {
                        var catalogId = parseInt($(this).data("catalog-id"));
                        var sellingPrice = parseInt($(this).data("selling-price"));
                        var quantity = parseInt($(this).val());
                        if (quantity > 10) {
                                alert("Quantity should not be above 10");
                                if (catalogId in indentMap) {
                                        $(this).val(indentMap[catalogId].quantity);
                                } else {
                                        $(this).val(0);
                                }
                                return;
                        }
                        description = $(this).data("description");
                        if (catalogId in indentMap) {
                                indentMap[catalogId]["quantity"] = quantity;
                                indentMap[catalogId]["sellingPrice"] = sellingPrice;
                                indentMap[catalogId]["description"] = description;
                                indentMap[catalogId]["catalogId"] = catalogId;
                                indentMap[catalogId]["diff"] =  quantity - indentMap[catalogId]["initialQuantity"];
                        } else {
                                indentMap[catalogId] = {
                                        "quantity" : quantity,
                                        "sellingPrice" : sellingPrice,
                                        "description" : description,
                                        "catalogId" : catalogId,
                                        "initialQuantity" : 0,
                                        "diff" : quantity
                                };
                        }
                        populateIndentSummary();
                }
        });
        $("button.mk_submit_indent")
                        .live(
                                        'click',
                                        function() {
                                                confirmString = [];
                                                confirmString
                                                                .push('<table class="table table-striped table-condensed table-bordered" id="entire-catalog-table">');
                                                confirmString.push('<tr>');
                                                confirmString.push('<th style="width:100px">Catalog Id</th>');
                                                confirmString.push('<th style="width:250px">Description</th>');
                                                confirmString.push('<th style="width:150px">Unit Price</th>');
                                                confirmString.push('<th style="width:150px">Quantity</th>');
                                                confirmString.push('<th style="width:200px">Item total</th>');
                                                confirmString.push('</tr>');
                                                changedItemIds.forEach(function(itemId){
                                                        confirmString.push('<tr style="background-color:#9C7D7E">');
                                                        confirmString.push('<td>' + itemId + '</td>');
                                                        confirmString.push('<td>'
                                                                        + indentMap[itemId].description
                                                                        + '</td>');
                                                        confirmString
                                                        .push('<td style="text-align:left">'
                                                                        + numberToComma(indentMap[itemId].sellingPrice)
                                                                        + '</td>');
                                                        confirmString
                                                        .push('<td style="text-align:left">'
                                                                        + indentMap[itemId].quantity + '(' + (indentMap[itemId].diff > 0 ? '+' : '-') + Math.abs(indentMap[itemId].diff) 
                                                                        + ' pcs)</td>');
                                                        confirmString
                                                        .push('<td style="text-align:left">'
                                                                        + numberToComma(indentMap[itemId].quantity
                                                                                        * indentMap[itemId].sellingPrice)
                                                                                        + '</td>');
                                                        confirmString.push('</tr>');
                                                });
                                                for ( var itemId in indentMap) {
                                                        if (itemId.indexOf(changedItemIds) > 0){
                                                                continue;
                                                        }
                                                        if (indentMap[itemId].quantity > 0) {
                                                                confirmString.push('<tr>');
                                                                confirmString.push('<td>' + itemId + '</td>');
                                                                confirmString.push('<td>'
                                                                                + indentMap[itemId].description
                                                                                + '</td>');
                                                                confirmString
                                                                                .push('<td style="text-align:left">'
                                                                                                + numberToComma(indentMap[itemId].sellingPrice)
                                                                                                + '</td>');
                                                                confirmString
                                                                                .push('<td style="text-align:left">'
                                                                                                + indentMap[itemId].quantity
                                                                                                + '</td>');
                                                                confirmString
                                                                                .push('<td style="text-align:left">'
                                                                                                + numberToComma(indentMap[itemId].quantity
                                                                                                                * indentMap[itemId].sellingPrice)
                                                                                                + '</td>');
                                                                confirmString.push('</tr>');
                                                        }
                                                }
                                                confirmString.push('</table>');
                                                $('#indent-container1').html(confirmString.join(''))
                                                                .show();
                                                $('#indent-container').hide();
                                                $('button.mk_submit_indent').hide();
                                                $('button.bk_toedit_indent').show();
                                                $('button.confirm_indent').show();
                                        });
        $("button.confirm_indent").live(
                        'click',
                        function() {
                                var itemQtyList = [];
                                for (catalogId in indentMap) {
                                        diff = indentMap[catalogId]["quantity"] - indentMap[catalogId]["initialQuantity"];
                                        if(diff!=0) {
                                                itemQtyList.push({
                                                        "quantity" : indentMap[catalogId]["quantity"],
                                                        "catalogId" : indentMap[catalogId]["catalogId"]
                                                });
                                        }
                                }
                                doPostAjaxRequestWithJsonHandler(context + "/open-indent/save?fofoId=" + currentFofoId + "&counterSize=" + counterSize,
                                                JSON.stringify(itemQtyList), function(data) {
                                                        alert('Indent has been created Successfully!');
                                                        indentMap = {};
                                                        loadIndent('main-content');
                                                });
                        });
        $("button.bk_toedit_indent").live('click', function() {
                $('button.bk_toedit_indent').hide();
                $('button.confirm_indent').hide();
                $('button.mk_submit_indent').show();
                $('#indent-container1').hide();
                $('#indent-container').show();

        });

});

function populateIndentSummary() {
        var totalQty = 0;
        var totalAmount = 0;
        var totalItems = 0;
        var arr = [];
        changedItemIds = [];
        arr[0]=[0,0,0];
        arr[1]=[0,0,0];
        arr[2]=[0,0,0];
        arr[3]=[0,0,0];

        for ( var itemId in indentMap) {
                var itemQty = indentMap[itemId]['quantity'];
                var previousItemQty = indentMap[itemId]['initialQuantity'];
                var sellingPrcice = indentMap[itemId]['sellingPrice'];
                if(previousItemQty !=0) {
                        arr[0][0]++;
                }
                if(itemQty !=0) {
                        arr[3][0]++;
                }
                var diff = itemQty - previousItemQty;
                arr[0][1] += previousItemQty;
                arr[0][2] += previousItemQty* sellingPrcice;
                arr[3][1] += itemQty;
                arr[3][2] += itemQty * sellingPrcice;
                if (diff != 0) {
                        changedItemIds.push(itemId);
                        if (diff > 0) {
                                arr[1][0]++;
                                arr[1][1] += diff;
                                arr[1][2] += (diff) * sellingPrcice;
                        } else {
                                arr[2][0]++;
                                arr[2][1] += diff;
                                arr[2][2] += diff * sellingPrcice;
                        }
                }
        }
        totalQty = arr[3][1];
        if (totalQty === 0) {
                $('button.mk_submit_indent').hide();
        } else {
                $('button.mk_submit_indent').show();
        }
        $("#summary-table").find("td").each(function(index, ele){
                if(index%3==2){
                        $(ele).html(numberToComma(arr[Math.floor(index/3)][2]));
                }else{
                        $(ele).html(arr[Math.floor(index/3)][index%3]);
                }
                
        });
}
function loadIndent(domId) {
        var url = "/indent/loadIndent?fofoId=" + currentFofoId + "&counterSize=" + counterSize;
        doAjaxRequestHandler(context + url, "GET", function(response) {
                $('#' + domId).html(response);
        });
}
function downloadIndent() {
        var url = "/indent/download?fofoId=" + currentFofoId + "&counterSize=" + counterSize;
        doAjaxGetDownload(url, "indent.csv")
}
function raisePO(){
        doPostAjaxRequestWithJsonHandler(context+"/indent/create-po", {}, function(response) {
                if(response) {
                        alert("PO Created Successfully");
                        $('a.allocation_po').click();                                                           
                } else {
                        alert("Problem while creating PO");
                }
        });
        
}