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

function custom_approval_check_project($approver, $project_id, $document_name)
{
    $dep_approved_cost_budgets = get_project_tasks_cost_budgets_total($project_id);
    $dep_approved_cost_budgets_total = $dep_approved_cost_budgets['current_budgets_total'];
    $dep_approved_overhead_budgets = get_project_tasks_overhead_budgets_total($project_id);
    $dep_approved_overhead_budgets_total = $dep_approved_overhead_budgets['current_budgets_total'];
    
    $project = get_project_record($project_id);
    $project_value = $project['editing_project_value'];
    $required_project_value_based_overhead_percentage = $project['overhead_percentage'];
    
    $actual_project_value_based_overhead_percentage = 0;
    if($project_value > 0){ 
        $actual_project_value_based_overhead_percentage = $dep_approved_overhead_budgets_total / $project_value * 100;
    }
    
    if($actual_project_value_based_overhead_percentage + 0.00000001 > $required_project_value_based_overhead_percentage){
        // Project has equired minimum overhead. No need to check approver limits. 
        // Approve
        return array("success" => true, "message" => '');        
    }
    
    // Project does not have equired minimum overhead. Check approver limits. 
    $user_minimum_overhead_percentage = $approver["over_budget_limit"];
    $user_minimum_profit_percentage = $approver["over_budget_percentage"];
    
    // 5  + 0.00000001 > 5
    if($actual_project_value_based_overhead_percentage  + 0.00000001 >= $user_minimum_overhead_percentage){
        // User can approve. Do nothing here.
    }
    else{
        // Actual project value based overhead percentage is below user's limit. 
        // Can not approve. 

        $message = "Your minimum overhead approval percentage of ".number_format($user_minimum_overhead_percentage, 2)
                    ." is higher than the actual project value based overhead percentage of ".number_format($actual_project_value_based_overhead_percentage, 2)." (=".number_format($dep_approved_overhead_budgets_total,2)."/".number_format($project_value,2)."). "
                    .$document_name." will be sent to the next approval group.";
        return array("success" => false, "message" => $message);    
    }
    
    $total_project_cost = $dep_approved_cost_budgets_total + $dep_approved_overhead_budgets_total;
    $actual_profit_percentage = ($project_value - $total_project_cost) / $project_value;
    if($actual_profit_percentage + 0.00000001 >= $user_minimum_profit_percentage ){
        // User can approve. Do nothing here.
    }
    else{
        // Actual profit percentage is below user's limit. 
        // Can not approve. 

        $message = "Your minimum profit approval percentage of ".number_format($user_minimum_profit_percentage, 2)
                    ." is higher than the actual project profit percentage of ".number_format($actual_profit_percentage, 2).". "
                    .$document_name." will be sent to the next approval group.";
        return array("success" => false, "message" => $message);   
    }    
    
    return array("success" => true, "message" => '');     
}

?>