<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 	print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 	return;
}
?>
<script>
 //alert('Cash will be posted automatically');
</script>
<?
include("opendb.php");
//include("../test.php");
include("exqty.php");
$tbl="sparetmp".$brcode;
$invdate=$_REQUEST['invdate'];
$invno=$_REQUEST['invno'];
$re13=mysqli_query($db,"select max(invdate) from spareinvoice where brcode = '$brcode' ")or die(mysqli_error($db));
$srow=mysqli_fetch_array($re13);
if ($user=='antonyc' || $user=='antony' ||$user=='antonye' ||$user=='antonyo') 
{
	print 'Ok';
}
else
{
	if ($srow[0] > $invdate)
	{
	  	print "<script> alert('Date less than last entry date'); </script>";
	  	return false;
	}
}
$partcode =$_REQUEST['partcode'];
$partname =$_REQUEST['partname'];
$i=0;
foreach($partcode as $a)
{
 	$partcode[$i]=$a; 
 	$i++;
}
$i=0;
foreach($partname as $a)
{
 	$partname1[$i]=$a; 
 	$i++;
}
$supqty =$_REQUEST['supqty'];
$ref=$_REQUEST['ref'];
$i=0;
foreach($supqty as $a)
{
 	$supqty1[$i]=$a; 
 	$i++;
}
if($ref=='' && $brcode == 'PSN980')
{
  	print "<script> alert('Invalid E-Focus Invoice No.'); </script>";
  	return false;
}
$disc =$_REQUEST['disc'];
$price=$_REQUEST['price'];
$mrp=$_REQUEST['mrp'];
$aprice=$_REQUEST['aprice'];
$i=0;
foreach($price as $a)
{
 	$price[$i]=$a; 
 	$i++;
}
$mno = $i;
$i=0;
foreach($mrp as $a)
{
 	$mrp[$i]=$a; 
 	$i++;
}
$i=0;
foreach($disc as $a)
{
 	$disc[$i]=$a; 
 	$i++;
}
$i=0;
foreach($aprice as $a)
{
 	$aprice[$i]=$a; 
	$tamt+=$aprice[$i];
 	$i++;
}
$hand=$_REQUEST['hand'];
$tax =$_REQUEST['tax'];
$i=0;
foreach($hand as $a)
{
 	$hand[$i]=$a; 
 	$i++;
}

$i=0;
foreach($tax as $a)
{
 	$tax[$i]=$a; 
 	$i++;
}
$taxamt =$_REQUEST['taxamt'];
$tot =$_REQUEST['tot'];
$i=0;
foreach($taxamt as $a)
{
 	$taxamt[$i]=$a; 
	$txamt+=$taxamt[$i];
 	$i++;
}

$i=0;
foreach($tot as $a)
{
 	$tot[$i]=$a; 
 	$i++;
}
$cessp =$_REQUEST['cessp'];
$cessamt=$_REQUEST['cessamt'];


$i=0;
foreach($cessp as $a)
{
 	$cessp[$i]=$a; 
 	$i++;
}
$i=0;
foreach($cessamt as $a)
{
 	$cessamt[$i]=$a;
	$csamt+=$cessamt[$i];
 	$i++;
}
$tot5=$tamt+$txamt+$csamt;
$paytype=$_REQUEST['paytype'];
$customer=$_REQUEST['customer'];
$invtype=$_REQUEST['invtype'];
$saleordno=$_REQUEST['saleordno'];
$addr=$_REQUEST['addr'];
$tin=$_REQUEST['tin'];
$vatorcst=$_REQUEST['vatorcst'];
$msno=$_REQUEST['msno'];
$engineer=$_REQUEST['engineer'];
if($paytype=='Credit')
{
	$re=mysqli_query($db,"select sum(op) from ledger where grp='Sundry Debtors' and led='$customer'")or die(mysqli_error($db));
	$srow=mysqli_fetch_array($re);
	$opbal=$srow[0];

 	$sql="select sum(dramt)-sum(cramt) from journal2 where partic='$customer'";
 	$balres=mysqli_query($db,$sql)or die('err0'.mysqli_error($db));
  	$balrow=mysqli_fetch_row($balres)or die('err0'.mysqli_error($db));
 	$balance=$opbal+$balrow[0];

	$tot1=$tot5+$balance;
  	$chstkre1=mysqli_query($db,"select cramt,crdays from customer where acname='$customer'");
  	$chstkrow1=mysqli_fetch_array($chstkre1);
   	if($tot1>$chstkrow1[0])
    	{
  //    print "exceeds limit ".$tot1;
    //  return;
    	}

	$sql="select sum(dramt) from journal2 where partic='$customer' and (to_days(curdate())-to_days(date))<=$chstkrow1[1]";
 	$res1=mysqli_query($db,$sql);
  	$row=mysqli_fetch_array($res1);
   	$r3=$row[0];
	$r2=$r3;
	if($opbal>0)
	{
		$r3=$r3+$opbal;
	}
	if ($r3>0)
	{
		if ($balance>$r3)
		{ 
		//	print  "Sorry, I can't bill,Payment outstanding ".$balance;
		//	return;
		}
	}

}
/* 
//include ("calcsparestock.php");
for($j1=0;$j1<$i;$j1++)
{
	if($supqty1[$j1]>0)
	{
		$availqty=exqty1($partcode[$j1],$brcode,$user);
		if($supqty1[$j1]>$availqty)
    		{
      			print "Insufficient Quantity for".$partcode[$j1];
      			print "<script> alert('Insufficient Quantity');history.back(1); </script>";
      			return;
    		}
 $chstkre=mysqli_query($db,"select * from $tbl where partcode='$partcode[$j1]'");
  		$chstkrow=mysqli_fetch_array($chstkre);
  		if($supqty1[$j1]>$chstkrow[supqty])
    		{
      			print "Insufficient Quantity ".$partcode[$j1];
      			return;
    		}

	}
}
*/


