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

function can_commit_project($project_id)
{
    if($project_id > 0){
        $project = get_project_record($project_id);
        if(!($project['approval_status'] == AS_NEW || $project['approval_status'] == AS_WITH_THE_APPROVER || 
                $project['approval_status'] == AS_AWAITING_APPROVAL || $project['approval_status'] == AS_BACK_TO_ORIGINATOR || 
                $project['approval_status'] == AS_REOPENED)){
            $message = ("This project status does not allow further updating. To reopen it, either update the project master or create a budget revision.");
            $set_focus = '';
            return array("success" => false, "focus_to" => $set_focus, "message" => $message);
        }
        
        $current_user_id = $_SESSION['wa_current_user']->user;
        $ced_msg = can_edit_document(ST_APPROVE_PROJECT, $project['project_id'], $project['next_approval_group_id'] , $current_user_id, $project['created_by'], $project_id);
        if($ced_msg['success'] !== true){
            return array("success" => false, "focus_to" => '', "message" => $ced_msg['message']);
        }
    } 
    
    return array("success" => true, "focus_to" => '', "message" => '');
    
}



function can_release_project_for_approval($project_id)
{
    $project = get_project_record($project_id);

    $dep_budget_totals = get_project_tasks_current_budgets_total($project_id);
    if($dep_budget_totals['current_budgets_total'] > $project['estimated_cost'] ){
        $message = ("Approved budget total is higher than the project estimated cost of ". number_format($project['estimated_cost'],2).". This project can not be released.");
        $set_focus = '';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
     
    return array("success" => true, "focus_to" => '', "message" => '');
    
}


function release_project_for_department_users($project_id)
{
    $cmp_msg = can_commit_project($project_id);
    if($cmp_msg['success'] !== true){
        return $cmp_msg;
    }
    
    $cap_msg = can_release_project_for_approval($project_id);
    if($cap_msg['success'] !== true){
        return $cap_msg;
    }
    
    $current_user_id = $_SESSION['wa_current_user']->user;
    
    begin_transaction();
    
    $project = get_project_record($project_id);
//    $project_value = $project['project_value'];    
    $row_state = $project['row_state'];
//    $next_approval_group_id = $project['next_approval_group_id'];
    $current_approval_group_id = 0;
    $originator_id = $project['created_by'];
    
    
    
//    $info = approve_document(ST_APPROVE_PROJECT, $project_id, $project_id, 
//                        $project_value, $next_approval_group_id, 
//                        $current_user_id, $row_state, $originator);
    
    $info = change_document_approval_position(ST_APPROVE_PROJECT, $project_id, $project_id, 
                        $current_approval_group_id, $current_user_id, $row_state,
                        $originator_id, "CHANGE_POSITION");

    if($info['success'] !== true){
        cancel_transaction();
        return array("success" => $info['success'], "message" => $info['message']);
    }
    else{
        commit_transaction();
        return array("success" => $info['success'], "message" => $info['message']);
    }      
        
}

function release_project_for_approval($project_id)
{
    $cmp_msg = can_commit_project($project_id);
    if($cmp_msg['success'] !== true){
        return $cmp_msg;
    }
    
    $cap_msg = can_release_project_for_approval($project_id);
    if($cap_msg['success'] !== true){
        return $cap_msg;
    }
    
    $current_user_id = $_SESSION['wa_current_user']->user;
    
    begin_transaction();
    
    $project = get_project_record($project_id);
    $project_new_value = $project['editing_project_value'];    
    $row_state = $project['row_state'];
    $next_approval_group_id = $project['next_approval_group_id'];
    $originator_id = $project['created_by'];
    
    
    
    $info = approve_document(ST_APPROVE_PROJECT, $project_id, $project_id, 
                        $project_new_value, $next_approval_group_id, 
                        $current_user_id, $row_state, $originator_id);
    

    if($info['success'] !== true){
        cancel_transaction();
        return array("success" => $info['success'], "message" => $info['message']);
    }

    if($info['approval_status'] != AS_APPROVED){
        commit_transaction();
        return array("success" => $info['success'], "message" => $info['message']);
    }
    
    $add_budgets = 0;
    $dep_approved_cost_budgets_total = get_project_tasks_cost_budgets_total($project_id);
    $dep_approved_overhead_budgets_total = get_project_tasks_overhead_budgets_total($project_id);
//        $approved_budgets_total = $current_budgets_total['current_budgets_total'];
    
    $dep_approved_overhead_current_budgets_total = $dep_approved_overhead_budgets_total['current_budgets_total'];
    if (! is_numeric($dep_approved_overhead_current_budgets_total)) {
        $dep_approved_overhead_current_budgets_total = 0;
    }
    $n = update_project_approved_values($project_id, $add_budgets, 
                                $dep_approved_cost_budgets_total['current_budgets_total'], 
                                $dep_approved_overhead_current_budgets_total,
                                $project_new_value);
    if($n !== 1){
        cancel_transaction();
        $message = display_error("Could not update project approved values.");
        return array("success" => false, "focus_to" => '', "message" => $message);
    }
    
    $approved_project = get_project_record($project_id);
    create_project_revision($project_id, $approved_project['start_date'], $approved_project['end_date'], $approved_project['project_value'], 
                            $approved_project['approved_cost_budgets_total'], $approved_project['approved_overhead_budgets_total'], $approved_project['revenue_account'], $approved_project['cost_account']);
    
    commit_transaction();
    
    $approval_message = $project['project_code']." - ".$project['description']." has been approved";
    return array("success" => true, "message" => $approval_message);
            
        
}

function send_project_back_to_originator($project_id)
{
    $cmp_msg = can_commit_project($project_id);
    if($cmp_msg['success'] !== true){
        return $cmp_msg;
    }
    
    
    begin_transaction();
    $current_user_id = $_SESSION['wa_current_user']->user;
    
    $project = get_project_record($project_id);   
    $row_state = $project['row_state'];
    $next_approval_group_id = $project['next_approval_group_id'];
    $originator = $project['created_by'];
    
    $sbdo_msg = send_back_to_document_originator(ST_APPROVE_PROJECT, $project_id, $project_id,
                                    $next_approval_group_id, $current_user_id, $row_state, 
                                    $originator);
    
    if($sbdo_msg['success'] !== true){        
        cancel_transaction();
        return $sbdo_msg;
    }
    else{
        commit_transaction();
        return $sbdo_msg;
    }

}

function reopen_project($project)
{
    $project_id = $project['project_id'];
    $row_state = $project['row_state'];
    $current_user_id = $_SESSION['wa_current_user']->user;

    if(!($project['approval_status'] == AS_NEW || $project['approval_status'] == AS_WITH_THE_APPROVER || 
            $project['approval_status'] == AS_AWAITING_APPROVAL || $project['approval_status'] == AS_BACK_TO_ORIGINATOR 
            || $project['approval_status'] == AS_REOPENED)){

        $sbdo_msg = reopen_approved_document(ST_APPROVE_PROJECT, $project_id, $project_id, 0, 
                $current_user_id, $row_state, $project['created_by']);
        $row_state = db_new_row_state($row_state);
    }
    else{
        $sbdo_msg = send_back_to_document_originator(ST_APPROVE_PROJECT, $project_id, $project_id, 
                            0, $current_user_id, $row_state, $project['created_by']);
    }
    
    return $sbdo_msg;
}
?>