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

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

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

$purchase_id = $_POST['purchase_id'];

$table_name = $_SESSION['db_prefix']."_purchase";
$saved_item_query = "SELECT purchase_date, supplier_id, ordered_by, company  FROM $table_name WHERE purchase_id = '".$purchase_id."' ";
$saved_item_result = mySQL_query($saved_item_query);
$saved_item_num = mysql_num_rows($saved_item_result);

$purchase_date_saved = mysql_result($saved_item_result, 0, 'purchase_date');
$supplier_id_saved = mysql_result($saved_item_result, 0, 'supplier_id');
$ordered_by_saved = mysql_result($saved_item_result, 0, 'ordered_by');
$company_saved = mysql_result($saved_item_result, 0, 'company');


$purchase_date = $_POST['purchase_date'];
$supplier_id = $_POST['supplier_id_select'];
$ordered_by = $_POST['ordered_by_select'];
$company = $_POST['company_select'];


$table_name = $_SESSION['db_prefix']."_purchase";
$edit_query = "UPDATE $table_name SET purchase_date = '".$purchase_date."', supplier_id = '".$supplier_id."', ordered_by = '".$ordered_by."', company = '".$company."' WHERE purchase_id = '".$purchase_id."' ";
execsql($edit_query);

$table_name = $_SESSION['db_prefix']."_tracking_store";

if ($purchase_date_saved!=$purchase_date) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '183', '".$purchase_id."' , '".$purchase_date_saved."','".$purchase_date."', 'Purchase date' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($supplier_id_saved!=$supplier_id) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '184', '".$purchase_id."' , '".$supplier_id_saved."','".$supplier_id."', 'Supplier' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($ordered_by_saved!=$ordered_by) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '183', '".$purchase_id."' , '".$ordered_by_saved."','".$ordered_by."', 'Ordered by' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($company_saved!=$company) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '183', '".$purchase_id."' , '".$company_saved."','".$company."', 'Company' )";
$trackingResult = mySQL_query($trackingQuery);
}


