<?
session_start();
include_once('includes/top.php');
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
    print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
    return;
}
include("opendb.php");
include("test.php");
$sq="select * from ledger where  brcode='$brcode' and led='CASH IN HAND'";
$result=mysqli_query($db,$sq)or die(mysqli_error($db));
$row=mysqli_fetch_array($result);
$bal=0;
if($row[0]=='CASH IN HAND' || $row[0]=='CASH IN HAND')
{
    $balres=mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where brcode='$brcode' and partic='$row[0]'");
    $balrow=mysqli_fetch_row($balres);
    if($row['drcr']=='Dr')
        {$bal=($balrow[0]+$row[2])-$balrow[1]; //print '<br>bal'.$row[2];
    }
    else
    {
        $bal=$balrow[1]+$row[2]-$balrow[0];
    }
}
$sv=$_REQUEST['sv'];
if($sv)
{
    $y=$_REQUEST['y'];
    $m=$_REQUEST['m'];
    $d=$_REQUEST['d'];
    $twothousand=$_REQUEST['twothousand'];
    $fivehundred=$_REQUEST['fivehundred'];
    $twohundred=$_REQUEST['twohundred'];
    $hundred=$_REQUEST['hundred'];
    $fifty=$_REQUEST['fifty'];
    $twenty=$_REQUEST['twenty'];
    $ten=$_REQUEST['ten'];
    $five=$_REQUEST['five'];
    $two=$_REQUEST['two'];
    $one=$_REQUEST['one'];
    $fiftyps=$_REQUEST['fiftyps'];
    $remark=$_REQUEST['remark'];
    $entrydate=date("Y-m-d H:i:s");
    $closedate=$y.'-'.$m.'-'.$d;
    $code=substr($place,0,3).date('is');
    $sql="create table if not exists currency(sno double(10,0),entrydate datetime,closedate date,brcode varchar(10),user varchar(50),thousand double(10,2),fivehundred double(10,2),hundred double(10,2),fifty double(10,2),twenty double(10,2),ten double(10,2),five double(10,2),two double(10,2),one double(10,2),fiftyps double(10,2),remark varchar(200),twothousand double(10,2),twohundred double(10,2))";
    mysqli_query($db,$sql) ;
    
     $invres1=mysqli_query($db,"select closedate from currency where brcode='$brcode' and closedate='$closedate'");
    $invrow1=mysqli_fetch_row($invres1);
    
    if(mysqli_num_rows($invres1)<=0)
    {
    
    $invres=mysqli_query($db,"select max(sno) from currency where brcode='$brcode'");
    $invrow=mysqli_fetch_row($invres);
    if($invrow[0]>0)
        $sno=($invrow[0]+1);
    else
        $sno=1;
    if($thousand<='')
        $thousand=0;
    $sql="insert into currency values('$sno','$entrydate','$closedate','$brcode', '$user','$thousand','$fivehundred' ,'$hundred','$fifty', '$twenty','$ten' ,'$five', '$two','$one','$fiftyps', '$remark','$twothousand','$twohundred')";
    mysqli_query($db,$sql)or die(mysqli_error($db));
    print "Record has been saved";
    }
    else
    {
   print "Entry already done for this date";
   return;
   
    }
}
$del=$_REQUEST['del'];
if($del)
{
        $code=$_REQUEST['code'];

     
    mysqli_query($db,"delete from currency where sno='$code' and brcode='$brcode'");

    
    
}
?>
<style>
tr{font-family:arial;font-size:13;background-color:white}
TH{COLOR:WHITE;BACKGROUND-COLOR:STEELBLUE}
input,textarea{border:solid;border-width:1px;border-color:black}
</style>
<script>
function validkm()
{
    var grtot=parseFloat(document.f1.twothousand.value*2000)+parseFloat(document.f1.fivehundred.value*500)+parseFloat(document.f1.twohundred.value*200)
+parseFloat(document.f1.hundred.value*100)+parseFloat(document.f1.fifty.value*50)+parseFloat(document.f1.twenty.value*20)
+parseFloat(document.f1.ten.value*10)+parseFloat(document.f1.five.value*5)+parseFloat(document.f1.two.value*2)
+parseFloat(document.f1.one.value*1)+parseFloat(document.f1.fiftyps.value*.5);
    document.f1.gtot.value=grtot;
}
function valid()
{
    if(parseFloat(document.f1.gtot.value)<=0)
    {
        alert('Invalid entry');
        return false
    }
    if(confirm('Do you want to save?'))
        return true;
    else
        return false;
}
</script>

<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<center>
<form name='f1' action='<?print $PHP_SELF?>' onSubmit='return valid()' method='post'>
<table cellspacing=1 cellpadding=3 bgcolor='gray'>
<th colspan=2>Currency Closing</th>


