<?php
	/**************************************************************************\
	* phpGroupWare - Trouble Ticket System                                     *
	* http://www.phpgroupware.org                                              *
	* --------------------------------------------                             *
	*  This program is free software; you can redistribute it and/or modify it *
	*  under the terms of the GNU General Public License as published by the   *
	*  Free Software Foundation; either version 2 of the License, or (at your  *
	*  option) any later version.                                              *
	\**************************************************************************/

	// $Id: viewticket_details.php,v 1.1.1.3.2.1 2004/12/03 10:55:32 kazuyan Exp $
	// $Source: /cvsroot/phpgwjp/tts/viewticket_details.php,v $

	$GLOBALS['phpgw_info']['flags'] = array(
		'enable_nextmatchs_class' => True,
		'enable_categories_class' => True,
		'enable_config_class' => True,
		'currentapp'              => 'tts',
		'enable_vfs_class' => True
	);
	$submit = $_POST['submit'];
	$cancel = $_POST['cancel'];
	$ticket_id = $_REQUEST['ticket_id'];

	if($submit)
	{
		$GLOBALS['phpgw_info']['flags']['noheader'] = True;
		$GLOBALS['phpgw_info']['flags']['nonavbar'] = True;
		$GLOBALS['phpgw_info']['flags']['enable_config_class'] = True;
	}
	include('../header.inc.php');
	
	$GLOBALS['phpgw']->config->read_repository();

	if($cancel)
	{
		$GLOBALS['phpgw']->redirect($GLOBALS['phpgw']->link('/tts/index.php'));
	}
	
	
	$GLOBALS['phpgw']->historylog = createobject('phpgwapi.historylog','tts');
	$GLOBALS['phpgw']->historylog->types = array(
		'R' => 'Re-opened',
		'X' => 'Closed',
		'O' => 'Opened',
		'A' => 'Re-assigned',
		'P' => 'Priority changed',
		'T' => 'Category changed',
		'S' => 'Subject changed',
		'B' => 'Billing rate',
		'H' => 'Billing hours',
		'G' => 'Group ownership changed',
		'Y' => 'Type',
		'D' => 'Deadline',
		'E' => 'Effort',
		'L' => 'Platform'
	);

	if(!$submit)
	{
		// Have they viewed this ticket before ?
		$GLOBALS['phpgw']->db->query("select count(*) from phpgw_tts_views where view_id='$ticket_id' "
			. "and view_account_id='" . $GLOBALS['phpgw_info']['user']['account_id'] . "'",__LINE__,__FILE__);
		$GLOBALS['phpgw']->db->next_record();

		if(!$GLOBALS['phpgw']->db->f(0))
		{
			$GLOBALS['phpgw']->db->query("insert into phpgw_tts_views values ('$ticket_id','"
				. $GLOBALS['phpgw_info']['user']['account_id'] . "','" . time() . "')",__LINE__,__FILE__);
		}

		// select the ticket that you selected
		$GLOBALS['phpgw']->db->query("select * from phpgw_tts_tickets where ticket_id='$ticket_id'",__LINE__,__FILE__);
		$GLOBALS['phpgw']->db->next_record();

		$ticket['billable_hours'] = $GLOBALS['phpgw']->db->f('ticket_billable_hours');
		$ticket['billable_rate']  = $GLOBALS['phpgw']->db->f('ticket_billable_rate');
		$ticket['assignedto']     = $GLOBALS['phpgw']->db->f('ticket_assignedto');
		$ticket['category']       = $GLOBALS['phpgw']->db->f('ticket_category');
		$ticket['details']        = $GLOBALS['phpgw']->db->f('ticket_details');
		$ticket['subject']        = $GLOBALS['phpgw']->db->f('ticket_subject');
		$ticket['priority']       = $GLOBALS['phpgw']->db->f('ticket_priority');
		$ticket['owner']          = $GLOBALS['phpgw']->db->f('ticket_owner');
		$ticket['group']          = $GLOBALS['phpgw']->db->f('ticket_group');
		$ticket['status']          = $GLOBALS['phpgw']->db->f('ticket_status');

		$ticket['type']           = $GLOBALS['phpgw']->categories->id2name($GLOBALS['phpgw']->db->f('ticket_type'));		
		if ($GLOBALS['phpgw']->db->f('ticket_deadline') == '0000-00-00')
		{
			$ticket['deadline'] = '';
		}
		$ticket['deadline'] = $GLOBALS['phpgw']->db->f('ticket_deadline');
			
		$ticket['effort']         = $GLOBALS['phpgw']->db->f('ticket_effort');
		$ticket['platform']       = $GLOBALS['phpgw']->categories->id2name($GLOBALS['phpgw']->db->f('ticket_platform'));
		
		$GLOBALS['phpgw']->db->query("SELECT name from phpgw_vfs where directory like '/tts/" . $ticket_id . "' AND size > 0",__LINE__,__FILE__);
		$x = 0;
		while ($GLOBALS['phpgw']->db->next_record() != "")
		{
			$ticket['attachment'][$x] =  '/tts/' . $ticket_id . '/' . $GLOBALS['phpgw']->db->f('name');
			++$x;
		}
		
		$GLOBALS['phpgw']->template->set_file('viewticket','viewticket_details.tpl');
		$GLOBALS['phpgw']->template->set_block('viewticket','options_select');
		$GLOBALS['phpgw']->template->set_block('viewticket','additional_notes_row');
		$GLOBALS['phpgw']->template->set_block('viewticket','additional_notes_row_empty');
		$GLOBALS['phpgw']->template->set_block('viewticket','row_history');
		$GLOBALS['phpgw']->template->set_block('viewticket','row_history_empty');
		$GLOBALS['phpgw']->template->set_block('viewticket','form');

		$messages .= $GLOBALS['phpgw']->session->appsession('messages','tts');
		if($messages)
		{
			$GLOBALS['phpgw']->template->set_var('messages',$messages);
			$GLOBALS['phpgw']->session->appsession('messages','tts','');
		}

		if($GLOBALS['phpgw']->db->f('ticket_status') == 'C')
		{
			$GLOBALS['phpgw']->template->set_var('t_status','FIX ME! time closed ' . __LINE__); // $GLOBALS['phpgw']->common->show_date($GLOBALS['phpgw']->db->f('t_timestamp_closed')));
		}
		else
		{
			$GLOBALS['phpgw']->template->set_var('t_status', lang('In progress'));
		}

		// Choose the correct priority to display
		$priority_selected[$ticket['priority']] = ' selected';
		$priority_comment[1]  = ' - '.lang('Lowest'); 
		$priority_comment[5]  = ' - '.lang('Medium'); 
		$priority_comment[10] = ' - '.lang('Highest'); 

		for($i=1; $i<=10; $i++)
		{
			$GLOBALS['phpgw']->template->set_var('optionname', $i.$priority_comment[$i]);
			$GLOBALS['phpgw']->template->set_var('optionvalue', $i);
			$GLOBALS['phpgw']->template->set_var('optionselected', $priority_selected[$i]);
			$GLOBALS['phpgw']->template->parse('options_priority','options_select',true);
		}

		// assigned to
		$accounts = CreateObject('phpgwapi.accounts');
		$account_list = $accounts->get_list('accounts');
		$GLOBALS['phpgw']->template->set_var('optionname',lang('None'));
		$GLOBALS['phpgw']->template->set_var('optionvalue','0');
		$GLOBALS['phpgw']->template->set_var('optionselected','');
		$GLOBALS['phpgw']->template->parse('options_assignedto','options_select',true);
		
		while(list($key,$entry) = each($account_list))
		{
			$GLOBALS['phpgw']->accounts->get_account_name($entry['account_id'],$lid,$fname,$lname);
			if(!$fname && !$lname)
				continue;
			$employees_list[$entry['account_id']] = $GLOBALS['phpgw']->common->display_fullname($lid,$fname,$lname);
			
			if(count($employees_list))
				{
					asort($employees_list);
					reset($employees_list);
				}
		}
		
		while (list($account_id,$account_name) = each($employees_list))
		{
			$selected = '';
			if ($account_id == $ticket['assignedto'])
			{
				$selected = 'selected';
			}
			$GLOBALS['phpgw']->template->set_var('optionname', $account_name);
			$GLOBALS['phpgw']->template->set_var('optionvalue', $account_id);
			$GLOBALS['phpgw']->template->set_var('optionselected', $selected);
			$GLOBALS['phpgw']->template->parse('options_assignedto','options_select',True);
		}
		

		// Figure out when it was opened and last closed
		$history_array = $GLOBALS['phpgw']->historylog->return_array(array(),array('X','O'),'','',$ticket_id);

		while(is_array($history_array) && list(,$value) = each($history_array))
		{
			if($value['status'] == 'O')
			{
				$ticket['opened'] = $GLOBALS['phpgw']->common->show_date($value['datetime'] - ((60*60) * $GLOBALS['phpgw_info']['user']['preferences']['common']['tz_offset']));
			}

			if($value['status'] == 'X')
			{
				$ticket['closed'] = $GLOBALS['phpgw']->common->show_date($value['datetime'] - ((60*60) * $GLOBALS['phpgw_info']['user']['preferences']['common']['tz_offset']));
			}
		}

		// group

		$group_list = array();
		$group_list = $GLOBALS['phpgw']->accounts->membership($GLOBALS['phpgw_info']['user']['account_id']);

		while(list($key,$entry) = each($group_list))
		{
			$tag = '';
			if($entry['account_id'] == $ticket['group'])
			{
				$tag = 'selected';
			}
			$GLOBALS['phpgw']->template->set_var('optionname', $entry['account_name']);
			$GLOBALS['phpgw']->template->set_var('optionvalue', $entry['account_id']);
			$GLOBALS['phpgw']->template->set_var('optionselected', $tag);
			$GLOBALS['phpgw']->template->parse('options_group','options_select',true);
		}

		$GLOBALS['phpgw']->template->set_var('options_category',$GLOBALS['phpgw']->categories->formated_list('select','',$ticket['category'],False));

		$ticket_status[$ticket['status']] = ' selected';
		$s = '<option value="O"' . $ticket_status['O'] . '>' . lang('Open') . '</option>';
		$s .= '<option value="X"' . $ticket_status['X'] . '>' . lang('Closed') . '</option>';

		$GLOBALS['phpgw']->template->set_var('options_status',$s);
		$GLOBALS['phpgw']->template->set_var('lang_status',lang('Status'));

		/**************************************************************\
		* Display additional notes                                     *
		\**************************************************************/
		$history_array = $GLOBALS['phpgw']->historylog->return_array(array(),array('C'),'','',$ticket_id);
		while(is_array($history_array) && list(,$value) = each($history_array))
		{
			$GLOBALS['phpgw']->template->set_var('lang_date',lang('Date'));
			$GLOBALS['phpgw']->template->set_var('lang_user',lang('User'));

			$GLOBALS['phpgw']->template->set_var('value_date',$GLOBALS['phpgw']->common->show_date($value['datetime'] - ((60*60) * $GLOBALS['phpgw_info']['user']['preferences']['common']['tz_offset'])));
			$GLOBALS['phpgw']->template->set_var('value_user',$value['owner']);

			$GLOBALS['phpgw']->template->set_var('value_note',nl2br(stripslashes($value['new_value'])));
			$GLOBALS['phpgw']->template->fp('rows_notes','additional_notes_row',True);
		}

		if(!count($history_array))
		{
			$GLOBALS['phpgw']->template->set_var('lang_no_additional_notes',lang('No additional notes'));
			$GLOBALS['phpgw']->template->fp('rows_notes','additional_notes_row_empty',True);
		}

		/**************************************************************\
		* Display record history                                       *
		\**************************************************************/
		$GLOBALS['phpgw']->template->set_var('lang_history',lang('history'));
		$GLOBALS['phpgw']->template->set_var('lang_user',lang('User'));
		$GLOBALS['phpgw']->template->set_var('lang_date',lang('Date'));
		$GLOBALS['phpgw']->template->set_var('lang_action',lang('Action'));
		$GLOBALS['phpgw']->template->set_var('lang_new_value',lang('New Value'));
		$GLOBALS['phpgw']->template->set_var('lang_old_value',lang('Old Value'));

		$history_array = $GLOBALS['phpgw']->historylog->return_array(array('C'),array(),'','',$ticket_id);
		while(is_array($history_array) && list(,$value) = each($history_array))
		{
			$GLOBALS['phpgw']->nextmatchs->template_alternate_row_color($GLOBALS['phpgw']->template);

			$GLOBALS['phpgw']->template->set_var('value_date',$GLOBALS['phpgw']->common->show_date($value['datetime'] - ((60*60) * $GLOBALS['phpgw_info']['user']['preferences']['common']['tz_offset'])));
			$GLOBALS['phpgw']->template->set_var('value_user',$value['owner']);

			switch($value['status'])
			{
				case 'R': $type = lang('Re-opened'); break;
				case 'X': $type = lang('Closed');    break;
				case 'O': $type = lang('Opened');    break;
				case 'A': $type = lang('Re-assigned'); break;
				case 'P': $type = lang('Priority changed'); break;
				case 'T': $type = lang('Category changed'); break;
				case 'S': $type = lang('Subject changed'); break;
				case 'H': $type = lang('Billable hours changed'); break;
				case 'B': $type = lang('Billable rate changed'); break;
				case 'G': $type = lang('Group ownership changed'); break;
				case 'Y': $type = lang('Type changed'); break;
				case 'D': $type = lang('Deadline changed'); break;
				case 'E': $type = lang('Effort changed'); break;
				case 'L': $type = lang('Platform changed'); break;
				case 'M': $type = lang('Attachment added'); break;
				default: break;
			}

			$GLOBALS['phpgw']->template->set_var('value_action',($type?$type:'&nbsp;'));
			unset($type);

			if($value['status'] == 'A')
			{
				if(!$value['new_value'])
				{
					$GLOBALS['phpgw']->template->set_var('value_new_value',lang('None'));
				}
				else
				{
					$GLOBALS['phpgw']->template->set_var('value_new_value',$GLOBALS['phpgw']->accounts->id2name($value['new_value']));
				}

				if(!$value['old_value'])
				{
					$GLOBALS['phpgw']->template->set_var('value_old_value',lang('None'));
				}
				else
				{
					$GLOBALS['phpgw']->template->set_var('value_old_value',$GLOBALS['phpgw']->accounts->id2name($value['old_value']));
				}
			}
			elseif($value['status'] == 'T')
			{
				$GLOBALS['phpgw']->template->set_var('value_new_value',$GLOBALS['phpgw']->categories->id2name($value['new_value']));
				$GLOBALS['phpgw']->template->set_var('value_old_value',$GLOBALS['phpgw']->categories->id2name($value['old_value']));
			}
			elseif($value['status'] == 'G')
			{
				$s = $GLOBALS['phpgw']->accounts->id2name($value['new_value']);
				$s = ($s ? $s : '--');
				$GLOBALS['phpgw']->template->set_var('value_new_value',$s);

				$s = $GLOBALS['phpgw']->accounts->id2name($value['old_value']);
				$s = ($s ? $s : '--');
				$GLOBALS['phpgw']->template->set_var('value_old_value',$s);
			}
			elseif($value['status'] != 'O' && $value['new_value'])
			{
				$GLOBALS['phpgw']->template->set_var('value_new_value',$value['new_value']);
				$GLOBALS['phpgw']->template->set_var('value_old_value',$value['old_value']);
			}
			else
			{
				$GLOBALS['phpgw']->template->set_var('value_new_value','&nbsp;');
				$GLOBALS['phpgw']->template->set_var('value_old_value','&nbsp;');
			}

			$GLOBALS['phpgw']->template->fp('rows_history','row_history',True);
		}

		if(!count($history_array))
		{
			$GLOBALS['phpgw']->template->set_var('lang_no_history',lang('No history for this record'));
			$GLOBALS['phpgw']->template->fp('rows_history','row_history_empty',True);
		}

		$GLOBALS['phpgw']->template->set_var('lang_update',lang('Update'));

//		$phpgw->template->set_var('additonal_details_rows',$s);

		$GLOBALS['phpgw']->template->set_var('viewticketdetails_link', $GLOBALS['phpgw']->link('/tts/viewticket_details.php'));
		$GLOBALS['phpgw']->template->set_var('ticket_id', $ticket_id);

		$GLOBALS['phpgw']->template->set_var('lang_assignedfrom', lang('Assigned from'));
		$GLOBALS['phpgw']->accounts->get_account_name($ticket['owner'],$owner_lid,$owner_fname,$owner_lname);
		$GLOBALS['phpgw']->template->set_var('value_owner',$GLOBALS['phpgw']->common->display_fullname($owner_lid,$owner_fname,$owner_lname));

		$GLOBALS['phpgw']->template->set_var('row_off', $GLOBALS['phpgw_info']['theme']['row_off']);
		$GLOBALS['phpgw']->template->set_var('row_on', $GLOBALS['phpgw_info']['theme']['row_on']);
		$GLOBALS['phpgw']->template->set_var('th_bg', $GLOBALS['phpgw_info']['theme']['th_bg']);

		$GLOBALS['phpgw']->template->set_var('lang_viewjobdetails', lang('View Job Detail'));

		$GLOBALS['phpgw']->template->set_var('lang_opendate', lang('Open Date'));
		$GLOBALS['phpgw']->template->set_var('value_opendate',$ticket['opened']);

		$GLOBALS['phpgw']->template->set_var('lang_priority', lang('Priority'));
		$GLOBALS['phpgw']->template->set_var('value_priority',$ticket['priority']);

		$GLOBALS['phpgw']->template->set_var('lang_group', lang('Group'));
		$s = $GLOBALS['phpgw']->accounts->id2name($ticket['group']);
		$s = ($s ? $s : '--');
		$GLOBALS['phpgw']->template->set_var('value_group',$s);

		$GLOBALS['phpgw']->template->set_var('lang_billable_hours',lang('Billable hours'));
		$GLOBALS['phpgw']->template->set_var('value_billable_hours',$ticket['billable_hours']);

		$GLOBALS['phpgw']->template->set_var('lang_billable_hours_rate',lang('Billable rate'));
		$GLOBALS['phpgw']->template->set_var('value_billable_hours_rate1',$GLOBALS['phpgw_info']['user']['preferences']['common']['lang'] != 'ja' ? $ticket['billable_rate'] : number_format($ticket['billable_rate']));
		$GLOBALS['phpgw']->template->set_var('value_billable_hours_rate2',$GLOBALS['phpgw_info']['user']['preferences']['common']['lang'] != 'ja' ? $ticket['billable_rate'] : sprintf('%d',$ticket['billable_rate']));
		
		$GLOBALS['phpgw']->template->set_var('currency',$GLOBALS['phpgw_info']['user']['preferences']['common']['currency']);

		$GLOBALS['phpgw']->template->set_var('lang_type',lang('Type'));
		$GLOBALS['phpgw']->template->set_var('value_type',$ticket['type']);
		
		$GLOBALS['phpgw']->template->set_var('lang_deadline',lang('Deadline'));
		$GLOBALS['phpgw']->template->set_var('value_deadline',$ticket['deadline']);
		
		$GLOBALS['phpgw']->template->set_var('lang_effort',lang('Effort'));
		$GLOBALS['phpgw']->template->set_var('value_effort',$ticket['effort']);
		
		$GLOBALS['phpgw']->template->set_var('lang_platform',lang('Platform'));
		$GLOBALS['phpgw']->template->set_var('value_platform',$ticket['platform']);

		for($i=0; $i <= $x-1; $i++)
		{
		$file = $GLOBALS['phpgw']->link('/index.php',Array(
															'menuaction' => 'tts.attachedFiles.show_file',
															'file' => $ticket['attachment'][$i]
															));															                                                            
		$attLink .= "<a href=\"" . $file . "\" target=\"_NEW\">" . basename($ticket['attachment'][$i]) . " </a><br>";
		}
		$GLOBALS['phpgw']->template->set_var('lang_attachment',lang('Attachment'));
		$GLOBALS['phpgw']->template->set_var('value_attachment',$attLink);
		
		$GLOBALS['phpgw']->template->set_var('lang_billable_hours_total',lang('Total billable'));
		$GLOBALS['phpgw']->template->set_var('value_billable_hours_total',$GLOBALS['phpgw_info']['user']['preferences']['common']['lang'] != 'ja' ? sprintf('%01.2f',($ticket['billable_hours'] * $ticket['billable_rate'])) : number_format($ticket['billable_hours'] * $ticket['billable_rate']));

		$type = CreateObject('tts.subcategories',$GLOBALS['phpgw_info']['user']['account_id'],'tts');
		unset($s);
		$s = '<select name="ticket[type]">' . $type->formated_list('select','appandtype',$type->getSubCatElementId('type',$ticket['type']),False) . '</select>';
		$GLOBALS['phpgw']->template->set_var('option_type',$s);
		
		$platform = CreateObject('tts.subcategories',$GLOBALS['phpgw_info']['user']['account_id'],'tts');
		unset($s);
		$s = '<select name="ticket[platform]">' . $platform->formated_list('select','appandplatform',$platform->getSubCatElementId('platform',$ticket['platform']),False) .'</select>';
		$GLOBALS['phpgw']->template->set_var('option_platform',$s);
		
		$GLOBALS['phpgw']->template->set_var('lang_assignedto',lang('Assigned to'));
		if($ticket['assignedto'])
		{
			$GLOBALS['phpgw']->accounts->get_account_name($ticket['assignedto'],$assignedto,$assignedto_fname,$assignedto_lname);
			$assignedto = $GLOBALS['phpgw']->common->display_fullname($assignedto,$assignedto_fname,$assignedto_lname);
		}
		else
		{
			$assignedto = lang('None');
		}
		$GLOBALS['phpgw']->template->set_var('value_assignedto',$assignedto);

		$GLOBALS['phpgw']->template->set_var('lang_subject', lang('Subject'));

		$GLOBALS['phpgw']->template->set_var('lang_details', lang('Details'));
		$GLOBALS['phpgw']->template->set_var('value_details', nl2br(stripslashes($ticket['details'])));

		$GLOBALS['phpgw']->template->set_var('value_subject', stripslashes($ticket['subject']));

		$GLOBALS['phpgw']->template->set_var('lang_additional_notes',lang('Additional notes'));
		$GLOBALS['phpgw']->template->set_var('lang_ok', lang('Save'));
		$GLOBALS['phpgw']->template->set_var('done_url',$GLOBALS['phpgw']->link('/tts/index.php'));
		$GLOBALS['phpgw']->template->set_var('lang_done', lang('done'));

		$GLOBALS['phpgw']->template->set_var('lang_category',lang('Category'));
		$GLOBALS['phpgw']->template->set_var('value_category',$GLOBALS['phpgw']->categories->id2name($ticket['category']));

		$GLOBALS['phpgw']->template->set_var('options_select','');

		$GLOBALS['phpgw']->template->pfp('out','form');
		$GLOBALS['phpgw']->common->phpgw_footer();
	}
	else
	{
		// DB Content is fresher than http posted value.
		$GLOBALS['phpgw']->db->query("select * from phpgw_tts_tickets where ticket_id='$ticket_id'",__LINE__,__FILE__);
		$GLOBALS['phpgw']->db->next_record();

		$ticket['billable_hours'] = $_POST['ticket']['billable_hours'];
		$ticket['billable_rate']  = $_POST['ticket']['billable_rate'];
		$ticket['assignedto']     = $_POST['ticket']['assignedto'];
		$ticket['category']       = $_POST['ticket']['category'];
		$ticket['note']           = $_POST['ticket']['note'];
		$ticket['priority']       = $_POST['ticket']['priority'];
		$ticket['group']          = $_POST['ticket']['group'];
		$ticket['status']         = $_POST['ticket']['status'];
		$ticket['type']           = $_POST['ticket']['type'];
		$ticket['deadline']       = $_POST['ticket']['deadline'];
		$ticket['effort']         = $_POST['ticket']['effort'];
		$ticket['platform']       = $_POST['ticket']['platform'];
		$ticket['attachment']     = $_POST['ticket']['attachment'];
		
		$oldassigned = $GLOBALS['phpgw']->db->f('ticket_assignedto');
		$oldpriority = $GLOBALS['phpgw']->db->f('ticket_priority');
		$oldcategory = $GLOBALS['phpgw']->db->f('ticket_category');
		$old_status  = $GLOBALS['phpgw']->db->f('ticket_status');
		$old_billable_hours = $GLOBALS['phpgw']->db->f('ticket_billable_hours');
		$old_billable_rate = $GLOBALS['phpgw']->db->f('ticket_billable_rate');
		$old_group   = $GLOBALS['phpgw']->db->f('ticket_group');
		$old_type = $GLOBALS['phpgw']->db->f('ticket_type');
		$old_deadline = $GLOBALS['phpgw']->db->f('ticket_deadline');
		$old_effort = $GLOBALS['phpgw']->db->f('ticket_effort');
		$old_platform = $GLOBALS['phpgw']->db->f('ticket_platform');
		$old_attachment = $GLOBALS['phpgw']->db->f('ticket_attachment');
		$old_details = $GLOBALS['phpgw']->db->f('ticket_details');
		
		$GLOBALS['phpgw']->db->transaction_begin();

		/*
		**	phpgw_tts_append.append_type - Defs
		**	R - Reopen ticket
		** X - Ticket closed
		** O - Ticket opened
		** C - Comment appended
		** A - Ticket assignment
		** P - Priority change
		** T - Category change
		** S - Subject change
		** B - Billing rate
		** H - Billing hours
		** G - Group
		** Y - Type
		** D - Deadline
		** E - Effort
		** L - Platform
		** M - Attachment
		*/

		

		if($old_group != $ticket['group'])
		{
			$fields_updated = True;
			$GLOBALS['phpgw']->db->query("update phpgw_tts_tickets set ticket_group='" . $ticket['group']
				. "' where ticket_id='$ticket_id'",__LINE__,__FILE__);
			$GLOBALS['phpgw']->historylog->add('G',$ticket_id,$ticket['group'],$old_group);
		}

		if($oldassigned != $ticket['assignedto'])
		{
			$fields_updated = True;
			$GLOBALS['phpgw']->db->query("update phpgw_tts_tickets set ticket_assignedto='" . $ticket['assignedto']
				. "' where ticket_id='$ticket_id'",__LINE__,__FILE__);
			$GLOBALS['phpgw']->historylog->add('A',$ticket_id,$ticket['assignedto'],$oldassigned);
		}

		if($oldpriority != $ticket['priority'])
		{
			$fields_updated = True;
			$GLOBALS['phpgw']->db->query("update phpgw_tts_tickets set ticket_priority='" . $ticket['priority']
				. "' where ticket_id='$ticket_id'",__LINE__,__FILE__);
			$GLOBALS['phpgw']->historylog->add('P',$ticket_id,$ticket['priority'],$oldpriority);
		}

		if($oldcategory != $ticket['category'])
		{
			$fields_updated = True;
			$GLOBALS['phpgw']->db->query("update phpgw_tts_tickets set ticket_category='" . $ticket['category']
				. "' where ticket_id='$ticket_id'",__LINE__,__FILE__);
			$GLOBALS['phpgw']->historylog->add('T',$ticket_id,$ticket['category'],$oldcategory);
		}

		if($old_billable_hours != $ticket['billable_hours'] && $ticket['billable_hours'])
		{
			$fields_updated = True;
			$GLOBALS['phpgw']->db->query("update phpgw_tts_tickets set ticket_billable_hours='" . $ticket['billable_hours']
				. "' where ticket_id='$ticket_id'",__LINE__,__FILE__);
			$GLOBALS['phpgw']->historylog->add('H',$ticket_id,$ticket['billable_hours'],$old_billable_hours);
		}

		if($old_billable_rate != $ticket['billable_rate'] && $ticket['billable_rate'])
		{
			$fields_updated = True;
			$GLOBALS['phpgw']->db->query("update phpgw_tts_tickets set ticket_billable_rate='" . $ticket['billable_rate']
				. "' where ticket_id='$ticket_id'",__LINE__,__FILE__);
			$GLOBALS['phpgw']->historylog->add('B',$ticket_id,$ticket['billable_rate'],$old_billable_rate);
		}

		if ($old_type != $ticket['type'] && $ticket['type'] != "")
		{
			$fields_updated = True;
			$GLOBALS['phpgw']->db->query("update phpgw_tts_tickets set ticket_type='" . $ticket['type']
				. "' where ticket_id='$ticket_id'",__LINE__,__FILE__);
			$GLOBALS['phpgw']->historylog->add('Y',$ticket_id,$ticket['type'],$old_type);
		}

		if ($old_deadline != $ticket['deadline'] && $ticket['deadline'] != "")
		{
			$fields_updated = True;
			$GLOBALS['phpgw']->db->query("update phpgw_tts_tickets set ticket_deadline='" . $ticket['deadline']
				. "' where ticket_id='$ticket_id'",__LINE__,__FILE__);
			$GLOBALS['phpgw']->historylog->add('D',$ticket_id,$ticket['deadline'],$old_deadline);
		}

		if ($old_effort != $ticket['effort'] && $ticket['effort'] != "")
		{
			$fields_updated = True;
			$GLOBALS['phpgw']->db->query("update phpgw_tts_tickets set ticket_effort='" . $ticket['effort']
				. "' where ticket_id='$ticket_id'",__LINE__,__FILE__);
			$GLOBALS['phpgw']->historylog->add('E',$ticket_id,$ticket['effort'],$old_effort);
		}
		
		if ($old_platform != $ticket['platform'] && $ticket['platform'] != "")
		{
			$fields_updated = True;
			$GLOBALS['phpgw']->db->query("update phpgw_tts_tickets set ticket_platform='" . $ticket['platform']
				. "' where ticket_id='$ticket_id'",__LINE__,__FILE__);
			$GLOBALS['phpgw']->historylog->add('L',$ticket_id,$ticket['platform'],$old_platform);
		}
		
		if ($HTTP_POST_FILES['attachment']['name'] != '')
		{
			$fields_updated = True;
			$attdir = "/tts/" . $ticket_id;
			$basedir = $GLOBALS['basedir'] . "/tts";
			
			if (!file_exists($basedir . "/" . $attdir))
			{
				$GLOBALS['phpgw']->vfs->override_acl = 1;
				$GLOBALS['phpgw']->vfs->mkdir (array (
						'string' => $attdir,
						'relatives' => array (RELATIVE_ALL)));
				$GLOBALS['phpgw']->vfs->override_acl = 0;
			}
						
			$GLOBALS['phpgw']->vfs->override_acl = 1;
			$GLOBALS['phpgw']->vfs->cp(array (
						'from'	=> $HTTP_POST_FILES['attachment']['tmp_name'],
						'to'	=> $attdir . '/' . $HTTP_POST_FILES['attachment']['name'],
						'relatives'	=> array (RELATIVE_NONE|VFS_REAL, RELATIVE_ALL)));
			$GLOBALS['phpgw']->vfs->override_acl = 0;
			
			$GLOBALS['phpgw']->historylog->add('M',$ticket_id,$HTTP_POST_FILES['attachment']['name'],'');
		}
		

		if($old_status != $_POST['ticket']['status'])
		{
 			//only allow assigned-to or admin members to close tickets
			if(($GLOBALS['phpgw_info']['user']['account_id'] == $oldassigned) ||
				($GLOBALS['phpgw']->applications->data['admin']['enabled'] == 1))
			{

				$fields_updated = True;
				$GLOBALS['phpgw']->historylog->add($_POST['ticket']['status'],$ticket_id,$_POST['ticket']['status'],$old_status);

				$GLOBALS['phpgw']->db->query("update phpgw_tts_tickets set ticket_status='"
					. $_POST['ticket']['status'] . "' where ticket_id='$ticket_id'",__LINE__,__FILE__);
			}
 			else
			{
				$messages .= '<br>You can only close a ticket if it is assigned to you.';
				$GLOBALS['phpgw']->session->appsession('messages','tts',lang($messages));
			}
		}
		
		if($ticket['note'])
		{
			$fields_updated = True;
			$GLOBALS['phpgw']->historylog->add('C',$ticket_id,$ticket['note'],'');

			// Do this before we go into mail_ticket()
			$GLOBALS['phpgw']->db->transaction_commit();
		}
		else
		{
			// Only do our commit once
			$GLOBALS['phpgw']->db->transaction_commit();
		}

		if($fields_updated)
		{
			$GLOBALS['phpgw']->session->appsession('messages','tts',lang('Ticket has been updated'));

			if($GLOBALS['phpgw']->config->config_data['mailnotification'])
			{
				mail_ticket($ticket_id);
			} 
		}

		$GLOBALS['phpgw']->redirect($GLOBALS['phpgw']->link('/tts/viewticket_details.php','ticket_id=' . $ticket_id));
	}
?>
