function calc_columns() {
    var total_ist = 0;
    var total_sst = 0;
    var total_st = 0;
    var total_ds = 0;
    var total_gt = 0;
    var total_ph = 0;
    var total_diff = 0;
    $('.total_ist').each(function () {
        if (!isNaN($(this).text())) {
            if ($(this).text() != "") {
                total_ist += parseFloat($(this).text());
            }
        }
    });
    $('.total_sst').each(function () {
        if (!isNaN($(this).text())) {
            if ($(this).text() != "") {
                total_sst += parseFloat($(this).text());
            }
        }
    });
    $('.total_st').each(function () {
        if (!isNaN($(this).text())) {
            if ($(this).text() != "") {
                total_st += parseFloat($(this).text());
            }
        }
    });
    $('.total_ds').each(function () {
        if (!isNaN($(this).text())) {
            if ($(this).text() != "") {
                total_ds += parseFloat($(this).text());
            }
        }
    });
    $('.total_gt').each(function () {
        if (!isNaN($(this).text())) {
            if ($(this).text() != "") {
                total_gt += parseFloat($(this).text());
            }
        }
    });
    $('.total_ph').each(function () {
        if (!isNaN($(this).text())) {
            if ($(this).text() != "") {
                total_ph += parseFloat($(this).text());
            }
        }
    });
    $('.total_diff').each(function () {
        if (!isNaN($(this).text())) {
            if ($(this).text() != "") {
                total_diff += parseFloat($(this).text());
            }
        }
    });
    $('#total_ist').text(total_ist.toFixed(3));
    $('#total_sst').text(total_sst.toFixed(3));
    $('#total_st').text(total_st.toFixed(3));
    $('#total_ds').text(total_ds.toFixed(3));
    $('#total_gt').text(total_gt.toFixed(3));
    $('#total_ph').text(total_ph.toFixed(3));
    $('#total_diff').text(total_diff.toFixed(3));
}
function calc_columns2() {
    var total_ist = 0;
    var total_sst = 0;
    var total_st = 0;
    var total_ds = 0;
    var total_gt = 0;
    $('.total_ist').each(function () {
        if (!isNaN($(this).text())) {
            if ($(this).text() != "") {
                total_ist += parseFloat($(this).text());
            }
        }
    });
    $('.total_sst').each(function () {
        if (!isNaN($(this).text())) {
            if ($(this).text() != "") {
                total_sst += parseFloat($(this).text());
            }
        }
    });
    $('.total_st').each(function () {
        if (!isNaN($(this).text())) {
            if ($(this).text() != "") {
                total_st += parseFloat($(this).text());
            }
        }
    });
    $('.total_ds').each(function () {
        if (!isNaN($(this).text())) {
            if ($(this).text() != "") {
                total_ds += parseFloat($(this).text());
            }
        }
    });
    $('.total_gt').each(function () {
        if (!isNaN($(this).text())) {
            if ($(this).text() != "") {
                total_gt += parseFloat($(this).text());
            }
        }
    });
    $('#total_ist').text(total_ist.toFixed(3));
    $('#total_sst').text(total_sst.toFixed(3));
    $('#total_st').text(total_st.toFixed(3));
    $('#total_ds').text(total_ds.toFixed(3));
    $('#total_gt').text(total_gt.toFixed(3));
}
$(document).ready(function () {
    // Datepicker 
    $('.datepicker').datepicker({ dateFormat: 'yy-mm-dd' });
    $('.MyTable').DataTable({
        layout: {
            topStart: 'buttons',
        },
        pageLength: 100,
        order: [],
        responsive: true,
   
    });
    

    $('#salesTable').DataTable({
        layout: {
            topStart: 'buttons',
        },
        pageLength: 500,
        "fnInitComplete": function(oSettings, json) {
                calc_columns();
            },
        "fnDrawCallback": function() {
                calc_columns();
            },
    });
    
    $('#sinvoice-details').on('show.bs.modal', function (e) {
        var invoice = e.relatedTarget.id;
        $('#invoice_number').text(invoice);
        var form_data = new FormData();
        form_data.append('invoice', invoice);
        $.ajax({
            url: base_url + 'report/sales-details',
            type: 'POST',
            processData: false,
            contentType: false,
            data: form_data,
            success: function (response) {
                var res = $.parseJSON(response)
                $('#sinvoice-mbody').html('');
                $('#sinvoice-mbody').html(res[0]);
            }
        });
    });

    $('#pinvoice-details').on('show.bs.modal', function (e) {
        var invoice = e.relatedTarget.id;
        $('#invoice_number').text(invoice);
        var form_data = new FormData();
        form_data.append('invoice', invoice);
        $.ajax({
            url: base_url + 'report/purchase-details',
            type: 'POST',
            processData: false,
            contentType: false,
            data: form_data,
            success: function (response) {
                var res = $.parseJSON(response)
                $('#sinvoice-mbody').html('');
                $('#sinvoice-mbody').html(res[0]);
            }
        });
    });

    $('#qinvoice-details').on('show.bs.modal', function (e) {
        var invoice = e.relatedTarget.id;
        $('#invoice_number').text(invoice);
        var form_data = new FormData();
        form_data.append('invoice', invoice);
        $.ajax({
            url: base_url + 'report/quotation-details',
            type: 'POST',
            processData: false,
            contentType: false,
            data: form_data,
            success: function (response) {
                var res = $.parseJSON(response)
                $('#qinvoice-mbody').html('');
                $('#qinvoice-mbody').html(res[0]);
            }
        });
    });

    $('#srvinvoice-details').on('show.bs.modal', function (e) {
        alert();
        var invoice = e.relatedTarget.id;
        $('#srvinvoice_number').text(invoice);
        var token_name = "<?php echo $this->security->get_csrf_token_name();?>";
        var token_value = $('#my_csrf').val();
        var form_data = new FormData();
        form_data.append('invoice', invoice);
        form_data.append(token_name, token_value);
        $.ajax({
            url: base_url + 'reports/services_invoice_details',
            type: 'POST',
            processData: false,
            contentType: false,
            data: form_data,
            success: function (response) {
                var res = $.parseJSON(response)
                $('#my_csrf').val(res[0]);
                $('#srvinvoice-mbody').html('');
                $('#srvinvoice-mbody').html(res[1] + '<hr>' + res[2] + '<hr>' + res[3]);
            }
        });
    });
    $('#stock-history-modal').on('show.bs.modal', function (e) {
        var stk_barcode = e.relatedTarget.id;
        var sdate = $('#sdate').val();
        var edate = $('#edate').val();
        var token_name = "<?php echo $this->security->get_csrf_token_name();?>";
        var token_value = $('#my_csrf').val();
        var form_data = new FormData();
        form_data.append('sdate', sdate);
        form_data.append('edate', edate);
        form_data.append('stk_barcode', stk_barcode);
        form_data.append(token_name, token_value);
        $.ajax({
            url: base_url + 'reports/stock_movement',
            type: 'POST',
            processData: false,
            contentType: false,
            data: form_data,
            success: function (response) {
                var res = $.parseJSON(response)
                $('#my_csrf').val(res[0]);
                $('#item-mbody').html('');
                $('#item-mbody').html(res[1]);
                $('#stkm-table').DataTable({ "order": [[1, "desc"]] });
                recalcId();
            }
        });
    });

    $('#spr-sales-modal').on('show.bs.modal', function (e) {
        var sdate = $('#sdate').val();
        var edate = $('#edate').val();
        var token_name = "<?php echo $this->security->get_csrf_token_name();?>";
        var token_value = $('#my_csrf').val();
        var form_data = new FormData();
        form_data.append('sdate', sdate);
        form_data.append('edate', edate);
        form_data.append(token_name, token_value);
        $.ajax({
            url: base_url + 'reports/spr_sales',
            type: 'POST',
            processData: false,
            contentType: false,
            data: form_data,
            success: function (response) {
                var res = $.parseJSON(response)
                $('#my_csrf').val(res[0]);
                $('#spr-sales-mbody').html('');
                $('#spr-sales-mbody').html(res[1]);
            }
        });
    });

    $('#spr-expenses-modal').on('show.bs.modal', function (e) {
        var sdate = $('#sdate').val();
        var edate = $('#edate').val();
        var token_name = "<?php echo $this->security->get_csrf_token_name();?>";
        var token_value = $('#my_csrf').val();
        var form_data = new FormData();
        form_data.append('sdate', sdate);
        form_data.append('edate', edate);
        form_data.append(token_name, token_value);
        $.ajax({
            url: base_url + 'reports/spr_expenses',
            type: 'POST',
            processData: false,
            contentType: false,
            data: form_data,
            success: function (response) {
                var res = $.parseJSON(response)
                $('#my_csrf').val(res[0]);
                $('#spr-expenses-mbody').html('');
                $('#spr-expenses-mbody').html(res[1]);
            }
        });
    });

    $('#spr-purchases-modal').on('show.bs.modal', function (e) {
        var sdate = $('#sdate').val();
        var edate = $('#edate').val();
        var token_name = "<?php echo $this->security->get_csrf_token_name();?>";
        var token_value = $('#my_csrf').val();
        var form_data = new FormData();
        form_data.append('sdate', sdate);
        form_data.append('edate', edate);
        form_data.append(token_name, token_value);
        $.ajax({
            url: base_url + 'reports/spr_purchases',
            type: 'POST',
            processData: false,
            contentType: false,
            data: form_data,
            success: function (response) {
                var res = $.parseJSON(response)
                $('#my_csrf').val(res[0]);
                $('#spr-purchases-mbody').html('');
                $('#spr-purchases-mbody').html(res[1]);
            }
        });
    });

    $('#spr-returns-modal').on('show.bs.modal', function (e) {
        var sdate = $('#sdate').val();
        var edate = $('#edate').val();
        var token_name = "<?php echo $this->security->get_csrf_token_name();?>";
        var token_value = $('#my_csrf').val();
        var form_data = new FormData();
        form_data.append('sdate', sdate);
        form_data.append('edate', edate);
        form_data.append(token_name, token_value);
        $.ajax({
            url: base_url + 'reports/spr_returns',
            type: 'POST',
            processData: false,
            contentType: false,
            data: form_data,
            success: function (response) {
                var res = $.parseJSON(response)
                $('#my_csrf').val(res[0]);
                $('#spr-returns-mbody').html('');
                $('#spr-returns-mbody').html(res[1]);
            }
        });
    });

    $('#trf-details').on('show.bs.modal', function (e) {
        var invoice = e.relatedTarget.id;
        $('#invoice_number').text(invoice);
        var form_data = new FormData();
        form_data.append('invoice', invoice);
        $.ajax({
            url: base_url + 'report/transfer-details',
            type: 'POST',
            processData: false,
            contentType: false,
            data: form_data,
            success: function (response) {
                var res = $.parseJSON(response)
                $('#trf-mbody').html('');
                $('#trf-mbody').html(res[0]);
            }
        });
    });

    var sdate = $('#sdate').val();
    var edate = $('#sdate').val();
    $('#btn-submit').on('click', function (e) {
        $('#dt').DataTable().ajax.reload();
    });
    $('#dt').DataTable({
        "pageLength": 100,
        "serverSide": true,
        "order": [
            [0, "asc"]
        ],
        "ajax": {
            url: base_url + 'reports/showData',
            type: 'POST',
            data: { sdate: sdate, edate: edate }
        },
    });
});// End Ready Function