<style>
     tr.strikethrough > td {text-decoration:line-through !important; color:red;} 
     tr.sale > td {font-weight:bold !important; color:blue;} 
</style>
<div class="wrapper">
            <div class="full-container">
                <!-- Page-Title -->
                <div class="row">
                    <div class="col-sm-12">
                        
                        <h4 class="page-title">Invoices</h4>
                    </div>
                </div>
                
                 <div class="row">
                     
                     <div class="row" id="divselection">
                        <div class="col-sm-12">
                            <div class="card-box ">
                                <!--<h4 class="header-title m-t-0 m-b-30">Selection:</h4>-->
                                <div class="row">
                                    <div class="col-lg-12">
                                        <div class="row">
                                            <div class="col-lg-6" >
                                                <span style="font-weight: bold;">Months Total Service:</span> <span  style="font-weight: bold;" id="monthstotalservice"><?php if(sizeof($monthstotalservice)>0) {echo number_format($monthstotalservice[0]['total_payable'],2); $mtservice=$monthstotalservice[0]['total_payable'];} else {echo '0';$mtservice=0;}?></span><span style="font-weight: bold;"><br/>
                                                    Months Total Retail:</span> <span  style="font-weight: bold;" id="monthstotalsale"><?php if(sizeof($monthstotalsale)>0) {echo number_format($monthstotalsale[0]['total_payable'],2); $mtsale=$monthstotalsale[0]['total_payable'];}else{echo '0';$mtsale=0;}?></span></br>
                                                    <hr><span style="font-weight: bold;">Total of Month:<?php echo number_format($mtservice + $mtsale,2);?></span>
                                            </div>
                                            <div class="col-lg-6">
                                            <form method="POST" action='<?php echo base_url();?>sh_controller/shadow_invoices'>
                                                <input type="hidden" name="csrf_test_name" id="daily_sheet_csrf" value=""/>
                                                    <div class="col-md-2">
                                                        <label class="control-label">Select Date</label>
                                                    </div>
                                                    <div class="col-md-6">

                                                        <input type="text" class="form-control inline m-r-5"  placeholder="mm/dd/yyyy" name="calendar_date" id="datepicker-autoclose">

                                                    </div>
                                                    <div class="col-md-4">
                                                        <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>
                                    </div>
                                </div>
                            </div>
                        </div>
                     </div>
                
                    <div class="col-sm-12">
                        <div class="card-box table-responsive">
                            <div class="row">
                                <div class='col-md-6 col-xs-12'>
                                    <h5 class='text-primary'>Shadowed Services: <span id="totalshadowservice"></span></h5>
                                    <h5 class='text-primary'>Shadowed Retail: <span id="totalshadowretail"></span></h5>
                                    <hr/>
                                    <h4 class='text-primary' style='font-weight:bold'>Total Shadowed: <span id="totalshadowed"></span></h4>
                                    <hr/>
                                </div>
                                <div class='col-md-6 col-xs-12'>
                                    <h5 class='text-success'>Actual Services: <span id="actualtotalservice"></span></h5>
                                    <h5 class='text-success'>Actual Retail: <span id="actualtotalretail"></span></h5>
                                    <hr/>
                                    <h4 class='text-success' style='font-weight:bold'>Actual Total: <span id="actualtotal"></span></h4>
                                    <hr/>
                                </div>
                            </div>
                            <div class="row">
                                <div class='col-md-2 col-sm-2 col-xs-12'>
                                    <div class='row'>
                                        <div class="col-md-2">
                                            <form method="POST" action='<?php echo base_url();?>sh_controller/shadow_invoices'>

                                                <input type="hidden" name="calendar_date" value="<?php echo $previousdate ?>"/>
                                                <button type="submit" class="btn btn-sm  btn-pink waves-effect waves-light m-t-0 m-b-30 "> <i class="fa fa-arrow-left"></i> </button>
                                            </form>
                                        </div>
                                        <div class="col-md-8">
                                             <input class="form-control form-inline  m-t-0 m-b-20" id="selected_date" value ="<?php echo $selecteddate;?>"/>   
                                        </div>
                                        <div class='col-md-2' style='text-align:left;' >
                                             <form method="POST" action='<?php echo base_url();?>sh_controller/shadow_invoices'>     
                                                 <input type="hidden" name="calendar_date" value="<?php echo $nextdate ?>"  />    
                                                <button type= "submit" class="btn btn-sm  btn-pink waves-effect waves-light m-t-0 m-b-30"> <i class="fa fa-arrow-right"></i> </button>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            </div>
                               
                                             
                            <table id="tblinvoice" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
                                <thead>
                                    <tr>
                                        <th>Day Seq.</th>
                                        
                                        <th class="hidden">Action</th>
                                        
                                        <th>Invoice #</th>
                                        <th>Invoice Date</th>
                                        <th>Visit Date</th>
                                        <th>Customer</th>                                        
                                        <th>Customer Cell</th>                                       
                                       
                                        <th>Balance</th>
                                        <th>check</th>
                                        <th>Paid Amount</th>
                                        <th>Advance Adjusted</th>
                                        <th>Sub Total</th>
                                        <th>Other Charges</th>
                                        <th>Discount</th>
                                        
                                        <th>Taxes</th>
                                        
                                        <th>Total Payable</th>
                                        <th>CC Charge</th>
                                        <th>Payment Mode</th>
                                        <th>Type</th>
                                        <th>Recovery</th>
                                        <th>Instrument</th>                                        
                                        <th>Discount Remarks</th>
                                        <th>Status</th>
                                        
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php 
                                        $balance = 0;
                                        $netamount = 0;
                                        $grossamount = 0;
                                        $subtotal = 0;
                                        $discount = 0;
                                        $taxes = 0;
                                        $paidamount = 0;
                                        $cc_charge=0;
                                        $totalshadow=0;
                                        $totalshadowservice=0;
                                        $totalshadowretail=0;
                                        
                                        $actualtotalservice=0;
                                        $actualtotalretail=0;
                                        $actualtotal=0;
                                        
                                        $advance=0;
                                    ?>
                                    <?php foreach ($invoices as $invoice){?>
                                    
                                    <tr class="<?php if($invoice['invoice_status']!=='valid'){echo 'strikethrough';}  if($invoice['invoice_type']=='sale'){echo "sale";}?>">
                                        <td><?php echo $invoice['invoice_seq']; ?></td>
                                        
                                        <td class="hidden">
                                            <form action ="<?php echo base_url();?>sh_controller/createshadow" method="POST">
                                                <input type="hidden" name="csrf_test_name" id="shadow_csrf_<?php echo $invoice['id_invoice'];?>" value=""/>
                                                <input type="hidden" id="invoiceidshadow_<?php echo $invoice['id_invoice'];?>" name="invoiceid" value="<?php echo $invoice['id_invoice'];?>">
                                                <input type="hidden" id="invoicedateshadow_<?php echo $invoice['id_invoice'];?>" name="selecteddate" value="<?php echo $selecteddate;?>">
                                                <?php if($invoice['invoice_seq'] ==0){?>
                                                    <button onclick="$('#shadow_csrf_<?php echo $invoice['id_invoice'];?>').val($('#cook').val());" type = submit name="btnshadow" class="btn btn-sm btn-icon waves-effect waves-light btn-danger m-b-5"> <i class="fa fa-copy"></i> Shadow </button>
                                                <?php } ?>
                                            </form>
                                            <form action ="<?php echo base_url();?>sh_controller/removeshadow" method="POST">
                                                <input type="hidden" name="csrf_test_name" id="removeshadow_csrf_<?php echo $invoice['id_invoice'];?>" value=""/>
                                                <input type="hidden" id="invoiceidremove_<?php echo $invoice['id_invoice'];?>" name="invoiceid" value="<?php echo $invoice['id_invoice'];?>">
                                                <input type="hidden" id="invoicedateremoveshadow_<?php echo $invoice['id_invoice'];?>" name="selecteddate" value="<?php echo $selecteddate;?>">
                                                <?php if($invoice['invoice_seq'] > 0 && $invoice['payment_mode'] !== 'Card'){?>
                                                    <button onclick="$('#removeshadow_csrf_<?php echo $invoice['id_invoice'];?>').val($('#cook').val());" type="submit" name="btnremoveshadow" class="btn btn-sm btn-warning waves-effect waves-light m-b-5"> <i class="fa fa-rocket m-r-5"></i> <span>Remove Shadow</span> </button>
                                                <?php } ?>
                                            </form>
                                            
                                            <?php
                                            $visit_order_id = $invoice['visit_id'];
                                            $flag = '';
                                            if($invoice['invoice_type'] === "service"){
                                                $flag = 'service';
                                            } else{
                                                $flag = 'retail';
                                            }
                                            ?>
                                            
                                        </td>
                                        
                                        <?php if($invoice['invoice_type']=='service'){ ?>
                                        <td><a href="<?php echo base_url().'existinginvoice/'.$invoice['id_invoice']; ?>" target="_blank"><?php echo $invoice['invoice_number']; ?></a></td>
                                        <?php } else { ?>
                                        <td><a href="<?php echo base_url().'existingorderinvoice/'.$invoice['id_invoice']; ?>" target="_blank"><?php echo $invoice['invoice_number']; ?></a></td>
                                        <?php } ?>
                                        <td><?php echo $invoice['invoice_date']; ?></td>
                                        <td><?php echo $invoice['visit_time']; ?></td>
                                        <td class="text-custom"><?php echo $invoice['customer_name']; ?></td>
                                        <td>
                                            <?php 
                                                if( $this->session->userdata('hide_cell') && $this->session->userdata('hide_cell') == "No"){
                                                    echo $invoice['customer_cell']; 
                                                } 
                                            ?>
                                        </td>
                                        
                                        
                                        <td class="text-danger"><?php echo $invoice['balance']; if($invoice['invoice_status']=='valid'){$balance+=$invoice['balance'];}?></td>
                                        <td><input style="height: 20px; width: 20px;"  onclick="checkagain('<?php echo $invoice['id_invoice'];?>');" <?php if($invoice['invoice_seq'] > 0){echo "checked='checked'";}?> invoicetype="<?php echo $invoice['invoice_type'];?>" selecteddate="<?php echo $selecteddate;?>" invoice_id="<?php echo $invoice['id_invoice'];?>" visit_id="<?php echo $invoice['visit_id'];?>" type="checkbox" name="mark_<?php echo $invoice['id_invoice'];?>" id="mark_<?php echo $invoice['id_invoice'];?>"></td>
                                        <td><?php echo $invoice['paid_amount']; if($invoice['invoice_status']=='valid'){$paidamount += $invoice['paid_amount'];}?></td>
                                        <td><?php echo $invoice['advance_amount']; $advance += $invoice['advance_amount'];?></td>
                                        <td><?php echo $invoice['sub_total']; if($invoice['invoice_status']=='valid'){$subtotal += $invoice['sub_total'];}?></td>
                                        <td><?php echo $invoice['other_charges']; ?></td>
                                        <td><?php echo $invoice['discount']; if($invoice['invoice_status']=='valid'){$discount += $invoice['discount'];}?></td>
                                        
                                        <td><?php echo $invoice['tax_total']; if($invoice['invoice_status']=='valid'){$taxes += $invoice['tax_total'];}?></td>
                                        
                                        <td><?php echo $invoice['total_payable']; if($invoice['invoice_status']=='valid' && $invoice['invoice_seq']>0){$totalshadow += $invoice['total_payable'];} $actualtotal+=$invoice['total_payable'];?></td>
                                        <?php if($flag=='service' && $invoice['invoice_seq']>0){$actualtotalservice=$actualtotalservice+  $invoice['total_payable'];    $totalshadowservice=$totalshadowservice+  $invoice['total_payable'];} elseif($flag=='service' && $invoice['invoice_seq']==0) { $actualtotalservice=$actualtotalservice+  $invoice['total_payable']; } 
                                        
                                        if($flag=='retail' && $invoice['invoice_seq']>0){$actualtotalretail=$actualtotalretail+ $invoice['total_payable']; $totalshadowretail=$totalshadowretail+ $invoice['total_payable'];} elseif($flag=='retail' && $invoice['invoice_seq']==0) { $actualtotalretail=$actualtotalretail+ $invoice['total_payable']; }?>
                                        <td><?php echo $invoice['cc_charge']; if($invoice['invoice_status']=='valid'){$cc_charge += $invoice['cc_charge'];}?></td>
                                        <td><?php echo $invoice['payment_mode']; ?></td>
                                        <td><?php echo $invoice['invoice_type']; ?></td>
                                        <td><?php echo $invoice['reference_invoice_number'] !== "" ? "<label class='label label-info'>Recovery</label>" : ""; ?></td>
                                        <td><?php echo $invoice['instrument_number']; ?></td>
                                        
                                        <td><?php echo $invoice['discount_remarks']; ?></td>
                                        <td><?php echo $invoice['invoice_status']; ?></td>
                                        
                                    </tr>
                                    
                                    <?php } ?>
                                </tbody>
                                
                                <tfoot>
                                    <tr>
                                        <th></th>
                                        <th class="hidden"></th>
                                        <th></th>
                                        <th></th>

                                        <th></th>
                                        <th></th>
                                        <th><?php //echo $subtotal ? $subtotal : 0; ?></th>
                                        <th><?php //echo $discount ? $discount : 0; ?></th>
                                        <th class="text-right"><strong>Total:</strong></th>
                                        <th class="text-right"><?php echo $paidamount ? $paidamount : 0; ?></th>
                                        <th class="text-right"><?php echo $advance ? $advance : 0; ?></th>


                                        <th ><?php //echo $grossamount ? $grossamount : 0; ?></th>
                                        <th><?php //echo $taxes ? $taxes : 0; ?></th>
                                        <th><?php //echo $netamount ? $netamount : 0; ?></th>
                                        <th class="text-right"><?php echo $taxes ? $taxes : 0; ?></th>
                                        <th class="text-right"><?php echo $actualtotal ? $actualtotal : 0; ?></th> 
                                        
                                        <th></th>
                                        <th></th>
                                        <th></th>
                                        <th></th>
                                        <th></th>
                                        <th></th>
                                        <th></th>

                                    </tr>
                                </tfoot>
                            </table>
                            <hr/>
                            <div class="row m-b-20">
                                <div class="col-lg-12" style="text-align: center;">
                                    <button onclick="resequence_day();" type="button" class="btn btn-lg btn-pink">ReSequence all marked invoices</button>
                                </div>
                            </div>
               
                    <div class="row">
                        <div class='col-md-6 col-xs-12'>
                            <h5 class='text-primary'>Shadowed Services: <span id="totalshadowservice1"></span></h5>
                            <h5 class='text-primary'>Shadowed Retail: <span id="totalshadowretail1"></span></h5>
                            <hr/>
                            <h4 class='text-primary' style='font-weight:bold'>Total Shadowed: <span id="totalshadowed1"></span></h4>
                            <hr/>
                        </div>
                        <div class='col-md-6 col-xs-12'>
                            <h5 class='text-success'>Actual Services: <span id="actualtotalservice1"></span></h5>
                            <h5 class='text-success'>Actual Retail: <span id="actualtotalretail1"></span></h5>
                            <hr/>
                            <h4 class='text-success' style='font-weight:bold'>Actual Total: <span id="actualtotal1"></span></h4>
                            <hr/>
                        </div>
                    </div>
                </div>

            </div>
        </div><!-- end row -->
    </div>
    <!-- end container -->
                
        </div>
    <script type="text/javascript">
        $(document).ready(function() {
            
             $('#datepicker-autoclose').datepicker({
                autoclose: true,
                todayHighlight: true,
                format: 'yyyy-mm-dd'
            });
            
            $('#tblinvoice').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", footer: true}, {
                            extend: "excel",
                            className: "btn-sm btn-warning btn-trans"
                        }, {extend: "pdf", className: "btn-sm btn-purple btn-trans", footer: true}
                    , {extend: "print", className: "btn-sm btn-primary btn-trans", footer: true}],
                    responsive: !0,
                    order:[[ 0, "desc" ]]
                    //"scrollX": true,
            });

            

        });
        
        $( window ).load(function() {
            $("#actualtotalservice").html("<?php echo $actualtotalservice; ?>"); 
            $("#actualtotalretail").html("<?php echo $actualtotalretail; ?>"); 
            
             $("#totalshadowservice").html("<?php echo $totalshadowservice; ?>"); 
            $("#totalshadowretail").html("<?php echo $totalshadowretail; ?>"); 
            
            
            
           $("#totalshadowed").html("<?php echo $totalshadow; ?>");
           $("#actualtotal").html("<?php echo $actualtotal; ?>");
           
           
           
           $("#actualtotalservice1").html("<?php echo $actualtotalservice; ?>"); 
            $("#actualtotalretail1").html("<?php echo $actualtotalretail; ?>"); 
            
             $("#totalshadowservice1").html("<?php echo $totalshadowservice; ?>"); 
            $("#totalshadowretail1").html("<?php echo $totalshadowretail; ?>"); 
            
            
            
           $("#totalshadowed1").html("<?php echo $totalshadow; ?>");
           $("#actualtotal1").html("<?php echo $actualtotal; ?>");
          });
        //TableManageButtons.init();
        
        function cancelinvoice(invoiceid, visit_order_id, flag){
            //Warning Message

            swal({
                title: "Are you sure?",
                text: "Give Reason For Cancelling This Invoice:",
                type: "input",
                showCancelButton: true,
                closeOnConfirm: false,
                inputPlaceholder: "reason for cancelling"
              }, function (inputValue) {
                if (inputValue === false) return false;
                if (inputValue === "") {
                  swal.showInputError("You need to write something!");
                  return false
                } 
                    $.ajax({
                        type: 'POST',
                        url: 'invoice_controller/cancelinvoice',
                        data: {
                            invoiceid : invoiceid,
                            visit_order_id : visit_order_id,
                            flag : flag,
                            reason : inputValue
                        },
                        success: function(data) {
                            var result = data.split("|");
                            if (result[0] === "success") {
                                swal("Deleted!", "Your invoice has been removed.", "success");
                                location.reload();
                            } else {
                                swal("Info!", "Invoice was not removed!. "+result[1] , "error");
                            }
                        } 
                    });
                
              });
            
            
            
        }

        function openinvoice(invoiceid){
            $.ajax({
                    type: 'POST',
                    url: 'invoice_controller/openinvoice',
                    data: {invoiceid: invoiceid},
                    success: function(data) {
                        
                    }
                });
        }
        
        
        function checkagain(id){
            //console.log(id);
            
            var temp = '#mark_'+id;
            
            //console.log ($(temp).prop('checked'));
            
            var totalshadowservice=0;
            var totalshadowretail=0;
            var totalshadowed=0;
            
            $('#tblinvoice tr').each(function(row, tr){
               
                if($(tr).find('td:eq(8) input').prop('checked')=== true ){
                    //console.log(parseFloat($(tr).find('td:eq(9)').text()));
                    if($(tr).find('td:eq(18)').text()=='service'){
                        totalshadowservice=totalshadowservice + parseFloat($(tr).find('td:eq(9)').text());
                    } else if($(tr).find('td:eq(18)').text()=='sale'){
                        totalshadowretail=totalshadowretail + parseFloat($(tr).find('td:eq(9)').text());
                    }
                }                
            });
            
            $("#totalshadowservice").html(totalshadowservice); 
            $("#totalshadowretail").html(totalshadowretail); 
            $("#totalshadowed").html(totalshadowservice + totalshadowretail); 
            
            $("#totalshadowservice1").html(totalshadowservice); 
            $("#totalshadowretail1").html(totalshadowretail); 
            $("#totalshadowed1").html(totalshadowservice + totalshadowretail); 
            
            
        }
        
        function resequence_day(){
            var TableData = invoiceOTblValues();
            
            $.ajax({
            type: "POST",
            url: "<?php echo base_url() . 'sh_controller/resequence_day'; ?>",
            data: {                
                tabledata:TableData,
                selecteddate: '<?php echo $selecteddate;?>'
            },
            success: function(data) {
                //console.log(data);
                if(data=='success'){
                    location.reload();
                }
                
                }
            });
            
        }
        
        function invoiceOTblValues(){
            var TableData = new Array();
            $('#tblinvoice tr').each(function(row, tr) {
                TableData[row] = {
                    "seq": $(tr).find('td:eq(0)').text()
                    , "check": $(tr).find('td:eq(8) input').prop('checked')
                    , "invoice_id": $(tr).find('td:eq(8) input').attr('invoice_id')
                    , "visit_id": $(tr).find('td:eq(8) input').attr('visit_id')
                    , "invoice_date": $(tr).find('td:eq(3)').text()
                    , "customer": $(tr).find('td:eq(5)').text()

                }
            });
           // TableData.shift();  // first row will be empty - so remove
            TableData.pop();  // last row will be empty - so remove
            TableData.pop();  // last row will be empty - so remove
            return TableData;
        }
    </script>
