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

function update_est_project($selected_id,$code,$description,
                            $long_description,$customer_id,$project_manager_name,$contact_person,$inactive, 
                            $start_date,$end_date,$project_value,$estimated_cost,$rate_include_taxes)
{			
        $id_user = $_SESSION['wa_current_user']->user;
        $datetime = date('Y-m-d H:i:s');
        
//        $approved_by = $_SESSION['wa_current_user']->user;
//        $approved_on = date('Y-m-d H:i:s');
        
        $sql = "UPDATE ".TB_PREF."est_projects_master SET 
                                long_description=".db_escape($long_description).",
                                updated_on=".db_escape($datetime).",
                                updated_by=".db_escape($id_user).",
                                inactive=".db_escape($inactive).",
                                project_value=".db_escape($project_value).",
                                estimated_cost=".db_escape($estimated_cost).",
                                rate_include_taxes=".db_escape($rate_include_taxes).",
                                code=".db_escape($code).",
                                description=".db_escape($description).",
                                start_date=".db_escape(date2sql($start_date)).",
                                start_date=".db_escape(date2sql($start_date)).",
                                end_date=".db_escape(date2sql($end_date)).", 
                                project_manager_name=".db_escape($project_manager_name).",
                                contact_person=".db_escape($contact_person).", 
                                customer_id=".db_escape($customer_id);      

        $sql .= " WHERE est_project_id=".db_escape($selected_id);

        db_query($sql, "The estimated project could not be updated");

}

function add_est_project($code,$description,$inactive,$project_name,$long_description,$department_id,$contact_person,$project_manager_name,$customer_id,
                         $project_start_date,$project_end_date,$project_value,$estimated_cost,$rate_include_taxes,$approval_project_id,$data_access_group_id)
{
        $id_user = $_SESSION['wa_current_user']->user;
        $datetime = date('Y-m-d H:i:s');
        
        $sql = "INSERT INTO ".TB_PREF."est_projects_master(description, long_description,inactive,
                                                            project_id,project_manager_name,
                                                            contact_person,customer_id, 
                                                            project_value,estimated_cost, rate_include_taxes,
                                                            code, project_name,created_on, created_by,
                                                            department_id, dag_id,start_date,end_date)
                         
                
                VALUES(".db_escape($description).", ".db_escape($long_description).","
                        .db_escape($inactive).",".db_escape($approval_project_id).","
                        .db_escape($project_manager_name).", ".db_escape($contact_person).", "
                        .db_escape($customer_id).",".db_escape($project_value).","
                        .db_escape($estimated_cost).",".db_escape($rate_include_taxes).","
                        .db_escape($code).",".db_escape($project_name).","
                        .db_escape($datetime).", ".db_escape($id_user).", "
                        .db_escape($department_id).",".db_escape($data_access_group_id).", "
                        .db_escape(date2sql($project_start_date)).",".db_escape(date2sql($project_end_date)).")"; 
                                               

        db_query($sql, "The project could not be added");

}

function get_est_project_details($project_id)
{
//	$sql = "SELECT ps.description company_category_name,
//                        psn.description project_category_name,
//                        pc.description customer_category_name,
//                        yn.value_name year_value,
//                        u.real_name,
//                        p.* 
//		FROM ".TB_PREF."est_projects_master p, "
//                        .TB_PREF."project_serial_numbers ps, "
//                        .TB_PREF."users u, "
//                        .TB_PREF."list_values yn, "
//                        .TB_PREF."project_serial_numbers pc, "
//                        .TB_PREF."project_serial_numbers psn
//		WHERE p.project_id = $project_id AND 
//                    
//                    p.project_manager_id = u.id AND
//                    p.customer_category_code = pc.category_code AND
//                    p.project_category_code = psn.category_code ";
    
	$sql = "SELECT
                    u.real_name,
                    p.* 
		FROM 0_est_projects_master p,  0_users u 
		WHERE p.est_project_id = ".db_escape($project_id)."  
                    ";
        
	$result = db_query($sql,"Project could not be retreived");

	return db_fetch($result);
}


