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


$line_num2  = $_POST['line_num2'];

$i=0;
while ($i<$line_num2) {
			$product_store_line_id = $_POST['2product_store_line_id'.$i];
			$product_item_id  = $_POST['2product_item_id_text'.$i];
			$balance  = $_POST['2balance_text'.$i];
	        $counted_qty  = $_POST['2counted_qty_text'.$i];
			$batch_code  = $_POST['2batch_code_text'.$i];
	        
			$stock_take_date = $_POST['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 - $balance;
			
						
			if ($difference < 0) {
				  	$table_name = $_SESSION['db_prefix']."_product_store_lines";
					$stock_update_query = "UPDATE $table_name SET quantity_out = quantity_out - '".$difference."' WHERE product_store_line_id = '".$product_store_line_id."' ";
					$stock_update_result = $con->query($stock_update_query);
					
					//$table_name = $_SESSION['db_prefix']."_product_store_lines";
//					$stock_count_query = "SELECT SUM(quantity_in - quantity_out) AS stock_quantity FROM $table_name WHERE product_item_id = '".$product_item_id."' ";
//					$stock_count_result = $con->query($stock_count_query);
//					$row = mysqli_fetch_assoc($stock_count_result);
//					$stock_quantity = $row['stock_quantity'];
					
					$table_name = $_SESSION['db_prefix']."_product_store";
					$stock_query = "SELECT item_name, category, subCategory, quantity, price FROM $table_name WHERE product_item_id = '".$product_item_id."' ";
					$stock_result = $con->query($stock_query);
					$row = mysqli_fetch_assoc($stock_result);
					$product_item_name = $row['item_name'];
					$category = $row['category'];
					$subCategory = $row['subCategory'];
					$quantity = $row['quantity'];
					$price = $row['price'];
					
										
					//$table_name3 = $_SESSION['db_prefix']."_product_store";
//					$count_query = "UPDATE $table_name3 SET quantity='".$counted_qty."', stockTake='".$stockTake."' WHERE product_item_id = '".$product_item_id."' ";
//					$count_result = $con->query($count_query);
					
					$table_name3 = $_SESSION['db_prefix']."_production";
					$production_query = "UPDATE $table_name3 SET damaged_no= damaged_no - '".$difference."' WHERE product_item_id = '".$product_item_id."' AND batch_code = '".$batch_code."' AND production_run = 'yes'";
					$production_result = $con->query($production_query);
					
			}

			if ($difference == 0) {
				  	//$table_name = $_SESSION['db_prefix']."_product_store_lines";
//					$stock_update_query = "UPDATE $table_name SET quantity_out = quantity_out - '".$difference."' WHERE product_store_line_id = '".$product_store_line_id."' ";
//					$stock_update_result = $con->query($stock_update_query);
					
					$table_name = $_SESSION['db_prefix']."_product_store";
					$stock_query = "SELECT item_name, category, subCategory, quantity, price FROM $table_name WHERE product_item_id = '".$product_item_id."' ";
					$stock_result = $con->query($stock_query);
					$row = mysqli_fetch_assoc($stock_result);
					$product_item_name = $row['item_name'];
					$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']."_product_store";
//					$count_query = "UPDATE $table_name3 SET stockTake='".$stockTake."' WHERE product_item_id = '".$product_item_id."' ";
//					$count_result = $con->query($count_query);
					
			}

			if ($difference > 0) {
				  	$table_name = $_SESSION['db_prefix']."_product_store_lines";
					$stock_update_query = "UPDATE $table_name SET quantity_in = quantity_in + '".$difference."' WHERE product_store_line_id = '".$product_store_line_id."' ";
					$stock_update_result = $con->query($stock_update_query);
					
					//$table_name = $_SESSION['db_prefix']."_product_store_lines";
//					$stock_count_query = "SELECT SUM(quantity_in - quantity_out) AS stock_quantity FROM $table_name WHERE product_item_id = '".$product_item_id."' ";
//					$stock_count_result = $con->query($stock_count_query);
//					$row = mysqli_fetch_assoc($stock_count_result);
//					$stock_quantity = $row['stock_quantity'];
					
					$table_name = $_SESSION['db_prefix']."_product_store";
					$stock_query = "SELECT item_name, category, subCategory, quantity, price FROM $table_name WHERE product_item_id = '".$product_item_id."' ";
					$stock_result = $con->query($stock_query);
					$row = mysqli_fetch_assoc($stock_result);
					$product_item_name = $row['item_name'];
					$category = $row['category'];
					$subCategory = $row['subCategory'];
					$quantity = $row['quantity'];
					$price = $row['price'];
					echo '<br>*****'.$subCategory;
					
					//$table_name3 = $_SESSION['db_prefix']."_product_store";
//					$count_query = "UPDATE $table_name3 SET quantity='".$counted_qty."', stockTake='".$stockTake."' WHERE product_item_id = '".$product_item_id."' ";
//					$count_result = $con->query($count_query);
					
					$table_name3 = $_SESSION['db_prefix']."_production";
					$production_query = "UPDATE $table_name3 SET damaged_no= damaged_no - '".$difference."' WHERE product_item_id = '".$product_item_id."' AND batch_code = '".$batch_code."' AND production_run = 'yes'";
					$production_result = $con->query($production_query);
					
					
			}
			
	$table_name = $_SESSION['db_prefix']."_product_stock_count";
	$report_query = "INSERT INTO $table_name (stock_take_date, product_item_id, product_item_name, batch_code, category, sub_category, old_stock, new_stock, unit_price, user_name) VALUES ('".$stock_take_date."' ,'".$product_item_id."','".$product_item_name."','".$batch_code."','".$category."','".$subCategory."', '".$balance."' ,'".$counted_qty."','".$price."','".$userId."')";
	$report_result = $con->query($report_query);
					
	$table_name = $_SESSION['db_prefix']."_product_store_lines";
	$stock_quantity_query = "SELECT SUM(quantity_in-quantity_out) AS stock_quantity FROM $table_name WHERE product_item_id = '".$product_item_id."'  ";
	$stock_quantity_result = $con->query($stock_quantity_query);
	$row = mysqli_fetch_assoc($stock_quantity_result);
	$stock_quantity = $row['stock_quantity'];
	
	$table_name3 = $_SESSION['db_prefix']."_product_store";
	$count_query = "UPDATE $table_name3 SET quantity='".$stock_quantity."', stockTake='".$stockTake."' WHERE product_item_id = '".$product_item_id."' ";
	$count_result = $con->query($count_query);
	


	$table_name4 = $_SESSION['db_prefix']."_product_history";
	$history_query = "INSERT INTO $table_name4 (product_item_id, entry, reference, reference_2, quantity, total, date, entryDate, userId) VALUES ('".$product_item_id."','5','5', '".$batch_code."', '".$difference."',  '".$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']."', '22', '".$product_item_id."', '".$difference."' ,'".$counted_qty."', '".$batch_code."' )";
	$tracking_result = $con->query($tracking_query);
	
	$i++;	
	}

?>
	   
       
    <script language="javascript" type="text/javascript">
         window.location="sales_products_stock_report.php?stock_take_date=<?php echo $stock_take_date ?>";
</script>