Subversion Repositories SmartDukaan

Rev

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

$(function() {
        $(document).on('click', ".warehouse-create-purchase-order", function() {
                loadCreatePurchase("main-content");
        });



        $(document).on('click', ".purchaseorderitemview", function() {

                var vendorId = $('#vendorId').val();
                var warehouseId = $('#warehouseMap').val();

                console.log(warehouseId)

                if (warehouseId == null && vendorId === "") {
                        alert("Field can't be empty");
                        return;
                }


                if (warehouseId == null) {
                        alert("please select warehouse");
                        return;
                }
                if (vendorId === "") {
                        alert("please select vendor");
                        return;
                }
                doGetAjaxRequestHandler(context + "/addPurchaseItemView?warehouseId=" + warehouseId + "&vendorId=" + vendorId, function(response) {
                        $(".createpurchaseordercontainer").html(response);
                });
        });

        var itemIds = [];

        $(document).on('click', ".addRowInPurchaseOrder", function() {
                var $html = $('<tr>  <td>  <input type="text" class="form-control typeaheaditem"   autocomplete="off"  placeholder="Search Model"/> </td> <td>  <input type="number" class="form-control qty"  name = "qty" placeholder="Quantity"/> </td><td>  <input type="number" class="form-control transferPrice"  disable placeholder="Transfer Price"/> </td>   <td>  <input type="number" class="form-control totalValue"  disable placeholder="Total Value"/> </td> <td> <input class="form-control btn btn-primary removeInPurchaseOrder" type="button" value="Remove"></td>  </tr> ');


                getItemAheadOptions($html.find('.typeaheaditem'), true, function(
                        selectedItem) {
                        var itemId = selectedItem.itemId;

                        var vendorId = $('#vendorId').val();

                        if (itemIds.indexOf(itemId) > -1) {
                                alert("item already selected");
                                $html.find('.typeaheaditem').val("")

                                return false;
                        }

                        itemIds.push(itemId)



                        doGetAjaxRequestHandler(context + "/getPricing?vendorId=" + vendorId + "&itemId=" + itemId, function(response) {
                                console.log(response)
                                if (response != "null") {
                                        var jsonObj = JSON.parse(response);

                                        console.log(jsonObj)
                                        $html.find('.transferPrice').val(jsonObj.transferPrice)

                                        $html.find('.transferPrice').data('itemid', itemId)
                                } else {

                                        console.log(response)
                                        $html.find('.typeaheaditem').val("")

                                }
                        });

                });
                $('tbody').append($html);

        });

        $(document).on('click', '.createSendPurchaseOrder', function() {

                createPurchase(true);

        });


        $(document).on('click', '.createPurchaseOrder', function() {


                createPurchase(false);
        });


        $(document).on('change', '.qty', function() {


                var row = $(this).closest("tr");
                var qty = $(row).find(".qty").val();
                var transferPrice = $(row).find(".transferPrice").val();

                var totalVal = transferPrice * qty;

                var totalValue = $(row).find(".totalValue").val(totalVal);

                var purchaseOrderValue = 0;

                $("table > tbody > tr").each(function() {

                        var totalValue = $(this).find(".totalValue").val();
                        purchaseOrderValue += totalValue

                });

        });



        $(document).on("click", '.removeInPurchaseOrder', function() {

                var row = $(this).closest("tr");
                var itemId = $(this).find(".transferPrice").data('itemid');
                itemIds.splice(itemIds.indexOf(itemId), 1);
                row.remove();
        });


        $(document).on('click', '.warehouse-open-purchase-order', function() {
                loadOpenPurchase("main-content");
        });



        $(document).on('click', '.editViewPurchaseOrder', function() {
                var purchaseOrderId = $(this).data('poid');
                doGetAjaxRequestHandler(context + "/getEditPOByPurchaseId?purchaseId=" + purchaseOrderId, function(response) {
                        $('#warehouseEditPurchaseContainer .modal-content').html(response);
                });

        });

        $(document).on('click', '.editPurchaseOrder', function() {
                var purchaseOrderId = $(this).data('poid');

                editPurchase(true, purchaseOrderId);

        });


        $(document).on('click', '.editSendPurchaseOrder', function() {
                var purchaseOrderId = $(this).data('poid');


                editPurchase(false, purchaseOrderId);
        });

        $(document).on('click', '.closePurchaseOrder', function() {
                var purchaseOrderId = $(this).data('poid');
                if (confirm("Are you sure you want to close the PO") == true) {
                        doPostAjaxRequestHandler(context + "/closePuchaseOrder?purchaseId=" + purchaseOrderId,
                                function(response) {

                                        console.log(response);
                                        if (response == 'true') {
                                                alert("successfully closed");
                                                loadOpenPurchase("main-content")
                                        }
                                });
                }
        });


        $(document).on('click', '.warehouse-view-purchase-order', function() {
                doGetAjaxRequestHandler(context + "/viewPurchaseOrder", function(response) {
                        $('#' + 'main-content').html(response);
                });
        });



        $(document).on('click', '.dateWisePo', function() {

                var startDate = getDatesFromPicker('input[name="duration"]').startDate;
                var endDate = getDatesFromPicker('input[name="duration"]').endDate


                doGetAjaxRequestHandler(context + "/getPurchaseOrders?startDate=" + startDate + "&endDate=" + endDate, function(response) {
                        $('.purchaseorderviewcontainer').html(response);

                });


        });



        $(document).on('click', '.viewPurchaseOrderlineItem', function() {
                var purchaseOrderId = $(this).data('poid');

                doGetAjaxRequestHandler(context + "/getWarehouseLineItemByPurchaseId?purchaseId=" + purchaseOrderId, function(response) {
                        $('#warehouseLineItem .modal-content').html(response);

                });
        });



});

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

