<?php
$this->load->view ( $left );
$this->load->view ( $header );
?>
<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-sm-12">
        <h2>Customer Sales</h2>
    </div>
</div>
<div class="wrapper wrapper-content">
    <div class="row text-center animated fadeInRightBig">
        <div class="ibox float-e-margins">
            <div class="ibox-content" style="padding: 15px 20px 15px 20px;">
                <div class="row">
                    <div class="col-lg-4">
                        <label class="col-lg-4 m-t-xs">From</label>
                        <div class="date_calender col-lg-8">
                            <div class="input-group date">
                                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                <input type="text" name="start_date" id="start_date" class="form-control" value="<?php echo (!empty($start_date) ? date('d/m/Y',strtotime(str_replace('/', '-', $start_date))) : date('d/m/Y'));?>">
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-4">
                        <label class="col-lg-4 m-t-xs">To</label>
                        <div class="date_calender col-lg-8">
                            <div class="input-group date">
                                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                <input type="text" name="end_date" id="end_date" class="form-control" value="<?php echo (!empty($end_date) ? date('d/m/Y',strtotime(str_replace('/', '-', $end_date))) : date('d/m/Y'));?>">
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-4"><button type="button" class="btn btn-info generate_report"><i class="fa fa-search"></i> Search</button></div>
                    <!-- <div class="col-lg-2"><button type="button" class="btn btn-info generate_excel"><i class="fa fa-file-excel-o"></i> Export Excel</button></div> -->
                </div>
            </div>
        </div>
        <div class="ibox float-e-margins">
            <div class="ibox-content" style="padding: 15px 20px 15px 20px;">
                <div class="row">
                    <table class="table" id="myTable">
                    	<thead>
                    		<tr>
                    			<th>Name</th>
                    			<th>Invoice Count</th>
                                <th>Sales</th>
                                <th>Sales With Tax</th>
                                <th>Credit</th>
                                <th>Outstanding Balance</th>
                    		</tr>
                    	</thead>
                        <tbody>
                            <?php 
                            if (count($combine_sale) > 0) {
                                $total_invoice = 0;
                                $total_invoice_actual_amount = 0;
                                $total_invoice_amount = 0;
                                $total_credit_amount = 0;
                                $total_outstanding = 0;
                                foreach ($combine_sale as $key_sale => $value_sale) {?>
                                    <tr class="sales_details" data-start="<?php echo !empty($start_date) ? $start_date : '';?>" data-end="<?php echo !empty($end_date) ? $end_date : '';?>" data-customer="<?php echo !empty($value_sale->id) ? $value_sale->id : '';?>">
                                       <td><?php echo !empty($value_sale->name) ? $value_sale->name : '';?></td>
                                       <td><?php echo !empty($value_sale->no_invoice) ? $value_sale->no_invoice : '0';?></td>
                                       <td><?php echo !empty($value_sale->invoice_actual_amount) ? number_format((float)$value_sale->invoice_actual_amount, 2, '.', '')  : '0.00';?></td>
                                       <td><?php echo !empty($value_sale->invoice_amount) ? number_format((float)$value_sale->invoice_amount, 2, '.', '')  : '0.00';?></td>
                                       <td><?php echo !empty($value_sale->credit_amount) ? number_format((float)$value_sale->credit_amount, 2, '.', '') : '0.00';?></td>
                                       <td><?php echo number_format((float)($value_sale->invoice_amount - $value_sale->credit_amount), 2, '.', '');?></td>
                                    </tr>
                            <?php 
                                $total_invoice += $value_sale->no_invoice;
                                $total_invoice_actual_amount += $value_sale->invoice_actual_amount;
                                $total_invoice_amount += $value_sale->invoice_amount;
                                $total_credit_amount += $value_sale->credit_amount;
                                $total_outstanding += ($value_sale->invoice_amount - $value_sale->credit_amount);
                                }?>
                                    <tr style="font-weight: bold;">
                                        <td>Total</td>
                                        <td><?php echo $total_invoice;?></td>
                                        <td><?php echo number_format((float)$total_invoice_actual_amount, 2, '.', '');?></td>
                                        <td><?php echo number_format((float)$total_invoice_amount, 2, '.', '');?></td>
                                        <td><?php echo number_format((float)$total_credit_amount, 2, '.', '');?></td>
                                        <td><?php echo number_format((float)$total_outstanding, 2, '.', '');?></td>
                                    </tr>
                            <?php }?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<?php $this->load->view($footer); ?>
<script type="text/javascript">
	$(document).ready(function(){
       // $('#myTable').DataTable();

        $('.date_calender .input-group.date').datepicker({
            todayBtn: "linked",
            keyboardNavigation: false,
            forceParse: false,
            calendarWeeks: true,
            language: 'en',
            autoclose: true
        });
        
        $(document).on('click','.sales_details',function(){
            var start_date = chage_date_format($('#start_date').val());
            var end_date = chage_date_format($('#end_date').val());
            var customer = $(this).data('customer');
            //window.location.href = '<?php //echo site_url("invoice/invoice_print/'+invoice+'");?>';
            window.open('<?php echo site_url("reports/customer_sale/'+customer+'/'+start_date+'/'+end_date+'");?>','_blank');
        });

        $('.generate_report').on('click',function(){
            var start_date = chage_date_format($('#start_date').val());
            var end_date = chage_date_format($('#end_date').val());
            window.location.href = '<?php echo site_url("reports/customer_sale_combine/'+start_date+'/'+end_date+'");?>';
        });

        function chage_date_format(date_str){
            return date_str.replace('/','-').replace('/','-');
        }
	});
</script>

	<script type="text/javascript" language="javascript" src="//code.jquery.com/jquery-1.12.4.js"></script>
    <script src="//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
    <script src="//cdn.datatables.net/buttons/1.5.1/js/dataTables.buttons.min.js"></script> 	
	<script src="//cdn.datatables.net/buttons/1.5.1/js/buttons.flash.min.js"></script> 
	<script src="//cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script> 
	<script src="//cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/pdfmake.min.js"></script> 
	<script src="//cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/vfs_fonts.js"></script>
	<script src="//cdn.datatables.net/buttons/1.5.1/js/buttons.html5.min.js"></script> 
    <script src="//cdn.datatables.net/buttons/1.5.1/js/buttons.print.min.js"></script>

<script type="text/javascript">
	var jq = jQuery.noConflict();

	jq(document).ready(function() {
	jq('#myTable').DataTable( {
			dom: 'Bfrtip',
			buttons: [
				'copy', 'csv', 'excel', 'pdf', 'print'
			]
		} );
	} );

</script>	