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

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


$table_name = $_SESSION['db_prefix']."_compiled_jobs";
$update_query = "UPDATE $table_name SET del_date = '".$_SESSION['start_date']."', end_date = '".$_SESSION['end_date']."', work_days = '".$_SESSION['work_days']."', project_no = '".$_SESSION['project_no']."', project_area = '".$_SESSION['project_area']."', project_place = '".$_SESSION['project_place']."', forman = '".$_SESSION['forman']."', quote_note = '".$_SESSION['quote_note']."', labour_charge = '".$_SESSION['labour']."', travel_expense = '".$_SESSION['travel']."', accomodation_expense = '".$_SESSION['accomodation']."', mworks = '".$_SESSION['mworks']."', mworks_charge = '".$_SESSION['mworks_charge']."', compiled='yes'  WHERE job_card_no = '".$_SESSION['job_card_no']."' ";
execsql($update_query);

if ($_SESSION['mworks']=='yes') {
	$table_name = $_SESSION['db_prefix']."_mworks_jobs";
	$mworks_query = "INSERT INTO $table_name (job_card_no, cust_id, entry_date, del_date, end_date, value, captured ) VALUES ('".$_SESSION['job_card_no']."', '".$_SESSION['cust_id']."', '".$today."', '".$_SESSION['start_date']."', '".$_SESSION['end_date']."', '".$_SESSION['mworks_charge']."', 'yes' ) ";
	execsql($mworks_query);
}

$table_name3 = $_SESSION['db_prefix']."_order_job_lines_temp";
$items_query = "SELECT * FROM $table_name3 WHERE  user_name =  '".$_SESSION['username']."' ";
$items_result = mySQL_query($items_query);
$items_num = mysql_num_rows($items_result);
$stock_line='';
$i=0;
$non_stock_list = '';
while ($i<$items_num) {
	
			
   $category = mysql_result($items_result, $i, "category");
   $sub_category = mysql_result($items_result, $i, "sub_category");
   $store_item_id = mysql_result($items_result, $i, "store_item_id");
   $item_name = mysql_result($items_result, $i, "item_name");
   $quantity = mysql_result($items_result, $i, "quantity");
   $rec_price = mysql_result($items_result, $i, "price");
   
   if ($store_item_id!=0) {
	if ($store_item_id<100000) {
	$table_name = $_SESSION['db_prefix']."_store_items";
	$stock_query = "SELECT item_name, quantity, price FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
   
   $stock_result = mySQL_query($stock_query);
   $stock_quantity = mysql_result($stock_result, 0, "quantity");
   $stock_item_name = mysql_result($stock_result, 0, "item_name");	
   $price = mysql_result($stock_result, 0, "price");
   }
   if ($store_item_id>100000) {
	$table_name = $_SESSION['db_prefix']."_store_items_special";
	$stock_query = "SELECT item_name, quantity, price FROM $table_name WHERE special_store_item_id = '".$store_item_id."'  ";
   
   $stock_result = mySQL_query($stock_query);
   $stock_quantity = mysql_result($stock_result, 0, "quantity");
   $stock_item_name = mysql_result($stock_result, 0, "item_name");	
   $price = mysql_result($stock_result, 0, "price");
   }
   }
   else {
   $stock_quantity = 0;
   $stock_item_name = $item_name;
   $price = $rec_price ;
   
   $table_name = $_SESSION['db_prefix']."_special_orders";
   $special_query = "INSERT INTO $table_name (job_card_no, special_name, date_ordered, quantity_ordered ) VALUES ('".$_SESSION['job_card_no']."', '".$stock_item_name."', '".$entryDate."', '".$quantity."' ) ";
   execsql($special_query);
   
   $non_stock_list = $non_stock_list.'<br>'.$stock_item_name.' qty: '.$quantity;  
   }
     

   $table_name4 = $_SESSION['db_prefix']."_order_job_lines";  
   $insert_item_query = "INSERT INTO $table_name4 (job_card_no, category, sub_category, store_item_id, item_name, quantity, price ) VALUES ('".$_SESSION['job_card_no']."', '".$category."', '".$sub_category."', '".$store_item_id."', '".$item_name."', '".$quantity."', '".$price."' ) ";
execsql($insert_item_query);


   if ($quantity>$stock_quantity) {
	   $stock_line = $stock_line.'<tr><td>'.$stock_item_name.'</td><td align="center">'.$stock_quantity.'</td><td align="center">'.$quantity.'</td></tr>';
   }



$i++;
}

if ($_SESSION['all_total'] > $_SESSION['value']) {
	 include('../include/order_exceed_alert.php'); 
}

if ($stock_line != '') {
	 include('../include/order_stock_alert.php'); 
}

if ($non_stock_list != '') {
	 include('../include/non_stock_order_alert.php'); 
}
$job_card_no = $_SESSION['job_card_no'];
$_SESSION['id'] = $_SESSION['cust_id'];
if ($_SESSION['mworks'] == 'yes') {
	 include('../include/new_mworks_job_alert.php'); 
}

$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 card ".$_SESSION['job_card_no']." compiled.', '".$_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']."', '90', '".$_SESSION['job_card_no']."', '".$_SESSION['project']."' ,'','' )";
	$trackingResult = mySQL_query($trackingQuery);	


$table_name = $_SESSION['db_prefix']."_order_job_lines_temp";
$delete2_query = "DELETE FROM $table_name WHERE  user_name =  '".$_SESSION['username']."' "; 
execsql($delete2_query);


	if (isset($_GET['new_button']) )
	   {
		 $action='compileJobWiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		$action='job_card.php?job_card_no='.$_SESSION['job_card_no'] ;
	  }
	
	 	 
	 $_SESSION['job_card_no']='';
	 $_SESSION['name']="";
	 $_SESSION['start_date']=date("Y-m-d");
	 $_SESSION['end_date']=date("Y-m-d");
	 $_SESSION['cust_id']='';
	 
	 
	 $_SESSION['quote_note'] = '';
	$_SESSION['id']='';	
	 $_SESSION['cust_id']='';
	$_SESSION['order_date']='';
	$_SESSION['project_description'] = '';
	$_SESSION['project_no'] = '';
	$_SESSION['project_place'] = '';
	$_SESSION['project_area'] = '';
	$_SESSION['work_days'] = '';
	$_SESSION['value'] = '';
	$_SESSION['deliver_date']= date("Y-m-d");

    $_SESSION['forman']='';
	$_SESSION['forman_name']='';
	 $_SESSION['labour']='';
	 $_SESSION['travel']='';
	 $_SESSION['accomodation']='';
	$_SESSION['mworks_charge']='';
	$_SESSION['mworks']='';
		
	$_SESSION['all_total'] = '';
	 
		
?>

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

