Subversion Repositories SmartDukaan

Rev

Rev 32145 | Rev 32195 | 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', ".warehouse-receive-new-invoice", function() {
                loadNewReceiveInvoice("main-content");
        });







        $(document)
                .on(
                        'input',
                        '#invoice',
                        function() {
                                if (confirm('Document has been selected, Do you want to upload ?')) {
                                        var fileSelector = $('#invoice')[0];
                                        if (fileSelector != undefined
                                                && fileSelector.files[0] != undefined) {
                                                var url = context + '/document-upload';

                                                console.log(url);
                                                var file = this.files[0];
                                                doAjaxUploadRequestHandler(
                                                        url,
                                                        'POST',
                                                        file,
                                                        function(response) {
                                                                console.log(response);
                                                                var documentId = response.response.document_id;
                                                                console.log("documentId : "
                                                                        + documentId);
                                                                $("#invoicehidden").val(documentId);
                                                        });

                                        }
                                } else {
                                        // Do nothing!
                                }
                        });

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

                var invoiceDate = $('#actualDate').val();

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

                var numItems = $('#numberofItems').val();

                var receivedBy = $('#authUser').val();

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

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

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


                var invoiceDoc = $("#invoicehidden").val();


                console.log(invoiceDoc)


                if (invoiceDate === "" && warehouseId === "" && numItems === "" && receivedBy === "" && supplierId === "" && totalValue === "" && invoiceNumber === "" && invoiceDoc === "") {
                        alert("Field can't be empty");
                        return false;
                }


                if (invoiceDate === "") {
                        alert("please select date");
                        return false;
                }
                if (warehouseId === "") {
                        alert("please choose warehouse");
                        return false;
                }

                if (numItems === "") {
                        alert("please fill number of items.");
                        return false;
                }
                if (receivedBy === "") {
                        alert("please choose received by  user");
                        return false;
                }

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



                if (totalValue === "") {
                        alert("please fill Total Value.");
                        return false;
                }

                if (invoiceNumber === "") {
                        alert("please fill Invoice Number.");
                        return false;
                }


                if (invoiceDoc === "") {
                        alert("please upload invoice.");
                        return false;
                }
                var newReceiveInvoice = {};

                newReceiveInvoice['invoiceDate'] = invoiceDate;
                newReceiveInvoice['warehouseId'] = warehouseId
                newReceiveInvoice['numItems'] = numItems
                newReceiveInvoice['receivedBy'] = receivedBy
                newReceiveInvoice['supplierId'] = supplierId
                newReceiveInvoice['totalValue'] = totalValue
                newReceiveInvoice['invoiceNumber'] = invoiceNumber
                newReceiveInvoice['invoiceDoc'] = invoiceDoc

                if (confirm("Are you sure you want to add new Invoice") == true) {

                        doPostAjaxRequestWithJsonHandler(context
                                + "/newReceiveInvoice", JSON
                                        .stringify(newReceiveInvoice), function(response) {
                                                if (response == 'true') {
                                                        alert("successfully added");
                                                        loadNewReceiveInvoice("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);
                });
        });


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


                var purchaseItemIds = [];
                var totalQty = 0

                $("table > tbody > tr").each(function() {
                        var itemId = $(this).find(".transferPrice").data('itemid');
                        var qty = $(this).find(".qty").val();

                        console.log(qty)

                        if (qty != undefined) {
                                totalQty += parseInt(qty)
                        }

                        console.log(totalQty)


                        if (itemId != undefined) {
                                purchaseItemIds.push(itemId)
                        }
                });

                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 (purchaseItemIds.indexOf(itemId) > -1) {
                                alert("item already selected");
                                $html.find('.typeaheaditem').val("")

                                return false;
                        }

                        purchaseItemIds.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', '.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);

                });
        });




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

                var row = $(this).closest("tr");
                row.remove();
        });

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

                var numitems = $(this).data("numitems");
                var invoiceId = $(this).data("invoiceid");


                var invoiceItemIds = [];
                var totalQty = 0

                $("#invoice-order-table > tbody > tr").each(function() {
                        var itemId = $(this).find(".rate").data('itemid');
                        var qty = $(this).find(".qty").val();

                        console.log(qty)

                        if (qty != undefined) {
                                totalQty += qty
                        }

                        console.log(totalQty)


                        if (itemId != undefined) {
                                invoiceItemIds.push(itemId)
                        }
                });

                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 rate"  disable placeholder="Rate"/> </td>' +
                        '<td> <input class="form-control btn btn-primary removeInInvoiceItem" type="button" value="Remove"></td>  </tr> ');


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


                        console.log(invoiceItemIds)

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

                                return false;
                        }
                        $html.find('.rate').data('itemid', itemId)

                        $html.find('.removeInInvoiceItem').data('invoiceid', invoiceId)
                        $html.find('.removeInInvoiceItem').data('itemid', itemId)

                });


                if (totalQty < numitems) {

                        $('#invoice-order-table tbody').append($html);
                }

        });





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

                var numItems = $(this).data("numitems");
                var invoiceId = $(this).data("invoiceid");
                var warehouseId = $(this).data("warehouseid");
                var supplierId = $(this).data("supplierid");
                getInvoiceItemData(numItems, invoiceId, warehouseId, supplierId, false);

        });



        $(document).on('click', '.validateInvoiceDetail', function() {
                var numItems = $(this).data("numitems");
                var invoiceId = $(this).data("invoiceid");
                var warehouseId = $(this).data("warehouseid");
                var supplierId = $(this).data("supplierid");

                getInvoiceItemData(numItems, invoiceId, warehouseId, supplierId, true);

        });


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

                var row = $(this).closest("tr");
                var invoiceId = $(this).data("invoiceid");
                var itemId = $(this).data("itemid");
                if (invoiceId != undefined && itemId != undefined) {
                        doDeleteAjaxRequestHandler(context + "/removeInvoiceItem?invoiceId="
                                + invoiceId + "&itemId=" + itemId, function(response) {
                                        if (response == "true") {
                                                alert("Item removed successfully");

                                        }
                                });
                }

                row.remove();

        });


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

                var grnItemArray = [];

                var invoiceId = $(this).data("invoiceid");


                $("#invoice-purchase-validate > tbody > tr").each(function() {
                        var grnItemJson = {};

                        var itemId = $(this).find(".itemId").text();
                        var qty = $(this).find(".qty").text();
                        var poId = $(this).find(".purchaseId").text();

                        var itemType = $(this).find(".itemType").text();

                        console.log(itemType);

                        if (itemType == "Serialized") {
                                var serialNumbers = $(".imeis-to-grn").tagsinput('items');
                                grnItemJson['serialNumbers'] = serialNumbers

                        }
                        console.log(serialNumbers)
                        grnItemJson['itemId'] = itemId;
                        grnItemJson['serialNumbers'] = serialNumbers
                        grnItemJson['qty'] = qty
                        grnItemJson['poId'] = poId
                        grnItemJson['invoiceId'] = invoiceId
                        grnItemArray.push(grnItemJson)

                        console.log(grnItemArray)

                });

                for (var i = 0; i < grnItemArray.length; i++) {

                        var serialNumbers = grnItemArray[i].serialNumbers.length

                        if (serialNumbers > 0) {

                                var serialNumbers = grnItemArray[i].serialNumbers.length
                                var qty = grnItemArray[i].qty

                                if (serialNumbers != qty) {
                                        alert("serial number is not matched with qty")
                                        return false;
                                }
                        }

                }


                console.log(JSON.stringify(grnItemArray))
                if (confirm("Are you sure you want to grn purchase order") == true) {

                        doPostAjaxRequestWithJsonHandler(context
                                + "/createGrn", JSON
                                        .stringify(grnItemArray), function(response) {
                                                if (response == 'true') {
                                                        alert("successfully done");
                                                }
                                        });
                }



        });


});

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