$res=mysqli_query($db,"select * from spareinvoice where invno='$invno' and brcode='$brcode'");
$row=mysqli_fetch_row($res);
$res_jv=$row[25];
if($res_jv>0)
{
mysqli_query($db,"delete from journal2 where brcode='$brcode' and jvno='$res_jv'");
}

mysqli_query($db,"delete from spareinvoice where brcode='$brcode' and invno='$invno'");
$jvres=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
					$jvrow=mysqli_fetch_row($jvres);
					if($jvrow[0]>0)
 					$jvno=($jvrow[0]+1);
					else
 					$jvno=1;



$i = $mno;
for($j=0;$j<$i;$j++)
{
	if($supqty1[$j]>0)
	{
         $sql="insert into spareinvoice values('$invno','$invtype','$invdate','$invdate','$saleordno', '$customer','$addr','$tin','$partcode[$j]','$partname1[$j]','$supqty1[$j]','$price[$j]','$disc[$j]','$aprice[$j]','$hand[$j]','$tax[$j]', '$taxamt[$j]','$tot[$j]', '$brcode','$paytype', '0','0','$cessp[$j]','$cessamt[$j]', '$vatorcst', '$jvno','$msno','$ref', '$engineer')";        

 	mysqli_query($db,$sql)or die('Invoice Err:'.mysqli_error($db));
	
        $sql="update saleorder_spare set partcode='$partcode[$j]',partname='$partname1[$j]',qty='$supqty1[$j]',date='$invdate', sup_qty=$supqty1[$j],sup_purno='$invno',sup_date='$invdate',trans_type='$invtype',closed='Y' where orderno='$saleordno' and brcode='$brcode'";
		mysqli_query($db,$sql)or die('err'.mysqli_error($db));
	}
		$sql="update spareprmast set mrp = '$mrp[$j]' where partcode = '$partcode[$j]' ";
 		mysqli_query($db,$sql)or die('MRP Updation Error:'.mysqli_error($db));
}
//********** chk ledger
$chre=mysqli_query($db,"select * from ledger where led='$customer' and brcode='$brcode'");
if(mysqli_num_rows($chre)<=0)
{
	$code=substr($customer,0,3).date(ymds);
	$code=str_replace(' ','-',$code);
	$sql="insert into ledger values('$customer','Sundry Debtors','0','Dr','$code','$brcode')";
	mysqli_query($db,$sql);
}
//********************* a/c posting

