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


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

$quotation_date =$_POST['quotation_date'];
$terms =$_POST['terms_radio_group'];
$vat =$_POST['vat_radio_group'];
$valid_days =$_POST['valid_days_select'];
$delivery_days =$_POST['delivery_days_select'];

if (isset($_POST['picture_checkbox']))   {
	$picture='yes';
	} 
	else {
		$picture='';
		}

$table_name = $_SESSION['db_prefix']."_quotations";
$saved_quote_query = "SELECT quotation_date, terms, vat_text, valid_days, delivery_days, picture FROM $table_name WHERE quote_no = '".$quote_no."' ";
$saved_quote_result = mySQL_query($saved_quote_query);
$saved_quote_num = mysql_num_rows($saved_quote_result);
$saved_quotation_date = mysql_result($saved_quote_result, 0, "quotation_date");
$saved_terms = mysql_result($saved_quote_result, 0, "terms");
$saved_vat = mysql_result($saved_quote_result, 0, "vat_text");
$saved_valid_days = mysql_result($saved_quote_result, 0, "valid_days");
$saved_delivery_days = mysql_result($saved_quote_result, 0, "delivery_days");


$table_name = $_SESSION['db_prefix']."_quotations";
$update_query = "UPDATE $table_name SET quotation_date = '".$quotation_date."', terms = '".$terms."', vat_text = '".$vat."', valid_days = '".$valid_days."', delivery_days = '".$delivery_days."', picture = '".$picture."' WHERE quote_no = '".$quote_no."' ";
execsql($update_query);

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

if ($saved_quotation_date!=$quotation_date) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '59','".$quote_no."', '".$cust_id."' ,'".$saved_quotation_date."','".$quotation_date."' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($saved_terms!=$terms) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '60','".$quote_no."', '".$cust_id."' ,'".$saved_terms."','".$terms."' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($saved_vat!=$vat) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '61','".$quote_no."', '".$cust_id."' ,'".$saved_vat."','".$vat."' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($saved_valid_days!=$valid_days) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '62','".$quote_no."', '".$cust_id."' ,'".$saved_valid_days."','".$valid_days."' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($saved_delivery_days!=$delivery_days) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '63','".$quote_no."', '".$cust_id."' ,'".$saved_delivery_days."','".$delivery_days."' )";
$trackingResult = mySQL_query($trackingQuery);
}



if ($project=='Electric fencing') {

$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<$quote_line_num) {
$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);

$j=0;

while ($j<$quote_line_num) {
	$quote_item_text[$j] = $_POST['quote_line_text'.$j];
	$quote_item_price[$j] = $_POST['quote_price_text'.$j];
	
	if ($j=='2') {
		  	$table_name = $_SESSION['db_prefix']."_quotations";
			$price_update_query = "UPDATE $table_name SET value = '".$quote_item_price[$j]."' WHERE quote_no = '".$quote_no."' ";
			execsql($price_update_query);
	}
	
	
	if ($quote_item_text[$j]!='') {
		  	$table_name = $_SESSION['db_prefix']."_quotation_lines";
			$quote_query = "INSERT INTO $table_name (quote_number, quote_option, quote_item_text, quote_item_price) VALUES ('".$quote_no."', '".$j."', '".$quote_item_text[$j]."', '".$quote_item_price[$j]."' ) ";
			execsql($quote_query);
	}
$j++;
}
	
$table_name = $_SESSION['db_prefix']."_tracking_sales";

$k=0;
while ($k<$quote_line_num) {

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_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '54', '".$quote_no."', '".$quote_item_text[$k]."' ,'".$saved_quote_item_price[$k]."','".$quote_item_price[$k]."' )";
		//echo '<br>id_number'.$trackingQuery;
$trackingResult = mySQL_query($trackingQuery);
}
}

$k++;
}
}


