<?php
include('../include/session.php');

$line_num  = $_GET['line_num'];
$i=0;
while ($i<$line_num) {
			$store_item_id = $_GET['store_item_id_text'.$i];
			$quantity  = $_GET['quantity_text'.$i];
	        $counted_qty  = $_GET['counted_qty_text'.$i];
			
			$stock_take_date = $_GET['stock_take_date'];
			
			date_default_timezone_set('Africa/Gaborone');
			$entryDate = date("Y-m-d H:i:s", time()); 
			$stockTake = 'C';	
			$userId = $_SESSION['username'];
																					
			$difference = $counted_qty - $quantity;
						
			if ($difference < 0) {
				  						
					$table_name = $_SESSION['db_prefix']."_store_items";
					$stock_query = "SELECT category, subCategory, quantity, price FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
					$stock_result = $con->query($stock_query);
					$row = mysqli_fetch_assoc($stock_result);
					$category = $row['category'];
					$subCategory = $row['subCategory'];
					$stock_quantity = $row['quantity'];
					$price = $row['price'];
										
					$new_stock_quantity = $stock_quantity + $difference;   //difference is negative, effectively quantity is subtracted

					$table_name3 = $_SESSION['db_prefix']."_store_items";
					$count_query = "UPDATE $table_name3 SET quantity='".$counted_qty."', stockTake='".$stockTake."' WHERE store_item_id = '".$store_item_id."' ";
					$count_result = $con->query($count_query);
					
			}
			
			if ($difference == 0) {
				  						
					$table_name = $_SESSION['db_prefix']."_store_items";
					$stock_query = "SELECT category, subCategory, quantity, price FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
					$stock_result = $con->query($stock_query);
					$row = mysqli_fetch_assoc($stock_result);
					$category = $row['category'];
					$subCategory = $row['subCategory'];
					$stock_quantity = $row['quantity'];
					$price = $row['price'];
//										
//					$new_stock_quantity = $stock_quantity + $difference;   //difference is negative, effectively quantity is subtracted
//
//					$table_name3 = $_SESSION['db_prefix']."_store_items";
//					$count_query = "UPDATE $table_name3 SET quantity='".$new_stock_quantity."', stockTake='".$stockTake."' WHERE store_item_id = '".$store_item_id."' ";
//					$count_result = $con->query($count_query);
					
	
			}
			
			if ($difference > 0) {
				  						
					$table_name = $_SESSION['db_prefix']."_store_items";
					$stock_query = "SELECT category, subCategory, quantity, price FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
					$stock_result = $con->query($stock_query);
					$row = mysqli_fetch_assoc($stock_result);
					$category = $row['category'];
					$subCategory = $row['subCategory'];
					$stock_quantity = $row['quantity'];
					$price = $row['price'];
					
					$new_stock_quantity = $stock_quantity + $difference;
					
					
					$table_name3 = $_SESSION['db_prefix']."_store_items";
					$count_query = "UPDATE $table_name3 SET quantity='".$counted_qty."', stockTake='".$stockTake."' WHERE store_item_id = '".$store_item_id."' ";
					$count_result = $con->query($count_query);
		}
			
	
	$table_name = $_SESSION['db_prefix']."_material_stock_count";
    $report_query = "INSERT INTO $table_name (stock_take_date, store_item_id, category, sub_category, old_stock, new_stock, unit_price, user_name) VALUES ('".$stock_take_date."' ,'".$store_item_id."','".$category."','".$subCategory."','".$stock_quantity."' ,'".$counted_qty."', '".$price."', '".$userId."')";
	$report_result = $con->query($report_query);
					
if ($difference!=0) {	
	$table_name4 = $_SESSION['db_prefix']."_item_history";
	$history_query = "INSERT INTO $table_name4 (store_item_id, entry, reference, quantity, total, date, entryDate, userId) VALUES ('".$store_item_id."','5','5','".$difference."', '".$new_stock_quantity."','".$stock_take_date."','".$entryDate."','".$userId."')";
	$history_result = $con->query($history_query);
}
			
			
	$table_name = $_SESSION['db_prefix']."_tracking_store";
	$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."', '".$_SESSION['username']."', '21', '".$store_item_id."', '".$quantity."' ,'".$counted_qty."', '' )";
	$tracking_result = $con->query($tracking_query);
	
	$i++;	
	}	
  
 	   
	   ?>
	   
     <script language="javascript" type="text/javascript">
          window.location="material_stock_report.php?stock_take_date=<?php echo $stock_take_date ?>";
</script>