<?
session_start();
$brcode = $_SESSION['brcode'];
$pono = $_REQUEST['pono'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.php'; </script>";
 return;
}
include("../opendb.php");
include("../test.php");
?>
<title>PSN Telcon</title>
    <script type="text/javascript" src="../scripts/jquery-latest.js"></script>
	<link type="text/css" href="../includes/style.css" rel="stylesheet" />
<link href="../css/form.css" type="text/css" rel="stylesheet" media="screen" />
    <script type="text/javascript">
        $(function() {
          if ($.browser.msie && $.browser.version.substr(0,1)<7)
          {
			$('li').has('ul').mouseover(function(){
				$(this).children('ul').show();
				}).mouseout(function(){
				$(this).children('ul').hide();
				})
          }
        });        
    </script>
<link rel="stylesheet" href="../style.css" type="text/css" />

<script>
function valid()
{
 if(document.f1.pbillno.value.length<=0)
 {
  alert('Invalid Purchase Bill No');
  document.f1.pbillno.focus();
  return false;
 }
 if(confirm('Do you want to save?'))
   return true;
 else
   return false;
}
</script>
<body background="../back_ground.jpg">
<?php include('logo1.php'); ?>

<?php include('top.php'); ?>
<br>
<table class="container"><tr><td>
<font style="font-family: 'Trebuchet MS',Verdana,Helvetica,sans-serif; font-size:15px;">  &nbsp;&nbsp;Spares >> <a href="#" style="text-decoration : none;">Purchase</a> >> Purchase by P Order</font>
<br>
<center>
<form name='f2' action="svpurchase.php" method=post onSubmit="return valid()">
<table bgcolor=silver>
<tr><td>Discount</td><td><input type=text name='discount' value=0 size=5></td>
    <td>Hand Charges/Bed Disc %</td><td><input type=text name='handling' value=0 size=5></td>
    <td><input type=button value='Set Value' onClick=setDis()></td>
</tr>
</table>

<br>
</center>

<input type=hidden name='porderno' value='<?print $pono?>'>
<input type=hidden name='taxtype' value='<?print $taxtype?>'>
<input type=hidden name='rnd' value='<?print $round?>'>
<?
$result=mysqli_query($db,"select * from purorder_spare where orderno='$pono' and brcode='$brcode'");
$row=mysqli_fetch_array($result);
print "<table align='center' border=1 cellspacing=1 cellpadding=3 ><th colspan=2> SPARE PURCHASE</TH>";
print "<tr><td>Pur Order No </td><td>$row[orderno]</td></tr>";

print "<tr><td>Pur Order Date </td><td>".datetohtml($row[date])."</td></tr>";
$br=mysqli_query($db,"select brname from branch where brcode='$row[orderto]'");
if(mysqli_num_rows($br)>0)
{
 $tobrow=mysqli_fetch_array($br);
 $tobr=$tobrow[0];
}else
{
  $br=mysqli_query($db,"select supname from sup_master where code='$row[orderto]'");
 $tobrow=mysqli_fetch_array($br);
 $tobr=$tobrow[0];
 
}
//print "<tr><td>Supplier </td><td><select name='sup'><option value='$row[orderto]'>$tobr
//</option></select></td></tr>";
print "<tr><td>Supplier </td><td><select name='sup'><option value='$tobr'>$tobr
</option></select></td></tr>";
print "<tr><td>Pur Bill No.</td><td><input type=text name='pbillno'></td></tr>";
print "<tr><td>Pur Date</td><td>dd<input type=text name='d' size=2 maxlength=2 value='".date("d")."'>
mm<input type=text name='m' size=2 maxlength=2 value='".date("m")."'>
yy<input type=text name='y' size=4 maxlength=4 value='".date("Y")."'>
</td></tr>";
if($user=="deepu")
{
	print "<tr><td>Entry Date</td><td>dd<input type=text name='d1' size=2 maxlength=2 value='".date("d")."'>
	mm<input type=text name='m1' size=2 maxlength=2 value='".date("m")."'>
	yy<input type=text name='y1' size=4 maxlength=4 value='".date("Y")."'>
	</td></tr>";
}
else
{
	print "<tr><td>Entry Date</td><td>dd<input type=text name='d1' readonly size=2 maxlength=2 value='".date("d")."'>
	mm<input type=text name='m1' size=2 maxlength=2 readonly value='".date("m")."'>
	yy<input type=text name='y1' size=4 maxlength=4 readonly value='".date("Y")."'>
	</td></tr>";
}
print"</table><br>";
?>
<br>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
//	tr{background-color:white}
	th{background-color:steelblue;color:white}
