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

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


$table_name = $_SESSION['db_prefix']."_compiled_jobs";
$update_query = "UPDATE $table_name SET labour_charge_final = '".$_SESSION['labour_adjust']."', travel_expense_final = '".$_SESSION['travel_adjust']."', accomodation_expense_final = '".$_SESSION['accomodation_adjust']."', mworks_charge_final = '".$_SESSION['mworks_charge_adjust']."', store_items_final = '".$_SESSION['a8tot']."', adjustment_text = '".$_SESSION['adjustment_text']."', adjustment_charge = '".$_SESSION['adjustment']."', completed = 'yes', completion_date = '".$entryDate."' WHERE job_card_no = '".$_SESSION['job_card_no']."' ";
execsql($update_query);


$table_name1 = $_SESSION['db_prefix']."_order_job_lines";
$table_name2 = $_SESSION['db_prefix']."_order_job_lines_done";
$copy_query = "INSERT INTO $table_name2 SELECT * FROM $table_name1 WHERE job_card_no = '".$_SESSION['job_card_no']."' ";
execsql($copy_query);

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



$table_name = $_SESSION['db_prefix']."_kpi_points";
$kpi_query = "INSERT INTO $table_name (emp_id, job_card_no, kpi_points ) VALUES ('".$_SESSION['forman']."', '".$_SESSION['job_card_no']."', '".$_SESSION['kpi_points']."' ) ";
execsql($kpi_query);


$table_name6 = $_SESSION['db_prefix']."_order_history";
$history_query = "INSERT INTO $table_name6 (job_card_number, note_date, note_text, user_name ) VALUES ('".$_SESSION['job_card_no']."', '".$entryDate."', 'Job completed by ".$_SESSION['username'].".', '".$_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 ('".$entryDate."' ,'".$_SESSION['username']."', '95', '".$_SESSION['job_card_no']."', '".$_SESSION['project']."' ,'".$_SESSION['username']."','' )";
	$trackingResult = mySQL_query($trackingQuery);	


include('../include/job_completed_alert.php');
	  
$action='costing_job_card.php?job_card_no='.$_SESSION['job_card_no'] ;
	
$_SESSION['job_card_no']="";
$_SESSION['name']="";
$_SESSION['invoice_date']=date("Y-m-d");
$_SESSION['invoice_no']='';
$_SESSION['cust_id']='';

$_SESSION['labour_adjust'] = '';
$_SESSION['travel_adjust'] = '';
$_SESSION['accomodation_adjust'] = '';
$_SESSION['mworks_charge_adjust'] = '';
$_SESSION['adjustment_text']= '';	
$_SESSION['adjustment']	= '';

$_SESSION['a4tot']	= '';
$_SESSION['a8tot']	= '';
$_SESSION['a10tot']	= '';
$_SESSION['estimated_total'] = '';
$_SESSION['invoiced_total']	= '';

$_SESSION['kpi_points']='';

$_SESSION['forman'] = '';
$_SESSION['forman_name']  = '';

$_SESSION['cust_id']  = '';
$_SESSION['order_date']  = '';
$_SESSION['project']  = '';
$_SESSION['project_description']  = '';
$_SESSION['project_no']  = '';
$_SESSION['project_area']  = '';
$_SESSION['project_place']  = '';
$_SESSION['quote_note']  = '';

$_SESSION['labour']='0';
$_SESSION['travel']='0';
$_SESSION['accomodation']='0';
$_SESSION['mworks_charge']='0';
		
?>

<script language="javascript" type="text/javascript">
        
		  window.location="<?php echo $action?>";
        
       </script>

