<?php
/**********************************************************************
***********************************************************************/
//include_once($path_to_root . "/includes/date_functions.inc");

//$pcs = ""

function get_salary_total_result($process_code, $employee, $payroll_run, $nopay_days)
{
    $salary_total_configuration = get_salary_total_configuration_detail($payroll_run['payroll_id'], $process_code);
    if($salary_total_configuration){
        if($salary_total_configuration['salary_function_id'] > 0){
            $gsfr = get_salary_function_result($salary_total_configuration);
            return format_salary_totals_result($gsfr, '', $salary_total_configuration['full_value']);
        }
        return get_salary_total_program_result($process_code, $employee, $payroll_run, $nopay_days);
    }
    return get_salary_total_program_result($process_code, $employee, $payroll_run, $nopay_days);
}
    
    
function get_salary_total_program_result($process_code, $employee, $payroll_run, $nopay_days)
{
    $employee_code = $employee['employee_code'];
    $r = array('success' => false, 'message' => 0, 'comment' => '');
    
    $temp_salary_component = array('effective_date' => INFINITE_START_DATE,
                                    'end_date' => INFINITE_END_DATE, 
                                    'nopay_deduct' => 0,
                                    'full_amount' => 0);
        
    $salary_totals_array = get_salary_totals_array($process_code);

    switch ($process_code) {
        case 'SALARY_FOR_OT':
            $sfot_msg = get_SALARY_FOR_OT_value($payroll_run, $employee, $temp_salary_component, $nopay_days);  
            return format_salary_totals_result($sfot_msg, '', true);
            break;
        
        case 'SALARY_TOTAL_1':
            $st1_msg = get_SALARY_TOTAL_1_value();
            return format_salary_totals_result($st1_msg);
            break;
    
        case 'SALARY_TOTAL_2':
            $st2_msg = get_SALARY_TOTAL_2_value();
            //$st3_salary_components_sum = get_all_salary_components_total();
            //$st3 = $salary_totals_array[1]['value'] + $st3_salary_components_sum;    
            return format_salary_totals_result($st2_msg);
            break;
    
        case 'SALARY_TOTAL_3':
            $st3_msg = get_SALARY_TOTAL_3_value();   
            return format_salary_totals_result($st3_msg);
            break;
    
        case 'SALARY_TOTAL_4':
            $st4_msg = get_SALARY_TOTAL_4_value();  
            return format_salary_totals_result($st4_msg);
            break;
        
        case 'SALARY_FOR_EPF':
            $st_salary_for_epf = get_SALARY_FOR_EPF_value($payroll_run['id'], $employee_code);
            return format_salary_totals_result($st_salary_for_epf);
            break;
        
        case 'TAKEHOME_BASIC':
            $thb_msg = get_TAKEHOME_BASIC_value();  
            return format_salary_totals_result($thb_msg);
            break;
        
        case 'TAKEHOME_INCENTIVE':
            $thi_msg = get_TAKEHOME_INCENTIVE_value();  
            return format_salary_totals_result($thi_msg);
            break;
    
        case 'TAKEHOME_TOTAL':
            $thi_msg = get_TAKEHOME_TOTAL_value();  
            return format_salary_totals_result($thi_msg);
            break;
        
        case 'BANKING_INCENT_TOTAL':
            $thi_msg = get_BANKING_INCENT_TOTAL_value();  
            return format_salary_totals_result($thi_msg);
            break;
        
        case 'OT_TOTAL':
            $thi_msg = get_OT_TOTAL_value();  
            return format_salary_totals_result($thi_msg);
            break;
        
        case 'MEAL_TOTAL':
            $thi_msg = get_MEAL_TOTAL_value();  
            return format_salary_totals_result($thi_msg);
            break;
        
        default:
            $all_salary_components_total = get_all_salary_components_total();
            return format_salary_totals_result($all_salary_components_total);
            break;
    }
    
    
}

function get_SALARY_FOR_EPF_value($payroll_run_id, $employee_code)
{
    $BASIC_SALARY = get_calculated_payroll_value("BASIC_SALARY", $employee_code, $payroll_run_id);
    $BRA_1 = get_calculated_payroll_value("BRA_1", $employee_code, $payroll_run_id);
    $BRA_2 = get_calculated_payroll_value("BRA_2", $employee_code, $payroll_run_id);
    $new_basic_salary = $BASIC_SALARY + $BRA_1 + $BRA_2;
    
    $INCENTIVE = get_calculated_payroll_value("INCENTIVE", $employee_code, $payroll_run_id);
    $NO_PAY_DEDUCTIONS = get_calculated_payroll_value("NO_PAY_DEDUCTIONS", $employee_code, $payroll_run_id);
    
    $SALARY_FOR_EPF = $new_basic_salary + (($NO_PAY_DEDUCTIONS / ($new_basic_salary + $INCENTIVE)) * $new_basic_salary);
    
    if($SALARY_FOR_EPF < 0){
        $SALARY_FOR_EPF = 0;
    }

    return array('success' => true, 
                    'calculated_value' => $SALARY_FOR_EPF, 
                    'full_value' => $SALARY_FOR_EPF,
                    'message' => '');
}



