<!--onLoad="window.print()"-->

<body id="tableWrap">
<?php $this->load->view("common/header"); ?>
<style type="text/css">
.report_view_th{
	color:#000 !important;

	font-size:12px;	

}.table-responsive td{
	font-size:11px;
	background-color:#fff !important;	
}
h4{
	font-size:13px;
}
body{
	background-color:#fff !important;
}
table{
	border:1 !important;
}
td{
	border:1 !important;
}
th{
	border:1 !important;
}
</style>
<div style="top:2px;" class="panel-tools open">
  <button id="excel" data-toggle="dropdown" class="btn btn-blue dropdown-toggle"> <i title="Copy to use file-excel-o" aria-hidden="true" class="fa fa-fw"></i> </button>
  <button data-toggle="dropdown" class="btn btn-blue dropdown-toggle" onClick="window.print()"> <i class="fa fa-print"></i> </button>
</div>
<div class="row">
  <div class="col-xs-12 text-center">
    <?php $this->load->view("common/report_header.php"); ?>
  </div>
  <!--col-xs-12-->
</div>
<!--row-->
<div style="margin-bottom:15px;" class="row">
  <div class="col-xs-12 pull-left">
    <h4 style="margin-top:10px;">Report : Customer Receivable</h4>
    <?php if ($srh_from_date){ ?>
    <p>Invoice From Date: <?php echo date('Y-m-d', strtotime($srh_from_date)); ?></p>
    <?php }?>
    <?php if ($srh_to_date){ ?>
    <p>Invoice To Date: <?php echo date('Y-m-d', strtotime($srh_to_date)); ?></p>
    <?php }?>
  </div>
</div>
<div class="table-responsive">
  <table class="table table-bordered table-hover table-striped print-table order-table">
    <thead>
      <tr class="report_view_th">
        <th class="col-sm-1 text-center">No</th>
        <th>ID</th>
        <th>Customer Name</th>
        <th>Invoice Date</th>
        <th>Invoice No.</th>
        <th>Balance</th>
        <th>Age</th>
      </tr>
    </thead>
    <tbody>
      <?php 
