function showSalesQuotationItemData(obj){
    $('#txtSalesQuotationItemsId').val(obj[0].items_id);
    $('#txtsalesitemsubtotal').val(obj[0].sub_total);
    $('#cobsalesitemdisctype').val(obj[0].disc_type);
    $('#txtsalesitemdiscpercentage').val(obj[0].disc_percentage);
    $('#txtsalesitemdisctotal').val(obj[0].disc_total);
    $('#txtsalesitemgstpercentage').val(obj[0].gst_percentage);
    $('#txtsalesitemgsttotal').val(obj[0].gst_total);
    $('#txtsalesitemgrandtotal').val(obj[0].grand_total);
    $('#txtsalesitemqtytotal').val(obj[0].qty_total);

    var detail = obj[0].detail;
    sales_item_idx = 1; //detail.length == 0 ? 1:detail.length;
    sales_item_section_idx = 0;
    sales_item_counter = 0;
    sales_item_additional_counter = 0;
    var id_max_q = [1000];
    var a = 0;
    for(var i=0;i<detail.length;i++) {
        id = obj[0].items_id;
        line_no = detail[i].line_no;
        line_desc = detail[i].line_desc;
        line_comment = detail[i].line_comment;
        line_amount = detail[i].line_amount;
        line_disc = detail[i].line_disc;
        line_disc_desc = detail[i].line_disc_desc;
        line_unit_price = detail[i].line_unit_price;
        line_qty = detail[i].line_qty;
        line_phase = detail[i].line_phase;
        line_additional = detail[i].line_additional;

        next_line_phase = "";
        a = i + 1;
        if(a < detail.length)
            next_line_phase = detail[a].line_phase;

        id_max_q.push(id);

        if(line_phase == ""){
            sales_item_counter++;

            var itemDescField = "<input type='text' class='form-control' name='input_row' id='txtsalesitemdescription"+sales_item_idx+"' placeholder='Item Description..' value='"+line_desc+"'/>";
            if($('#txtSalesQuotationType').val()==1 && line_additional!=0) {
                itemDescField = "<select id='txtsalesitemdescription"+sales_item_idx+"' name='input_row' class='form-control'>"+$('#cmbItemAdditionalComb').html()+"</select>";
                sales_item_additional_counter++;
            }

            var newRow = "<tr data-row='sales-quotation-item-rows-"+sales_item_idx+"' id='sales-quotation-item-rows-"+sales_item_idx+"' class='dnd-moved kt-datatable__row' style='left: 0px;'> " +
                    "<td data-field='id' class='kt-datatable__cell' id='col_id"+sales_item_idx+"' style='display: none;'>" +
                    "<input type='text' class='form-control' name='input_row' id='txtsalesitemid"+sales_item_idx+"' value='"+id+"' />"+
                    "</td> " +

                    "<td data-field='lineno' class='kt-datatable__cell' id='col_lineno"+sales_item_idx+"' style='display: none;'>" +
                    "<input type='text' class='form-control' name='input_row' id='txtsalesitemlineno"+sales_item_idx+"' value='"+line_no+"' style='text-align: right;' />"+
                    "</td> " +
                    
                    "<td data-field='text' class='kt-datatable__cell' id='col_number"+sales_item_idx+"' style='width: 50px;vertical-align:top;'>"+                   
                    "<span>"+
                    "<input type='text' class='form-control' style='width:40px;text-align:right' name='input_row' id='txtsalesitemno"+sales_item_idx+"' placeholder='No.' value='"+sales_item_counter+"' />"+
                    "</span></td> " +

                    "<td data-field='description' class='kt-datatable__cell' id='col_description"+sales_item_idx+"' style='width: 600px;'>"+
                    "<span><div style='padding-right: 25px;'>"+
                    itemDescField +
                    "</div><div style='margin-top:10px;padding-left: 25px;width:600px;'>"+
                    "<textarea class='form-control' name='input_row' id='txtsalesitemcomment"+sales_item_idx+"' placeholder='Item Comment..' rows='5'>"+line_comment+"</textarea>"+
                    "</div><span></td>"+
                    
                    "<td data-field='unitprice' class='kt-datatable__cell' id='col_unitprice"+sales_item_idx+"' style='width: 50px;vertical-align:top;'>"+                   
                    "<input type='text' class='form-control' name='input_row' id='txtsalesitemunitprice"+sales_item_idx+"' placeholder='0.00' value='"+line_unit_price+"'  style='text-align:right' onchange='javascript:calculateAmount("+sales_item_idx+");' />" +
                    "</td> " +
                    
                    "<td data-field='qty' class='kt-datatable__cell' id='col_qty"+sales_item_idx+"' style='width: 50px;vertical-align:top;'>"+                   
                    "<input type='number' class='form-control' name='input_row' id='txtsalesitemqty"+sales_item_idx+"' placeholder='0.00' value='"+line_qty+"'  style='text-align:right' onchange='javascript:calculateAmount("+sales_item_idx+");' />" +
                    "</td> " +

                    "<td data-field='discount' class='kt-datatable__cell' id='col_amount"+sales_item_idx+"' style='width: 100px;vertical-align:top;'>"+
                    "<span>"+
                    // "<div style='visibility:hidden;'><input type='text' class='form-control' name='input_row' /></div>"+
                    "<input type='text' class='form-control' name='input_row' id='txtsalesitemdiscamount"+sales_item_idx+"' placeholder='0.00' style='text-align:right' onchange='javascript:calculateAmount("+sales_item_idx+");' value='"+line_disc+"' />" +
                    "<div style='margin-top:10px;'>"+
                    "<input type='text' class='form-control' name='input_row' id='txtsalesitemdiscreason"+sales_item_idx+"' placeholder='Discount ..' value='"+line_disc_desc+"' />" +
                    "</div><span></td>"+

                    "<td data-field='amount' class='kt-datatable__cell' id='col_amount"+sales_item_idx+"' style='width: 100px;vertical-align:top;'>"+
                    "<span><div >"+
                    "<input type='text' class='form-control' name='input_row' id='txtsalesitemamount"+sales_item_idx+"' placeholder='0.00' style='text-align:right' onchange='javascript:calculateSubTotal("+sales_item_idx+");' value='"+line_amount+"' />"+
                    "</div><div style='margin-top:10px;'>"+
                    "</div><span></td>"+

                    "<td data-field='delete' class='kt-datatable__cell' style='width: 50px;vertical-align:top' id='col_filter"+sales_item_idx+"'>" +
                    "<button type='button' class='btn btn-icon btn-danger btn-sm btn-icon-md' id='btnRemoveSalesItem' onclick='removeSalesItem("+sales_item_idx+");'><i class='fa fa-trash'></i></button></td> ";

                    // if(next_line_phase != "") {
                    //     newRow += "<td data-field='new' class='kt-datatable__cell' style='width: 50px;vertical-align:top' id='col_new"+sales_item_idx+"'>" +
                    //     "<button type='button' class='btn btn-icon btn-success btn-sm btn-icon-md' id='btnAddSalesItem"+sales_item_idx+"' onclick='addSalesQuotationItemData("+sales_item_idx+");'><i class='fa fa-plus'></i></button></td>";
                    // } else if(sales_item_idx == detail.length) {
                    //     newRow += "<td data-field='new' class='kt-datatable__cell' style='width: 50px;vertical-align:top' id='col_new"+sales_item_idx+"'>" +
                    //     "<button type='button' class='btn btn-icon btn-success btn-sm btn-icon-md' id='btnAddSalesItem"+sales_item_idx+"' onclick='addSalesQuotationItemData("+sales_item_idx+");'><i class='fa fa-plus'></i></button></td>";
                    // } else {
                        newRow += "<td data-field='new' class='kt-datatable__cell' style='width: 50px;' id='col_new"+sales_item_idx+"'></td>";
                    // }

                newRow += "</tr>";

                $('#btnAddSalesItem'+sales_item_section_idx).attr("onclick", "addSalesQuotationItemData("+sales_item_idx+")");

            // sales_item_idx++;
        } else {
            sales_item_section_idx++;
            sales_item_counter = 0;
            var newRow = "<tr data-row='sales-quotation-item-section-rows-"+sales_item_idx+"' id='sales-quotation-item-section-rows-"+sales_item_idx+"' class='dnd-moved kt-datatable__row' style='left: 0px;background-color: #eaeff3'>"+
                    "<td data-field='id' class='kt-datatable__cell' id='col_id"+sales_item_idx+"' style='display: none;'>"+
                    "<input type='text' class='form-control' name='input_row' id='txtsalesitemid"+sales_item_idx+"' value='"+id+"' />"+
                    "</td>"+

                    "<td data-field='lineno' class='kt-datatable__cell' id='col_lineno"+sales_item_idx+"' style='display: none;'>"+
                    "<input type='text' class='form-control' name='input_row' id='txtsalesitemlineno"+sales_item_idx+"' value='"+line_no+"' />"+
                    "</td>"+
                                    
                    "<td data-field='text' class='kt-datatable__cell' id='col_number"+sales_item_idx+"' style='width: 60px;vertical-align:top;'>  "+                 
                    "<span>"+
                    "<input type='text' class='form-control' name='input_row' id='txtsalesitemsectionphase"+sales_item_idx+"' placeholder='Phase' value='"+line_phase+"' />"+
                    "</span></td>"+

                    "<td data-field='description' class='kt-datatable__cell' id='col_description"+sales_item_idx+"'>"+
                    "<div>"+
                    "<input type='text' class='form-control' name='input_row' id='txtsalesitemsectiondescription"+sales_item_idx+"' placeholder='Description' value='"+line_desc+"' />"+
                    "</div></td>"+
                                    
                    "<td data-field='unitprice' class='kt-datatable__cell' style='width: 50px;vertical-align:top;'>"+
                    "<span><span></td>"+
                                    
                    "<td data-field='qty' class='kt-datatable__cell' style='width: 50px;vertical-align:top;'>  "+               
                    "<span><span></td>"+

                    "<td data-field='discount' class='kt-datatable__cell' style='width: 100px;vertical-align:top;'>"+
                    "<span><span></td>"+

                    "<td data-field='amount' class='kt-datatable__cell' style='width: 100px;vertical-align:top;'>"+
                    "<span><span></td>"+

                    "<td data-field='delete' class='kt-datatable__cell' style='width: 50px;vertical-align:top' id='col_filter"+sales_item_idx+"'>"+
                    "<button type='button' class='btn btn-icon btn-danger btn-sm btn-icon-md' id='btnRemoveSalesItemSection"+sales_item_idx+"' onclick='removeSalesItemSection("+sales_item_idx+");'><i class='fa fa-trash'></i></button></td>"+

                    // "<td data-field='new' class='kt-datatable__cell' style='width: 50px;' id='col_new"+sales_item_idx+"'></td>"+
                    "<td data-field='new' class='kt-datatable__cell' style='width: 50px;vertical-align:top' id='col_new"+sales_item_idx+"'>" +
                    "<button type='button' class='btn btn-icon btn-success btn-sm btn-icon-md' id='btnAddSalesItem"+sales_item_section_idx+"' onclick='addSalesQuotationItemData("+sales_item_idx+");'><i class='fa fa-plus'></i></button></td>"+
                    
                "</tr>";
            
        }
        $('#sales-quotation-item-list').append(newRow);

        if($('#txtSalesQuotationType').val()==1) {
            if(line_additional!=0) {
                $('#txtsalesitemdescription'+sales_item_idx).val(line_additional);
            }
        }

        sales_item_idx++;
    }
}

