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

page(_($help_context = "Employee Rates"));

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

include_once($path_to_root . "/payroll/includes/db/employee_rates_db.inc");

simple_page_mode(false);
//----------------------------------------------------------------------------------

if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
{
	//initialise no input errors assumed initially before we test
	$input_error = 0;
        
	if (strlen($_POST['rate_category']) == 0)
	{
		$input_error = 1;
		display_error(_("Rate category can not be empty."));
		set_focus('rate_category');
//                return 1;              
	}
        if (is_numeric($_POST['rate_category'])){
            
                $input_error = 1;
		display_error(_("Rate category can not be numeric."));
		set_focus('rate_category');
//                return 1;
        }
	if (!check_num('rate', 0))
        {
            $input_error = 1;
            display_error( _("Rate must be numeric and can not be negative."));
            set_focus('rate');

        }


	if ($input_error !=1)
        {
            write_employee_rates($selected_id, $_POST['rate_category'], input_num('rate'));
            
            if($selected_id != '')
            {
                display_notification(_('Selected employee rate has been updated'));
            }
            else
            {
                display_notification(_('New employee rate has been inserted'));
            }    
                
            
            $Mode = 'RESET';
	}
}

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

if ($Mode == 'Delete')
{
    delete_employee_rates($selected_id);
    display_notification(_('Selected employee rate has been deleted'));
    $Mode = 'RESET';
}

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

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

$result = get_all_employee_rates();

start_form();
start_table(TABLESTYLE, "width=20%");
$th = array(_('Rate Category'),_('Rate'), "", "");
//inactive_control_column($th);

table_header($th);
$k = 0; //row colour counter

while ($myrow = db_fetch($result))
{
    alt_table_row_color($k);

    label_cell($myrow["description"]);
    label_cell($myrow["rate"], 'align=right');
    
    edit_button_cell("Edit".$myrow["id"], _("Edit"));
    
    delete_button_cell("Delete".$myrow["id"], _("Delete"));
    
    end_row();
}

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

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

start_table(TABLESTYLE2);

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

        $myrow = get_employee_rates($selected_id);
        
        $_POST['rate'] = $myrow["rate"];
        $_POST['rate_category']  = $myrow["description"];
    }
    hidden('selected_id', $myrow["id"]);

}

text_row(_("Rate Category:"), 'rate_category', null, 20, 20);
amount_row(_("Rate:"), 'rate');
end_table(1);

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

end_form();

end_page();

?>
