<?php

function get_rebuilt_project_task_cost_details($task_id = 0, $allocations = false)
{    
    
    $sql = "SELECT pbig.description boq_group_description, 
                    pt.description task_description,
                    tcr.id,
                    tran_type,
                    tran_no,
                    amount,
                    total_cost,
                    added_on
            FROM ".TB_PREF."task_costs_rebuild tcr 
                    JOIN ".TB_PREF."project_tasks pt 
                            ON tcr.task_id = pt.task_id
                    JOIN ".TB_PREF."project_task_groups ptg 
                            ON pt.task_group_id = ptg.id 
                    LEFT JOIN ".TB_PREF."project_boq_item_groups pbig 
                            ON ptg.boq_group_id = pbig.id AND pbig.project_id = pt.project_id 
            
            WHERE pt.task_id =".db_escape($task_id)." 
                    AND tcr.allocation =".db_escape($allocations)." 
            ORDER BY tcr.task_id, tcr.added_on, tcr.id";
    
    return db_query($sql);
    

}

function get_sql_for_rebuild_project_budget_costs_inquirys($task_id)
{
    $sql = "SELECT tcr.*,
                    pt.description budget_description        
            
            FROM ".TB_PREF."task_costs_rebuild tcr
                LEFT JOIN ".TB_PREF."project_tasks pt 
                ON tcr.task_id = pt.task_id                 
            WHERE  "; 
    
    if($task_id > 0){
        
        $sql .= " tcr.task_id=".db_escape($task_id);
    }
    
    
//    $sql .= " ORDER BY order_no ";
    return db_query($sql); //send as resource
     
   
}

function update_task_costs_from_rebuilt_values($task_id)
{
    delete_task_all_cost_details($task_id);
	
	$sql = "INSERT INTO ".TB_PREF."task_costs (
                        tran_type, tran_no, 
                        task_id, amount, total_cost, 
                        added_by, added_on)
                    
			SELECT tran_type, 
                                tran_no, 
                                task_id, 
                                amount, 
                                total_cost, 
                                added_by, 
                                added_on
			FROM ".TB_PREF."task_costs_rebuild
			WHERE task_id =  ".db_escape($task_id)." AND 
                                allocation = 0 
                        ORDER BY added_on, id   ";
        
        db_query($sql);
}

function update_task_allocations_from_rebuilt_values($task_id)
{
    delete_task_all_allocation_details($task_id);
	
	$sql = "INSERT INTO ".TB_PREF."task_cost_allocations (
                        tran_type, tran_no, 
                        task_id, amount, total_allocation, 
                        updated_by, updated_on)
                    
			SELECT tran_type, 
                                tran_no, 
                                task_id, 
                                amount, 
                                total_cost, 
                                added_by, 
                                added_on
			FROM ".TB_PREF."task_costs_rebuild
			WHERE task_id =  ".db_escape($task_id)." AND 
                                allocation = 1 
                        ORDER BY added_on, id   ";
        
        db_query($sql);
}

function delete_task_all_cost_details($task_id)
{
    $sql = "DELETE  FROM ".TB_PREF."task_costs   
            WHERE task_id = ".db_escape($task_id);

    db_query($sql);

}

function delete_task_all_allocation_details($task_id)
{
    $sql = "DELETE  FROM ".TB_PREF."task_cost_allocations    
            WHERE task_id = ".db_escape($task_id);

    db_query($sql);

}

function get_rebuilt_task_total($task_id, $allocations = 0)
{
    $sql = "SELECT SUM(amount) total_cost   
            FROM ".TB_PREF."task_costs_rebuild   
            WHERE  task_id=".db_escape($task_id);
    
    if($allocations){
        $sql .= " AND allocation = 1";   
    }
    else{
        $sql .= " AND allocation = 0";   
    }
    
    $result = db_query($sql); 
    
    $row = db_fetch($result);
    if(isset($row['total_cost']) && $row['total_cost'] > 0){
        return $row['total_cost'];
    }
    else{
        return 0;
    }
}


        
function update_task_cost_totals($task_id, $total_allocation, $total_cost, $row_state)
{
    $sql = "UPDATE ".TB_PREF."project_tasks   
                SET budget_allocated = ".db_escape($total_allocation).", 
                    budget_used = ".db_escape($total_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);
    return db_num_affected_rows();
}


function get_last_task_cost_recalculation_record($task_id)
{
    $sql = "SELECT *  
            FROM ".TB_PREF."project_task_cost_recalculations 
            WHERE task_id=".db_escape($task_id)." 
            ORDER BY end_time DESC LIMIT 1 ";
    
    $result = db_query($sql); 
    return db_fetch($result);
}
?>