<?
if ($user=='sunith'|| $user=='anoop' || $user=='joy'|| $user=='jasil')
{
?>
<tr><td  bgcolor='silver'>Closing Date </td><td> 
	dd <input type=text  name='d' size=2 maxlength=2 value='<?print date('d')?>'>
	mm <input type=text  name='m' size=2 maxlength=2 value='<?print date('m')?>'>
	yy <input type=text  name='y' size=4 maxlength=4 value='<?print date('Y')?>'>
     </td>
</tr>

<?
}
else
{
?>

<tr><td  bgcolor='silver'>Closing Date </td><td> 
	dd <input type=text readonly name='d' size=2 maxlength=2 value='<?print date('d')?>'>
	mm <input type=text readonly name='m' size=2 maxlength=2 value='<?print date('m')?>'>
	yy <input type=text readonly name='y' size=4 maxlength=4 value='<?print date('Y')?>'>
     </td>
</tr>

<?
}

?>

<tr><td bgcolor='white' colspan=2 align=center>Denomination</td></tr>

<tr><td bgcolor='silver'>2000</td>
<td> 
	<input type=text name='twothousand' value='0' size=5  onChange=validkm()>
</td> </tr>

<tr><td bgcolor='silver'>500</td>
<td> 
	<input type=text name='fivehundred' size=5 value='0' onChange=validkm()>
</td></tr>

<tr><td bgcolor='silver'>200</td>
<td> 
	<input type=text name='twohundred' size=5 value='0' onChange=validkm()>
</td></tr>

<tr><td bgcolor='silver'>100</td>
<td> 
	<input type=text name='hundred' size=5 value='0' onChange=validkm()>
</td></tr>
<tr><td bgcolor='silver'>50</td>
<td> 
	<input type=text name='fifty' size=5 value='0' onChange=validkm()>
</td></tr>
<tr><td bgcolor='silver'>20</td>
<td> 
	<input type=text name='twenty' size=5 value='0' onChange=validkm()>
</td></tr>
<tr><td bgcolor='silver'>10</td>
<td> 
	<input type=text name='ten' size=5 value='0' onChange=validkm()>
</td></tr>

<tr><td bgcolor='silver'>5</td>
<td> 
	<input type=text name='five' size=5 value='0' onChange=validkm()>
</td></tr>
<tr><td bgcolor='silver'>2</td>
<td> 
	<input type=text name='two' size=5 value='0' onChange=validkm()>
</td></tr>
<tr><td bgcolor='silver'>1</td>
<td> 
	<input type=text name='one' size=5 value='0' onChange=validkm()>
</td></tr>


<tr><td bgcolor='silver'>.50ps</td>
<td> 
	<input type=text name='fiftyps' size=5 value=0 onChange=validkm()>
</td></tr>

<tr><td bgcolor='silver'>Remark</td>
<td> 
	<textarea name='remark' rows=3 cols=46></textarea>
</td></tr>
<tr><td bgcolor='silver'>Grand Total</td>
<td> 
	<input type=text name='gtot' size=20 value='0' READONLY> CashBalance = <?print $bal?>
	<input type=text name='cl' size=5 hidden value=<?print $bal?> >
</td></tr>
<th colspan=2><input type=submit value='Save' name='sv'></th>
</table>
</form>
<table cellspacing=1 cellpadding=3 bgcolor='grey' border=1>
<th>Date <th>2Thousand<th>Thousand <th>5Hundred <th>2Hundred<th>Hundred <th>Fifty <th>Twenty <th>Ten <th>Five <th>Two<th>One<th>Fifty Ps.<th>Total<th>Remark
<th>Del
<?
//print "select * from currency where brcode='$brcode' order by closedate";
$re=mysqli_query($db,"select * from currency where brcode='$brcode' order by closedate");
while($row=mysqli_fetch_array($re))
{
    $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;
    print "\n<tr><form method='post'>\n<input type=hidden name='code' value='$row[sno]'>\n<td>".datetohtml($row['closedate'])."</td><td align=right>$row[twothousand]</td><td align=right>$row[thousand]</td>";
    print "<td align=right>$row[fivehundred]</td><td align=right>$row[twohundred]</td><td align=right>$row[hundred]</td>";
    print "<td align=right>$row[fifty]</td><td align=right>$row[twenty]</td>";
    print "<td  align=right>$row[$ten]</td><td>$row[five]</td>";
    print "<td  align=right>$row[two]</td><td>$row[one]</td>";
    print "<td  align=right>$row[fiftyps]</td><td  align=right>$tot</td><td>$row[remark]</td>";
    
    if($user=='sunith' ||  $user=='anoop' ||  $user=='joy' )
    {
    print "<td><input type=submit value='Del' name='del'></form></tr>";
}
        
    }

?>



