<?php 

  session_start();
if(isset($_POST['aname'])){
 $uid=$_SESSION['userId'];
extract($_POST);
 $qtytotal=0;
$accname="";
if($aname==211){
$accname="Umer Farms Profit / Loss";
}
require 'connection.php';

?>

<tr>
  <td colspan="4" style=" text-align: center; color: red; font-size: 30px;"><?php echo $accname; ?></td>
  
</tr>
   <tr>
      <th   style=" font-weight: bold;  font-size: 16px; background-color: black; color: white;">Shed Name</th>
      <th  style=" font-weight: bold;  font-size: 16px; background-color: black; color: white;">Balance</th>                           
   </tr>
<?php
$qry = "SELECT sum(debit)-sum(credit) as bal from ledger where accid='$aname'";
        $run_qry = mysqli_query($connect, $qry) or die(mysqli_error($connect));
      $row = mysqli_fetch_array($run_qry);
      $totalexp=$row['bal'];



$sr=0;
$tbal=0;
$qry = "SELECT sum(credit)-sum(debit) as bal, shortname from ledger inner join accreg on ledger.accid=accreg.accid where ownerid='$aname' group by ledger.accid order by ledger.accid asc";
        $run_qry = mysqli_query($connect, $qry) or die(mysqli_error($connect));
        while($row = mysqli_fetch_array($run_qry)){
            $tbal=$tbal+$row['bal'];
       if($sr==1 or$sr==3 or $sr==5 or $sr==7 or $sr==9 or $sr==11 ){
    $clr='white';

   }
  
    else{
   $clr='cyan';

   }
?>

 <tr>
      <td   style=" font-weight: bold;  font-size: 14px; background-color: <?php echo $clr ?>; ">

<?php  echo $row['shortname']; ?>

      </td>
      <td  style=" font-weight: bold;  font-size: 14px; background-color: <?php echo $clr ?>;">

<?php  

if($row['bal']>0){
    $bals="(Profit)";
}
else{
    $bals="(Loss)";

}

echo $row['bal']." " .$bals; ?>

      </td>                           
   </tr>

<?php

$sr=$sr+1;

}
//end while body

?>
    <tr>
  <th   style=" font-weight: bold;  font-size: 16px; background-color: black; color: white;">Final Balance</th>
      <th  style=" font-weight: bold;  font-size: 16px; background-color: black; color: white;"><?php

      if($tbal>0){
    $bals="(Profit)";

      }
else{
    $bals="(Loss)";

}
       echo $tbal." ".$bals; ?></th>                           
   </tr>
<?php 
   if($uid==57 or $uid==52){
?>
     <tr>
  <th   style=" font-weight: bold;  font-size: 16px; background-color: red; color: white;">Personal Expenses</th>
      <th  style=" font-weight: bold;  font-size: 16px; background-color: red; color: white;"><?php echo $totalexp; ?></th>                           
   </tr>

    <tr>
  <th   style=" font-weight: bold;  font-size: 16px; background-color: blue; color: white;">Current Balance</th>
      <th  style=" font-weight: bold;  font-size: 16px; background-color: blue; color: white;"><?php 

if($tbal-$totalexp>0){
    $bals="(Profit)";

      }
else{
    $bals="(Loss)";

}


      echo ($tbal-$totalexp)." ".$bals; ?></th>                           
   </tr>


<?php
}




}
//end main body

 ?>