<link href="../css/form.css" type="text/css" rel="stylesheet" media="screen"  />
<html>
<body>


<?
session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}
include("../opendb.php");
include("../test.php");
if($save)
{
$dt=$y.'-'.$m.'-'.$d;

$rr1=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
 $jv_row=mysqli_fetch_row($rr1);
  if($jv_row<=0)
    $jvno=1;
  else
    $jvno=$jv_row[0]+1;
  

 if($ptype=='CR')
  {

   $sql="select max(crno) from cshrec where brcode='$brcode'";
   $rr=mysqli_query($db,$sql);
   $cr_row=mysqli_fetch_row($rr);
    if($cr_row[0]<=0)
     $csno=1;
    else
     $csno=$cr_row[0]+1;

   $sql="insert into cshrec values('$dt','$csno','Cr','$cus','0','$amt', 'Advance $narr', '$brcode','$jvno','done','$fno')";
 mysqli_query($db,$sql)or die('Csh rec err:'.mysqli_error());

 }

 elseif($ptype=='BR')
  {
   
   $sql="select max(brno) from bnkrec where brcode='$brcode'";
   $rr=mysqli_query($db,$sql)or die('brno err:'.mysqli_error());
   $br_row=mysqli_fetch_row($rr);
    if($br_row[0]<=0)
     $brno2=1;
    else
     $brno2=$br_row[0]+1;

  $sql="insert into bnkrec values('$dt','$brno2','Dr','$ac','$amt','0','Advance $narr','$brcode','$jvno','done','$fno','')";
 mysqli_query($db,$sql) or die('bnk ins err:'.mysqli_error());
 }

 elseif($ptype=='CP')
  {


  
   $sql="select max(cpno) from cshpay where brcode='$brcode'";
   $rr=mysqli_query($db,$sql)or die('brno err:'.mysqli_error());
   $br_row=mysqli_fetch_row($rr);
    if($br_row[0]<=0)
     $brno2=1;
    else
     $brno2=$br_row[0]+1;


  $sql="insert into cshpay values('$dt','$brno2','Dr','$ac','$amt','0','$narr','$brcode','$jvno','done','$fno')";


 mysqli_query($db,$sql) or die('cshpay ins err:'.mysqli_error());
 }

 elseif($ptype=='BP')
  {
   
   $sql="select max(bpno) from bnkpay where brcode='$brcode'";
   $rr=mysqli_query($db,$sql)or die('brno err:'.mysqli_error());
   $br_row=mysqli_fetch_row($rr);
    if($br_row[0]<=0)
     $brno2=1;
    else
     $brno2=$br_row[0]+1;

  $sql="insert into bnkpay values('$dt','$brno2','Dr','$ac','$amt','0','$narr','$brcode','$jvno','done','$fno','')";
   mysqli_query($db,$sql) or die('bnk ins err:'.mysqli_error());
 }



 //posting  

if($ptype=='CR')
 {
$cr='CR';
 $jour_dr="insert into journal2 values('$dt','$jvno','Dr','$ac','$amt','0','$narr, CRNo.:$csno', '$brcode','$cr', 'done','$fno','$csno')"; 
$nr="".$narr.", CRNo:$csno";
 mysqli_query($db,$jour_dr) or die('dr err:'.mysqli_error());
 $jour_cr="insert into journal2 values('$dt','$jvno','Cr','$cus','0','$amt','$nr','$brcode','$cr', 'done','$fno','')";
 mysqli_query($db,$jour_cr) or die('cr err:'. mysqli_error());


}
elseif($ptype=='BR')
 {
$cr='BR';
$jour_dr="insert into journal2 values('$dt','$jvno','Dr','$ac','$amt','0','$narr,BR No.:$brno2', '$brcode','$cr', 'done','$fno','$brno2')"; 
$nr="".$narr.", BRNo:$brno2";
 mysqli_query($db,$jour_dr) or die('dr err:'.mysqli_error());
 $jour_cr="insert into journal2 values('$dt','$jvno','Cr','$cus','0','$amt','$nr','$brcode','$cr', 'done','$fno','')";
 mysqli_query($db,$jour_cr) or die('cr err:'. mysqli_error());


}

elseif($ptype=='CP')
 {
$cr='CP';
$jour_dr="insert into journal2 values('$dt','$jvno','Dr','$ac','$amt','0','$narr,CP No.:$brno2', '$brcode','$cr', 'done','$fno','$brno2')"; 
$nr="".$narr.", CPNo:$brno2";
 mysqli_query($db,$jour_dr) or die('dr err:'.mysqli_error());
 $jour_cr="insert into journal2 values('$dt','$jvno','Cr','$cus','0','$amt','$nr','$brcode','$cr', 'done','$fno','')";
 mysqli_query($db,$jour_cr) or die('cr err:'. mysqli_error());


}

elseif($ptype=='BP')
 {
$cr='BP';
$jour_dr="insert into journal2 values('$dt','$jvno','Dr','$ac','$amt','0','$narr,BR No.:$brno2', '$brcode','$cr', 'done','$fno','$brno2')"; 
$nr="".$narr.", BPNo:$brno2";
 mysqli_query($db,$jour_dr) or die('dr err:'.mysqli_error());
 $jour_cr="insert into journal2 values('$dt','$jvno','Cr','$cus','0','$amt','$nr','$brcode','$cr', 'done','$fno','')";
 mysqli_query($db,$jour_cr) or die('cr err:'. mysqli_error());


}


elseif($ptype=='JV')
 {
$cr='JV';
$jour_dr="insert into journal2 values('$dt','$jvno','Dr','$ac','$amt','0','$narr', '$brcode','$cr', 'done','$fno','$brno2')"; 
$nr="".$narr."";
 mysqli_query($db,$jour_dr) or die('dr err:'.mysqli_error());
 $jour_cr="insert into journal2 values('$dt','$jvno','Cr','$cus','0','$amt','$nr','$brcode','$cr', 'done','$fno','')";
 mysqli_query($db,$jour_cr) or die('cr err:'. mysqli_error());


}


$sq="update journal2 set against='done',tr_no='$jvno' where brcode='$brc' and jvno='$fno' ";
print $sq;
mysqli_query($db,$sq) or die('advcoll err:'.mysqli_error());
 
$dt1 = explode("-", $dfrom);
$dt2 = explode("-", $dto);
?>
<script> 
document.location='../clearing.php?d1=<?print $dt1[2]?>&m1=<?print $dt1[1]?>&y1=<?print $dt1[0]?>&d2=<?print $dt2[2]?>&m2=<?print $dt2[1]?>&y2=<?print $dt2[0]?>&show=Show'; 

</script>";
<?
} // end save
print "<p style={background-color:c1d799}>&nbsp; Customer :".$name."</p>";
print "<p style={background-color:c1d799}>&nbsp; Address :".$addr."</p>";
?>
<script>
function valid()
{
var cus=document.f1.cus.selectedIndex;
var ptype=document.f1.ac.selectedIndex;

submitOK="true"

if (cus<=0)
 {
 
 }
if (ptype<=0)
 {
 
 }

if (submitOK=="false")
 {
 return false
 }
}
</script>

