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


$page_security = 'SA_APPROVAL_GROUPS';
$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 Group"), @$_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_group_db.inc");
include_once($path_to_root . "/projects/includes/db/approvers_db.inc");

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


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

$selected_id = get_post('approval_group_id');

if (list_updated('approval_group_id')) {
	$_POST['NewProjectID'] = $project_id = get_post('approval_group_id');
    clear_data();
	$Ajax->activate('details');
	$Ajax->activate('controls');
}
$project1 = get_post('NewProjectID');
if (get_post('cancel')) {
	$_POST['NewProjectID'] = $project_id = $_POST['approval_group_id'] = '';
    clear_data();
	set_focus('approval_group_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_group_code']);
    unset($_POST['approval_group_description']);
}

function can_commit($selected_id)
{
	if (strlen($_POST['approval_group_code']) == 0)
	{
		display_error(_("The approval group code cannot be empty."));
		set_focus('approval_group_code');
        return false;
	}
    
    if (strlen($_POST['approval_group_description']) == 0)
	{
		display_error(_("The approval group description cannot be empty."));
		set_focus('approval_group_description');
        return false;
	}

	if (strlen($_POST['approval_group_description'])>(200))
	{
		display_error(_("The approval group description is too long."));
		set_focus('approval_group_description');
        return false;
	}

    $code = get_existing_approval_group_code($_POST['approval_group_code'], $selected_id);
    if(isset($code['approval_group_code'])){
        display_error(_("The entered approval group code already exists."));
		set_focus('approval_group_code');
        return false;
    }
	return true;
    
}
//------------------------------------------------------------------------------------

if (isset($_POST['addupdate'])) 
{
	if(can_commit($selected_id)){
        write_approval_group($selected_id, $_POST['approval_group_code'] , $_POST['approval_group_description'] );
        clear_data();
		if($selected_id > 0){
			display_notification(_('Selected approval group has been updated'));
            $selected_id = -1;
        }
		else{
			display_notification(_('New approval group has been added'));
        }
        
        
        $Ajax->activate('_page_body');
        
		$Mode = 'RESET';
    }
	
}

if (isset($_POST['delete']) && strlen($_POST['delete']) > 1) 
{
    $result = get_all_approvers($selected_id);
    $myrow = db_fetch($result);
    if($myrow != '')
    {
        display_error(_("Selected approval group has approvers and can not be deleted. "));
        $Ajax->activate('_page_body');
    }
    else 
    {

        delete_approval_group($selected_id);

        display_notification(_("Selected approval group 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(_('Code:'), 'approval_group_code', null, 20, 20);
             text_row(_('Description:'), 'approval_group_description', null, 50, 200);

            $_POST['inactive'] = 0;
	} 
	else 
	{ // Must be modifying an existing item

                if (get_post('approval_group_id') || get_post('addupdate')) { // first item display
 
                    $_POST['NewProjectID'] = $_POST['approval_group_id'];
                    
                    $myrow = get_approval_group_details($_POST['NewProjectID']);

                    $_POST['approval_group_code']  = $myrow["approval_group_code"];
                    $_POST['approval_group_description']  = $myrow["approval_group_description"];
		}

        text_row(_('Code:'), 'approval_group_code', null, 20, 20);
		text_row(_('Description:'), 'approval_group_description', null, 50, 200);
		
	}
    set_focus('Code');

	end_outer_table(1);

	div_start('controls');
	if (!isset($_POST['NewProjectID']) || $new_item) 
	{
		submit_center('addupdate', _("Insert New Approval Group"), true, '', 'default');
	} 
	else 
	{
		submit_center_first('addupdate', _("Update Approval Group"), '', 
			@$_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 Group"), true, '', true);
		submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
	}

	div_end();
}

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

start_form(true);

if (db_has_approval_groups()) 
{
	start_table(TABLESTYLE_NOBORDER);
	start_row();
    approval_group_list_cells(_("Select Approval Group:"), 'approval_group_id', null,
	  _('New Approval Group'), true);
    
	$new_item = get_post('approval_group_id')=='';
        
        if(get_post('approval_group_id') == -1 || get_post('approval_group_id') == ''){
            $new_item = TRUE;
        }

        button_cell('Refresh', 'Refresh');

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

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



div_start('details');

$project_id = get_post('approval_group_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 Group'), $project_id),
		'sales_pricing' => array(_('&Approvers'), $project_id),
	));
	
	switch (get_post('_tabs_sel')) 
        {
		default:
		case 'settings':
                        project_settings($project_id); 
			break;
		case 'sales_pricing':
			$_GET['approval_group_id'] = $project_id;
			$_GET['popup'] = 1;
			include_once($path_to_root."/projects/manage/approvers.php");
			break;
	};
br();
tabbed_content_end();

div_end();


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

end_form();

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

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