<?php
/**********************************************************************
***********************************************************************/
function add_budget_task($task_id, $start_date, $end_date, $budget)
{
        $id_user = $_SESSION['wa_current_user']->user;
        $datetime = date('Y-m-d H:i:s');
        $budget_used = 0;
        $budget_allocated = 0;
        
        $sql = "INSERT INTO ".TB_PREF."task_budgets (
                        task_id, start_date, 
                        end_date, budget,
                        budget_used, budget_allocated,
                        created_by,created_on) 
                VALUES (".db_escape($task_id).", ".db_escape(date2sql($start_date)).", "
                        .db_escape(date2sql($end_date)).", ".db_escape($budget).", "
                        .db_escape($budget_used).", ".db_escape($budget_allocated).", "
                        .db_escape($id_user).", ".db_escape($datetime).")";

        db_query($sql,"Task budget could not be added");		
}

function update_budget_task($id, $start_date, $end_date, $budget)
{
        $id_user = $_SESSION['wa_current_user']->user;
        $datetime = date('Y-m-d H:i:s');
                
        $sql = "UPDATE ".TB_PREF."task_budgets 
                SET updated_by=".db_escape($id_user).",
                    updated_on=".db_escape($datetime).",
                    start_date=".db_escape(date2sql($start_date)).",
                    end_date=".db_escape(date2sql($end_date)).",
                    budget=".db_escape($budget)." 
                WHERE id = ".db_escape($id)." " ;

        db_query($sql,"Task budget could not be updated");		
}
//
function delete_budget_task($id)
{
	$sql="DELETE FROM ".TB_PREF."task_budgets WHERE id= ".db_escape($id);
	db_query($sql,"Task budget could not be deleted");			
}
//
function get_task_budget($task_id)
{
        $sql = "SELECT * FROM ".TB_PREF."task_budgets WHERE task_id=".db_escape($task_id). " 
               ORDER BY start_date";
	
	return db_query($sql,"Task budget could not be retreived");
}
//
function get_task_budget_details($id)
{
	$sql = "SELECT * FROM ".TB_PREF."task_budgets WHERE id=".db_escape($id);
	
	$result = db_query($sql,"Task budget detail could not be retreived");
	
	return db_fetch($result);
}

function get_overlap_task_budgets($task_id, $start_date, $end_date, $row_id)
{
    $start_date = date2sql($start_date);
    $end_date = date2sql($end_date);
    
    $sql = "SELECT 
                tb.*
            FROM 
                ".TB_PREF."task_budgets tb 
            WHERE 
                ((start_date >= ".db_escape($start_date)." AND start_date <= ".db_escape($end_date).") OR  
                (end_date >= ".db_escape($start_date)." AND end_date <= ".db_escape($end_date).")) AND   
                tb.task_id = ".db_escape($task_id)." ";
    
    if($row_id > 0){
        $sql .= " AND id <> ".$row_id;
    }
    $result = db_query($sql,"Task budget detail could not be retreived");
    return db_fetch($result);
}
?>