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

global $path_to_root;
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 . "/includes/db/project_cost_functions_db.inc");


function allocate_to_tasks($tran_type, $tran_no, 
                        &$tasks_summary, $current_user_id)
{
    $in_string = "";
    
    foreach ($tasks_summary as &$task) {
        if(!(isset($task['task_id']) && $task['task_id'] > 0)){
            array("success" => false, "message" => "No task selected for one or more items.");
        }
        $atet_msg = allocate_to_each_task($tran_type, $tran_no, $task, $current_user_id);
        if($atet_msg['success'] !== true){
            return $atet_msg;
        }
        
        if($in_string == ""){
            $in_string .= $task['task_id'];
        }
        else{
            $in_string .= ",".$task['task_id'];
        }
    }
    
//    if($in_string != ""){
//        $redundent_cost_allocations = get_redundent_cost_allocations($tran_type, $tran_no, $in_string);
//        while ($row = mysql_fetch_row($redundent_cost_allocations)) {
//            $task = get_task_record($row['task_id']);
//            $rta_msg = remove_task_allocation($tran_type, $tran_no, $task, $current_user_id);
//            if($rta_msg['success'] !== true){
//                return $rta_msg;
//            }
//        }
//    }
    return $atet_msg;
}

function allocate_to_each_task($tran_type, $tran_no, 
                        &$task, $current_user_id)
{
        //$new_allocation_amount = number_format($task['cost'], 2);
        $new_allocation_amount = $task['remaining_cost'];
                
        //$task = get_task_record($task_id);
        $task_id = $task['task_id'];
        
        if(!$task){
            $msg['success'] = false;
            $msg['message'] = "Could not find details of the project task [".$task_id."]. "
                                        . "Please contact your system administrator";
            return $msg;
        }

        $task_cost_allocation_detail = get_task_allocation_record($tran_type, $tran_no, $task_id);    
        if(!$task_cost_allocation_detail){
            $exisiting_total_cost_allocation = get_task_total_budget_allocated($task_id);
            $new_total_cost_allocation = $exisiting_total_cost_allocation + $new_allocation_amount;
            $uptca_msg = update_task_total_allocation($tran_type, $tran_no, 
                                        $task_id, $new_total_cost_allocation, 
                                        $current_user_id, $task['row_state']);
            
            $task['row_state'] = db_new_row_state($task['row_state']);
            
            if($uptca_msg['success'] !== true){
                return $uptca_msg;
            }

            $atad_msg = add_task_allocation_detail($task_id, $tran_type, $tran_no, 
                                    $new_allocation_amount, $new_total_cost_allocation, $current_user_id);

            return $atad_msg;
            
        }
        else{
            $allocation_adjustment = $new_allocation_amount - $task_cost_allocation_detail['amount'];

            $exisiting_total_cost_allocation = get_task_total_budget_allocated($task_id);
            $new_total_cost_allocation = $exisiting_total_cost_allocation + $allocation_adjustment;
            if($new_total_cost_allocation < 0){
                $new_total_cost_allocation = 0;
            }
            $uptca_msg = update_task_total_allocation($tran_type, $tran_no, 
                                        $task_id, $new_total_cost_allocation, 
                                        $current_user_id, $task['row_state']);
            
            $task['row_state'] = db_new_row_state($task['row_state']);
            if($uptca_msg['success'] !== true){
                return $uptca_msg;
            }
            
            if($task_cost_allocation_detail['amount'] == $new_allocation_amount){
                return array('success' => true, 'message' => 1);
            }

            $upc_msg = update_task_allocation_detail($task_cost_allocation_detail['id'], $task_id, $tran_type, 
                                        $tran_no, $new_allocation_amount, $new_total_cost_allocation, 
                                        $current_user_id);   

            return $upc_msg;
             
        }
}


function remove_task_allocations($tran_type, $tran_no, 
                        $tasks, $current_user_id)
{
    foreach ($tasks as $task) {
        $rta_msg = remove_task_allocation($tran_type, $tran_no, $task, $current_user_id);
        if($rta_msg['success'] !== true){
            return $rta_msg;
        }
    }
    
    return array("success" => true, "message" => "");
}


function remove_task_allocation($tran_type, $tran_no, $task, $current_user_id)
{
        $task_cost_allocation_detail = array();
    
        $task_id = $task['task_id'];
        $task_cost_allocation_detail_rec = get_task_allocation_record($tran_type, $tran_no, $task_id);    
        if(!$task_cost_allocation_detail_rec){
            $comment = "No cost allocation record found";
            write_to_task_allocation_log($task_id, $tran_type, 
                        $tran_no, 0,
                        $current_user_id, $comment);
//            $tad = array('id' => 0, 'amount' => 0);
//            $task_cost_allocation_detail[] = $tad;
            $task_cost_allocation_detail['amount'] = 0;
            $task_cost_allocation_detail['id'] = 0;
            
        }
        else{
            $task_cost_allocation_detail = $task_cost_allocation_detail_rec;
        }
        
        if($task_cost_allocation_detail['id'] > 0){
            $dpca_msg = delete_task_allocation_detail($task_cost_allocation_detail['id'], $task_id, $tran_type, 
                                                    $tran_no, $task_cost_allocation_detail['amount'],
                                                    $current_user_id);
            if($dpca_msg['success'] !== true){
                return $dpca_msg;
            }
        }
        
        $exisiting_total_cost_allocation = get_task_total_budget_allocated($task_id);
        $new_total_cost_allocation = $exisiting_total_cost_allocation;// - $task_cost_allocation_detail['amount'];
        if($new_total_cost_allocation < 0){
            $new_total_cost_allocation= 0;
        }

        if($new_total_cost_allocation != $task['budget_allocated']){
            $uptca_msg = update_task_total_allocation($tran_type, $tran_no, 
                                        $task_id, $new_total_cost_allocation, 
                                        $current_user_id, $task['row_state']);
            if($uptca_msg['success'] !== true){
                return $uptca_msg;
            }
        }

        return array("success" => true, "message" => "");

}