if($vatorcst=='VAT')
{
		$num1='';
		if ($brcode == 'PSN638')
		{
			$num1= '[PSN] S-'.$invno;
		}
		else if ($brcode == 'PSN168')
		{
			$num1= 'BHMDCC'.$invno;
		}
		else
			$num1= $invno;

 	$taxdrres=mysqli_query($db,"select sum(aprice),tax,sum(hand) from spareinvoice where invno='$invno' and vatorcst='VAT' and brcode='$brcode' group by tax")or die('Led Post Err3'.mysqli_error($db));

  	while($taxdrrow=mysqli_fetch_array($taxdrres))
  	{
    		$ledger='Spare invoice '.$taxdrrow[tax];  
		if ($brcode=='PSN980' || $brcode=='PSN470' || $brcode=='PSN606')
		{
	    		$sql="insert into journal2 values('$invdate','$jvno','Cr','$ledger','0','$taxdrrow[0]','Spare Invoice : $ref','$brcode','Spare Invoice','Sale Order','$saleordno','0','0')";

	     		mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
		}

		else
		{
		    	$sql="insert into journal2 values('$invdate','$jvno','Cr','$ledger','0','$taxdrrow[0]','Spare Invoice : $num1','$brcode','Spare Invoice','Sale Order','$saleordno','0','0')";
	     	mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));

		}
		if($taxdrrow[2]>0)
		{
		    	$sql="insert into journal2 values('$invdate','$jvno','Cr','Freight Charges','0','$taxdrrow[2]','Spare Invoice : $num1','$brcode','Spare Invoice','Sale Order','$saleordno','0','0')";
     	        mysqli_query($db,$sql) or die('Led Post Err3'.mysqli_error($db));
		}

  	}
 	$taxres=mysqli_query($db,"select tax, sum(taxamt) from spareinvoice where invno='$invno' and vatorcst='VAT' and brcode='$brcode' group by tax")or die('Led Post Err3'.mysqli_error($db));
  	while($taxrow=mysqli_fetch_array($taxres))
  	{
    		$ledger='VAT Collected '.$taxrow[tax];  
    		if ($brcode=='PSN980' || $brcode=='PSN470' || $brcode=='PSN606')
    			$sql="insert into journal2 values('$invdate','$jvno','Cr','$ledger','0','$taxrow[1]','Spare Invoice : $ref','$brcode','Spare Invoice','Sale Order','$saleordno','0','0')";
    		else
    			$sql="insert into journal2 values('$invdate','$jvno','Cr','$ledger','0','$taxrow[1]','Spare Invoice : $num1','$brcode','Spare Invoice','Sale Order','$saleordno','0','0')";
     		mysqli_query($db,$sql) or die('Led Post Err4'.mysqli_error($db));
  	}
//*************** cess

 	$taxcessres=mysqli_query($db,"select sum(cessamt),cessp from spareinvoice where invno='$invno' and vatorcst='VAT' and brcode='$brcode' group by invno")or die('cess err.'.mysqli_error($db));
  	while($taxcessrow=mysqli_fetch_array($taxcessres))
  	{
    		$ledger='Output Cess '.$taxcessrow[1].'%';
    		if ($brcode=='PSN980' || $brcode=='PSN470' || $brcode=='PSN606')
	 		$sql="insert into journal2 values('$invdate','$jvno','Cr','$ledger','0','$taxcessrow[0]','Spare Invoice : $ref','$brcode','Spare Invoice','Sale Order','$saleordno','0','0')";
    		else
	 		$sql="insert into journal2 values('$invdate','$jvno','Cr','$ledger','0','$taxcessrow[0]','Spare Invoice : $num1','$brcode','Spare Invoice','Sale Order','$saleordno','0','0')";
		mysqli_query($db,$sql) or die('Led Post Err6'.mysqli_error($db));
  	}

//************************
	$sq="select sum(aprice),sum(hand),sum(taxamt) from spareinvoice where invno='$invno' and vatorcst='VAT' and brcode='$brcode'";
 	$taxcrres=mysqli_query($db,$sq)or die('Led Post Err3'.mysqli_error($db));
  	while($taxcrrow=mysqli_fetch_array($taxcrres))
  	{
   		if ($brcode=='PSN980' || $brcode=='PSN470' || $brcode=='PSN606')
       		$sql="insert into journal2 values('$invdate','$jvno','Dr','$customer','".round($taxcrrow[0]+$taxcrrow[1]+$taxcrrow[2])."','0','Spare Invoice : $ref','$brcode','Spare Invoice','Sale Order','$saleordno','0','0')";
   		else
       		$sql="insert into journal2 values('$invdate','$jvno','Dr','$customer','".round($taxcrrow[0]+$taxcrrow[1]+$taxcrrow[2])."','0','Spare Invoice : $num1','$brcode','Spare Invoice','Sale Order','$saleordno','0','0')";
		
                //print 'Rounding '.$sql;
     		mysqli_query($db,$sql) or die('Led Post Err3'.mysqli_error($db));

	} // end while

} // end if
else
{

//********************* CST
 $taxdrres=mysqli_query($db,"select sum(aprice+((aprice*hand)/100)),tax from spareinvoice where invno='$invno' and vatorcst='CST' and brcode='$brcode' group by tax")or die('Led Post Err3'.mysqli_error($db));
  while($taxdrrow=mysqli_fetch_array($taxdrres))
  {
    $ledger='Spare Invoice CST '.$taxdrrow[tax];  
    $sql="insert into journal2 values('$invdate','$jvno','Cr','$ledger','0','$taxdrrow[0]','Spare Invoice : $ref','$brcode','Spare Invoice','Sale Order','$saleordno','0','0')";
     mysqli_query($db,$sql) or die('Led Post Err4'.mysqli_error($db));
  }


 $taxres=mysqli_query($db,"select tax, sum(taxamt) from spareinvoice where invno='$invno' and vatorcst='CST' and brcode='$brcode' group by tax")or die('Led Post Err3'.mysqli_error($db));
  while($taxrow=mysqli_fetch_array($taxres))
  {
    $ledger='CST Collected '.$taxrow[tax];  
    $sql="insert into journal2 values('$invdate','$jvno','Cr','$ledger','0','$taxrow[1]','Spare Invoice : $ref','$brcode','Spare Invoice','Sale Order','$saleordno','0','0')";
     mysqli_query($db,$sql) or die('Led Post Err5'.mysqli_error($db));
  }
//************************
 $taxcrres=mysqli_query($db,"select sum(tot) from spareinvoice where invno='$invno' and vatorcst='CST' and brcode='$brcode'")or die('Led Post Err3'.mysqli_error($db));
  while($taxcrrow=mysqli_fetch_array($taxcrres))
  {
    $sql="insert into journal2 values('$invdate','$jvno','Dr','$customer','".round($taxcrrow[0])."','0','Spare Invoice : $ref','$brcode','Spare Invoice','Sale Order','$saleordno','0','0')";
     mysqli_query($db,$sql) or die('Led Post Err6'.mysqli_error($db));
  
/*  $rnd=round($taxcrrow[0]);
  $rnd2=round(($taxcrrow[0]-$rnd),2);
  if($rnd2<0)
  {
    $sql="insert into journal2 values('$invdate','$jvno','Dr','Round Off','".abs($rnd2)."', '0','Spare Invoice :   $ref','$brcode','Spare sale','invno','$invno','$invno','0')";
   mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error($db));
  }
  else if($rnd2>0)
   {
     $sql="insert into journal2 values('$invdate','$jvno','Cr','Round Off','0','".abs($rnd2)."','Spare Invoice : $ref','$brcode','Spare sale','invno','$invno','$invno','0')";
   mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error($db));
   }
*/
 } // end while
}// end else

