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

$str=" ";


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

$tran_no= trim($_POST['tran_no']);
$error=0;
$loc_qty=0;

//echo $tran_no;
//mysqli_autocommit($con, false);
mysqli_begin_transaction($con);

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

/////!!!!!!!!!!!!!!!!!!  Open stock_trans_temp_header_temp_header   !!!!!!!!!!!!!!!!!!!!!!!!!!!!
$query=mysqli_query($con," select * from stock_trans_temp_header where tran_id='$tran_no'  ")or die(mysqli_error($con));
$count=mysqli_num_rows($query);

if ($count>0)
{

	while($rowsp=mysqli_fetch_array($query)){			
		//$grnd_no=$rowd['grnd_no'];	
		$tran_date=$rowsp['tran_date'];
		$from_loc_id=$rowsp['from_loc_id'];	
		$to_loc_id=$rowsp['to_loc_id'];	
		$note=$rowsp['refnote'];
	
	}
//echo $from_loc_id;


	   	/// !!!!!!!!!!!!!!!!!!!      item detail
	       $queryd=mysqli_query($con," select * from stock_trans_temp_detail where tran_id='$tran_no'  ")or die(mysqli_error($con));
			$countd=mysqli_num_rows($queryd);
			
			if ($countd>0)
			{


					while($rowd=mysqli_fetch_array($queryd)){ 	
					$item_id=$rowd['item_id'];	
					$tran_qty=$rowd['tran_qty'];
					$note=$rowd['note'];

					//////Checking stock ///////////////////////////////
$queryitem=mysqli_query($con," SELECT * FROM stock_trans_temp_detail LEFT JOIN stock_trans_temp_header ON stock_trans_temp_header.tran_id=stock_trans_temp_detail.tran_id
LEFT JOIN item_stocks ON item_stocks.item_id=stock_trans_temp_detail.item_id AND stock_trans_temp_header.from_loc_id=item_stocks.loc_id WHERE stock_trans_temp_detail.item_id=$item_id ")or die(mysqli_error($con));
             while($rowitem=mysqli_fetch_array($queryitem)){ 	
             		$loc_qty=$rowitem['loc_qoh'];	
             		//echo $loc_qty;
             		if ($tran_qty<=$loc_qty){
		if ( mysqli_query($con,"INSERT INTO stock_trans_detail(tran_id,item_id,tran_qty,note) 
	        VALUES($trn_new_no,$item_id,$tran_qty,'$note' )  ")or die(mysqli_error($con)))
	        {   }

	    		if ( mysqli_query($con,"update item_stocks set loc_qoh = loc_qoh - $tran_qty  where item_id=$item_id and loc_id= $from_loc_id ")or die(mysqli_error($con)))			
						{   }

/////////////////////////////////////////////////////////////////////
 $queryitem=mysqli_query($con," select * from item_stocks where item_id=$item_id and loc_id= $to_loc_id  ")or die(mysqli_error($con));
			$countitem=mysqli_num_rows($queryitem);
			
			if ($countitem>0)
			{

if ( mysqli_query($con,"update item_stocks set loc_qoh =loc_qoh + $tran_qty  where item_id=$item_id and loc_id= $to_loc_id ")or die(mysqli_error($con)))			
						{   }


			}  ////	if ($countitem>0)

else {

if ( mysqli_query($con,"INSERT INTO item_stocks(item_id,loc_id,loc_qoh) 
	        VALUES($item_id,$to_loc_id,$tran_qty )  ")or die(mysqli_error($con)))
	        {   }

}





//////////////////////////////////////////////////////////

             			//echo "a";	
             		}  ////if ($tran_qty<=$loc_qty){
             		} /// while($rowitem=mysqli_fetch_array($queryitem)){ 	
				} //while($rowd=mysqli_fetch_array($queryd)){ 
			} /// if ($countd>0)






	if ( mysqli_query($con,"INSERT INTO stock_trans_header(tran_id,tran_date,from_loc_id,to_loc_id,refnote,userid,edate) 
		VALUES($trn_new_no,'$tran_date',$from_loc_id,$to_loc_id,'$note',$userid,'$edate' )  ")or die(mysqli_error($con)))
	        {

	if ( mysqli_query($con,"update sequence set cno =cno + 1  where seq_id=4  ")or die(mysqli_error($con)))			
						{   							
							
		if ( mysqli_query($con,"delete from stock_trans_temp_header where tran_id='$tran_no' ")or die(mysqli_error($con))) {					
			if ( mysqli_query($con,"delete from stock_trans_temp_detail where tran_id='$tran_no' ")or die(mysqli_error($con))) {	

							if (!mysqli_commit($con)) {
							    echo("Transaction commit failed\n");
							    exit();

							   

							}
							else {
			echo "<script>document.location='stock_trans.php?tran_no '</script>";

							}
			}	//if ( mysqli_query($con,"delete from grn_temp_detail where grnd_no='$grn_no' ")or die(mysqli_error($con))) {				
			} //if ( mysqli_query($con,"delete from grn_temp_header where grnd_no='$grn_no' ")or die(mysqli_error($con))) {	



					} ///if ( mysqli_query($con,"update sequence set cno =cno + 1  where seq_id=1  ")or die(mysqli_error($con)))





	 		}  //~~~~~~~~~ end of if ( mysqli_query($con,"INSERT INTO grn_header(
	 		else {

	 			//mysqli_rollback($con);

	 		}










}///if ($count>1)


