Subversion Repositories SmartDukaan

Rev

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

function addNewRowForLineItem(table) {
    $(table).find('#sample-lineitem').clone(true).insertAfter($(table).find('tbody>tr:last'));

    var newRow = $(table).find('tbody>tr:last');

    // Enabling the new record added
    $(newRow).find('input').removeAttr('disabled');
    $(newRow).removeAttr('id');

    // Making this row visible
    $(newRow).show();
}

function updateModelNameXferPrice(trElement, itemId, supplierId) {
    $.ajax({
        type      : 'GET',
        url       : '/inventory/purchase-order!getModelNameXferPrice',
        data      : 'itemId=' + itemId + '&supplierId=' + supplierId,
        success   : function(response) {
            $(trElement).find('div#modelName').html(response.split('~')[0]);
            $(trElement).find('input[name="unitPrice"]').val(response.split('~')[1]);
        }
    });
}

function closePO(id){
        $.ajax({
        type      : 'GET',
        url       : '/inventory/purchase-order!closePO',
        data      : 'id=' + id,
        success   : function(response) {
            window.location.href = "/inventory/purchase-order";
        },
                error     : function(response) {
                        alert("Error in closing PO");
                }
    });
}

function updateTotalAmount(supplierId) {
    var totalAmount = 0;
    var totalQuantity = 0;

    $('div#lineitems-' + supplierId + ' table tbody>tr').each(function(index, element) {
        // Ignoring the first sample row
        if (index == 0) {
            return;
        }

        var price = $(element).find('input[name="unitPrice"]').val();
        var quantity = $(element).find('input[name="quantity"]').val();
        var amount = price * quantity;
        totalAmount += amount;
        totalQuantity += (1 * quantity);
        var amountInput = $(element).find('div#amount');
        $(amountInput).html(amount);
    });

    $('#total-amount-' + supplierId).html(totalAmount);
    $('#total-quantity-' + supplierId).html(totalQuantity);
}

function validateForm() {
    return true;
}

function submitPurchaseOrderForm(form) {
    if (validateForm()) {
        // index starts from zero, Also we need to ignore first sample row
        $(form).find('tbody>tr').each(function(rowIndex, rowElement) {
            // Skipping first sample row
            if (rowIndex != 0) {
                $(rowElement).find('input').each(function(inputIndex, inputElement) {
                    if ($(inputElement).attr('name').indexOf('lineItems[') == -1) {
                        $(inputElement).attr('name', 'lineItems[' + (rowIndex - 1) + '].' + $(inputElement).attr('name'));
                    }
                });
            }
        });

        $.ajax({
            type      : 'POST',
            url       : '/inventory/purchase-order',
            data      : $(form).serialize(),
            success   : function(response) {
                var responseInteger = parseInt(response);
                if (responseInteger > 0) {
                    document.location.href = '/inventory/purchase-order/' + responseInteger + '/edit';
                }
                else {
                    alert(response);
                }
            }
        });
    }
}

function submitEditPurchaseOrderForm(form, purchaseOrderId) {
    if (validateForm()) {
        // index starts from zero, Also we need to ignore first sample row
        $(form).find('tbody>tr').each(function(rowIndex, rowElement) {
            // Skipping first sample row
            if (rowIndex != 0) {
                $(rowElement).find('input').each(function(inputIndex, inputElement) {
                    if ($(inputElement).attr('name').indexOf('lineItems[') == -1) {
                        $(inputElement).attr('name', 'lineItems[' + (rowIndex - 1) + '].' + $(inputElement).attr('name'));
                    }
                });
            }
        });

        $.ajax({
            type      : 'POST',
            url       : '/inventory/purchase-order!update',
            data      : $(form).serialize(),
            success   : function(response) {
                var responseInteger = parseInt(response);
                if (responseInteger > 0) {
                    document.location.href = '/inventory/purchase-order/' + responseInteger + '/edit';
                }
                else {
                    alert(response);
                }
            }
        });
    }    
}