<style>
    .table thead tr th{
        border-top:2px solid #eee;
        border-bottom:2px solid #eee;
        background: #fcfcfc;
    }
    .table tfoot tr th{
        border-top:2px solid #eee;
        border-bottom:2px solid #eee;
        background: #fcfcfc;
    }
</style>
<div class="wrapper">
    <div class="container-full">
        <div class="row m-t-20">
            <div class="col-md-12">
                <div class="panel panel-default m-t-20">
                    <div class="panel-body">
                        <div class="row">
                            <div class="pull-left" >
                                <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 'SkedWise';
                                    }
                                    ?></h3>
                            </div>
                            <div class="pull-right m-t-20" >
                                <div class="col-md-6 hidden-print m-t-20">
                                    <form method="POST" action='<?php echo base_url(); ?>marketing'>
                                        <input type="hidden" name="csrf_test_name" id="daily_sheet_csrf" value=""/>
                                        <div class="col-md-10">
                                            <input type="month" autocomplete="off" class="form-control inline m-r-5" name="calendar_date" id="datepicker-autoclose1">
                                        </div>
                                        <div class="col-md-2">
                                            <button type='submit' onclick="$('#daily_sheet_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 class="col-md-6 hidden-print m-t-20">
                                    <!--<a class="btn btn-warning m-l-10" onclick="tableToExcel('sheet1', 'Marketing Sheet')" style="float:right">Export to Excel</a>-->
<!--                                    <a class="btn btn-warning m-l-10" href="<?php //echo base_url('marketing_controller/marketing_excell'.'/'.$date); ?>" style="float:right">Export to Excel</a>
                                    <a href="javascript:window.print()" class="btn btn-inverse waves-effect waves-light hidden-print pull-right" id="btnprint"><i class="fa fa-print"></i></a>-->
                                </div>
                            </div>
                            <div class="clearfix"></div>
                        </div>
                        <!-- end row -->

                        <div class="row">
                            <table class="table m-t-10"  id='sheet1'>
                                <tr>
                                    <td colspan="4"><strong>Marketing Sheet <u> <?php echo isset($date) && !empty($date) ? date('M-Y', strtotime($date)) : date('M-Y'); ?></u></strong></td>
                                </tr>
                                <tr>
                                    <td colspan="3">
                                        <table class="table table-striped table-bordered"  id='datatable-buttons'>
                                            <thead>
