<? session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}

include("../opendb.php");

$recpt=mysqli_query($db,"select profinv.*,profreceipt.* from profinv join profreceipt on profinv.invno=profreceipt.profno where brcode='$brcode' and profinv.invno='$no'");
$recptdata=mysqli_fetch_array($recpt);

if($_POST[save])
{
 $dt=$y.'-'.$m.'-'.$d;
 if(strlen($amt)<=0 || strlen($no)<=0)
 {
  print "<script> alert('Invalid Entry'); history.back(1);</script>";
  return;
 }

if(strlen($chqno)> 0 && $ptype=='Cash')
 {
  print "<script> alert('Please select type as cheque'); history.back(1);</script>";
  return;
 }

 $rr=mysqli_query($db,"select * from profrec where no='$no' and brcode='$brcode'");
 if(mysqli_num_rows($rr)>0)
 {
  print "<script> alert('Duplicate Receipt NO'); history.back(1);</script>";
  return;
 }

$rec1dt=date('Y-m-d',strtotime($rd));
$chq1dt=date('Y-m-d',strtotime($cd));


 $sql="update profinv set mmoney='$amt',invdate='$rec1dt',ptype='$ptype' where invno='$no' and brcode='$brcode'";

 //$sql="insert into profrec values('$no','$dt','$amt','$cust','$brcode','$ptype')";
 mysqli_query($db,$sql) or die(mysqli_error());
 //print "<script> window.open('prnduprec.php?no=$no'); </script>";
 
$recdate=$y.'-'.$m.'-'.$d;
$chqdate=$cy.'-'.$cm.'-'.$cd;
//$sqlrec= "insert into profreceipt values(' ', '$no','$recdate','$amt','$ptype','$chqno','$chqdate','$bank')";


$sqlrecedit= "update profreceipt  set order_no='$orderno', recamt='$amt',recdt='$rec1dt',rectype='$ptype',chqno='$chqno',chqdt='$chq1dt',bank='$bank' where recno='$recptno'";
mysqli_query($db,$sqlrecedit) or die(mysqli_error());
print "<script> window.open('prnprofrec.php?invno=$no'+'&recno='+'$recptno'); </script>";




}

?>
<script>
 function valid()
 {
  if(document.f1.no.value.length<=0)
   {
     alert('Invalid receipt no');
     return false; 
   }
  if(document.f1.d.value.length<=0 || document.f1.m.value.length<=0|| document.f1.y.value.length<=0)
   {
     alert('Invalid Date');
     return false;
   }
  if(document.f1.amt.value.length<=0 || isNaN(document.f1.amt.value))
   {
     alert('Invalid Amount');
     return false;
   }
  if(document.f1.cust.value.length<=0)
   {
   
   }

if(document.f1.chqno.value.length>=0)
   {

alert('cash');
return false;

     
   }
}
</script>

<style>
body,td{font-family:'arial narrow';letter-spacing:1;}
tr{background-color:c1d7bb}
.mnu{margin-left:5}
</style>
<BODY BGCOLOR='IVORY'>
<input type=button value='Show Receipt' onClick=document.location='listreceipt.php'><br>
<CENTER>
<h3>PRINT PROFORMA RECEIPT</H3>
<form name='f1' action='<?print $_SERVER[PHP_SELF]?>' method='post' onSubmit="return valid()">

<table cellspacing=1 cellpadding=4 bgcolor=gray>
<tr><td>Proforma No.</td><td> <input type=text name='no' READONLY  value='<?print $no?>'> 
&nbsp;&nbsp; Order Form No. <input type=text name='orderno' style={text-align:right} size=10 value='<?print $recptdata[order_no]?>'> 
</td></tr>

<tr><td>Receipt Date</td><td> <input type=text name='rd' size=12 maxlength=12 value='<?print date('d-m-Y',strtotime($recptdata[recdt]));?>'>

 </td>
</tr>

<tr><td>Receipt Amount</td><td> <input type=text name='amt' style={text-align:right} value='<?print $recptdata[recamt]?>'> </td></tr>
<tr><td>Type</td><td> <select name='ptype' ><option><? print $recptdata[rectype]?></option><option value="Cash">Cash</option><option value ="Cheque">Cheque</option> </select> </td></tr>
<tr><td>Cheque Details</td><td> No: <input type=text name='chqno' size=8 value='<?print $recptdata[chqno]?>'>     Date:  <input type=text name='cd' size=12 maxlength=12 value='<?print date('d-m-Y',strtotime($recptdata[chqdt]));?>'>
	Bank <input type=text name='bank' size=25 value='<?print $recptdata[bank]?>'>

</td> 
<input type=hidden name='recptno' size=25 value='<?print $recptno?>'>

</tr>

<!--

<tr><td>Customer <br>(with addr)</td><td> <input type=text name='cust' size=90 value='<?print $custn?>'></td></tr>
-->
<tr><td colspan=2 align=center><input type=submit value='Edit & Print' name='save'></td></tr>
</table>
</form>
