<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='../index.html' </script>";
  return;
}
include("../opendb.php");
include("../test.php");
$ordres=mysqli_query($db,"select max(orderno) from q_spare where brcode='$brcode'");
$ordrow=mysqli_fetch_row($ordres);
$ordnumber=$ordrow[0]+1;

?>

<BASE TARGET=main22>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
	tr{background-color:white}
	th{background-color:steelblue;color:white}
</style>
<?
$tbl="sparetmp".$brcode;
//include("calcsparestock.php");
?>

<script>
var pna=[];
var qty=[];
var hand=[];
var price=[];
var disc=[];

function validqty()
{
if(parseFloat(document.f1.qty.value)<parseFloat(document.f1.sqty.value))
{
alert('Insufficient Quantity');
document.f1.sqty.value=0;
document.f1.sqty.focus();
return;
}
}
function getname()
{
 if(document.f1.partcode.selectedIndex<=0)
 {
 document.f1.prtname.value="";
 document.f1.price.value="";
 document.f1.discount.value="";
 document.f1.hand.value="";
 }
 else{
 document.f1.prtname.value=pna[document.f1.partcode.selectedIndex];
 document.f1.price.value=price[document.f1.partcode.selectedIndex];
 document.f1.discount.value=disc[document.f1.partcode.selectedIndex];
 document.f1.hand.value=hand[document.f1.partcode.selectedIndex];
 if(parseFloat(qty[document.f1.partcode.selectedIndex])>=0)
  document.f1.qty.value=qty[document.f1.partcode.selectedIndex];
 else
  document.f1.qty.value=0;

 var act=parseFloat(document.f1.price.value)*parseFloat(document.f1.sqty.value);
 var act1=act*parseFloat(document.f1.discount.value)/100;

 document.f1.aprice.value=parseFloat(act-act1).toFixed(2);
 var act2=parseFloat(document.f1.aprice.value);
  var hnd=parseFloat(act2)*document.f1.hand.value/100;
  var cst=parseFloat(act2)*document.f1.cst.value/100;
  var gtot=parseFloat(act2+cst+hnd).toFixed(2);
  document.f1.tot.value=gtot;
 }
}
function valid()
{
 if(document.f1.partcode.selectedIndex<=0)
 {
  alert('Invalid Item code');
  return false;
 }
if(parseFloat(document.f1.sqty.value)<=0)
{
alert('Invalid Quantity');
document.f1.sqty.value=0;
document.f1.sqty.focus();
return false;
}
/*
var x=document.getElementById("mselect")
 document.f1.partcode1.value=x.options[x.selectedIndex].text
x.remove(x.selectedIndex)
*/
}
</script>
<body bgcolor='ivory'>
<center>
<form name='f1' action='qtemp.php' onSubmit='return valid()'>
<input type=hidden name='partcode1'>
<table>
<th colspan=10><h3>SPARE INVOICE ORDER - (<?print $ordnumber?>)</h3></th>
<tr>
<td>Part Code</td><td><select name='partcode' id='mselect' onChange=getname();><option></option>
<?
$i=1;
$re=mysqli_query($db,"select * from partmaster order by partcode");
while($row=mysqli_fetch_array($re))
{
 print "<option value='$row[0]'>$row[0]</option>";

$stkre=mysqli_query($db,"select * from $tbl where partcode='$row[0]'");
while($stkrow=mysqli_fetch_array($stkre))
{
 if($stkrow[supqty]>0)
  print "\n<script> qty[$i]='$stkrow[supqty]';</script>";
 else
  print "\n<script> qty[$i]='0';</script>";

}

  $disres=mysqli_query($db,"select lstprice,discount from spareprmast where partcode='$row[0]' order by effectfrom desc")or die('errr'.mysqli_error($db));
   $disrow=mysqli_fetch_array($disres);
 print "<script>pna[$i]='$row[partname]'; price[$i]='$disrow[0]'; disc[$i]='0'; </script>";

$disres1=mysqli_query($db,"select hand_charge from partmaster where partcode='$row[0]'");
   $disrow1=mysqli_fetch_array($disres1);
print "<script>hand[$i]='$disrow1[0]';</script>";
 $i++;
}
?>
</select></td><td>Part Name </td><td COLSPAN=3><input type=text readonly  name='prtname' size=30></td></tr>
<tr><td>Ex Qty</td><td><input type=text name='qty' size=6 readonly> Oqty <input type=text name='sqty' size=6 onChange='getname()'></td>
  <td>Price </td><td><input type=text readonly name='price' size=10></td>
  <td>Disc% </td><td><input type=text  name='discount' size=6 onChange='getname()'> 
   Act Price <input type=text name='aprice' size=10>
