$( document ).ready(function() {
    report();
 
});
function report(){
    var company_id=$("#company_id").val();
    var datefrom=$("#datefrom").val();
    var dateto=$("#dateto").val();
    var manageTable = $('#transaction-table').DataTable();
    manageTable.destroy();
    manageTable= $("#transaction-table").DataTable({
      "ajax": {
                "url": 'fetchTransactionData',
                'type': "GET",
                'data':{company_id:company_id,datefrom:datefrom,dateto:dateto},
               },
      lengthMenu: [
                [10, 25, 50, 100, -1],
                ['10', '25', '50', '100', 'All']],
                "responsive": false, "lengthChange": false, "autoWidth": false,
     "order": [[0, 'asc']],
       dom: "<'row'<'col-sm-4'l><'col-sm-4 text-center'B><'col-sm-4'f>>" +
                    "<'row'<'col-sm-12'tr>>" +
                    "<'row'<'col-sm-6'i><'col-sm-6'p>>",
            buttons: [{
                    extend: 'copy',
                    exportOptions: {
                        columns: ':visible:not(:eq(11))'
                    }
                },
                {
                    extend: 'csv',
                    exportOptions: {
                        columns: ':visible:not(:eq(11))'
                    }
                },
                {
                    extend: 'print',
                    exportOptions: {
                        columns: ':visible:not(:eq(11))',
                    }

                },
                {
                    extend: 'excel',
                    exportOptions: {
                        columns: ':visible:not(:eq(11))'
                    }
                },
                {
                    extend: 'pdf',
                    exportOptions: {
                        columns: ':visible:not(:eq(11))'
                    }
                }],

                 "footerCallback": function (row, data, start, end, display) {
                var api = this.api(), data;

                // converting to interger to find total

                var intVal = function (i) {


                    if (typeof i === 'string') {

                        i = i.replace(/[\a-zA-Z₹]/g, '') * 1;

                    }
                    // check if you got a valid number.
                    if (Number.isNaN(i)) {
                        return 0;
                    }
                    return i;
                };

                var total=api
                        .column(3, {page: 'current'})
                        .data()
                        .reduce(function (a, b) {

                            return intVal(a) + intVal(b);
                        }, 0);
            
                  $(api.column(0).footer()).html('TOTAL');
                  $(api.column(3).footer()).html(' ' + total.toFixed(2));
                  

            }
      });
}