$line_num = $_POST['line_num'];
$i=0;
while ($i<$line_num) {
	$store_item_id = $_POST['store_item_id'.$i];
	//echo '<br>*****'.$line_num;
if (isset($_POST['line_checkbox'.$i])) {
	//echo '<br>*****'.$line_num;
	
	$quantity = $_POST['quantity_text'.$i];
	$price = $_POST['amount_text'.$i];
	
	$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);
	$item_name = mysql_result($name_result, 0, "item_name");
	
		
	$table_name = $_SESSION['db_prefix']."_purchase_lines";
	$saved_query = "SELECT quantity, price FROM $table_name WHERE purchase_id = '".$purchase_id."' AND store_item_id = '".$store_item_id."' ";
	$saved_result = mySQL_query($saved_query);
	$saved_num = mysql_num_rows($saved_result);
	if ($saved_num>0) {
	       $saved_quantity = mysql_result($saved_result, 0, "quantity");
		   $saved_price = mysql_result($saved_result, 0, "price");
		   $table_name = $_SESSION['db_prefix']."_purchase_lines";
			$update_query = "UPDATE $table_name SET store_item_id = '".$store_item_id."', special_order_id = '0', order_no = '', quantity = '".$quantity."', price = '".$price."' WHERE purchase_id = '".$purchase_id."' AND store_item_id = '".$store_item_id."' ";
			//echo '<br>*****'.$update_query;
	execsql($update_query);
	
	if (($saved_quantity!=$quantity) AND ($quantity!=0)) {
		$table_name2 = $_SESSION['db_prefix']."_tracking_store";
	    $trackingQuery = "INSERT INTO $table_name2 (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '183', '".$purchase_id."' , '".$saved_quantity."','".$quantity."', 'Quantity for ".$item_name."' )";
		
		$trackingResult = mySQL_query($trackingQuery);
	}
		
	if (($saved_price!=$price) AND ($quantity!=0)) {
		$table_name2 = $_SESSION['db_prefix']."_tracking_store";
	    $trackingQuery = "INSERT INTO $table_name2 (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '183', '".$purchase_id."' , '".$saved_price."','".$price."', 'Price for ".$item_name."' )";
		$trackingResult = mySQL_query($trackingQuery);	
}
		   
	}
	else {
		$saved_quantity = 0;
		
		$table_name = $_SESSION['db_prefix']."_store_items";
		$item_query = "SELECT item_name, price FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
		$item_result = mySQL_query($item_query);
		$item_name = mysql_result($item_result, 0, "item_name");
		//$price = mysql_result($item_result, 0, "price");
		
		$table_name2 = $_SESSION['db_prefix']."_tracking_store"; 
		 $trackingQuery = "INSERT INTO $table_name2 (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '186', '".$purchase_id."' , '".$saved_quantity."','".$quantity."', '".$item_name."' )";
		 $trackingResult = mySQL_query($trackingQuery);
		
		$table_name = $_SESSION['db_prefix']."_purchase_lines";
		$replace_query = "INSERT INTO $table_name (purchase_id, store_item_id, special_order_id, item_name, order_no, quantity, price ) VALUES ('".$purchase_id."', '".$store_item_id."', '0', '".$item_name."', '', '".$quantity."', '".$price."'  ) ";
		//echo '<br>*****'.$replace_query;
		execsql($replace_query);
	}
	
	if ($quantity==0) {
		   	$table_name = $_SESSION['db_prefix']."_store_items";
			$item_query = "SELECT item_name, price FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
			$item_result = mySQL_query($item_query);
			$item_name = mysql_result($item_result, 0, "item_name");
		
		   	$table_name = $_SESSION['db_prefix']."_purchase_lines";
			$delete_query = "DELETE FROM $table_name WHERE purchase_id = '".$purchase_id."' AND store_item_id = '".$store_item_id."' ";
			//echo '<br>*****1'.$delete_query;
			execsql($delete_query);
				
		$table_name2 = $_SESSION['db_prefix']."_tracking_store";
		$trackingQuery = "INSERT INTO $table_name2 (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '183', '".$purchase_id."' , '".$saved_quantity."','".$quantity."', '".$item_name."' )";
		$trackingResult = mySQL_query($trackingQuery);	
	}
	}
	else {
		
			$table_name = $_SESSION['db_prefix']."_store_items";
			$item_query = "SELECT item_name, price FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
			$item_result = mySQL_query($item_query);
			$item_num = mysql_num_rows($item_result);
			if ($item_num > 0) {
			$item_name = mysql_result($item_result, 0, "item_name");
			
		  	$table_name = $_SESSION['db_prefix']."_purchase_lines";
			$delete_query = "DELETE FROM $table_name WHERE purchase_id = '".$purchase_id."' AND store_item_id = '".$store_item_id."' ";
			//echo '<br>*****2'.$delete_query;
			execsql($delete_query);
				
		$table_name2 = $_SESSION['db_prefix']."_tracking_store";
		$trackingQuery = "INSERT INTO $table_name2 (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '185', '".$purchase_id."' , '','', '".$item_name."' )";
		$trackingResult = mySQL_query($trackingQuery);
			}	
	}
	
$i++;	
}


