<div class="wrapper">
    <div class="container">
        <!-- Page-Title -->
        <div class="row">
            <div class="col-lg-12">                
                <h4 class="page-title">Tax Collection (<?php echo $business[0]['business_name'];?>):</h4>
            </div>
        </div>

        <div class="row" id="divselection">
            <div class="col-sm-12">
                <div class="card-box ">                    
                    <div class="row hidden-print">
                        <div class="col-lg-12">
                            <div class="row m-t-40">                               
                                <div class="col-md-4">
                                    <h3 class="logo invoice-logo"><?php if(isset($business)){ echo "<img src='".base_url().'assets/images/business/'.$business[0]['business_logo']."' alt='".$business[0]['business_name']."' class='img-responsive' />";} else {echo 'SalonPK';}?></h3>
                                </div>
                                <div class="btn-group col-md-8 hidden-print m-t-20">
                                    <form method="POST" action='<?php echo base_url();?>accountreports_controller/salestaxreport'>
                                        <input type="hidden" name="csrf_test_name" id="statement_csrf" value=""/>
                                        <div class="col-md-2" >
                                            <input onchange="checkdaterange();" style="background-color: white;"  class="form-control datepicker" name="startdate" id="startdate" value="<?php if(isset($startdate)){echo $startdate;}?>"/>
                                        </div>
                                        <div class="col-md-2">
                                            <input onchange="checkdaterange();" style="background-color: white;"  class="form-control datepicker" name="enddate" id="enddate" value="<?php if(isset($enddate)){echo $enddate;}?>"/>
                                        </div>
                                        
                                        <div class="col-md-2">
                                            <button type='submit' onclick="$('#statement_csrf').val($('#cook').val());" class="btn btn-pink btn-bordred waves-effect w-md waves-light m-b-5">Run</button>
                                        </div>
                                        <div class="col-md-3">
                                            <select id="businessid" name="businessid" class="form-control">
                                                <option value="0">All</option>
                                                <?php foreach ($businesses as $business) {?>
                                                    <option <?php if($selected_business==$business['id_business']){echo " selected='selected' ";} ?> value="<?php echo $business['id_business']; ?>"><?php echo $business['business_name']; ?></option>
                                               <?php  }?>
                                            </select>
                                        </div>
                                        <div class="col-md-3">
                                            <select id="invoicetype" name="invoicetype" class="form-control">
                                                <option <?php if($invoicetype == 'service'){echo 'selected="selected"';} ?> value="service">Service</option>
                                                <option <?php if($invoicetype == 'sale'){echo 'selected="selected"';} ?> value="sale">Retail</option>
                                                <option <?php if($invoicetype == 'all'){echo 'selected="selected"';} ?> value="all">All</option>                                               
                                            </select>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div><!--Card box-->
            </div>
        </div>
        <div class="row">
             <div class="col-sm-12">
                <div class="card-box print-area">
                    <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', 'Sales Tax Details')" style="float:right">Export to Excel</a>
                        <?php } ?>
                            <table id='sheet1' class='table table-bordered'>
                                <thead class="pink">
                                <tr>
                                    <th>Sr.No.</th>
                                    <th>Date</th>
                                    <th>IMS #</th>
                                    <th>Main Service/Product</th>
                                    <th>Brand</th>
                                    <th>Branch/Division</th>
                                    <th>Sales Rep.</th>
                                    <th>Type</th>
                                    <th>Client Name</th>
                                    <th>Value Exc. Sales Tax</th>
                                    <th>Sales Tax</th>
                                    <th>Tax %age</th>
                                    <th>Value Inc. Sales Tax</th>
                                    <th>Cash/Card</th>
                                </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td colspan="13">
                                            From <?php echo date_format(date_create($startdate), 'd-m-Y');?> To <?php echo date_format(date_create($enddate), 'd-m-Y');?>
                                        </td>
                                    </tr>
                                <?php $x=1; $cashservices=0; $cardservices=0; $totalexctax=0;$totaltax=0;$totalinctax=0; foreach($data as $d){?>
                                <tr>
                                    <td><?php echo $x;?>
                                    <td><?php echo $d['invoice_date'];?></td>
                                    <td><?php echo "`".$d['imsfiscal_invoice_number'];?></td>
                                    <td><?php echo $d['Main Service'];?></td>
                                    <td><?php echo $d['business_brand_name'];?></td>
                                    <td><?php echo $d['Branch'];?></td>
                                    <td><?php echo $d['Sales Rep'];?></td>
                                    <td><?php echo $d['Type'];?></td>
                                    <td><?php echo $d['Client Name'];?></td>
                                    <td style='text-align: right;'><?php echo $d['Value Exc. Sales Tax']; $totalexctax=$totalexctax+$d['Value Exc. Sales Tax'];?></td>
                                    <td style='text-align: right;'><?php echo $d['Sales Tax']; $totaltax=$totaltax+$d['Sales Tax'];?></td>
                                    <td style='text-align: right;'><?php echo $d['tax_perc']. ' %'; ?></td>
                                    <td style='text-align: right;'><?php echo $d['Value Exc. Sales Tax']+$d['Sales Tax']; $totalinctax=$totalinctax+ ($d['Value Exc. Sales Tax']+$d['Sales Tax']);?></td>
                                    
                                    <td><?php echo $d['Cash/Card']; if($d['Cash/Card']=='CASH'){$cashservices=$cashservices+($d['Value Exc. Sales Tax']+$d['Sales Tax']);}else{$cardservices=$cardservices+($d['Value Exc. Sales Tax']+$d['Sales Tax']);}?></td>
                                </tr>
                                <?php $x++; } ?>
                                </tbody>
                                <tfoot>
                                    <tr>
                                        <th>Sr.No.</th>
                                        <th>Date</th>
                                        <th>IMS #</th>
                                        <th>Main Service/Product</th>
                                        <th>Branch/Division</th>
                                        <th>Sales Rep.</th>
                                        <th>Type</th>
                                        <th>Client Name</th>
                                        <th>Value Exc. Sales Tax</th>
                                        <th>Sales Tax</th>
                                        <th>Tax %age</th>
                                        <th>Value Inc. Sales Tax</th>
                                        <th>Cash/Card</th>
                                        <th></th>
                                    </tr>
                                    <tr>
                                        <td colspan="8"></td><!-- comment -->
                                        <td style='text-align: right; font-weight: bold;'><?php echo number_format($totalexctax,2);?></td>
                                        <td style='text-align: right; font-weight: bold;'><?php echo number_format($totaltax,2);?></td><!-- comment -->
                                        <td></td>
                                        <td style='text-align: right; font-weight: bold;'><?php echo number_format($totalinctax,2);?></td>
                                        <td></td>
                                        <td></td>
                                    </tr>
                                    <tr>
                                        <td colspan="8" style="text-align: right;"><strong>Collection Breakup:</strong></td><!-- comment -->
                                        <td></td>
                                        <td></td>
                                        <td colspan="4" ></td>
                                        <td></td>
                                    </tr>
                                    <tr>
                                        <td colspan="8"></td>
                                        <td><strong>Cash: </strong></td>
                                        <td><strong><?php echo number_format($cashservices,2); ?></strong></td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                       <td></td>
                                        
                                    </tr>
                                    <tr>
                                        <td colspan="8"></td>
                                        <td><strong>Card: </strong></td>
                                        <td><strong><?php echo number_format($cardservices,2); ?></strong></td>
                                        <td></td>
                                        <td></td>
                                         <td></td>
                                        <td></td>
                                    </tr>
                                </tfoot>
                            </table>
                            
                            
                        </div>
                    </div>
                    
                    
                </div>
            </div>
        </div>
        
    </div>
        

<script>
    $(document).ready(function() {
       
       $("#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;
            }
        });
        
                            
//        $('#sheet1').DataTable({
//            lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]],
//            stateSave: true, fixedHeader: {header: true},
//            dom: "Bfrtlip",
//            buttons: [{extend: "copy", className: "btn-sm btn-custom btn-trans"},
//                {extend: "csv", className: "btn-sm btn-pink btn-trans"},
//                {extend: "pdf", className: "btn-sm btn-purple btn-trans"},                    
//                {extend: "print", className: "btn-sm btn-warning btn-trans"}
//        ],
//            responsive: 0,
//            ordering: false
//        });
                    
        
    });
    
    
    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();
                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'
                    });
                } 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);
                    e.setDate(startdate.getDate() + 183);
                    var eMonth = ((e.getMonth()+1) <10 ? '0' : '') + (e.getMonth()+1);
                    var eDay = (e.getDate() < 10 ? '0' : '') + e.getDate();
                    $("#enddate").val(e.getFullYear() +'-'+ eMonth + '-' + eDay); 
                    
                }
            }
            
        }
        
        
        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>        