<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>


<style>
th { white-space: nowrap; }

button.dt-button, div.dt-button, a.dt-button, input.dt-button {
    border: 0px solid rgba(0, 0, 0, 0.3) !important;
    border-radius: 2px !important;
    color: white !important;
    background: #428bca !important;
    padding: 0.3em 1em !important;
}
</style>
 <link href="<?= $assets ?>styles/jquery.dataTables.css" rel="stylesheet"/>
 
 <link href="https://cdn.datatables.net/1.11.5/css/jquery.dataTables.min.css" rel="stylesheet">
<link href="https://cdn.datatables.net/buttons/2.2.2/css/buttons.dataTables.min.css" rel="stylesheet">
 
 <script type="text/javascript" src="<?= $assets ?>js/jquery.js"></script>
   <script type="text/javascript" src="<?= $assets ?>js/jquery.dataTables.js"></script>

<div class="box">
    <div class="box-header">
        <div class="box-header">
        <h2 class="blue"><i class="fa-fw fa fa-plus"></i>Customer Statement</h2>
        
       
    </div>
        
    </div>
    <div class="box-content">
        <div class="row">
            <div class="col-lg-12">
                <!--<p class="introtext"><?= lang('list_results'); ?></p>-->
                

					<div>
				  
				  <button id="btnExport" class="dt-button buttons-excel buttons-html5"><span>Excel</span></button>
				  <button id="btnExportPdf" class="dt-button buttons-excel buttons-html5" onclick="exportpdf()"><span>Pdf</span></button>
				  
				  <!--<input type="button" id="btnExport" value="Export" onclick="Export()" />-->
				</div>
				
                <div class="table-responsive"> 
                   <table id="example" class="table table-bordered table-hover table-striped dataTable example" cellspacing="0" width="100%">
                        <thead>
                        <tr class="primary">
                            <!--<th style="min-width:30px; width: 30px; text-align: center;">
                                <input class="checkbox checkth" type="checkbox" name="check"/>
                            </th>-->
                            
                            <th><?= lang("Payment Type") ?></th>
                            <th><?= lang("Bill No") ?></th>
                             <th><?= lang("Voucher No") ?></th>
							 <th><?= lang("Date") ?></th>
							 <th>Customer</th> 
							  <th>Product Category</th>
							<!--<th><?= lang("deposit/withdrawal") ?></th>-->
                            <!--<th><?= lang("date") ?></th>-->
                            <th><?= lang("Debit") ?></th>
                            <th><?= lang("Credit") ?></th>
                            <th>Balance</th>
                            
                             
                            <!--<th><?= lang("reference_no") ?></th>-->
							<!--<th><?= lang("actions") ?></th>-->
                           
                        </tr>
                        </thead>
                        
 <!--                        <tfoot align="right">-->
	<!--	<tr>  <th colspan="7" style="text-align:right"></th><th></th><th style="text-align:right"></th><th colspan="2" style="text-align:right"></th></tr>-->
	<!--</tfoot>   -->
                        <tbody>
						<?php 
						if($trans_sale != ''){?>
						<?php $totol_balance_sum =0;
						$total_balance =0;
						$sale_totl =0;
						$value = 0;
						foreach($trans_sale as $row):?>
						<?php if(!empty($row->customer_id)){ ?>
                        <tr>
						
						    <td style="text-align:center;">Sales</td>

                            <td style="text-align:center;"><?=$row->reference_no; ?></td>
                            
                             <td style="text-align:center;">-</td>
                            
                            <td style="text-align:center;"><?=$row->date; ?></td>
                            
                            <?php $company = $this->db->get_where('companies' , array('id' => $row->customer_id))->row()->company;?>
							<td style="text-align:center;"><?=!empty($company)?$company:'';?></td>
                            
                            <?php $categories = $this->db->get_where('categories' , array('id' => $row->category_id))->row()->name;?>
							<td style="text-align:center;"><?=!empty($categories)?$categories:'';?></td>
							

                            <!--<td style="text-align:center;">-</td>-->

                            <td style="text-align:center;"><?=$row->grand_total;?></td> 
                            
                            <td style="text-align:center;">-</td>
                            
                           
						   <?php
						   $total_bal =$balamce= 0;
						   $grnd_amnt=$row->grand_total;
						   $total_bal+= $grnd_amnt;
						   $totol_balance_sum += $total_bal; 
						   ?>
						   <td style="text-align:center;"><?=$totol_balance_sum;?></td>
						   
                        </tr> 
						 <?php  } ?>
						<?php endforeach;
						}
						if(!empty($totol_balance_sum)){
						    	$sale_totl = $totol_balance_sum;
						}
						else{
						   	$sale_totl = 0;
						}
					
					
						if(!empty($row)){
						    	$sale_ref=$row->reference_no;
						}
						else{
						   	$sale_ref=0; 
						}
					
				// 		print_r($sale_totl);
						?>
                        </tbody>
                        
                      
                         <tbody>
						<?php  
						if($deposit_sale != ''){?>
						<?php $total_bal =0;
						foreach($deposit_sale as $row):?>
						<?php if(!empty($row->company_id)){ ?>
                        <tr>
						
						
							
                            <?php if($row->type =="income")
                            { ?> 

                                <td style="text-align:center;">Payment</td>
                                <?php 
                                
                            } 
                            
                                else { ?>
                                 <td style="text-align:center;">Previous balance</td>  <?php 
                                }?>

                            <?php if($row->type == "previous balance")
                            { ?>
                                <td style="text-align:center;">-</td> <?php 
                            } else 
                            { ?>
                             <td style="text-align:center;"><?= $sale_ref; ?></td><?php 
                             } ?>
                            
                            <?php if($row->type == "previous balance"){ ?>
                            <td style="text-align:center;"><?=$row->voucher; ?></td> <?php 
                            } else { ?> <td style="text-align:center;"><?=$row->voucher; ?></td> <?php } ?>
                            
                            <?php if($row->type == "previous balance"){ ?>
                            <td style="text-align:center;"><?=$row->payed_date; ?></td>
                            
                            <?php } elseif($row->type == "income"){ ?>
                            <td style="text-align:center;"><?=$row->payed_date; ?></td><?php } ?>
                           
                            
                            <?php $company = $this->db->get_where('companies' , array('id' => $row->company_id))->row()->company;?>
							<td style="text-align:center;"><?=!empty($company)?$company:'';?></td>
                            
                            <?php $categories = $this->db->get_where('categories' , array('id' => $row->category_id))->row()->name;?>
							<td style="text-align:center;"><?=!empty($categories)?$categories:'';?></td>
							
						

                            <?php if($row->type == "previous balance"){ ?>
                            <td style="text-align:center;">
                            <?=(abs($row->amount));?>
                            </td>
                            <?php 
                              $value= (abs($row->amount)); 
                            } 
                            
                            elseif($row->type == "income"){ ?>
                            <td style="text-align:center;">-</td> <?php } ?>
                           
                            
                            <?php if($row->type == "previous balance"){ ?>
                            <td style="text-align:center;">-</td><?php } 
                            elseif($row->type == "income") { ?>
                            <td style="text-align:center;"><?=$this->sma->formatQuantity($row->amount);?></td><?php } else { ?>
                            <td style="text-align:center;">-</td><?php } ?>
                            
                            
                            
                            <?php if($row->type == "income"){  ?>
                            <!--<td style="text-align:center;"><?=$this->sma->formatQuantity($total_amnt);?></td>-->
                            <td style="text-align:center;">
                                <?php 
                            
						   
						   //$total_bal = 0;
						   $grnd_amnt=(abs($row->amount));
						   $total_bal = $value + $sale_totl;
						   //$total_balan = $total_bal - $grnd_amnt;
						    if(!empty($grnd_amnt) && empty($total_balan) ){
						       $total_balan = $total_bal - $grnd_amnt;
						      //  echo $total_balan;
						      echo  $this->sma->formatQuantity($total_balan);
						   }
						   elseif(!empty($grnd_amnt) && !empty($total_balan) ){
						       $total_balan = $total_balan - $grnd_amnt;
						      // echo $total_balan;
						      echo  $this->sma->formatQuantity($total_balan);
						   }
						  // $balamce =$this->sma->formatQuantity($total_balan);
						  
                            ?>
                               
                                </td>
                            <?php } 
                           
                            elseif($row->type == "previous balance"){ 
                            $prev_amnt = (abs($row->amount));
                            
                            ?>
                            <!--<td style="text-align:center;"><?=$this->sma->formatQuantity($tot_sal);?></td>-->
                            <td style="text-align:center;">
                                <?php
                                $prev_amnt=(abs($row->amount));
                                $tot_amnt= $prev_amnt + $sale_totl;
                                echo $tot_amnt;
                                ?>
                                </td>
                            <?php   
                            } ?>
							
						   
                        </tr>
						 <?php  } ?>
						<?php endforeach;
						}
						?>
                        </tbody>

                         
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.2.2/js/dataTables.buttons.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>
<script src="https://cdn.datatables.net/buttons/2.2.2/js/buttons.html5.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.2.2/js/buttons.print.min.js"></script>
 