function get_SALARY_TOTAL_1_value()
{
    $all_salary_components_total = get_all_salary_components_total();
    return array('success' => true, 
                    'calculated_value' => $all_salary_components_total['calculated_value'], 
                    'full_value' => $all_salary_components_total['full_value'],
                    'message' => '');
}

function get_SALARY_TOTAL_2_value()
{
    $applicable_incentive = 0;
    
    $BASIC_SALARY = get_calculated_salary_component_value("BASIC_SALARY");
    $BRA_1 = get_calculated_salary_component_value("BRA_1");
    $BRA_2 = get_calculated_salary_component_value("BRA_2");
    $new_BASIC_SALARY = $BASIC_SALARY + $BRA_1 + $BRA_2;
    
    
    $INCENTIVE = get_calculated_salary_component_value("INCENTIVE");
    $INCENTIVE_OT = get_calculated_salary_component_value("INCENTIVE_OT");
    $NO_PAY_DEDUCTIONS = get_calculated_salary_component_value("NO_PAY_DEDUCTIONS");
    
    if($INCENTIVE_OT > 0){
        $applicable_incentive = $INCENTIVE_OT;
    }
    else{
        $applicable_incentive = $INCENTIVE;
    }
    $SALARY_TOTAL_2 = $applicable_incentive + (($NO_PAY_DEDUCTIONS / ($new_BASIC_SALARY + $applicable_incentive)) * $applicable_incentive);
    
    if($SALARY_TOTAL_2 < 0){
        $SALARY_TOTAL_2 = 0;
    }
    
    return array('success' => true, 
                    'calculated_value' => $SALARY_TOTAL_2, 
                    'full_value' => $SALARY_TOTAL_2,
                    'message' => '');
}


function get_SALARY_TOTAL_3_value()
{
    $SALARY_FOR_EPF = get_calculated_salary_component_value("SALARY_FOR_EPF");
    $ADDONS_TO_EPF_SALARY = get_calculated_salary_component_value("ADDONS_TO_EPF_SALARY");     
    $VEHICLE_REIMBURSEMEN = get_calculated_salary_component_value("VEHICLE_REIMBURSEMEN");   
    $BANKING_INCENTIVE = get_calculated_salary_component_value("BANKING_INCENTIVE");  
    $BANKING_INCENTIVE_I = get_calculated_salary_component_value("BANKING_INCENTIVE_I"); 
    $BANKING_INCENTIVE_II = get_calculated_salary_component_value("BANKING_INCENTIVE_II");
    $OT_TOTAL = get_calculated_salary_component_value("OT_TOTAL");
    
    $st3 = $SALARY_FOR_EPF + $ADDONS_TO_EPF_SALARY + $VEHICLE_REIMBURSEMEN + $BANKING_INCENTIVE + $BANKING_INCENTIVE_I + $BANKING_INCENTIVE_II + $OT_TOTAL;
    
    return array('success' => true, 
                'calculated_value' => $st3, 
                'full_value' => $st3,
                'message' => '');
}

function get_SALARY_TOTAL_4_value()
{
    $SALARY_TOTAL_2 = get_calculated_salary_component_value("SALARY_TOTAL_2");
    $ADDONS_TO_INCENTIVE = get_calculated_salary_component_value("ADDONS_TO_INCENTIVE");
    $st4 = $SALARY_TOTAL_2 + $ADDONS_TO_INCENTIVE;

    return array('success' => true, 
                'calculated_value' => $st4, 
                'full_value' => $st4,
                'message' => '');
}


function get_TAKEHOME_BASIC_value()
{
    $SALARY_TOTAL_4 = get_calculated_salary_component_value("SALARY_TOTAL_4");
    $SALARY_TOTAL_3 = get_calculated_salary_component_value("SALARY_TOTAL_3");
    
    if($SALARY_TOTAL_4 < 0){
        $TAKEHOME_BASIC = $SALARY_TOTAL_3 + $SALARY_TOTAL_4;
    }
    else{
        $TAKEHOME_BASIC = $SALARY_TOTAL_3;
    }

    return array('success' => true, 
                'calculated_value' => $TAKEHOME_BASIC, 
                'full_value' => $TAKEHOME_BASIC,
                'message' => '');
}


function get_SALARY_FOR_OT_value($payroll_run, $employee, $temp_salary_component, $nopay_days)
{
    $SALARY_FOR_OT = get_all_salary_components_total();
    $SALARY_FOR_OT_calculated_value = $SALARY_FOR_OT['calculated_value'];
    $applicable_start_date = $applicable_end_date = '';
    $payroll_days_factor = 1;
            
    $applicable_days = get_payroll_calculation_applicable_values($payroll_run, $employee, 
                                                $temp_salary_component, $nopay_days, 
                                                $applicable_start_date, $applicable_end_date, $payroll_days_factor);    
    
    if($payroll_days_factor < 1 && $applicable_days < 28){
        if($payroll_days_factor > 0){
        $SALARY_FOR_OT_calculated_value = $SALARY_FOR_OT_calculated_value/$payroll_days_factor;
        //$SALARY_FOR_OT_calculated_value = number_format($SALARY_FOR_OT_calculated_value, 2);
        }
        else{
            $SALARY_FOR_OT_calculated_value = 0;
        }
    }
    return array('success' => true, 
                    'calculated_value' => $SALARY_FOR_OT_calculated_value, 
                    'full_value' => $SALARY_FOR_OT['full_value'], 
                    'message' => '');
}



