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




function display_project_budget_status_view($project_id){

    global $Ajax;
    
    $Ajax->activate('price_table');
    
    $boq_groups = array();
//    build_boq_groups_summary_array($boq_groups, $project_id);
    build_boq_groups_summary_array_new($boq_groups, $project_id);
    
    div_start('price_table');

    start_table(TABLESTYLE, "width=98%");


    $th = array(_("BOQ Item Group"),_("Budget"), _("Used"), _("Allocated"), "Available", "%", "New Budget");  
    

    table_header($th);

    $budgets_total = $used_total = $allocated_total = $new_budgets_total = 0;
    $k = 0; //row colour counter

//    while ($myrow = db_fetch($result))
    foreach ($boq_groups as $myrow) {
        $depth = spaces(($myrow["depth"]) * 10);
    
        $name = "[".$myrow["code"]."] ".$myrow["description"];
        $s = get_project_trans_view_str(ST_PROJECT_BOQ_ITEM_GROUP, $myrow['project_id']."|".$myrow["id"], $name);

//        label_cell($depth.text_bold($name), 'colspan=5');
        label_cell($depth.text_bold($name));
            
        display_boq_item_group_budgets($myrow, $budgets_total, $used_total, $allocated_total, $new_budgets_total);
    }

    
    display_no_boq_item_group_budgets($project_id, $budgets_total, $used_total, $allocated_total, $new_budgets_total);
    
    start_row();
    label_cell(text_bold("Total"));
        
    amount_cell($budgets_total, true); 
    amount_cell($used_total, true); 
    amount_cell($allocated_total, true); 
    amount_cell($budgets_total - ($used_total + $allocated_total), true); 
    label_cell("");
    amount_cell($new_budgets_total); 
    end_row();
    end_table();
    div_end();
}


function display_boq_item_group_budgets($boq_group, &$budgets_total, &$used_total, &$allocated_total, &$new_budgets_total)
{
    $project_id = $boq_group["project_id"];
    $boq_group_id = $boq_group["id"];
    $result = get_boq_item_group_budget_details($project_id, $boq_group_id);
        
    $depth = spaces(($boq_group["depth"]+1) * 10);
    
    $budget = $used = $allocated = 0;
    $k = 0;
    while ($row = db_fetch($result)) {
        alt_table_row_color($k);

        $name = "[".$row["task_code"]."] ".$row["description"];
        
        label_cell($depth.$name);
        
        $budget = $row["budget"];
        $used = $row["budget_used"];
        $allocated = $row["budget_allocated"]; 
        $new_budget = $row["new_budget"]; 
        
        amount_decimal_cell($budget); 
        
        $used_link = get_task_cost_details_view_str($row['task_id'], number_format($used,2));
        label_cell($used_link, 'align=right '); 
        
        $allocation_link = get_task_cost_allocation_details_view_str($row['task_id'], number_format($allocated,2));
        label_cell($allocation_link, 'align=right ');
        
        $available = $budget - ($used + $allocated);
//        amount_decimal_cell($available); 
        label_cell(number_format($available,2), 'align=right '); 
        
        $percentage = 0;
        $percentage_text = 0.00;
        $colour = "";
        if($budget > 0){
            $percentage = $available/$budget*100;
        
        
            $rgb = get_background_colour($percentage);
            if($rgb){
                $colour = "style=".$rgb;
                $percentage_text = text_bold(number_format($percentage,2));
            }else{
                $colour = "";
                $percentage_text = number_format($percentage,2);
            }
        }
         
        label_cell($percentage_text, 'align=right '.$colour);  
        
        if($new_budget == $budget){
            label_cell("<font color='lightgreen'>".number_format($new_budget,2)."</font>", 'align=right foreground-color=red '); 
        }else{
            label_cell(number_format($new_budget,2), 'align=right '); 
        }
        
        
        $budgets_total += $budget;
        $used_total += $used;
        $allocated_total += $allocated;         
        $new_budgets_total += $new_budget; 
    }
    


    
    end_row();
    

}

function display_no_boq_item_group_budgets($project_id, &$budgets_total, &$used_total, &$allocated_total, &$new_budgets_total)
{
        
    $name = "Budgets with no BOQ Item Group";
//    label_cell(text_bold($name), 'colspan=5');
    label_cell(text_bold($name));
    
    $result = get_boq_item_group_budget_details($project_id, 0);
        
    $depth = spaces(10);    
    
    $budget = $used = $allocated = 0;
    $k = 0;
    while ($row = db_fetch($result)) {
        alt_table_row_color($k);

        $name = "[".$row["task_code"]."] ".$row["description"];
        
        label_cell($depth.$name);
        
        $budget = $row["budget"];
        $used = $row["budget_used"];
        $allocated = $row["budget_allocated"]; 
        $new_budget = $row["new_budget"]; 
        
        amount_decimal_cell($budget); 
        $used_link = get_task_cost_details_view_str($row['task_id'], number_format($used,2));
        label_cell($used_link, 'align=right '); 
        
        $allocation_link = get_task_cost_allocation_details_view_str($row['task_id'], number_format($allocated,2));
        label_cell($allocation_link, 'align=right ');
        
        $available = $budget - ($used + $allocated);
//        amount_decimal_cell($available);         
        label_cell(number_format($available,2), 'align=right '); 
        
        $percentage = $available/$budget*100;
        $rgb = get_background_colour($percentage);
        if($rgb){
            $colour = "style=".$rgb;
            $percentage_text = text_bold(number_format($percentage,2));
        }else{
            $colour = "";
            $percentage_text = number_format($percentage,2);
        }
        label_cell($percentage_text, 'align=right '.$colour); 
        
        if($new_budget == $budget){
            label_cell("<font color='lightgreen'>".number_format($new_budget,2)."</font>", 'align=right foreground-color=red '); 
        }else{
            label_cell(number_format($new_budget,2), 'align=right '); 
        }
        
        $budgets_total += $budget;
        $used_total += $used;
        $allocated_total += $allocated; 
        $new_budgets_total += $new_budget; 
    }
}


function get_background_colour($value)
{
    $colour = '';
    if($value < 0){
        $colour = "'background-color:rgb(138,141,143)'";
    }
    elseif($value <= 5){
        $colour = "'background-color:rgb(204, 0, 0)'";
    }
    elseif($value <= 10){
        $colour = "'background-color:rgb(255, 80, 80)'";
    }
    elseif($value <= 20){
        $colour = "'background-color:rgb(255, 102, 102)'";
    }
    elseif($value <= 30){
        $colour = "'background-color:rgb(255, 153, 153)'";
    }
    elseif($value <= 40){
        $colour = "'background-color:rgb(255, 204, 204)'";
    }
    elseif($value <= 50){
        $colour = "'background-color:rgb(255, 255, 204)'";
    }
    else{
        
    }
    
    return $colour;
}