<script>
  $(document).ready(function() {
    $('#example').DataTable( {
      "language": {
        "emptyTable":     "No Sale Data Available"
    }  
    // bInfo:false
    } );
} );



   

</script>


<!-- EXPORT TO EXCEL STARTS HERE -->
<script src="https://cdn.jsdelivr.net/gh/linways/table-to-excel@v1.0.4/dist/tableToExcel.js"></script>
<script>
  $(document).ready(function(){
    $("#btnExport").click(function() {
        let table = document.getElementsByTagName("table");
        TableToExcel.convert(table[1], { // html code may contain multiple tables so here we are refering to 1st table tag
           name: `Customer Statement.xlsx`, // fileName you could use any name
           sheet: {
              name: 'Sheet 1' // sheetName
           }
        });
    });
    
});
</script>

<!-- EXPORT TO EXCEL ENDS HERE -->

<!-- EXPORT TO PDF STARTS HERE -->

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.22/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
    <script type="text/javascript">
        function exportpdf() {
            html2canvas(document.getElementById('example'), {
                onrendered: function (canvas) {
                    var data = canvas.toDataURL();
                    var docDefinition = {
                        content: [{
                            image: data,
                            width: 500
                        }]
                    };
                    pdfMake.createPdf(docDefinition).download("Customer Statement.pdf");
                }
            });
        }
    </script>
    
    

<!-- EXPORT TO EXCEL ENDS HERE -->
