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

$str=" ";

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

$t_no= trim($_POST['t_no']);


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

/////!!!!!!!!!!!!!!!!!!  Open grn_temp_header   !!!!!!!!!!!!!!!!!!!!!!!!!!!!
$query=mysqli_query($con," select * from stock_adjust_temp_header where tran_id='$t_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'];	
		$gdate=$rowsp['tran_date'];
		$loc_id=$rowsp['from_loc_id'];	
		//$sup_id=$rowsp['sup_id'];
		$note=$rowsp['refnote'];
		//$supinv=$rowsp['invoice_no'];
	}

	
	   	/// !!!!!!!!!!!!!!!!!!!      item detail
	       $queryd=mysqli_query($con," select * from stock_adjust_temp_detail where tran_id='$t_no'  ")or die(mysqli_error($con));
			$countd=mysqli_num_rows($queryd);
			mysqli_begin_transaction($con);
			if ($countd>0)
			{
	
				while($rowd=mysqli_fetch_array($queryd)){ 	
					$item_id=$rowd['item_id'];	
					$issue_qty=$rowd['tran_qty'];
					//$costprice=$rowd['cost_price'];	

/////!!!!!!!!!!!!!!!!!!  opening Item Matsre table   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
$queryitem=mysqli_query($con,"select * from item_master where item_id=$item_id ")or die(mysqli_error());
while($rowitem=mysqli_fetch_array($queryitem)){  
$qoh=$rowitem['qoh'];
//$cost_price=$rowitem['cost_price'];
}
///~~~~~~~~~~~~~~~~~~~~~~~~~~~   End of  Item Matsre table ~~~~~~~~~~~~~~~~~~







if ($issue_qty<>0){

 					
		if ( mysqli_query($con,"INSERT INTO stock_adjust_detail(tran_id,item_id,tran_qty) 
	        VALUES($adj_new_no,$item_id,$issue_qty )  ")or die(mysqli_error($con)))
	        {  
//////////////// Stock Update


/////////////////////////////////////////////////////////////////////
 $queryitem=mysqli_query($con," select * from item_stocks where item_id=$item_id and loc_id= $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 + $issue_qty  where item_id=$item_id and loc_id= $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,$loc_id,$issue_qty )  ")or die(mysqli_error($con)))
	        {   }

}





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





	        	//////////////////
	        		//grnd_loc_id
	        //	if ( mysqli_query($con,"update item_stocks set loc_qoh = loc_qoh + $issue_qty where item_id=$item_id and  loc_id='$loc_id' ")or die(mysqli_error($con)))
		//{
			//echo "<script>document.location='Admin_location_view.php'</script>"; 
		//}		



		//////update item master //////////////

if ( mysqli_query($con,"update item_master set  qoh=qoh +$issue_qty where item_id=$item_id  ")or die(mysqli_error($con)))
		{
			//echo "<script>document.location='Admin_location_view.php'</script>"; 
		}		


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

				///////////////////////end of stock update
	          }
	        //mysqli_begin_transaction($con);
	      } //if ($issue_qty<>0){
	        	
	       	}	///while

			} // end of if ($countd>0)
	     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~end of item detail   	
			

		  
		//echo "ddd";
			

	if ( mysqli_query($con,"INSERT INTO stock_adjust_header(tran_id,tran_date,from_loc_id,refnote,userid,edate) 
		VALUES($adj_new_no,'$gdate',$loc_id,'$note',$userid,'$edate' )  ")or die(mysqli_error($con)))
	        {

	if ( mysqli_query($con,"update sequence set cno =cno + 1  where seq_id=11  ")or die(mysqli_error($con)))			
						{   							
							
		if ( mysqli_query($con,"delete from stock_adjust_temp_header where tran_id='$t_no' ")or die(mysqli_error($con))) {					
			if ( mysqli_query($con,"delete from stock_adjust_temp_detail where tran_id='$t_no' ")or die(mysqli_error($con))) {	

							if (!mysqli_commit($con)) {
							    echo("Transaction commit failed\n");
							    exit();
							}
							else {
					echo "<script>document.location='stock_adjust.php'</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)
?>
<script>
        function printPage() {
        	//alert("Please enter the route you visited !!!"); 
           // window.print(); // This initiates the print dialog
       // http://localhost:81/officesync/pages/stock_req_print.php
url=' http://localhost:81/saleshub/pages/item_grn_pdf.php?grn_no=<?php echo $grn_new_no;?> ';
 var printWindow = window.open( url, 'Print', 'left=200, top=200, width=950, height=500, toolbar=0, resizable=0' , '_blank');

    printWindow.addEventListener('load', function() {
        if (Boolean(printWindow.chrome)) {
            printWindow.print();
            setTimeout(function(){
               printWindow.close();
               window.location.href = "item_grn.php";
            }, 500);
        } else {
            printWindow.print();
            printWindow.close();
            window.location.href = "item_grn.php";
        }
    }, true);



        }

window.onload = function() {

        printPage();
      // window.location.href = "inv_new.php";
    };
        
    </script>    


?>