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

function display_document_task_status($task_costs_array)
{
    start_table(TABLESTYLE, "width=90%");

    $th = array("Type", "Id", "BOQ Item Group", "Budget", 
                "Budget Total", "Budget Allocated", "Budget Used", 
                "Budget Available",  "Document Budget Total", "Budget Availability" );


    table_header($th);
    
    $th2 = array("Type", "Id", "BOQ Item Group", "BOQ Item", 
                "Max. Qty", "", "Qty Completetd", 
                "Qty Remaining",  "", "Workdone Against Cost" );


    table_header($th2);

    foreach ($task_costs_array as $line) {

        $insufficient_budget = false;
        $budget_available = $line['budget'] - ($line['budget_allocated'] + $line['budget_used']);
        $task_cost = $line['task_cost'];
        $task_required_amount = $line['task_cost'];
        $task_total_allocation = 0;
        
        switch ($line['tran_type']) {
            case ST_MATERIAL_REQ:
            case ST_INVENTORY_ISSUE_REQUEST:
            case ST_PURCH_REQ:
            case ST_I_OWE_YOU:
            case ST_SUBCONTRACTOR_PURCH_ORDER:
                if($line['approval_status'] == AS_NEW || $line['approval_status'] == AS_AWAITING_APPROVAL || 
                        $line['approval_status'] == AS_WITH_THE_APPROVER || $line['approval_status'] == AS_BACK_TO_ORIGINATOR){
                            if($task_cost > $budget_available){
                                $insufficient_budget = true;
                            }
                        }
                break;
            case ST_I_OWE_YOU_BATCH:
                if($line['approval_status'] == AS_NEW || $line['approval_status'] == AS_AWAITING_APPROVAL || 
                        $line['approval_status'] == AS_WITH_THE_APPROVER || $line['approval_status'] == AS_BACK_TO_ORIGINATOR){
//                            if($task_cost > $budget_available){
                                $task_total_allocation = get_batch_i_owe_yous_total_task_allocation($line['trans_no'], $line['task_id']);
                                if($task_cost > $task_total_allocation){
                                    // Only the additional amount for this task is required. Balance is already allocated
                                    $task_required_amount = $task_cost - $task_total_allocation;
                                    if($task_required_amount > $budget_available){
                                        $insufficient_budget = true;
                                    }
                                }
                                else{
                                    // More than the required amount already allocated
                                    $task_required_amount = 0;
                                }
                                
//                            }
                        }
                break;

            default:
                break;
        }
        
        start_row();
            label_cell("Budget");
            label_cell($line['task_id']);
            label_cell($line['boq_group_description']);        
            label_cell($line['task_code']." - ".$line['task_description']);
            
            amount_cell($line['budget']);
            
            $task_allocation_link = get_task_cost_allocation_details_view_str($line['task_id'], number_format($line['budget_allocated'],2));
            label_cell($task_allocation_link, 'align=right');
            
            $task_used_link = get_task_cost_details_view_str($line['task_id'], number_format($line['budget_used'],2));
            label_cell($task_used_link, 'align=right');
//            amount_cell($line['budget_used']);
            
            amount_cell($budget_available);
            
            if($line['tran_type'] == ST_I_OWE_YOU_BATCH && 
                    ($line['approval_status'] == AS_NEW || $line['approval_status'] == AS_AWAITING_APPROVAL || 
                        $line['approval_status'] == AS_WITH_THE_APPROVER || $line['approval_status'] == AS_BACK_TO_ORIGINATOR)){
                label_cell(number_format($task_cost,2)." (Allocated: ".number_format($task_total_allocation,2).")","align=right");
            }
            else{
                label_cell(number_format($task_cost,2),"align=right");
            }
            
            
            $budget_availability = number_format($budget_available/$line['budget']*100,2);
            
            if($insufficient_budget){
                $budget_short = $task_required_amount - $budget_available;
                if(!($budget_short > 0)){
                    $budget_short = 0;
                }
                label_cell("<font color='red'>".$budget_short." (Insufficient)","align=right");
            }
            else{
                label_cell($budget_availability."%","align=right");
            }

        end_row();
        
        start_row();
            label_cell("BOQ Item");
            if($line['boq_item_id'] > 0){
                label_cell($line['boq_item_id']);
                label_cell($line['boq_group_description']);        
                label_cell($line['boq_item_code']." - ".$line['boq_item_description']);
                
                amount_decimal_cell($line['max_quantity'],"", null, 2);                
                amount_decimal_cell(0,"", null, 2);
                amount_decimal_cell($line['total_work_done'],"", null, 2);

                $remaining_work = $line['max_quantity'] - $line['total_work_done'];
                amount_decimal_cell($remaining_work,"", null, 2);
                
//                label_cell("");
                $work_done_perc = 0;
                if($line['max_quantity'] > 0){
                    $work_done_perc = $line['total_work_done'] / $line['max_quantity'];
                }

                $cost_used_perc = 0;
                if($line['budget'] > 0){
                    $cost_used_perc = $line['budget_used'] / $line['budget'];
                }
                
                $workdone_against_cost = 0;
                if($cost_used_perc > 0){
                    $workdone_against_cost = $work_done_perc/$cost_used_perc*100;
                }
                
                amount_decimal_cell(0,"", null, 2);
                label_cell(number_format($workdone_against_cost, 2)."%","align=right");
            }
            else{
                label_cell("No BOQ Item", "colspan=9");
            }
        end_row();
        
        start_row();
            label_cell("-", "colspan=10");
        end_row();

    }

    end_table(1);
    
}