function addSalesQuotationItemData(item_idx) {
    if($('#txtSalesQuotationType').val()==1 && sales_item_additional_counter == 2) {
        swal.fire("System Warning", "Additional Item reached maximum number.", "warning");
        return;
    }

    var sno = 0;
    if(item_idx == undefined || item_idx == null) {
        sales_item_counter++;
        sno = sales_item_counter;
    } else {
        if($('#sales-quotation-item-rows-'+item_idx).length > 0) {
            sno = parseInt($('#txtsalesitemno'+item_idx).val()) + 1;
            sales_item_counter++;
        } else {
            sales_item_counter++;
            sno = sales_item_counter;
        }
    }

    var itemDescField = "<input type='text' class='form-control' name='input_row' id='txtsalesitemdescription"+sales_item_idx+"' placeholder='Item Description..'/>";
    if($('#txtSalesQuotationType').val()==1) {
        itemDescField = "<select id='txtsalesitemdescription"+sales_item_idx+"' name='input_row' class='form-control'>"+$('#cmbItemAdditionalComb').html()+"</select>";
        sales_item_additional_counter++;
    }

    var newRow = "<tr data-row='sales-quotation-item-rows-"+sales_item_idx+"' id='sales-quotation-item-rows-"+sales_item_idx+"' class='dnd-moved kt-datatable__row' style='left: 0px;'> " +
                    "<td data-field='id' class='kt-datatable__cell' id='col_id"+sales_item_idx+"' style='display: none;'>" +
                    "<input type='text' class='form-control' name='input_row' id='txtsalesitemid"+sales_item_idx+"' value='-1' />"+
                    "</td> " +

                    "<td data-field='lineno' class='kt-datatable__cell' id='col_lineno"+sales_item_idx+"' style='display: none;'>" +
                    "<input type='text' class='form-control' name='input_row' id='txtsalesitemlineno"+sales_item_idx+"' />"+
                    "</td> " +
                    
                    "<td data-field='text' class='kt-datatable__cell' id='col_number"+sales_item_idx+"' style='width: 50px;vertical-align:top;'>"+                   
                    "<span>"+
                    "<input type='text' class='form-control' style='width:40px;text-align:right' name='input_row' id='txtsalesitemno"+sales_item_idx+"' placeholder='No.' value='"+sno+"' />"+
                    "</span></td> " +

                    "<td data-field='description' class='kt-datatable__cell' id='col_description"+sales_item_idx+"' style='width: 600px;'>"+
                    "<span><div style='padding-right: 25px;'>"+
                    itemDescField +
                    "</div><div style='margin-top:10px;padding-left: 25px;width:600px;'>" +
                    // "<input type='text' class='form-control' name='input_row' id='txtsalesitemcomment"+sales_item_idx+"' placeholder='Item Comment..'/>" +
                    "<textarea class='form-control' name='input_row' id='txtsalesitemcomment"+sales_item_idx+"' placeholder='Item Comment..' rows='5'></textarea>"+
                    "</div><span></td>"+
                    
                    "<td data-field='unitprice' class='kt-datatable__cell' id='col_unitprice"+sales_item_idx+"' style='width: 50px;vertical-align:top;'>"+                   
                    "<input type='text' class='form-control' name='input_row' id='txtsalesitemunitprice"+sales_item_idx+"' placeholder='0.00' value='0.00'  style='text-align:right' onchange='javascript:calculateAmount("+sales_item_idx+");' />" +
                    "</td> " +
                    
                    "<td data-field='qty' class='kt-datatable__cell' id='col_qty"+sales_item_idx+"' style='width: 25px;vertical-align:top;'>"+                   
                    "<input type='number' class='form-control' name='input_row' id='txtsalesitemqty"+sales_item_idx+"' placeholder='0' value='0' style='text-align:right' onchange='javascript:calculateAmount("+sales_item_idx+");' />" +
                    "</td> " +

                    "<td data-field='discount' class='kt-datatable__cell' id='col_amount"+sales_item_idx+"' style='width: 100px;vertical-align:top;'>"+
                    "<span>"+
                    // "<div style='visibility:hidden;'><input type='text' class='form-control' name='input_row' /></div>"+
                    "<input type='text' class='form-control' name='input_row' id='txtsalesitemdiscamount"+sales_item_idx+"' value='0.00' placeholder='0.00' style='text-align:right' onchange='javascript:calculateAmount("+sales_item_idx+");' />" +
                    "<div style='margin-top:10px;'>"+
                    "<input type='text' class='form-control' name='input_row' id='txtsalesitemdiscreason"+sales_item_idx+"' placeholder='Discount ..'/>" +
                    "</div><span></td>"+

                    "<td data-field='amount' class='kt-datatable__cell' id='col_amount"+sales_item_idx+"' style='width: 100px;vertical-align:top;'>"+
                    "<span><div >"+
                    "<input type='text' class='form-control' name='input_row' id='txtsalesitemamount"+sales_item_idx+"' value='0.00' placeholder='0.00' style='text-align:right' onchange='javascript:calculateSubTotal("+sales_item_idx+");' />"+
                    "</div><div style='margin-top:10px;'>"+
                    "</div><span></td>"+

                    "<td data-field='delete' class='kt-datatable__cell' style='width: 50px;vertical-align:top' id='col_filter"+sales_item_idx+"'>" +
                    "<button type='button' class='btn btn-icon btn-danger btn-sm btn-icon-md' id='btnRemoveSalesItem' onclick='removeSalesItem("+sales_item_idx+");'><i class='fa fa-trash'></i></button></td> " +

                    "<td data-field='new' class='kt-datatable__cell' style='width: 50px;' id='col_new"+sales_item_idx+"'></td>"+

                "</tr>";
    
    if(item_idx == undefined || item_idx == null) {
        $('#sales-quotation-item-list').append(newRow);
    } else {
        if($('#sales-quotation-item-rows-'+item_idx).length > 0) {
            $('#sales-quotation-item-rows-'+item_idx).after(newRow);
        } else {
            $('#sales-quotation-item-list').append(newRow);
        }
    }
    sales_item_idx++;

    if(sales_item_additional_counter==2) {
        $('#btnNewItem').prop("disabled", true);
    }
}

