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

function update_total_task_cost($task_id, $cost, $row_state)
{       
    	$sql = "UPDATE ".TB_PREF."project_tasks 
                SET budget_used = ".db_escape($cost).",
                    row_state = ". db_new_row_state($row_state)." 
                WHERE task_id = ".db_escape($task_id)." AND 
                    row_state = ".db_escape($row_state); 

    	db_query($sql, "could not update task total cost allocation");
        
        $num_recs = db_num_affected_rows();
        if($num_recs == 1){
            $msg['success'] = true;
            $msg['message'] = $num_recs;
            return $msg;
        }
        else{
            $msg['success'] = false;
            $msg['message'] = $num_recs." records updated in update_total_task_cost";
            return $msg;
        }
}


//--------------------------------------------------------------------------------------------------

function update_task_total_allocation($tran_type, $tran_no, 
                                        $task_id, $new_total_cost_allocation, 
                                        $current_user_id, $row_state)
{       
    	$sql = "UPDATE ".TB_PREF."project_tasks 
                SET budget_allocated = ".db_escape($new_total_cost_allocation).",  
                    row_state = ". db_new_row_state($row_state)." 
                WHERE task_id = ".db_escape($task_id)." AND 
                    row_state = $row_state"; 

    	db_query($sql, "could not update task total cost allocation");
        
        $num_recs = db_num_affected_rows();
        if($num_recs == 1){
            write_to_task_allocation_log($task_id, $tran_type, 
                        $tran_no, $new_total_cost_allocation,
                        $current_user_id);
            $msg['success'] = true;
            $msg['message'] = $num_recs;
            return $msg;
        }
        else{
            $msg['success'] = false;
            $msg['message'] = $num_recs." records updated in update_task_total_cost_allocation";
            return $msg;
        }

        
}


//--------------------------------------------------------------------------------------------------

function add_cost_detail_to_task($tran_type, $tran_no, 
                            $task_id, $amount,
                            $total_cost, $current_user_id)
{
        $added_on = date('Y-m-d H:i:s');
        
    	$sql = "INSERT INTO ".TB_PREF."task_costs(
                        task_id, tran_type, 
                        tran_no, amount, 
                        total_cost,
                        added_by, added_on)
    		VALUES (".db_escape($task_id).", ".db_escape($tran_type).", "
                        .db_escape($tran_no).", ".db_escape($amount).", "
                        .db_escape($total_cost).", "
                        .db_escape($current_user_id).", ".db_escape($added_on).")";
        
        $text = "Could not add cost to task";
    	db_query($sql, $text);
        
        $rec_id = db_insert_id();
        if($rec_id > 0){
            $msg['success'] = true;
            $msg['message'] = $rec_id;
            return $msg;
        }
        else{
            $msg['success'] = false;
            $msg['message'] = $text;
            return $msg;
        }

}


//--------------------------------------------------------------------------------------------------

function add_task_allocation_detail($task_id, $tran_type, $tran_no, 
                                    $amount, $total_allocation, $current_user_id)
{
        $updated_on = date('Y-m-d H:i:s');
        
    	$sql = "INSERT INTO ".TB_PREF."task_cost_allocations(
                        task_id, tran_type, 
                        tran_no, amount, 
                        total_allocation,
                        updated_by, updated_on)
    		VALUES (".db_escape($task_id).", ".db_escape($tran_type).", "
                        .db_escape($tran_no).", ".db_escape($amount).", "
                        .db_escape($total_allocation).", "
                        .db_escape($current_user_id).", ".db_escape($updated_on).")";
        
        $text = "could not add task cost allocation";
    	db_query($sql, $text);
        
        $rec_id = db_insert_id();
        if($rec_id > 0){
            write_to_task_allocation_log($task_id, $tran_type, 
                        $tran_no, $amount,
                        $current_user_id);
            $msg['success'] = true;
            $msg['message'] = $rec_id;
            return $msg;
        }
        else{
            $msg['success'] = false;
            $msg['message'] = $text;
            return $msg;
        }

}


//--------------------------------------------------------------------------------------------------

function update_task_allocation_detail($rec_id, $task_id, $tran_type, 
                        $tran_no, $amount, $total_allocation,
                        $current_user_id)
{
        $updated_on = date('Y-m-d H:i:s');
        
    	$sql = "UPDATE ".TB_PREF."task_cost_allocations
                SET amount = ".db_escape($amount).",  
                    total_allocation = ".db_escape($total_allocation).", 
                    updated_by = ".db_escape($current_user_id).",
                    updated_on = ".db_escape($updated_on)." 
                WHERE id = ".db_escape($rec_id); 

        $text = "could not update task cost allocation";
    	db_query($sql, $text);
        
        $num_recs = db_num_affected_rows();
        if($num_recs == 1){
            write_to_task_allocation_log($task_id, $tran_type, 
                        $tran_no, $amount,
                        $current_user_id);
            $msg['success'] = true;
            $msg['message'] = $num_recs;
            return $msg;
        }
        else{
            $msg['success'] = false;
            $msg['message'] = $text;
            return $msg;
        }

}

//--------------------------------------------------------------------------------------------------

