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

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

$today = date("Y-m-d");

$job_card_no = $_POST['job_card_no'];
$mworks_compiled = $_POST['mworks_compiled'];


$table_name = $_SESSION['db_prefix']."_compiled_jobs";
$saved_item_query = "SELECT project, forman, date, project_no, value, project_area, project_place, quote_note, mworks, mworks_charge FROM $table_name WHERE job_card_no = '".$job_card_no."' ";
$saved_item_result = mySQL_query($saved_item_query);
$saved_item_num = mysql_num_rows($saved_item_result);

$project_saved = mysql_result($saved_item_result, 0, 'project');
$forman_saved = mysql_result($saved_item_result, 0, 'forman');
$order_date_saved = mysql_result($saved_item_result, 0, 'date');
$value_saved = mysql_result($saved_item_result, 0, 'value');
$project_no_saved = mysql_result($saved_item_result, 0, 'project_no');
$project_area_saved = mysql_result($saved_item_result, 0, 'project_area');
$project_place_saved = mysql_result($saved_item_result, 0, 'project_place');
$quote_note_saved = mysql_result($saved_item_result, 0, 'quote_note');
$mworks_saved = mysql_result($saved_item_result, 0, 'mworks');
$mworks_charge_saved = mysql_result($saved_item_result, 0, 'mworks_charge');

$project = $_POST['project_select'];
$forman = $_POST['forman_select'];
$order_date = $_POST['order_date'];
$value = $_POST['value_text'];
$project_no = $_POST['project_no_text'];
$project_area = $_POST['project_area_select'];
$project_place = $_POST['project_place_select'];
$quote_note = $_POST['notes_text'];



if (isset($_POST['mworks_checkbox']) ) {
	   $mworks='yes' ;
	   $mworks_charge=$_POST['mworks_text'];
	   }
	 else {
		$mworks='no';  
		$mworks_charge='';
		}

$table_name = $_SESSION['db_prefix']."_compiled_jobs";
$edit_query = "UPDATE $table_name SET project = '".$project."', forman = '".$forman."', date = '".$order_date."', value = '".$value."', project_no = '".$project_no."', project_area = '".$project_area."', project_place = '".$project_place."', quote_note = '".$quote_note."', mworks = '".$mworks."', mworks_charge = '".$mworks_charge."'  WHERE job_card_no = '".$job_card_no."' ";
execsql($edit_query);

if (isset($_POST['mworks_checkbox']) ) {
		$table_name = $_SESSION['db_prefix']."_mworks_jobs";
		$check_query = "SELECT job_card_no FROM $table_name WHERE job_card_no = '".$job_card_no."' ";
		$check_result = mySQL_query($check_query);
		$check_num = mysql_num_rows($check_result);
		if ($check_num>0) {
			$table_name = $_SESSION['db_prefix']."_mworks_jobs";
			$update_query = "UPDATE $table_name SET value = '".$mworks_charge."' WHERE job_card_no = '".$job_card_no."' ";
			execsql($update_query);
		}
		else {	
	
	    $table_name = $_SESSION['db_prefix']."_compiled_jobs";
		$job_query = "SELECT * FROM $table_name WHERE job_card_no = '".$job_card_no."' ";
		$job_result = mySQL_query($job_query);
		$cust_id = mysql_result($job_result, 0, "cust_id");
		$del_date = mysql_result($job_result, 0, "del_date");
		$end_date = mysql_result($job_result, 0, "end_date");
		
	   	$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 ('".$job_card_no."', '".$cust_id."', '".$today."', '".$del_date."', '".$end_date."', '".$mworks_charge."', 'yes' ) ";
		execsql($mworks_query);
		}
	 }
	 else {
		$table_name = $_SESSION['db_prefix']."_mworks_jobs";
		$delete_query = "DELETE FROM $table_name WHERE job_card_no = '".$job_card_no."'  "; 
		execsql($delete_query);  
	}

$table_name = $_SESSION['db_prefix']."_tracking_orders";

