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

$userId = $_SESSION['username'];
date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s"); 

$job_card_no = $_GET['job_card_no_text'];
$cust_id = $_GET['cust_id'];
$project = $_GET['project'];
$result_1 = $_GET['survey_result_1'];
$result_2 = $_GET['survey_result_2'];
$result_3 = $_GET['survey_result_3'];
$result_4 = $_GET['survey_result_4'];

$cust_notes = $_GET['cust_notes_text'];
$maintenance_notes = $_GET['maintenance_text'];

$table_name = $_SESSION['db_prefix']."_survey_orders";
$delete_query = "DELETE FROM $table_name WHERE job_card_no =  '".$job_card_no."'   "; 
execsql($delete_query);

$table_name = $_SESSION['db_prefix']."_survey_orders";
$survey_query = "INSERT INTO $table_name ( job_card_no, result_1, result_2, result_3, result_4, cust_notes, maintenance_notes) VALUES ('".$job_card_no."', '".$result_1."', '".$result_2."', '".$result_3."', '".$result_4."', '".$cust_notes."', '".$maintenance_notes."' )";
execsql($survey_query);


$table_name = $_SESSION['db_prefix']."_compiled_jobs";
$update_query = "UPDATE $table_name SET survey = 'yes' WHERE job_card_no = '".$job_card_no."' ";
execsql($update_query);

if ($maintenance_notes!='') {
	   include('../include/maintenance_request_alert.php');
}


$survey_perc = (($result_1+$result_2+$result_3+$result_4)/8)*100;


$survey_perc = number_format((($result_1+$result_2+$result_3+$result_4)/8)*100, 0, '.', ',');


if ($maintenance_notes!='') {
$table_name6 = $_SESSION['db_prefix']."_order_history";
$history_query = "INSERT INTO $table_name6 (job_card_number, note_date, note_text, user_name ) VALUES ('".$job_card_no."', '".$entry_date."', 'After sales maintenance requested: ".$maintenance_notes." ', '".$_SESSION['username']."' ) ";
execsql($history_query);
}


$table_name6 = $_SESSION['db_prefix']."_order_history";
$history_query = "INSERT INTO $table_name6 (job_card_number, note_date, note_text, user_name ) VALUES ('".$job_card_no."', '".$entry_date."', 'Job survey done by ".$_SESSION['username'].". Survey result: ".$survey_perc."%.', '".$_SESSION['username']."' ) ";
execsql($history_query);


$table_name = $_SESSION['db_prefix']."_tracking_orders";
$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '191', '".$job_card_no."', '".$survey_perc."' ,'','' )";
$trackingResult = mySQL_query($trackingQuery);

?>
       <script>
	 		 window.location="orders_main.php";
		</script>


