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

//$path_to_root = "..";
global $path_to_root;
include_once($path_to_root . "/projects/includes/db/projects_db.inc");
include_once($path_to_root . "/projects/includes/db/tasks_db.inc");
include_once($path_to_root . "/includes/db/approval_functions_db.inc");
include_once($path_to_root . "/admin/db/list_values_db.inc");
include_once($path_to_root . "/projects/includes/db/approval_group_db.inc");
include_once($path_to_root . "/includes/custom_approval_functions.inc");

function approve_document($tran_type, $tran_no, $project_id, 
                        &$task_costs, $current_approval_group_id, $current_user_id, 
                        $document_row_state, $originator_id = 0, $different_document_total = 0)
{
        global $systypes_array;
    
        $document_name = $systypes_array[$tran_type]." #".$tran_no;
        //$use_future_budgets = true;
        if(is_array($task_costs)){
            $simple_approval = false;
            $document_total = get_document_reference_cost_total($task_costs);
        }
        else{
            $simple_approval = true;
            $document_total = $task_costs;
        }
        
        if($different_document_total > $document_total){
            $document_total = $different_document_total;
        }
        
        $info['project_id'] = $project_id;
        $info['tran_type'] = $tran_type;
        $info['tran_no'] = $tran_no;
        $info['document_total'] = $document_total;
        $info['current_approval_group_id'] = $current_approval_group_id;
        $info['current_user_id'] = $current_user_id;
        $info['over_budget_approval'] = false;
        $info['over_budget_tasks'] = "";
        $info['message'] = "";
        $info['row_state'] = $document_row_state;
        $info['change_type'] = "APPROVE";
        $info['display_status'] = "";
        $info['position_code'] = "";
        
        
        $current_approver_approval_group = '';

        $project = get_project_record($project_id);
        if(!$project){
                $info['success'] = false;
                $info['approval_status'] = AS_WITH_THE_APPROVER;
                $info['next_approval_group_id'] = $current_approval_group_id;
                $info['message'] = "Could not find details of the project ".$project['description'].". Please contact your system administrator";
                write_to_approval_log($info);
                return $info;
        }
        
        if($tran_type != ST_APPROVE_PROJECT){
            if($project['approval_status'] != AS_APPROVED){
                    $info['success'] = false;
                    $info['approval_status'] = AS_WITH_THE_APPROVER;
                    $info['next_approval_group_id'] = $current_approval_group_id;
                    $info['message'] = "Project ".$project['description']." is not approved. Can not make any transactions against it.";
                    write_to_approval_log($info);
                    return $info;
            }
        }
        
        $approval_path_type_code = get_approval_path_type($tran_type);
        if($approval_path_type_code == ""){
                $info['success'] = false;
                $info['approval_status'] = AS_WITH_THE_APPROVER;
                $info['next_approval_group_id'] = $current_approval_group_id;
                $info['message'] = "No approval path type defined for ".$systypes_array[$tran_type].". Please contact your system administrator";
                write_to_approval_log($info);
                return $info;
        }
        
        
        $approval_path = get_approval_path($project['project_id'], $approval_path_type_code);
        if(!$approval_path){
                $info['success'] = false;
                $info['approval_status'] = AS_WITH_THE_APPROVER;
                $info['next_approval_group_id'] = $current_approval_group_id;
                $approval_path_type = get_listvalue_by_code("PROJ_APP_PATH_TYPES", $approval_path_type_code);
                $info['message'] = "No approval path defined for ".$approval_path_type['value_name']." for the selected project ".$project['description'].". Please contact your system administrator";
                write_to_approval_log($info);
                return $info;
        }
            
        if($current_approval_group_id == 0){         
            //Originator has not released the document yet. But anyone in Originator's group can edit the document
            //Get the document originator's group as the current approver approval group
                $current_approver_approval_group = get_originator_approval_group($info, $approval_path['approval_path_id'], $originator_id);
                if(!$current_approver_approval_group){
                    $info['success'] = false;
                    $info['approval_status'] = AS_WITH_THE_APPROVER;
                    $info['next_approval_group_id'] = $current_approval_group_id;
                    $info['message'] = "Could not find an approval group of the $document_name originator ".get_new_position_user_name($originator_id). " 
                                        in the approval path ".get_approval_path_name($approval_path['approval_path_id'])." 
                                        of this project ".$project['description'].". Not possible to edit $document_name without an approval group.
                                        Please contact your system administrator.";
                    
                    write_to_approval_log($info);
                    return $info;
                }
                else{
                    $info['success'] = true;
                    $info['message'] = "";
                }
                
                $current_approval_group_id = $current_approver_approval_group['approval_group_id'];
        }
        else{
                $current_approver_approval_group = get_approval_group_in_approval_path($approval_path['approval_path_id'], $current_approval_group_id);
                if(!$current_approver_approval_group){
                        $info['success'] = false;
                        $info['approval_status'] = AS_WITH_THE_APPROVER;
                        $info['next_approval_group_id'] = $current_approval_group_id;
                        //$info['message'] = "Could not find your approval group in the approval path of this project task ".$project['description'].". Please contact your system administrator";
                        $info['message'] = "Could not find current approval group ".get_approval_group_name($current_approval_group_id)
                                                    ." in the approval path ".get_approval_path_name($approval_path['approval_path_id'])
                                                    ." of this project ".$project['description'].
                                                    ". Please contact your system administrator";
                        write_to_approval_log($info);
                        return $info;
                }
        }
        
        $approver = get_approver_in_approval_group($current_approver_approval_group['approval_group_id'], $current_user_id);
        if(!$approver){
                $info['success'] = false;
                $info['approval_status'] = AS_WITH_THE_APPROVER;
                $info['next_approval_group_id'] = $current_approval_group_id;
                $info['message'] = "You are not included in the current approval group [".$current_approver_approval_group['approval_group_description']."] of the $document_name.";
                write_to_approval_log($info);
                return $info;
        }
        
        if($approver['approval_limit'] < $document_total){
                // Approver does not have sufficient approval limit
                $info['approval_status'] = AS_AWAITING_APPROVAL;
                $info['next_approval_group_id'] = 0;
                $info['display_status'] = $current_approver_approval_group['display_status'];
                $info['position_code'] = $current_approver_approval_group['position_code'];
//                $info['position_code'] = $current_approver_approval_group['position_code'];
                if($current_approver_approval_group['next_position_message'] != ""){
                        $info['message'] = $current_approver_approval_group['next_position_message'];
                }
                else{
                        $info['message'] = "Your approval limit of ".$approver['approval_limit']." is  not sufficient to approve $document_name. Document will be sent to the next approval group.";
                }
                $next_approval_group = get_next_approval_group($approval_path['approval_path_id'], $current_approver_approval_group['position_seq_no']);
                if(!$next_approval_group){
                        $info['success'] = false;
                        $info['approval_status'] = AS_WITH_THE_APPROVER;
                        $info['next_approval_group_id'] = $current_approval_group_id;
                        $info['message'] = $info['message']." "."Could not find the next approval group after the sequence [".$current_approver_approval_group['position_seq_no']."]. Please contact your system administrator";
                        write_to_approval_log($info);
                        return $info;
                }
                $info['next_approval_group_id'] = $next_approval_group['approval_group_id'];
                $udas_msg = update_document_approval_status($info);
                if($udas_msg['success'] === true){
                        $info['success'] = $udas_msg['success'];
                }
                else{
                        $info['success'] = $udas_msg['success'];
                        $info['message'] = $udas_msg['message'];
                }
                return $info;
        }
        else{
            // approver has sufficient approval limit
            // Check for task budgets
            
            $can_approve_for_all_task_budgets = true;
            
            if(!$simple_approval){
                get_task_cost_details($task_costs);

                foreach ($task_costs as $document_task) {           
                    $available_budget = $document_task['budget'] - ($document_task['budget_used'] + $document_task['budget_allocated']);
                    if($available_budget + 0.0000001 < $document_task['charging_cost']){
                        // Available budget not sufficient
                        // Check for over budget approval
                        
                        $new_budget_perc = $document_task['budget'] * (1 + $approver['over_budget_percentage']/100);
                        $new_available_budget_perc = $new_budget_perc - ($document_task['budget_used'] + $document_task['budget_allocated']);

                        $new_budget_value = $document_task['budget'] + $approver['over_budget_limit'];
                        $new_available_budget_value = $new_budget_value - ($document_task['budget_used'] + $document_task['budget_allocated']);

                        $new_over_budget_available_budget = $new_available_budget_perc;
                        if($approver['over_budget_percentage'] > 0){
                            $over_budget_text = "(with additional percentage of ".$approver['over_budget_percentage'].")";
                        }else{
                            $over_budget_text = "";
                        }
                        
                        if($new_over_budget_available_budget > $new_available_budget_value){
                            $new_over_budget_available_budget = $new_available_budget_value;
                            if($approver['over_budget_limit'] > 0){
                                $over_budget_text = "(with additional value of".number_format($approver['over_budget_limit'],2).")";
                            }
                        }

//                        $over_budget_value = $document_task['charging_cost'] - $available_budget; 
//                        if($document_task['budget'] > 0){
//                            $over_budget_percentage = ($document_task['charging_cost'] - $available_budget) * 100 / $document_task['budget'];
//                        }
//                        else{
//                            $over_budget_percentage = 9999999999;
//                        }
//                        if($approver['over_budget_limit'] >= $over_budget_value && $approver['over_budget_percentage'] >= $over_budget_percentage){
                        if($new_over_budget_available_budget >= $document_task['charging_cost']){
                            // Approver has sufficient over budget approval limits
                            // Can approve
                            $info['over_budget_approval'] = false;
                            $info['over_budget_tasks'] .= ", ".$document_task['task_description'];
//                            display_db_error("new_over_budget_available_budget >= charging_cost", null, true);
//                            exit;
                            continue ;
                        }
                        else{
                            // No sufficient budget
                            // Approve does not have sufficient over budget approval limits
                            // Can not approve
                            $info['approval_status'] = AS_AWAITING_APPROVAL;
                            $info['next_approval_group_id'] = 0 ;
                            $info['display_status'] = $current_approver_approval_group['display_status'];
                            $info['position_code'] = $current_approver_approval_group['position_code'];
                            if($current_approver_approval_group['next_position_message'] != ""){
                                    $info['message'] = $current_approver_approval_group['next_position_message'];
                            }
                            else{
                                    $info['message'] = "Budget : ".$document_task['task_description'].". Available ".$over_budget_text." : ".number_format($new_over_budget_available_budget,2).". Required : ".number_format($document_task['remaining_cost'],2).". <br>Your over budget approval limits are not sufficient to approve $document_name.";
                            }

                            $next_approval_group = get_next_approval_group($approval_path['approval_path_id'], $current_approver_approval_group['position_seq_no']);
                            if(!$next_approval_group){
                                    $info['success'] = false;
                                    $info['approval_status'] = AS_WITH_THE_APPROVER;
                                    $info['next_approval_group_id'] = $current_approval_group_id;
                                    $info['message'] = $info['message']." "."Could not find the next approval group after the sequence [".$current_approver_approval_group['position_seq_no']."]. Please contact your system administrator";
                                    write_to_approval_log($info);
                                    $can_approve_for_all_task_budgets = false;
                                    return $info;
                            }

                            $info['next_approval_group_id'] = $next_approval_group['approval_group_id'];;
                            $udas_msg = update_document_approval_status($info);
                            if($udas_msg['success'] === true){
                                    $info['success'] = $udas_msg['success'];
                            }
                            else{
                                    $info['success'] = $udas_msg['success'];
                                    $info['message'] = $udas_msg['message'];
                            }
                            $can_approve_for_all_task_budgets = false;
                            return $info;
                        }
                    }
                    else{
                            // There is available budget
                            // can approve

                            continue;   
                    }
                }
            }
        }
        
        
        // Custom approval checks 
        if($info['tran_type'] == ST_APPROVE_PROJECT){
            $custom_approval_check_msg['success'] = false;
            $custom_approval_check_msg['message'] = "Unknown result from the custome approval check. Please contact your system administrator";
            
            if($info['tran_type'] == ST_APPROVE_PROJECT){
                $custom_approval_check_msg = custom_approval_check_project($approver, $project_id, $document_name);
            }
            
            $info['message'] = $custom_approval_check_msg['message'];
            $info['success'] = $custom_approval_check_msg['success'];
            
            if($custom_approval_check_msg['success'] === false){
                $next_approval_group = get_next_approval_group($approval_path['approval_path_id'], $current_approver_approval_group['position_seq_no']);
                if(!$next_approval_group){
                        $info['success'] = false;
                        $info['approval_status'] = AS_WITH_THE_APPROVER;
                        $info['next_approval_group_id'] = $current_approval_group_id;
                        $info['message'] = $info['message']." "."Could not find the next approval group after the sequence [".$current_approver_approval_group['position_seq_no']."]. Please contact your system administrator";
                        write_to_approval_log($info);
                        return $info;
                }
                
                $info['next_approval_group_id'] = $next_approval_group['approval_group_id'];
                $info['approval_status'] = AS_WITH_THE_APPROVER;
                $udas_msg = update_document_approval_status($info);
                if($udas_msg['success'] === true){
                        $info['success'] = $udas_msg['success'];
                }
                else{
                        $info['success'] = $udas_msg['success'];
                        $info['message'] = $udas_msg['message'];
                }
                return $info;
            }
                
        }
        
        
        if($can_approve_for_all_task_budgets == true){
        
            $info['approval_status'] = AS_APPROVED;
            $info['next_approval_group_id'] = 0;
            if($info['over_budget_approval'] == true){
                $info['message'] = $document_name." has been approved with over budget approvals for tasks ".substr(1,$info['over_budget_tasks']);
            }
            else{
                $info['message'] = $document_name." has been approved";
            }
                
            $info['display_status'] = $current_approver_approval_group['display_status'];
            $info['position_code'] = $current_approver_approval_group['position_code'];
            $udas_msg = update_document_approval_status($info);
            if($udas_msg['success'] === true){
                $info['success'] = $udas_msg['success'];
                $info['message'] = $udas_msg['message'];
            }
            else{
                $info['success'] = $udas_msg['success'];
                $info['message'] = $udas_msg['message'];
            }
        }
        
        return $info;
}

