<?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');
$dated = date('Y-m-d h:i a');
 $html = '';

 

 $count = 0;

 



  $tot_debit=0;

  $tot_credit=0;

  $tot_balance = 0;

  

  $sql = "SELECT IFNULL(doc_status,'ON') FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'gl_reports_trial_balance'";

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

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

            $gl_reports_trial_balance = $statement->fetchColumn();



            if($gl_reports_trial_balance=='OFF'){header('Location: dashboard');}







    





  //$statement = $connect->prepare("select d.acc_no,a.acc_name, a.acc_type,sum(d.debit) as debit,

  //  sum(d.credit)as credit from jv_detail d, jv m,accounts a 

   // where m.jv_id=d.jv_id and a.acc_no=d.acc_no and m.dated between

  //  '".$fdate."' and '".$tdate."' and d.acc_no > 1037 group by acc_no,acc_name");




 ?>



<!DOCTYPE html>

<html>

<head>

  <title>

  Trial Balance Detail

  </title>

  <style>
    * {
      font-family: arial;
      padding: 5px;
    }
  .rightside {text-align: right;}

  table td{

    

    

    border-bottom: 1px solid gray;



  }
  thead {
    font-weight: bold;
  }

  </style>

</head>

<body>



 



  

  <table width="100%">

  <tr>
    <?php echo '

  <td colspan="10" style="text-align:center;font-size:18px;font-weight:bold;margin:5px">Trial Balance as on '.$dated.'</td>';
?>
  </tr>

  

  

  </table>

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


<thead>
  <tr>

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

  <td width="15%">Acc No.</td>

  <td width="37%">Account Name</td>

  

  

  <td width="15%" class="rightside">Debit</td> 

  <td width="15%" class="rightside">Credit</td> 

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

  

  

  </tr>



</thead>

  <tbody>

<?php


$statement = $connect->prepare("select a.acc_no,a.acc_name,sum(d.debit) debit,sum(d.credit) credit

  from jv_detail d,jv m ,accounts a where m.jv_id=d.jv_id and d.acc_no=a.acc_no and m.status = 'Active'

  group by acc_no,acc_name");





 $statement->execute();

 

 $result = $statement->fetchAll(); 
foreach($result as $row)

 {



  $debit = $row["debit"];

  $credit = $row["credit"];

  

 





  $balance = $debit-$credit;

  $tot_debit = $tot_debit + $debit;

  $tot_credit = $tot_credit + $credit;

  $tot_balance = $tot_balance + $balance;



  

  $count = $count+1;





     // if($count == 1){$stock = $opening + $qty_in - $qty_out; }else{$stock = $stock + $qty_in - $qty_out;}

   

 echo '

  

    

  

  



<tr>

<td width="3%">'.$count.'</td>

  

  <td width="15%">'.$row["acc_no"].'</td>

<td width="37%">'.$row["acc_name"].'</td>







<td width="15%" class="rightside">'.number_format($debit, 2).'</td>

   <td width="15%" class="rightside">'.number_format($credit, 2).'</td>

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

   

  

  

   

    

   </tr>

   





        ';



}

?>

 

 </table>







  <div id="footer">

  <hr />

  

  <table width="100%">

  

  <td colspan="3" width="55%" class="rightside bor">Total : </td>

  

  <td width="15%" class="rightside"><?php echo''.number_format($tot_debit, 2).'';?></td>

  <td width="15%" class="rightside"><?php echo''.number_format($tot_credit, 2).'';?></td>

  <td width="15%" class="rightside"><?php echo''.number_format($tot_balance, 2).'';?></td>

  

  </tr>

  </table>

    

  </div>

 </body>

 </html>