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

//$pcs = ""

function get_salary_total_result(&$salary, $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, $salary_total_configuration);
            return format_salary_totals_result($gsfr, '', $salary_total_configuration['full_value']);
        }
        return get_salary_total_program_result($salary, $process_code, $employee, $payroll_run, $nopay_days);
    }
    return get_salary_total_program_result($salary, $process_code, $employee, $payroll_run, $nopay_days);
}
    
    
function get_salary_total_program_result(&$salary, $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($salary, $process_code);

    if ($process_code == "CARID_FRWRD_DEFICIT"){
        $dd =1342314;
    }
    switch ($process_code) {
        case 'SALARY_FOR_OT':
            $sfot_msg = get_SALARY_FOR_OT_value($salary, $payroll_run, $employee, $temp_salary_component, $nopay_days);  
            return format_salary_totals_result($sfot_msg, '', true);
            break;
        
        case 'BASIC_SALARY_TOT':
            $st1_msg = get_BASIC_SALARY_TOT_value($salary);
            return format_salary_totals_result($st1_msg);
            break;
    
        case 'CARID_FRWRD_DEFICIT':
            $st2_msg = get_CARID_FRWRD_DEFICIT_value($salary);
            //$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 'NET_SALARY_TOT':
            $st3_msg = get_NET_SALARY_TOT_value($salary);   
            return format_salary_totals_result($st3_msg);
            break;
    
        case 'GROSS_SALARY':
            $st4_msg = get_GROSS_SALARY_value($salary);  
            return format_salary_totals_result($st4_msg);
            break;
        
        case 'SALARY_FOR_EPF':
            $st_salary_for_epf = get_SALARY_FOR_EPF_value($salary);
            return format_salary_totals_result($st_salary_for_epf);
            break;
        
        case 'TAKEHOME_BASIC':
            $thb_msg = get_TAKEHOME_BASIC_value($salary);  
            return format_salary_totals_result($thb_msg);
            break;
        
        case 'TAKEHOME_INCENTIVE':
            $thi_msg = get_TAKEHOME_INCENTIVE_value($salary);  
            return format_salary_totals_result($thi_msg);
            break;
    
        case 'TAKEHOME_TOTAL':
            $thi_msg = get_TAKEHOME_TOTAL_value($salary);  
            return format_salary_totals_result($thi_msg);
            break;
        
        case 'TOTAL_SALARY_COST':
            $thi_msg = get_TOTAL_SALARY_COST_value($salary);  
            return format_salary_totals_result($thi_msg);
            break;
        
        case 'OT_TOTAL':
            $thi_msg = get_OT_TOTAL_value($salary);  
            return format_salary_totals_result($thi_msg);
            break;
        
        case 'TAKEHOME_FULL':
            $thi_msg = get_TAKEHOME_FULL_value($salary);  
            return format_salary_totals_result($thi_msg);
            break;

        
        default:
            $all_salary_components_total = get_all_salary_components_total($salary);
            return format_salary_totals_result($all_salary_components_total);
            break;
    }
    
    
}

