<?php

 
 include('db_con.php');
 include('function.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();
}

if($user_type=='user')
{
  header('Location: dashboard');
}

$dated  = date('Y-m-d');
$count = 0;
accounts_update($connect);
$acc_no = $_GET["acc_no"];
  
         
  $statement = $connect->prepare("select acc_no as parent_acc,acc_name,balance from accounts_parent where acc_no = '".$acc_no."' order by acc_no");
 $statement->execute();
 $result = $statement->fetchAll(); 
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Sub Account Balance</title>
   <style>
* {
  
  font-family: arial;
  padding:4px;
}
@media print {
  @page { margin: 0.5cm; }
  body { margin: 0.1cm;}
}
  .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;
  }
  .total {
    font-weight: bold;
  }
  #heading {
    line-height: 0.1cm;
    font-size: 13px;
  }
  #heading p {
    line-height: 0.1cm;
    font-size: 10px;
  }

  h2 {
    font-size: 18px;
    font-family: calibri;
  }
  .head_row{
    font-weight: bold;
  }
  </style>
</head>
<body>

  <div id="header">
  <h3>Sub Account Report</h3>
  <table width="100%">
  <tr>
  <td width="15%">Printed at  :</td><td width="55%"><?php echo $dated; ?></td><td width="10%" class="rightside"></td><td width="20%" class="rightside">--</td>
  </tr>
  <tr>
  <td width="15%"> </td><td  width="55%" style="font-size:12px"></td><td width="10%" class="rightside"></td><td width="20%" class="rightside">--</td>
  </tr>
  
  </table>
</div>
  <table width="100%" id="data_table">
    <thead>
  <tr>
  <td width="5%">#</td>
  <td width="15%">No.</td>
  <td width="65%">Name</td> 
  
  <td width="15%" class="rightside">Balance</td>
  </tr>
 </thead>
 <tbody>
<?php
foreach($result as $row)
 {
$parent_acc = $row["parent_acc"];
   $count++;
          $detail_statement = $connect->prepare("select acc_no,acc_name,balance from accounts where status = 'Active' and balance <> 0 and acc_parent = '".$parent_acc."'");
         $detail_statement->execute(
                   );
         $detail_result = $detail_statement->fetchAll();
 
  // if($count == 1){$balance = $opening + $debit - $credit; }else{$balance = $balance + $debit - $credit;}
   
 
 echo ' 
<tr class="head_row">
<td>'.$count.'</td>
  <td>'.$row["parent_acc"].'</td>
<td class="text-bold">'.$row["acc_name"].'</td>
<td  class="text-bold">'.number_format($row["balance"],0).'</td>
</tr>';
    
  
   $sr = 0;
   //$tot_amount = 0;
   //$tot_qty = 0;
   //$tot_dis_amount = 0;
                foreach($detail_result as $detail_row)
                  {
                      $sr++;
                     
                      $balance = $detail_row["balance"];
                      
                      //$tot_amount = $tot_amount + $amount;
                     // $tot_qty = $tot_qty + $qty;
                     // $tot_dis_amount = $tot_dis_amount + $dis_amount;
                   echo '
                      <tr>
                       
                      
                      
                      <td width="5%">'.$sr.'</td>
                      <td width="15%">'.$detail_row['acc_no'].'</td>
                      <td width="65%">'.$detail_row['acc_name'].'</td>
    <td width="15%" class="rightside">'.number_format($balance, 2).'</td>
  
   
    
   </tr>
  
   ';
                  }
                 
                 
                  
}

?>
</tbody>
</table>
</body>
</html>