function add_task_details_to_array(&$task_costs_array)
{
    foreach ($task_costs_array as &$line) {
        $task = get_task_boq_details($line["task_id"]);
        $line["task_code"] = $task["task_code"];
        $line["task_description"] = $task["task_description"];
        $line["budget"] = $task["budget"];
        $line["budget_used"] = $task["budget_used"];
        $line["budget_allocated"] = $task["budget_allocated"];
        $line["task_group_description"] = $task["task_group_description"];
        $line["boq_group_description"] = $task["boq_group_description"];      
        
        $boq_item = get_boq_item_details($line["task_id"]);
        if(isset($boq_item["id"]) && $boq_item["id"] > 0){
            $line["boq_item_id"] = $boq_item["id"];
            $line["boq_item_code"] = $boq_item["boq_item_code"];
            $line["boq_item_description"] = $boq_item["boq_item_description"];
            $line["max_quantity"] = $boq_item["max_quantity"]; 
            $line["total_work_done"] = 0;
        }
        else{
            $line["boq_item_id"] = 0;
            $line["boq_item_code"] = "";
            $line["boq_item_description"] = "";
            $line["max_quantity"] = "";  
            
            $line["total_work_done"] = 0;
        }
    }
}


function get_material_requisition_task_status_array($trans_no, &$task_costs_array)
{
    $result = get_material_requisition_task_cost_details($trans_no);
    
    while ($row = db_fetch($result)) {
        $s = array("tran_type" => ST_MATERIAL_REQ, 
                    "trans_no" => $row['trans_no'], 
                    "approval_status" => $row['approval_status'], 
                    "task_id" => $row['task_id'], 
                    "task_cost" => $row['task_cost']);
        $task_costs_array[] = $s;
    }    
}

function get_inventory_adjustment_request_task_status_array($trans_type, $trans_no, &$task_costs_array)
{
    $result = get_inventory_adjustment_request_task_cost_details($trans_type, $trans_no);
    
    while ($row = db_fetch($result)) {
        $s = array("tran_type" => $row['trans_type'], 
                    "trans_no" => $row['trans_no'], 
                    "approval_status" => $row['approval_status'], 
                    "task_id" => $row['task_id'], 
                    "task_cost" => $row['task_cost']);
        $task_costs_array[] = $s;
    }    
}

    
function get_purchase_requisition_task_status_array($trans_no, &$task_costs_array)
{
    $result = get_purchase_requisition_task_cost_details($trans_no);
    
    while ($row = db_fetch($result)) {
        $s = array("tran_type" => ST_PURCH_REQ, 
                    "trans_no" => $row['trans_no'], 
                    "approval_status" => $row['approval_status'], 
                    "task_id" => $row['task_id'], 
                    "task_cost" => $row['task_cost']);
        $task_costs_array[] = $s;
    }    
}

function get_work_order_task_status_array($trans_no, &$task_costs_array)
{
    $result = get_work_order_task_cost_details($trans_no);
    
    while ($row = db_fetch($result)){
        $s = array("tran_type" => ST_WORKORDER, 
                    "trans_no" => $row['trans_no'], 
                    "approval_status" => $row['approval_status'], 
                    "task_id" => $row['task_id'], 
                    "task_cost" => $row['task_cost']);
        $task_costs_array[] = $s;
    }    
}


function get_i_owe_you_task_status_array($trans_no, &$task_costs_array)
{
    $result = get_i_owe_you_task_cost_details($trans_no);
    
    while ($row = db_fetch($result)) {
        $s = array("tran_type" => ST_I_OWE_YOU, 
                    "trans_no" => $row['trans_no'], 
                    "approval_status" => $row['approval_status'], 
                    "task_id" => $row['task_id'], 
                    "task_cost" => $row['task_cost']);
        $task_costs_array[] = $s;
    }    
}

function get_i_owe_you_batch_status_array($trans_no, &$task_costs_array)
{
    $result = get_i_owe_you_batch_task_cost_details($trans_no);
    
    while ($row = db_fetch($result)){
        
        $task_cost = $row['task_cost'];
        $s = array("tran_type" => ST_I_OWE_YOU_BATCH, 
                    "trans_no" => $row['trans_no'], 
                    "approval_status" => $row['approval_status'], 
                    "task_id" => $row['task_id'], 
                    "task_cost" => $task_cost);
        $task_costs_array[] = $s;
    }    

}
 