function get_document_reference_cost_total($task_costs)
{
    $document_total = 0;
    
    foreach ($task_costs as $task) {
        $document_total += $task['reference_cost'];
    }
    
    return $document_total;
}

function get_approval_path_type($tran_type)
{
        switch ($tran_type) {
            case ST_MATERIAL_REQ:
                $approval_path_type_code = "MR";
                break;
            case ST_TRANSFERREQ:
                $approval_path_type_code = "TR";
                break;
            case ST_PURCH_REQ:
                $approval_path_type_code = "PR";
                break;
            case ST_PURCHORDER:
                $approval_path_type_code = "PO";
                break;
            case ST_WORKORDER:
                $approval_path_type_code = "WO";
                break;
            case ST_I_OWE_YOU:
                $approval_path_type_code = "OU";
                break;
            case ST_I_OWE_YOU_BATCH:
                $approval_path_type_code = "EX";
                break;
            case ST_VEHICLE_REQUEST:
                $approval_path_type_code = "VR";
                break;
            case ST_PROJECT_INVOICE:
                $approval_path_type_code = "PI";
                break;
            case ST_PROJECT_CREDIT_NOTE:
                $approval_path_type_code = "PCN";
                break;
            case ST_PROJECT_CREDIT_NOTE:
                $approval_path_type_code = "PC";
                break;
            case ST_SUPPLIER_TRANSPORT_INVOICES:
                $approval_path_type_code = "STI";
                break;
            case ST_SUBCONTRACTOR_INVOICE:
                $approval_path_type_code = "SCI";
                break;
            case ST_UTILITY_BILLS:
                $approval_path_type_code = "UB";
                break;
            case ST_UTILITY_BILL_CREDIT_NOTE:
                $approval_path_type_code = "UBCN";
                break;
            case ST_SUBCONTRACTOR_PURCH_ORDER:
                $approval_path_type_code = "SCPO";
                break;
            case ST_SUBCONTRACTOR_CREDIT_NOTE:
                $approval_path_type_code = "SCCN";
                break;
            case ST_MISCELLANEOUS_PROJECT_COST:
                $approval_path_type_code = "MPC";
                break;
            case ST_TOOL_REQUEST:
                $approval_path_type_code = "TRQ";
                break;
            case ST_APPROVE_PROJECT:
                $approval_path_type_code = "AP";
                break;
            case ST_APPROVE_EST_PROJECT:
                $approval_path_type_code = "AEP";
                break;
            case ST_MONTHLY_PROJECT_COSTS:
                $approval_path_type_code = "NPC";
                break;
            case ST_INVENTORY_RECEIPT_REQUEST:
                $approval_path_type_code = "IAJ";
                break;
            case ST_INVENTORY_ISSUE_REQUEST:
                $approval_path_type_code = "IAJ";
                break;
            case ST_ASSET_TRANSFER_REQUEST:
                $approval_path_type_code = "ATR";
                break;
            case ST_ASSET_TRANSFER:
                $approval_path_type_code = "AT";
                break;
            case ST_ASSET_QUANTITY_ADJUSTMENT_REQUEST:
                $approval_path_type_code = "AQAR";
                break;
            case ST_ASSET_QUANTITY_ADJUSTMENT:
                $approval_path_type_code = "AQA";
                break;
            case ST_JOB_COST_ADDITION:
                $approval_path_type_code = "JCA";
                break;
            case ST_JOB_COST_DEDUCTION:
                $approval_path_type_code = "JCD";
                break;
            case ST_JOB_MATERIAL_REQ:
                $approval_path_type_code = "JMR";
                break;
            case ST_JOB_MATERIAL_ISSUE_REQ:
                $approval_path_type_code = "JMIR";
                break;
            case ST_JOB_MATERIAL_ISSUE:
                $approval_path_type_code = "JMI";
                break;
            case ST_JOB_MATERIAL_RETURN:
                $approval_path_type_code = "JMRT";
                break;
            case ST_PERSONAL_MATERIAL_REQ:
                $approval_path_type_code = "PMRQ";
                break;
            case ST_PERSONAL_MATERIAL_TRANSFER:
                $approval_path_type_code = "PMTR";
                break;
            case ST_PERSONAL_MATERIAL_TRANSFER_RETURN:
                $approval_path_type_code = "PMTRRT";
                break;
            case ST_PERSONAL_MATERIAL_ISSUE:
                $approval_path_type_code = "PMI";
                break;
            case ST_PERSONAL_MATERIAL_RETURN:
                $approval_path_type_code = "PMR";
                break;
            case ST_OUTSIDE_MATERIAL_REQ:
                $approval_path_type_code = "OMR";
                break;
            case ST_OUTSIDE_MATERIAL_RECEIPT:
                $approval_path_type_code = "OMRC";
                break;
            case ST_OUTSIDE_MATERIAL_RETURN:
                $approval_path_type_code = "OMRT";
                break;
            case ST_DIRECT_PURCHASE_REQ:
                $approval_path_type_code = "DPR";
                break;
            case ST_QUALITY_CHECK:
                $approval_path_type_code = "QC";
                break;
            case ST_DIRECT_PURCHASE_RECEIPT:
                $approval_path_type_code = "DPRCP";
                break;
            case ST_DIRECT_PURCHASE_RETURN:
                $approval_path_type_code = "DPRET";
                break;
            case ST_MATERIAL_LIST:
                $approval_path_type_code = "ML";
                break;
            case ST_BUDGET_TO_BUDGET_TRANSFER:
                $approval_path_type_code = "BBT";
                break;
            case ST_SALVAGE_RETURN:
                $approval_path_type_code = "SLRT";
                break;
            case ST_JOB_MANUAL_COST:
                $approval_path_type_code = "JMC";
                break;
            case ST_JOB_MANUAL_COST_RETURN:
                $approval_path_type_code = "JMCR";
                break;
            
            
            default:
                $approval_path_type_code = "";
                break;
        }
        
        return $approval_path_type_code;
}