.right{text-align:right};
</style>
<center>
<table border=0 cellspacing=1 cellpadding=3 bgcolor='silver'>
<th>No<th>P Code <th> Name <th>Ord Qty <th> Sup Qty<th>FOC<th>Basic<br>Price<th>MRP<th>CenVat<th>CessCenVat<th>HE Cess CenVat<th>Total
<?
if($taxtype=='CST')
   print "<th>CST%";
else
   print "<th>VAT%";
?>
<th>Tax Amt <th>Cess% <th>Cess <br>Amt<th> G Total
<?
    $taxres=mysqli_query($db,"select tax from taxmaster where type='$taxtype'");
    $taxrow=mysqli_fetch_row($taxres);
    $tax=$taxrow[0];
$i=1;
$result=mysqli_query($db,"select * from purorder_spare where orderno='$pono' and closed='N' and brcode='$brcode'");
while($row=mysqli_fetch_array($result))
{
 $itmres=mysqli_query($db,"select * from partmaster where partcode='$row[0]'");
 $prtrow=mysqli_fetch_array($itmres);

 $prres=mysqli_query($db,"select * from spareprmast where partcode='$row[0]' order by effectfrom desc");
 $prrow=mysqli_fetch_array($prres);
 if(mysqli_num_rows($prres)<=0)
 {
  print "<tr><td colspan=10 align=center><font color=red>$row[0] - Price could not be found</font></td></tr>";
 return;

}
 print "\n<tr bgcolor='white'; onMouseOver=bgColor='c1d7bb'; onMouseOut=bgColor='white'>\n<td>".$i."</td><td><input type=text name='partcode[]' value='$row[0]' size=10></td><td>";
print "<textarea name='partname[]' rows=1>$row[1]</textarea></td>";
print "<td align=center>".($row[qty]-$row[sup_qty])."</td>";
 print "\n<td align=center><input type=text name='supqty[]' class=right id='$i' size=4 onChange=calc($i) value='0'></td>";
 print "<td align=center><input type=text name='foc[]' size=4 value='0'></td>";

 print "<td align=center><input class=right type=text  name='price[]' id='price$i' size=8 onChange=calc($i) value='$prrow[lstprice]'></td>";
 print "<td align=center><input class=right type=text  name='asval[]' id='asval$i' size=8 onChange=calc($i) value='0'></td>";
 print "<td align=center><input class=right type=text  name='cenvat[]' id='cenvat$i' size=8 onChange=calc($i) value='0'></td>";
 print "<td align=center><input class=right type=text  name='cesscenvat[]' id='cesscenvat$i' size=8 onChange=calc($i) value='0'></td>";
 print "<td align=center><input class=right type=text  name='hecesscenvat[]' id='hecesscenvat$i' size=8 onChange=calc($i) value='0'></td>";
 print "<td align=center><input class=right type=text   name='total[]' id='total$i' size=5 value='0' onChange=calc($i)></td>";
if($taxtype=='CST')
 print "<td align=center><input class=right type=text   name='tax[]' id='tax$i' size=3 value='2' onChange=calc($i)></td>";
else
 print "<td align=center><input class=right type=text   name='tax[]' id='tax$i' size=3 value='14.5' onChange=calc($i)></td>";

 print "<td align=center><input class=right type=text   name='taxamt[]' id='taxamt$i' size=4 value='0' ></td>";
if($taxtype=='CST')
 print "<td align=center><input class=right type=text   name='cessp[]' id='cessp$i' size=2 onChange=calc($i) value=0></td>";
else
 print "<td align=center><input class=right type=text   name='cessp[]' id='cessp$i' size=2 onChange=calc($i) value=0></td>";
 
print "<td align=center><input class=right type=text   name='cessamt[]' id='cessamt$i' size=8  value=0></td>";

 print "<td align=center><input class=right type=text   name='tot[]' id='tot$i' size=12></td></tr>";
$i++;
}
print "\n<tr><td colspan=4> <center><b>Total </td><td align=center><input type=text readonly name='totsupqty[]' id='totsupqty' size=8></td>";
print "\n<td colspan=2 align=center></td><td align=center>";
print "<input type=text  readonly name='ttcess' class=right id='ttcess' size=8></td>";
print "\n<td colspan=3 align=center></td><td align=center>";
print "<input type=text readonly name ='totbtx' class=right id='totbtx' size=8></td>";
print "\n<td align=center></td><td align=center>";
print "<input type=text  readonly name='tottx' class=right id='tottx' size=8></td>";
print "\n<td>Round</td>";
print "\n<td><input type=text name='round' id='round' size=8 value=0 onChange=cal()></td>";
print "<td><input type=text name='grtot'  readonly id='grtot' size=12></td>";
print "</tr>";

