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

function build_est_boq_groups_summary_array(&$items, $est_project_id)
{
    $loop_count = 1;
    $position = 0;
    
    $boq_groups_top_q = get_top_level_est_boq_groups($est_project_id); 
    
    while ($boq_groups_top = db_fetch($boq_groups_top_q))
    {
        $position++;
        
        $line = array();
        $line['id'] = $boq_groups_top['id']; 
        $line['code'] = $boq_groups_top['code']; 
        $line['description'] = $boq_groups_top['description']; 
        $line['parent_id'] = $boq_groups_top['parent_id']; 
        $line['est_project_id'] = $est_project_id;
        $line['total'] = $boq_groups_top['total']; 
//        $line['budgets_used_total'] = $boq_groups_top['budgets_used_total']; 
//        $line['budgets_allocated_total'] = $boq_groups_top['budgets_allocated_total']; 
        
        $line['depth'] = $loop_count;
        $line['position'] = $position;
        
        $child_groups_array = add_child_est_boq_groups_to_array($boq_groups_top['id'], $loop_count, $position);
        if($child_groups_array){
            foreach ($child_groups_array as $child_group){
                $line['total'] += $child_group['total'];
//                $line['budgets_used_total'] += $child_group['budgets_used_total'];                
//                $line['budgets_allocated_total'] += $child_group['budgets_allocated_total'];
            }      
            $line['children'] = $child_groups_array;
        }
        else{
            $line['children'] = array();
        }       

        $items[] = $line;
        
    }

}


function add_child_est_boq_groups_to_array($parent_id, &$loop_count, &$position)
{
    $loop_count++;
    if($loop_count > 20){
        //Stop infinite recursive loop
        return null;
    }
    $boq_groups_array = array();

        
    
    $child_boq_groups_q = get_child_est_boq_groups($parent_id);   
    
    while ($boq_group = db_fetch($child_boq_groups_q))
    {
        $position++;
        
        $line = array();
        $line['id'] = $boq_group['id']; 
        $line['code'] = $boq_group['code']; 
        $line['description'] = $boq_group['description']; 
        $line['parent_id'] = $boq_group['parent_id']; 
        $line['est_project_id'] = $boq_group['est_project_id']; 
        $line['total'] = $boq_group['total']; 
//        $line['budgets_used_total'] = $boq_group['budgets_used_total']; 
//        $line['budgets_allocated_total'] = $boq_group['budgets_allocated_total'];

        $line['depth'] = $loop_count;
        $line['position'] = $position;

        $child_groups_array = add_child_est_boq_groups_to_array($boq_group['id'], $loop_count, $position);
        if($child_groups_array){
            foreach ($child_groups_array as $child_group){
                $line['total'] += $child_group['total'];
//                $line['budgets_used_total'] += $child_group['budgets_used_total'];                
//                $line['budgets_allocated_total'] += $child_group['budgets_allocated_total'];
            }      
            $line['children'] = $child_groups_array;
        }
        else{
            $line['children'] = array();
        }       

        $boq_groups_array[] = $line;
    }
    
    $loop_count--;

    return $boq_groups_array;

}

function display_est_boq_item_group($boq_groups, $editable)
{
    alt_table_row_color($k);
//    label_cell($boq_groups["display_order"]);
    $depth = spaces(($boq_groups["depth"]-1) * 10);
    $name = "[".$boq_groups["code"]."] ".$boq_groups["description"];
    $s = get_estimation_trans_view_str(ST_EST_PROJECT_BOQ_ITEM_GROUP, $boq_groups['est_project_id']."|".$boq_groups["id"], $name);
        
    label_cell($depth.$s);
//    amount_decimal_cell($boq_groups["child_groups_total"]); 
//    amount_decimal_cell($boq_groups["child_groups_taxes_total"]); 
    amount_decimal_cell($boq_groups["total"]); 
//    amount_decimal_cell($boq_groups["budgets_used_total"]); 
//    amount_decimal_cell($boq_groups["budgets_allocated_total"]); 
//    amount_decimal_cell($boq_groups["total"] - ($boq_groups["budgets_used_total"] + $boq_groups["budgets_allocated_total"])); 

    if($editable == 1){
        edit_button_cell("Edit".$boq_groups['id'], _("Edit"));
        delete_button_cell("Delete".$boq_groups['id'], _("Delete"));
    }
    
    end_row();
    
    foreach ($boq_groups['children'] as $myrow) {
        display_est_boq_item_group($myrow, $editable);
    }
}