function reject_document($tran_type, $tran_no, $project_id, 
                        $current_approval_group_id, $current_user_id, $document_row_state)
{        
    $cdap_msg = change_document_approval_position($tran_type, $tran_no, $project_id, 
                        $current_approval_group_id, $current_user_id, $document_row_state,
                        $current_user_id, "REJECT");
    
    return $cdap_msg;
}

function send_back_to_document_originator($tran_type, $tran_no, $project_id, 
                        $current_approval_group_id, $current_user_id, $document_row_state,
                        $originator_id)
{
    $cdap_msg = change_document_approval_position($tran_type, $tran_no, $project_id, 
                        $current_approval_group_id, $current_user_id, $document_row_state,
                        $originator_id, "BACK_TO_ORIGINATOR");
    
    return $cdap_msg;
}

function reopen_approved_document($tran_type, $tran_no, $project_id, 
                        $current_approval_group_id, $current_user_id, $document_row_state,
                        $originator_id)
{
    $cdap_msg = change_document_approval_position($tran_type, $tran_no, $project_id, 
                        $current_approval_group_id, $current_user_id, $document_row_state,
                        $originator_id, "REOPEN");
    
    return $cdap_msg;
}


function change_document_approval_position($tran_type, $tran_no, $project_id, 
                        $current_approval_group_id, $current_user_id, $document_row_state,
                        $new_position_user_id, $change_type)
{
    global $systypes_array;
    
    $document_name = $systypes_array[$tran_type]." #".$tran_no;
    
    switch ($change_type) {
        case "REJECT": // Only by the current approval group approvers
            $approval_status = AS_REJECTED;
            break;
        case "BACK_TO_ORIGINATOR": //Only by the current approval group approvers
            switch ($tran_type) {
                case ST_INVENTORY_RECEIPT_REQUEST:
                case ST_INVENTORY_ISSUE_REQUEST:
                case ST_APPROVE_PROJECT:
                case ST_MONTHLY_PROJECT_COSTS:
                case ST_I_OWE_YOU_BATCH:
                    $approval_status = AS_BACK_TO_ORIGINATOR;
                    break;
                default:
                    $approval_status = AS_AWAITING_APPROVAL;
                    break;
            }
            break;
        case "REOPEN": //By anyone who has access to approved document reopen screen
            $approval_status = AS_REOPENED;
            break;
        case "CHANGE_POSITION": //By anyone who has access to document position change screen
        default:
            $approval_status = AS_AWAITING_APPROVAL;
            break;
    }
        $info['project_id'] = $project_id;
        $info['tran_type'] = $tran_type;
        $info['tran_no'] = $tran_no;
        $info['current_user_id'] = $current_user_id;
        $info['document_total'] = 0;
        $info['current_approval_group_id'] = $current_approval_group_id;
        $info['display_status'] = "";
        $info['position_code'] = "";
        
        $project = get_project_record($project_id);
        
        $approval_path_type_code = get_approval_path_type($tran_type);
        if($approval_path_type_code == ""){
                $info['success'] = false;
                $info['approval_status'] = AS_WITH_THE_APPROVER;
                $info['next_approval_group_id'] = $current_approval_group_id;
                $info['message'] = "No approval path type defined for ".$systypes_array[$tran_type].". Please contact your system administrator";
                write_to_approval_log($info);
                return $info;
        }
        
        $approval_path = get_approval_path($project_id, $approval_path_type_code);
        if(!$approval_path){
                $info['success'] = false;
                $info['approval_status'] = AS_WITH_THE_APPROVER;
                $info['next_approval_group_id'] = $current_approval_group_id;
                $approval_path_type = get_listvalue_by_code("PROJ_APP_PATH_TYPES", $approval_path_type_code);
                $info['message'] = "No approval path defined for ".$approval_path_type['value_name']." for the selected project ".$project['description'].". Please contact your system administrator";
                write_to_approval_log($info);
                return $info;
        }
        
        if($change_type == "REJECT" || $change_type == "BACK_TO_ORIGINATOR"){
            /*=======================================================================================================================================
            * Allow anyone in the approval path to perform this. Change this to check whether the approver is in any position of the approval path
            * =======================================================================================================================================
            */
                if($current_approval_group_id == 0){         
                        $current_approver_approval_group = get_user_approval_group($approval_path['approval_path_id'], $current_user_id);
                        if(!$current_approver_approval_group){
                            $info['success'] = false;
                            $info['approval_status'] = AS_WITH_THE_APPROVER;
                            $info['next_approval_group_id'] = $current_approval_group_id;
                            //$info['message'] = "Could not find your approval group for the approval path of this project ".$project['description'].". Please contact your system administrator";
                            $info['message'] = "Could not find current approval group ".get_approval_group_name($current_approval_group_id)
                                                    ." in the approval path ".get_approval_path_name($approval_path['approval_path_id'])
                                                    ." of this project ".$project['description'].
                                                    ". Please contact your system administrator";
                            write_to_approval_log($info);
                            return $info;
                        }
                        $current_approval_group_id = $current_approver_approval_group['approval_group_id'];
                }
                else{
                        $current_approver_approval_group = get_approval_group_in_approval_path($approval_path['approval_path_id'], $current_approval_group_id);
                        if(!$current_approver_approval_group){
                                $info['success'] = false;
                                $info['approval_status'] = AS_WITH_THE_APPROVER;
                                $info['next_approval_group_id'] = $current_approval_group_id;
                                $info['message'] = "Could not find current approval group ".get_approval_group_name($current_approval_group_id)
                                                    ." in the approval path ".get_approval_path_name($approval_path['approval_path_id'])
                                                    ." of this project ".$project['description'].
                                                    ". Please contact your system administrator";
                                write_to_approval_log($info);
                                return $info;
                        }
                }
                
                $approver = get_approver_in_approval_group($current_approver_approval_group['approval_group_id'], $current_user_id);
                if(!$approver){
                        $info['approval_status'] = AS_WITH_THE_APPROVER;
                        $info['next_approval_group_id'] = $current_approval_group_id;
                        $info['message'] = "You are not included in the current approval group [".$current_approver_approval_group['approval_group_description']."] of the $document_name.";
                        write_to_approval_log($info);
                        return $info;
                }
        }
        
        //if($change_type != "REJECT"){
        $originator_approval_group = get_user_approval_group($approval_path['approval_path_id'], $new_position_user_id);
        if(!$originator_approval_group){
                $info['success'] = false;
                $info['approval_status'] = AS_WITH_THE_APPROVER;
                $info['next_approval_group_id'] = $current_approval_group_id;

                if($change_type == "BACK_TO_ORIGINATOR" || $change_type == "REOPEN"){
                    $info['message'] = "Could not find an approval group of $document_name originator ".get_new_position_user_name($new_position_user_id)
                            . " in the approval path ".get_approval_path_name($approval_path['approval_path_id'])
                            . " of this project ".$project['description'].
                            ". Please contact your system administrator";
                }
                else{
                    $info['message'] = "Could not find new user approval group in the approval path of this project ".$project['description'].". Please contact your system administrator";
                }
                write_to_approval_log($info);
                return $info;
        }
        //}
        
        $info['next_approval_group_id'] = $originator_approval_group['approval_group_id'];
        $info['current_user_id'] = $current_user_id;
        $user = get_user($new_position_user_id);
        
        switch ($change_type) {
        case "REJECT": // Only by the current approval group approvers
            $info['message'] = "$document_name has been rejected";
            break;
        case "BACK_TO_ORIGINATOR": //Only by the current approval group approvers
            $info['message'] = "$document_name has been sent back to approval group of its originator ".$user['real_name'];
            break;
        case "REOPEN": //By anyone who has access to approved document reopen screen
            $info['message'] = "$document_name has been reopened and sent back to approval group of its originator ".$user['real_name'];
            break;
        case "CHANGE_POSITION": //By anyone who has access to document position change screen
        default:
            $info['message'] = "$document_name has been sent to approval group of the user ".$user['real_name'];
            break;
        }

        $info['row_state'] = $document_row_state;
        $info['approval_status'] = $approval_status;
        $info['change_type'] = $change_type;
                
        begin_transaction();
        $udas_msg = update_document_approval_status($info);
        if($udas_msg['success'] === true){
            commit_transaction();
            $info['success'] = $udas_msg['success'];      
        }
        else{
            cancel_transaction();
            $info['success'] = $udas_msg['success'];
            $info['message'] = $udas_msg['message'];
        }
        
        return array("success" => $info['success'], "tran_no" => $info['tran_no'], "message" => $info['message']);
}