if ( ($project=='Manase 358') OR ($project=='Gate') OR ($project=='Razor wire') OR ($project=='Wall spikes') OR ($project=='Farm fence') OR ($project=='Free standing electric fence') OR ($project=='Miscelaneous') ) {

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


$table_name = $_SESSION['db_prefix']."_quotation_lines";
$quote_line_query = "SELECT * FROM $table_name WHERE quote_number = '".$quote_no."' AND (quote_option = '1' OR quote_option = '11') ORDER BY quote_option ASC";
$quote_line_result = mySQL_query($quote_line_query);
$quote_line_num = mysql_num_rows($quote_line_result);
$saved_quote_option_text = mysql_result($quote_line_result, 0, "quote_option_text");

$i=0;
while ($i<$quote_line_num) {
$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');
$quote_option[$i] = mysql_result($quote_line_result, $i, 'quote_option');
$i++;
}

$table_name = $_SESSION['db_prefix']."_quotation_lines";
$delete_query = "DELETE FROM $table_name WHERE  quote_number = '".$quote_no."' AND (quote_option = '1' OR quote_option = '11') ";
execsql($delete_query);
$main_price=0;
$j=0;
while ($j<$quote_line_num) {
	$quote_item_text[$j] = $_POST['quote_line_text'.$j];
	if ($quote_option[$j]=='11') {
	$quote_item_price[$j] = $_POST['quote_price_text'.$j];
	if($main_price==0) {
	$table_name = $_SESSION['db_prefix']."_quotations";
	$price_update_query = "UPDATE $table_name SET value = '".$quote_item_price[$j]."' WHERE quote_no = '".$quote_no."' ";
	execsql($price_update_query);
	$main_price=1;
	}
	}
	else {
		$quote_item_price[$j]=0;
	}
	if ($quote_item_text[$j]!='') {
		  	$table_name = $_SESSION['db_prefix']."_quotation_lines";
			$quote_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text, quote_item_text, quote_item_price) VALUES ('".$quote_no."', '".$quote_option[$j]."', '".$quote_option_text."', '".$quote_item_text[$j]."', '".$quote_item_price[$j]."' ) ";
			execsql($quote_query);
	}
$j++;
}


$quote_line2_query = "SELECT * FROM $table_name WHERE quote_number = '".$quote_no."' and (quote_option = '2' OR quote_option = '22') ORDER BY quote_option ASC ";
$quote_line2_result = mySQL_query($quote_line2_query);
$quote_line2_num = mysql_num_rows($quote_line2_result);
if ($quote_line2_num!=0) {
$saved_quote_option2_text = mysql_result($quote_line2_result, 0, "quote_option_text");
}
else {
	$saved_quote_option2_text='';
}

if ($saved_quote_option2_text!='') {
	
$table_name = $_SESSION['db_prefix']."_quotation_lines";
$quote_line_query = "SELECT * FROM $table_name WHERE quote_number = '".$quote_no."' AND (quote_option = '2' OR quote_option = '22')  ORDER BY quote_option ASC ";
$quote_line_result = mySQL_query($quote_line_query);
$quote_line_num = mysql_num_rows($quote_line_result);
$saved_quote_option_text = mysql_result($quote_line_result, 0, "quote_option_text");
	
$quote_option2_text = $_POST['quote_option2_text'];
	
	
$i=0;
while ($i<$quote_line2_num) {
$saved_quote_item2_text[$i]='';
$saved_quote_item2_price[$i]='';
$i++;


$i=0;
while ($i<$quote_line2_num) {
$saved_line_id[$i] = mysql_result($quote_line2_result, $i, 'line_id');	
$saved_quote_item2_text[$i] = mysql_result($quote_line2_result, $i, 'quote_item_text');
$saved_quote_item2_price[$i] = mysql_result($quote_line2_result, $i, 'quote_item_price');
$quote_option[$i] = mysql_result($quote_line2_result, $i, 'quote_option');
$i++;
}
}

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


$j=0;
while ($j<$quote_line2_num) {
	$quote_item2_text[$j] = $_POST['quote_line2_text'.$j];
	if ($quote_option[$j]=='22') {
	$quote_item2_price[$j] = $_POST['quote_price2_text'.$j];
	}
	else {
		$quote_item2_price[$j]=0;
	}
	if ($quote_item2_text[$j]!='') {
		  	$table_name = $_SESSION['db_prefix']."_quotation_lines";
			$quote_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text, quote_item_text, quote_item_price) VALUES ('".$quote_no."', '".$quote_option[$j]."', '".$quote_option2_text."', '".$quote_item2_text[$j]."', '".$quote_item2_price[$j]."' ) ";
			execsql($quote_query);
	}
$j++;
}

}

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

