<?php
include('../include/session.php');
date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s", time()); 
$today = date("Y-m-d");
$receive_id = $_GET['receive_id'];
$receive_date = $_GET['receive_date'];


$table_name = $_SESSION['db_prefix']."_receive_store";
$saved_date_query = "SELECT receive_date FROM $table_name WHERE receive_id = '".$receive_id."' ";
$saved_date_result = mySQL_query($saved_date_query);
$saved_date_num = mysql_num_rows($saved_date_result);
$saved_receive_date = mysql_result($saved_date_result, 0, 'receive_date');

if ($saved_receive_date!=$receive_date) {
		 	$table_name = $_SESSION['db_prefix']."_receive_store";
	   		$date_update_query = "UPDATE $table_name SET receive_date = '".$receive_date."' WHERE receive_id = '".$receive_id."' ";
	   		execsql($date_update_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 ('".$entry_date."' ,'".$_SESSION['username']."', '35', '".$receive_id."' ,'".$saved_receive_date."','".$receive_date."','' )";
	   		execsql($trackingQuery);
}


$table_name = $_SESSION['db_prefix']."_receive_store_lines";
$saved_order_query = "SELECT store_item_id, quantity, purchase_price FROM $table_name WHERE receive_id = '".$receive_id."' ";
$saved_order_result = mySQL_query($saved_order_query);
$saved_order_num = mysql_num_rows($saved_order_result);
$i=0;
while ($i<$saved_order_num) {
$saved_store_item_id = mysql_result($saved_order_result, $i, 'store_item_id');
$saved_quantity = mysql_result($saved_order_result, $i, 'quantity');
$saved_price = mysql_result($saved_order_result, $i, 'purchase_price');


$store_item_id = $_GET['store_item_id'.$i];
$quantity = $_GET['quantity_text'.$i];
$price = $_GET['price_text'.$i];

//if ($quantity<>0) {

if ($saved_store_item_id!=$store_item_id) {
	    
		$table_name = $_SESSION['db_prefix']."_store_items";
		$name_query = "SELECT price FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
		$name_result = mySQL_query($name_query);
		
	
	   $table_name = $_SESSION['db_prefix']."_receive_store_lines";
	   $tile_update_query = "UPDATE $table_name SET store_item_id = '".$store_item_id."' WHERE receive_id = '".$receive_id."'  AND store_item_id = '".$saved_store_item_id."' ";
	   execsql($tile_update_query);
	   
	   $table_name = $_SESSION['db_prefix']."_store_items";
	   $name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
	   $name_result = mySQL_query($name_query);
	   $saved = mysql_result($name_result, 0, "item_name");
	   
	   $saved_quantity=0;
	   $diff_quantity = $quantity;
		
		$table_name2 = $_SESSION['db_prefix']."_store_items";
   		$stock_total_query = "SELECT quantity FROM $table_name2 WHERE store_item_id = '".$store_item_id."'";
   		$stock_total_result = mySQL_query($stock_total_query);
   		$total_quantity = mysql_result($stock_total_result ,0, "quantity");
	
   		$total_quantity = $total_quantity + $diff_quantity;
      
   		$stockQuery = "UPDATE $table_name2 SET supplier_id = '".$_SESSION['supplier_id']."', quantity = '".$total_quantity."' , lastModDate = '".$_SESSION['receive_date']."', purchase_price = '".$price."'  WHERE store_item_id = '".$store_item_id."'";
		$stockResult = mySQL_query($stockQuery);
		
		
		$table_name3 = $_SESSION['db_prefix']."_item_history";
		$historyQuery = "INSERT INTO $table_name3 (store_item_id, entry, reference, quantity, total, date, entryDate, userId) VALUES ('".$store_item_id."', '35', '".$receive_id."','".$diff_quantity."', '".$total_quantity."','".$today."', '".$entry_date."','".$_SESSION['username']."')";
		execsql($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 ('".$entry_date."' ,'".$_SESSION['username']."', '33', '".$receive_id."' ,'".$saved_quantity."','".$quantity."','".$store_item_id."' )";
	   execsql($trackingQuery);
		
		
}

$table_name = $_SESSION['db_prefix']."_receive_store_lines";
$update_query = "UPDATE $table_name SET quantity = '".$quantity."', purchase_price = '".$price."' WHERE receive_id = '".$receive_id."'  AND store_item_id = '".$store_item_id."' ";
execsql($update_query);

if ($saved_quantity!=$quantity) {
		$table_name = $_SESSION['db_prefix']."_store_items";
		$name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$saved_store_item_id."'  ";
		$name_result = mySQL_query($name_query);
		$item = mysql_result($name_result, 0, "item_name");
		
		$diff_quantity = $quantity-$saved_quantity;
		
		$table_name2 = $_SESSION['db_prefix']."_store_items";
   		$stock_total_query = "SELECT quantity FROM $table_name2 WHERE store_item_id = '".$saved_store_item_id."'";
   		$stock_total_result = mySQL_query($stock_total_query);
   		$total_quantity = mysql_result($stock_total_result ,0, "quantity");
	
   		$total_quantity = $total_quantity + $diff_quantity;
      
   		$stockQuery = "UPDATE $table_name2 SET supplier_id = '".$_SESSION['supplier_id']."', quantity = '".$total_quantity."' , lastModDate = '".$_SESSION['receive_date']."', purchase_price = '".$price."'  WHERE store_item_id = '".$store_item_id."'";
		$stockResult = mySQL_query($stockQuery);
		
		
		$table_name3 = $_SESSION['db_prefix']."_item_history";
		$historyQuery = "INSERT INTO $table_name3 (store_item_id, entry, reference, quantity, total, date, entryDate, userId) VALUES ('".$store_item_id."', '35', '".$receive_id."','".$diff_quantity."', '".$total_quantity."','".$today."', '".$entry_date."','".$_SESSION['username']."')";
		execsql($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 ('".$entry_date."' ,'".$_SESSION['username']."', '33', '".$receive_id."' ,'".$saved_quantity."','".$quantity."','".$saved_store_item_id."' )";
	   execsql($trackingQuery);
}
	   
if ($saved_price!=$price) {
		$table_name = $_SESSION['db_prefix']."_store_items";
		$name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$saved_store_item_id."'  ";
		$name_result = mySQL_query($name_query);
		$item = mysql_result($name_result, 0, "item_name");
	
	   $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 ('".$entry_date."' ,'".$_SESSION['username']."', '34', '".$receive_id."' ,'".$saved_price."','".$price."','".$saved_store_item_id."' )";
	   execsql($trackingQuery);

}
$i++;
}

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

