<?
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'];
$untprc=$_REQUEST['untprc'];
$itemvl=$_REQUEST['itemvl'];
$qty=$_REQUEST['qty'];
$stafdis=$_REQUEST['stafdis'];
$cus=$_REQUEST['custnme'];
$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') or ($itmtp=='S') or ($itmtp=='C')  or ($itmtp=='N'))
	{
		$upr=$untprc-($untprc*$itemvl/100);
		$itmrdct=$itemvl;
		$itmrdctvl=round(($untprc*$itemvl/100)*$qty,2);

	}
 else
	{
		$upr=$untprc;
	}      
	$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);


			//$grprdvl=$nrow[2]-$stfdisvl;
			//$strbtvl=$grprdvl*$strbt/100;
			//$strbtvl=round(($grprdvl*$strbt/100)*$qty,2);
				//$itmrdctvl=round(($grprdvl*$itmrdct/100)*$qty,2);
			//$kvicdisvl=round(($grprdvl*$kvicdis/100)*$qty,2);
			//$kvsplrdtvl=round(($grprdvl*$kvsplrdt/100)*$qty,2);


			//$itmrdctvl=$grprdvl*$itmrdct/100;
			//$kvicdisvl=$grprdvl*$kvicdis/100;
			//$kvsplrdtvl=$grprdvl*$kvsplrdt/100;
			$totexamt=$strbtvl+$kvicdisvl+$kvsplrdtvl;
                        
			$bfrtxamt=$tot-$stfdisvl-$totexamt;
			$bfrtxamt=round($bfrtxamt,2);
			$txprcvl=$bfrtxamt*$txprc/100;
			$txprcvl=round($txprcvl,2);
			$totnw=$bfrtxamt+$txprcvl;
			$totnw=round($totnw,2);
		}
	else
		{
			if($stafdis>0)
				{

					$stfqry="select jvno from prod_invoice where customer ='$cus' and brcode='$brcode'";
					$stfrs=mysqli_query($db,$stfqry);
					$mdisc_amt=0;
					while($stfrow=mysqli_fetch_array($stfrs))
						{
   							$dsgv=mysqli_query($db,"select sum(dramt) from journal2 where partic like 'Stffdiscount%' and jvno='$stfrow[0]' and brcode='$brcode'");
   							$disar=mysqli_fetch_row($dsgv);
   							$mdisc_amt=$disar[0];
						}



					$khquery="select cramt from khadistaff where brcode='$brcode' and cname='$cus'";
                                        $khres=mysqli_query($db,$khquery);
                                        $khrow=mysqli_fetch_array($khres);
                                        $crtotvl=$khrow[0];
                                        $crqry="select sum(total) from prod_invoice where brcode='$brcode' and customer='$cus' and staffdisc>0";
                                        $crres=mysqli_query($db,$crqry);
                                        $crrow=mysqli_fetch_array($crres);
                                        $totcrbl=$crrow[0]+$tot;
                                        $tot1=$tot-$stfdisvl;
					$tot1=round($tot-$stfdisvl,2);
					$strbtvl=round($tot1*$strbt/100,2);
					//$itmrdctvl=round($tot1*$itmrdct/100,2);
					$kvicdisvl=round($tot1*$kvicdis/100,2);
					$kvsplrdtvl=round($tot1*$kvsplrdt/100,2);
					$totexamt=$strbtvl+$kvicdisvl+$kvsplrdtvl;
					$bfrtxamt=$tot1-$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);
				}
		}


	?>
	<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;?>'>
        <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;?>'>
	<input type='hidden' name='stf_amt' id='stf_amt' value='<? echo $mdisc_amt;?>'>

