<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_APPROVAL_GROUPS';
$path_to_root = "../..";
//include($path_to_root . "/includes/session.inc");

//page(_($help_context = "Approvers"));

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

include_once($path_to_root . "/projects/includes/db/approvers_db.inc");

simple_page_mode(false);
//----------------------------------------------------------------------------------
if (isset($_GET['approval_group_id']))
{
	$approval_group_id = $_POST['approval_group_id'] = $_GET['approval_group_id'];
}

function display_inactive_approvers($approval_group_id)
{
    $result = get_all_inactive_approvers($approval_group_id);
    label_cell(text_bold("Inactive Approvers"),"align=center colspan=6");
    if(!(db_num_rows($result) > 0)){
        start_row();
        label_cell("No inactive approvers","align=center colspan=6");
        end_row();
    }
    display_approvers($result);
    
}

function display_active_approvers($approval_group_id)
{
    $result = get_all_active_approvers($approval_group_id);
    label_cell(text_bold("Active Approvers"),"align=center colspan=6");
    if(!(db_num_rows($result) > 0)){
        start_row();
        label_cell("No active approvers","align=center colspan=6");
        end_row();
    }
    display_approvers($result);
    
}

function display_approvers($result)
{
    $k = 0; //row colour counter
    
    while ($myrow = db_fetch($result))
    {

        alt_table_row_color($k);

        //label_cell($myrow["approval_group_description"]);
        label_cell($myrow["real_name"]);
        amount_cell($myrow["approval_limit"]);
        amount_cell($myrow["over_budget_limit"]);
        amount_cell($myrow["over_budget_percentage"]);
        //inactive_control_cell($id, $myrow["inactive"], 'item_units', 'abbr');
        edit_button_cell("Edit".$myrow["id"], _("Edit"));
        delete_button_cell("Delete".$myrow["id"], _("Delete"));
        end_row();
    }
}


if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
{
    	//initialise no input errors assumed initially before we test
        $input_error = 0;
        
        $result = get_all_approvers($approval_group_id);
        while ($myrow = db_fetch($result))
        {
            if($_POST['user_id'] == $myrow['user_id'] && $selected_id != $myrow['id'])
                {
                $input_error = 1;
                display_error($myrow['real_name']." is already in this approval group");
                set_focus('user_id');
            }
        }

      
    if (check_num('approval_limit', -1) == FALSE)
	{
		$input_error = 1;
		display_error( _("The approval limit must be -1 or higher."));
		set_focus('approval_limit');
	}
    
//    if (check_num('over_budget_limit', 0) == FALSE)
//	{
//		$input_error = 1;
//                $label = nl2br(text_bold("Budgets: ")."Over Budget Limit \r\n ".
//                                text_bold("Sales Orders: ")."Discount Value Limit  \r\n ".
//                                text_bold("Projects: ")."Project Value Based Overhead Percentage  \r\n ");
//		display_error( _($label." must be numeric and positive."));
//		set_focus('over_budget_limit');
//	}
//    
//    if (check_num('over_budget_percentage', 0) == FALSE)
//	{
//		$input_error = 1;
//                $label = nl2br(text_bold("Budgets: ")."Over Budget Percentage \r\n ".
//                                text_bold("Sales Orders: ")."Discount Percentatge  \r\n ".
//                                text_bold("Projects: ")."Profit Percentage  \r\n ");
//		display_error( _($label." must be numeric and positive."));
//		set_focus('over_budget_percentage');
//	}

	if ($input_error !=1) {
    	write_approvers($selected_id, $_POST['approval_group_id'], $_POST['user_id'], input_num('approval_limit')
                , input_num('over_budget_limit'),input_num('over_budget_percentage'));
		if($selected_id != ''){
			display_notification(_('Selected approver has been updated'));
        }
		else{
			display_notification(_('New approver has been added'));
        }
		$Mode = 'RESET';
	}
}

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

if ($Mode == 'Delete')
{
        delete_approver($selected_id);
        display_notification(_('Selected approver has been deleted'));
	$Mode = 'RESET';
}

if ($Mode == 'RESET')
{
	$selected_id = '';
	$sav = get_post('show_inactive');
	unset($_POST);
	$_POST['show_inactive'] = $sav;
}

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

$result = get_all_approvers($approval_group_id);

start_form();
start_table(TABLESTYLE, "width=80%");
$th = array(_('User'), _('Approval Limit'),
            nl2br("Budgets: Over Budget Limit \r\n Sales Orders: Discount Value Limit  \r\n Projects: Project Value Based Overhead Percentage"),
            nl2br("Budgets: Over Budget Percentatge \r\n Sales Orders: Discount Percentatge  \r\n Projects: Profit Percentage"), "", "");
//inactive_control_column($th);

table_header($th);

display_active_approvers($approval_group_id);
display_inactive_approvers($approval_group_id);

//inactive_control_row($th);
end_table(1);

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

start_table(TABLESTYLE2);

if ($selected_id != '') 
{
 	if ($Mode == 'Edit') {
		//editing an existing item category

		$myrow = get_approvers_details($selected_id);

		$_POST['user_id'] = $myrow["user_id"];
		$_POST['approval_limit']  = $myrow["approval_limit"];
		$_POST['approval_group_description']  = $myrow["approval_group_description"];
                $_POST['approval_group_id']  = $myrow["approval_group_id"];
                $_POST['over_budget_limit']  = $myrow["over_budget_limit"];
                $_POST['over_budget_percentage']  = $myrow["over_budget_percentage"];
	}
	hidden('selected_id', $myrow["id"]);
}
//approval_group_list_cells(_("Select a group:"), 'approval_group_id', null,
//	  _('New Approval'),true, check_value('show_inactive'));
//text_row(_("User ID:"), 'user_id', null, 20, 20);
user_list_row(_("User:"), 'user_id', null, false, true);
//text_row(_("Approval Limit:"), 'approval_limit', null, 20, 20);
amount_row(_("Approval Limit:"), 'approval_limit');
//amount_row(_("Over Budget Limit/ Discount Value Limit:"), 'over_budget_limit');
amount_row(nl2br(text_bold("Budgets: ")."Over Budget Limit \r\n ".
                text_bold("Sales Orders: ")."Discount Value Limit  \r\n ".
                text_bold("Projects: ")."Project Value Based Overhead Percentage"), 'over_budget_limit');
//amount_row(_("Over Budget Percentage/ Discount Percentatge:"), 'over_budget_percentage');
amount_row(nl2br(text_bold("Budgets: ")."Over Budget Percentage \r\n ".
                text_bold("Sales Orders: ")."Discount Percentatge  \r\n ".
                text_bold("Projects: ")."Profit Percentage"), 'over_budget_percentage');
//number_list_row(_("Decimal Places:"), 'decimals', null, 0, 6, _("User Quantity Decimals"));

end_table(1);

submit_add_or_update_center($selected_id == '', '', 'both');

end_form();


?>
