<?
session_start();
include("../opendb.php");
include("../test.php");
//print 'tax type='.$taxtype;
$result=mysqli_query($db,"select * from purorder_spare where orderno='$pono' and brcode='$brcode' and closed='N'");
$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>
<center>
<form name='f2'>
<table bgcolor=silver>
<tr><td>Discount</td><td><input type=text name='discount' size=5></td>
    <td>Hand Charges</td><td><input type=text name='handling' size=5></td>
    <td><input type=button value='Set Value' onClick=setDis()></td>
</tr>
</table>
</form>
<br>
</center>
<form name="f1" action="svpurchase.php" method=post onSubmit="return valid()">
<input type=hidden name='porderno' value='<?print $pono?>'>
<input type=hidden name='taxtype' value='<?print $taxtype?>'>
<?
print "<table 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>";
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>";

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;
$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></td>";

 print "<td align=center><input class=right type=text  name='price[]' id='price$i' size=8 value='$prrow[lstprice]'></td>";
 print "<td align=center><input class=right type=text   name='disc[]' id='dis$i' size=5 value='0' onChange=calc($i)></td>";
 print "<td align=center><input class=right type=text   name='aprice[]' id='ap$i' size=8></td>";
 print "<td align=center><input class=right type=text   name='hand[]' id='hnd$i' size=2 value='0'></td>";
$txres=mysqli_query($db,"select tax from taxmaster where type='CST'");
$txrow=mysqli_fetch_row($txres);
 print "<td align=center><input class=right type=text   name='tax[]' id='tax$i' size=3 value='$tax' onChange=calc($i)></td>";

// print "<td align=center><input class=right type=text   name='tax[]' id='tax$i' size=3 value='$prtrow[tax_pur]' onChange=calc($i)></td>";
 print "<td align=center><input class=right type=text   name='taxamt[]' id='taxamt$i' size=4 value='0' onChange=calc($i)></td>";
 print "<td align=center><input class=right type=text   name='cessp[]' id='cessp$i' size=2 onChange=calc($i)></td>";
 print "<td align=center><input class=right type=text   name='cessamt[]' id='cessamt$i' size=8></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 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 disamt=document.f2.discount.value;
 var handamt=document.f2.handling.value;
 if(disamt.length<=0||handamt.length<=0||isNaN(disamt)||isNaN(handamt))
  {
   document.f2.discount.focus();
  alert('Invalid Discount');
  return false;
 }
 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>