function loadOpenPurchase(domId) {

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

function createPurchase(sendPo) {
        var purchaseOrder = {};

        var vendorId = $('#vendorId').val();

        var warehouseId = $('#warehouseMap').val();


        var items = []
        $("table > tbody > tr").each(function() {
                var purchaseOrderJson = {};

                var itemId = $(this).find(".transferPrice").data('itemid');
                var qty = $(this).find(".qty").val();
                var transferPrice = $(this).find(".transferPrice").val();
                var totalValue = $(this).find(".totalValue").val();


                purchaseOrderJson['itemId'] = itemId;
                purchaseOrderJson['qty'] = qty
                purchaseOrderJson['totalValue'] = totalValue
                purchaseOrderJson['transferPrice'] = transferPrice

                items.push(purchaseOrderJson)
        });


        for (var i = 0; i < items.length; i++) {
                console.log(items[i])

                var itemId = items[i].itemId
                var qty = items[i].qty
                var transferPrice = items[i].transferPrice
                var totalValue = items[i].totalValue

                if (itemId === "" && qty === "" && transferPrice === "" && totalValue === "") {
                        alert("Field can't be empty");
                        return false;
                }


                if (itemId === "") {
                        alert("please select item");
                        return false;
                }
                if (qty === "") {
                        alert("please choose qty");
                        return false;
                }

                if (transferPrice === "") {
                        alert("Transfer Price can't be empty");
                        return false;
                }
                if (totalValue === "") {
                        alert("Total value can't be empty");
                        return false;
                }



        }




        purchaseOrder['vendorId'] = vendorId;
        purchaseOrder['warehouseId'] = warehouseId
        purchaseOrder['sendPo'] = sendPo

        purchaseOrder['items'] = items

        console.log(purchaseOrder)



        if (confirm("Are you sure you want to create purchase order") == true) {

                doPostAjaxRequestWithJsonHandler(context
                        + "/createPurchaseOrder", JSON
                                .stringify(purchaseOrder), function(response) {
                                        if (response == 'true') {
                                                alert("successfully created");
                                        }
                                });
        }

}


function editPurchase(sendPo, poid) {
        var editpurchaseOrder = {}
        var items = []
        $("#purchase-edit-lineitem > tbody > tr").each(function() {
                var purchaseOrderJson = {};
                var itemId = $(this).find("td:eq(0)").text();

                var qty = $(this).find(".editqty").val();

                purchaseOrderJson['itemId'] = itemId;
                purchaseOrderJson['qty'] = qty

                items.push(purchaseOrderJson)
        });


        for (var i = 0; i < items.length; i++) {
                console.log(items[i])

                var qty = items[i].qty

                if (qty === "") {
                        alert("please choose qty");
                        return false;
                }

                if (qty < 0) {
                        alert("Quantity should be greater than 0");
                        return false;
                }


        }

        editpurchaseOrder['purchaseOrderId'] = poid;
        editpurchaseOrder['sendPo'] = sendPo
        editpurchaseOrder['items'] = items


        if (confirm("Are you sure you want to edit purchase order") == true) {

                doPostAjaxRequestWithJsonHandler(context
                        + "/editPurchaseOrder", JSON
                                .stringify(editpurchaseOrder), function(response) {
                                        if (response == 'true') {
                                                alert("successfully created");
                                        }
                                });
        }

}