function removeSalesItem(idx) {
    $("#sales-quotation-item-rows-"+idx).remove();
    sales_item_idx--;
    sales_item_counter--;
    updateSalesItemCounter();
    calculateAmount();
    if($('#txtSalesQuotationType').val()==1) {
        sales_item_additional_counter--;
        $('#btnNewItem').prop("disabled", false);
    }
}

function updateSalesItemCounter(){
    var count = 0;
    $('#sales-quotation-item-list tr').each(function() {
        var idx = $(this).attr('id');
        if(idx.includes("sales-quotation-item-section-rows")) {
            count = 0;
        } else {
            idx = idx.substring(idx.lastIndexOf('-')+1);
            count++;
            $('#txtsalesitemno'+idx).val(count);
        }
    });
}

function addSalesQuotationItemSection() {
    sales_item_counter = 0;
    var newRow = "<tr data-row='sales-quotation-item-section-rows-"+sales_item_idx+"' id='sales-quotation-item-section-rows-"+sales_item_idx+"' class='dnd-moved kt-datatable__row' style='left: 0px;background-color: #eaeff3'>"+
                    "<td data-field='id' class='kt-datatable__cell' id='col_id"+sales_item_idx+"' style='display: none;'>"+
                    "<input type='text' class='form-control' name='input_row' id='txtsalesitemid"+sales_item_idx+"' value='' />"+
                    "</td>"+

                    "<td data-field='lineno' class='kt-datatable__cell' id='col_lineno"+sales_item_idx+"' style='display: none;'>"+
                    "<input type='text' class='form-control' name='input_row' id='txtsalesitemlineno"+sales_item_idx+"' value='' />"+
                    "</td>"+
                                    
                    "<td data-field='text' class='kt-datatable__cell' id='col_number"+sales_item_idx+"' style='width: 60px;vertical-align:top;'>  "+                 
                    "<span>"+
                    "<input type='text' class='form-control' name='input_row' id='txtsalesitemsectionphase"+sales_item_idx+"' placeholder='Phase' />"+
                    "</span></td>"+

                    "<td data-field='description' class='kt-datatable__cell' id='col_description"+sales_item_idx+"'>"+
                    "<div>"+
                    "<input type='text' class='form-control' name='input_row' id='txtsalesitemsectiondescription"+sales_item_idx+"' placeholder='Description' />"+
                    "</div></td>"+
                                    
                    "<td data-field='unitprice' class='kt-datatable__cell' style='width: 50px;vertical-align:top;'>"+
                    "<span><span></td>"+
                                    
                    "<td data-field='qty' class='kt-datatable__cell' style='width: 50px;vertical-align:top;'>  "+               
                    "<span><span></td>"+

                    "<td data-field='discount' class='kt-datatable__cell' style='width: 100px;vertical-align:top;'>"+
                    "<span><span></td>"+

                    "<td data-field='amount' class='kt-datatable__cell' style='width: 100px;vertical-align:top;'>"+
                    "<span><span></td>"+

                    "<td data-field='delete' class='kt-datatable__cell' style='width: 50px;vertical-align:top' id='col_filter"+sales_item_idx+"'>"+
                    "<button type='button' class='btn btn-icon btn-danger btn-sm btn-icon-md' id='btnRemoveSalesItemSection"+sales_item_idx+"' onclick='removeSalesItemSection("+sales_item_idx+");'><i class='fa fa-trash'></i></button></td>"+

                    // "<td data-field='new' class='kt-datatable__cell' style='width: 50px;' id='col_new"+sales_item_idx+"'></td>"+
                    "<td data-field='new' class='kt-datatable__cell' style='width: 50px;vertical-align:top' id='col_new"+sales_item_idx+"'>" +
                    "<button type='button' class='btn btn-icon btn-success btn-sm btn-icon-md' id='btnAddSalesItem"+sales_item_idx+"' onclick='addSalesQuotationItemData("+sales_item_idx+");'><i class='fa fa-plus'></i></button></td>"+

                "</tr>";
                
    $('#sales-quotation-item-list').append(newRow);
    sales_item_idx++;
    sales_item_section_idx++;
}

