<?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'];

$table_name = $_SESSION['db_prefix']."_compiled_jobs";
$saved_item_query = "SELECT project, date, project_no, value, project_area, project_place, quote_note  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');
$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');

$project = $_POST['project_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'];

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

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

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']."', '108', '".$job_card_no."' , '".$project_saved."','".$project."', 'Project' )";
$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']."', '108', '".$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']."', '108', '".$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']."', '108', '".$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']."', '108', '".$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']."', '108', '".$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']."', '108', '".$job_card_no."' , '".$quote_note_saved."','".$quote_note."', 'Notes' )";
$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']."_mworks_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']."_mworks_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_production";
	    $trackingQuery = "INSERT INTO $table_name2 (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '105', '".$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_production"; 
		 $trackingQuery = "INSERT INTO $table_name2 (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '107', '".$job_card_no."' , '".$saved_quantity."','".$quantity."', '".$store_item_id."' )";
		 $trackingResult = mySQL_query($trackingQuery);
		
		$table_name = $_SESSION['db_prefix']."_mworks_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']."_mworks_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_production";
		$trackingQuery = "INSERT INTO $table_name2 (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '106', '".$job_card_no."' , '".$saved_quantity."','".$quantity."', '".$store_item_id."' )";
		$trackingResult = mySQL_query($trackingQuery);	
	}
	
	
$i++;	
}

$line2_num = $_POST['line2_num'];
$i=0;
while ($i<$line2_num) {
	$component_id = $_POST['component_id7'.$i];

if (isset($_POST['line_checkbox7'.$i])) { 
	$component = $_POST['item_text7'.$i];
	$quantity = $_POST['quantity_text7'.$i];
	$notes = $_POST['notes_text7'.$i];
		
if ($component_id!='') {
	$table_name = $_SESSION['db_prefix']."_mworks_job_components";
	$saved_query = "SELECT component, quantity, notes  FROM $table_name WHERE mworks_job_component_id = '".$component_id."'  ";
	$saved_result = mySQL_query($saved_query);
	$saved_component = mysql_result($saved_result, 0, "component");
	$saved_quantity = mysql_result($saved_result, 0, "quantity");
	$saved_notes = mysql_result($saved_result, 0, "notes");
	
	if ($component!=$saved_component) {
		$table_name = $_SESSION['db_prefix']."_mworks_job_components";
		$update_query = "UPDATE $table_name SET component = '".$component."' WHERE mworks_job_component_id = '".$component_id."'  ";
		execsql($update_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']."', '108', '".$job_card_no."' , '".$saved_component."','".$component."', 'Component' )";
		$trackingResult = mySQL_query($trackingQuery);
	}
	
	if ($quantity!=$saved_quantity) {
		$table_name = $_SESSION['db_prefix']."_mworks_job_components";
		$update_query = "UPDATE $table_name SET quantity = '".$quantity."' WHERE mworks_job_component_id = '".$component_id."'  ";
		execsql($update_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']."', '108', '".$job_card_no."' , '".$saved_quantity."','".$quantity."', 'Quantity' )";
		$trackingResult = mySQL_query($trackingQuery);
	}
	
	if ($notes!=$saved_notes) {
		$table_name = $_SESSION['db_prefix']."_mworks_job_components";
		$update_query = "UPDATE $table_name SET notes = '".$notes."' WHERE mworks_job_component_id = '".$component_id."'  ";
		execsql($update_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']."', '108', '".$job_card_no."' , '".$saved_notes."','".$notes."', 'Notes' )";
		$trackingResult = mySQL_query($trackingQuery);
	}
}
else {
	$table_name = $_SESSION['db_prefix']."_mworks_job_components";
	$insert_query = "INSERT INTO $table_name (job_card_no, component, quantity, notes ) VALUES ('".$job_card_no."', '".$component."', '".$quantity."', '".$notes."') ";
	execsql($insert_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']."', '110', '".$job_card_no."' , '".$component."','".$quantity."', 'Item added' )";
	$trackingResult = mySQL_query($trackingQuery);
}

}

else {
	$table_name = $_SESSION['db_prefix']."_mworks_job_components";
	$delete_query = "DELETE FROM $table_name WHERE mworks_job_component_id = '".$component_id."'  ";
	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']."', '111', '".$job_card_no."' , '".$component."','".$quantity."', 'Item deleted' )";
	$trackingResult = mySQL_query($trackingQuery);
}

$i++;	
}

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