<style>
body,td{font-family:'arial narrow';letter-spacing:1;}
th{background-color:c1d799}
.mnu{margin-left:5}
body{BACKGROUND-COLOR:fcfff1}
</style>

<body>
<form name='f1' action='<?print $PHP_SELF?>' onSubmit="return valid()">
<input type=hidden name='code' value='<?print $code ?>'>
<input type=hidden name='amt1' value='<?print $amt ?>'>
<input type=hidden name='ptype' value='<?print $ptype ?>'>
<input type=hidden name='brc' value='<?print $brc ?>'>
<input type=hidden name='fno' value='<?print $fno ?>'>
<input type=hidden name='dfrom' value='<?print $dfrom ?>'>
<input type=hidden name='dto' value='<?print $dto ?>'>


<table border=1>
<th colspan=4 align=center>RECEIPT ENTRY</TH>
<?

   print "<tr>";
   print "<td>Date </td><td>
        dd <input type=text name='d' size='2' value='".date("d",time())."'>";
   print "mm <input type=text name='m' size='2' value='".date("m",time())."'>";
   print "yy <input type=text name='y' size='4' value='".date("Y",time())."'>";
   print "</td></tr>";
   print "<tr><td>Ledger(Cr) </td><td>";

if($type=='Dr')
{
   $re=mysqli_query($db,"select led from ledger where led='$name' order by led limit 1")or die(mysqli_error());
}
else
{
   $re=mysqli_query($db,"select led from ledger where brcode='$brcode'")or die(mysqli_error());
}

 
  print "<select name='cus'>";
   while($row=mysqli_fetch_row($re))
   {
     print "\n<option>$row[0]</option>";
   }
   print "</select></td>";
   print "<td>Amount </td><td><input type=text name='amt' size='12' readonly value='$amt' style={text-align:right}></td></tr>";
  print "\n<tr><td>VoucherType</td><td><select name='ptype'><option>$ptype</option><option>JV</option><option>CR</option><option>CP</option><option>BR</option><option>BP</option></td>";
  print "</tr><tr><td>Ledger (Dr)</td><td><select name='ac'>";



if($type=='Cr')
{
   $re=mysqli_query($db,"select led from ledger where led='$name' order by led limit 1")or die(mysqli_error());
}
else
{
   $re=mysqli_query($db,"select led from ledger where brcode='$brcode'")or die(mysqli_error());
}

   while($row=mysqli_fetch_row($re))
   {
     print "\n<option>$row[0]</option>";
   }
   print "</select></td>";
?>
<tr><td>Narration<td colspan=3><input type=text name='narr' size=70  ></td></tr>

 <tr bgcolor=c1d799><td colspan=4 align=center>
     <input type=submit value=' SAVE AND PRINT RECEIPT ' name='save'> 
     <input type=button value=' CANCEL ' onClick=document.location='../clearing.php'> 
</td></tr>
</form>