function loadOpenPurchase(domId) {

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

function loadNewReceiveInvoice(domId) {
        doGetAjaxRequestHandler(context + "/newReceiveInvoice", 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");
                                        }
                                });
        }

}


function getInvoiceItemData(numItems, invoiceId, warehouseId, supplierId, validate) {

        var invoiceJson = {};
        console.log(numItems)
        var totalQty = 0;
        var invoiceItems = []
        $("#invoice-order-table > tbody > tr").each(function() {
                var invoiceItemJson = {};
                var itemId = $(this).find(".rate").data('itemid');
                var qty = $(this).find(".qty").val();

                if (qty != undefined) {
                        totalQty += parseInt(qty)
                }
                var rate = $(this).find(".rate").val();
                invoiceItemJson['itemId'] = itemId;
                invoiceItemJson['qty'] = qty
                invoiceItemJson['rate'] = rate
                invoiceItems.push(invoiceItemJson)
        });


        for (var i = 0; i < invoiceItems.length; i++) {

                var itemId = invoiceItems[i].itemId
                var qty = invoiceItems[i].qty
                var rate = invoiceItems[i].rate
                if (itemId === "" && qty === "" && rate === "") {
                        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 (rate === "") {
                        alert("Rate can't be empty");
                        return false;
                }

        }

        console.log(totalQty)

        if (totalQty > numItems) {
                alert("Qty Should not be more than  Invoice items");
                return false;
        }

        if (validate) {

                if (totalQty != numItems) {
                        alert("Qty Should not match with Invoice items");
                        return false;
                }
        }


        invoiceJson['invoiceId'] = invoiceId;
        invoiceJson['warehouseId'] = warehouseId;
        invoiceJson['supplierId'] = supplierId;
        invoiceJson['invoiceItems'] = invoiceItems
        if (validate) {
                validateInvoiceItems(invoiceJson);
        } else {
                saveInvoiceItems(invoiceJson)
        }
}


function saveInvoiceItems(invoiceJson) {


        if (confirm("Are you sure you want to save invoice item") == true) {

                doPostAjaxRequestWithJsonHandler(context
                        + "/invoiceItemDetail", JSON
                                .stringify(invoiceJson), function(response) {
                                        if (response == 'true') {
                                                getInvoiceItems(invoiceJson.invoiceId);
                                        }
                                });
        }

}


function validateInvoiceItems(invoiceJson) {


        if (confirm("Are you sure you want to validate invoice item") == true) {

                doPostAjaxRequestWithJsonHandler(context
                        + "/validateInvoiceItemDetail", JSON
                                .stringify(invoiceJson), function(response) {
                                        $('.invoiceadditemcontainer').html(response);

                                });
        }

}

function getOpenPOFromWarehouse() {

        var warehouseId = $('#warehouseMap').val();
        doGetAjaxRequestHandler(context + "/getOpenPurchaseOrderByWarehouseId?warehouseId=" + warehouseId, function(response) {
                $('#warehouseManagePuchaseContainer').html(response);
        });



}

function getPurchaseLineitem(poId) {


        doGetAjaxRequestHandler(context + "/getPurchaseOrderItemByPoId?poId=" + poId, function(response) {
                $('#warehouseManagePuchaseItemContainer').html(response);
                $('#warehousepurchaseitemgrn').hide()
        });


}


function getInvoiceItems(invoiceId) {

        doGetAjaxRequestHandler(context + "/getInvoiceItems?invoiceId=" + invoiceId, function(response) {
                $('.invoiceadditemcontainer').html(response);

        });
}