function get_SALARY_FOR_EPF_value(&$salary)
{
    $SALARY_TOTAL_1 = get_calculated_salary_component_value($salary, "BASIC_SALARY_TOT");
//    $ADDONS_TO_BASIC = get_calculated_salary_component_value($salary, "SALARY_ADVANCE"); 

    $SALARY_FOR_EPF = $SALARY_TOTAL_1;

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



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


function get_NET_SALARY_TOT_value(&$salary)
{
    $SALARY_TOTAL_1 = get_calculated_salary_component_value($salary, "GROSS_SALARY");
    $BASIC_SAL_DEDUCTIONS = get_calculated_salary_component_value($salary, "BASIC_SAL_DEDUCTIONS"); 
    $INCENTIVE_DEDUCTIONS = get_calculated_salary_component_value($salary, "INCENTIVE_DEDUCTIONS"); 
    
    $SALARY_TOTAL_3 = $SALARY_TOTAL_1 + ($BASIC_SAL_DEDUCTIONS + $INCENTIVE_DEDUCTIONS);
    
    return array('success' => true, 
                'calculated_value' => $SALARY_TOTAL_3, 
                'full_value' => $SALARY_TOTAL_3,
                'message' => '');
}

function get_GROSS_SALARY_value(&$salary)
{
    $BASIC_SALARY_TOT = get_calculated_salary_component_value($salary, "BASIC_SALARY_TOT");
    $INCENTIVE_TOTAL = get_calculated_salary_component_value($salary, "ATTENDANCE_INCENTIVE"); 
    
    $GROSS_SALARY_value = $BASIC_SALARY_TOT + $INCENTIVE_TOTAL;

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




function get_TAKEHOME_INCENTIVE_value($salary)
{
    $ATTENDANCE_INCENTIVE = get_calculated_salary_component_value($salary, "ATTENDANCE_INCENTIVE");
    $INCENTIVE_DEDUCTIONS = get_calculated_salary_component_value($salary, "INCENTIVE_DEDUCTIONS");
    

    $TAKEHOME_INCENTIVE = $ATTENDANCE_INCENTIVE + $INCENTIVE_DEDUCTIONS;
    if($TAKEHOME_INCENTIVE < 0){
        $TAKEHOME_INCENTIVE = 0;
    }
    

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


function get_TAKEHOME_BASIC_value(&$salary)
{
    $BASIC_SALARY_TOT = get_calculated_salary_component_value($salary, "BASIC_SALARY_TOT");
    $BASIC_SAL_DEDUCTIONS = get_calculated_salary_component_value($salary, "BASIC_SAL_DEDUCTIONS");
    
    $ATTENDANCE_INCENTIVE = get_calculated_salary_component_value($salary, "ATTENDANCE_INCENTIVE");
    $INCENTIVE_DEDUCTIONS = get_calculated_salary_component_value($salary, "INCENTIVE_DEDUCTIONS");
    
    $takehome_incentive = $ATTENDANCE_INCENTIVE + $INCENTIVE_DEDUCTIONS;
    if($takehome_incentive < 0){
        $takehome_incentive_deficit = $takehome_incentive;
    }else{
        $takehome_incentive_deficit = 0;
    }
    
    $TAKEHOME_BASIC = $BASIC_SALARY_TOT + $BASIC_SAL_DEDUCTIONS + $takehome_incentive_deficit;
    if($TAKEHOME_BASIC < 0){
        $TAKEHOME_BASIC = 0;
    }


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


function get_CARID_FRWRD_DEFICIT_value(&$salary)
{
    $BASIC_SALARY_TOT = get_calculated_salary_component_value($salary, "BASIC_SALARY_TOT");
    $BASIC_SAL_DEDUCTIONS = get_calculated_salary_component_value($salary, "BASIC_SAL_DEDUCTIONS");
    $INCENTIVE_TOTAL = get_calculated_salary_component_value($salary, "ATTENDANCE_INCENTIVE");
    $INCENTIVE_DEDUCTIONS = get_calculated_salary_component_value($salary, "INCENTIVE_DEDUCTIONS");
    
    $takehome_incentive = $INCENTIVE_TOTAL + $INCENTIVE_DEDUCTIONS;
    if($takehome_incentive < 0){
        $takehome_incentive_deficit = $takehome_incentive;
    }else{
        $takehome_incentive_deficit = 0;
    }
    
    $TAKEHOME_BASIC = $BASIC_SALARY_TOT + $BASIC_SAL_DEDUCTIONS + $takehome_incentive_deficit;
    if($TAKEHOME_BASIC < 0){
        $CARID_FRWRD_DEFICIT = $TAKEHOME_BASIC * -1;
    }else{
        $CARID_FRWRD_DEFICIT = 0;
    }
    
    return array('success' => true, 
                    'calculated_value' => $CARID_FRWRD_DEFICIT, 
                    'full_value' => $CARID_FRWRD_DEFICIT,
                    'message' => '');
}



function get_SALARY_FOR_OT_value(&$salary, $payroll_run, $employee, $temp_salary_component, $nopay_days)
{
    $SALARY_FOR_OT = get_all_salary_components_total($salary);
    $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_TOTAL_value(&$salary)
{
    $TAKEHOME_BASIC = get_calculated_salary_component_value($salary, "TAKEHOME_BASIC");
    $TAKEHOME_INCENTIVE = get_calculated_salary_component_value($salary, "TAKEHOME_INCENTIVE");
    
    $TAKEHOME_TOTAL = $TAKEHOME_BASIC + $TAKEHOME_INCENTIVE;

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


function get_TOTAL_SALARY_COST_value(&$salary)
{
    $GROSS_SALARY = get_calculated_salary_component_value($salary, "GROSS_SALARY");
    $BROGHT_FORWRD_DEDUCT = get_calculated_salary_component_value($salary, "BROGHT_FORWRD_DEDUCT");
    $CARID_FRWRD_DEFICIT = get_calculated_salary_component_value($salary, "CARID_FRWRD_DEFICIT");
    $EMP_EPF12 = get_calculated_salary_component_value($salary, "EMP_EPF12");
    
    $TOTAL_SALARY_COST = $GROSS_SALARY + $EMP_EPF12 + ($BROGHT_FORWRD_DEDUCT + $CARID_FRWRD_DEFICIT);

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


function get_OT_TOTAL_value(&$salary)
{
    $OVER_TIME = get_calculated_salary_component_value($salary, "OVER_TIME");
    $SINGLE_OT = get_calculated_salary_component_value($salary, "SINGLE_OT");
    $DOUBLE_OT = get_calculated_salary_component_value($salary, "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_TAKEHOME_FULL_value(&$salary)
{
    $TAKEHOME_BASIC = get_calculated_salary_component_value($salary, "TAKEHOME_BASIC");
    $TAKEHOME_INCENTIVE = get_calculated_salary_component_value($salary, "TAKEHOME_INCENTIVE");
    
    $MEAL_TOTAL = $TAKEHOME_BASIC + $TAKEHOME_INCENTIVE;

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



function get_salary_totals_array($salary, $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);
    $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($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;
}
?>
