<?php 
session_start();
include('../includes/dbcon.php');

$str=" ";

$error=0;

$edate=date("Y-m-d");
$userid=$_SESSION['rm_id'];


$loc_id= mysqli_real_escape_string($con,trim($_POST['loc_id']));
$gdate= mysqli_real_escape_string($con,trim($_POST['gdate']));
$note= mysqli_real_escape_string($con,trim($_POST['note']));
$supinv= mysqli_real_escape_string($con,trim($_POST['supinv']));


///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!     Validation    !!!!!!!!!!

///~~~~~~~~~~~~~~~~~~~~~~~~~~End of Validation ~~~~~~~~~~~~~~~~~~~
if(!empty($loc_id)){ $error==1; } 
if(!empty($gdate)){ $error==1; } 
if(!empty($supinv)){ $error==1; } 


if ($error==1)  {  
	 echo "<script type='text/javascript'>alert('Please select The Item !');</script>";
	 echo "<script>document.location='item_grn.php'</script>";
}

else {
   // echo "ok";
    mysqli_begin_transaction($con);
    $nn="select * from grn_header where invoice_no='$supinv'";
    //echo $nn;
    $query=mysqli_query($con,$nn)or die(mysqli_error($con));
    $count=mysqli_num_rows($query);
    //echo $count;
    if ($count > 0)
    {
	    echo "<script type='text/javascript'>alert('Exsisting Receipt Number!');</script>";
	    echo "<script>document.location='item_grn_add.php'</script>"; 
    }
  
  
    else {
        
        /////!!!!!!!!!!!!!!!!!!  sequence   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        $querydoc=mysqli_query($con,"select * from sequence where seq_id=2 ")or die(mysqli_error());
        while($row=mysqli_fetch_array($querydoc)){  
        $grn_no=$row['cno'];
        }
        ///~~~~~~~~~~~~~~~~~~~~~~~~~~~   End of sequence ~~~~~~~~~~~~~~~~~~
        

        $pp="INSERT INTO grn_temp_header(grnd_no,grnd_date,grand_loc_id,refnote,invoice_no,userid,edate) 
			 VALUES($grn_no,'$gdate',$loc_id,'$note','$supinv',$userid,'$edate' ) ";
        //echo $pp;
        	if ( mysqli_query($con,$pp)or die(mysqli_error($con)))
	        {
                    if ( mysqli_query($con,"update sequence set cno =cno + 1  where seq_id=2  ")or die(mysqli_error($con)))			
						{ 
						    mysqli_commit($con);
							echo "<script>document.location='item_grn1.php?grnd_no=$grn_no '</script>"; 

						}
        
	        }
    }
  
  

    
    
    
    
}
?>