<?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/hold_salary_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['payroll_run_id']) == false || $_POST['payroll_run_id'] == -1)
	{
            $input_error = 1;
            display_error(_("Select a payroll run."));
            set_focus('payroll_run_id');        
	}
   
	if ($input_error !=1){
        if($Mode ==  'UPDATE_ITEM'){
            update_hold_salary($selected_id, $_POST['payroll_run_id'], $_POST['hold_salary'],
                            $_POST['comment']);
            display_notification(_('Selected hold salary has been updated'));
        }
        else
        {
            add_hold_salary($_POST['employee_code'], $_POST['payroll_run_id'], $_POST['hold_salary'],
                            $_POST['comment']);

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

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

if ($Mode == 'Delete')
{
    delete_hold_salary($selected_id);
    display_notification(_('Selected hold salary has been deleted'));
    $Mode = 'RESET';
}

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

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

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

start_form();
br(1);
start_table(TABLESTYLE, "width=30%");
$th = array(_('Payroll Run'),_('Hold salary'), _("Comment"), "", "");
//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["payroll_run"]);
    if($myrow["hold_salary"] == 1){
        label_cell("Yes", 'align = center');
    }
    else{
        label_cell("No", 'align = center');
    }
        
    label_cell($myrow["comment"]);

    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 ($Mode == 'Edit') {
    //editing an existing item category

    $myrow = get_hold_salary($selected_id);

    $_POST['payroll_run_id'] = $myrow["payroll_run_id"];
    $_POST['hold_salary']  = $myrow["hold_salary"];
    $_POST['comment']  = $myrow["comment"];
    hidden('selected_id', $myrow["id"]);
}
else{
    $_POST['payroll_run_id'] = 0;
    $selected_id = -1;
}


//value_list_row(_("Salary Run:"), 'payroll_run_id', 'PAYROLLS', $_POST['payroll_run_id'],"Select a Salary Run");
payroll_run_list_row("Payroll Run:", 'payroll_run_id', $_POST['payroll_run_id'], "Select a Payroll Run");
yesno_list_row(_("Hold Salary:"), 'hold_salary');
text_row(_("Comment:"), 'comment', null, 40, 200);

end_table(1);

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

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

?>
