<?
session_start();
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='inde.php'; </script>";
 return;
}
//$br=$_SESSION['branch'];
$part=$_REQUEST['part'];
$itmtp=$_REQUEST['itmtp'];
$itemvl=$_REQUEST['itemvl'];
$stafdis=$_REQUEST['stafdis'];
$qty=$_REQUEST['qty'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}

$qry=mysqli_query($db,"select * from spareprmast where partcode='$part'");
$row=mysqli_fetch_array($qry);
$nqry=mysqli_query($db,"select gname,rbtbl,rbtvl,reduction from partmaster where partcode='$part'");
$nrow=mysqli_fetch_array($nqry);
$sql="select * from groupmaster where gname='$nrow[0]'";
$rs=mysqli_query($db,$sql);
$rw=mysqli_fetch_array($rs);
if($rw[rebt_aply]=='1')
{
$strbt=$rw[stat_rbt];
}
else
{
$strbt=0;
}
if($rw[redct_aply]=='1')
{
$itmrdct=$rw[reduct];
}
else
{
$itmrdct=0;
}
if($rw[kvic_aply]=='1')
{
$kvicdis=$rw[grp_kvic];
}
else
{
$kvicdis=0;
}
if($rw[kvspl_aply]=='1')
{
$kvsplrdt=$rw[grp_kvicspl];
}
else
{
$kvsplrdt=0;
}
if($rw[tax_aply]=='1')
{
$txprc=$rw[grp_tax];
}
else
{
$txprc=0;
}	
if($itmtp=='D')
{
$upr=$row[4]-($row[4]*$itemvl/100);
}
 else
{
$upr=$row[4];
}      
	
	$tot=($upr*$qty);
	if($stafdis>0)
		{
		$stfdisvl=$tot*$stafdis/100;
		}
		else
		{
		$stfdisvl=0;
		}
	if($nrow[1]=='Yes')
		{
			$strbtvl=$nrow[2]*$strbt/100;
			$itmrdctvl=$nrow[2]*$itmrdct/100;
			$kvicdisvl=$nrow[2]*$kvicdis/100;
			$kvsplrdtvl=$nrow[2]*$kvsplrdt/100;
			$totexamt=$strbtvl+$itmrdctvl+$kvicdisvl+$kvsplrdtvl+$stfdisvl;
			$bfrtxamt=$tot-$totexamt;
			$txprcvl=$bfrtxamt*$txprc/100;
			$txprcvl=round($txprcvl,2);
			$totnw=$bfrtxamt+$txprcvl;
			$totnw=round($totnw,2);
		}
	else
		{
			if($stafdis>0)
				{
					$tot=$tot-$stfdisvl;
					$strbtvl=$tot*$strbt/100;
					$itmrdctvl=$tot*$itmrdct/100;
					$kvicdisvl=$tot*$kvicdis/100;
					$kvsplrdtvl=$tot*$kvsplrdt/100;
					$totexamt=$strbtvl+$itmrdctvl+$kvicdisvl+$kvsplrdtvl;
					$bfrtxamt=$tot-$totexamt;
					$txprcvl=$bfrtxamt*$txprc/100;
					$txprcvl=round($txprcvl,2);
					$totnw=$bfrtxamt+$txprcvl;
					$totnw=round($totnw,2);
				}
			else
				{
					$strbtvl=$tot*$strbt/100;
					$itmrdctvl=$tot*$itmrdct/100;
					$kvicdisvl=$tot*$kvicdis/100;
					$kvsplrdtvl=$tot*$kvsplrdt/100;
					$totexamt=$strbtvl+$itmrdctvl+$kvicdisvl+$kvsplrdtvl+$stfdisvl;
					$bfrtxamt=$tot-$totexamt;
					$txprcvl=$bfrtxamt*$txprc/100;
					$txprcvl=round($txprcvl,2);
					$totnw=$bfrtxamt+$txprcvl;
					$totnw=round($totnw,2);
				}
		}
	
	?>
	<input type='hidden' name='totalamt' id='totalamt' value='<? print $tot;?>'>
	<input type='hidden' name='untprice' id='untprice' value='<? print $upr;?>'>
	<input type='hidden' name='alertmsg' id='alertmsg' value='1'>
	<input type='hidden' name='redct_per' id='redct_per' value='<? echo $itmrdct;?>'>
       <input type='hidden' name='redct_val' id='redct_val' value='<? echo $itmrdctvl;?>'>
	<input type='hidden' name='strbt_val' id='strbt_val' value='<? echo $strbtvl;?>'>
	<input type='hidden' name='kvic_val' id='kvic_val' value='<? echo $kvicdisvl;?>'>
	<input type='hidden' name='kvicspl_val' id='kvicspl_val' value='<? echo $kvsplrdtvl;?>'>
	<input type='hidden' name='bfrtx_val' id='bfrtx_val' value='<? echo $bfrtxamt;?>'>
	<input type='hidden' name='tx_val' id='tx_val' value='<? echo $txprc;?>'>
	<input type='hidden' name='txprc_val' id='txprc_val' value='<? echo $txprcvl;?>'>
	<input type='hidden' name='totnw_val' id='totnw_val' value='<? echo $totnw;?>'>
	