<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_SALESPRICE';
if (!@$_GET['popup'])
	$path_to_root = "..";
else	
	$path_to_root = "../..";

//include($path_to_root . "/includes/session.inc");
//include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/payroll/includes/db/employee_departments_db.inc");

$js = '';

if (user_use_date_picker()) {
        $js .= get_js_date_picker();
}
if (!@$_GET['popup'])
	page(_($help_context = "Promotion Benefit Items"));

simple_page_mode(false);

if (isset($_GET['employee_code']))
{
	$_POST['employee_code'] = $_GET['employee_code'];
}

if (!isset($_POST['employee_code'])){
	$_POST['employee_code'] = get_global_employee_code();
}

set_global_employee_code($_POST['employee_code']);

if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
{
	//initialise no input errors assumed initially before we test
	$input_error = 0;
        
	if (isset($_POST['department_id']) == false || $_POST['department_id'] == -1)
	{
            $input_error = 1;
            display_error(_("Select a department."));
            set_focus('department');        
	}
	
    if (!isset($_POST['rate_category_id']) || $_POST['rate_category_id'] == -1)
	{
            $input_error = 1;
            display_error(_("Select a rate category."));
            set_focus('department');        
	}
    
	if (!is_date($_POST['start_date']))
    {
        $input_error = 1;
        display_error( _("The start date entered is invalid."));
        set_focus('start_date');
    }

    if (!is_date($_POST['end_date']))
    {
        $input_error = 1;
        display_error( _("The end date entered is invalid."));
        set_focus('end_date');
    }

    if (date1_greater_date2($_POST['start_date'], $_POST['end_date']))
    {
        $input_error = 1;
        display_error( _("End date can not be greater than start date"));
        set_focus('end_date');         
    }


	if ($input_error !=1){
        if($Mode ==  'UPDATE_ITEM'){
            update_employee_department($selected_id, $_POST['department_id'], $_POST['rate_category_id'],
                            $_POST['start_date'], $_POST['end_date']);
            display_notification(_('Selected employee department has been updated'));
        }
        else
        {
            add_employee_department($_POST['employee_code'], $_POST['department_id'], $_POST['rate_category_id'],
                            $_POST['start_date'], $_POST['end_date']);

            display_notification(_('New employee department has been added'));
        }    
        $selected = -1;
        $Mode = 'RESET';
	}
}

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

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

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

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

$result = get_all_employee_departments($_POST['employee_code']);

start_form();
start_table(TABLESTYLE, "width=30%");
$th = array(_('Department'),_('Rate Category'), _("Start Date"), _("End date"), "", "");
//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["department_name"]);
    label_cell($myrow["rate_category_name"]);
    label_cell($myrow["start_date"]);
    if($myrow["end_date"] == INFINITE_END_DATE){
        label_cell("");
    }
    else{
        label_cell(sql2date($myrow["end_date"]),"align=center");
    }
    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_department($selected_id);
        
        $_POST['department_id'] = $myrow["department_id"];
        $_POST['rate_category_id']  = $myrow["rate_category_id"];
        $_POST['start_date'] = sql2date($myrow["start_date"]);
        $_POST['end_date'] = sql2date($myrow["end_date"]);
        hidden('selected_id', $myrow["id"]);
    }
    else{
        $_POST['department_id'] = 0;
        $_POST['rate_category_id']  = 0;
        $selected_id = -1;
    }
    

//}


//text_row(_("Department:"), 'department', null, 20, 20);
value_list_row(_("Department:"), 'department_id', 'DEPARTMENTS', $_POST['department_id'],"Select a Department");
rate_category_list_row(_("Rate Category:"), 'rate_category_id', $_POST["rate_category_id"], "Select a Rate category");
date_row(_('Start Date:'), 'start_date');   

if ($Mode != 'Edit') 
{  
    $_POST['end_date'] = sql2date(INFINITE_END_DATE);
} 
date_row(_('End Date:'), 'end_date');


end_table(1);

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

if (!@$_GET['popup'])
{
	end_form();
	end_page(@$_GET['popup'], false, false);
}

?>
