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

function get_approve_project_details($project_id)
{
	$sql = "SELECT ra.account_name revenue_account_name,
                        ca.account_name cost_account_name,
                        u.real_name, 
                        dep.value_name department_name,
                        dm.name customer_name,
                        pstatusn.value_name project_status_name, 
                        p.* 
                        
		FROM  ".TB_PREF."projects_master p                           

                        INNER JOIN ".TB_PREF."users u ON
                            p.project_manager_id = u.id

                        INNER JOIN ".TB_PREF."list_values yn ON
                            p.year = yn.value_code                            

                            
                        LEFT JOIN ".TB_PREF."chart_master ra ON
                            p.revenue_account = ra.account_code
                       
                        LEFT JOIN ".TB_PREF."chart_master ca ON
                            p.cost_account = ca.account_code
                            
                        LEFT JOIN ".TB_PREF."list_values dep ON
                            p.department_id = dep.id
           
                        LEFT JOIN ".TB_PREF."debtors_master dm ON
                            p.customer_id = dm.debtor_no 
                            
                        LEFT JOIN ".TB_PREF."list_values pstatusn ON
                            p.project_status_id = pstatusn.id

                            
		WHERE p.project_id = $project_id"; 
                              
                                  
	$result = db_query($sql,"Project could not be retreived");        
	return db_fetch($result);
}


function get_budget_total_cost($project_id)
{
    $sql = "SELECT SUM(budget) new_budgets_total 
            FROM ".TB_PREF."project_tasks
            WHERE project_id=".db_escape($project_id);
    
    $result = db_query($sql);
    $row =  db_fetch($result);
    return $row['new_budgets_total'];
    
}

function create_project_revision($project_id, $start_date, $end_date, $project_value, 
                                    $approved_cost_budgets_total, $approved_overhead_budgets_total, $revenue_account, $cost_account)
{
    $id_user = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');
        
    $sql = "INSERT INTO ".TB_PREF."project_revisions(
                    project_id, start_date, 
                    end_date, project_value, 
                    approved_cost_budgets_total, approved_overhead_budgets_total, 
                    revenue_account, cost_account, 
                    created_by, created_on) 
                    
            VALUES (".db_escape($project_id).", ".db_escape($start_date).", "
                        .db_escape($end_date).", ".db_escape($project_value).","
                        .db_escape($approved_cost_budgets_total).", ".db_escape($approved_overhead_budgets_total).","
                        .db_escape($revenue_account).", ".db_escape($cost_account).","
                        .db_escape($id_user).", ".db_escape($datetime).")";
    
    db_query($sql, "create_project_revision");   
}

function get_project_revisions_count($project_id)
{
    $sql = "SELECT COUNT(id) project_revisions_count 
            FROM ".TB_PREF."project_revisions 
            WHERE project_id =  ".db_escape($project_id);
    
    $result = db_query($sql, "get_project_revisions_count");
    
    return db_result_value($result, 'project_revisions_count');
    
}



?>
