<?php
/**********************************************************************
***********************************************************************/


$page_security = 'SA_APPROVAL_PATHS';
$path_to_root = "../..";
include($path_to_root . "/includes/session.inc");
include_once ($path_to_root . "/trace.php");
$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(900, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();
	
page(_($help_context = "Approval Path"), @$_REQUEST['popup'], false, "", $js);

include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");

include_once($path_to_root . "/inventory/includes/inventory_db.inc");
include_once($path_to_root . "/projects/includes/db/approval_path_db.inc");
include_once($path_to_root . "/projects/includes/db/approval_path_configuration_db.inc");

$user_comp = user_company();
$new_item = get_post('approval_path_id')== -1 || get_post('cancel'); 


//------------------------------------------------------------------------------------

$selected_id = get_post('approval_path_id');

if (list_updated('approval_path_id')) {
	$_POST['NewProjectID'] = $project_id = get_post('approval_path_id');
    clear_data();
	$Ajax->activate('details');
	$Ajax->activate('controls');
}
$project1 = get_post('NewProjectID');
if (get_post('cancel')) {
	$_POST['NewProjectID'] = $project_id = $_POST['approval_path_id'] = '';
    clear_data();
	set_focus('approval_path_id');
	$Ajax->activate('_page_body');
}
//if (list_updated('category_id') || list_updated('mb_flag')) {
//	$Ajax->activate('details');
//}


//check_db_has_project_tasks(_("There are no project tasks defined in the system."));

function clear_data()
{
	unset($_POST['approval_path_description']);
        unset($_POST['approval_path_name']);
}

//------------------------------------------------------------------------------------

if (isset($_POST['addupdate'])) 
{ 
	$input_error = 0;
        
	if (strlen($_POST['approval_path_description']) == 0) 
	{
		$input_error = 1;
		display_error( _('The approval path description must be entered.'));
		set_focus('description');
	}

	if ($input_error !=1) 
    {
		if($selected_id > 0)
        {
            update_approval_path($selected_id, $_POST['approval_path_name'], $_POST['approval_path_description'] );
            display_notification(_('Selected approval path has been updated'));
            clear_data();
            set_focus('approval_path_description');
        }
		else{
            add_approval_path($selected_id, $_POST['approval_path_name'], $_POST['approval_path_description'] );
            display_notification(_('New approval path has been added'));
            clear_data();
            set_focus('approval_path_name');
        }
        
        $Ajax->activate('_page_body');
	}
}

if (isset($_POST['delete']) && strlen($_POST['delete']) > 1) 
{
    $result = get_approval_path_configuration($_POST['approval_path_id']);
    $myrow = db_fetch($result);
    if($myrow != '')
    {
        display_error(_("Selected approval path can not be deleted.Its has approval path configurations"));
        $Ajax->activate('_page_body');
    }
    else 
    {
        $approval_path_id = $_POST['approval_path_id'];
        delete_approval_path($approval_path_id);

        display_notification(_("Selected approval path has been deleted."));

        clear_data();
        $Ajax->activate('_page_body'); 
    }
	
}
//------------------------------------------------------------------------------------

function project_settings(&$project_id) 
{
	global $SysPrefs, $path_to_root, $new_item, $pic_height, $Refs;;

	start_outer_table(TABLESTYLE2);

	table_section(1);

	table_section_title(_("General"));
        
        

	//------------------------------------------------------------------------------------
	if ($new_item) 
	{
		//text_row(_("Project Code:"), 'NewProjectID', null, 21, 20);
                text_row(_('Name:'), 'approval_path_name', null, 50, 200);
                //text_row(_('Description:'), 'approval_path_description', null, 52, 200);
                textarea_row(_('Description:'), 'approval_path_description', null, 31, 3);
		$_POST['inactive'] = 0;
	} 
	else 
	{ // Must be modifying an existing item

                if (get_post('approval_path_id') || get_post('addupdate')) { // first item display

                    
                        $_POST['NewProjectID'] = $_POST['approval_path_id'];
			$myrow = get_approval($_POST['NewProjectID']);
                        
			$_POST['approval_path_description'] = $myrow["approval_path_description"];
			$_POST['approval_path_name'] = $myrow["approval_path_name"];
		}

		text_row(_('Name:'), 'approval_path_name', null, 50, 200);
                textarea_row(_('Description:'), 'approval_path_description', null, 31, 3);
		//hidden('NewProjectID', $_POST['NewProjectID']);
		set_focus('description');
	}


	end_outer_table(1);

	div_start('controls');
	if (!isset($_POST['NewProjectID']) || $new_item) 
	{
		submit_center('addupdate', _("Insert New Approval Path"), true, '', 'default');
	} 
	else 
	{
		submit_center_first('addupdate', _("Update Approval Path"), '', 
			@$_REQUEST['popup'] ? true : 'default');
                
		submit_return('select', get_post('project_id'), 
			_("Select this items and return to document entry."), 'default');
		//submit('clone', _("Clone This Item"), true, '', true);
		submit('delete', _("Delete This Approval Path"), true, '', true);
		submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
	}

	div_end();
}

//-------------------------------------------------------------------------------------------- 

start_form(true);

if (db_has_approval_paths()) 
{
	start_table(TABLESTYLE_NOBORDER);
	start_row();
        approval_path_list_cells(_("Select Approval Path:"), 'approval_path_id', null,
	  _('New Approval Path'), true);
    
	$new_item = get_post('approval_path_id')=='';
        
        if(get_post('approval_path_id') == -1 || get_post('approval_path_id') == ''){
            $new_item = TRUE;
        }


	//check_cells(_("Show inactive:"), 'show_inactive', null, true);
	end_row();
	end_table();

}
else
{
	hidden('approval_path_id', get_post('approval_path_id'));
}



div_start('details');

$project_id = get_post('approval_path_id');


if ($project_id == -1){
    $project_id = '';
}

if (!$project_id){
	unset($_POST['_tabs_sel']); // force settings tab for new customer
}

tabbed_content_start('tabs', array(
		'settings' => array(_('&Approval Path'), $project_id),
		'sales_pricing' => array(_('&Approval Path Configuration'), $project_id),
	));
	
	switch (get_post('_tabs_sel')) 
        {
		default:
		case 'settings':
                        project_settings($project_id); 
			break;
		case 'sales_pricing':
			$_GET['approval_path_id'] = $project_id;
			$_GET['popup'] = 1;
			include_once($path_to_root."/projects/manage/approval_path_configuration.php");
			break;
	};
br();
tabbed_content_end();

div_end();


hidden('popup', @$_REQUEST['popup']);

end_form();

//------------------------------------------------------------------------------------

end_page(@$_REQUEST['popup']);
?>