function get_est_project($project_id)
{
	$sql = "SELECT p.*,
                    dn.value_name department_name,
                    dm.name customer_name
                    
        	FROM ".TB_PREF."est_projects_master p 
                INNER JOIN ".TB_PREF."list_values dn ON 
                        p.department_id = dn.id 
                LEFT JOIN ".TB_PREF."debtors_master dm ON 
                        p.customer_id = dm.debtor_no 
                
            WHERE p.est_project_id =".db_escape($project_id);
    
	$result = db_query($sql,"Could not get_est_project");

	return db_fetch($result);
}

function get_est_project_record($est_project_id)
{
	$sql = "SELECT ep.* 
                FROM ".TB_PREF."est_projects_master ep 
                WHERE ep.est_project_id =".db_escape($est_project_id);
    
	$result = db_query($sql,"Estimation Project record could not be retreived");

	return db_fetch($result);
}

function delete_est_project($id)
{
	$sql="DELETE FROM ".TB_PREF."est_projects_master WHERE project_id=".db_escape($id);

	db_query($sql,"Selected project could not be deleted");
}

function get_est_project_task($project_id, $task_id)
{
    $sql = "SELECT p.description as project_description,
                    apn.value_name task_description
                    
            FROM ".TB_PREF."est_projects_master p, "
                        .TB_PREF."project_tasks pt, "
                            .TB_PREF."list_values apn
                            
             WHERE  p.project_id = ".db_escape($project_id)." AND 
                    pt.task_id = " .db_escape($task_id)." AND
                    pt.type_id = apn.id ";
    
    $result = db_query($sql,"Project and task could not be retreived");
    
    return db_fetch($result);
}

function get_est_job_ref($project_id)
{
    $sql="SELECT description FROM ".TB_PREF."est_projects_master WHERE project_id=".db_escape($project_id);
    
    $result = db_query($sql,"Job referece not be retreived");
    
    return db_fetch($result);
}


function get_est_project_approval_path($project_id)
{
    $sql = " SELECT pm.approval_path_id, ap.approval_path_name "
            . "FROM ".TB_PREF."est_projects_master pm ,"
                    .TB_PREF."approval_path ap    
                WHERE pm.approval_path_id = ap.approval_path_id AND pm.project_id= ". db_escape($project_id);
                
   
    $result = db_query($sql,"Project approval path could not be retreived");
    return db_fetch($result);
}


function can_access_est_project($project_id, $user_id)
{
    $sql = "SELECT est_project_id  
            FROM ".TB_PREF."est_projects_master pm 
                    INNER JOIN ".TB_PREF."user_data_access_groups udag ON
                            pm.dag_id = udag.dag_id  
            WHERE pm.est_project_id = ".db_escape($project_id)." AND 
                    udag.user_id =".db_escape($user_id);
    
    $result = db_query($sql,"Could not check can_access_project");
    if(db_num_rows($result) > 0){
        return array("success" => true, "message" => "");
    }
    else{
        $project = get_est_project_record($project_id);
        return array("success" => false, "message" => "You do not have access to the project ".$project['description']);
    }
}


function update_est_project_approved_values($project_id, $cost_budgets_total, $overhead_budgets_total)
{			
        
    $sql = "UPDATE ".TB_PREF."est_projects_master 
            SET 
                approved_cost_components_total=".db_escape($cost_budgets_total).", 
                approved_overhead_components_total=".db_escape($overhead_budgets_total).",
                row_state = row_state + 1 
            WHERE approval_status = 30 AND est_project_id=".db_escape($project_id);

        db_query($sql, "The project could not be updated");
        
        return db_num_affected_rows();

}

function update_est_project_unapproved($project_id)
{			

    $sql = "UPDATE ".TB_PREF."est_projects_master SET 
                       
                approved = 0,   
                approved_by = 0,  
                approved_on = '', 
                add_budgets = 0 

            WHERE project_id=".db_escape($project_id);

        db_query($sql, "The project could not be updated");
        
        return db_num_affected_rows();

}

?>

























































































