<?php //
/**********************************************************************
***********************************************************************/


function get_unapproved_project_tasks($project_id, $boq_group_id)
{
    $sql = "SELECT t.*
            FROM ".TB_PREF."project_tasks t
                JOIN ".TB_PREF."project_task_groups g ON 
                    t.task_group_id = g.id 
                
                JOIN ".TB_PREF."list_values dtn ON 
                    t.detail_type_code = dtn.value_code AND dtn.list_code = 'PROJECT_TASK_DETAIL_TYPES'

            WHERE t.project_id = $project_id AND g.boq_group_id = $boq_group_id AND t.budget <> t.new_budget";
    
    
    return db_query($sql, "get_unapproved_project_tasks");  
    
}


function get_approved_task_budgets_total($project_id)
{
    $sql = "SELECT SUM(t.budget) budgets_total 
            FROM ".TB_PREF."project_tasks t 

            WHERE t.project_id = $project_id AND t.budget = t.new_budget";
    
    
    $result = db_query($sql, "get_approved_task_budgets_total");  
    
    return db_result_value($result, 'budgets_total');
    
}

function get_unapproved_task_budgets_total($project_id)
{
    $sql = "SELECT SUM(t.new_budget) new_budgets_total 
            FROM ".TB_PREF."project_tasks t 

            WHERE t.project_id = $project_id AND t.budget <> t.new_budget";
    
    
    $result = db_query($sql, "get_unapproved_task_budgets_total");  
    
    return db_result_value($result, 'new_budgets_total');
    
}

function add_task_budget_revision($task_id, $old_budget, $budget_used, $budget_allocated, $new_budget, $approver_id)
{
    $datetime = date('Y-m-d H:i:s');
    
    $sql = "INSERT INTO ".TB_PREF."task_budget_revisions (
                    task_id, old_budget, budget_used, 
                    budget_allocated, new_budget, 
                    approved_by, approved_on)  
            VALUES (".db_escape($task_id).", ".db_escape($old_budget).", ". db_escape($budget_used).", "
                    .db_escape($budget_allocated).", ". db_escape($new_budget).", "
                    .db_escape($approver_id).", ". db_escape($datetime).")";
    
    db_query($sql,"add_task_budget_revision");
}

function get_budget_revisions_count($project_id)
{
    $sql = "SELECT COUNT(tbr.id) budget_revisions_count  
            FROM ".TB_PREF."task_budget_revisions tbr 
                JOIN ".TB_PREF."project_tasks pt  
                    ON pt.task_id = tbr.task_id 
            WHERE pt.project_id =  ".db_escape($project_id);
    
    $result = db_query($sql, "get_budget_revisions_count");
    
    return db_result_value($result, 'budget_revisions_count');
    
}


?>