function can_edit_document($tran_type, $project_id, 
                        $current_approval_group_id, $current_user_id,
                        $originator_id, $tran_no = 0)
{
    global $systypes_array;
    
    if($tran_no>0){
        $document_name = $systypes_array[$tran_type]." #".$tran_no;
    }
    else{
        $document_name = $systypes_array[$tran_type];
    }
    
        $project = get_project_record($project_id);
        
        $approval_path_type_code = get_approval_path_type($tran_type);
        if($approval_path_type_code == ""){
                $info['success'] = false;
                $info['message'] = "No approval path type defined for ".$systypes_array[$tran_type].". Please contact your system administrator";
                return $info;
        }
        
        $approval_path = get_approval_path($project_id, $approval_path_type_code);
        if(!$approval_path){
                $info['success'] = false;
                $approval_path_type = get_listvalue_by_code("PROJ_APP_PATH_TYPES", $approval_path_type_code);
                $info['message'] = "No approval path defined for ".$approval_path_type['value_name']." for the selected project ".$project['description'].". Please contact your system administrator";
                return $info;
        }
        
        if($current_approval_group_id == 0){     
            //Originator has not released the document yet. But anyone in Originator's group can edit the document
            //Get the document originator's group as the current approver approval group
            
            $info['tran_type'] = $tran_type;
            $info['tran_no'] = $tran_no;
            
            $current_approver_approval_group = get_originator_approval_group($info, $approval_path['approval_path_id'], $originator_id);
            if(!$current_approver_approval_group){
                $info['success'] = false;
                $info['message'] = "Could not find an approval group of the $document_name originator ".get_new_position_user_name($originator_id). " 
                                        in the approval path ".get_approval_path_name($approval_path['approval_path_id'])." 
                                        of this project ".$project['description'].". Please contact your system administrator";
                return $info;
            }
            $current_approval_group_id = $current_approver_approval_group['approval_group_id'];
            
        }
        else{
            $current_approver_approval_group = get_approval_group_in_approval_path($approval_path['approval_path_id'], $current_approval_group_id);
            if(!$current_approver_approval_group){
                $info['success'] = false;
                $info['message'] = "Could not find current approver approval group  ".get_approval_group_name($current_approval_group_id)." 
                                        in the approval path ".get_approval_path_name($approval_path['approval_path_id'])." 
                                        of this project ".$project['description'].". Please contact your system administrator";
                return $info;
            }
        }
            
        $current_user_approval_group = get_approver_in_approval_group($current_approval_group_id, $current_user_id);
        if(!$current_user_approval_group){
            $info['success'] = false;
            //$info['message'] = "Could not find your approval group for the approval path of this project ".$project['description'].". Please contact your system administrator";
            $info['message'] = "You are not included in the current approval group ".get_approval_group_name($current_approval_group_id)." of the $document_name  
                                    in the approval path ".get_approval_path_name($approval_path['approval_path_id'])." 
                                    of this project ".$project['description'].". You are not allowed to edit this $document_name.";
            return $info;
        }
        else{
            $info['success'] = true;
            $info['message'] = "";
        }
        
        return $info;
}