$jvdifres=mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where jvno='$jvno' and brcode='$brcode'");
$jvdifrow=mysqli_fetch_row($jvdifres);
$dif=$jvdifrow[0];

if($dif<0)
{
 $xx=abs($dif);
$sql="insert into journal2 values('$invdate','$jvno','Dr','Round Off','".$xx."', '0','Spare Invoice : $ref','$brcode','Spare Invoice','S ORd no','$saleordno','$invno','0')";
mysqli_query($db,$sql) or die("J V Write error dif0:".mysqli_error($db));

}
else if($dif>0)
{
 $xx=abs($dif);
 $sql="insert into journal2 values('$invdate','$jvno','Cr','Round Off','0','".$xx."','Spare Invoice : $ref','$brcode','Spare Invoice','S ORd no','$saleordno','$invno','0')";
mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error($db));

}

$jdres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where jvno='$jvno' and brcode='$brcode'");
$jdiff=mysqli_fetch_row($jdres);


if($jdiff[0]!=$jdiff[1]||$jdiff[0]==0)
{
 print "<sctipt> alert('Error in posting. Please check the data now');</script>";
}

if($paytype=='Cash')
{
$rs=mysqli_query($db,"select max(crno) from cshrec where brcode='$brcode'");
$cr_row=mysqli_fetch_row($rs);
 if($cr_row[0]<=0)
   $crno=1;
 else   
   $crno=$cr_row[0]+1;

$jpostres=mysqli_query($db,"select sum(dramt) from journal2 where jvno='$jvno' and partic='$customer' and brcode='$brcode'");
$jpostrow=mysqli_fetch_row($jpostres);
$jvno=$jvno+1;
 $sql="insert into journal2 values('$invdate','$jvno','Dr','Cash','$jpostrow[0]','0','Spare Invoice of $customer(invno:$ref)','$brcode','CR','sp invoice','$invno','$crno','0')";
mysqli_query($db,$sql) or die("J V Write error jverr2:".mysqli_error($db));

 $sql="insert into journal2 values('$invdate','$jvno','Cr','$customer','0','$jpostrow[0]','Spare Invoice of $customer(invno:$ref)','$brcode','CR','sp invoice','$invno','$crno','0')";
mysqli_query($db,$sql) or die("J V Write error jverr3:".mysqli_error($db));

 $sql="insert into cshrec values('$invdate','$crno','Cr','$customer','0','$jpostrow[0]','Spare invoice (invno:$ref)','$brcode','$jvno','invno','$invno','0','0000-00-00')";
mysqli_query($db,$sql) or die("J V Write error csherr1:".mysqli_error($db));
print "<script> window.open('../accounts/payrec/prncshrec.php?no=$crno'); </script>";
}
//log_entry($brcode,$user,$REMOTE_ADDR,"spareinvoice:$invno jvno: $jvno");
print "<script>alert('Data Has Been Updated...'); document.location='invoicesale_editinv1.php'; </script>";
?>

