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

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

$result=mysqli_query($db,"select * from sparepurchase where jvno='$jvno' and brcode='$brcode'");
$row=mysqli_fetch_array($result);
?>
<title>Spare Purchase </title>
<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>
<form name="f2" action="updpurchase.php" method=post onSubmit="return valid()">
<input type=hidden name='purno' value='<?print $row[purno]?>'>
<input type=hidden name='taxtype' value='<?print $ttype?>'>
<input type=hidden name='jvno' value='<?print $jvno?>'>
<input type=hidden name='porderno' value='<?print $row[porderno]?>'>
<?
print "<table border=1 cellspacing=1 cellpadding=3 ><th colspan=2> SPARE PURCHASE</TH>";
print "<tr><td>Pur Order No </td><td>$row[porderno]</td></tr>";

print "<tr><td>Pur Order Date </td><td>".datetohtml($row[purdate])."</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='suplier'>";
print "<option>$row[suplier]</option>";
print "<option value='$tobr'>$tobr
</option></select></td></tr>";
print "<tr><td>Pur Bill No.</td><td><input type=text name='pbillno' value='$row[pbillno]'></td></tr>";
print "<tr><td>Pur Date</td><td><input type=text name='purdate' value='$row[purdate]'>
</td></tr>";
print "<tr><td>Entry Date</td><td><input type=text name='entrydate' value='$row[entrydate]'>
</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>Price <th>Disc% <th> A Price<th>Hand Chrge 
<?
if($taxtype=='CST')
   print "<th>CST%";
else
 print "<th>VAT%";
?>
<th>Tax Amt <th>Cess% <th>Cess <br>Amt<th> Total
<?
    $taxres=mysqli_query($db,"select tax from taxmaster where type='$taxtype'");
    $taxrow=mysqli_fetch_row($taxres);
    $tax=$taxrow[0];
$i=1;
$sq="select * from sparepurchase where jvno='$jvno' and brcode='$brcode'";
//print $sq;
$result1=mysqli_query($db,$sq);
while($row=mysqli_fetch_array($result1))
{
 $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[partcode]' size=10></td><td>";
print "<textarea name='partname[]' rows=1>$row[partname]</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(this.id) value='$row[supqty]'></td>";
 print "<td align=center><input type=text name='foc[]' size=4 value='$row[foc]'></td>";

 print "<td align=center><input class=right type=text  name='price[]' id='price$i' size=8 value='$row[price]' onChange=calc($i)></td>";
 print "<td align=center><input class=right type=text   name='disc[]' id='dis$i' size=5 value='$row[disc]' onChange=calc($i)></td>";
 print "<td align=center><input class=right type=text   name='aprice[]' id='ap$i' size=8 value='$row[aprice]'></td>";
 print "<td align=center><input class=right type=text   name='hand[]' id='hnd$i' size=2 value='$row[hand]'></td>";

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

 print "<td align=center><input class=right type=text   name='tot[]' id='tot$i' size=12 value='$row[tot]'></td></tr>";
$i++;
}
print "\n<tr><td colspan=4> <center><b>Total </td><td align=center><input type=text name='totsupqty[]' id='totsupqty' size=8></td>";
print "\n<td colspan=6 align=center></td><td align=center>";
print "<input type=text class=right id='tottx' size=8></td>";
print "<td></td><td></td><td><input type=text name='grtot' id='grtot' size=8></td>";
print "</tr>";

?>
<tr>
<th colspan=18><input type=submit value="Save" name="sv">
<input type=button value='Cancel' onClick=window.close()>
</table>
</form>

<script>
 gtt=0;
 function calc(x)
 {
 var ele='price'+x; 
 var disele='dis'+x;
 var actele='ap'+x;
 var hndele='hnd'+x;
 var taxele='tax'+x;
 var totele='tot'+x;
 var txamtele='taxamt'+x;
 var totsupq='totsupqty';
 var tx='tottx';

 var cesspele='cessp'+x;
 var cessele='cessamt'+x;

 var amt=parseFloat(document.getElementById(ele).value)*parseFloat(document.getElementById(x).value);
var hnd=amt*document.getElementById(hndele).value/100;

 var disc=(amt)*parseFloat(document.getElementById(disele).value)/100;

 var actamt=amt+hnd-disc;
//document.getElementById(disele).value=parseFloat(disc);

document.getElementById(actele).value=parseFloat(actamt).toFixed(2);


var tx=parseFloat(document.getElementById(taxele).value);
var tax=((actamt)*tx)/100;
document.getElementById(txamtele).value=tax.toFixed(2);
//document.getElementById(totele).value=(actamt+hnd+tax).toFixed(2);
//alert(tax);

var cessp1=document.getElementById(cesspele).value;
var cessam=(cessp1*tax)/100;
document.getElementById(cessele).value=cessam.toFixed(2);
var cs1=cessam;
document.getElementById(totele).value=(actamt+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);
}

tottax1=0;
gtt=0;
for(j=1;j<<?print $i?>;j++)
 {
 var tx='taxamt'+j;
 var tte='tot'+j; 
   t=document.getElementById(tx).value; 
   if(t.length<=0) 
     t=0;
   tottax1=parseFloat(tottax1)+parseFloat(t);
   if(document.getElementById(tte).value.length>0)
    gtt=parseFloat(gtt)+parseFloat(document.getElementById(tte).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);

}

function setDis()
{
 var disamt=document.f2.discount.value;
 var handamt=document.f2.handling.value;
for(j=1;j<<?print $i?>;j++)
 {
 var hndele='hnd'+j;
 var disele='dis'+j;
 document.getElementById(hndele).value=handamt;
 document.getElementById(disele).value=disamt;

 }
}
</script>

