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

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/inventory.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/payroll/includes/db/salary_functions_details_db.inc");


$js = '';

if ($SysPrefs->use_popup_windows) {
	$js .= get_js_open_window(900, 500);
}

if (user_use_date_picker()) {
	$js .= get_js_date_picker();
}

page(_($help_context = "Salary Functions Details"));

//----------------------------------------------------------------------------------------
simple_page_mode(true);

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






function check_data($selected_id='-1')
{	
        if (!isset($_POST['salary_function_id']) || $_POST['salary_function_id']=="-1" )
   	{
            display_error( _("Please select a salary function."));
            set_focus('salary_function_id');
            return false;
   	}
        
        if (!isset($_POST['salary_component_id']) || $_POST['salary_component_id']=="" )
   	{
            display_error( _("Please select a salary component."));
            set_focus('salary_function_id');
            return false;
   	}
    
       if(!check_num("seq_no", 0) || ! (input_num('seq_no') > 0)){
            display_error( _("Sequence number entered is not valid."));
            set_focus('seq_no');
            return false;
       }
    
        return true;
    
}
//--------------------------------------------------------------------------------------------------


if (($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') && check_data($selected_id))
{

    if ($Mode == 'ADD_ITEM') 
    {
            $rec_id = add_salary_function_detail($_POST['salary_function_id'],$_POST['seq_no'], $_POST['plus_minus'],$_POST['salary_component_id'], $_POST['full_value']);
  

            if($rec_id > 0){
                display_notification(_("Salary function detail  has been added."));
            }
            else{
                display_error(_("Salary function detail could not be added."));
            }
    } 
    if ($Mode == 'UPDATE_ITEM') 
    {
            $recs = update_salary_function_detail($selected_id, $_POST['seq_no'], $_POST['plus_minus'],$_POST['salary_component_id'], $_POST['full_value']);

            $selected_id = -1;
            
            if($recs == 1){
                display_notification(_("Salary function detail has been updated."));
            }
            else{
                display_error(_("Salary function detail could not be updated."));
            }
    }
    $Mode = 'RESET';

}

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

if ($Mode == 'Delete')
{
        $recs = delete_salary_function_detail($selected_id);
        
        if($recs == 1){
                display_notification(_("Salary function detail has been deleted."));
        }
        else{
                display_error(_("Salary function detail could not be deleted."));
        }

	$Mode = 'RESET';
}

if ($Mode == 'RESET')
{
	$selected_id = -1;
}

if (isset($_POST['_selected_id_update']) )
{
	$selected_id = $_POST['selected_id'];
	//$Ajax->activate('_page_body');
}

//if (list_updated('employee_code')) 
	$Ajax->activate('_page_body');
//--------------------------------------------------------------------------------------------------

start_form();

    start_table();
        value_list_row(_("Select a Salary Total:"), 'salary_function_id', 'SALARY_FUNCTIONS', null, null, true);
    end_table(2);

    $result = get_salary_functions_details($_POST['salary_function_id']);
    
    div_start('price_table');
    
    if (db_num_rows($result) == 0)
    {
    	display_note(_("There are no salary functions created for the selected salary total"));
    }
    else
    {
        start_table(TABLESTYLE, "width=60%");

	$th = array("Seq. No", _("Plus/Minus"),_("Salary Component Type"), _("Component Code"), _("Component Name"), _("Full Amount"),"", "");

        table_header($th);

        $k = $j = 0; //row colour counter

        while ($myrow = db_fetch($result))
        {
            alt_table_row_color($k);
            $plus_minus="";
            if($myrow["minus"]!=0)
            {
                $plus_minus="Minus";
            }
            else
            {
                $plus_minus="Plus";
            }
            qty_cell($myrow["seq_no"], false, 0);
            label_cell($plus_minus, "align=center");
            label_cell($myrow["component_type_name"]);
            label_cell($myrow["componant_code"]);
            label_cell($myrow["componant_name"]);

            label_cell(get_yes_no_name($myrow["full_value"]), "align=center");
            
            edit_button_cell("Edit".$myrow['id'], _("Edit"));
            delete_button_cell("Delete".$myrow['id'], _("Delete"));
       
            }

            end_row();

            $j++;
            If ($j == 12)
            {
            	$j = 1;
        		table_header($th);
            } //end of page full new headings
       // } //end of while loop

        end_table();
    }
    div_end();

//-----------------------------------------------------------------------------------------------
    $_POST['id'] = 0;
    $_POST['plus_minus'] = "";
    $_POST['salary_component_id'] = "";
   
    
$dec2 = 6;
if ($Mode =='Edit')
{
    $myrow = get_salay_function_detail_line($selected_id);

    $_POST['id'] = $myrow["id"];
    $_POST['seq_no'] = $myrow["seq_no"];
    $_POST['plus_minus'] = $myrow["minus"];
    $_POST['salary_component_id'] = $myrow["componant_id"];
    $_POST['full_value'] = $myrow["full_value"];
    
    
    hidden('id', $_POST['id']); 
}

br();

hidden('selected_id', $selected_id);

start_table(TABLESTYLE2);

amount_row("Sequence No:", "seq_no", null, null, null, 0);

yesno_list_row('Plus/Minus:', 'plus_minus', null,'Minus','Plus', false);

payroll_salary_components_list_row('Salary Component','salary_component_id',null,'Select Component');
   
yesno_list_row('Full Amount:', 'full_value', null,'Yes','No', false);
   

end_table(1);

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


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