<?
session_start();
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$part=$_REQUEST['part'];
$itmtp=$_REQUEST['itmtp'];
$itemvl=$_REQUEST['itemvl'];
$stafdis=$_REQUEST['stafdis'];
$qty=$_REQUEST['qty'];
$invcno=$_REQUEST['invcno'];
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($itmtp=='D')
    {
        $stksl="select curstk_dam from sparestock where itemcode='$part' and brcode='$brcode'";
	$stksl1="select qty from prod_invoice where item_code='$part' and itemtyp='D' and invno='$invcno' and brcode='$brcode'";
    }
else
    {
        $stksl="select curstk from sparestock where itemcode='$part' and brcode='$brcode'";
	$stksl1="select qty from prod_invoice where item_code='$part' and itemtyp='N' and invno='$invcno' and brcode='$brcode'";
    }
  
$stkrs=mysqli_query($db,$stksl);
$stkrw=mysqli_fetch_array($stkrs);
$stkrs1=mysqli_query($db,$stksl1);
$stkrw1=mysqli_fetch_array($stkrs1);
$bilqty=$stkrw1[0];
$balqty=$stkrw[0]-$qty+$stkrw1[0];

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') or ($itmtp=='S') or ($itmtp=='C')   or (($itmtp=='N') and ($nrow[0]=='POLYSTER')))
{
$upr=$row[4]-($row[4]*$itemvl/100);
$itmrdctvl=round(($row[4]*$itemvl/100)*$qty,2);
}
 else
{
$upr=$row[4];
$itmrdctvl=0;
$itemvl=0;
}      
	
	$tot=($upr*$qty);
	if($stafdis>0)
		{
		$stfdisvl=$tot*$stafdis/100;
		}
		else
		{
		$stfdisvl=0;
		}
	if($nrow[1]=='Yes')
		{
                        if($stafdis>0)
                            {
                                $rebtvl=round(($nrow[2]-($nrow[2]*$stafdis/100)));
                                $stfdisvl=($nrow[2]*$stafdis/100)*$qty;

                            }
                        else
                            {
                                $rebtvl=$nrow[2];
                            }
			$strbtvl=round(($rebtvl*$strbt/100)*$qty,2);
			//$itmrdctvl=round(($tot*$itmrdct/100)*$qty,2);
			$kvicdisvl=round(($rebtvl*$kvicdis/100)*$qty,2);
			$kvsplrdtvl=round(($rebtvl*$kvsplrdt/100)*$qty,2);

                        //$strbtvl=round(($nrow[2]*$strbt/100)*$qty,2);
			//$itmrdctvl=round(($tot*$itmrdct/100)*$qty,2);
			//$kvicdisvl=round(($nrow[2]*$kvicdis/100)*$qty,2);
			//$kvsplrdtvl=round(($nrow[2]*$kvsplrdt/100)*$qty,2);
			$totexamt=$strbtvl+$kvicdisvl+$kvsplrdtvl+$stfdisvl;
			$bfrtxamt=$tot-$totexamt;
			$bfrtxamt=round($bfrtxamt,2);
			$txprcvl=$bfrtxamt*$txprc/100;
			$txprcvl=round($txprcvl,2);
			$totnw=$bfrtxamt+$txprcvl;
			$totnw=round($totnw,2);
		}
	else
		{
			if($stafdis>0)
				{
					$tot=round($tot-$stfdisvl,2);
					$strbtvl=round($tot*$strbt/100,2);
					//$itmrdctvl=round($tot*$itmrdct/100,2);
					$kvicdisvl=round($tot*$kvicdis/100,2);
					$kvsplrdtvl=round($tot*$kvsplrdt/100,2);
					$totexamt=$strbtvl+$kvicdisvl+$kvsplrdtvl;
					$bfrtxamt=$tot-$totexamt;
					$bfrtxamt=round($bfrtxamt,2);
					$txprcvl=$bfrtxamt*$txprc/100;
					$txprcvl=round($txprcvl,2);
					$totnw=$bfrtxamt+$txprcvl;
					$totnw=round($totnw,2);
				}
			else
				{
					$strbtvl=round($tot*$strbt/100,2);
					//$itmrdctvl=round($tot*$itmrdct/100,2);
					$kvicdisvl=round($tot*$kvicdis/100,2);
					$kvsplrdtvl=round($tot*$kvsplrdt/100,2);
					$totexamt=$strbtvl+$kvicdisvl+$kvsplrdtvl+$stfdisvl;
					$bfrtxamt=$tot-$totexamt;
					$bfrtxamt=round($bfrtxamt,2);
					$txprcvl=$bfrtxamt*$txprc/100;
					$txprcvl=round($txprcvl,2);
					$totnw=$bfrtxamt+$txprcvl;
					$totnw=round($totnw,2);
				}
		}
	if($balqty<0)
        {
            $msgvl='-1';
        }
 else {
        $msgvl='1';
        }
	?>
	<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='<? print $msgvl;?>'>
	<input type='hidden' name='redct_per' id='redct_per' value='<? echo $itemvl;?>'>
       <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;?>'>
	<input type='hidden' name='tot_crvl' id='tot_crvl' value='<? echo $crtotvl;?>'>
        <input type='hidden' name='tot_crbl' id='tot_crbl' value='<? echo $totcrbl;?>'>
	<input type='hidden' name='stfds_val' id='stfds_val' value='<? echo $stfdisvl;?>'>