function get_vehicle_request_status_array($trans_no, &$task_costs_array)
{
    $result = get_vehicle_request_task_cost_details($trans_no);
    
    while ($row = db_fetch($result)){
        $s = array("tran_type" => ST_VEHICLE_REQUEST, 
                    "trans_no" => $row['trans_no'], 
                    "approval_status" => $row['approval_status'], 
                    "task_id" => $row['task_id'], 
                    "task_cost" => $row['task_cost']);
        $task_costs_array[] = $s;
    }    
}


   
function get_supplier_transport_invoices_status_array($trans_no, &$task_costs_array)
{
    $result = get_supplier_transport_invoices_task_cost_details($trans_no);
    
    while ($row = db_fetch($result)){
        $s = array("tran_type" => ST_SUPPLIER_TRANSPORT_INVOICES, 
                    "trans_no" => $row['trans_no'], 
                    "approval_status" => $row['approval_status'], 
                    "task_id" => $row['task_id'], 
                    "task_cost" => $row['task_cost']);
        $task_costs_array[] = $s;
    }    
} 
  
function get_subcontractor_invoices_status_array($trans_no, &$task_costs_array)
{
    $result = get_subcontractor_invoices_task_cost_details($trans_no);
    
    while ($row = db_fetch($result)){
        $s = array("tran_type" => ST_SUBCONTRACTOR_INVOICE, 
                    "trans_no" => $row['trans_no'], 
                    "approval_status" => $row['approval_status'], 
                    "task_id" => $row['task_id'], 
                    "task_cost" => $row['task_cost']);
        $task_costs_array[] = $s;
    }    
}    
   
function get_utility_bills_status_array($trans_no, &$task_costs_array)
{
    $result = get_utility_bills_task_cost_details($trans_no);
    
    while ($row = db_fetch($result)){
        $s = array("tran_type" => ST_UTILITY_BILLS, 
                    "trans_no" => $row['trans_no'], 
                    "approval_status" => $row['approval_status'], 
                    "task_id" => $row['task_id'], 
                    "task_cost" => $row['task_cost']);
        $task_costs_array[] = $s;
    }    
}  


function get_subcontractor_purchase_orders_status_array($trans_no, &$task_costs_array)
{
    $result = get_subcontractor_purchase_orders_task_cost_details($trans_no);
    
    while ($row = db_fetch($result)){
        $s = array("tran_type" => ST_SUBCONTRACTOR_PURCH_ORDER, 
                    "trans_no" => $row['trans_no'], 
                    "approval_status" => $row['approval_status'], 
                    "task_id" => $row['task_id'], 
                    "task_cost" => $row['task_cost']);
        $task_costs_array[] = $s;
    }    
}  


function get_subcontractor_credit_notes_status_array($trans_no, &$task_costs_array)
{
    $result = get_subcontractor_credit_notes_task_cost_details($trans_no);
    
    while ($row = db_fetch($result)){
        $s = array("tran_type" => ST_SUBCONTRACTOR_CREDIT_NOTE, 
                    "trans_no" => $row['trans_no'], 
                    "approval_status" => $row['approval_status'], 
                    "task_id" => $row['task_id'], 
                    "task_cost" => $row['task_cost']);
        $task_costs_array[] = $s;
    }    
}    

function build_task_cost_details_array($tran_type, $trans_no, &$task_costs_array)
{
    switch ($tran_type) {
        //Inventory
        case ST_MATERIAL_REQ:
            get_material_requisition_task_status_array($trans_no, $task_costs_array);
            break;
        
        case ST_INVENTORY_ISSUE_REQUEST:
            get_inventory_adjustment_request_task_status_array(ST_INVENTORY_ISSUE_REQUEST, $trans_no, $task_costs_array);
            break;
        
        //Purchasing
        case ST_PURCH_REQ:
            get_purchase_requisition_task_status_array($trans_no, $task_costs_array);
            break;  

        // Manufacturingh
        case ST_WORKORDER:
            get_work_order_task_status_array($trans_no, $task_costs_array);              
            break;
        
        // Perry Cash
        case ST_I_OWE_YOU:
            get_i_owe_you_task_status_array($trans_no, $task_costs_array);              
            break;
        case ST_I_OWE_YOU_BATCH:
            get_i_owe_you_batch_status_array($trans_no, $task_costs_array);             
            break;
        
        // Transaport
        case ST_VEHICLE_REQUEST:
            get_vehicle_request_status_array($trans_no, $task_costs_array);                             
            break;
        case ST_SUPPLIER_TRANSPORT_INVOICES:
            get_supplier_transport_invoices_status_array($trans_no, $task_costs_array);             
            break;
        
        // Projects
        case ST_SUBCONTRACTOR_INVOICE:
            get_subcontractor_invoices_status_array($trans_no, $task_costs_array);               
            break;
        case ST_UTILITY_BILLS:
            get_utility_bills_status_array($trans_no, $task_costs_array);           
            break;
        case ST_SUBCONTRACTOR_PURCH_ORDER:
            get_subcontractor_purchase_orders_status_array($trans_no, $task_costs_array);               
            break;
        case ST_SUBCONTRACTOR_CREDIT_NOTE:
            get_subcontractor_credit_notes_status_array($trans_no, $task_costs_array); 
            break;

        default:
            break;
        }
       
        add_task_details_to_array($task_costs_array);
        
}


?>