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

date_default_timezone_set('Africa/Gaborone');
$entryDate = date("Y-m-d H:i:s"); 

$today = date("Y-m-d");

$receive_id = $_POST['receive_id'];
$line_num = $_POST['line_num'];
$line_num0 = $_POST['line_num0'];


$i=0;
while ($i<$line_num0) {
	$store_item_id = $_POST['store_item_id'.$i];
	
	$table_name = $_SESSION['db_prefix']."_receive_store_lines";
	$saved_item_query = "SELECT quantity, price  FROM $table_name WHERE receive_id = '".$receive_id."' AND store_item_id = '".$store_item_id."'";
	$saved_item_result = mySQL_query($saved_item_query);
	$saved_item_num = mysql_num_rows($saved_item_result);
	$quantity_saved = mysql_result($saved_item_result, 0, 'quantity');
	$price_saved = mysql_result($saved_item_result, 0, 'price');
	
	$quantity = $_POST['quantity_text'.$i];
	$price = $_POST['amount_text'.$i];
	
	$table_name = $_SESSION['db_prefix']."_receive_store_lines";
	$edit_query = "UPDATE $table_name SET quantity = '".$quantity."', price = '".$price."' WHERE receive_id = '".$receive_id."' AND store_item_id = '".$store_item_id."'";
	execsql($edit_query);
	
	
	if ($quantity_saved!=$quantity) {
		
	$table_name = $_SESSION['db_prefix']."_store_items";
	$stock_query = "SELECT quantity FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
	$stock_result = mySQL_query($stock_query);
	$stock_quantity = mysql_result($stock_result, 0, "quantity");
	
	$adjust_quantity = $quantity-$quantity_saved;
	$new_stock_quantity = $stock_quantity + $adjust_quantity;	
	
	$table_name = $_SESSION['db_prefix']."_store_items";
	$quantity_query = "UPDATE $table_name SET quantity = '".$new_stock_quantity."' WHERE store_item_id = '".$store_item_id."' ";
	execsql($quantity_query);	
	
	$table_name2 = $_SESSION['db_prefix']."_item_history";	
	$historyQuery = "INSERT INTO $table_name2 (store_item_id, entry, reference, reference_2, reference_3, quantity, total, price, date, entryDate, userId) VALUES ('".$store_item_id."', '21', '".$receive_id."','', '', '".$adjust_quantity."', '".$new_stock_quantity."', '".$price."', '".$entryDate."','".$entryDate."','".$_SESSION['username']."')";
	$historyResult = mySQL_query($historyQuery);
		
	$table_name = $_SESSION['db_prefix']."_tracking_store";	
	$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '190', '".$receive_id."' , '".$quantity_saved."','".$quantity."', '".$store_item_id."' )";
$trackingResult = mySQL_query($trackingQuery);
	}
	
	
	if ($price_saved!=$price) {
	
	$table_name = $_SESSION['db_prefix']."_store_items";
	$price_query = "UPDATE $table_name SET price = '".$price."' WHERE store_item_id = '".$store_item_id."' ";
	execsql($price_query);	
		
	$table_name = $_SESSION['db_prefix']."_tracking_store";
	$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '191', '".$receive_id."' , '".$price_saved."','".$price."', '".$store_item_id."' )";
$trackingResult = mySQL_query($trackingQuery);
	}
	
	$i++;
}

if ($line_num>$line_num0) {
	   
	   
$j=$line_num0;
while ($j<$line_num) {
	$store_item_id = $_POST['store_item_id'.$j];
	$quantity = $_POST['quantity_text'.$j];
	$price = $_POST['amount_text'.$j];
	
	
	$table_name = $_SESSION['db_prefix']."_receive_store_lines";
	$add_query = "INSERT INTO $table_name (receive_id, store_item_id, quantity, price  ) VALUES ('".$receive_id."', '".$store_item_id."', '".$quantity."', '".$price."' ) ";
	
	execsql($add_query);
	
	$table_name = $_SESSION['db_prefix']."_store_items";
	$stock_query = "SELECT quantity FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
	$stock_result = mySQL_query($stock_query);
	$stock_quantity = mysql_result($stock_result, 0, "quantity");
	
	
	$new_stock_quantity = $stock_quantity + $quantity;
	
	$table_name = $_SESSION['db_prefix']."_store_items";
	$quantity_query = "UPDATE $table_name SET quantity = '".$new_stock_quantity."' WHERE store_item_id = '".$store_item_id."' ";
	execsql($quantity_query);	
	
	$table_name2 = $_SESSION['db_prefix']."_item_history";	
	$historyQuery = "INSERT INTO $table_name2 (store_item_id, entry, reference, reference_2, reference_3, quantity, total, price, date, entryDate, userId) VALUES ('".$store_item_id."', '21', '".$receive_id."','', '', '".$quantity."', '".$new_stock_quantity."', '".$price."', '".$entryDate."','".$entryDate."','".$_SESSION['username']."')";
	$historyResult = mySQL_query($historyQuery);
		
	$table_name = $_SESSION['db_prefix']."_tracking_store";	
	$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '190', '".$receive_id."' , '0','".$quantity."', '".$store_item_id."' )";
$trackingResult = mySQL_query($trackingQuery);
	
	
	$j++;
}
	   
	   
	   
	   
}


if (isset($_POST['save_close_button']) )
	   { ?>
       <script>
	 		 window.location="receive_note.php?receive_id=<?php echo $receive_id?>";
		</script>
        <?php		 
	   }
	   
	   if (isset($_POST['save_button']) )
	   {  ?>
       <script>
	           window.location="edit_receive_note.php?receive_id=<?php echo $receive_id?>";
	   </script>
        <?php		 
	   }
	   ?>

