
<!--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 class="">

         

         
            <div class="modal-body">

 <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 }?>

<?php 
$srh_rep_id=$_GET['srh_rep_id'];
if($srh_rep_id){ 
 $ref_id = new User_Model();
  $data['user_details']=$ref_id->get_user_info($srh_rep_id);
 ?>
<p>Sales Rep: <?php  echo $data['user_details']['user_first_name'].' '.$data['user_details']['user_last_name']; ?></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>Due Date</th>
<th>Invoice Amount</th>
<th>Return</th>
<th>Balance</th>
<th>Age</th>

</tr>
</thead>
<tbody>
 <?php 
// print_r($customer_details[0]);
 $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();
 
 //print_r($customer_details);
  foreach ($customer_details as $row)
 {
	$srh_rep_id='';
	 $srh_rep_id=$_GET['srh_rep_id'];
	 //if(isset($srh_rep_id)){
		 
	// }
	 
	 //get customer pending and partial invoice 	
	 $invocie_data=$refsalemod->get_all_sales_bu_cus_id($row->cus_id,$srh_rep_id);
	 
	 //print_r($invocie_data);
	// echo $row->cus_id.":".count($invocie_data) .", ";
	 
	
	if(count($invocie_data)){ 
	
	
	
	$balance_sub_tot=0;
	 $balance_sub_tot_each=0;
foreach ($invocie_data as $inv)
 {
	 
	 
	// echo "rep id:".$_GET['srh_rep_id'];

	 
	 $total_paid_amount=$refsalemod->get_total_paid_by_sale_id($inv->sale_id);
	 $balance_h=0;
	 $balance_h=$inv->sale_total-$total_paid_amount;
	 
	 //check date range
	  $due_date='';
	   $sales_term_days=0;//$inv->sales_term_days;
	  //$due_date=date('Y-m-d', strtotime($inv->sale_datetime . " +$sales_term_days day"));
	  $due_date=date('Y-m-d', strtotime($inv->sale_datetime));
	  $due_date_rpt='';
	  $sales_term_days=0;//$inv->sales_term_days;
	   $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));;
    //echo $paymentDate; // echos today! 
    $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> </td>
<td>&nbsp;</td>
<td>&nbsp;</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;
	 $sale_tot=$inv->sale_total-$total_paid_amount;
	 $sales_term_days=0;//$inv->sales_term_days;
	 $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($inv->sale_datetime);
$endTimeStamp = strtotime(date("Y-m-d"));

$timeDiff = abs($endTimeStamp - $startTimeStamp);

$numberDays = $timeDiff/86400;  // 86400 seconds in one day

// and you might want to convert to integer
$numberDays = intval($numberDays);

/*****/


$date_age=$numberDays. ' Days';

//printf("%d years, %d months, %d days\n", $years, $months, $days);

$ren_mod=new Sales_Return_Model();
	$rtn_amount=$ren_mod->get_total_return_by_sale_id($inv->sale_id);
	$balance=$balance-$rtn_amount;
	

	if($balance){ 
	
	
	
	$des='';
	$paymentDate = $due_date;
    $paymentDate=date('Y-m-d', strtotime($paymentDate));;
    //echo $paymentDate; // echos today! 
    $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', strtotime($inv->sale_datetime)); ?></td>
<td><?php //echo $des ?> <?php echo $inv->sale_reference_no ?> </td>
<td> <?php  echo $due_date_rpt ?> <?php echo ", $sales_term_days"; ?></td>
<td align="right"><?php echo number_format( $sale_tot, 2, '.', ','); ?></td>
<td align="right"><?php echo number_format( $rtn_amount, 2, '.', ','); ?></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></td>
<td align="right">&nbsp; <?php //echo count($invocie_data) ?>, <?php //echo $inv_count ?></td>
<td align="right">&nbsp;</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  } } } } }  } }?>
<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"> <?php //echo number_format($tot_grand_total, 2, '.', ','); ?></th>
<th class="text-right">&nbsp;</th>
<th class="text-right">&nbsp;</th>
<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>