function removeSalesItemSection(idx) {
    $("#sales-quotation-item-section-rows-"+idx).remove();
    sales_item_idx--;
}

// function getItemSection(){
//     var arrays = [];
//     var data = {};
//     $('#sales-quotation-item-list tr').each(function() {
//         var idx = $(this).attr('id');
//         if(idx.includes("sales-quotation-item-section-rows")) {
//             idx = idx.substring(idx.lastIndexOf('-')+1);
//             if($('#txtsalesitemsectiondescription'+idx).val() != '') {
//                 data = { 
//                         "item_id"    	        : $('#txtsalesitemid'+idx).val(),
//                         "item_section_phase"     : $('#txtsalesitemsectionphase'+idx).val(),
//                         "item_section_desc"     : $('#txtsalesitemsectiondescription'+idx).val(),
//                     };
                
//                 arrays.push(data);
//             } else {
//                 Swal.fire("Warning", "Phase Description cannot be blank!", "warning");
//                 $('#txtsalesitemsectiondescription'+idx).focus();
//                 return;
//             }
//         }
//     });
    
//     return JSON.stringify(arrays) ;
// }

function getItems(){
    var arrays = [];
    var data = {};
    $('#sales-quotation-item-list tr').each(function() {
        var idx = $(this).attr('id');
        if(idx.includes("sales-quotation-item-section-rows")) {
            idx = idx.substring(idx.lastIndexOf('-')+1);
            if($('#txtsalesitemsectiondescription'+idx).val() != '') {
                data = { 
                        "item_id"    	    : $('#txtsalesitemid'+idx).val(),
                        "item_desc"         : $('#txtsalesitemsectiondescription'+idx).val(),
                        "item_comment"      : "",
                        "item_amount"		: "0.00",  
                        "item_disc"         : "0.00",
                        "item_disc_reason"  : "",
                        "item_unit_price"   : "0.00",
                        "item_qty"          : 0,
                        "item_phase"        : $('#txtsalesitemsectionphase'+idx).val(),
                        "item_no"           : 0,
                        "item_additional"   : 0,
                    };
                
                arrays.push(data);
            } else {
                Swal.fire("Warning", "Item Phase Description cannot be blank!", "warning");
                return;
            }
        } else {
            idx = idx.substring(idx.lastIndexOf('-')+1);
            if($('#txtsalesitemdescription'+idx).val() != '') {
                data = { 
                        "item_id"    	    : $('#txtsalesitemid'+idx).val(),
                        "item_desc"         : $('#txtsalesitemdescription'+idx).prop('tagName') == "INPUT" ? $('#txtsalesitemdescription'+idx).val() : $('#txtsalesitemdescription'+idx+' option:selected').text(),
                        "item_comment"      : $('#txtsalesitemcomment'+idx).val(),
                        "item_amount"		: $('#txtsalesitemamount'+idx).val(),  
                        "item_disc"         : $('#txtsalesitemdiscamount'+idx).val(),
                        "item_disc_reason"  : $('#txtsalesitemdiscreason'+idx).val(),
                        "item_unit_price"   : $('#txtsalesitemunitprice'+idx).val(),
                        "item_qty"          : $('#txtsalesitemqty'+idx).val(),
                        "item_phase"        : "",
                        "item_no"           : $('#txtsalesitemno'+idx).val(),
                        "item_additional"   : $('#txtsalesitemdescription'+idx).val(),
                    };
                
                arrays.push(data);
            } else {
                Swal.fire("Warning", "Item Description cannot be blank!", "warning");
                return;
            }
        }
    });
    
    return JSON.stringify(arrays) ;
}

