<?php

function add_monthly_summary_calculation_run($month_id, $calculation_type, $group_code, $created_on)
{
    $created_by = $_SESSION['wa_current_user']->user;
    
    $status = "C"; // Use payroll calculation statuses
    
    $sql = "INSERT INTO ".TB_PREF."monthly_summary_calculation_runs
                (month_id, calculation_type, group_code,
                status, created_by, created_on) 
            VALUES( ".db_escape($month_id).",".db_escape($calculation_type).",".db_escape($group_code).","
                .db_escape($status).",".db_escape($created_by).",".db_escape($created_on).")";

    db_query($sql,"add_monthly_summary_calculation_run");
    
    return db_insert_id();
}

function get_monthly_summary_calculation_last_completed_value($calculation_id)
{
    $sql = "SELECT last_completed_value 
            FROM ".TB_PREF."monthly_summary_calculation_runs
            WHERE id = ".db_escape($calculation_id);

    $result = db_query($sql,"get_monthly_summary_calculation_last_completed_value");
    $row = db_fetch($result);
    if(isset($row['last_completed_value'])){
        return $row['last_completed_value'];
    }
    else{
        return '';
    }
}

function update_monthly_summary_calculation_calculated_value($calculation_id, $project_id)
{
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
	$sql = "UPDATE ".TB_PREF."monthly_summary_calculation_runs 
                SET
                    last_completed_value = ".db_escape($project_id).",
                    updated_by=".db_escape($updated_by).",
                    updated_on=".db_escape($updated_on)."
        	WHERE id = ".db_escape($calculation_id);

	db_query($sql,"update_monthly_summary_calculation_calculated_value");
}

function get_monthly_summary_calculation_run($calculation_id)
{
    $sql = "SELECT * 
            FROM ".TB_PREF."monthly_summary_calculation_runs
            WHERE id = ".db_escape($calculation_id);

    $result = db_query($sql,"get_monthly_summary_calculation_run");
    return db_fetch($result);
}


function get_summary_calculation_history($month_id, $calculation_type, $group_code)
{
    $sql = "SELECT cr.*,
                    m.description month_description,
                    u.real_name user_name 
            FROM ".TB_PREF."monthly_summary_calculation_runs cr 
                JOIN ".TB_PREF."months m 
                    ON cr.month_id = m.id 
                JOIN ".TB_PREF."users u 
                    ON cr.created_by = u.id  
            WHERE cr.month_id = ".db_escape($month_id)." 
                AND cr.calculation_type = ".db_escape($calculation_type)." 
                AND cr.group_code = ".db_escape($group_code)." 
            ORDER BY id DESC LIMIT 10 ";

    return db_query($sql,"get_summary_calculation_history");
    
//    return db_fetch($result);
}

function complete_monthly_summary_calculation_run($calculation_id, $duration_s, $comments='')
{
    
    $sql = "UPDATE ".TB_PREF."monthly_summary_calculation_runs 
            SET duration = duration + ".db_escape($duration_s).",
                comments = ".db_escape($comments)." 
            WHERE id = ".db_escape($calculation_id);

    db_query($sql,"complete_monthly_summary_calculation_run");
}

function get_last_completed_value($month_id, $group_code, $calculation_type)
{
    $sql = "SELECT last_completed_value 
            FROM ".TB_PREF."monthly_summary_calculation_runs  
            WHERE month_id = ".db_escape($month_id)."
                    AND group_code = ".db_escape($group_code)." 
                    AND calculation_type = ".db_escape($calculation_type)."
                    AND last_completed_value > 0 
            ORDER BY id DESC 
            LIMIT 1";
            
    $result = db_query($sql,"get_last_completed_value");
    
    return db_result_value($result, "last_completed_value");
}