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

function can_commit_est_project($project_id)
{
    if($project_id > 0){
        $project = get_est_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.");
            $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_EST_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_est_project_for_approval($project_id)
{
    $project = get_est_project_record($project_id);

    $total_cost = get_est_project_boq_items_cost($project_id);
    if($total_cost > $project['project_value'] ){
        $message = ("All the BOQ items total cost of ". number_format($total_cost,2)." is higher than the project value of  ". number_format($project['project_value'],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_est_project_for_approval($est_project_id)
{
    $cmp_msg = can_commit_est_project($est_project_id);
    if($cmp_msg['success'] !== true){
        return $cmp_msg;
    }
    
    $cap_msg = can_release_est_project_for_approval($est_project_id);
    if($cap_msg['success'] !== true){
        return $cap_msg;
    }
    
    $current_user_id = $_SESSION['wa_current_user']->user;
    
    begin_transaction();
    
    $est_project = get_est_project_record($est_project_id);
    $project_value = $est_project['project_value'];    
    $row_state = $est_project['row_state'];
    $next_approval_group_id = $est_project['next_approval_group_id'];
    $originator = $est_project['created_by'];
    
    
    
    $info = approve_document(ST_APPROVE_EST_PROJECT, $est_project_id, $est_project['project_id'], 
                        $project_value, $next_approval_group_id, 
                        $current_user_id, $row_state, $originator);

    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']);
    }
    

    $dep_approved_cost_components_total = get_est_project_boq_items_cost_components_total($est_project_id);
    $dep_approved_overhead_components_total = get_est_project_boq_items_overhead_components_total($est_project_id);
//        $approved_budgets_total = $current_budgets_total['current_budgets_total'];
    $n = update_est_project_approved_values($est_project_id, $dep_approved_cost_components_total, $dep_approved_overhead_components_total);
    if($n !== 1){
        cancel_transaction();
        $message = "Could not update project approved values.";
        return array("success" => false, "focus_to" => '', "message" => $message);
    }
    
    commit_transaction();
    
    $approval_message = $est_project['code']." - ".$est_project['description']." has been approved";
    return array("success" => true, "message" => $approval_message);
            
        
}


function send_est_project_back_to_originator($est_project_id)
{
    $cmp_msg = can_commit_est_project($est_project_id);
    if($cmp_msg['success'] !== true){
        return $cmp_msg;
    }
    
    
    begin_transaction();
    $current_user_id = $_SESSION['wa_current_user']->user;
    
    $est_project = get_est_project_record($est_project_id);   
    $row_state = $est_project['row_state'];
    $next_approval_group_id = $est_project['next_approval_group_id'];
    $originator = $est_project['created_by'];
    $project_id = $est_project['project_id'];
    
    $sbdo_msg = send_back_to_document_originator(ST_APPROVE_EST_PROJECT, $est_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_est_project($project)
{
    $est_project_id = $project['est_project_id'];
    $approval_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_EST_PROJECT, $est_project_id, $approval_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_EST_PROJECT, $est_project_id, $approval_project_id, 
                            0, $current_user_id, $row_state, $project['created_by']);
    }
    
    return $sbdo_msg;
}