?>
<tr>
<th colspan=18><input type=submit value="Save" name="sv">
<input type=button value='Cancel' onClick=window.close()>
</table>
</form>
</td></tr></table>
<script>
gtt=0;
function calc(x)
{
	var ele='price'+x; 
	var disele='dis'+x;
	var hndele='hnd'+x;
	var taxele='tax'+x;
	var totele='tot'+x;
	var tx='tottx';
	var cesspele='cessp'+x;
 	var cessele='cessamt'+x;
 	var asvale='asval'+x;
 	var cenvate='cenvat'+x;
 	var cesscenvate='cesscenvat'+x;
 	var hecesscenvate='hecesscenvat'+x;
 	var totale='total'+x;
 	var txamtele='taxamt'+x;
 	var totsupq='totsupqty';
 	var totbtxx='totbtx';
 	var rnd='round';
	var tx=parseFloat(document.getElementById(taxele).value);
	var masvale = parseFloat(document.getElementById(asvale).value);
	var mdisc_per = parseFloat(document.f2.discount.value);
	var mhandling1 = parseFloat(document.f2.handling.value);
	<?
        $taxtype = $_REQUEST['taxtype'];
	if($taxtype=='CST')
	{
	?>
     		var masvale1 = (masvale - (masvale * mhandling1/100))*parseFloat(document.getElementById(x).value);
		var amt  = parseFloat(document.getElementById(ele).value);
		var amt1 = (amt-(amt*mdisc_per/100))*parseFloat(document.getElementById(x).value);
	<?
	}
	else
	{
	?>
		var masvale1 = masvale*parseFloat(document.getElementById(x).value);
		var amt  = parseFloat(document.getElementById(ele).value);
		var amt1 = amt*parseFloat(document.getElementById(x).value);
	<?
	}
        ?>
		
	var cenvat= (masvale1*.7)*12.5/100;
	document.getElementById(cenvate).value=cenvat.toFixed(2);
//	var cesscenvat= parseFloat(document.getElementById(cenvate).value)*2/100;
	var cesscenvat= 0;
	document.getElementById(cesscenvate).value=cesscenvat.toFixed(2);
//	var hecesscenvat= parseFloat(document.getElementById(cenvate).value)*1/100;
	var hecesscenvat= 0;
	document.getElementById(hecesscenvate).value=hecesscenvat.toFixed(2);
      alert (amt1);
	var amt=amt1+cenvat+cesscenvat+hecesscenvat;
	document.getElementById(totale).value=amt.toFixed(2);
	var tx=parseFloat(document.getElementById(taxele).value);
	var tax=((amt)*tx)/100;
	document.getElementById(txamtele).value=tax.toFixed(2);
	var cessp1=document.getElementById(cesspele).value;
	var cessam=(cessp1*tax)/100;
	document.getElementById(cessele).value=cessam.toFixed(2);
	var cs1=cessam;
	var tot=(amt+tax+cs1);
	document.getElementById(totele).value=(amt+tax+cs1).toFixed(2);
	tot=0;
	for(j=1;j<<?print $i?>;j++)
	{
   		t=document.getElementById(j).value; 
   		if(t.length<=0) 
     			t=0;
   		tot=parseFloat(tot)+parseFloat(t);
	}
	totbtax=0;
	tottax1=0;
	gtt=0;
	cesst=0;
	for(j=1;j<<?print $i?>;j++)
 	{
 		var tx='taxamt'+j;
 		var tte='tot'+j; 
 		var btx='total'+j; 
 		var cesstt='cessamt'+j;
 		var rnd='round';
   		t=document.getElementById(tx).value; 
   		if(t.length<=0) 
     			t=0;
   		tottax1=parseFloat(tottax1)+parseFloat(t);
		cesst=parseFloat(cesst)+parseFloat(document.getElementById(cesstt).value);
		if(document.getElementById(tte).value.length>0)
    			gtt=parseFloat(gtt)+parseFloat(document.getElementById(tte).value);
	    	totbtax=parseFloat(totbtax)+parseFloat(document.getElementById(btx).value);
	}
	gtt=parseFloat(gtt)+parseFloat(document.getElementById(rnd).value);
	document.getElementById(totsupq).value=parseFloat(tot).toFixed(2);
	document.getElementById('tottx').value=parseFloat(tottax1).toFixed(2);
	document.getElementById('grtot').value=parseFloat(gtt).toFixed(2);
	document.getElementById('totbtx').value=parseFloat(totbtax).toFixed(2);
	document.getElementById('ttcess').value=parseFloat(cesst).toFixed(2);
	var rnd=document.getElementById(round).value;
}
function cal()
{
 	ttcess=parseFloat(document.f1.ttcess.value);
 	round=parseFloat(document.f1.round.value);
 	btax=parseFloat(document.f1.totbtx.value);
 	tax=parseFloat(document.f1.tottx.value);
	document.f1.grtot.value=parseFloat((round+btax+tax+ttcess).toFixed(2));
}
</script>