</td></tr>
<tr><td>Hand Chrg%</td><td><input type=text readonly  name='hand' size=6></td>
<?
 $cstres=mysqli_query($db,"select tax from taxmaster where type='Local'");
  $cstrow=mysqli_fetch_row($cstres);
?>
<td>VAT%</td><td><input type=text name='cst'  readonly size=6 value='<?print $cstrow[0]?>'></td>
<td>Total</td><td><input type=text name='tot' readonly  size=16></td></tr>
<tr><td colspan=6 align=center bgcolor='steelblue'><input type=submit value='Add' name='add'></td></tr>
</table>
</form>







<?
/*function stk()
{
mysqli_query($db,"CREATE TABLE $tbl (partcode varchar(200),partname varchar(200),supqty double(10,2))");  
mysqli_query($db,"delete from $tbl");

$sq="select partcode,partname,sum(supqty) from sparepurchase where brcode='$brcode' group by  partcode";
$re=mysqli_query($db,$sq);


while($res=mysqli_fetch_array($re))
{
  $sql="insert into $tbl values('$res[0]','$res[1]','$res[2]')"; 
  mysqli_query($db,$sql);
}

$sqq="select itemcode,itemname,sum(qty) from spareopstock where brcode='$brcode' group by  itemcode";
$ree=mysqli_query($db,$sqq);

while($ress=mysqli_fetch_array($ree))
{
  $rr=mysqli_query($db,"select * from $tbl where partcode='$ress[itemcode]'");
  if(mysqli_num_rows($rr)>0)
  {
	$inrow=mysqli_fetch_array($rr);
    	$sql="update $tbl set supqty=(supqty+".$ress[2].") where partcode='$inrow[partcode]'";
	mysqli_query($db,$sql);
  }
  else
   	{
	$sql="insert into $tbl values('$ress[0]','$ress[1]','$ress[2]')";
	mysqli_query($db,$sql);
   	}
}


$sqq="select partcode,partname,sum(qty) from salesreturn where brcode='$brcode' group by  partcode";
$ree=mysqli_query($db,$sqq);

while($ress=mysqli_fetch_array($ree))
{
  $rr=mysqli_query($db,"select * from $tbl where partcode='$ress[partcode]'");
  if(mysqli_num_rows($rr)>0)
  {
	$inrow=mysqli_fetch_array($rr);
    	$sql="update $tbl set supqty=(supqty+".$ress[2].") where partcode='$inrow[partcode]'";
	mysqli_query($db,$sql);
  }
  else
   	{
	$sql="insert into $tbl values('$ress[0]','$ress[1]','$ress[2]')";
	mysqli_query($db,$sql);
   	}
}



$sq1="select partcode,partname,sum(qty) from sparetransin where brcode='$brcode' group by  partcode";
$re1=mysqli_query($db,$sq1);

while($res1=mysqli_fetch_array($re1))
{
  $rr=mysqli_query($db,"select * from $tbl where partcode='$res1[partcode]'");
  if(mysqli_num_rows($rr)>0)
  {
    $inrow=mysqli_fetch_array($rr);
	$sql="update $tbl set supqty=(supqty+".$res1[2].") where partcode='$inrow[partcode]'";
	mysqli_query($db,$sql);
  }
  else
   	{
	$sql="insert into $tbl values('$res1[0]','$res1[1]','$res1[2]')";
	mysqli_query($db,$sql);
   	}
}


$sq2="select partcode,partname,sum(qty) from sparetransout where brcode='$brcode' group by  partcode";

$re2=mysqli_query($db,$sq2)or die('Err:'.mysqli_error($db));


while($res2=mysqli_fetch_array($re2))
{
 
$sql="update $tbl set supqty=(supqty-".$res2[2].") where partcode='$res2[partcode]'";

mysqli_query($db,$sql)or die('err'.mysqli_error($db));
}


$sq2="select partcode,partname,sum(qty) from spareinvoice where brcode='$brcode' group by  partcode";

$re2=mysqli_query($db,$sq2)or die('Err:'.mysqli_error($db));


while($res2=mysqli_fetch_array($re2))
{
 
$sql="update $tbl set supqty=(supqty-".$res2[2].") where partcode='$res2[partcode]'";

mysqli_query($db,$sql)or die('err'.mysqli_error($db));
}


$sq2="select partcode,partname,sum(retqty) from sparepurreturn where brcode='$brcode' group by  partcode";

$re2=mysqli_query($db,$sq2)or die('Err:'.mysqli_error($db));


while($res2=mysqli_fetch_array($re2))
{
 
$sql="update $tbl set supqty=(supqty-".$res2[2].") where partcode='$res2[partcode]'";

mysqli_query($db,$sql)or die('err'.mysqli_error($db));
}
} // end function*/
?>

