<? 
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($sprint)
{ 
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}


$mmm=mysqli_query($db,"select invno from scert where invno='$pno' and type !='d' and brcode='$brcode' and taxtype='$taxtype'");
 if(mysqli_num_rows($mmm)>0)
 { print "<script> alert('Duplicate sale cert no'); history.back(1); </script>"; return;}

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

 $re=mysqli_query($db,"select max(invno) from scert where brcode='$brcode' and taxtype='$taxtype'")or die(mysqli_error());
 $inv_row=mysqli_fetch_row($re);
 if($inv_row[0]<=0)
  $invno=1;
 else 
  $invno=$inv_row[0]+1;
 $invdate=$y.'-'.$m.'-'.$d;
 $rr=mysqli_query($db,"select code from rep_master where repname='$rep'");
 $rep_row=mysqli_fetch_row($rr);
 $rep=$rep_row[0];

$ttt=$cessamt+$amt2+$tax;
if($ttt!=$netamt)
{
 $dif=($netamt-$ttt);
}
  $sql="insert into scert values('$cust','$formno','$mcode','$rep','$area', '$fin','$invno', '$invdate','$da','$config','$unit','$disc','$netamt', '$taxp','$tax', '$amt2','$color','$engno','$segment','$chasis','$remark','$brcode','-','$invno', '$emcode','$tp','$cessp','$cessamt','$taxtype','$mfyear')";
 mysqli_query($db,$sql) or die(mysqli_error());

 
$cure=mysqli_query($db,"select code,acname from cust_master where code='$cust'");
$cust_row=mysqli_fetch_row($cure);
 $cust_name=strtoupper($cust_row[1]);

if($taxtype=='CST')
 print "<script>window.open('scertreport.php?invno=$invno');</script>";
else if($tp=='8')
 print "<script>window.open('scertreport.php?invno=$invno&da=$da');</script>";
else
 print "<script>window.open('scertreport.php?invno=$invno&da=$da');</script>";

} // end save

?>
<style>
select,input,body,td{font-family:'arial narrow';letter-spacing:1;font-size:13}
tr{background-color:c1d7bb}
.mnu{margin-left:5}
</style>
<body bgcolor='ivory'>

<script>
var adr=[];
var ar1=[]; 
var config=[];; var config2=[];
var model=[];
var rep=[];
var area=[];
var color=[];
var engno=[];
var chasis=[];
var em_code=[];
var sconfig=[];

function fun1()
{
 var x=document.f1.da.selectedIndex; 
 
 if(x<=0)
   {
     document.f1.mcode.value='';
     document.f1.config.value='';
   }
 else
  { 
    document.f1.mcode.value=ar1[document.f1.da.selectedIndex];
    document.f1.config.value=config[document.f1.da.selectedIndex];
    document.f1.color.value=color[document.f1.da.selectedIndex];
     document.f1.engno.value=engno[document.f1.da.selectedIndex];
     document.f1.chasis.value=chasis[document.f1.da.selectedIndex];
     document.f1.emcode.value=em_code[document.f1.da.selectedIndex];
    
  }

  if(document.f1.model.value != document.f1.mcode.value)
   {
       alert('Model code doesn\'t match with order form');
       document.f1.mcode.value='';
       document.f1.config.value='';
      document.f1.emcode.value= '';  
}

}
function chmcode()
{ 
    document.f1.config.value=config2[document.f1.mcode.selectedIndex];
  document.f1.maker.value=sconfig[document.f1.mcode.selectedIndex];

if(document.f1.model.value != document.f1.mcode.value)
   {
       alert('Model code doesn\'t match with order form');
       document.f1.mcode.value='';
       document.f1.config.value='';
      document.f1.emcode.value= '';  
   }
}
function valid()
{
 submitOK="true";
 if(document.f1.invno.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Invoice No.');
 }
 if(document.f1.cust.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Customer');
 }

 if(document.f1.maker.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Descr.');
	return;
 }

 if(document.f1.mfyear.value.length<=0)
 {
   submitOK="false";
   alert('Invalid month/year');
 }

 if(document.f1.fin.selectedIndex<=0)
 {
   submitOK="false";
   alert('Invalid Financer');
 }

 if(document.f1.formno.seletedIndex<=0)
 {
   submitOK="false";
   alert('Invalid Formno');
 }
 if(document.f1.unit.value.length<=0)
 {
   submitOK="false";
   alert('Invalid unit price');
 }
 if(document.f1.segment.selectedIndex<=0)
 {
   submitOK="false";
   alert('Invalid Segment No.');

 }
if(submitOK=="true")
{
 x=confirm("Do you want to save?");
 if(x)
   submitOK="true";
 else
    submitOK="false";
}
  if(submitOK=="true")
   return true;
  else
   return false;
}

