<style>
    /* enable absolute positioning */
    .inner-addon { 
        position: relative; 
    }

    /* style icon */
    .inner-addon .glyphicon {
        position: absolute;
        padding: 10px;
        pointer-events: none;
    }

    /* align icon */
    .left-addon .glyphicon  { left:  0px;}
    .right-addon .glyphicon { right: 0px;}

    /* add padding  */
    .left-addon input  { padding-left:  30px; }
    .right-addon input { padding-right: 30px; }

    .loader {
        position: fixed;
        left: 0px;
        top: 0px;
        width: 100%;
        height: 100%;
        z-index: 9999;
        background: url('<?php echo base_url(); ?>assets/images/page-loader-1.gif') 50% 50% no-repeat ;
        display: none;
    }


    table.dataTable tr.group td{font-weight:bold;background-color:#e0e0e0}

    table.dataTable tr.group,
    tr.group:hover {
        background-color: #ddd !important;
    }

    table td{
     /*border: none !important;*/   
    }
</style>
<?php
// echo "<pre>";
// print_r($balanceSheet);
//exit();
?>
<div class="wrapper">
    <div class="container">
        <!-- Page-Title -->
        <div class="row">
            <div class="col-sm-12">
                <div class="btn-group pull-right m-t-15">
                    <form method="get" action="<?php echo base_url(); ?>accounting_controller/accountvoucheradd">
                        <input type="hidden" name="voucher_add_csrf" id="voucher_add_csrf" value=""/>
<!--                        <button type="submit" onclick="$('#voucher_add_csrf').val($('#cook').val());" class="btn btn-custom waves-effect waves-light" >Add Account Voucher <span class="m-l-5"><i class="fa fa-plus"></i></span></button>-->
                    </form>
                </div>
                <h4 class="page-title">Profit and Loss (<?php echo $business[0]['business_name']; ?>):</h4>
            </div>
        </div>

        <div class="row" id="divselection">
            <div class="col-sm-12">
                <div class="card-box ">
                    <h4 class="header-title m-t-0 m-b-10 hidden-print">Filters:</h4>
                    <div class="row m-b-10 hidden-print" id="div-filters">
                        <div class="col-lg-12">
                            <div class="row">
                                <div class="col-lg-3">
                                    <label class="control-label">Start Date Range</label>
                                </div>

                                <div class="col-lg-3">
                                    <label class="control-label">End Date Range</label>
                                </div>
                                <?php if($this->session->userdata('ho')=='Yes'){ ?>
                                <div class="col-lg-4">
                                    <label class="control-label">Branch</label>
                                </div>
                                <?php } ?>
                                <div class="col-lg-2"></div>    
                            </div>
                        </div>
                        <div class="row">
                            <form method="post" action="<?php echo base_url(); ?>profit_loss">

                                <input type="hidden" name="csrf_test_name" id="accounting_voucher_csrf" value=""/>
                                <div class="col-lg-3">
                                    <input onchange="checkdaterange();"  style="background-color: white;" required readonly class="form-control" type="text" name="startdate" id="startdate" value="<?php if (isset($startdate)) { echo $startdate;} ?>"/>                                
                                </div>
                                <div class="col-lg-3">
                                    <input onchange="checkdaterange();" style="background-color: white;" required readonly class="form-control" type="text" name="enddate" id="enddate" value="<?php if (isset($enddate)) { echo $enddate;} ?>"/>
                                </div>

                                <div class="col-lg-2 hidden">
                                    <select class="form-control" id="fromAccount" name="from_accounts">
                                        <option value="">From Account</option>
                                        <option value="0">All</option>
                                        <?php foreach ($account_heads as $ba) { ?>
                                            <option <?php if (isset($from_account)) {if ($from_account == $ba['id_account_heads']) {echo " selected='selected' ";}} ?> value="<?php echo $ba['id_account_heads']; ?>"><?php echo $ba['account_head']; ?></option>
                                         <?php } ?>
                                    </select>
                                </div>

                                <div class="col-lg-2 hidden">
                                    <select class="form-control" id="toAccount" name="to_accounts">
                                        <option value="">To Account</option>
                                        <option value="0">All</option>
                                        <?php foreach ($account_heads as $ba) { ?>
                                            <option <?php if (isset($to_account)) {
                                                if ($to_account == $ba['id_account_heads']) {
                                                    echo " selected='selected' ";
                                                }
                                            } ?> value="<?php echo $ba['id_account_heads']; ?>"><?php echo $ba['account_head']; ?></option>
                                        <?php } ?>
                                    </select>
                                </div>

                                <div class="col-lg-4 hidden">
                                    <select class="form-control" id="voucher_type" name="voucher_type">
                                        <option value="">All</option>
                                        <?php foreach ($voucher_types as $vt) { ?>
                                            <option <?php if (isset($voucher_type)) {
                                                if ($voucher_type == $vt['id_account_voucher_type']) {
                                                    echo " selected='selected' ";
                                                }
                                            } ?> value="<?php echo $vt['id_account_voucher_type']; ?>"><?php echo $vt['account_voucher_type']; ?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                                 <?php if($this->session->userdata('ho')=='Yes'){ ?>
                                <div class="col-lg-3">
                                    <select style="background-color: white;" class="form-control" type="text" name="business_id" id="business_id">
                                        <option value="">All</option>
                                        <?php foreach($businesses as $b){?>
                                        <option <?php if($businessselected==$b['id_business']){echo "selected='selected'";}?> value="<?php echo $b['id_business'];?>"><?php echo $b['business_name'];?></option>
                                        <?php } ?>
                                    </select>                                
                                </div>
                                <?php } ?>
                                <div class="col-lg-2"><button type="submit" onclick="$('#accounting_voucher_csrf').val($('#cook').val());" class="btn btn-pink btn-bordred waves-effect w-md waves-light m-b-5">Run</button></div>
                            </form>
                        </div>
                    </div>
                    
                   <!--From Revenue Breakup Income-->
<!--                     <div class="row">
                        <div class="col-md-12">
                            <table class="table  table-condensed">
                                <thead>
                                    <tr>
                                        <th class="text-center">Profit & Loss Statement</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    
                                    <?php $total_service = 0; $total_sale=0; $total_revenue=0;?> 
                                    <tr><td>
                                    <table class="table  table-condensed">
                                        <tbody>
                                         <tr><td colspan="3"><strong><i>Sale Income</i></strong></td></tr>
                                            <?php $total = 0;foreach ($revenue_service as $rs): ?>    
                                                <tr>
                                                    <td style="width:60%; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $rs['Type']; ?></td>
                                                    <td style="width:40%; text-align: right;"><?php echo abs($rs['amount']) ?></td>
                                                    <?php $total += $rs['amount']; ?>
                                                    <?php $total_service += $rs['amount']; ?>
                                                </tr>
                                            <?php endforeach; ?> 
                                           <?php foreach ($revenue_sale as $rs): ?>    
                                                <tr>
                                                    <td style="width:60%; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $rs['Type']; ?></td>
                                                    <td style="width:40%; text-align: right;"><?php echo abs($rs['amount']) ?></td>
                                                    <?php $total += $rs['amount']; ?>
                                                    <?php $total_sale += $rs['amount']; ?>
                                                </tr>
                                            <?php endforeach; ?> 
                                            <tfoot>
                                                <th><span class="pull-right">Total Sale:</span></th>
                                                <th style='text-align: right;'><?php echo number_format(abs($total), 2) ?></th>
                                            </tfoot> 
                                        </tbody>
                                    </table>    
                                    </td></tr>
                                    <?php  ?>  
                                </tbody>
                            </table>
                        </div>
                     </div>
                    
                    End From Revenue Breakup Income-->

                    
                    <?php if($this->session->userdata('auto_voucher')=='Yes'){?>
                    <!--From Vouchers Income-->
                     <div class="row">
                        <div class="col-md-12">
                        <?php if(DOWNLOAD == true || $this->session->userdata('role') == 'Admin' || $this->session->userdata('role')=='Super User'){ ?>
                            <a class="btn btn-warning m-l-10 hidden-print" onclick="tableToExcel('sheet1', 'Profit and Loss')" style="float:right">Export to Excel</a>
                            <btn class="btn btn-default m-l-10 hidden-print" onclick="javascript:window.print();" style="float:right"><i class="fa fa-print"></i></button>
                            <?php } ?>
                        </div>
                    </div>
                     <div class="row">
                        <div class="col-md-12">                            
                            <table class="table table-bordered table-condensed" id="sheet1">
                                <thead>
                                    <tr>
                                        <th colspan="3" class="text-center" style='font-size:16px; '>Profit & Loss Statement - <?php echo $business[0]['business_name'];?></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    
                                    <tr>
                                        <td colspan="2">
                            <table class="table table-condensed">
                                
                                <tbody>
                                    
                                    <?php $total_revenue = 0;
                                    foreach ($profit_loss['Revenue'] as $ma_index => $ma_value):?> 
                                    
                                    <tr><td colspan="2" style="width:80%">
                                        <table class="table  table-condensed" style='border: none !important;'>
                                            <tbody>
                                             <tr><td colspan="3"><strong ><i><?php echo $ma_index ?></i></strong></td></tr>
                                                <?php $total = 0;
                                                foreach ($ma_value as $index => $value): ?>    
                                                    <tr>
                                                        <td style="width:40%; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $index ?></td>
                                                        <td style="width:20%; text-align: right;"><?php echo $value[0]['amount']<0 ?'('.abs($value[0]['amount']).')' : abs($value[0]['amount']) ; ?></td>
                                                        <?php $total += $value[0]['amount']; ?>

                                                        <td style="width:20%; text-align: right;"></td>

                                                    </tr>
                                                <?php endforeach; ?> 
                                            </tbody>
                                            <tfoot>
                                                <th><span class="pull-right">Total <?php echo $ma_index ?>:</span></th>
                                                <?php $total_revenue += $total; ?>
                                                <th></th>
                                                <th style='text-align: right;'><?php if($total<0){echo "(";}  echo number_format(abs($total), 2); if($total<0){echo ")";}  ?></th>

                                            </tfoot> 

                                        </table>    
                                    </td>
                                    <td></td>
                                    
                                    </tr>
                                    <?php endforeach; ?>  
                                
                                </tbody>
                                <tfoot>
                                    <tr>
                                        <th style='width:40%;'><strong  style='font-size: 16px; '>Net Sale</strong></th>
                                        <th style='text-align: right;'></th>
                                       
                                       
                                        <th style='text-align: right;'><?php if($total_revenue<0){echo "(";} echo number_format(abs($total_revenue), 2); if($total_revenue<0){echo ")";}  ?></th>
                                        
                                    </tr>
                                </tfoot>
                            </table>
                        
                    
                    <!--End From Vouchers Income-->
                    
                    <!--Cost of Goods-->
                            <?php $total_expense_suppleirs = 0;
                            foreach ($profit_loss['Expenses'] as $ma_index => $ma_value): ?>
                                <?php if ($ma_index === 'Cost of Goods'): ?> 
                            <table class="table  table-condensed">
                                <tbody>
                                    <tr><td colspan="4"><strong><i><?php echo $ma_index; ?></i></strong></td></tr>
                                    <?php $total = 0;
                                    foreach ($ma_value as $index => $value): ?>    
                                        <tr>
                                            <td style="width:40%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $index   ?></td>
                                            <td style="width:20%; text-align: right;"><?php echo $value[0]['amount']<0 ?'('.abs($value[0]['amount']).')' : abs($value[0]['amount']);?></td>
                                    <?php $total += $value[0]['amount']; ?>
                                    <?php $total_expense_suppleirs += $value[0]['amount']; ?>
                                            <td  style="width:20%; text-align: right;"></td>
                                            <td></td>
                                        </tr>
                                    <?php endforeach; ?> 

                                

                                </tbody>
                                <tfoot>
                                
                                <th style=""><strong style="font-size:16px;">Total <?php echo $ma_index; ?>:</strong></th>
                                    <th></th>
                                    <th></th>
                                    <th  style="width:20%; text-align: right;"><?php if($total<0){echo "(";} echo  number_format(abs($total), 2); if($total<0){echo ")";}?></th>
                                    
                                </tfoot> 
                            </table>

                            <?php endif; ?>   
                            <?php endforeach; ?>
                        <!--Cost of Goods-->
                        <!---Gross Profit-->
                            <table class="table  table-condensed">
                                <tbody>
                                    <td style="width:80%;"><strong style='font-size:16px;'>Gross Profit</strong></td>  
                                    <?php  $gross_profit = $total_revenue - $total_expense_suppleirs; ?>
                                    <td style="width:20%; text-align: right;"><strong style='font-size:14px;'><i><?php if($gross_profit<0){echo "(";}  echo number_format(abs($gross_profit), 2); if($gross_profit<0){echo ")";} ?></i></strong></td>

                                </tbody>
                            </table>   
                        <!--End Gross profit-->

                    <!--Expenes-->
                    
                    <table class="table  table-condensed">
                        <tbody><tr><td colspan="2" style="width:80%;">
                        <?php $total_expense = 0;    
                            foreach ($profit_loss['Expenses'] as $ma_index => $ma_value): ?> 
                            <?php if ($ma_index !== 'Cost of Goods'): ?> 
                                    <table class="table  table-condensed">
                                        <tbody>
                                            <tr><td colspan="3"><strong><i><?php echo $ma_index ?></i></strong></td></tr>
                                            <?php $total = 0;
                                            foreach ($ma_value as $index => $value): ?>    
                                                <tr>
                                                    <td style="width:60%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $index  ?></td>
                                                    <td style="width:20%; text-align: right;"><?php echo $value[0]['amount']<0 ?'('.abs($value[0]['amount']).')' : abs($value[0]['amount']);// echo abs($value[0]['amount']) ?></td>
                                                    <?php $total += $value[0]['amount']; ?>
                                                    <?php $total_expense += $value[0]['amount']; ?>
                                                    <td></td>
                                                   
                                                </tr>
                                            <?php endforeach; ?> 
                                        <tfoot>
                                        
                                        <th><span class="pull-right">Total <?php echo $ma_index; ?>:</span></th>
                                        <th></th>
                                        <th style="text-align:right;"><?php if($total<0){echo "(";} echo number_format(abs($total), 2); if($total<0){echo ")";} ?></th>
                                        
                                        
                                        </tfoot> 
                                        </tbody>
                                    </table>
                                <?php endif; ?>   
                            <?php endforeach; ?>  
                                </td>
                                <td></td>
                                </tbody>
                                <tfoot>
                                    <tr>
                                        <th style="width:40%"><strong style="font-size:16px;">Total Expense</strong></th>
                                        <th style="width:40%"></th>
                                        <th style="text-align: right;"><i><?php if($total_expense<0){echo "(";} echo number_format(abs($total_expense), 2); if($total_expense<0){echo ")";}?></i></th>
                                    </tr>
                                </tfoot>
                                </table>

                            </td>
                            <td></td>
                            </tr>
                            </tbody>
                            <tfoot>
                                <tr>
                                    <th colspan="2" style="width:80%;  "><strong style="font-size:16px;"> P&L Statement</strong></th>
                                    <th style="width:20%; text-align: right;"><i><?php echo number_format($gross_profit - $total_expense, 2); ?></i>
                                    </th>

                                </tr>
                            </tfoot>
                            </table>                    
                       
                   <!--Expenses End--> 
                   <!--End Using Account Vouchers-->  
                   
                    <?php } else { ?>
                   <!--Using Old Method-->  
                    <div class="row">
                        <div class="col-md-12">
                        <?php if(DOWNLOAD == true || $this->session->userdata('role') == 'Admin' || $this->session->userdata('role')=='Super User'){ ?>
                            <a class="btn btn-warning m-l-10" onclick="tableToExcel('sheet1', 'Profit and Loss')" style="float:right">Export to Excel</a>
                        <?php } ?>
                            <table class="table  table-condensed" id="sheet1">
                                <thead>
                                    <tr>
                                        <th class="text-center">Profit & Loss Statement</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td>
                                        <?php $total_income = 0; //foreach($profit_loss['Revenue'] as $ma_index=>$ma_value):  ?> 
                                            <table class="table  table-condensed">
                                                <tbody>
                                                    <tr><td colspan="3"><strong><i>Invoiced Revenue</i></strong></td></tr>

                                                    <?php $total = 0; foreach ($invoice as $index => $value): ?>  
                                                    <?php if ($index !== 'totalPaid'): ?>
                                                            <tr>
                                                                <td style="width:60%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $index ?></td>
                                                                <td style="width:40%;"><?php echo number_format(abs($value), 2) ?></td>
                                                            <?php $total += $value; ?>
                                                            <?php $total_income += $value; ?>
                                                            </tr>
                                                        <?php endif; ?> 
                                                     <?php endforeach; ?> 

                                                <tfoot>
                                                <th><spn class="pull-right">Total Sale:</spn></th>
                                                <th><?php echo number_format(abs($total), 2) ?></th>
                                                </tfoot> 
                                </tbody>
                            </table>

                                    <?php //endforeach; ?> 

                            <br />

                             <?php //foreach($profit_loss['Revenue'] as $ma_index=>$ma_value):  ?> 
                            <table class="table  table-condensed">
                                <tbody>
                                    <tr><td colspan="3"><strong><i>Vouchers Revenue</i></strong></td></tr>

                                    <?php $total = 0;
                                    foreach ($voucher as $index => $value): ?>  
                                        <?php if ($index !== 'totalVoucherAmount'): ?>
                                            <tr>
                                                <td style="width:60%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $index ?></td>
                                                <td style="width:40%;"><?php echo number_format(abs($value), 2) ?></td>
                                    <?php $total += $value; ?>
                                    <?php $total_income += $value; ?>
                                            </tr>
                                        <?php endif; ?> 
                                    <?php endforeach; ?> 

                                <tfoot>
                                    <tr>
                                        <th><span class="pull-right">Total Vouchers:</span></th>
                                        <th><?php echo number_format(abs($total), 2) ?></th>
                                    </tr>
                                    <tr>
                                        <th><span class="pull-right"><b>Total Income:</b></span></th>
                                        <th><?php echo number_format(abs($total_income), 2) ?></th>
                                    </tr>
                                </tfoot> 
                                </tbody>
                            </table>

                                    <?php //endforeach; ?> 

                            <br />

                            <!--Cost of Goods-->
                            <?php $total_expense_suppleirs = 0;
                            foreach ($profit_loss['Expenses'] as $ma_index => $ma_value): ?>
                                <?php if ($ma_index === 'Cost of Goods Sold'): ?> 
                            <table class="table  table-condensed">
                                <tbody>
                                    <tr><td colspan="3"><strong><i><?php echo $ma_index ?></i></strong></td></tr>
                                    <?php $total = 0;
                                    foreach ($ma_value as $index => $value): ?>    
                                        <tr>
                                            <td style="width:60%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $index ?></td>
                                            <td style="width:40%;"><?php echo abs($value[0]['amount']) ?></td>
                                    <?php $total += $value[0]['amount']; ?>
                                    <?php $total_expense_suppleirs += $value[0]['amount']; ?>
                                        </tr>
                                    <?php endforeach; ?> 

                                <tfoot>
                                <th><spn class="pull-right">Total <?php echo $ma_index ?>:</spn></th>
                                <th><?php echo number_format(abs($total), 2) ?></th>
                                </tfoot> 

                                </tbody>
                            </table>

                            <?php endif; ?>   
                            <?php endforeach; ?>
                        <!--Cost of Goods-->
                        <!---Gross Profit-->
                            <table class="table  table-condensed">
                                <tbody>
                                <td style="width:60%;"><strong><i>Gross Profit</i></strong></td>  
                                <?php $gross_profit = $total_income - $total_expense_suppleirs; ?>
                                <td style="width:40%;"><strong><i><?php echo number_format(abs($gross_profit), 2) ?></i></strong></td>

                                </tbody>
                            </table>   
                        <!--End Gross profit-->
                            
                            
                            <?php $total_expense = 0;
                            foreach ($profit_loss['Expenses'] as $ma_index => $ma_value): ?> 
                            <?php if ($ma_index !== 'Cost of Goods'): ?> 
                                    <table class="table  table-condensed">
                                        <tbody>
                                            <tr><td colspan="3"><strong><i><?php echo $ma_index ?></i></strong></td></tr>
                                            <?php $total = 0;
                                            foreach ($ma_value as $index => $value): ?>    
                                                <tr>
                                                    <td style="width:60%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $index ?></td>
                                                    <td style="width:40%;"><?php echo abs($value[0]['amount']) ?></td>
                                                    <?php $total += $value[0]['amount']; ?>
                                                    <?php $total_expense += $value[0]['amount']; ?>
                                                </tr>
                                            <?php endforeach; ?> 
                                        <tfoot>
                                        <th><spn class="pull-right">Total <?php echo $ma_index ?>:</spn></th>
                                        <th><?php echo number_format(abs($total), 2) ?></th>
                                        </tfoot> 
                                        </tbody>
                                    </table>
                                <?php endif; ?>   
                            <?php endforeach; ?>  


                            </td>

                            </tr>
                            </tbody>
                            <tfoot>
                                <tr>
                                    <th>
                                        <div class="pull-left" style="width:60%;">Statement</div>
                                        <div class="pull-left" style="width:40%;"><?php echo number_format($gross_profit - $total_expense, 2); ?></div>
                                    </th>

                                </tr>
                            </tfoot>
                            </table>
                    <?php } ?>
                        </div>


                    </div>

                </div>
            </div>
        </div>    
    </div>
</div>

<script>
    $(document).ready(function () {
        $("#fromAccount").select2();
        $("#toAccount").select2();

        $("#startdate").datepicker({autoclose: true, format: 'yyyy-mm-dd'});
        $("#enddate").datepicker({autoclose: true, format: 'yyyy-mm-dd'});

        toastr.options = {
            "closeButton": true,
            "debug": true,
            "newestOnTop": true,
            "progressBar": true,
            "positionClass": "toast-bottom-center",
            "preventDuplicates": true,
            "onclick": null,
            "showDuration": "300",
            "hideDuration": "1000",
            "timeOut": "5000",
            "extendedTimeOut": "1000",
            "showEasing": "swing",
            "hideEasing": "linear",
            "showMethod": "fadeIn",
            "hideMethod": "fadeOut"
        }

        $(".numeric").keypress(function (e) {
            //if the letter is not digit then display error and don't type anything
            if (e.which != 13 && e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
                return false;
            }
        });
    });



    function runreport() {

        if ($('#rangehidden').val() != 0) {
            $("#cog").show();
            setTimeout(function () {
                $("#cog").hide();
            }, 500);

            if (startdate == "" || enddate == "") {
                return false;
            } else {
                $('#rangehidden').val(0);
            }

        }
        if (startdate == "" || enddate == "") {
            swal({
                title: "Select Date Range",
                text: "Please select a date Range",
                type: "warning",
                confirmButtonText: 'OK!'
            });
            return false;
        }

        console.log(startdate.format('YYYY-MM-DD') + ' ' + enddate.format('YYYY-MM-DD'));
        voucher_list();

    }

var tableToExcel = (function () {
          //  application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
            var uri = 'data:application/vnd.ms-excel;base64,'
              , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{table}</table></body></html>'
              , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
              , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
            return function (table, name) {
                if (!table.nodeType) table = document.getElementById(table)
                var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
                var link = document.createElement("a");
                
                var d = new Date();
                var datestring = ("0" + d.getDate()).slice(-2) + "-" + ("0"+(d.getMonth()+1)).slice(-2) + "-" +
                d.getFullYear() + " " + ("0" + d.getHours()).slice(-2) + ":" + ("0" + d.getMinutes()).slice(-2);
    
                link.download = ctx.worksheet + ' ' + datestring;
                link.href = uri + base64(format(template, ctx));
                link.click();
                //window.location.href = uri + base64(format(template, ctx))
            }
        })()



        function checkdaterange(){

            const startdate = new Date($("#startdate").val());
            const enddate = new Date($("#enddate").val());
            
            if($("#enddate").val()==''){
                var d = new Date(startdate);
                d.setDate(startdate.getDate() + 1);           
                var mMonth = ((d.getMonth()+1) <10 ? '0' : '') + (d.getMonth()+1);
                var mDay = (d.getDate() < 10 ? '0' : '') + d.getDate();
                $("#enddate").val(d.getFullYear() +'-'+ mMonth + '-' + mDay); 
            }else if($("#startdate").val()==''){
                var d = new Date();
                d.setDate(enddate.getDate() - 1);           
                var mMonth = ((d.getMonth()+1) <10 ? '0' : '') + (d.getMonth()+1);
                var mDay = (d.getDate() < 10 ? '0' : '') + d.getDate();
                $("#startdate").val(d.getFullYear() +'-'+ mMonth + '-' + mDay);                 
            }
            
            if($("#startdate").val()!='' && $("#enddate").val()!=''){  
                var selecteddays = date_diff_indays(startdate, enddate);
                console.log(selecteddays);
                if(selecteddays < 0){
                    swal({
                        title: 'Invalid Date Selected',
                        text: "End Date cannot be less then you start date",
                        type: 'warning',
                        showCancelButton: false,
                        confirmButtonColor: '#ff5b5b !important'
                    });
                     $("#enddate").val('');
                } else if(selecteddays > 183){
                    swal({
                        title: 'Date Range Is Too Big',
                        text: "It is advisable to use date range of maximum 6 Months at a time",
                        type: 'warning',
                        showCancelButton: false,
                        confirmButtonColor: '#ff5b5b !important'
                    });
                    
                    var e = new Date(startdate);
                    if(startdate.getMonth()>5){
                        e.setDate(startdate.getDate() + 183);
                    }else{
                        e.setDate(startdate.getDate() + 180);
                    }
                    
                    var eMonth = ((e.getMonth()+1) <10 ? '0' : '') + (e.getMonth()+1);
                    var eDay = (e.getDate() < 10 ? '0' : '') + e.getDate();
                    //$("#enddate").val(e.getFullYear() +'-'+ eMonth + '-' + eDay); 
                    $("#enddate").val('');
                }
            }
            
        }
        
        
        var date_diff_indays = function(date1, date2) {
            dt1 = new Date(date1);
            dt2 = new Date(date2);
            return Math.floor((Date.UTC(dt2.getFullYear(), dt2.getMonth(), dt2.getDate()) - Date.UTC(dt1.getFullYear(), dt1.getMonth(), dt1.getDate()) ) /(1000 * 60 * 60 * 24));
        }
        
 

</script>