<?
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($ac=='Cash')
  {
$chac=mysqli_query($db,"select * from ledger where led='$cus' and brcode='$brcode'");
if(mysqli_num_rows($chac)<=0)
{
 print "<script> alert('Ledger does not exist'); history.back(1); </script>";
 return;
}
   $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','o_form','$fno')";
 mysqli_query($db,$sql)or die('Csh rec err:'.mysqli_error());

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

   $sql="insert into receipts (formno,date,cus_ac,amount,ptype,cr_no,br_no,brcode,narr,jvno) values( '$fno','$dt','$cus','$amt','Cash','$crno','','$brcode','$narr','$jvno')";
  mysqli_query($db,$sql) or die('receipts err'.mysqli_error());
 }
 else
  {
   $sql="select max(br_no) from receipts where brcode='$brcode'";
   $rr=mysqli_query($db,$sql);
   $cr_row=mysqli_fetch_row($rr);
    if($br_row[0]<=0)
     $brno=1;
    else
     $brno=$br_row[0]+1;
   $sql="insert into receipts (formno,date,cus_ac,amount,ptype,cr_no,br_no,brcode,narr,jvno,fine) values('$fno','$dt','$cus','$amt','$ac','','$brno','$brcode','$narr','$jvno','$ptype')";
mysqli_query($db,$sql) or die('rec err:'.mysqli_error());

   $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','o_form','$fno','')";
 mysqli_query($db,$sql) or die('bnk ins err:'.mysqli_error());
 }
   

if($ac=='Cash')
 {
 $jour_dr="insert into journal2 values('$dt','$jvno','Dr','$ac','$amt','0','Advance:$narr, CRNo.:$csno', '$brcode','CR', 'o_form','$fno','$csno')"; 
$nr="Advance:".$narr.", CRNo:$csno";
}
else
 {
$jour_dr="insert into journal2 values('$dt','$jvno','Dr','$ac','$amt','0','Advance:$narr,BR No.:$brno2', '$brcode','BR', 'o_form','$fno','$brno2')"; 
$nr="Advance:".$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','-', 'o_form','$fno','')";
 mysqli_query($db,$jour_cr) or die('cr err:'. mysqli_error());

$sq="update advcollection set clearing='y' where code='$code' and brcode='$brcode' and formno='$fno'";

 mysqli_query($db,$sq) or die('advcoll err:'.mysqli_error());

print "<script> window.open('prnadvrec.php?jvno=$jvno'); </script>";
print "<script> document.location='../loc-clear.php'; </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)
 {
 alert("Invalid Customer")
 submitOK="false"
 }
if (ptype<=0)
 {
 alert("Invalid A/c Type")
 submitOK="false"
 }

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 ?>'>
<table border=1>
<th colspan=4 align=center>RECEIPT ENTRY</TH>
<?

   print "<tr><td>Order Form No</td><td><input type=text readonly name='fno' value='$fno' size=8></td>";
   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>Customer </td><td>";
   $re=mysqli_query($db,"select led from ledger where grp='Sundry Debtors' and brcode='$brcode' order by led")or die(mysqli_error());
   print "<select name='cus'><option></option>";
   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>Financer</td><td><input type=text name='ptype' size=40 readonly value='$ptype' style={text-align:left} title='Transfer to A/c'></td>";
  print "<td>A/c Type</td><td><select name='ac'><option></option><option>Cash</option>";

$re=mysqli_query($db,"select led from ledger where (grp='Bank Accounts' or grp='Bank O/d')  and brcode='$brcode' order by led")or die(mysqli_error());

   while($row=mysqli_fetch_row($re))
   {
     print "\n<option>$row[0]</option>";
   }
   print "</select></td>";
?>
<tr><td>Cheque Details.<td colspan=3><input type=text name='narr' size=70  value='No:   Dt:   of:'></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='loc-clear.php'> 
</td></tr>
</form>