if ($saved_quote_option_text!=$quote_option_text) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '57','".$quote_no."', '".$cust_id."' ,'".$saved_quote_option_text."','".$quote_option_text."' )";
$trackingResult = mySQL_query($trackingQuery);
}

$k=0;
while ($k<$quote_line_num) {
	
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_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '58', '".$quote_no."', '".$saved_quote_item_text[$k]."' ,'".$saved_quote_item_text[$k]."','".$quote_item_text[$k]."' )";
$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_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '54', '".$quote_no."', '".$quote_item_text[$k]."' ,'".$saved_quote_item_price[$k]."','".$quote_item_price[$k]."' )";
		//echo '<br>id_number'.$trackingQuery;
$trackingResult = mySQL_query($trackingQuery);
}
}
$k++;
}

if ($saved_quote_option2_text!='') {
if ($saved_quote_option2_text!=$quote_option2_text) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '57','".$quote_no."', '".$cust_id."' ,'".$saved_quote_option2_text."','".$quote_option2_text."' )";
$trackingResult = mySQL_query($trackingQuery);
}

$k=0;
while ($k<$quote_line2_num) {
	
if ( ($saved_quote_item2_text[$k]!='') || ($quote_item2_text[$k]!='') ) {	
if ($saved_quote_item2_text[$k]!=$quote_item2_text[$k]) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '58', '".$quote_no."', '".$saved_quote_item2_text[$k]."' ,'".$saved_quote_item2_text[$k]."','".$quote_item2_text[$k]."' )";
$trackingResult = mySQL_query($trackingQuery);
}
}

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

$k++;
}
}

}


if  ($project=='Diamond mesh') { 

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

$quote_option5_text = $_POST['quote_option_text'];

$table_name = $_SESSION['db_prefix']."_quotation_lines";
$quote_line_query = "SELECT * FROM $table_name WHERE quote_number = '".$quote_no."' AND (quote_option = '1' OR quote_option = '11')ORDER BY quote_option ASC";
$quote_line_result = mySQL_query($quote_line_query);
$quote_line_num = mysql_num_rows($quote_line_result);
$saved_quote_option_text = mysql_result($quote_line_result, 0, "quote_option_text");



$i=0;
while ($i<$quote_line_num) {
$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');
$quote_option[$i] = mysql_result($quote_line_result, $i, 'quote_option');
$i++;
}

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

$j=0;
while ($j<$quote_line_num) {
	$quote_item_text[$j] = $_POST['quote_line_text'.$j];
	if ($quote_option[$j]=='11') {
		  $quote_item_price[$j] = $_POST['price_text'.$j];
		  	if($main_price==0) {
			$table_name = $_SESSION['db_prefix']."_quotations";
			$price_update_query = "UPDATE $table_name SET value = '".$quote_item_price[$j]."' WHERE quote_no = '".$quote_no."' ";
			execsql($price_update_query);
			$main_price=1;
	}
	}
	else {
		$quote_item_price[$j] = '0.00';
	}
	
	if ($quote_item_text[$j]!='') {
		  	$table_name = $_SESSION['db_prefix']."_quotation_lines";
			$quote_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text, quote_item_text, quote_item_price ) VALUES ('".$quote_no."', '".$quote_option[$j]."', '".$quote_option_text."', '".$quote_item_text[$j]."', '".$quote_item_price[$j]."' ) ";
			execsql($quote_query);
	}
$j++;
}


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

if ($saved_quote_option_text!=$quote_option_text) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '57','".$quote_no."', '".$cust_id."' ,'".$saved_quote_option_text."','".$quote_option_text."' )";
$trackingResult = mySQL_query($trackingQuery);
}