function get_TAKEHOME_INCENTIVE_value()
{
    $SALARY_TOTAL_4 = get_calculated_salary_component_value("SALARY_TOTAL_4");
    
    if($SALARY_TOTAL_4 < 0){
        $TAKEHOME_INCENTIVE = 0;
    }
    else{
        $TAKEHOME_INCENTIVE = $SALARY_TOTAL_4;
    }

    return array('success' => true, 
                'calculated_value' => $TAKEHOME_INCENTIVE, 
                'full_value' => $TAKEHOME_INCENTIVE,
                'message' => '');
    
}

function get_TAKEHOME_TOTAL_value()
{
    $TAKEHOME_BASIC = get_calculated_salary_component_value("TAKEHOME_BASIC");
    $TAKEHOME_INCENTIVE = get_calculated_salary_component_value("TAKEHOME_INCENTIVE");
    
    $TAKEHOME_TOTAL = $TAKEHOME_BASIC + $TAKEHOME_INCENTIVE;

    return array('success' => true, 
                'calculated_value' => $TAKEHOME_TOTAL, 
                'full_value' => $TAKEHOME_TOTAL,
                'message' => '');
    
}


function get_BANKING_INCENT_TOTAL_value()
{
    $BANKING_INCENTIVE = get_calculated_salary_component_value("BANKING_INCENTIVE");
    $BANKING_INCENTIVE_I = get_calculated_salary_component_value("BANKING_INCENTIVE_I");
    $BANKING_INCENTIVE_II = get_calculated_salary_component_value("BANKING_INCENTIVE_II");
    
    $BANKING_INCENT_TOTAL = $BANKING_INCENTIVE + $BANKING_INCENTIVE_I + $BANKING_INCENTIVE_II;

    return array('success' => true, 
                'calculated_value' => $BANKING_INCENT_TOTAL, 
                'full_value' => $BANKING_INCENT_TOTAL,
                'message' => '');
    
}


function get_OT_TOTAL_value()
{
    $OVER_TIME = get_calculated_salary_component_value("OVER_TIME");
    $SINGLE_OT = get_calculated_salary_component_value("SINGLE_OT");
    $DOUBLE_OT = get_calculated_salary_component_value("DOUBLE_OT");
    
    $OT_TOTAL = $OVER_TIME + $SINGLE_OT + $DOUBLE_OT;

    return array('success' => true, 
                'calculated_value' => $OT_TOTAL, 
                'full_value' => $OT_TOTAL,
                'message' => '');
    
}

function get_MEAL_TOTAL_value()
{
    $MEAL_REIMBURSEMENT_D = get_calculated_salary_component_value("MEAL_REIMBURSEMENT_D");
    $MEAL_REIMBURSE_D = get_calculated_salary_component_value("MEAL_REIMBURSE_D");
    
    $MEAL_TOTAL = $MEAL_REIMBURSEMENT_D + $MEAL_REIMBURSE_D;

    return array('success' => true, 
                'calculated_value' => $MEAL_TOTAL, 
                'full_value' => $MEAL_TOTAL,
                'message' => '');
    
}


function get_salary_totals_array($process_code)
{
    //$result = get_employee_salary_totals($payroll_run_id, $employee_code);
    
    $salary_totals_array = array();

    $all_salary_components_total = get_all_salary_components_total();
    $salary_totals_array[] = array('component' => $process_code, 
                                    'calculated_value' => $all_salary_components_total['calculated_value'],
                                    'full_value' => $all_salary_components_total['full_value']);
  
    
    return $salary_totals_array;
}

function get_all_salary_components_total()
{
    global $salary;

    $elements_calculated_total = 0;
    $elements_full_value_total = 0;
    
    foreach ($salary as $element) {
        if(!($element['type'] == "SALARY_TOTALS" || $element['type'] == "GROUP_TOTALS")){
            $elements_calculated_total += $element['calculated_value'];
            $elements_full_value_total += $element['full_value'];
        }
    }
    return array('success' => true, 
                'calculated_value' => $elements_calculated_total, 
                'full_value' => $elements_full_value_total,
                'message' => '');

}

function format_salary_totals_result($r, $comment = '', $save_full_value = false){
    $f['success'] = $r['success'];
    $f['calculated_value'] = number_format($r['calculated_value'],2,".","");
    $f['full_value'] = number_format($r['full_value'],2,".","");
    $f['save_full_value'] = $save_full_value;
    $f['message'] = $r['message'];
    $f['comment'] = $comment;
    
    return $f;
}
?>
