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

	
		
	$d = date("d");
	$m = date("m");
	$y = date("Y");
	
	
	$mm = date("m");
	$yy = date("y");
	
	$user_id = $_SESSION['username'];
	date_default_timezone_set('Africa/Gaborone');
	$entry_date = date("Y-m-d H:i:s", time()); 
	$read_date = date("Y-m-d");	
	
	
	
	$table_name = $_SESSION['db_prefix']."_compiled_jobs";
	$id_query = "SELECT job_card_no  FROM $table_name ORDER BY job_card_no DESC LIMIT 1 ";
	$id_result = mySQL_query($id_query);
	$id_result_num = mysql_num_rows($id_result);
	if ($id_result_num!=0) {
	$job_card_no = mysql_result($id_result, 0, "job_card_no");
	}else {
		$job_card_no='';
	}
	
	$job_card_no = $job_card_no + 1;
		 
	$table_name = $_SESSION['db_prefix']."_compiled_jobs";
	$main_job_query = "INSERT INTO $table_name (job_card_no, cust_id, date, project, project_no, project_area, project_place, project_description,  value, mworks, mworks_charge, captured) VALUES ('".$job_card_no."', '".$_SESSION['id']."', '".$_SESSION['receive_date']."', 'M-works', '".$_SESSION['project_no']."', '".$_SESSION['project_area']."', '".$_SESSION['project_place']."', '".$_SESSION['description']."', '".$_SESSION['value']."', 'only', '".$_SESSION['value']."', 'yes' ) ";
	execsql($main_job_query);
	
	$table_name = $_SESSION['db_prefix']."_mworks_jobs";
    $mworks_job_query = "INSERT INTO $table_name (job_card_no, cust_id, entry_date, value, mworks, captured ) VALUES ('".$job_card_no."', '".$_SESSION['id']."', '".$_SESSION['receive_date']."',  '".$_SESSION['value']."', 'only', 'yes' ) ";
	
    execsql($mworks_job_query);
	
	
	
	$table_name = $_SESSION['db_prefix']."_customer";
$customer_query = "UPDATE $table_name SET contact_1 = '".trim($_SESSION['comp_contact'])."', cell_1 = '".trim($_SESSION['contact_phone'])."' WHERE cust_id = '".trim($_SESSION['id'])."' ";
execsql($customer_query);
	
	$table_name1 = $_SESSION['db_prefix']."_mworks_order_components_temp";
	$table_name2 = $_SESSION['db_prefix']."_mworks_job_components";
    $copy_query = "INSERT INTO $table_name2 (job_card_no, component, quantity, notes )   SELECT '".$job_card_no."', component, quantity, notes FROM $table_name1 WHERE user_name = '".$_SESSION['username']."' ";
	execsql($copy_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 ('".$entry_date."' ,'".$_SESSION['username']."', '101', '".$job_card_no."' ,'".$_SESSION['id']."','".$_SESSION['project']."','' )";
	$trackingResult = mySQL_query($trackingQuery);
	
	
	$table_name = $_SESSION['db_prefix']."_mworks_order_components_temp";
	$delete_query = "DELETE FROM $table_name WHERE user_name = '".$_SESSION['username']."' "; 
	execsql($delete_query);
	
	include('../include/new_mworks_job_alert.php');
	
	$_SESSION['company_name'] ="";
	 $_SESSION['name'] ="";
	 $_SESSION['id'] = "";
     $_SESSION['type'] ="";
     $_SESSION['contact'] = "";
	 $_SESSION['cell'] = "";
	 $_SESSION['bag_box'] = "";
	 $_SESSION['box_number'] = "";
	 $_SESSION['post_place'] = "";
	 $_SESSION['phone'] = "";
	 $_SESSION['fax'] = "";
	 $_SESSION['email'] = "";
	 $_SESSION['reference'] = "";
	 $_SESSION['comp_contact'] = "";
	 $_SESSION['contact_phone'] = "";
	 $_SESSION['project_no'] = "";
	 $_SESSION['project_area'] = "";
	 $_SESSION['project_place'] = "";
	 $_SESSION['description'] = "";
	 $_SESSION['seller'] = "";
	 $_SESSION['seller'] = "";
	 $_SESSION['receive_date'] = date('Y-m-d');
	 
	 
	 	
    if (isset($_GET['new_button']) )
	   {
		 $action='new_mworks_jobWiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		$action='m_job_card.php?job_card_no='.$job_card_no ;
	  }
	 
?>

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



