<?php
include('../include/session.php');
date_default_timezone_set('Africa/Khartoum');
$entryDate = date("Y-m-d H:i:s",time()-3600); 

$quote_no = $_POST['quote_no_text'];
$cust_id = $_POST['cust_id_text'];


$table_name = $_SESSION['db_prefix']."_quotation_lines";
$quote_line_query = "SELECT * FROM $table_name WHERE quote_number = '".$quote_no."' ";
$quote_line_result = mySQL_query($quote_line_query);
$quote_line_num = mysql_num_rows($quote_line_result);



$i=0;
while ($i<3) {
$saved_quote_item_text[$i]='';
$saved_quote_item_price[$i]='';
$i++;
}

$i=0;
while ($i<$quote_line_num) {
$saved_line_id[$i] = mysql_result($quote_line_result, $i, 'line_id');	
$saved_quote_item_text[$i] = mysql_result($quote_line_result, $i, 'quote_item_text');
$saved_quote_item_price[$i] = mysql_result($quote_line_result, $i, 'quote_item_price');
$i++;
}

$table_name = $_SESSION['db_prefix']."_quotation_lines";
$delete_query = "DELETE FROM $table_name WHERE  quote_number = '".$quote_no."' ";
execsql($delete_query);

$quote_value = 0;
$j=0;
while ($j<3) {
	$quote_item_text[$j] = $_POST['quote_line_text'.$j];
	$quote_item_price[$j] = $_POST['quote_price_text'.$j];
	$quote_value = $quote_value + $quote_item_price[$j];
	if ($quote_item_text[$j]!='') {
		  	$table_name = $_SESSION['db_prefix']."_quotation_lines";
			$quote_query = "INSERT INTO $table_name (quote_number, quote_item_text, quote_item_price) VALUES ('".$quote_no."', '".$quote_item_text[$j]."', '".$quote_item_price[$j]."' ) ";
			execsql($quote_query);
	}
$j++;
}

$table_name = $_SESSION['db_prefix']."_quotations";
$update_query = "UPDATE $table_name SET value = '".$quote_value."' WHERE quote_no = '".$quote_no."' ";
execsql($update_query);



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

$k=0;
while ($k<3) {	
if ( ($saved_quote_item_text[$k]!='') || ($quote_item_text[$k]!='') ) {	
if ($saved_quote_item_text[$k]!=$quote_item_text[$k]) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '44','".$quote_no."', '".$cust_id."' ,'".$saved_quote_item_text[$k]."','".$quote_item_text[$k]."' )";
		//echo '<br>id_number'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}
}

if ( ($saved_quote_item_price[$k]!='') || ($quote_item_price[$k]!='') ) {	
if ($saved_quote_item_price[$k]!=$quote_item_price[$k]) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '44', '".$quote_no."', '".$cust_id."' ,'".$saved_quote_item_price[$k]."','".$quote_item_price[$k]."' )";
		//echo '<br>id_number'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}
}

$k++;
}


if (isset($_POST['save_close_button']) )
	   { ?>
       <script>
	 		 window.location="../sales/sales_main.php?date=0";
		</script>
        <?php		 
	   }
	   
	if (isset($_POST['save_button']) )
	   {  ?>
       <script>
	          window.location="quotation_details_edit.php?quote_no=<?php echo $quote_no?>"
	   </script>
        <?php		 
	   }
	   ?>

