<?php
	include("includes/dbconfig.php");
	 
	$err= "";
	
	$itemscount = 0;
	$seles_details = '';
	/*
	echo $pice_id = 35931;
 
	$shop =  $_SESSION["connecteduserbranch"];
	
    $item_id = Item_id($pice_id, $shop );
	$bal_qn = Stock_level($item_id   , $shop);
	 $balance = $bal_qn[0];
	$stock_id = $bal_qn[1];
	 
	$balance = $balance-1;
	if($balance < 1){
		$balance = 0;
	}
	
    Change_quantity( $balance , $stock_id, 1);
	
	  $bal_qn = Stock_level($item_id   , 'NULL');
	  $stock_id = $bal_qn[1];
      $balance = $bal_qn[0];
	  $balance = $balance + 1;
		if($balance < 1){
			$balance = 0;
		}
      Change_quantity( $balance , $stock_id , 1);
	
	
	*/
	 
	
	function Item_id($item_id , $shop){
		$id = 0;
		$count =0;
		$item_sold_per_id = 0;
		$categorydata = DB::query("SELECT item FROM item_price where id=%i    "  , $item_id   );
			if(DB::count() != 0){										
		       foreach ($categorydata as $category)
	              {
					  
			          $id =  $category['item'];
					 
		          
			}
			}
			return $id;
	}
	
	
	function Change_quantity(  $quantity , $item, $user){
		 
			$s = DB::query('UPDATE item_stock_level SET updated_date = current_timestamp(), updated_by = '.$user.', quantity = ' . $quantity . ' WHERE   id ='.$item.''  );
		
		 
	}
	
	
	
		function Stock_level($item   , $shop){
			$qty =  0;
			$stockitemsdata =  false;
			if($shop > 0){
				$stockitemsdata = DB::query("SELECT  quantity, id FROM item_stock_level WHERE item_stock_level.item   =%i     and shop =%i  LIMIT 1", $item , $shop);
			}else {
				$stockitemsdata = DB::query("SELECT  quantity, id FROM item_stock_level WHERE item_stock_level.item   =%i    ORDER BY shop  LIMIT 1", $item , $shop);
			}
			 
			 
			 
		 	  if(DB::count() != 0){
				foreach ($stockitemsdata  as $category)
	              {
			        	$qty =  array(  $category['quantity'] , $category['id']);//now we can get the price ID
					 
					 
			  
		          } 
				
			 }
			 
			  
			 return $qty;
	}
	
	
 
	
	
	
	//select_items_from_category.php
	
	if(isset($_POST['category'])){
	$name = '<option value="0">No Item found</option>';
	 
		$categorydata = DB::query("SELECT item_name, id FROM stock_item where category =%i" , $_POST['category']);
			if(DB::count() != 0){
				$name ='';
				
		       foreach ($categorydata as $category)
	              {
			         
					$name .= '<option value="'.$category['id'].'">'.$category['item_name'].'</option>';
			  
		          }
			}
			echo $name;
	 
	
	}
	
	 
		
	 