<?php
 
 include('db_con.php');
 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();
}
 $html = '';
 
 //$store_id = $_Get['fitem_no'];
// $store_id = $_Get['titem_no'];
 $dated = date('d-m-Y h:m A');
 $city = $_GET['city'];
 $rpt_filter = $_GET['rpt_filter'];
 $name = "ALL";
 if($rpt_filter=='rec'){$name = 'Receivables';}
 if($rpt_filter=='pay'){$name = 'Payables';}
 
 $tot_balance = 0;
 
  ?>
<!DOCTYPE html>
<html>
<head>
  <title>Out Standings</title>
  <style>
  * {
    font-family: arial;
    font-weight: bold;
    font-size: 12px;
    padding: 4px;
  }
  @media print {
  @page { margin: 0.5cm; }
  body { margin: 0.1cm;}
}

thead td {
  border-bottom: 2px solid black;
  border-top: 2px solid black;
  background-color:#b388ff;
}

tbody td {
   border-bottom: 1px solid black;
  
}
.rightside {
  text-align: right;
}
.liability {
  color: red;
}
.bigfont {
  font-size: 14px;
}
.nobor {
  border: none;
}
</style>

</head>
<body>
<table width="100%" class="table_header">
<tr>
<td class="nobor">Outstanding Report</td>
</tr>
<tr>
<td class="nobor">City : <?php echo $city;?></td>

</tr>
<tr>
<td class="nobor">Filter By : <?php echo $name;?></td>

</tr>
</table>
  <table width="100%" id="print_table" class="print_table">
    <thead>
  <tr>
  <td width="5%">#</td>  
  <td width="10%">No</td>
  <td width="40%">Name</td>
  
  <td width="20%">Phone</td>
  
  <td width="12%" class="rightside">Balance</td>
  <td width="13%" class="rightside">Recovery</td>
  </tr>
  </thead>
  <tbody>
<?php

  $sql = "select a.acc_no,a.acc_name, a.phone, sum(d.debit-d.credit) balance from  jv_detail d, jv m, accounts a   where m.jv_id=d.jv_id and d.acc_no=a.acc_no and a.city  LIKE '%".$city."%' and m.status = 'Active' AND (a.acc_type = 'CREDITOR' OR a.acc_type = 'DEBTOR')  group by acc_no";
  
  
   $statement = $connect->prepare($sql);
 $statement->execute();
  
 
 $result = $statement->fetchAll();

$balance = 0;
$sr = 0;
foreach($result as $row)
 {
  
  $balance = $row["balance"];
  
  
if($balance < 0)
  {
    $liablility = "liability";
  }else{$liablility = "";}

if($rpt_filter=='rec')
{
  if($balance > 0)
  {
    $tot_balance = $tot_balance + $balance;
    $sr++;
    echo '
  
  
  
<tr>
 <td>'.$sr.'</td>
  <td>'.$row["acc_no"].'</td>
<td>'.$row["acc_name"].'</td>
<td>'.$row["phone"].'</td>
   <td class="rightside '.$liablility.'">'.number_format($balance, 0).'</td>
     <td class="rightside"></td>
  
   
    
   </tr>
  ';
  }
}elseif($rpt_filter=='pay')
{
   if($balance < 0)
  {
    $tot_balance = $tot_balance + $balance;
    
    $sr++;
    echo '
  
  
  
<tr>
<td>'.$sr.'</td>
  <td>'.$row["acc_no"].'</td>
<td>'.$row["acc_name"].'</td>

<td>'.$row["phone"].'</td>
   <td class="rightside '.$liablility.'">'.number_format($balance, 0).'</td>
     <td class="rightside"></td>
  
   
    
   </tr>
  ';
  }
}else
{
   if($balance <> 0)
  {
    $tot_balance = $tot_balance + $balance;
    $sr++;
    echo '
  
  
  
<tr>
<td>'.$sr.'</td>
  <td>'.$row["acc_no"].'</td>
<td>'.$row["acc_name"].'</td>

<td>'.$row["phone"].'</td>
   <td class="rightside '.$liablility.'">'.number_format($balance, 0).'</td>
     <td class="rightside"></td>
  
   
    
   </tr>
  ';
  }
}

  
  
}

  
 if($tot_balance < 0)
  {
    $liablility = "liability";
  }else{$liablility = "";}  
  
   echo '
 
<tr>
  <td colspan="4" class="rightside">Total :</td>
   <td class="bigfont rightside '.$liablility.'">'.number_format($tot_balance, 0).'</td>
   <td class="rightside"></td>
  
   
    
   </tr>
  
  ';?>
   <tr><td colspan="6">Printed at : <?php echo $dated;?></td></tr>
  </tbody>

  
   
  </table>
</body>
</html>