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

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

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

$project_text = $_POST['project_text'];



$quote_no=$_POST['quote_no'];
$table_name = $_SESSION['db_prefix']."_maintenance_quotations";
$quote_query = "SELECT * FROM $table_name WHERE quote_no = '".$quote_no."' ";
$quote_result = mySQL_query($quote_query);
$cust_id = mysql_result($quote_result, 0, "cust_id");
//$value = mysql_result($quote_result, 0, "value");

$request_date_saved = mysql_result($quote_result, 0, "request_date");

$project_description_saved = mysql_result($quote_result, 0, "project_description");
$project_saved = mysql_result($quote_result, 0, "project");
$project_no_saved = mysql_result($quote_result, 0, "project_no");
$project_place_saved = mysql_result($quote_result, 0, "project_place");
$project_area_saved = mysql_result($quote_result, 0, "project_area");
$vat_text = mysql_result($quote_result, 0, "vat_text");


$project = $_POST['project_select'];
$request_date = $_POST['request_date'];
//$value = $_POST['value_text'];
$project_no = $_POST['project_no_text'];
$project_area = $_POST['project_area_select'];
$project_place = $_POST['project_place_select'];
$project_description = $_POST['notes_text'];
$option_text = $_POST['option_text'];


$table_name = $_SESSION['db_prefix']."_maintenance_quotations";
$edit_query = "UPDATE $table_name SET project = '".$project."', request_date = '".$request_date."', project_no = '".$project_no."', project_area = '".$project_area."', project_place = '".$project_place."', project_description = '".$project_description."' WHERE quote_no = '".$quote_no."' ";
execsql($edit_query);

if (($project_text=='Maintenance Fence (Residential)') OR ($project=='Maintenance Fence (Commercial)') OR ($project=='Maintenance Gate Motor') OR ($project=='Maintenance miscellaneous') ) { 

$table_name = $_SESSION['db_prefix']."_maintenance_quotation_lines";
$line_query = "SELECT * FROM $table_name WHERE quote_number = '".$quote_no."' ";
$line_result = mySQL_query($line_query);
$line_num = mysql_num_rows($line_result);
$option_text_saved = mysql_result($line_result, 0, "quote_option_text");


$table_name = $_SESSION['db_prefix']."_maintenance_quotation_lines";
$update_query = "UPDATE $table_name SET quote_option_text = '".$option_text."' WHERE quote_number = '".$quote_no."' ";
execsql($update_query);

if ($option_text_saved!=$option_text) {
	    $table_name = $_SESSION['db_prefix']."_tracking_maintenance";
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '130', '".$quote_no."' , '".$option_text_saved."','".$option_text."', 'Option' )";
$trackingResult = mySQL_query($trackingQuery);
}

$quote_value = '';	
$line_num = $_POST['line_num'];
$i=0;
while ($i<$line_num) { 
$line_id = $_POST['line_id'.$i];

if ($line_id!='0') {
$table_name = $_SESSION['db_prefix']."_maintenance_quotation_lines";
$check_query = "SELECT quote_item_text,quote_item_price FROM $table_name WHERE line_id = '".$line_id."' ";
$check_result = mySQL_query($check_query);
$check_num = mysql_num_rows($check_result);
$quote_item_text_saved = mysql_result($check_result, 0, "quote_item_text");
$quote_item_price_saved = mysql_result($check_result, 0, "quote_item_price");
}
else {
	$quote_item_text_saved = '';
    $quote_item_price_saved = '';
}

if (isset($_POST['line_checkbox'.$i])) {
$quote_item_text = $_POST['item_text'.$i];
$quote_item_price = $_POST['amount_text'.$i];	
$quote_value = $quote_value + $quote_item_price;
if ($line_id!='0') {
$table_name = $_SESSION['db_prefix']."_maintenance_quotation_lines";
$update_query = "UPDATE $table_name SET quote_item_text = '".$quote_item_text."', quote_item_price = '".$quote_item_price."'  WHERE line_id = '".$line_id."' ";
execsql($update_query);
}
else {
	$table_name = $_SESSION['db_prefix']."_maintenance_quotation_lines";
	$new_line_query = "INSERT INTO $table_name (quote_number, quote_option, quote_option_text, quote_item_text, quote_item_price ) VALUES ('".$quote_no."', '11', '".$option_text_saved."', '".$quote_item_text."', '".$quote_item_price."'  ) ";
	execsql($new_line_query);
}

if ($line_id!='0') {
if ($quote_item_text_saved!=$quote_item_text) {
	    $table_name = $_SESSION['db_prefix']."_tracking_maintenance";
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '130', '".$quote_no."' , '".$quote_item_text_saved."','".$quote_item_text."', 'Item description' )";
$trackingResult = mySQL_query($trackingQuery);
}

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

}
else {
	$table_name = $_SESSION['db_prefix']."_maintenance_quotation_lines";
	$delete_query = "DELETE FROM $table_name WHERE line_id = '".$line_id."' ";
	execsql($delete_query);
	
	$table_name = $_SESSION['db_prefix']."_tracking_maintenance";
	$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '131', '".$quote_no."' , '".$quote_item_text_saved."','', 'Item deleted' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($line_id=='0') {
if ($quote_item_text_saved!=$quote_item_text) {
	    $table_name = $_SESSION['db_prefix']."_tracking_maintenance";
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '132', '".$quote_no."' , '','".$quote_item_text."', 'New item' )";
$trackingResult = mySQL_query($trackingQuery);
}
}

$i++;
}