function clearItems() {
    sales_item_idx = 1;
    sales_item_counter = 0;
    sales_item_additional_counter = 0;
    sales_item_section_idx = 0;
    $('#sales-quotation-item-list').html('');
    $('#btnNewItem').show();
    $('#btnNewItemAdditional').hide();
    
    $('#txtsalesitemsubtotal').val('0');
    $('#txtsalesitemsubtotal').prop("disabled", true);
    $('#cobsalesitemdisctype').val('');
    $('#txtsalesitemdiscpercentage').val('0');
    $('#txtsalesitemdiscpercentage').prop("disabled", true);
    $('#txtsalesitemdisctotal').val('0');
    $('#txtsalesitemdisctotal').prop("disabled", true);
    $('#txtsalesitemgstpercentage').val('0');
    $('#txtsalesitemgsttotal').val('0');
    $('#txtsalesitemgsttotal').prop("disabled", true);
    $('#txtsalesitemgrandtotal').val('0');
    $('#txtsalesitemgrandtotal').prop("disabled", true);
    $('#txtsalesitemqtytotal').val('0');

    changeItemDiscountType($('#cobsalesitemdisctype'));
}

function calculateAmount(idx) {
    var item_amount     = 0

    var unit_price  = $('#txtsalesitemunitprice'+idx).val();
    var unit_qty    = $('#txtsalesitemqty'+idx).val();
    var unit_disc   = $('#txtsalesitemdiscamount'+idx).val();
    item_amount     = (parseFloat(unit_price) * parseInt(unit_qty)) - parseFloat(unit_disc);

    $('#txtsalesitemunitprice'+idx).val(parseFloat(unit_price).toFixed(2));
    $('#txtsalesitemdiscamount'+idx).val(parseFloat(unit_disc).toFixed(2));
    $('#txtsalesitemamount'+idx).val(parseFloat(item_amount).toFixed(2));  

    calculateSubTotal();
}

