<!--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="pull-right">
  <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>
 <!--<center>
 <span class="btn btn-danger"><h1>UNDER DEVELOPMENT</h1></span>
 </center>-->
  <!--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
        }
        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>Sale 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();
$Pos_Model             = new Pos_Model();


foreach ($customer_details as $row) {
    $tr_html = "";
    $rw1 ="";
    $rw2 ="";
    $rw3 = "";
    //get customer pending and partial invoice    
    $invocie_data = $Pos_Model->get_sale_info_by_customer_id($row->cus_id, $srh_from_date, $srh_to_date);
    /*echo "<pre>";
    print_r($invocie_data);
    echo "</pre>";
    exit;*/
    if (count($invocie_data)) {
        
        $balance_sub_tot      = 0;
        $balance_sub_tot_each = 0;
        foreach ($invocie_data as $inv) {
            $row_balance = 0;
            $row_balance += $inv->sale_total;
            /*sale paid*/
            $row_balance    -= $Pos_Model->get_total_paid_by_sale_id($inv->sale_id);
            /*return*/
            $total_return         = 0;
            $total_return_pymnt   = 0;
            $return_sales_details = $Pos_Model->get_return_sale_info_sale_id($inv->sale_id);
            foreach ($return_sales_details as $row2) {
                $row_balance -= $row2->sl_rtn_total;
                $row_balance += $Pos_Model->get_total_return_paid_by_sale_id($row2->sl_rtn_id);
            }
            
            $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';
            //end check date range
            if($row_balance){
                $rw2.= "<tr>
                            <td style=\"text-align:right; width:40px; vertical-align:middle;\"></td>
                            <td></td><td></td><td>".date('Y-m-d h:i A', strtotime($inv->sale_datetime))."
                            </td><td>".$inv->sale_reference_no."</td><td align=\"right\">".number_format($row_balance, 2, '.', ',')."</td><td>".$date_age."</td>
                </tr>";
                $balance_sub_tot += $row_balance;
            }
        }
        if($balance_sub_tot){
            $balance_sub_tot_each += $balance_sub_tot;
            $tmpcount++;
            $rw1 = "<tr>
                    <td style=\"text-align:right; width:40px; vertical-align:middle;\">".sprintf("%04d", $tmpcount)."</td>
                    <td>".$row->cus_id."</td><td>".$row->cus_name."</td>
                    <td>Tel:".$row->cus_phone."</td><td></td><td></td><td></td>
                </tr>";
            
            $rw3 = "<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;\">".number_format($balance_sub_tot_each, 2, '.', ',')."</div></td>
                        <td></td>
                    </tr>";
                    
                    echo $rw1.$rw2.$rw3;
                    $balance_grand_tot += $balance_sub_tot;
        } //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="https://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>