<?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  mworks_charge_final = '".$_SESSION['invoiced_total']."'  WHERE job_card_no = '".$_SESSION['job_card_no']."' ";
execsql($update_query);

$table_name = $_SESSION['db_prefix']."_mworks_jobs";
$mworks_update_query = "UPDATE $table_name SET mworks_charge = '".$_SESSION['estimated_total']."',  labour_charge_final = '".$_SESSION['labour_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($mworks_update_query);


$table_name1 = $_SESSION['db_prefix']."_mworks_order_job_lines";
$table_name2 = $_SESSION['db_prefix']."_mworks_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']."_mworks_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']."_tracking_production";
	$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '103', '".$_SESSION['job_card_no']."', '".$_SESSION['project']."' ,'".$_SESSION['username']."','' )";
	$trackingResult = mySQL_query($trackingQuery);	


include('../include/m_job_completed_alert.php');
	  
$action='m_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['adjustment_text']= '';	
$_SESSION['adjustment']	= '';

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



		
?>

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

