<?
session_start();
include_once('includes/top.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}

include("opendb.php");
include("test.php");
?>

<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
th{background-color:lightsteelblue;color:black}
//tr{background-color:white}
</style>
<center>

<?
if(isset($_POST['show']))
{
$brres=mysqli_query($db,"select * from branch where brcode='$brcode'");
$brrow=mysqli_fetch_array($brres);
?>
<table>
<TR><TD COLSPAN=2 ALIGN=CENTER><b> PSN AUTOMOTIVE P LTD </b><BR>
	 <?print $brrow['braddr'].",".$brrow['brcity'].",<br><u> PH - ".$brrow['brphone']?></u>
&nbsp;
</td></tr>
<tr><td COLSPAN=2 ALIGN=CENTER>
</td></tr>
</table>
<?

$d1=$_REQUEST['d1'];
$m1=$_REQUEST['m1'];
$y1=$_REQUEST['y1'];

$d=$_REQUEST['d'];
$m=$_REQUEST['m'];
$y=$_REQUEST['y'];
 $dfrom=$y.'-'.$m.'-'.$d;
 $dto=$y1.'-'.$m1.'-'.$d1;
 $pstvl=$_REQUEST['pstvl'];
 

 
 
print "<b Report from ".datetohtml($dfrom).' to '.datetohtml($dto)."";
print "<table border=0 bgcolor='steelblue' cellspacing=1 cellpadding=3>";


     if($pstvl=='All')
     {
         $sql="select * from currency where closedate>='$dfrom' and closedate<='$dto' order by brcode";
      
     }
     else
     {
    $sql="select * from currency where brcode='$pstvl' and closedate>='$dfrom' and closedate<='$dto'  order by brcode";
     }
 
 
 $re=mysqli_query($db,$sql) or die(mysqli_error($db));
 $t1=0;
 $t2=0;
 $t3=0;
 $gtot=0;
 $i=0;
 $gstp=$row1[0]*2;
print "<table border=0 cellspacing=1 cellpadding=4 bgcolor='lightsteelblue'>";

print "<th>SNo<th>Closing Date <th>Branch<th>2000<th>500<th>200<th>100<th>50<th>20<th>10<th>5<th>2<th>1<th>Total <th>Remark<th>Book Balance";
while($row=mysqli_fetch_array($re))
{
    
    $sq1="select * from ledger where  brcode='$row[brcode]' and led='CASH IN HAND'";
$result1=mysqli_query($db,$sq1)or die(mysqli_error($db));
$row1=mysqli_fetch_array($result1);
$bal=0;
if($row1[0]=='CASH IN HAND' || $row1[0]=='CASH IN HAND')
{
    $balres=mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where brcode='$row[brcode]' and partic='$row1[0]' and date<='$row[closedate]'");
    $balrow=mysqli_fetch_row($balres);
    if($row1['drcr']=='Dr')
        {$bal=($balrow[0]+$row1[2])-$balrow[1]; //print '<br>bal'.$row[2];
    }
    else
    {
        $bal=$balrow[1]+$row1[2]-$balrow[0];
    }
}

    
        $twothousand1=$row['twothousand']*2000;
    $thousand1=$row['thousand']*1000;
    $fivehundred1=$row['fivehundred']*500;
    $twohundred1=$row['twohundred']*200;
    $hundred1=$row['hundred']*100;
    $fifty1=$row['fifty']*50;
    $twenty1=$row['twenty']*20;
    $ten1=$row['ten']*10;
    $five1=$row['five']*5;
    $two1=$row['two']*2;
    $one1=$row['one']*1;
    $fiftyps1=$row['fiftyps']*.50;
    $tot=$twothousand1+$thousand1+$fivehundred1+$twohundred1+$hundred1+$fifty1+$twenty1+$ten1+$five1+$two1+$one1+$fiftyps1;
    
    
    $i++;
    $brn=getbrname($row['brcode'],$db);
 print "<tr bgcolor='white' onMouseOver=bgColor='silver'; onMouseOut=bgColor='white'>";
 print  "<td>$i</td><td>".datetohtml($row['closedate'])."</td>";
 print  "<td>$brn</td><td align=right>$row[twothousand]</td><td align=right>$row[fivehundred]</td><td align=right>$row[twohundred]</td><td align=right>$row[hundred]</td><td align=right>$row[fifty]</td><td align=right>$row[twenty]</td><td align=right>$row[ten]</td><td align=right>$row[five]</td><td align=right>$row[two]</td><td align=right>$row[one]</td>";


 print "<td align=right>$tot</td><td align=right>$row[remark]</td><td align=right>$bal</td>";
print "</tr>";
 $t1+=$row['aprice'];
 $t2+=$gstp1;
 $t3+=$row['sgstamt'];
 $gtot+=$row['tot'];
}
print "<tr bgcolor='white'><td align=right><b></td>";



//return;

}


print "</table><br>";
//*********************************


?>

<form action='<?print $PHP_SELF?>' name='f1' method='post'>
<table>
<tr><td bgcolor='steelblue' colspan=2><center><b><font color=white>CASH CLOSING REPORT</td></tr>
<tr><td>Date From </td><td>dd<input type=text name='d' size=2 value='<?print date("d");?>'>
			mm<input type=text name='m' size=2 value='<?print date("m");?>'>
			yy<input type=text name='y' size=4 value='<?print date("Y");?>'>
</td></tr>
<tr><td>Date To </td><td>dd<input type=text name='d1' size=2 value='<?print date("d");?>'>
			mm<input type=text name='m1' size=2 value='<?print date("m");?>'>
			yy<input type=text name='y1' size=4 value='<?print date("Y");?>'>
</td></tr>
<tr><td>Search BY</td><td><select name='pstvl'>
    
    <?

if($user=='joy' ||  $user=='sunith' || $user=='anoop'|| $user=='jasil')
{
    ?>

   <option value='All'>All</option> 
<?
$qry=mysqli_query($db,"select distinct brcode,brname from  branch order by brname ASC");
}

else
{
 $qry=mysqli_query($db,"select distinct brcode,brname from  branch where brcode='$brcode' order by brname ASC");   
}

while($row=mysqli_fetch_array($qry))
{
?>
       <option value='<? echo $row[0] ?>'><? echo $row[1] ?></option>
<?
}
?>

 
 
 
</select></td></tr>
<th colspan=2><INPUT TYPE=SUBMIT VALUE='SHOW' name='show'></td></tr>
</table>
</form>
<script>
function edfun(x,y)
{
 if(confirm('Do you want to edit P.O. No.'+x+' ?'))
  document.location='edpur.php?purno='+x+'&ttype='+y;
}
</script>