$line2_num = $_POST['line_num2'];
$i=0;
while ($i<$line2_num) {
	
	if (isset($_POST['line_checkbox2'.$i])) {
	$special_order_id = $_POST['special_order_id2'.$i];
	//echo '<br>*****'.$i.'='.$special_order_id;
	//$job_no = substr($_POST['item_select2'.$i], 0, 3);
	//$item_name = substr($_POST['item_select2'.$i], 7);
	$quantity2 = $_POST['quantity_text2'.$i];
	$price = $_POST['amount_text2'.$i];
			
	$table_name = $_SESSION['db_prefix']."_purchase_lines";
	$saved_query = "SELECT quantity, price, item_name FROM $table_name WHERE purchase_id = '".$purchase_id."' AND special_order_id = '".$special_order_id."'  ";
	$saved_result = mySQL_query($saved_query);
	$saved_num = mysql_num_rows($saved_result);
	if ($saved_num>0) {
	       $saved_quantity = mysql_result($saved_result, 0, "quantity");
		   $saved_price = mysql_result($saved_result, 0, "price");
		   $item_name = mysql_result($saved_result, 0, "item_name");
		   $table_name = $_SESSION['db_prefix']."_purchase_lines";
			$update_query = "UPDATE $table_name SET quantity = '".$quantity2."', price = '".$price."' WHERE purchase_id = '".$purchase_id."' AND special_order_id = '".$special_order_id."' AND store_item_id = '0'";
			//echo '<br>*****'.$update_query;
	execsql($update_query);
	
	if (($saved_quantity!=$quantity2) AND ($quantity2!=0)) {
		$table_name2 = $_SESSION['db_prefix']."_tracking_store";
	    $trackingQuery = "INSERT INTO $table_name2 (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '183', '".$purchase_id."' , '".$saved_quantity."','".$quantity2."', 'Quantity for ".$item_name."' )";
		$trackingResult = mySQL_query($trackingQuery);
	}
		
	if (($saved_price!=$price) AND ($quantity2!=0)) {
		$table_name2 = $_SESSION['db_prefix']."_tracking_store";
	    $trackingQuery = "INSERT INTO $table_name2 (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '183', '".$purchase_id."' , '".$saved_price."','".$price."', 'Price for ".$item_name."' )";
		$trackingResult = mySQL_query($trackingQuery);	
}
		   
	}
	else {
		$saved_quantity = 0;
		$table_name = $_SESSION['db_prefix']."_special_orders";
		$item_query = "SELECT special_name, job_card_no FROM $table_name WHERE special_order_id = '".$special_order_id."' ";
		$item_result = mySQL_query($item_query);
		$special_name = mysql_result($item_result, 0, "special_name");
		$job_card_no = mysql_result($item_result, 0, "job_card_no");
		//$price_ordered = mysql_result($item_result, 0, "price_ordered");
		
		
		$table_name2 = $_SESSION['db_prefix']."_tracking_store"; 
		 $trackingQuery = "INSERT INTO $table_name2 (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '186', '".$purchase_id."' , '".$saved_quantity."','".$quantity2."', '".$special_name."' )";
		 $trackingResult = mySQL_query($trackingQuery);
		
		$table_name = $_SESSION['db_prefix']."_purchase_lines";
		$replace_query = "INSERT INTO $table_name (purchase_id, special_order_id, store_item_id, item_name, order_no, quantity, price ) VALUES ('".$purchase_id."', '".$special_order_id."', '0', '".$special_name."', '".$job_card_no."', '".$quantity2."', '".$price."'  ) ";
		execsql($replace_query);
		//echo '<br>*****'.$replace_query;
	}
	
	if ($quantity2==0) {
		    $table_name = $_SESSION['db_prefix']."_purchase_lines";
			$item_query = "SELECT item_name FROM $table_name WHERE special_order_id = '".$special_order_id."' ";
			$item_result = mySQL_query($item_query);
			$item_name = mysql_result($item_result, 0, "item_name");
		 
		   	$table_name = $_SESSION['db_prefix']."_purchase_lines";
			$delete_query = "DELETE FROM $table_name WHERE purchase_id = '".$purchase_id."' AND special_order_id = '".$special_order_id."'  ";
			//echo '<br>*****3'.$delete_query;
			execsql($delete_query);
				
		$table_name2 = $_SESSION['db_prefix']."_tracking_store";
		$trackingQuery = "INSERT INTO $table_name2 (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '185', '".$purchase_id."' , '','', '".$item_name."' )";
		$trackingResult = mySQL_query($trackingQuery);	
	}
	}
	else {
		//echo '<br>*****'.'to delete line';
		$special_order_id = $_POST['special_order_id2'.$i];
		
		$table_name = $_SESSION['db_prefix']."_purchase_lines";
			$item_query = "SELECT item_name FROM $table_name WHERE special_order_id = '".$special_order_id."' ";
			$item_result = mySQL_query($item_query);
			$item_name = mysql_result($item_result, 0, "item_name");
		
		
			$table_name = $_SESSION['db_prefix']."_purchase_lines";
			$delete_query = "DELETE FROM $table_name WHERE purchase_id = '".$purchase_id."'  AND special_order_id <> '0'  ";
			//echo '<br>*****4'.$delete_query;
			execsql($delete_query);
			
		if ($item_name!='') {				
		$table_name2 = $_SESSION['db_prefix']."_tracking_store";
		$trackingQuery = "INSERT INTO $table_name2 (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '185', '".$purchase_id."' , '','', '".$item_name."' )";
		$trackingResult = mySQL_query($trackingQuery);
		}
	}
	
$i++;	
}


//echo '<br>*****'.'purchased order saved';

if (isset($_POST['save_close_button']) )
	   { ?>
       <script>
	 		 window.location="purchase_order.php?purchase_id=<?php echo $purchase_id?>&message=0";
		</script>
        <?php		 
	   }
	   
	   if (isset($_POST['save_button']) )
	   {  ?>
       <script>
	           window.location="edit_purchase_order.php?purchase_id=<?php echo $purchase_id?>";
	   </script>
        <?php		 
	   }
	   ?>