function chkey(abc)
{
 yy=abc.value
 if(yy.indexOf(' ')>-1) 
  {
    alert('Space is not allowed');
    abc.value='';
  }
}

function setadr()
{
  var x= document.f1.cust.selectedIndex ; 
  if(x>0)
    document.f1.addr.value=adr[document.f1.cust.selectedIndex];
  else
    document.f1.addr.value='';
}
function setModel()
{
  var x= document.f1.formno.selectedIndex ; 
  if(x>0)
    {
     document.f1.model.value=model[document.f1.formno.selectedIndex];


   }
  else
    {
      document.f1.model.value='';
     document.f1.rep.value='';
     document.f1.area.value='';
   }

}
 
function setttype()
{
 if(document.f1.taxtype.selectedIndex==1)
  {
    document.f1.taxp.value=2;
    document.f1.cessp.value=0;
    document.f1.cessamt.value=0;

 }
 else
 {
  document.f1.cessp.value=1;
    document.f1.taxp.value=12.5;
 }

 calc();
}
</script>


<style>
body,td{font-family:'arial narrow';letter-spacing:1;}
th{background-color:green;color:white}
.mnu{margin-left:5}
</style>
<body bgcolor='ivory'>

<form name='f1' action='<?print $PHP_SELF?>' onSubmit="return valid()">
<table cellspacing=1 cellpadding=1 bgcolor=black>
<tr><td align=center bgcolor='GRAY'><FONT COLOR='IVORY'><B>CUSTOMER DETAILS</TD></TR>
<TR><td align=center bgcolor='ivory'>
<table>
<tr><td><font color=red>Customer</td><td colspan=1> <select name='cust' onChange='setadr()'>
<option></option>
<?
$i=1;
$re=mysqli_query($db,"select * from cust_master where branch='$brcode' order by acname");
while($row=mysqli_fetch_array($re))
{
 print "\n<option value='$row[code]'>$row[acname]</option>";
 print "\n<script>adr[$i]='$row[addr1] - $row[city] - $row[dist] - $row[stat] - $row[pin] - $row[phone1]' </script>";
 $i++;
}
?>
</select> </td></tr>
<tr>
<td>Address </td><td><textarea name='addr' cols=40></textarea></td>



<tr><td><font color=red>Financer</font> </td><td colspan=5> <select name='fin'> <option></option>
<?
 $re=mysqli_query($db,"select code,finname from fin_master where branch='$brcode' order by finname");
  while($row=mysqli_fetch_row($re))
  {
   print "<option value='$row[1]'>$row[1]</option>";
  }
?>
</select>
</td></tr>

 </td><td>Month & Year Of Manufacture</td><td><input type=text  name='mfyear'></td></tr>
<tr><td>S.Cert Config.</td><td><input type=text name='maker' size=80></td></tr>
<tr><td>Mod Code</td><td><input type=text name='model' readonly></td></tr>

<tr><td><font color=red>Form No</td><td> <select name='formno' onChange='setModel()'>
<option></option>
<?
$rr=mysqli_query($db,"select a.formno,a.model,b.repname,b.dist from orderform a,rep_master b where a.rname=b.code and a.sale_invno='-' and a.brcode='$brcode'")or die(mysqli_error());
 $i=1;
while($row=mysqli_fetch_row($rr))
{
 print "\n<option>$row[0]</option>";
 print "\n<script> model[$i]='$row[1]' </script>";
 print "\n<script> rep[$i]='$row[2]' </script>";
 print "\n<script> area[$i]='$row[3]' </script>";

 $i++;
}
?>
</select> 

<!-- <input type=text readonly name='model'>-->
</td></tr>




<br>
<CENTER>

