<?php
 include('function.php');
 
 include('db_con.php');
 //stock_update($connect);
 if (isset($_SESSION['user_id'])) {
        $user_type = $_SESSION['user_type'];
        $user_id = $_SESSION['user_id'];
        $user_name = $_SESSION['user_name'];
        $branch_id = $_SESSION['branch_id'];
        $branch_name = $_SESSION['branch_name'];
        $store_id = $_SESSION['store_id'];
        $store_name = $_SESSION['store_name'];
}else{
    header('Location: login');
    die();
}
date_default_timezone_set('Asia/Karachi');

$curstamp = date('Y-m-d h:i:s a');
 $html = '';
 $sr = 0;
 
 $count = 0;
        $sr = 0;
        $tot_amount= 0;
        $gross_amount = 0;
        $amount = 0;
        $tot = 0;
 

$acc_no = $_GET['acc_no'];

$statement = $connect->prepare("select acc_name from accounts where acc_no = '".$acc_no."'");
$statement->execute();
$acc_name = $statement->fetchColumn();

$dated= date('Y-m-d');
$gross_total = 0;
    
$sql = "select m.dated,m.ref_id,m.description,d.debit from jv_detail d, jv m where m.jv_id = d.jv_id and d.acc_no = '".$acc_no."' and d.debit > 0 order by dated";
$statement = $connect->query($sql);
$sale = $statement->fetchAll();

 
$sql = "select sum(d.credit) from jv_detail d, jv m where m.jv_id = d.jv_id and d.acc_no = '".$acc_no."'";
$statement = $connect->query($sql);
$payment = $statement->fetchColumn();



 ?>
<!DOCTYPE html>
<html>
<head>
   <title><?php echo basename(__FILE__, '.php'); ?></title>
  <style>
    * {
      
      font-family: arial;
    }
  .rightside {text-align: right;}
  table td{
    
    border-bottom: 1px solid gray;
    padding: 5px;
  }
  .head_row {
    font-weight: bold;
  }
  thead td {
    font-weight: bold;
     border-bottom: 1px solid gray;
      border-top: 1px solid gray;
       background-color: :  gray;
  }
  table.header {
    font-family: arial; 
    font-size: 16px;
    font-weight: bold;
  }
  </style>
</head>
<body>
 <table class="header" width="100%">
  
 
<tr>
  <td colspan="9" width="100%">Invoice Aging Detail For <?php echo $acc_name;?></td>
  
</tr>
 </table> 
  <table id="main" width="100%">
  
  <thead>
<tr>
  <td width="5%">#</td>
  <td width="15%">S_ID</td>
  <td width="20%">Date</td>
   
  <td width="35%">Description</td>
  
  
  
  <td width="10%">Over</td>
  
  <td width="15%" class="rightside">Amount</td>
  
</tr>
  </thead>
  
<tbody>
 


<?php 
$tot_amount = 0;
$count = 0;
foreach ($sale as $invoice) {
$dated = date('Y-m-d');
  $invoice_dated = $invoice['dated'];
 $date1_ts = strtotime($invoice_dated);
    $date2_ts = strtotime($dated);
    $diff = $date2_ts - $date1_ts;
    $diff = round($diff / 86400);
  $amount = $invoice['debit'];
  $pre_amount = $amount;
  
  if($amount > $payment)
  {

$count++;
 if($payment > 0 ){$amount = $amount - $payment;}
  
echo '
  
  <tr>
  <td>'.$count.'</td>
  <td>'.$invoice["ref_id"].'</td>
  <td>'.$invoice["dated"].'</td>
  <td>'.$invoice["description"].'</td>
  <td>'.$diff.'</td>
  <td class="rightside">'.$amount.'</td>
</tr>
';
$tot_amount = $tot_amount + $amount;
}
$payment = $payment - $pre_amount;

}
echo '
<tr>
  
  <td colspan="5" class="rightside">Total:</td>
  <td class="rightside">'.$tot_amount.'</td>
</tr>';
?>

</tbody>
 
 </table><!--end main table-->
    

 </body>
 </html>