function display_est_boq_item_groups($project_id, $editable = 0){

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

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

    if($editable == 1){
        $th = array(_("Estimation BOQ Item Group"),_("Total"), "", "");  
    }
    else{
        $th = array(_("Estimation BOQ Item Group"),_("Total"));  
    }

    table_header($th);

    $k = 0; //row colour counter

//    while ($myrow = db_fetch($result))
    foreach ($boq_groups as $myrow) {
            display_est_boq_item_group($myrow, $editable);
    }

    
    display_est_boq_groups_total($project_id, $boq_groups, $editable);
    
    end_table();
    div_end();
}

function recalculate_est_boq_item_groups($est_project_id)
{
    $boq_groups_q = get_est_project_boq_item_groups($est_project_id);
    while($boq_group = db_fetch($boq_groups_q)){          
        recalculate_est_boq_item_group($boq_group['id'], $boq_group['est_project_id']);
    }
    
//    $value_total = $tax_total = 0;
//    $pa_totals = get_project_pa_boq_groups_total($pa_id);

}

function recalculate_est_boq_item_group($boq_group_id, $est_project_id)
{
    $total = 0;
    
    //$group_summary = get_boq_group_tasks_summary($boq_group_id, $project_id);
    $group_summary = get_est_boq_group_boq_items_summary($boq_group_id, $est_project_id);
       
    $total += $group_summary["total"]; 
    
    
    update_est_project_boq_group_totals($boq_group_id, $total);
}




function display_est_boq_groups_total($est_project_id, $boq_groups, $editable)
{
        
    $no_boq_group_budgets = get_est_boq_group_boq_items_summary(0, $est_project_id);

    alt_table_row_color($k);

    $name = "BOQ Items with no BOQ Item Group";
    $s = get_estimation_trans_view_str(ST_EST_PROJECT_BOQ_ITEM_GROUP, $est_project_id."|0", $name);
        
    label_cell($s);
//    amount_decimal_cell($boq_groups["child_groups_total"]); 
//    amount_decimal_cell($boq_groups["child_groups_taxes_total"]); 
    amount_decimal_cell($no_boq_group_budgets["total"]); 
//    amount_decimal_cell($no_boq_group_budgets["budget_used"]); 
//    amount_decimal_cell($no_boq_group_budgets["budget_allocated"]); 
//    amount_decimal_cell($no_boq_group_budgets["budget"] - ($no_boq_group_budgets["budget_used"] + $no_boq_group_budgets["budget_allocated"])); 

    if($editable == 1){
        label_cell("");
        label_cell("");
    }
    
    $total = $no_boq_group_budgets["total"];
//    $budgets_used_total = $no_boq_group_budgets["budget_used"];
//    $budgets_allocated_total = $no_boq_group_budgets["budget_allocated"];
    
    foreach ($boq_groups as $boq_group) {
        $total += $boq_group['total'];
//        $budgets_used_total += $boq_group['budgets_used_total'];
//        $budgets_allocated_total += $boq_group['budgets_allocated_total'];
    }
    
    start_row();
    label_cell("Total:", "align=right");
    amount_cell($total, true);
//    amount_cell($budgets_used_total, true);
//    amount_cell($budgets_allocated_total, true);
//    amount_cell($total - ($budgets_used_total + $budgets_allocated_total), true);
    end_row();
}


function can_process_est_boq_item_group($boqg)
{    
    if(!($boqg['est_project_id'] > 0 ))
    {
        $message = ( _("Please select a Project."));
        $set_focus = ('code');
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);  
    }
    $cod = $boqg['code'];  
    if(!(strlen($cod) > 0))
    {
        $message = ( _("Please enter a Code."));
        $set_focus = ('code');
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);  
    }
    $des = $boqg['description'];   
    if(!(strlen($des) > 0))
    {
        $message = ( _("Please enter Description."));
        $set_focus = ('description');
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);  
    }

    
    $count = get_existing_code_from_est_project_boq_item_groups($boqg['est_project_id'], $boqg['code'], $boqg['id']);
    if($count > 0)
    {
        $message = ( _("Entered code already exists."));
        $set_focus = ('code');
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);         
    }
    
    return array("success"=>true, "set_focus"=>"", "message"=>"");  
}


function recalculate_est_boq_item_group_positions($project_id)
{
    $boq_groups = array();
    build_est_boq_groups_summary_array($boq_groups, $project_id);
    
    update_est_boq_item_group_positions($boq_groups);
}

?>


