<?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 = '';



 $v_id = $_GET['v_id'];



 $count = 0;



 $balance = 0;



 $tot_balance = 0;



 $dated = date('d-m-Y');    



  



             $sql = "SELECT name FROM vendors WHERE v_id  ='".$v_id."' ";



            $statement = $connect->prepare($sql);



            $statement->execute(); // no need to add `$sql` here, you can take that out



           $acc_name = $statement->fetchColumn();



$sql = "select sv_id from sub_vendors where v_id = '".$v_id."' and status = 'Active'";



            $statement = $connect->prepare($sql);



            $statement->execute();



            $result = $statement->fetchAll();



?>



<!DOCTYPE html>



<html>



<head>



  <title>



  </title>



  <style>



* {



  font-size: 11px;



  font-family: arial;



}



  .rightside {text-align: right;}



  table td{   



    border-bottom: 1px solid gray;



  }



  .head_row {



    font-weight: bold;



  }



  thead td {



    font-weight: bold;



     border-bottom: 1px solid gray;



      border-top: 1px solid gray;



       background-color: :  gray;



  }



  </style>



</head>



<body>



  <table width="100%">



<tr>



  <td colspan="2" width="25%"><b>SubVendor Balances</b> as on : <?php echo $dated;?></td>



  </tr>



  <tr>



  <td width="50%">Acc No :</td><td width="50%"><?php echo $v_id;?></td>



  </tr>



  <tr>



  <td width="50%">Name : </td><td width="50%"><?php echo $acc_name;?></td>



  </tr>



  </table>



  <table width="100%" id="table_header">



<thead>



  <tr>



  <td width="5%">#</td>



  <td width="65%">Name</td>



  



  <td width="30%" class="rightside">Balance</td>



  </tr>



</thead>



  



<?php



foreach($result as $row)



 {



$sv_id = $row["sv_id"];



$acc_no = "201".$sv_id;



$subsql = "select a.acc_name,sum(d.debit-d.credit) balance from jv_detail d, accounts a where a.acc_no=d.acc_no and d.acc_no = '".$acc_no."' group by acc_name";



            $substatement = $connect->prepare($subsql);



            $substatement->execute();



            $subresult = $substatement->fetchAll();



foreach($subresult as $subrow)



 {



  $balance = $subrow["balance"];



$tot_balance = $tot_balance + $balance;



  $count = $count+1;



echo '



<tr>



<td width="5%" class="dataline">'.$count.'</td>



  



<td width="65%" class="dataline">'.$subrow["acc_name"].'</td>



<td width="30%" class="rightside dataline">'.number_format($balance, 2).'</td>



  



   </tr>



';



}



}



?>



<tfoot>



  <tr>



  <td colspan="2" width="65%" class="rightside"><b>Total: </b></td>



  <td width="30%" class="rightside total"><b><?php echo  number_format($tot_balance, 2);?></b></td>



  </tr>



</tfoot>



</table>



</body>



</html>