$k=0;
while ($k<$quote_line_num) {
	
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_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '58', '".$quote_no."', '".$saved_quote_item_text[$k]."' ,'".$saved_quote_item_text[$k]."','".$quote_item_text[$k]."' )";
$trackingResult = mySQL_query($trackingQuery);
}
}

$k++;
}


}

if  ($project=='Palisade standard') { 

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

//$quote_option5_text = $_POST['quote_option_text'];

$height = $_POST['height_select'];

$table_name = $_SESSION['db_prefix']."_quotation_lines";
$quote_line_query = "SELECT * FROM $table_name WHERE quote_number = '".$quote_no."' AND (quote_option = '1' OR quote_option = '11')ORDER BY quote_option ASC";
$quote_line_result = mySQL_query($quote_line_query);
$quote_line_num = mysql_num_rows($quote_line_result);
$saved_quote_option_text = mysql_result($quote_line_result, 0, "quote_option_text");
//$saved_quote_item_price = mysql_result($quote_line_result, 0, "quote_item_price");



$i=0;
while ($i<$quote_line_num) {
$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');
$quote_option[$i] = mysql_result($quote_line_result, $i, 'quote_option');
$i++;
}

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

$j=0;
while ($j<$quote_line_num) {
	$quote_item_text[$j] = $_POST['quote_line_text'.$j];
	if ($quote_option[$j]=='11') {
		  $quote_item_price[$j] = $_POST['price_text'.$j];
		  	if($main_price==0) {
			$table_name = $_SESSION['db_prefix']."_quotations";
			$price_update_query = "UPDATE $table_name SET value = '".$quote_item_price[$j]."' WHERE quote_no = '".$quote_no."' ";
			execsql($price_update_query);
			$main_price=1;
	}
	}
	else {
		$quote_item_price[$j] = '0.00';
	}
	
	if ($quote_item_text[$j]!='') {
		  	$table_name = $_SESSION['db_prefix']."_quotation_lines";
			if ($quote_option[$j]==1) {
			$quote_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text, quote_item_text, quote_item_price ) VALUES ('".$quote_no."', '".$quote_option[$j]."', '".$height."', 'Fence height: ".$height."mm', '".$quote_item_price[$j]."' ) ";	
			}
			else {
			$quote_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text, quote_item_text, quote_item_price ) VALUES ('".$quote_no."', '".$quote_option[$j]."', '', '".$quote_item_text[$j]."', '".$quote_item_price[$j]."' ) ";
			}
			//echo '<br>'.$quote_query;
			execsql($quote_query);
	}
$j++;
}


$k=0;
while ($k<$quote_line_num) {
	
$table_name = $_SESSION['db_prefix']."_tracking_sales";

if ($quote_option[$k]=='1') {
if ($saved_quote_option_text!=$height) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '57','".$quote_no."', 'height' ,'".$saved_quote_option_text."','".$height."' )";
$trackingResult = mySQL_query($trackingQuery);
}
}
	
if ($quote_option[$k]=='11') {	

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_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '58', '".$quote_no."', '".$saved_quote_item_text[$k]."' ,'".$saved_quote_item_text[$k]."','".$quote_item_text[$k]."' )";
$trackingResult = mySQL_query($trackingQuery);
}
}

if ($saved_quote_item_price[$k]!=$quote_item_price[$k]) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '57', '".$quote_no."', 'price' ,'".$saved_quote_item_price[$k]."','".$quote_item_price[$k]."' )";
$trackingResult = mySQL_query($trackingQuery);
}

}

$k++;
}
}