<!--                                                <tr>
                                                    <th colspan='7'><strong>Invoice Details</strong></th>
                                                </tr>-->
                                                <tr>
                                                    <th>Date</th>
                                                    <th>Invoice</th>
                                                    <th>Staff Name</th>
                                                    <th>Customer</th>
                                                    <th>Service / Product</th>
                                                    <th>SERVICE / PRODUCT / ADVANCE / RECOVERY</th>
                                                    <th>Payment Mode</th>
                                                    <th>Price</th>
                                                    <th>Discount</th>
                                                    <th>Addition</th>
                                                    <th>Advance Adjusted</th>
                                                    <th>Amount</th>
                                                    <th>Total</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                if(isset($invoices) && !empty($invoices)){
                                                    $invoice_detail_id = 0; $invoice_id=0; $staffid= 'start';$totalpaid=0; $invoicetype=''; $totalservice=0; $totalsale=0; $overalltotal=0; $totaladvanceadj=0;
                                                    foreach ($invoices as $invoice) {
                                                        
                                                        if($staffid!=='start' && $staffid!==$invoice['staff_id']){?>
                                                            <!--//do Add Row -->
                                                            <!--add a totals row-->
                                                         <tr>
                                                            <td ></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td ></td>
                                                            <td style='font-weight:bold;font-style: italic;'><?php echo $invoicetype;?></td>
                                                            <td style='font-weight:bold;font-style: italic;'><?php echo $staffid;?></td>
                                                            <td style='font-weight:bold;font-style: italic;'><?php echo number_format($totalpaid,2); $totalpaid=0;?></td>
                                                            
                                                        </tr>    
                                                        <?php } ?>
                                                        <tr>
                                                            <td>
                                                                <?php if($invoice['reference_invoice_number']!==''){echo '<span class="text-danger"> Recovery '.$invoice['visit_date'];}else {echo $invoice['invoice_date'];}?>
                                                            </td>
                                                            <td>
                                                                <?php if ($invoice['invoice_type'] == 'service') {?>
                                                                <?php echo '<a target="_blank" href="'.base_url().'existinginvoice/'. $invoice['id_invoice'] .'"><span class="text-danger"> '.$invoice['id_invoice'];?>
                                                                <?php } else { ?>
                                                                <?php echo '<a target="_blank" href="'.base_url().'existingorderinvoice/'. $invoice['id_invoice'] .'"><span class="text-danger"> '.$invoice['id_invoice'];?>
                                                                <?php } ?>
                                                            </td>
                                                            <!--staff-->
                                                            <td>
                                                                <?php 
                                                                if ($invoice['invoice_type'] == 'service') {
                                                                    if ($invoice['additional_staff'] =='No'){
                                                                        echo $invoice['staff']; 
                                                                    } elseif ($invoice['additional_staff'] =='Yes'){
                                                                        echo $invoice['staff'].' (supporting)'; 
                                                                    }
                                                                } else { 
                                                                    echo $invoice['product_staff'];
                                                                } 
                                                                ?>
                                                            </td>
                                                            <td><b><?php echo $invoice['customer_name']; ?></b></td>
                                                            <td><b><?php echo $invoice['invoice_type']; ?></b></td>
                                                            <td>
                                                                <?php if ($invoice['invoice_type'] == 'service') { ?>
                                                                    <?php echo $invoice['service']; ?>
                                                                <?php } else { ?>
                                                                    <?php echo $invoice['product_name']; ?>
                                                                <?php } ?>
                                                            </td>
                                                            <td><?php echo $invoice['payment_mode']; ?></td>
                                                            <!--Price-->
                                                            <td> 
                                                                <?php if($invoice['reference_invoice_number']!=='' ){ 
                                                                    if($invoice_id!==$invoice['id_invoice'] || $invoice['paid_details']>0){
                                                                        echo $invoice['gross_amount'] - $invoice['paid_earlier'] ; 
                                                                    }
                                                                
                                                                } elseif($invoice['invoice_type']=='service'){
                                                                    if ($invoice['additional_staff'] =='No'){
                                                                        echo $invoice['price']; 
                                                                    }
                                                                } else {
                                                                    echo number_format($invoice['price']*$invoice['invoice_qty'],2); 
                                                                }
                                                                 ?>
                                                             </td>
                                                             <!--Discount-->
                                                             <td>
                                                                 <?php 
                                                                 if($invoice['invoice_type']=='service'){
                                                                     if($invoice['reference_invoice_number']==''){
                                                                         if ($invoice['additional_staff'] =='No'){
                                                                             
                                                                            echo number_format($invoice['total_discount'],2); 
                                                                         }
                                                                     }
                                                                 }else {
                                                                    echo number_format($invoice['total_discount'],2); 
                                                                 }
                                                                 ?>
                                                             </td>
                                                             <!--Addition-->
                                                             <td>
                                                                 <?php 
                                                                 if($invoice['invoice_type']=='service'){
                                                                     if($invoice['reference_invoice_number']==''){
                                                                         if ($invoice['additional_staff'] =='No'){
                                                                             
                                                                            echo number_format($invoice['service_addition'],2); 
                                                                         }
                                                                     }
                                                                 }else {
                                                                    echo number_format($invoice['service_addition']*$invoice['invoice_qty'],2); 
                                                                 }
                                                                 ?>
                                                             </td>
                                                              <!--Advance Adjusted-->
                                                              <td>
                                                                  <?php if($invoice['reference_invoice_number']==''){
                                                                      if($invoice['invoice_type']=='service'){
                                                                        if ($invoice['additional_staff'] =='No'){
                                                                            $advance_adj= ($invoice['invoice_advance_percentage']*$invoice['discounted_price'])/100;
                                                                           echo number_format($advance_adj,2); 
                                                                           $totaladvanceadj+=$advance_adj;
                                                                        }
                                                                      
                                                                      } else {
                                                                         $advance_adj= ($invoice['invoice_advance_percentage']*($invoice['discounted_price']))/100;
                                                                           echo number_format($advance_adj,2); 
                                                                           $totaladvanceadj+=$advance_adj;
                                                                      }
                                                                  }
                                                                  ?>
                                                                  
                                                              </td>
                                                              
                                                            <!--Paid-->
                                                            <td style="text-align: right;">
                                                               
                                                                <strong>
                                                                    <?php 
                                                                    
                                                                    if($invoice['reference_invoice_number']!==''){
                                                                        if($invoice_id!==$invoice['id_invoice'] || $invoice['paid_details']>0){
                                                                            if($invoice['invoice_type'] == 'service'){
                                                                                if ($invoice['additional_staff'] =='No'){
                                                                                    echo $invoice['paid_details'];
                                                                                    $totalpaid+=$invoice['paid_details'];
                                                                                    $totalservice+=$invoice['paid_details'];
                                                                                    
                                                                                }
                                                                            } else {
                                                                                echo $invoice['paid_details'];
                                                                                    $totalpaid+=$invoice['paid_details'];
                                                                                    $totalsale+=$invoice['paid_details'];
                                                                                    
                                                                            }
                                                                        }
                                                                    }elseif ($invoice['invoice_type'] == 'service') { 
                                                                      //  if ($invoice['additional_staff'] == 'No'){
                                                                            
                                                                            echo number_format($invoice['paid_details'],2);
                                                                            $totalpaid+= $invoice['paid_details'];
                                                                            $totalservice+=$invoice['paid_details'];
                                                                       // } 
                                                                    } elseif ($invoice['invoice_type'] == 'sale') {
                                                                        echo number_format($invoice['paid_details'], 2);                                                                         
                                                                        $totalpaid+=$invoice['paid_details'];
                                                                        $totalsale+=$invoice['paid_details'];
                                                                    }
                                                                    ?>
                                                                </strong>
                                                            </td>
                                                            <td></td>
                                                        </tr>
                                                       
                                                        <?php
                                                        if ($invoice['invoice_type'] == 'service') {
                                                            $staffid= $invoice['staff_id']; 
                                                        } elseif ($invoice['invoice_type'] == 'sale') { 
                                                            $staffid= $invoice['staff_id'];
                                                        } else {
                                                            $staffid= 'Advance';
                                                        }
                                                        $invoice_detail_id=$invoice['id_invoice_details'];
                                                        $invoice_id=$invoice['id_invoice'];
                                                        $invoicetype=$invoice['invoice_type'];
                                                        

                                                       // $staffid=$invoice['staff'];
                                                    }?>
                                                    <!--Add last staff addition row-->
                                                        <tr>
                                                            <td ></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td style='font-weight:bold;font-style: italic;'></td>
                                                            <td style='font-weight:bold;font-style: italic;'><?php echo $invoicetype;?></td>
                                                            <td style='font-weight:bold;font-style: italic;'><?php echo $staffid;?></td>
                                                            <td style='font-weight:bold;font-style: italic;'><?php echo number_format($totalpaid,2); $overalltotal=$overalltotal+$totalpaid; $totalpaid=0;?></td>
                                                            
                                                        </tr>    
                                                    <?php 
                                                    if(isset($advances) && !empty($advances)){
                                                    ?>
                                                        
                                                    <?php $idadvance=0;$totaladvance=0;
                                                    foreach ($advances as $advance){
                                                        if($idadvance!==$advance['id_visit_advance']){
                                                    ?>
                                                        
                                                        <tr>
                                                            <td><?php echo $advance['advance_date']; ?></td>
                                                            <td><?php echo $advance['id_visit_advance']; ?></td>
                                                            <td>Advance</td>
                                                            <td><strong><?php echo $advance['customer_name']; ?></strong></td>
                                                            <td>Appointment</td>
                                                            <td><?php echo $advance['service_category'].' '.$advance['service_name'].' <?php echo CURRENCY_CODE;?>. '.$advance['service_rate']; ?></td>
                                                            <td><?php echo $advance['advance_mode']; ?></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td style="text-align: right;"><strong><?php echo number_format($advance['advance_amount'], 2); $totaladvance+=$advance['advance_amount']; ?></strong></td>
                                                            <td></td>
                                                        </tr>
                                                        <?php $idadvance=$advance['id_visit_advance'];}
                                                        }
                                                    }?>
                                                    <!--//Add advance total row-->
                                                    <tr>
                                                            <td ></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td style='font-weight:bold;font-style: italic;'>Advance</td>
                                                            <td style='font-weight:bold;font-style: italic;'>Total</td>
                                                            <td style='font-weight:bold;font-style: italic;'><?php if(isset($totaladvance)){ echo number_format($totaladvance,2);}?></td>
                                                            
                                                        </tr>    
                                                <?php }else{
                                                ?>
                                                        <tr>
                                                            <td >No Data to show</td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                        </tr>     
                                                <?php } ?>
                                            </tbody>
                                            <tfoot>
                                                <tr>
                                                    <th ></th>
                                                    <th></th>
                                                    <th></th>
                                                    <th></th>
                                                    <th></th>
                                                    <th></th>
                                                    <th></th>
                                                    <th></th>
                                                    <th>Advance Adjusted</th>
                                                    <th>Advances Collected</th>
                                                    <th>Retail</th>
                                                    <th>Services</th>
                                                    <th>Total</th>
                                                </tr>     
                                                <tr>
                                                    <th ></th>
                                                    <th></th>
                                                    <th></th>
                                                    <th></th>
                                                    <th></th>
                                                    <th></th>
                                                    <th></th>
                                                    <th></th>
                                                    <th><?php echo number_format($totaladvanceadj,2);?></th>
                                                    <th><?php if(isset($totaladvance)){ echo number_format($totaladvance,2);}?></th>
                                                    <th><?php echo number_format($totalsale,2);?></th>
                                                    <th><?php echo number_format($totalservice,2);?></th>
                                                    <th><?php
                                                        $mTotal=$totalsale+$totalservice;
                                                        if(isset($totaladvance)){
                                                            $mTotal = $mTotal + $totaladvance;
                                                        }
                                                        if(isset($totaladvanceadj)){
                                                            $mTotal = $mTotal - $totaladvanceadj;
                                                        }
                                                         echo number_format($mTotal,2);
                                                        ?>
                                                    </th>
                                                </tr>     
                                            </tfoot>
                                        </table>
                                    </td>
                                    <td>

                                    </td>
                                </tr>

                            </table>
                        </div>
                        <!-- end row -->
                    </div>
                </div>

<script>
    $(document).ready(function(){
//        $('#datepicker-autoclose').datepicker({
//            autoclose: true,
//                todayHighlight: true,
//                format: 'yyyy-mm-dd'
//        });

        $('#datatable-buttons').DataTable({
                    lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]],
                    stateSave: true, fixedHeader: {header: true},
                    <?php if(DOWNLOAD == true || $this->session->userdata('role') == 'Admin' || $this->session->userdata('role')=='Super User'){ ?>
                    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"}
                    ],
                    <?php } else { ?>
                        dom: "frtlip",
                    <?php } ?> 
                    responsive: 0,
                    ordering: false
                });
    
    });


</script>