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

$str=" ";

$data = array();

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



//echo $loc_id;
 if (isset($_POST['category'])) {
 	$item = trim($_POST['category']);
 	$invref = trim($_POST['invref']);
 	$issue_qty= trim($_POST['issue_qty']);
 	//$item = 3;
 	//$invref = 264;
 	//$issue_qty= 1;

if (empty($item)){   $item=0;   $error=1;       }   
if (empty($issue_qty)){   $issue_qty=0;   $error=1;       } 

if ( $error<>1){

///////////////opening inv header //////////////////////

	$sqlinv=mysqli_query($con,"SELECT  * from inv_temp_header where invd_id=$invref  ")or die(mysqli_error($con));
	        	$countinv=mysqli_num_rows($sqlinv);	
	        	if ($countinv>0) {
	        		while($row=mysqli_fetch_array($sqlinv)){			
					$loc_id =$row['loc_id'];
					$vat_inv =$row['vat_inv'];
					//$cost_price =$row['cost_price'];	
					//$discount_rate =$row['discount_rate'];			
				   }
				}
///////  end of inv header

if(	$vat_inv==1){
    	$sqlvat=mysqli_query($con,"SELECT  * from company_master  ")or die(mysqli_error($con));
        $countvat=mysqli_num_rows($sqlvat);	
	        	if ($countvat>0) {
	        	   	while($row=mysqli_fetch_array($sqlvat)){			
					   $vat_amt =$row['vat'];
				   }
	        	} 
}
else { $vat_amt=0 ; }



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



///Getting Item detil

	$sqlsp=mysqli_query($con,"SELECT  * from view_item_master where item_id=$item  ")or die(mysqli_error($con));
	        	$count=mysqli_num_rows($sqlsp);	
	        	if ($count>0) {
	        		while($row=mysqli_fetch_array($sqlsp)){			
					$sales_price =$row['sales_price'];
					$cost_price =$row['cost_price'];	
					$discount_rate =$row['discount_rate'];			
				   }
				}

				if ($sales_price>0){ }
					else { exit(); }

					
///////  end of Getting Item detil

//////getting item stock //////////////

	$sqlstock=mysqli_query($con,"SELECT  * from item_stocks where loc_id=$loc_id and item_id =$item ")or die(mysqli_error($con));
	        	$countst=mysqli_num_rows($sqlstock);	
	        	if ($countst>0) {
	        		while($rowst=mysqli_fetch_array($sqlstock)){			
					$loc_qoh =$rowst['loc_qoh'];
								
				   }
				}


//////End of getting item stock
$sqlinv=mysqli_query($con,"SELECT  * from inv_temp_detail where item_id=$item and invd_id= $invref ")or die(mysqli_error($con));
	$countinv=mysqli_num_rows($sqlinv);	
	        	if ($countinv==0) {

		if ( mysqli_query($con,"INSERT INTO inv_temp_detail(invd_id,item_id,qty,sales_price,cost_price,qoh,item_dis,loc_id,vat) 
	VALUES($invref,$item,$issue_qty,$sales_price,$cost_price,$loc_qoh,$discount_rate,$loc_id,$vat_amt )  ")or die(mysqli_error($con)))
	        {
        	exit();	        	 
	        }

	    }
} //// if error

} ///// if (isset($_POST['category'])) {

?>