<?php
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include "connect.php";
include "removespecial.php";
$cmpmid=$_SESSION['linkarisecmpmid'];
$brmid=$_SESSION['linkarisebranchid'];
	$umid = $_SESSION['linkariseumid'];

		$salesmid=$_SESSION['linkariseumid'];
		$suppliermid=$_POST['suppliermid'];
		$pomid=$_POST['pomid'];
		$grnmid=$_POST['grnmid'];
	//	$porefno=$_POST['porefno'];
		$invoiceno=$_POST['invoiceno'];
		$grndate=date("Y-m-d", strtotime($_POST['grndate']) ); 
		//$fdate=date("Y-m-d", strtotime($_POST['fdate']) ); 
		$reviseqt1=$_POST['revise1'];
		$copyqt=$_POST['copyqt'];
		date_default_timezone_set("Asia/Kolkata");
		$mdate= date('Y-m-d');
		$mdatetime= date('Y-m-d H:i:s');
		$cmpcurrentyear='20-21';
		$prefix='GRN-';
		$maxgrnmno=0;
		/* $evelastyear = "select * from res_businessregister_master limit 0,1";
		$relastyear = mysqli_query($conn, $evelastyear);
		while($rtlastyear = mysqli_fetch_assoc($relastyear))
		{
			$cmpcurrentyear=$rtlastyear['cmpcurrentyear'];
		} */
		
		$evelastyear = "select * from res_company_master where cmpmid=$cmpmid";
	$relastyear = mysqli_query($conn, $evelastyear);
	while($rtlastyear = mysqli_fetch_assoc($relastyear))
	{
		$cmpcurrentyear=$rtlastyear['cmpyear'];
		$prefix1=$rtlastyear['grnprefix'];
	}
		
		if($grnmid==0)
		{
		//$maxgrnmno=0;
		 $evemaxq = "SELECT max(grnmno) AS maxgrnmno FROM res_grn_master where grnyear='$cmpcurrentyear' and cmpmid=$cmpmid";
		$remaxq = mysqli_query($conn, $evemaxq);
		while($rtmaxq = mysqli_fetch_assoc($remaxq))
		{
			$maxgrnmno=$rtmaxq['maxgrnmno'];
		}
		$maxgrnmno1=$maxgrnmno+1;
		$maxgrnmno2=$prefix.$maxgrnmno1;
		
		$eve1 = "INSERT INTO res_grn_master (grnstatus,grnprefix,grnmno,grnfordisplay,grnyear,addedby,addedon,cmpmid) 
		VALUES('In Review','$prefix','$maxgrnmno1','$maxgrnmno2','$cmpcurrentyear','$salesmid','$mdatetime','$cmpmid')";
		if ($conn->query($eve1) === TRUE)
		{
			 $grnmid = $conn->insert_id;
			 if($pomid!=0)
			 {
				$eve15 = "update res_po_master set postatus='GRN Recieved' where pomid=$pomid";
				if ($conn->query($eve15) === TRUE)
				{
					
				}
			 }
		}
		}
		date_default_timezone_set("Asia/Kolkata");
		$mdate= date('Y-m-d');
		echo $eve11 = "update res_grn_master set invoiceno='$invoiceno',suppliermid='$suppliermid',refpoid='$pomid',grndate='$grndate' where grnmid=$grnmid";
		if ($conn->query($eve11) === TRUE)
		{
			$n=0;
			$n2=0;
			$subtotal=0;
			$grosstotal=0;
			$amount=0;
			$gtamount=0;
			$totaltaxcalc1=0;
			$totaltaxcalc2=0;
			$tcper1 = 0;
			$tcper2 = 0;
			$maxgrnmno1=$maxgrnmno+1;
		$maxgrnmno2=$prefix.$maxgrnmno1;
			$delete_checkbox_array = $_POST['itemid'];
			foreach ($delete_checkbox_array as $one_by_one_sid)
			{
					$itemid=$one_by_one_sid;
				$grndid=$_POST['qdid'][$n];
				$srate=$_POST['pprice'][$n];
				$sqty=$_POST['pqty'][$n];
				//$iunit=$_POST['iunit'][$n];
				$sdesc=removespecialchar($_POST['pdesc'][$n]);
				$pdiscper=$_POST['pdiscper'][$n];
				$ptax = $_POST['pgst'][$n];
				$tcper1 = $ptax/2;
				$tcper2 = $ptax/2;
				$eve_id_2 = "select * from res_item_master WHERE itemid='$itemid'";
				$re_id_2 = mysqli_query($conn, $eve_id_2);
				$pid=0;
				
				$iunitmasterstock=0;
				$hsncode="";
			
				if(mysqli_num_rows($re_id_2) > 0)
				{
					while($rt_id_2 = mysqli_fetch_assoc($re_id_2))
					{
						$itemname = $rt_id_2['itemname'];
						$hsncode = $rt_id_2['itemhsncode'];
						$ptax = $rt_id_2['itemtaxper'];
						$iunitmasterstock = $rt_id_2['iunitmasterstock'];
						$iunitmaster=$rt_id_2['iunitmaster'];
						$iunit=$rt_id_2['itemunit'];
						
						$tcper1 = $ptax/2;
						$tcper2 = $ptax/2;	
						break;
					}
				}			
				$amount=floatval($srate)*floatval($sqty);
				$pdiscamt=($amount*$pdiscper)/100;
				$samount=$amount-$pdiscamt;
				$subtotal=$subtotal+$samount;
				$taxcalc=($samount * $tcper1)/100;
				$taxcalc2=($samount * $tcper2)/100;
				$taxcalctotal=($samount * $ptax)/100;
				$totaltaxcalc1=$totaltaxcalc1+$taxcalc;;
				$totaltaxcalc2=$totaltaxcalc2+$taxcalc2;
				$tamount=$samount+$taxcalc+$taxcalc2;
				$tamount=$samount+$taxcalctotal;
				$gtamount=$gtamount+$tamount;
				$grosstotal=$grosstotal+$tamount;
				$n = $n + 1;
				$n2++;
				if( $copyqt==1)
				{
				if($itemid==0 || $sqty==0)
				{
					$_SESSION['msg1'] = "Pls select product or add some quantity";
						header('Location: grngenerate.php');
				}
				else
				{
					echo $query = "INSERT INTO res_grn_detail (grnmid,itemid,itemname,itemunit,itemqty,itemiprice,itemamount,itemgstper,itemcgstamount,itemsgstamount,itemgstamount,itemtotalamount,itemhsncode,itemdesc,itemdiscper,itemdiscamt) 
					VALUES('$grnmid','$itemid','$itemname','$iunit','$sqty','$srate','$samount','$ptax','$taxcalc','$taxcalc2','$taxcalctotal','$tamount','$hsncode','$sdesc','$pdiscper','$pdiscamt')";
					if ($conn->query($query) === TRUE)
					{
						 $grndid = $conn->insert_id;
					}
					/* $result= mysqli_query($conn, $query) or die("Error");*/
				}}
				else
				{
					if($grndid==0)
					{
						if($itemid==0 || $sqty==0)
						{
							$_SESSION['msg1'] = "Pls select product ";
						header('Location: grngenerate.php');
						}
						else
						{ 
					       
							echo $query = "INSERT INTO res_grn_detail (grnmid,itemid,itemname,itemunit,itemqty,itemiprice,itemamount,itemgstper,itemcgstamount,itemsgstamount,itemgstamount,itemtotalamount,itemhsncode,itemdesc,itemdiscper,itemdiscamt) 
					VALUES('$grnmid','$itemid','$itemname','$iunit','$sqty','$srate','$samount','$ptax','$taxcalc','$taxcalc2','$taxcalctotal','$tamount','$hsncode','$sdesc','$pdiscper','$pdiscamt')";
							if ($conn->query($query) === TRUE)
							{
								$grndid = $conn->insert_id;
							}
							
						}						
					}
					else
					{
						if($itemid==0 || $sqty==0)
						{
							$query_del = "delete from res_grn_detail where grndid=$grndid";
							$result_del= mysqli_query($conn, $query_del) or die("Error");
						}
						else
						{
							
						echo $query = "update res_grn_detail set itemname='$itemname',itemunit='$iunit',itemqty='$sqty',itemiprice='$srate',itemamount='$samount',itemdiscper='$pdiscper',itemdiscamt='$pdiscamt',itemgstper='$ptax',itemcgstamount='$taxcalc',itemsgstamount='$taxcalc2',itemgstamount='$taxcalctotal',itemtotalamount='$tamount',itemdesc='$sdesc' where grndid=$grndid";
						$result= mysqli_query($conn, $query) or die("Error");
						
						}
					}
				}
						$inqtymaster=$sqty;
					$iunitmasterstock2=$iunitmasterstock+$inqtymaster;
					/*stock update*/
					$eve1 = "INSERT INTO res_item_detail (itemtranmid,itemtrandid,itemid,itransactiontype,iinouttype,itemtrandate,iinqty,iinqtyactual,inunit,inunitactual,itransremarks,itfinancialyear,ilateststockback) 
					VALUES('$grnmid','$grndid','$itemid','GRN In','In','$mdate','$sqty','$inqtymaster','$iunit','$iunitmaster','GRN In','$cmpcurrentyear','$iunitmasterstock2')";	
					if ($conn->query($eve1) === TRUE)
					{
						$last_id = $conn->insert_id;
						echo $eve2 = "update res_item_master set iunitmasterstock=$iunitmasterstock2  where itemid=$itemid";	
						if ($conn->query($eve2) === TRUE)
						{
							
						}
					}
				}
			
			$eve55 = "select sum(itemgstamount) as tax1,sum(itemqty) as tqty from res_grn_detail WHERE grnmid='$grnmid'";
			$re55 = mysqli_query($conn, $eve55);
			$totaltaxamt=0;
			$totalqty=0;
			while($rt55 = mysqli_fetch_assoc($re55))
			{
				$totaltaxamt=$rt55['tax1']; 
				$totalqty=$rt55['tqty'];
			}
			
			
			$netamount1=round($gtamount);
			$invoiceroundoffamount=$netamount1-$gtamount;
			$eve3 = "update res_grn_master set grnroundoffamount='$invoiceroundoffamount',grntotalitem='$totalqty',grntotaltaxamount='$totaltaxamt',grntotalamount='$subtotal',grngrossamount='$grosstotal',grnnetamount='$netamount1' ,addedby='$salesmid',addedon='$mdatetime' where grnmid='$grnmid'";
			$re3 = mysqli_query($conn, $eve3);
		
	
	
		
							
			
		}
		$_SESSION['msg'] = "GRN Added";
		                
		//header("Location: grn.php");
?>