function direct_approve_sales_documents($cart, $current_user_id, $check_only = false)
{
    $sales_order_approval_group_id = get_company_pref('sales_order_appr_group_id');
    if($sales_order_approval_group_id == null || $sales_order_approval_group_id == '' || $sales_order_approval_group_id == 0){
        return array("success" => false, "message" => "Sales order approval group is not set.");
    }
    
    calculate_sales_order_losses($cart, $value_loss, $perc_loss);
    
    $info['project_id'] = $cart->project_id;
    $info['tran_type'] = $cart->trans_type;
    $info['tran_no'] = $cart->order_no;
    $info['document_total'] = $cart->get_trans_total();
    $info['current_approval_group_id'] = $sales_order_approval_group_id;
    $info['current_user_id'] = $current_user_id;
    $info['message'] = "";
    $info['row_state'] = $cart->row_state;
    $info['change_type'] = "APPROVE";
    
    $info['value_loss'] = $value_loss;
    $info['perc_loss'] = $perc_loss;

    return direct_approve_document($info, $current_user_id, $check_only);
            
}


function direct_reject_sales_documents($cart, $current_user_id, $check_only = false)
{
    $sales_order_approval_group_id = get_company_pref('sales_order_appr_group_id');
    if($sales_order_approval_group_id == null || $sales_order_approval_group_id == '' || $sales_order_approval_group_id == 0){
        return array("success" => false, "message" => "Sales order approval group is not set.");
    }
    
    $value_loss = $perc_loss = 0;    
    
    $info['project_id'] = $cart->project_id;
    $info['tran_type'] = $cart->trans_type;
    $info['tran_no'] = $cart->order_no;
    $info['document_total'] = $cart->get_trans_total();
    $info['current_approval_group_id'] = $sales_order_approval_group_id;
    $info['current_user_id'] = $current_user_id;
    $info['message'] = "";
    $info['row_state'] = $cart->row_state;
    $info['change_type'] = "REJECT";
    
    $info['value_loss'] = $value_loss;
    $info['perc_loss'] = $perc_loss;

    return direct_approve_document($info, $current_user_id, $check_only);
            
}

