<?
session_start();
include_once('includes/top.php');
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$cd=$_REQUEST['cd'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
?>
<html>
<body>
<style>
 td{font-family:'verdana';font-size:14;color:black;}
 th{font-family:'verdana';font-size:14;color:white;background-color:#5b5858}
 body{background-color:ivory}
</style>
<link type="text/css" href="includes/style1.css" rel="stylesheet" />
<link type="text/css" href="css/form1.css" rel="stylesheet" />
<center>
<font face='arial narrow' size=3 >Transaction Details of <b><? print $cd?> </font></center>
<form action='<?print $PHP_SELF?>'>

<input type=hidden name=cd value='<?print $cd?>'>
<input type=button value="<- Back" onClick=history.back(1)>


<table  cellspacing=1 cellpadding=1 width=100% class="grid">



   
 
   <tr>
              <th colspan='1' height='35'><b>Branches</b></th>
			<th><b>Opening</b></th><th><b>Closing</b></th>
		<th><b>APR</b></th>
		<th><b>MAY</th>
		<th><b>JUNE</th>
		<th><b>JULY</th>
		<th><b>AUG</th>
		<th><b>SEPT</th>
		<th><b>OCT</th>
		<th><b>NOV</th>
		<th><b>DEC</th>
                <th><b>JAN</th>
		<th><b>FEB</th>
		<th><b>MAR</th>
		<th><b>TOTAL</th>
         </TR>


<?
include("test.php");
include ("opendb.php");
$sql="select brcode,brname from branch order by brname";
$br=mysqli_query($db,$sql) or die ('Error'.mysqli_error($db));
$baltt=0;
while($brc=mysqli_fetch_array($br))
{

$re11=mysqli_query($db,"select * from ledger where led='$cd' and brcode='$brc[0]' ");
$opbbb=mysqli_fetch_array($re11);
$grp=$opbbb['grp'];
$re111=mysqli_query($db,"select desig from bruser where username='$user' and brcode='$brc[0]'");

$opbbb1=mysqli_fetch_array($re111);
$grp1=$opbbb1['desig'];
//print $grp1;

$cd=str_replace('~','&',$cd);

$re=mysqli_query($db,"select * from ledger where led='$cd' and brcode='$brc[0]' ");
$opb=mysqli_fetch_array($re);

$tr=0.0;$tp=0.0;$tr1=0.0;
$sql="select * from ledger where led='$cd'  and brcode='$brc[0]'";
$re=mysqli_query($db,$sql);
$opb=mysqli_fetch_array($re);


$sql="select * from journal2 where brcode='$brc[0]' and partic='$cd' order by date,jvno limit 1 ";
$result=mysqli_query($db,$sql) or die ('Error'.mysqli_error($db));
if(mysqli_num_rows($result)>0)
{
while($row=mysqli_fetch_array($result))
{
    $yr=2017;
    $re3=mysqli_query($db,"select * from ledger where led='$cd' and brcode='$brc[0]'"); 
  			$opb=mysqli_fetch_array($re3);
  			$hr_total=0;
  			
  				if($opb['drcr']=="Dr")
			{
				$hr_total=$opb['op'];
			}
			else if($opb['drcr']=="Cr")
			{
				$hr_total=-$opb['op'];
			}
			$optotal+=$hr_total;
  			
  			$op=number_format($hr_total,2,'.','');
  			
  			$balres1=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where brcode='$brc[0]' and partic='$cd'");
		$balrow1=mysqli_fetch_row($balres1);
		if($opb['drcr']=='Dr')
 		{
			$bal1=($balrow1[0]+$opb['op'])-$balrow1[1]; //print '<br>bal'.$row[2];
			//$bal1=$bal1*-1;
			$baltt+=$bal1;
		}
 		else
 		{
 			$bal1=($balrow1[1]+$opb['op'])-$balrow1[0];
 			$bal1=$bal1*-1;
 				$baltt+=$bal1;
 		}

		print "<tr><td colspan=1><blockquote>$brc[1]</td><td align=right>$op</td>";
		print "<td align=right>$bal1</td>";

		for($month=4; $month<16; $month++)
		{
            $k=$month;
             $yr=2017;
            if($month=='13')
            {
                $k=1;
            }
            if($k=='14')
            {
                $k=2;
            }
            if($month=='15')
            {
                $k=3;
            }
            
             if($k==1||$k==2||$k==3)
                 {
                     $yr=$yr+1;
                 }
            
            
            if($k<10)
                 $k= sprintf("%02s", $k);
            $df=$yr."-".$k."-01"; 
            $timestamp    = strtotime($df);
            $dt  = date('Y-m-t', $timestamp);  
          $sq="select * from journal2 where partic='$cd' and brcode='$brc[0]' and date between '$df' and '$dt' ";
           
   			$re2=mysqli_query($db,$sq);
  			$re3=mysqli_query($db,"select * from ledger where led='$cd' and brcode='$brc[0]'"); 
  			$opb=mysqli_fetch_array($re3);
			$tr=0; 
			$tp=0;
			if($opb['drcr']=="Dr")
			{
				$tr+=$opb['op'];
			}
			else if($opb['drcr']=="Cr")
			{
				$tp+=$opb['op'];
			}
			$a1=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where partic='$cd' and brcode='$brc[0]' and date between '$df' and '$dt'");
			$a1row=mysqli_fetch_row($a1);
			$tr=$a1row[0];
			$tp=$a1row[1];
			$bal=$tr-$tp;
			$tot+=$bal;
 			//print"<td align=right><a href='vmledgerdt.php?cd=$cd& df=".$df."&dt=".$dt."' target='blank'>".putComa($bal)."</a></td>";
 			print"<td align=right>".putComa($bal)."</a></td>";
 			
			$hr_total+=$bal;
            $vr_total[$k]+=$bal;
		}//end forloop
        //print "<td align='right'><a href='vmledgerdt.php?cd=$cd' target='blank'>".putcoma($hr_total)."</a></td></tr>";
        print "<td align='right'>".putcoma($hr_total)."</a></td></tr>";
        
    }
}
	  

		

}
    print "<tr><td colspan=1><b>Total</b></td><td align=right><b>".number_format($optotal,2,".","")."</b></td><td><b>".number_format($baltt,2,".","")."</b></td>";

             for($j=4;$j<16;$j++)

		{

$k=$j;

if($j=='13')
{
$k=1;
}
if($j=='14')
{
$k=2;
}
if($j=='15')
{
$k=3;
}
                     $k= sprintf("%02s", $k);
			print "<td align='right'>".putcoma($vr_total[$k])."</td>";

                     $vr_gp[$k]-=$vr_total[$k];
		}

               print "<td align='right'><b>".putcoma(array_sum($vr_total)+$optotal)."</b></td></tr>";
	
$sumleft+=$tot;
