<?php


function get_salary_advances_total($process_code, $employee_code, $payroll_run)
{
    $salary_advances_total = 0;
    $comment = '';
    
    $result = select_salary_advances($process_code, $employee_code, $payroll_run['period_end_date']);
    while ($myrow = db_fetch($result)){
        $salary_advance = get_salary_advance_value($myrow['id'], $process_code, $employee_code, $payroll_run);
        $salary_advances_total += $salary_advance;
        $comment .= sql2date($myrow["due_date"])." - ".$myrow["salary_advance"]."\r\n";
    }
    
    $salary_advances_total = -$salary_advances_total;
    
    if(db_num_rows($result) > 0){
        insert_payroll_run_detail($payroll_run['id'], $employee_code, $process_code, $salary_advances_total, $comment);
    }

    return array("calculated_value" => $salary_advances_total, "full_value" => $salary_advances_total);
    
}

function get_salary_advance_value($salary_advance_id, $process_code, $employee_code, $payroll_run)
{
    $salary_deduction = 0;
    
    $myrow = select_salary_advance_record($salary_advance_id);
    if($myrow == null){
        $salary_deduction = 0;
    }
    else{
        $salary_deduction = $myrow['salary_advance'];
    }
    
    update_salary_advance_record_status($salary_advance_id, $employee_code, "L", $payroll_run['id']);
    
    return $salary_deduction;
}



function get_salary_additions_total($process_code, $employee_code, $payroll_run)
{
    $salary_additions_total = 0;
    $comment = '';
    
    $result = select_salary_additions($process_code, $employee_code, $payroll_run['period_end_date']);
    while ($myrow = db_fetch($result)){
        $salary_addition = get_salary_addition($myrow['id'], $process_code, $employee_code, $payroll_run);
        $salary_additions_total += $salary_addition;
        $comment .= sql2date($myrow["due_date"])." - ".$myrow["salary_addition"]."\r\n";
    }
    
    if(db_num_rows($result) > 0){
        insert_payroll_run_detail($payroll_run['id'], $employee_code, $process_code, $salary_additions_total, $comment);
    }
    
    return array("calculated_value" => $salary_additions_total, "full_value" => $salary_additions_total);
    
}

function get_salary_addition($salary_addition_id, $process_code, $employee_code, $payroll_run)
{
    $salary_addition = 0;
    
    $myrow = select_salary_addition_record($salary_addition_id);
    if($myrow == null){
        $salary_addition = 0;
    }
    else{
        $salary_addition = $myrow['salary_addition'];
    }
    
    update_salary_addition_record_status($salary_addition_id, $employee_code, "L", $payroll_run['id']);
    
    return $salary_addition;
}


function get_salary_deductions_total($process_code, $employee_code, $payroll_run)
{
    $salary_deductions_total = 0;
    $comment = '';
    
    $result = select_salary_deductions($process_code, $employee_code, $payroll_run['period_end_date']);
    while ($myrow = db_fetch($result)){
        $salary_deduction = get_salary_deduction($myrow['id'], $process_code, $employee_code, $payroll_run);
        $salary_deductions_total += $salary_deduction;
        $comment .= sql2date($myrow["due_date"])." - ".$myrow["salary_deduction"]."\r\n";
    }
    
    $salary_deductions_total = -$salary_deductions_total;
    
    if(db_num_rows($result) > 0){
        insert_payroll_run_detail($payroll_run['id'], $employee_code, $process_code, $salary_deductions_total, $comment);
    }
    
    return array("calculated_value" => $salary_deductions_total, "full_value" => $salary_deductions_total);
    
}

function get_salary_deduction($salary_deduction_id, $process_code, $employee_code, $payroll_run)
{
    $salary_deduction = 0;
    
    $myrow = select_salary_deduction_record($salary_deduction_id);
    if($myrow == null){
        $salary_deduction = 0;
    }
    else{
        $salary_deduction = $myrow['salary_deduction'];
    }
    
    update_salary_deduction_record_status($salary_deduction_id, $employee_code, "L", $payroll_run['id']);
    
    return $salary_deduction;
}



?>