if ($project_saved!=$project) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '97', '".$job_card_no."' , '".$project_saved."','".$project."', 'Project' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($forman_saved!=$forman) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '97', '".$job_card_no."' ,  '".$forman_saved."','".$forman."', 'Foreman' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($order_date_saved!=$order_date) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '97', '".$job_card_no."' , '".$order_date_saved."','".$order_date."', 'Date' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($value_saved!=$value) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '97', '".$job_card_no."' , '".$value_saved."','".$value."', 'Value' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($project_no_saved!=$project_no) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '97', '".$job_card_no."' , '".$project_no_saved."','".$project_no."', 'Project no' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($project_area_saved!=$project_area) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '97', '".$job_card_no."' , '".$project_area_saved."','".$project_area."', 'Project area' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($project_place_saved!=$project_place) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '97', '".$job_card_no."' , '".$project_place_saved."','".$project_place."', 'Project place' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($quote_note_saved!=$quote_note) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '97', '".$job_card_no."' , '".$quote_note_saved."','".$quote_note."', 'Notes' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($mworks_saved!=$mworks) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '97', '".$job_card_no."' , '".$mworks_saved."','".$mworks."', 'M-works job' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($mworks_charge_saved!=$mworks_charge) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '97', '".$job_card_no."' , '".$mworks_charge_saved."','".$mworks_charge."', 'M-works charge' )";
$trackingResult = mySQL_query($trackingQuery);
}


$line_num = $_POST['line_num'];

$i=0;
while ($i<$line_num) {
	$store_item_id = $_POST['store_item_id'.$i];
	$item_name = $_POST['store_item_text'.$i];
	$quantity = $_POST['quantity_text'.$i];
	
	$table_name = $_SESSION['db_prefix']."_order_job_lines";
	$saved_query = "SELECT quantity FROM $table_name WHERE job_card_no = '".$job_card_no."' AND store_item_id = '".$store_item_id."' AND item_name = '".$item_name."' ";
	$saved_result = mySQL_query($saved_query);
	$saved_num = mysql_num_rows($saved_result);
	if ($saved_num>0) {
	       $saved_quantity = mysql_result($saved_result, 0, "quantity");
		   $table_name = $_SESSION['db_prefix']."_order_job_lines";
			$update_query = "UPDATE $table_name SET store_item_id = '".$store_item_id."',  quantity = '".$quantity."' WHERE job_card_no = '".$job_card_no."' AND store_item_id = '".$store_item_id."' AND item_name = '".$item_name."' ";
	execsql($update_query);
	
	if (($saved_quantity!=$quantity) AND ($quantity!=0)) {
		$table_name2 = $_SESSION['db_prefix']."_tracking_orders";
	    $trackingQuery = "INSERT INTO $table_name2 (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '98', '".$job_card_no."' , '".$saved_quantity."','".$quantity."', '".$store_item_id."' )";
		$trackingResult = mySQL_query($trackingQuery);
}
		   
	}
	else {
		$saved_quantity = 0;
		
		$table_name = $_SESSION['db_prefix']."_store_items";
		$item_query = "SELECT item_name, price FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
		$item_result = mySQL_query($item_query);
		$item_name = mysql_result($item_result, 0, "item_name");
		$price = mysql_result($item_result, 0, "price");
		
		$table_name2 = $_SESSION['db_prefix']."_tracking_orders"; 
		 $trackingQuery = "INSERT INTO $table_name2 (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '190', '".$job_card_no."' , '".$saved_quantity."','".$quantity."', '".$store_item_id."' )";
		 $trackingResult = mySQL_query($trackingQuery);
		
		$table_name = $_SESSION['db_prefix']."_order_job_lines";
		$replace_query = "INSERT INTO $table_name (job_card_no, store_item_id, item_name, quantity, price ) VALUES ('".$job_card_no."', '".$store_item_id."', '".$item_name."', '".$quantity."', '".$price."'  ) ";
		execsql($replace_query);
	}
	
	if ($quantity==0) {
		   	$table_name = $_SESSION['db_prefix']."_order_job_lines";
			$delete_query = "DELETE FROM $table_name WHERE job_card_no = '".$job_card_no."' AND store_item_id = '".$store_item_id."'  AND item_name = '".$item_name."' ";
			execsql($delete_query);
				
		$table_name2 = $_SESSION['db_prefix']."_tracking_orders";
		$trackingQuery = "INSERT INTO $table_name2 (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '99', '".$job_card_no."' , '".$saved_quantity."','".$quantity."', '".$store_item_id."' )";
		$trackingResult = mySQL_query($trackingQuery);	
	}
	
	
$i++;	
}

if (isset($_POST['save_close_button']) )
	   { ?>
       <script>
	 		 window.location="job_card.php?job_card_no=<?php echo $job_card_no?>";
		</script>
        <?php		 
	   }
	   
	   if (isset($_POST['save_button']) )
	   {  ?>
       <script>
	          window.location="edit_job_card.php?job_card_no=<?php echo $job_card_no?>";
	   </script>
        <?php		 
	   }
	   ?>

