<?php



function get_mobile_component_value($record_type, $process_code, $employee, $payroll_run, $nopay_days, $nopay_reference_days_factor = 30)
{ 
    $employee_code = $employee['employee_code'];
    $payroll_days_factor = 1;
    
    $my_row = select_salary_component($record_type, $process_code, $employee_code, $payroll_run);
    if($my_row == null){
        return 0;
    }
 
    //---------------------------
    $applicable_start_date = $applicable_end_date = '';
    $applicable_days = get_payroll_calculation_applicable_values($payroll_run, $employee, 
                                                $my_row, $nopay_days, 
                                                $applicable_start_date, $applicable_end_date, $payroll_days_factor,
                                                $nopay_reference_days_factor);
    if($applicable_days == 0){
        return 0;
    }
    //-----------------------------
 
    $salary_component_result = get_salary_component_calculation_result($employee_code, $payroll_run['id'], $my_row, $payroll_days_factor, "V");
    $salary_component_value = $salary_component_result[0];
    $comment = $salary_component_result[1];
    
    if($my_row['calculation_type'] == "V" && $my_row['full_amount']){
        $salary_component_value_full = $my_row['amount'];
    }
    else{
        $salary_component_value_full = $salary_component_value;
    }
    
    if($record_type == SCT_MOBILE_OVER_USAGE){    
        $mobile_bill_total = get_mobile_bill_total($employee_code, $my_row['process_code'], $payroll_run);
    
        $mobile_component_value = $mobile_bill_total['mobile_bill_total'] - $salary_component_value;
        $mobile_component_value_full = $mobile_bill_total['mobile_bill_total'] - $salary_component_value_full;
        
        if($mobile_component_value < 0){
            $mobile_component_value = 0;
        }
        if($mobile_component_value_full < 0){
            $mobile_component_value_full = 0;
        }
        
        $mobile_component_value = $mobile_component_value * -1;
        $mobile_component_value_full = $mobile_component_value_full * -1;
        
        $comment = $mobile_bill_total['comments'];
    }
    elseif($record_type == SCT_MOBILE_ALLOWANCES){
        $mobile_component_value = $salary_component_value;
        $mobile_component_value_full = $salary_component_value_full;
    }
    else{
        $mobile_component_value = 0;
        $mobile_component_value_full = 0;
    }
    
    
    if($my_row['calculation_type'] == "V" && $my_row['full_amount']){
        $salary_value = $mobile_component_value_full;
    }
    else{
        $salary_value = $mobile_component_value;
    }
    
    insert_payroll_run_detail($payroll_run['id'], $employee_code, $process_code, $salary_value, $comment);
    
    return array("calculated_value" => $mobile_component_value, "full_value" => $mobile_component_value_full);
}

function get_mobile_bill_total($employee_code, $component, $payroll_run)
{
    $mobile_bill_total = 0;
    $comment = '';
    
    $result = select_mobile_allowance_over_usage_records($component, $employee_code, $payroll_run['id']);
    while ($myrow = db_fetch($result)){
        $mobile_bill = get_mobile_bill($myrow['id'], $payroll_run);
        $mobile_bill_total += $mobile_bill;
        //$comment .= $myrow["month"]." - ".$myrow["mobile_bill"]."\r\n";
    }
    
    return array('mobile_bill_total' => $mobile_bill_total, 'comments' => $comment);
}

function get_mobile_bill($rec_id, $payroll_run)
{
    $mobile_bill = 0;
    
    $myrow = select_mobile_allowance_over_usage_record($rec_id);
    if($myrow == null){
        $mobile_bill = 0;
    }
    else{
        $mobile_bill = $myrow['mobile_bill'];
    }
    
    update_mobile_allowance_over_usage_record_status($rec_id, "L", $payroll_run['id']);
    
    return $mobile_bill;
}

?>