// print_r($customer_details);
 $tmpcount=0;
 $tot_grand_total=0;
 $tot_total_paid_amount=0;
 $tot_net_amount=0;
 $balance_grand_tot=0;
 $refcusmod=new Customer_Model();
 $refsalemod=new Sales_Model();
  foreach ($customer_details as $row){
	 //get customer pending and partial invoice 	
	 $invocie_data=$refsalemod->get_all_sales_bu_cus_id($row->cus_id);
	 if(count($invocie_data)){ 
		$balance_sub_tot=0;$balance_sub_tot_each=0;
		foreach ($invocie_data as $inv){
			$total_paid_amount=$refsalemod->get_total_paid_by_sale_id($inv->sale_id);
			$balance_h=0;
			$balance_h=$inv->sale_total-$total_paid_amount;
			$due_date='';
			$sales_term_days=0;
			$due_date=date('Y-m-d', strtotime($inv->sale_datetime));
			$due_date_rpt='';
			$due_date_rpt=date('Y-m-d', strtotime($inv->sale_datetime . " +$sales_term_days day"));
			$des_h='';
			$paymentDate = $due_date;
			$paymentDate=date('Y-m-d', strtotime($paymentDate));;
			$contractDateBegin = date('Y-m-d', strtotime($srh_from_date));
			$contractDateEnd = date('Y-m-d', strtotime($srh_to_date));
			if (($paymentDate > $contractDateBegin) && ($paymentDate < $contractDateEnd)){
			  $des_h= "is between";
			}
			else {
			  $des_h= "";  
			}
			if($balance_h){
				if($des_h){
					$balance_sub_tot_each+=$balance_h;
				}
			}
		 //end check date range
		}
	//if($balance_h){
	if($balance_sub_tot_each){
			$tmpcount++;	
	
	 ?>
		  <tr>
			<td style="text-align:right; width:40px; vertical-align:middle;"><?php echo sprintf("%04d",$tmpcount); ?></td>
			<td><?php echo $row->cus_id ?></td>
			<td><?php echo $row->cus_name ?></td>
			<td>Tel: <?php echo $row->cus_phone ?></td>
			<td><?php //echo count($invocie_data) ?></td>
			<td><?php //echo number_format($total_paid_amount, 2, '.', ','); ?></td>
			<td></td>
		  </tr>
		  <?php
	//get customer pending and partial invoice
	
	$inv_count=0; 
	foreach ($invocie_data as $inv){
		$inv_count++;
		//get paid amount
		$total_paid_amount=$refsalemod->get_total_paid_by_sale_id($inv->sale_id);
		$balance=0;
		$balance=$inv->sale_total-$total_paid_amount;
		$sales_term_days=0;
		$due_date='';
		//$due_date=date('Y-m-d', strtotime($inv->sale_datetime . " +$sales_term_days day"));
		$due_date=date('Y-m-d', strtotime($inv->sale_datetime));
		$date_age='';
		$diff= abs(strtotime(date('Y-m-d')) - strtotime($inv->sale_datetime));
		$years = floor($diff / (365*60*60*24));
		$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
		$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
		$startTimeStamp = strtotime(date('Y-m-d', strtotime($inv->sale_datetime)));
		$endTimeStamp = strtotime(date("Y-m-d"));
		$timeDiff = abs($endTimeStamp - $startTimeStamp);
		$numberDays = $timeDiff/86400;  // 86400 seconds in one day
		$numberDays = intval($numberDays);
		$date_age=$numberDays. ' Days';
		if($balance){
			$des='';$paymentDate = $due_date;$paymentDate=date('Y-m-d', strtotime($paymentDate));
			$contractDateBegin = date('Y-m-d', strtotime($srh_from_date));
			$contractDateEnd = date('Y-m-d', strtotime($srh_to_date));
			if (($paymentDate > $contractDateBegin) && ($paymentDate < $contractDateEnd)){
				$des= "is between";
			}else{
				$des= "";  
			}
			if($des){
				$balance_sub_tot+=$balance;
				$balance_grand_tot+=$balance;
			?>
			  <tr>
				<td style="text-align:right; width:40px; vertical-align:middle;"></td>
				<td></td>
				<td></td>
				<td><?php echo date('Y-m-d h:i A', strtotime($inv->sale_datetime)); ?></td>
				<td><?php //echo $des ?>
				  <?php echo $inv->sale_reference_no ?></td>
				<td align="right"><?php echo number_format( $balance, 2, '.', ','); ?></td>
				<td><?php echo $date_age ?></td>
			  </tr>
			  <?php }?>
	  <?php if(count($invocie_data)==$inv_count){
		  		//if($balance_sub_tot)
							//{
							?>
								  <tr>
									<td style="text-align:right; width:40px; vertical-align:middle;"></td>
									<td></td>
									<td></td>
									<td></td>
									<td></td>
									<td align="right"><div style="border-top:1px solid gray; border-bottom:4px double gray;"> <?php echo number_format( $balance_sub_tot, 2, '.', ','); ?>
										<?php //echo $balance_sub_tot; //echo $row->sale_reference_no ?>
									  </div></td>
									<td></td>
								  </tr>
								  <?php  
							//}
						}//if (count($invocie_data)==$inv_count)
					}//if($balance)
				} //foreach ($invocie_data as $inv)
			}//if($balance_sub_tot_each) 
			//} 
		}//if(count($invocie_data))  
	}//  foreach ($customer_details as $row)
	?>
      <tr>
        <td style="text-align:right; width:40px; vertical-align:middle;"></td>
        <td style="vertical-align:middle;"></td>
        <td></td>
        <td></td>
        <td></td>
        <th class="text-right"><div style="border-top:1px solid gray; border-bottom:4px double gray;"> <?php echo number_format( $balance_grand_tot, 2, '.', ','); ?> </div>
        </th>
        <th class="text-right"> <?php //echo number_format($tot_net_amount, 2, '.', ','); ?></th>
      </tr>
    </tbody>
    <tfoot>
    </tfoot>
  </table>
</div>
<div class="row">
  <div class="col-xs-12"> </div>
  <div class="col-xs-5 pull-right">
    <div class="well-sm">
      <p> Created by: <?php echo $this->session->userdata('ss_user_first_name'); ?> (<?php echo $this->session->userdata('ss_user_group_name'); ?>)</p>
      <p> Date: <?php echo display_date_time_format(date("Y-m-d H:i:s")); ?> </p>
    </div>
  </div>
</div>

<!--/.col-md-12--> 

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.0.js"></script> 
<script type="text/javascript">

$(function(){

    $('#excel').click(function(){

        var url='data:application/vnd.ms-excel,' + encodeURIComponent($('#tableWrap').html()) 

        location.href=url

        return false

    })

})

</script>
</body>