function direct_approve_work_order($wo, $current_user_id, $check_only = false)
{

    $work_order_approval_group_id = get_company_pref('work_order_appr_group_id');
    if($work_order_approval_group_id == null || $work_order_approval_group_id == '' || $work_order_approval_group_id == 0){
        return array("success" => false, "message" => "Workorder approval group is not set.");
    }
    
    $value_loss = 0;
    $perc_loss = 0;
    
    $info['project_id'] = $wo['wo_project_id'];
    $info['tran_type'] = ST_WORKORDER;
    $info['tran_no'] = $wo['wo_id'];
    $info['document_total'] = $wo['wo_total'];
    $info['current_approval_group_id'] = $work_order_approval_group_id;
    $info['current_user_id'] = $current_user_id;
    $info['message'] = "";
    $info['row_state'] = $wo['wo_row_state'];
    $info['change_type'] = "APPROVE";
    
    $info['value_loss'] = $value_loss;
    $info['perc_loss'] = $perc_loss;
    
    return direct_approve_document($info, $current_user_id, $check_only);

}

/* No approval path. action is performed by an Approval Group only*/

function direct_approve_document($info, $current_user_id, $check_only = false)
{            
    global $systypes_array;
    
               
    
    $current_approval_group_id = $info['current_approval_group_id'];
    $value_loss = $info['value_loss'];
    $perc_loss = $info['perc_loss'];
    $type = $info['tran_type'];
    
    $info['display_status'] = "";
    $info['position_code'] = "";
    
    switch ($info['change_type']) {
        case "REJECT":
            $change_type_name = "reject";
            break;
        case "REOPEN":
            $change_type_name = "reopen";
            break;
        default:
            $change_type_name = "approve";
            break;
    }
    
    $document_name = $systypes_array[$type]." #".$info['tran_no'];
    
    $project = get_project_record($info['project_id']);
    if($project['approval_status'] != AS_APPROVED){
            $info['success'] = false;
            $info['approval_status'] = AS_WITH_THE_APPROVER;
            $info['next_approval_group_id'] = $current_approval_group_id;
            $info['message'] = "Project ".$project['description']." is not approved. Can not make any transactions against it.";
            if(!$check_only){
                write_to_approval_log($info);
            }
            return $info;
    }

    
    if(!$current_approval_group_id > 0){
            $info['success'] = false;
            $info['approval_status'] = AS_WITH_THE_APPROVER;
            $info['next_approval_group_id'] = $current_approval_group_id;
            $info['message'] = "No approval group defined for ".$systypes_array[$type]." Approval. Please contact system administrator";
            if(!$check_only) write_to_approval_log($info);
            return $info;
    }
    
    $current_approver_approval_group = get_approval_group_details($current_approval_group_id);
    
    $approver = get_approver_in_approval_group($current_approver_approval_group['approval_group_id'], $current_user_id);
    if(!$approver){
            $info['success'] = false;
            $info['approval_status'] = AS_WITH_THE_APPROVER;
            $info['next_approval_group_id'] = $current_approval_group_id;
            $info['message'] = "You are not included in the current approval group [".$current_approver_approval_group['approval_group_description']."] of the $document_name";
            if(!$check_only) write_to_approval_log($info);
            return $info;
    }

    
    if($info['document_total'] > $approver['approval_limit']){
        //Approver does not have suficient approval limit. So can not approve.
        $info['approval_status'] = AS_AWAITING_APPROVAL;
        $info['next_approval_group_id'] = 0;
//        $info['display_status'] = $current_approver_approval_group['display_status'];
//        $info['position_code'] = $current_approver_approval_group['position_code'];
        if(isset($current_approver_approval_group['next_position_message']) && $current_approver_approval_group['next_position_message'] != ""){
            $info['message'] = $current_approver_approval_group['next_position_message'];
        }
        else{
            $info['message'] = "Your approval limit of ".$approver['approval_limit']." is  not sufficient to $change_type_name $document_name.";
        }
        
        if($check_only){
            $info['success'] = false;
        }
        else{
            $info['success'] = true;
            $udas_msg = update_document_approval_status($info);
            $info['success'] = $udas_msg['success'];
            $info['message'] = $udas_msg['message'];
        }
        return $info;
    }
    
    //Approver has sufficient approval limit
    // Check for value loss
    if($value_loss > 0 && $value_loss >= $approver['over_budget_limit']){
        $info['approval_status'] = AS_AWAITING_APPROVAL;
        $info['next_approval_group_id'] = 0;
//        $info['display_status'] = $current_approver_approval_group['display_status'];
//        $info['position_code'] = $current_approver_approval_group['position_code'];
        if($current_approver_approval_group['next_position_message'] != ""){
            $info['message'] = $current_approver_approval_group['next_position_message'];
        }
        else{
            if($info['tran_type'] == ST_SALESORDER || $info['tran_type'] == ST_SALESQUOTE){
                $info['message'] = "Your overall order discont value limit of ".$approver['over_budget_limit']." is  not sufficient to $change_type_name $document_name.";
            }
            else{
                $info['message'] = "Your over budget limit of ".$approver['over_budget_limit']." is  not sufficient to $change_type_name $document_name.";
            }
        }
        
        if($check_only){
            $info['success'] = false;
        }
        else{
            $info['success'] = true;
            $udas_msg = update_document_approval_status($info);
            $info['success'] = $udas_msg['success'];
            $info['message'] = $udas_msg['message'];
        }
        return $info;
    }
    
    //Approver has sufficient approval limit
    // Approver has sufficient value loss approval limit
    // Check for percentage loss
    
    if($perc_loss > 0 && $perc_loss >= $approver['over_budget_percentage']){
        $info['approval_status'] = AS_AWAITING_APPROVAL;
        $info['next_approval_group_id'] = 0;
//        $info['display_status'] = $current_approver_approval_group['display_status'];
//        $info['position_code'] = $current_approver_approval_group['position_code'];
        if($current_approver_approval_group['next_position_message'] != ""){
            $info['message'] = $current_approver_approval_group['next_position_message'];
        }
        else{
            if($info['tran_type'] == ST_SALESORDER || $info['tran_type'] == ST_SALESQUOTE){
                $info['message'] = "Your overall order discount percentage limit of ".$approver['over_budget_percentage']."% is  not sufficient to $change_type_name $document_name.";
            }
            else{
                $info['message'] = "Your over budget percentage limit of ".$approver['over_budget_percentage']."% is  not sufficient to $change_type_name $document_name.";
            }
        }
        
        if($check_only){
            $info['success'] = false;
        }
        else{
            $info['success'] = true;
            $udas_msg = update_document_approval_status($info);
            $info['success'] = $udas_msg['success'];
            $info['message'] = $udas_msg['message'];
        }
        return $info;
    }
    
    switch ($info['change_type']) {
        case "REJECT":
            $info['approval_status'] = AS_REJECTED;
            break;
        case "REOPEN":
            $info['approval_status'] = AS_REOPENED;
            break;
        default:
            $info['approval_status'] = AS_APPROVED;
            break;
    }
    
    $info['next_approval_group_id'] = 0;
    $info['message'] = $document_name." has been ".$change_type_name."ed.";
        
    if($check_only){
        $info['success'] = true;
        $info['message'] = "Can $change_type_name $document_name";
        return $info;
    }
    else{
        $udas_msg = update_document_approval_status($info);
        if($udas_msg['success'] === true){
                $info['success'] = $udas_msg['success'];
        }
        else{
                $info['success'] = $udas_msg['success'];
                $info['message'] = $udas_msg['message'];
        }
        return $info;
    }

}