function charge_cost_to_tasks($tran_type, $tran_no, 
                        $tasks_charging_summary, $current_user_id)
{
    foreach ($tasks_charging_summary as $task) {
        $cctet_msg = charge_cost_to_each_task($tran_type, $tran_no, $task, $current_user_id);
        if($cctet_msg['success'] !== true){
            return $cctet_msg;
        }
    }
    return array("success" => true, "message" => "");
}



function charge_cost_to_each_task($tran_type, $tran_no, 
                                $task, $current_user_id)
{
        $cost = $task['charging_cost'];
                
        if($cost == 0){
            return array("success" => true, "message" => 1);
        }

        $existing_total_task_cost = get_task_total_budget_used($task['task_id']);
        $new_total_task_cost = $existing_total_task_cost + $cost;
        
        $uttc_msg = update_total_task_cost($task['task_id'], $new_total_task_cost, $task['row_state']);
        if($uttc_msg['success'] !== true){
            return array("success" => $uttc_msg['success'], "message" => $uttc_msg['message']);
        }

        $acdtt_msg = add_cost_detail_to_task($tran_type, $tran_no, 
                            $task['task_id'], $cost,
                            $new_total_task_cost, $current_user_id);

         return $acdtt_msg;
        

}

function remove_costs_from_tasks($tran_type, $tran_no, 
                        $tasks_removing_costs_summary, $current_user_id)
{
    foreach ($tasks_removing_costs_summary as $task) {
        $cctet_msg = remove_cost_from_each_task($tran_type, $tran_no, $task, $current_user_id);
        if($cctet_msg['success'] !== true){
            return $cctet_msg;
        }
    }
    return $cctet_msg;
}


function remove_cost_from_each_task($tran_type, $tran_no, 
                                $task, $current_user_id)
{
    $removing_cost = $task['charging_cost'];
    
        if($removing_cost == 0){
            return array("success" => true, "message" => 1);
        }
        
//        $task = get_task_details($task['task_id']);
//        if(!$task){
//            $msg['success'] = false;
//            $msg['message'] = "Could not find details of the project task [".$task_id."]. "
//                                        . "Please contact your system administrator";
//            return $msg;
//        }

//        $removing_cost = abs($removing_cost);
        
        $existing_total_task_cost = get_task_total_budget_used($task['task_id']);
        $new_total_task_cost = $existing_total_task_cost - $removing_cost;
        
        $actp_msg = add_cost_detail_to_task($tran_type, $tran_no, 
                            $task['task_id'], -$removing_cost,
                            $new_total_task_cost, $current_user_id);
        if($actp_msg['success'] !== true){
            return array("success" => $actp_msg['success'], "message" => $actp_msg['message']);
        }

        return update_total_task_cost($task['task_id'], $new_total_task_cost, $task['row_state']);


}

//-------------------------------------------------------------------------------------------------------------

function add_to_task_cost(&$task_costs, $task_id, $line_remaining_cost, $line_remaining_reference_cost, $line_charging_cost)
{
    if(is_array($task_costs)){
        foreach ($task_costs as &$task_cost_line) {
            if(isset($task_cost_line['task_id']) && $task_cost_line['task_id'] == $task_id){
                $task_cost_line['remaining_cost'] += $line_remaining_cost;
                $task_cost_line['reference_cost'] += $line_remaining_reference_cost;
                $task_cost_line['charging_cost'] += $line_charging_cost;
                return;
            }        
        }
    }
    
    $new_task_cost_line = array('task_id' => $task_id, 
                                'remaining_cost' => $line_remaining_cost, 
                                'reference_cost' => $line_remaining_reference_cost, 
                                'charging_cost' => $line_charging_cost);
    $task_costs[] = $new_task_cost_line;
}


function get_all_tasks_remaining_total($tasks_summary)
{
    $remaining_total = 0;
    
    foreach ($tasks_summary as &$task_cost_line) {
        if($task_cost_line['remaining_cost'] >= 0){
            $remaining_total += $task_cost_line['remaining_cost'];
        }
    }
    
    return $remaining_total;
}


function reallocate_to_tasks($tran_type, $tran_no, 
                        &$tasks_summary, $current_user_id)
{
    foreach ($tasks_summary as &$task) {
        if(!(isset($task['task_id']) && $task['task_id'] > 0)){
            $rtt_msg = array("success" => false, "message" => "No budget selected for one or more items.");
        }
        
        if($task['remaining_cost'] > 0){
            $rtt_msg = allocate_to_each_task($tran_type, $tran_no, $task, $current_user_id);
            if($rtt_msg['success'] !== true){
                return $rtt_msg;
            }
        }
        else{
            $rtt_msg = remove_task_allocation($tran_type, $tran_no, $task, $current_user_id);
            if($rtt_msg['success'] !== true){
                return $rtt_msg;
            }
        }
        
    }
    
    return array("success" => true, "message" => "") ;
}

?>