<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($user)<=0)
{
   print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
  return;
}
include_once('../opendb.php');
$savecount=$_REQUEST['savecount'];
$cus=$_REQUEST['cus'];
$invno=$_REQUEST['invno'];
$taxtype=$_REQUEST['taxtype'];
$purdate=$_REQUEST['supinvdt'];
$entrydate=$_REQUEST['invdt'];
$purno=$invno;
$sup=$_REQUEST['sup'];
if($savecount==0)
{
	if(($taxtype=='-select-')||($taxtype==''))
    {
    	echo "Data could not saved <br> Invalid Tax type";
    }
    if($sup=='-select-')
    {
    	echo "Error!! Please select Suppliername";
    }
	
  	else
  	{
  	    $partcode=$_REQUEST['partcode'];
        $partname=$_REQUEST['partname'];
        $qty=$_REQUEST['qty'];
        $unitprice=$_REQUEST['unitprice'];
        $befrtxamt=$_REQUEST['befrtxamt'];
        $vat=$_REQUEST['vat'];
        $taxamt=$_REQUEST['taxamt'];
        $tot=$_REQUEST['tot'];
        $sup=$_REQUEST['sup'];
        $pbillno=$_REQUEST['pbillno'];
        $handling=$_REQUEST['handling'];
        $grnno=$_REQUEST['grnno'];
        $itmtyp=$_REQUEST['itmtyp'];
 		$i=0;
		foreach($partcode as $a)
		{
 		$partcode[$i]=$a; 
 		$i++;
		}
		
		$i=0;
		foreach($partname as $a)
		{
 		$partname[$i]=$a; 
 		$i++;
		}
		$i=0;
		foreach($qty as $a)
		{
 		$qty[$i]=$a; 
 		$i++;
		}

		$i=0;
		foreach($unitprice as $a)
		{
 		$unitprice[$i]=$a; 
 		$i++;
		}

		$i=0;
		foreach($befrtxamt as $a)
		{
 		$befrtxamt[$i]=$a; 
 		$i++;
		}


		$i=0;
		foreach($vat as $a)
		{
 		$vat[$i]=$a; 
 		$i++;
		}

		$i=0;
		foreach($taxamt as $a)
		{
 		$taxamt[$i]=$a; 
 		$i++;
		}
		$i=0;
		foreach($tot as $a)
		{
 		$tot[$i]=$a; 
 		$i++;
		}
        $i=0;
        foreach($itmtyp as $a)
        {
            $itmtyp[$i]=$a; 
            $i++;
        }
	

		$flg=0;
		$spersonflg=0;
		$totflg=0;
		for($j=0;$j<$i;$j++)
		{
			$k=$j+1;
			if(($qty[$j]=='')||($qty[$j]=='0'))
			{
    			print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
 			    $flg++;
    		}
   			if(($tot[$j]=='')||($tot[$j]=='0'))
    		{
    			print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
     			$totflg++;
    		}
                    /*
                 	$sql="select partcode from partmaster where partcode='$partcode[$j]'";
                 	echo $sql; 
                	$rsq=mysqli_query($db,$sql);
                	$countnm=mysqli_num_rows($rsq);
                	if($countnm==0)
                	{
                           $spersonflg++;
                	}
                	*/
		}
		if($flg>0)
		{
  		echo "<small>Check the itemname/Qty of Red marked row(s)</br></small>";
 		}
		elseif($totflg>0)
 		{
  		echo "<small>Enter Total of Red marked row(s)</small>";
 		}
       
		else
 		{
		
			$find_jv=mysqli_query($db,"select * from sparepurchase where grno='$grnno' and brcode='$brcode'");
			
  			$f_j=mysqli_fetch_row($find_jv);
  			$j_no=$f_j[19];
  		
	 		mysqli_query($db,"delete from sparepurchase where grno='$grnno' and brcode='$brcode'");
	 		
	 		if($j_no >0)
			{
		 		mysqli_query($db,"delete from journal2 where jvno='$j_no' and brcode='$brcode'");
  		    }
  		     	
			$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;
	 		$flg1=0;
			
			$mctr=0;
			
		
			
			for($m=0;$m<$i;$m++)
       		{
			//$sql="insert into sparepurchase(purno,suplier,pbillno,purdate,entrydate,partcode,partname,supqty,price,aprice,hand,tax,taxamt,tot,brcode,taxtype,jvno,ac_postdt) values('$purno','$sup','$pbillno','$purdate','$entrydate','$partcode[$m]','$partname[$m]','$qty[$m]','$unitprice[$m]','$befrtxamt[$m]','$handling','$vat[$m]', '$taxamt[$m]','$tot[$m]','$brcode','$taxtype','$jvno','0000-00-00')";
			if($taxtype == 'CGST')
			{
			    $cgstp = $vat[$m]/2;
			    $cgstamt = $taxamt[$m]/2;
			    $sgstp = $vat[$m]/2;
			    $sgstamt = $taxamt[$m]/2;
			    $igstp = 0;
			    $igstamt = 0;
			}
			elseif($taxtype == 'IGST')
			{
			    $cgstp = 0;
			    $cgstamt = 0;
			    $sgstp = 0;
			    $sgstamt = 0;
			    $igstp = $vat[$m];
			    $igstamt = $taxamt[$m];
			}
			else
			{
			    $cgstp = 0;
			    $cgstamt = 0;
			    $sgstp = 0;
			    $sgstamt = 0;
			    $igstp = 0;
			    $igstamt = 0;
			}
                $sql="insert into sparepurchase(purno,suplier,pbillno,purdate,entrydate,partcode,partname,supqty,price,aprice,hand,tot,brcode,taxtype,jvno,ac_postdt,itemtype,grno,cgstp,cgstamt,sgstp,sgstamt,igstp,igstamt) values('$purno','$sup','$pbillno','$purdate','$entrydate','$partcode[$m]','$partname[$m]','$qty[$m]','$unitprice[$m]','$befrtxamt[$m]','$handling','$tot[$m]','$brcode','$taxtype','$jvno','0000-00-00','$itmtyp[$m]','$grnno',$cgstp,$cgstamt,$sgstp,$sgstamt,$igstp,$igstamt)";

			    mysqli_query($db,$sql)or die(mysqli_error($db));
		        $flg1++;
	      	}
		
        $chre=mysqli_query($db,"select * from ledger where led='$sup' and brcode='$brcode'");
        if(mysqli_num_rows($chre)<=0)
        {
            $code=substr($sup,0,3).date(ymds);
            $code=str_replace(' ','-',$code);
            $sql="insert into ledger values('$sup','SUNDRY CREDITORS','0','Dr','$code','$brcode','0000-00-00')";
            echo $sql."<br>";
            mysqli_query($db,$sql);
        }
		if($taxtype=='CGST'||$taxtype=='UNREG')
		{
 			$taxdrres=mysqli_query($db,"select sum(aprice),cgstp from sparepurchase where grno='$grnno' and (taxtype='CGST' or taxtype='UNREG') and itemtype='Spares' and brcode='$brcode' group by cgstp");
            while($taxdrrow=mysqli_fetch_array($taxdrres))
            {
                $mtot_tax = $taxdrrow[cgstp]+$taxdrrow[sgstp];
                $ledger='Spare Purchase CGST '.$mtot_tax.'%';  
                $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Dr','$ledger','$taxdrrow[0]','0','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur','Pur Order','$purno','$grnno')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
            }
 			
 			
 			$taxdrres=mysqli_query($db,"select sum(aprice),cgstp from sparepurchase where grno='$grnno' and (taxtype='CGST' or taxtype='UNREG') and itemtype='Lubricant' and brcode='$brcode' group by cgstp");
            while($taxdrrow=mysqli_fetch_array($taxdrres))
            {
                $mtot_tax = $taxdrrow[cgstp]+$taxdrrow[sgstp];
                $ledger='Lubricant Purchase CGST '.$mtot_tax.'%';  
                $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Dr','$ledger','$taxdrrow[0]','0','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur','Pur Order','$purno','$grnno')";
    
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
            }


 			$taxres=mysqli_query($db,"select sum(cgstamt),cgstp from sparepurchase where grno='$grnno' and (taxtype='CGST' or taxtype='UNREG') and brcode='$brcode' group by cgstp");
  			while($taxrow=mysqli_fetch_array($taxres))
  			{
    			$ledger='CGST Collected '.$taxrow[1].'%';  
    			$sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Dr','$ledger','$taxrow[0]','0','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur','Pur Order','$purno','$grnno')";
     			mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
     			$ledger='SGST Collected '.$taxrow[1].'%';  
    			$sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Dr','$ledger','$taxrow[0]','0','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur','Pur Order','$purno','$grnno')";
     			mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
  			}

//*************** cess
 			$taxcessres=mysqli_query($db,"select sum(cessamt),cessp from sparepurchase where grno='$grnno' and (taxtype='CGST' or taxtype='UNREG') and brcode='$brcode' group by purno")or die('cess err.'.mysqli_error($db));
  			while($taxcessrow=mysqli_fetch_array($taxcessres))
  			{
    			$ledger='Cess Paid '.$taxcessrow[1].'%';
    			$sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Dr','$ledger','$taxcessrow[0]','0','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur','Pur Order','$purno','$grnno')";
    //echo $sql;
     			mysqli_query($db,$sql) or die('Led Post Err6'.mysqli_error($db));
  			}


//************************
 			$taxcrres=mysqli_query($db,"select sum(tot) from sparepurchase where grno='$grnno' and (taxtype='CGST' or taxtype='UNREG') and brcode='$brcode'");
 			
 		
  			while($taxcrrow=mysqli_fetch_array($taxcrres))
  			{
  			    $sumvl=$taxcrrow[0]+$handling;
  			     $sumvl=round($sumvl);
    			$sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$sup','0','$sumvl','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur','Pur Order','$purno','$grnno')";
    
     			mysqli_query($db,$sql) or die('Led Post Err3'.mysqli_error($db));
  			}
		} // end if
		else
		{
//********************* CST


            $taxdrres=mysqli_query($db,"select sum(aprice),igstp from sparepurchase where grno='$grnno' and taxtype='ICST' and itemtype='Spares' and brcode='$brcode' group by igstp");
            while($taxdrrow=mysqli_fetch_array($taxdrres))
            {
                $ledger='Spares Purchase IGST '.$taxdrrow[tax].'%';  
                $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Dr','$ledger','$taxdrrow[0]','0','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur','Pur Order','$purno','$grnno')";
    
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
            }

            $taxdrres=mysqli_query($db,"select sum(aprice),igstp from sparepurchase where grno='$grnno' and taxtype='IGST' and itemtype='Lubricant' and brcode='$brcode' group by igstp");
            while($taxdrrow=mysqli_fetch_array($taxdrres))
            {
                $ledger='Lubricant Purchase CGST '.$taxdrrow[igstp].'%';  
                $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Dr','$ledger','$taxdrrow[0]','0','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur','Pur Order','$purno','$grnno')";
    
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
            }

 		    $taxdrres=mysqli_query($db,"select sum(aprice),igstp from sparepurchase where grno='$grnno' and taxtype='IGST' and brcode='$brcode' group by igstp");
  		    while($taxdrrow=mysqli_fetch_array($taxdrres))
  		    {
    		    $ledger='Spare Purchase IGST '.$taxdrrow[tax].'%';  
    		    $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Dr','$ledger','$taxdrrow[0]','0','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur','Pur Order','$purno','$grnno')";
    //echo $sql;
    			mysqli_query($db,$sql) or die('Led Post Err4'.mysqli_error($db));
  		    }


 		    $taxres=mysqli_query($db,"select igstp, sum(igstamt) from sparepurchase where grno='$grnno' and taxtype='IGST' and brcode='$brcode' group by igstp");
  		    while($taxrow=mysqli_fetch_array($taxres))
  		    {
    		    $ledger='IGST Collected '.$taxrow[tax].'%';  
    		    $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Dr','$ledger','$taxrow[1]','0','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur','Pur Order','$purno','$grnno')";
    //echo $sql;
     		    mysqli_query($db,$sql) or die('Led Post Err5'.mysqli_error($db));
  		    }
//************************
 		    $taxcrres=mysqli_query($db,"select sum(tot) from sparepurchase where grno='$grnno' and taxtype='IGST' and brcode='$brcode'");
  		    while($taxcrrow=mysqli_fetch_array($taxcrres))
  		    {
  		        $sumvl=$taxcrrow[0]+$handling;
  		        $sumvl=round($sumvl);
    		    $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$sup','0','$sumvl','Spare purno: $purno,PBill:$pbillno','$brcode','Spare Pur','Pur Order','$purno','$grnno')";
    //echo $sql;
    		    mysqli_query($db,$sql) or die('Led Post Err6'.mysqli_error($db));
  		    }
		

		 
 	    }
 			//$taxcrres=mysqli_query($db,"select sum(hand) from sparepurchase where purno='$purno' and brcode='$brcode'");
            if($handling>0)
            {
                $sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','Handling Charges','$handling','0','Handling Charges for pur: $purno,PBill:$pbillno','$brcode','Spare Pur','invno','$purno','$grnno')";
               
                mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error($db));
            }
 		    $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)
			{
			$x=abs($dif);
			$sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Dr','Round Off','$x', '0','Round ofSpur No: $purno','$brcode','Spare Pur','invno','$purno','$grnno')";
//echo $sql;
			mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error($db));
			}
			else if($dif>0)
			{
 			$sql="insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','Round Off','0','$dif','Round of adjusted pur No: $purno,PBill:$pbillno','$brcode','Spare Pur','invno','$purno','$grnno')";
 //echo $sql;
			mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error($db));
			}	
 			
 			
 			if($flg1==$i) 
 			{
			
			echo "Data  updated with invoice number:".$grnno;
			echo "<script> document.getElementById('savecount').value=1;document.location='purchase_edit1.php';</script>";
			} 
 	
		}
    	}
}
elseif($savecount==1)
{
	echo "Data already saved";
  	echo "<script> document.location='purchase_edit1.php';</script>";
}
?>