function calculate_sales_order_losses($order, &$value_loss, &$perc_loss)
{
    $sales_order_price_total = 0;
    $sales_order_list_price_total = 0;
    
    $result = get_sales_order_detail_records($order->order_no, $order->trans_type);
    while ($row = db_fetch($result)) {

        $line_total = $row['order_unit_qty'] * $row['order_unit_price'] * (1 - $row['discount_percent']);

        $qty = $row['order_unit_qty']* $row['convertion_factor'];
        
        $line_list_price_total = $qty * $row['list_price'];
        
        $sales_order_price_total += $line_total;
        $sales_order_list_price_total += $line_list_price_total;
    }
    
    $value_loss = $sales_order_list_price_total - $sales_order_price_total;
    if($value_loss < 0){
        $value_loss = 0;
    }
    
    if($sales_order_list_price_total > 0){
        $perc_loss = ($value_loss)/$sales_order_list_price_total * 100;
    }
    else{
        $perc_loss = 0;
    }
}

function can_direct_approve_sales_document($cart, $current_user_id)
{
    $dad_msg = direct_approve_sales_documents($cart, $current_user_id, true);
    return $dad_msg;

}

function direct_reject_work_order($wo, $current_user_id, $check_only = false)
{
    $work_order_approval_group_id = get_company_pref('work_order_appr_group_id');
    if($work_order_approval_group_id == null || $work_order_approval_group_id == '' || $work_order_approval_group_id == 0){
        return array("success" => false, "message" => "Workorder approval group is not set.");
    }
    
    $value_loss = 0;
    $perc_loss = 0;
    
    $info['project_id'] = $wo['wo_project_id'];
    $info['tran_type'] = ST_WORKORDER;
    $info['tran_no'] = $wo['wo_id'];
    $info['document_total'] = $wo['wo_total'];
    $info['current_approval_group_id'] = $work_order_approval_group_id;
    $info['current_user_id'] = $current_user_id;
    $info['message'] = "";
    $info['row_state'] = $wo['wo_row_state'];
    $info['change_type'] = "REJECT";
    
    $info['value_loss'] = $value_loss;
    $info['perc_loss'] = $perc_loss;
    
    return direct_approve_document($info, $current_user_id, $check_only);
}
     

function reopen_work_order($wo, $current_user_id, $check_only = false)
{
//    $work_order_approval_group_id = get_company_pref('work_order_appr_group_id');
//    if($work_order_approval_group_id == null || $work_order_approval_group_id == '' || $work_order_approval_group_id == 0){
//        return array("success" => false, "message" => "Workorder approval group is not set.");
//    }
//    
//    $value_loss = 0;
//    $perc_loss = 0;
//    
//    $info['project_id'] = $wo['wo_project_id'];
//    $info['tran_type'] = ST_WORKORDER;
//    $info['tran_no'] = $wo['wo_id'];
//    $info['document_total'] = $wo['wo_total'];
//    $info['current_approval_group_id'] = $work_order_approval_group_id;
//    $info['current_user_id'] = $current_user_id;
//    $info['message'] = "";
//    $info['row_state'] = $wo['wo_row_state'];
//    $info['change_type'] = "REOPEN";
//    
//    $info['value_loss'] = $value_loss;
//    $info['perc_loss'] = $perc_loss;
    
    return reopen_approved_document(ST_WORKORDER, $wo['wo_id'], $wo['wo_project_id'], 
                        $wo['wo_next_approval_group_id'], $current_user_id, $wo['wo_row_state'],
                        $wo['wo_created_by']);
    
    //return direct_approve_document($info, $current_user_id, $check_only);
}
  

