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

function recalculate_task_total_cost($task_id)
{
    update_rebuilt_total_task_cost($task_id);
}

function recalculate_project_task($task_id)
{
    
    delete_task_cost_cost_recalculations($task_id);
    
    $task = get_task_record($task_id);
    $row_state = $task['row_state'];
    
    $id = add_task_cost_cost_recalculation($task_id, $row_state);
    
    delete_task_cost_rebuild_task_details($task_id);
    
    recalculate_project_task_allocations($task_id);
    recalculate_project_task_costs($task_id);
    
    $n = update_task_cost_recalculation_completed($task_id, $row_state);
    if($n !== 1){
        $m = "Budget ".$task['task_id']." - ".$task['task_code']." - ".$task['description']." has been updated after cost recalculation started. These cost are now outdated.";
        return array("success" => false, "message" => $m);
    }
    else{
        return array("success" => true, "message" => "");
    }
}


function recalculate_project_task_allocations($task_id)
{
    recalculate_material_requisitions($task_id);
    recalculate_purchase_requisitions($task_id);
    recalculate_vehicle_requisitions($task_id);
    recalculate_iou_requests($task_id);
    recalculate_tool_requests($task_id);
    recalculate_tool_issues($task_id);
    recalculate_subcontractor_purchase_orders($task_id);
}

function recalculate_project_task_costs($task_id)
{

    recalculate_material_issues($task_id);//checked and ok
    recalculate_material_return($task_id);//checked and has some error
    
    recalculate_inventory_adjustments(ST_INVENTORY_RECEIPT, $task_id); //checked and ok
    recalculate_inventory_adjustments(ST_INVENTORY_ISSUE, $task_id);//checked and ok
    
    recalculate_stock_counts($task_id);//checked and ok
    
    recalculate_purchase_requistion_issues($task_id);//checked and ok
    recalculate_i_owe_you_expenses($task_id);//checked and ok    
    recalculate_vehicle_running_chart_batch($task_id); //checked and ok
    recalculate_task_cost_adjustments($task_id); //checked and ok `
    
    recalculate_subcontractor_invoices($task_id); //checked and ok
    recalculate_subcontractor_credit_notes($task_id);
    
//    recalculate_supplier_transport_invoices($task_id);//checked and ERROR
    recalculate_utility_bills($task_id);//checked and ok
    recalculate_utility_bill_credit_notes($task_id);
    
    recalculate_miscellaneous_project_costs($task_id);
    
    recalculate_tool_usage_charges($task_id);
    
////    recalculate_inventory_adjustments(ST_INVADJUST, $task_id);//could not find the projects  
//    recalculate_grns($task_id);
    
    recalculate_task_total_cost($task_id);
    
}

    
    function recalculate_entire_project_task_costs($project_id)
    {
        $result = get_project_boq_item_groups($project_id);
        while($boq_item_group = db_fetch($result))
        { 
            $msg = recalculate_project_boq_group_task_costs($project_id, $boq_item_group['id']);
            if($msg['success'] !== true){
                return $msg;
            }
        }
        
        return recalculate_project_boq_group_task_costs($project_id, 0);

    }
    

    function recalculate_project_boq_group_task_costs($project_id, $boq_group_id)
    {
        $boq_group_task_groups_result = get_boq_group_task_groups($project_id, $boq_group_id);
        while($boq_group_task_group = db_fetch($boq_group_task_groups_result))
        { 
            $task_group_tasks_result = get_task_group_tasks($project_id, $boq_group_task_group['id']);
            while($task = db_fetch($task_group_tasks_result))
            {                         
                if($task['task_id'] > 0){
                    $msg = recalculate_project_task($task['task_id']);
                    if($msg['success'] !== true){
                        return $msg;
                    }
                }
            }
        }
        
        return array("success" => true, "message" => "");
    }
    

function save_to_task_costs_rebuild($result, $allocation = false)
{
    $total_cost=0;
    while($row = db_fetch($result))
    {      
        $type = $row['type'];
        $tran_no = $row['tran_no'];
        $task_id = $row['task_id'];
        $amount = $row['task_cost'];
        $created_by = $row['created_by'];
        $created_on = $row['created_on'];

        $total_cost+=$row['task_cost'];
        
        switch ($type) {
            case ST_SUBCONTRACTOR_CREDIT_NOTE:
            case ST_UTILITY_BILL_CREDIT_NOTE:
                $tax_factor = $row['tax_factor'];
                $amount *= $tax_factor;
                break;
            default:
                break;
        }

        if($amount != 0){
            add_to_task_costs_rebuild($type, $tran_no, $task_id, $amount, $created_by, $created_on, $allocation);
        }

    }
}

?>