<tr  bgColor='c1d7bb'><td align=center>
<tr bgColor='c1d7bb'><td colspan=7 align=center bgcolor='green'>
<font color='ivory'><STRONG>SALE CERTIFICATE  (No:

<?
$rs=mysqli_query($db,"select max(invno) from scert where brcode='$brcode' ")or die(mysqli_error());
 $p_row=mysqli_fetch_row($rs);
 if(mysqli_num_rows($rs)<=0)
  $pno=1;
 else
    $pno=$p_row[0]+1;
print $pno.')';
?>
<input type=hidden name='pno' value='<?print $pno?>'>
</TD></TR>
<tr>

</tr>
<tr>
<td>Certificate No </td><td><input type=text  name='invno' value='<?print $pno?>' size=8 style={background-color:silver}></td>

</tr>
<tr><td>Chasis </td><td>
<select name='da' onChange=fun1()>
<option></option>
<?
$i=1;
$result=mysqli_query($db,"select * from veh_purchase where chasis not in (select chasis from scert) and  brcode='$brcode' order by chasis")or die(mysqli_error());
while($row=mysqli_fetch_array($result))
{
 print "\n<option>$row[chasis]</option>";
 print "\n<script> ar1[$i]='$row[mcode]';\n config[$i]='$row[config]'; \n";
 print "color[$i]='$row[color]'; \n engno[$i]='$row[engno]'; \n chasis[$i]='$row[chasis]';";
$result1=mysqli_query($db,"select * from veh_master where mcode='$row[mcode]' and  brcode='$brcode'")or die(mysqli_error());
$row1=mysqli_fetch_array($result1);
print "em_code[$i]='$row[emcode]'; </script>";
// print "em_code[$i]='$row[emcode]'; </script>";
 $i++;
}

$result=mysqli_query($db,"select * from vehopstock where brcode='$brcode' order by dano")or die(mysqli_error());
while($row=mysqli_fetch_array($result))
{
 print "\n<option>$row[chno]</option>";
 print "\n<script> ar1[$i]='$row[mcode]';\n config[$i]='$row[config]'; \n";
 print "color[$i]='$row[color]'; \n engno[$i]='$row[engno]'; \n chasis[$i]='$row[chno]'; </script>";
 $i++;
}

$result=mysqli_query($db,"select * from psndb0910.veh_purchase where chasis not in (select chasis from scert) and  brcode='$brcode' order by chasis")or die(mysqli_error());
while($row=mysqli_fetch_array($result))
{
 print "\n<option>$row[chasis]</option>";
 print "\n<script> ar1[$i]='$row[mcode]';\n config[$i]='$row[config]'; \n";
 print "color[$i]='$row[color]'; \n engno[$i]='$row[engno]'; \n chasis[$i]='$row[chasis]';";
$result1=mysqli_query($db,"select * from psndb0910.veh_master where mcode='$row[mcode]' and  brcode='$brcode'")or die(mysqli_error());
$row1=mysqli_fetch_array($result1);
print "em_code[$i]='$row[emcode]'; </script>";
// print "em_code[$i]='$row[emcode]'; </script>";
 $i++;
}
?>
</select>
</td></tr>
<td>Model Code </td><td><select name='mcode' onChange=chmcode()>
<option></option>
<?
$rr=mysqli_query($db,"select * from veh_master where brcode='$brcode' order by mcode");
$k=1;
while($rrow=mysqli_fetch_array($rr))
{
 print "<option value='$rrow[1]'>$rrow[1]-$rrow[2]</option>";
$result1=mysqli_query($db,"select * from veh_master where mcode='$rrow[1]' and  brcode='$brcode'")or die(mysqli_error());
$row1=mysqli_fetch_array($result1);

 print "\n<script>config2[$k]='$rrow[2]';\n sconfig[$k]='$row1[sconfig]';</script>";

 $k++;
}
?>
</select>

</tr>
<tr><td>config </td><td ><input type=text name='config' size=40></td></tr>

<tr></td><td>Engine No</td><td><input type=text readonly name='engno'></td></tr>
<tr><td>Color</td> <td><input type=text name='color'></td></tr>
<tr><td>Chasis No</td><td><input type=text readonly name='chasis'></td></tr>

<tr><td>Date </td><td>dd <input type=text name='d' size=2 value='<?print date("d",time())?>'>
mm <input type=text name='m' size=2 value='<?print date("m",time())?>'>
yy <input type=text name='y' size=4 value='<?print date("Y",time())?>'>
</td></tr>
<tr><td colspan=6 align=center bgcolor='green'> 
<font color=white size=4>

</font>
<input type=submit  value=' SAVE ' name='sprint'>


<!--<input type=BUTTON value='  SAVE AND ADD ITEMS  ' name='svadd'> -->
<input type=button value='  CANCEL  ' onClick=history.back(1)></td></tr>
</table>
</form>
</td></tr>
</table>
</center>
<br>
<script>
function calc()
{
var taxper=parseFloat(document.f1.taxp.value);
document.f1.netamt.value=document.f1.unit.value-document.f1.disc.value;
 

 var cessper=parseFloat(document.f1.cessp.value).toFixed(0);

 net=parseFloat(document.f1.netamt.value);
 if(cessper==0 && taxper==2)
	{ amt=(net*100)/102; }
 else if(cessper==1)
	 amt=(net*100)/112.625; 
 else if(cessper==0)
	 amt=(net*100)/112.5; 

 document.f1.amt2.value=parseFloat(amt).toFixed(2);

 tax=parseFloat(document.f1.amt2.value)*parseFloat(document.f1.taxp.value)/100;
 document.f1.tax.value=(tax).toFixed(2);
 var cessper=parseFloat(document.f1.cessp.value).toFixed(0);

 var ces_amt=parseFloat((document.f1.tax.value*cessper)/100);
 document.f1.cessamt.value=ces_amt.toFixed(2);
}
</script>



