<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_SALARY_TOTAL_CONFIG';
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/data_checks.inc");
include_once($path_to_root . "/payroll/includes/db/salary_total_configuration_db.inc");
include_once($path_to_root . "/payroll/includes/db/salary_functions_details_db.inc");
include_once($path_to_root . "/payroll/includes/payroll_calculation/pc_functions.inc");
include_once($path_to_root . "/payroll/includes/db/payroll_calculation_db.inc");
simple_page_mode(true);

page(_($help_context = "Salary Total Configuration"));

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

start_form();


br();



$Ajax->activate('leaves');

div_start('leaves');

display_heading('Salary Total Configuration');

br();

value_list_row(_("Select a Salary Total:"), 'payroll_id', 'PAYROLLS', null, null, true);

br(2);

start_table(TABLESTYLE, "width=80%");

$th = array(_("Salary Total Code"),_("Salary Total Name"), 
            _("Function"),  _("Full Amount"));
table_header($th);

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

//function calculate_salary_functions($payroll_run_id,$employee_code,$salary_function_code)
//{   
//    $function_total=0;
//    $salary_function=get_salary_functions_details($salary_function_code);
//    while($salary_function_data = db_fetch($salary_function) )
//    {
//        
//        $value=get_calculated_payroll_db_value($salary_function_data['componant_code'], $employee_code, $payroll_run_id);
//        if($salary_function_data['minus']==0)
//        {
//            $function_total +=$value;
//        }
//        else
//        {
//             $function_total -=$value;
//        }
//        
//    }
//    return $function_total;
//}


//if (isset($_POST['AddComponents']))
//{
//   $fun_total=calculate_salary_functions(3,'0005','OT_TOTAL');
//   label_cells('Function Total', number_format($fun_total, 2));
//}


if (isset($_POST['UpdateData']))
{
    $a_result = get_salary_total_configurations($_POST['payroll_id']);
    delete_salary_configuration_info($_POST['payroll_id']);
    while ($a_row = db_fetch($a_result))
    {
       
        if((isset($_POST["salary_function".$a_row["id"]])) &&
                ($_POST["salary_function".$a_row["id"]] == true))
        {        
                   
          $slfunc_id=0;
          if($_POST['salary_function'.$a_row["id"]] !='-1')   
          {
              $slfunc_id=$_POST['salary_function'.$a_row["id"]];
          }
          
          $full_value=0;
          if($_POST['full_value'.$a_row["id"]] > 0)   
          {
              $full_value=$_POST['full_value'.$a_row["id"]];
          }
          
          add_salary_configuration_info($_POST['payroll_id'], $a_row["salary_total_code"],$slfunc_id,  $full_value);
             
          }
        display_notification(_("Salary total configurations have been updated."));
    }
}



$result_salary_componants=get_salary_total_configurations($_POST['payroll_id']);


while ($myrow = db_fetch($result_salary_componants))
{
    unset($_POST['salary_function'.$myrow["id"]]);
    unset($_POST['full_value'.$myrow["id"]]);
    
	alt_table_row_color($k);

        label_cells('',$myrow["salary_total_code"]);
        label_cells('',$myrow["salary_total_name"]);        
        
        $salary_function_id = 0;
        if(isset($myrow["salary_function_id"]) && $myrow["salary_function_id"] > 0){
            $salary_function_id = $myrow["salary_function_id"];
        }
        value_list_cells(null, 'salary_function'.$myrow["id"], "SALARY_FUNCTIONS",$salary_function_id,"System Function");
        
        $full_value = 0;
        if(isset($myrow["full_value"]) && $myrow["full_value"] > 0){
            $full_value = $myrow["full_value"];
        }
        yesno_list_cells(null, 'full_value'.$myrow["id"], $full_value,'Yes','No', false);
      

	end_row();
	$j++;

}

end_table(1);

br();


div_end();
submit_center('UpdateData', _("Update"), true, false, 'default');

br();
//submit_center('AddComponents', _("Add"), true, false, 'default');

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