function delete_task_allocation_detail($rec_id, $task_id, $tran_type, 
                        $tran_no, $allocated_amount,
                        $current_user_id)
{       
    	$sql = "DELETE FROM ".TB_PREF."task_cost_allocations 
                WHERE id = ".db_escape($rec_id); 

        $text = "could not delete task cost allocation";
    	db_query($sql, $text);
        
        $num_recs = db_num_affected_rows();
        if($num_recs == 1){
            $comment = "Deleted";
            write_to_task_allocation_log($task_id, $tran_type, 
                        $tran_no, $allocated_amount,
                        $current_user_id);
            $msg['success'] = true;
            $msg['message'] = $num_recs;
            return $msg;
        }
        else{
            $msg['success'] = false;
            $msg['message'] = $text;
            return $msg;
        }
}

//--------------------------------------------------------------------------------------------------

function get_task_allocation_record($tran_type, $tran_no, $task_id)
{
    $sql = "SELECT * 
            FROM ".TB_PREF."task_cost_allocations 
            WHERE tran_type = ".db_escape($tran_type)." AND 
                    tran_no = ".db_escape($tran_no)." AND 
                    task_id = ".db_escape($task_id);
    
    $result = db_query($sql,"Could not get task cost allocation record");
    
    return db_fetch($result);
}



//--------------------------------------------------------------------------------------------------

function write_to_task_allocation_log($task_id, $tran_type, 
                        $tran_no, $allocated_amount,
                        $current_user_id, $comment = "")
{
        $allocated_on = date('Y-m-d H:i:s');
        
    	$sql = "INSERT INTO ".TB_PREF."task_cost_allocation_log(
                        task_id, tran_type, 
                        tran_no, amount, 
                        allocated_by, allocated_on)
    		VALUES (".db_escape($task_id).", ".db_escape($tran_type).", "
                        .db_escape($tran_no).", ".db_escape($allocated_amount).", "
                        .db_escape($current_user_id).", ".db_escape($allocated_on).")";

    	db_query($sql, "could not write to task cost allocation log");

}

//--------------------------------------------------------------------------------------------------

//function get_task_budget_details($task_id)
//{
//    $sql = "SELECT budget, budget_allocated, budget_used 
//            FROM ".TB_PREF."project_tasks  
//            WHERE task_id = ".db_escape($task_id);
//
//    $result = db_query($sql, "Could not retrieve available budget");
//    
//    return db_fetch($result);
//}


//--------------------------------------------------------------------------------------------------

function get_task_budgets($task_id)
{
    $sql = "SELECT * FROM ".TB_PREF."task_budgets  
            WHERE task_id = ".db_escape($task_id);
    
    
    return db_query($sql, "could not retrieve task budgets");

}


//--------------------------------------------------------------------------------------------------

function update_task_budgets($row_id, $budget_used, $budget_allocated, $row_status)
{       
    	$sql = "UPDATE ".TB_PREF."task_budgets  
                SET budget_used = ".db_escape($budget_used).", 
                    budget_allocated = ".db_escape($budget_allocated).",  
                    row_status = row_status + 1  
                WHERE id = ".db_escape($row_id)."  AND row_status = ".db_escape($row_status);  

    	db_query($sql, "could not update task budget");
        
        $num_recs = db_num_affected_rows();
        if($num_recs == 1){
            $msg['success'] = true;
            $msg['message'] = $num_recs;
            return $msg;
        }
        else{
            $msg['success'] = false;
            $msg['message'] = $num_recs." records updated in update_task_total_cost_allocation";
            return $msg;
        }
}

//--------------------------------------------------------------------------------------------------


function get_current_budget_record($task_id, $date)
{
    $date = date2sql($date);
    
    $sql = "SELECT * 
            FROM ".TB_PREF."task_budgets 
            WHERE task_id = ".db_escape($task_id)." AND 
                    start_date <= ".db_escape($date)." AND 
                    end_date >= ".db_escape($date)." 
            ORDER BY start_date LIMIT 1";
    
    $result = db_query($sql, "could not retrieve current budget record");
    
    return db_fetch($result);
}

function get_redundent_cost_allocations($tran_type, $tran_no, $in_string)
{
        $sql = "SELECT * 
                FROM ".TB_PREF."task_cost_allocations 
                WHERE tran_type = ".db_escape($tran_type)." AND 
                    tran_no = ".db_escape($tran_no)." AND 
                    task_id NOT IN(".$in_string.") ";
        
    
    $result = db_query($sql, "could not retrieve current budget record");
    
}

function get_task_total_budget_allocated($task_id)
{
    $sql = "SELECT SUM(amount) total 
                FROM ".TB_PREF."task_cost_allocations 
                WHERE task_id = ".db_escape($task_id);
        
    
    $result = db_query($sql, "get_task_total_budget_allocated");
    
    return db_result_value($result, "total");
    
}

function get_task_total_budget_used($task_id)
{
    $sql = "SELECT SUM(amount) total 
                FROM ".TB_PREF."task_costs 
                WHERE task_id = ".db_escape($task_id);
        
    
    $result = db_query($sql, "get_task_total_budget_used");
    
    return db_result_value($result, "total");
    
}


?>