$quote_value = $quote_value + ($quote_value * ($vat_text/100));
$table_name = $_SESSION['db_prefix']."_maintenance_quotations";
$update_query = "UPDATE $table_name SET  value = '".$quote_value."'  WHERE quote_no = '".$quote_no."' ";
execsql($update_query);

}


if (($project_text=='Monthly maintenance') ) { 

$table_name = $_SESSION['db_prefix']."_maintenance_quotation_lines";
$line_query = "SELECT quote_option_text, quote_item_price, line_id FROM $table_name WHERE quote_number = '".$quote_no."' AND quote_item_text = 'Year rate (11 months)' ";
$line_result = mySQL_query($line_query);
$line_num = mysql_num_rows($line_result);
$option_text_saved = mysql_result($line_result, 0, "quote_option_text");
$amount_text_saved = mysql_result($line_result, 0, "quote_item_price");
$line_id = mysql_result($line_result, 0, "line_id");

$table_name = $_SESSION['db_prefix']."_maintenance_quotation_lines";
$update_query = "UPDATE $table_name SET quote_option_text = '".$option_text."' WHERE quote_number = '".$quote_no."' ";
execsql($update_query);

if ($option_text_saved!=$option_text) {
	    $table_name = $_SESSION['db_prefix']."_tracking_maintenance";
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '130', '".$quote_no."' , '".$option_text_saved."','".$option_text."', 'Option' )";
$trackingResult = mySQL_query($trackingQuery);
}

$amount_text = $_POST['amount_text'];


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

$table_name = $_SESSION['db_prefix']."_maintenance_quotation_lines";
$update_query = "UPDATE $table_name SET  quote_item_price = '".$amount_text."'  WHERE line_id = '".$line_id."' ";
execsql($update_query);

if ($amount_text_saved!=$amount_text) {
	    $table_name = $_SESSION['db_prefix']."_tracking_maintenance";
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '130', '".$quote_no."' , '".$amount_text_saved."','".$amount_text."', 'Amount' )";
$trackingResult = mySQL_query($trackingQuery);
}

}

$table_name = $_SESSION['db_prefix']."_tracking_maintenance";
if ($project_saved!=$project) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '130', '".$quote_no."' , '".$project_saved."','".$project."', 'Project' )";
$trackingResult = mySQL_query($trackingQuery);
}

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

if ($project_description_saved!=$project_description) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '130', '".$quote_no."' , '".$project_description_saved."','".$project_description."', 'Project description' )";
$trackingResult = mySQL_query($trackingQuery);
}

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

if ($project_no_saved!=$project_no) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '130', '".$quote_no."' , '".$project_no_saved."','".$project_no."', 'Project no' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($project_place_saved!=$project_place) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '130', '".$quote_no."' , '".$project_place_saved."','".$project_place."', 'Project place' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($project_area_saved!=$project_area) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '130', '".$quote_no."' , '".$project_area_saved."','".$project_area."', 'Project area' )";
$trackingResult = mySQL_query($trackingQuery);
}


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