function calculateSubTotal() {
    var item_amount     = 0;
    var item_disc       = 0;
    var sub_total       = 0;
    var item_qty        = 0;
    var qty_total       = 0;

    $('#sales-quotation-item-list tr').each(function() {
        var idx = $(this).attr('id');
        if(idx.includes("sales-quotation-item-section-rows")) {
        } else {
            idx = idx.substring(idx.lastIndexOf('-')+1);
            item_amount     = $('#txtsalesitemamount'+idx).val();
            item_disc       = $('#txtsalesitemdiscamount'+idx).val();
            item_qty        = $('#txtsalesitemqty'+idx).val();
            
            qty_total       += parseInt(item_qty);
            sub_total       += parseFloat(item_amount);

            // $('#txtsalesitemamount'+idx).val(parseFloat(item_amount).toFixed(2));
            // $('#txtsalesitemdiscamount'+idx).val(parseFloat(item_disc).toFixed(2));
        }
    });
    
    $('#txtsalesitemqtytotal').val(parseInt(qty_total));
    $('#txtsalesitemsubtotal').val(parseFloat(sub_total).toFixed(2));
    calculateTotal();
}

function calculateTotal() {
    var sub_total       = $('#txtsalesitemsubtotal').val();
    var disc_type       = $('#cobsalesitemdisctype').val();
    var disc_percent    = $('#txtsalesitemdiscpercentage').val();
    var disc_total      = $('#txtsalesitemdisctotal').val();
    var gst_percent     = $('#txtsalesitemgstpercentage').val();
    var gst_total       = $('#txtsalesitemgsttotal').val();
    var grand_total     = $('#txtsalesitemgrandtotal').val();

    if(disc_type == 'P' && disc_percent != 0) {
        disc_total = (parseFloat(disc_percent) / 100) * parseFloat(sub_total);
        $('#txtsalesitemdisctotal').val(parseFloat(disc_total).toFixed(2));
    } else {
        $('#txtsalesitemdisctotal').val(parseFloat(disc_total).toFixed(2));
    }

    // if(gst_percent != 0) {
        gst_total = (parseFloat(gst_percent) / 100) * (parseFloat(sub_total) - parseFloat(disc_total));
        $('#txtsalesitemgsttotal').val(parseFloat(gst_total).toFixed(2));
    // }

    $('#txtsalesitemsubtotal').val(parseFloat(sub_total).toFixed(2));
    grand_total = (parseFloat(sub_total) - parseFloat(disc_total)) + parseFloat(gst_total);
    $('#txtsalesitemgrandtotal').val(parseFloat(grand_total).toFixed(2));
}

$("txtsalesitemsubtotal").focusout(function(){
    var amount = $(obj).val().toFixed(2);
    $(obj).val(amount);
});

function numberWithCommas(x) {
    return x.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ",");
}
function changeItemDiscountType(obj) {
    $('#txtsalesitemdiscpercentage').prop("disabled", true);
    $('#txtsalesitemdisctotal').prop("disabled", true);
    if($(obj).val()=='P'){
        $('#txtsalesitemdiscpercentage').prop("disabled", false);
        $('#txtsalesitemdisctotal').prop("disabled", true);
    } else if($(obj).val()=='A'){
        $('#txtsalesitemdiscpercentage').prop("disabled", true);
        $('#txtsalesitemdisctotal').prop("disabled", false);
    }
    $('#txtsalesitemdiscpercentage').val('0');
    $('#txtsalesitemdisctotal').val('0');
    calculateTotal();
}