if  ($project=='Palisade fencing') { 

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

$table_name = $_SESSION['db_prefix']."_quotation_lines";
$line_query = "SELECT quote_option_text, quote_item_text, quote_item_price  FROM $table_name WHERE quote_number = '".$quote_no."' ";
			
$line_result = mySQL_query($line_query);
$line_num = mysql_num_rows($line_result);
$i=0;
while ($i<$line_num) {
	$option_text = mysql_result($line_result, $i, "quote_option_text");
	$item_text = mysql_result($line_result, $i, "quote_item_text");
	$price = mysql_result($line_result, $i, "quote_item_price");
	$price = mysql_result($line_result, $i, "quote_item_price");
	if ($option_text=='Angle iron 40x40x3') {$saved_price1 = $price;}
	if ($option_text=='Devils fork')  {$saved_price2 = $price;}
	if ($option_text=='Alto') {$saved_price3 = $price;}
	if ($option_text=='Bermuda') {$saved_price4 = $price;}
	if ($option_text=='Carmel') {$saved_price5 = $price;}
	if ($option_text=='Devon') {$saved_price6 = $price;}
	if ($option_text=='Gate 1') {$saved_price7 = $price; $saved_gate7 = $item_text; }
	if ($option_text=='Gate 2') {$saved_price8 = $price; $saved_gate8 = $item_text; }
	if ($option_text=='Gate 3') {$saved_price9 = $price; $saved_gate9 = $item_text; }
	$i++;
	}


$price2 = $_POST['price2_text'];
$price7 = $_POST['price7_text'];
$price8 = $_POST['price8_text'];
$price9 = $_POST['price9_text'];
$gate7 = $_POST['gate7_text'];
$gate8 = $_POST['gate8_text'];
$gate9 = $_POST['gate9_text'];

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

$table_name = $_SESSION['db_prefix']."_quotation_lines";
$update_query = "UPDATE $table_name SET quote_item_price = '".$price2."' WHERE quote_option_text = 'Devils fork' AND quote_number = '".$quote_no."' ";
execsql($update_query);

$update_query = "UPDATE $table_name SET quote_item_text = '".$gate7."', quote_item_price = '".$price7."' WHERE quote_option_text = 'Gate 1' AND quote_number = '".$quote_no."' ";
execsql($update_query);
$update_query = "UPDATE $table_name SET quote_item_text = '".$gate8."', quote_item_price = '".$price8."' WHERE quote_option_text = 'Gate 2' AND quote_number = '".$quote_no."' ";
execsql($update_query);
$update_query = "UPDATE $table_name SET quote_item_text = '".$gate9."', quote_item_price = '".$price9."' WHERE quote_option_text = 'Gate 3' AND quote_number = '".$quote_no."' ";
execsql($update_query);


if ($saved_price2!=$price2) {
	$table_name = $_SESSION['db_prefix']."_tracking_sales";
	$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '64','".$quote_no."', 'Devils fork' ,'".$saved_price2."', '".$price2."' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($saved_price7!=$price7) {
	$table_name = $_SESSION['db_prefix']."_tracking_sales";
	$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '64','".$quote_no."', 'Gate 1' ,'".$saved_price7."', '".$price7."' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($saved_price8!=$price8) {
	$table_name = $_SESSION['db_prefix']."_tracking_sales";
	$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '64','".$quote_no."', 'Gate 2' ,'".$saved_price8."', '".$price8."' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($saved_price9!=$price9) {
	$table_name = $_SESSION['db_prefix']."_tracking_sales";
	$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '64','".$quote_no."', 'Gate 3' ,'".$saved_price9."', '".$price9."' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($saved_gate7!=$gate7) {
	$table_name = $_SESSION['db_prefix']."_tracking_sales";
	$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '65','".$quote_no."', 'Gate 1' ,'".$saved_gate7."', '".$gate7."' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($saved_gate8!=$gate8) {
	$table_name = $_SESSION['db_prefix']."_tracking_sales";
	$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '65','".$quote_no."', 'Gate 1' ,'".$saved_gate8."', '".$gate8."' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($saved_gate9!=$gate9) {
	$table_name = $_SESSION['db_prefix']."_tracking_sales";
	$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '65','".$quote_no."', 'Gate 1' ,'".$saved_gate9."', '".$gate9."' )";
$trackingResult = mySQL_query($trackingQuery);
}

}



if ( ($project=='Gate motor') OR ($project=='Pool fence') ) { 


$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."' and quote_option = '1' ";
$quote_line_result = mySQL_query($quote_line_query);
$quote_line_num = mysql_num_rows($quote_line_result);
$quote_option_text = mysql_result($quote_line_result, 0, 'quote_option_text');


$quote_line2_query = "SELECT * FROM $table_name WHERE quote_number = '".$quote_no."' and quote_option = '2' ";
$quote_line2_result = mySQL_query($quote_line2_query);
$quote_line2_num = mysql_num_rows($quote_line2_result);
$quote_option2_text = mysql_result($quote_line2_result, 0, 'quote_option_text');



$i=0;
while ($i<$quote_line_num) {
$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++;
}


$i=0;
while ($i<$quote_line2_num) {
$saved_quote_item2_text[$i]='';
$saved_quote_item2_price[$i]='';
$i++;


$i=0;
while ($i<$quote_line2_num) {
$saved_line_id[$i] = mysql_result($quote_line2_result, $i, 'line_id');	
$saved_quote_item2_text[$i] = mysql_result($quote_line2_result, $i, 'quote_item_text');
$saved_quote_item2_price[$i] = mysql_result($quote_line2_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);

$j=0;
while ($j<$quote_line_num) {
	$quote_item_text[$j] = $_POST['quote_line_text'.$j];
	$quote_item_price[$j] = $_POST['quote_price_text'.$j];
	if ($j=='0') {
		  	$table_name = $_SESSION['db_prefix']."_quotations";
			$price_update_query = "UPDATE $table_name SET value = '".$quote_item_price[$j]."' WHERE quote_no = '".$quote_no."' ";
			execsql($price_update_query);
	}
	
	if ($quote_item_text[$j]!='') {
		  	$table_name = $_SESSION['db_prefix']."_quotation_lines";
			$quote_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text, quote_item_text, quote_item_price) VALUES ('".$quote_no."', '1', '".$quote_option_text."', '".$quote_item_text[$j]."', '".$quote_item_price[$j]."' ) ";
			execsql($quote_query);
	}
$j++;
}


$j=0;
while ($j<$quote_line2_num) {
	$quote_item2_text[$j] = $_POST['quote_line2_text'.$j];
	$quote_item2_price[$j] = $_POST['quote_price2_text'.$j];
	
	if ($quote_item2_text[$j]!='') {
		  	$table_name = $_SESSION['db_prefix']."_quotation_lines";
			$quote_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text, quote_item_text, quote_item_price) VALUES ('".$quote_no."', '2', '".$quote_option2_text."', '".$quote_item2_text[$j]."', '".$quote_item2_price[$j]."' ) ";
			execsql($quote_query);
	}
$j++;
}


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

$k=0;
while ($k<$quote_line_num) {
	
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_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '58', '".$quote_no."', '".$saved_quote_item_text[$k]."' ,'".$saved_quote_item_text[$k]."','".$quote_item_text[$k]."' )";
$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_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '54', '".$quote_no."', '".$quote_item_text[$k]."' ,'".$saved_quote_item_price[$k]."','".$quote_item_price[$k]."' )";
		//echo '<br>id_number'.$trackingQuery;
$trackingResult = mySQL_query($trackingQuery);
}
}
$k++;
}


$k=0;
while ($k<$quote_line2_num) {
	
if ( ($saved_quote_item2_text[$k]!='') || ($quote_item2_text[$k]!='') ) {	
if ($saved_quote_item2_text[$k]!=$quote_item2_text[$k]) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '58', '".$quote_no."', '".$saved_quote_item2_text[$k]."' ,'".$saved_quote_item2_text[$k]."','".$quote_item2_text[$k]."' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ( ($saved_quote_item2_price[$k]!='') || ($quote_item2_price[$k]!='') ) {	
if ($saved_quote_item2_price[$k]!=$quote_item2_price[$k]) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '54', '".$quote_no."', '".$quote_item2_text[$k]."' ,'".$saved_quote_item2_price[$k]."','".$quote_item2_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		 
	   }
	   ?>

