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

$str=" ";
$error=0;

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

$str=" ";

$error=0;



$tdate= trim($_POST['tdate']);
$fstock= trim($_POST['fstock']);
$estock= trim($_POST['estock']);
$note= trim($_POST['note']);
//echo $userid;


///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!     Validation    !!!!!!!!!!
if (empty($fstock)){
	$fstock=0;
	$error=1;
	//$error_des='Date Error';
}
else {

		if (empty($estock)){
			$estock=0;
			$error=1;
			//$error_des='Location Error';
		}
}
///~~~~~~~~~~~~~~~~~~~~~~~~~~End of Validation ~~~~~~~~~~~~~~~~~~~

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


else {
mysqli_begin_transaction($con);

/////!!!!!!!!!!!!!!!!!!  sequence   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
$querydoc=mysqli_query($con,"select * from sequence where seq_id=3 ")or die(mysqli_error());
while($row=mysqli_fetch_array($querydoc)){  
$trn_no=$row['cno'];
}
///~~~~~~~~~~~~~~~~~~~~~~~~~~~   End of sequence ~~~~~~~~~~~~~~~~~~

$sql="INSERT INTO stock_trans_temp_header(tran_id,from_loc_id,to_loc_id,refnote,tran_date,userid,edate) 
			 VALUES($trn_no,'$fstock',$estock,'$note','$tdate',$userid,'$edate' )  ";

echo $sql;
if ( mysqli_query($con,$sql)or die(mysqli_error($con)))
	        {

	        	//echo $trn_no;	
	        			if ( mysqli_query($con,"update sequence set cno =cno + 1  where seq_id=3  ")or die(mysqli_error($con)))			
						{   
                            //echo $trn_no;
							mysqli_commit($con);
							echo "<script>document.location='stock_trans1.php?trn_no=$trn_no '</script>"; 

						}  
	        }




}  /////~~~~~~~~~~end of else of if ($error==1)  { 