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

//page(_($help_context = "Email Template"));

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

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

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

if (isset($_GET['payroll_id']) && $_GET['payroll_id'] > 0) 
{
    $payroll_id = $_GET['payroll_id'];
}

if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
{

	//initialise no input errors assumed initially before we test
	$input_error = 0;

	if (!check_num('days_factor',0))
    {        
        $input_error = 1;
        display_error("Days factor must be positive and numeric");
        set_focus('days_factor');
    }
    
    $result = select_nopay_references($payroll_id);
    while ($row = db_fetch($result)) {
        if(($Mode == 'ADD_ITEM' && $row['employment_type_id'] == $_POST['employment_type_id']) || 
                    ($Mode == 'UPDATE_ITEM' && $row['employment_type_id'] == $_POST['employment_type_id'] && $row['id'] != $selected_id)){
            $input_error = 1;
            display_error("There is an already added same employment type record for this payroll.");
            set_focus('employment_type_id');
        }
    }
    

	if ($input_error == 0) {
    	write_nopay_reference($selected_id, $_POST['employment_type_id'], $_POST['nopay_reference_value_id'], $_POST['days_factor'], $payroll_id);
		if($selected_id != ''){
            display_notification(_('Selected nopay reference has been updated'));
        }    
    	else{
            display_notification(_('New nopay reference has been added'));
        }
		$Mode = 'RESET';
	}
}


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

if ($Mode == 'Delete')
{

        delete_nopay_reference($selected_id);
        display_notification(_('Selected reference has been deleted'));
	$Mode = 'RESET';
}

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

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

$result = select_nopay_references($payroll_id);

start_form();
start_table(TABLESTYLE, "width=60%");
$th = array(_('Employment Type'),_('Reference Value'),_('Days Factor'), "", "");
inactive_control_column($th);

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

while ($myrow = db_fetch($result))
{

	alt_table_row_color($k);

	alt_table_row_color($k);

        label_cell($myrow["emp_type"]);
        label_cell($myrow["ref_value"]);
        amount_cell($myrow["days_factor"]);

        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_nopay_refercnce_details($selected_id);

		$_POST['employment_type_id'] = $myrow["employment_type_id"];
        $_POST['nopay_reference_value_id'] = $myrow["nopay_reference_value_id"];
        $_POST['days_factor']  = $myrow["days_factor"];
        }
        hidden('selected_id', $myrow["id"]);
}

    if(isset($_POST['employment_type_id'])){
    value_list_row(_("Employment Type:"), 'employment_type_id', "EMPLOYMENT_TYPES", $_POST['employment_type_id']);
    }
    else {
        value_list_row(_("Employment Type:"), 'employment_type_id', "EMPLOYMENT_TYPES");
    }
    if(isset($_POST['nopay_reference_value_id'])){
    value_list_row(_("Nopay Calculation Reference Value:"), 'nopay_reference_value_id', "SALARY_TOTALS", $_POST['nopay_reference_value_id']);
    }
    else {
        value_list_row(_("Nopay Calculation Reference Value:"), 'nopay_reference_value_id', "SALARY_TOTALS");
    }
    amount_cells(_("Days Factor:"), 'days_factor');

end_table(1);

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

end_form();

//end_page();

?>
