<?php

function get_ot_calculation_total($process_code, $employee_code, $payroll_run, $based_on_full_value = false)
{
    $ot_calculation_total = 0;
    $ot_amount = 0;
    $payroll_ot_hours_hms = "00:00:00";
    $comment = '';
    
    $result = select_all_approved_ot($employee_code, $payroll_run);
    while ($myrow = db_fetch($result)){
        $ot_calculation_result = get_ot_calculation_value($myrow['id'], $process_code, $employee_code, $payroll_run, $based_on_full_value);
        $ot_amount = $ot_calculation_result[0];
        $payroll_ot_hours_hms = $ot_calculation_result[1];

        $ot_calculation_total += $ot_amount;
        $comment .= sql2date($myrow["from_date_time1"])." - ".$payroll_ot_hours_hms." - ".$ot_amount."\r\n";
    }
    
    if(db_num_rows($result) > 0){
        insert_payroll_run_detail($payroll_run['id'], $employee_code, $process_code, $ot_calculation_total, $comment);
    }
    
    return array("calculated_value" => $ot_calculation_total, "full_value" => $ot_calculation_total);
    
}

function get_ot_calculation_value($id, $process_code, $employee_code, $payroll_run, $based_on_full_value = false)
{
    $ot_calculation_value = 0;
    $payroll_ot_time = "00:00:00";
    
    $myrow = select_ot_record($id);
    if($myrow == null){
        //$ot_calculation_value = 0;
    }
    else{
        $t1 = get_time_array($myrow["granted_ot_time"]);
        $ot_seconds = (int)$t1[0]*3600 + (int)$t1[1]*60 + (int)$t1[2];
        $ot_hours = $ot_seconds/3600;
        
        if($ot_hours > 0){
            $rate_table_result = get_rate_table_result($myrow['rate_table_id'], $employee_code, $ot_hours, $payroll_run['id'], $based_on_full_value);
            $ot_calculation_value = $rate_table_result[0];
            $payroll_ot_hours = $rate_table_result[1];
            
            $payroll_ot_hours_s = $payroll_ot_hours * 3600;
            $payroll_ot_time = gmdate("H:i:s", $payroll_ot_hours_s);
        }
        
        update_ot_record_status($myrow["id"], "L", $payroll_run['id'], $ot_calculation_value, $payroll_ot_time);
    }
    
    return array($ot_calculation_value, $payroll_ot_time);
}


function get_ot_payments_total($process_code, $employee_code, $payroll_run)
{
    $ot_payments_total = 0;
    $comment = '';
    
    $result = select_all_ot_payments($employee_code);
    while ($myrow = db_fetch($result)){
        $ot_payment_value = get_ot_payment_value($myrow['id'], $process_code, $employee_code, $payroll_run);
        $ot_payments_total += $ot_payment_value;
        $comment .= sql2date($myrow["from_date_time1"])." - ".substr($myrow["granted_ot_time"],-8,5)." - ".$myrow["ot_amount"]."\r\n";
    }
    
    if(db_num_rows($result) > 0){
        insert_payroll_run_detail($payroll_run['id'], $employee_code, $process_code, $ot_payments_total, $comment);
    }
    
    return array("calculated_value" => $ot_payments_total, "full_value" => $ot_payments_total);
    
}

function get_ot_payment_value($id, $process_code, $employee_code, $payroll_run)
{
    $ot_payment_value = 0;
    
    $myrow = select_ot_payment_record($id);
    if($myrow == null){
        $ot_payment_value = 0;
    }
    else{
        $ot_payment_value = $myrow['ot_amount'];
        update_ot_payment_record_status($myrow["id"], "S", $payroll_run['id']);
    }

    return $ot_payment_value;
}




?>