function direct_reopen_sales_order($so, $current_user_id, $check_only = false)
{
    $sales_order_approval_group_id = get_company_pref('sales_order_appr_group_id');
    if($sales_order_approval_group_id == null || $sales_order_approval_group_id == '' || $sales_order_approval_group_id == 0){
        return array("success" => false, "message" => "Sales order approval group is not set.");
    }
    
    $value_loss = 0;
    $perc_loss = 0;
    
    $info['project_id'] = $so['project_id'];
    $info['tran_type'] = ST_SALESORDER;
    $info['tran_no'] = $so['order_no'];
    $info['document_total'] = $so['total'];
    $info['current_approval_group_id'] = $sales_order_approval_group_id;
    $info['current_user_id'] = $current_user_id;
    $info['message'] = "";
    $info['row_state'] = $so['row_state'];
    $info['change_type'] = "REOPEN";
    
    $info['value_loss'] = $value_loss;
    $info['perc_loss'] = $perc_loss;
    
    return direct_approve_document($info, $current_user_id, $check_only);
}
  


function direct_reopen_sales_quotation($sq, $current_user_id, $check_only = false)
{
    $sales_order_approval_group_id = get_company_pref('sales_order_appr_group_id');
    if($sales_order_approval_group_id == null || $sales_order_approval_group_id == '' || $sales_order_approval_group_id == 0){
        return array("success" => false, "message" => "Sales order approval group is not set.");
    }
    
    $value_loss = 0;
    $perc_loss = 0;
    
    $info['project_id'] = $sq['project_id'];
    $info['tran_type'] = ST_SALESQUOTE;
    $info['tran_no'] = $sq['order_no'];
    $info['document_total'] = $sq['total'];
    $info['current_approval_group_id'] = $sales_order_approval_group_id;
    $info['current_user_id'] = $current_user_id;
    $info['message'] = "";
    $info['row_state'] = $sq['row_state'];
    $info['change_type'] = "REOPEN";
    
    $info['value_loss'] = $value_loss;
    $info['perc_loss'] = $perc_loss;
    
    return direct_approve_document($info, $current_user_id, $check_only);
}

function direct_approve_job_material_requisition($info, $check_only=false) {
    $current_user_id = $info['current_user_id'];
    $info['message'] = "";
    $info['change_type'] = "APPROVE";
    
    return direct_approve_document($info, $current_user_id, $check_only);
}

//function direct_change_document_approval_position($info, $current_user_id, $check_only = false)
//{            
//    global $systypes_array;
//    
//    $current_approval_group_id = $info['current_approval_group_id'];
//    $value_loss = $info['value_loss'];
//    $perc_loss = $info['perc_loss'];
//    $type = $info['tran_type'];
//    
//    switch ($info['change_type']) {
//        case "REJECT":
//            $change_type_name = "reject";
//            break;
//        case "REOPEN":
//            $change_type_name = "reopen";
//            break;
//        default:
//            $change_type_name = "approve";
//            break;
//    }
//        
//    if(!$current_approval_group_id > 0){
//            $info['success'] = false;
//            $info['approval_status'] = AS_WITH_THE_APPROVER;
//            $info['next_approval_group_id'] = $current_approval_group_id;
//            $info['message'] = "No approval group defined for ".$systypes_array[$type]." Approval. Please contact system administrator";
//            if(!$check_only) write_to_approval_log($info);
//            return $info;
//    }
//    
//    $current_approver_approval_group = get_approval_group_details($current_approval_group_id);
//    
//    $approver = get_approver_in_approval_group($current_approver_approval_group['approval_group_id'], $current_user_id);
//    if(!$approver){
//            $info['success'] = false;
//            $info['approval_status'] = AS_WITH_THE_APPROVER;
//            $info['next_approval_group_id'] = $current_approval_group_id;
//            $info['message'] = "You are not included in document's current approval group [".$current_approver_approval_group['approval_group_description']."]";
//            if(!$check_only) write_to_approval_log($info);
//            return $info;
//    }
//
//    
//            
//    if($info['document_total'] >= $approver['approval_limit']){
//        $info['approval_status'] = AS_AWAITING_APPROVAL;
//        $info['next_approval_group_id'] = 0;
//        $info['message'] = "Your approval limit of ".$approver['approval_limit']." is  not sufficient to $change_type_name this document.";
//        if($check_only){
//            $info['success'] = false;
//        }
//        else{
//            $info['success'] = true;
//            $udas_msg = update_document_approval_status($info);
//            $info['success'] = $udas_msg['success'];
//            $info['message'] = $udas_msg['message'];
//        }
//        return $info;
//    }
//    if($value_loss >= $approver['over_budget_limit']){
//        $info['approval_status'] = AS_AWAITING_APPROVAL;
//        $info['next_approval_group_id'] = 0;
//        $info['message'] = "Your overall order loss value / over budget limit of ".$approver['over_budget_limit']." is  not sufficient to $change_type_name this document.";
//        if($check_only){
//            $info['success'] = false;
//        }
//        else{
//            $info['success'] = true;
//            $udas_msg = update_document_approval_status($info);
//            $info['success'] = $udas_msg['success'];
//            $info['message'] = $udas_msg['message'];
//        }
//        return $info;
//    }
//    if($perc_loss >= $approver['over_budget_percentage']){
//        $info['approval_status'] = AS_AWAITING_APPROVAL;
//        $info['next_approval_group_id'] = 0;
//        $info['message'] = "Your overall order loss / over budget percentage limit of ".$approver['over_budget_percentage']."% is  not sufficient to $change_type_name this document.";
//        if($check_only){
//            $info['success'] = false;
//        }
//        else{
//            $info['success'] = true;
//            $udas_msg = update_document_approval_status($info);
//            $info['success'] = $udas_msg['success'];
//            $info['message'] = $udas_msg['message'];
//        }
//        return $info;
//    }
//    
//    $info['approval_status'] = AS_APPROVED;
//    $info['next_approval_group_id'] = 0;
//    $info['message'] = "Document no ".$info['tran_no']." has been ".$change_type_name."ed.";
//        
//    if($check_only){
//        $info['success'] = true;
//        $info['message'] = "Can approve document";
//        return $info;
//    }
//    else{
//        $udas_msg = update_document_approval_status($info);
//        if($udas_msg['success'] === true){
//                $info['success'] = $udas_msg['success'];
//        }
//        else{
//                $info['success'] = $udas_msg['success'];
//                $info['message'] = $udas_msg['message'];
//        }
//        return $info;
//    }
//
//}
//

?>