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

function build_boq_groups_summary_array_new(&$items, $project_id)
{
    $loop_count = 1;
    $position = 0;
    
    $boq_groups_top_q = get_project_boq_item_groups($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['item_description']; 
        $line['parent_id'] = $boq_groups_top['parent_id']; 
        $line['project_id'] = $project_id;
        $line['budgets_total'] = $boq_groups_top['budgets_total']; 
        $line['budgets_used_total'] = $boq_groups_top['budgets_used_total']; 
        $line['budgets_allocated_total'] = $boq_groups_top['budgets_allocated_total']; 
        
        $line['depth'] = $boq_groups_top['depth']; 
        $line['position'] = $boq_groups_top['position']; 
        
//        $child_groups_array = add_child_boq_groups_to_array($boq_groups_top['id'], $loop_count, $position);
//        if($child_groups_array){
//            foreach ($child_groups_array as $child_group){
//                $line['budgets_total'] += $child_group['budgets_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();
//        }       

        $line['children'] = array();
        $items[] = $line;
        
    }

}


function build_boq_groups_summary_array(&$items, $project_id)
{
    $loop_count = 1;
    $position = 0;
    
    $boq_groups_top_q = get_top_level_boq_groups($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['project_id'] = $project_id;
        $line['budgets_total'] = $boq_groups_top['budgets_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_boq_groups_to_array($boq_groups_top['id'], $loop_count, $position);
        if($child_groups_array){
            foreach ($child_groups_array as $child_group){
                $line['budgets_total'] += $child_group['budgets_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_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_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['project_id'] = $boq_group['project_id']; 
        $line['budgets_total'] = $boq_group['budgets_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_boq_groups_to_array($boq_group['id'], $loop_count, $position);
        if($child_groups_array){
            foreach ($child_groups_array as $child_group){
                $line['budgets_total'] += $child_group['budgets_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_boq_item_group($boq_groups, $editable)
{
    alt_table_row_color($k);
    
    if($editable == 1){
        label_cell($boq_groups["id"], "align=center");
    }
    
//    $depth = spaces(($boq_groups["depth"]-1) * 10);
    $depth = spaces(($boq_groups["depth"]) * 10);
    
    $name = "[".$boq_groups["code"]."] ".$boq_groups["description"];
    $s = get_project_trans_view_str(ST_PROJECT_BOQ_ITEM_GROUP, $boq_groups['project_id']."|".$boq_groups["id"], $name);
        
    label_cell($depth.$s);
    
    
//    label_cell($s);
//    amount_decimal_cell($boq_groups["child_groups_total"]); 
//    amount_decimal_cell($boq_groups["child_groups_taxes_total"]); 
    amount_decimal_cell($boq_groups["budgets_total"]); 
    amount_decimal_cell($boq_groups["budgets_used_total"]); 
    amount_decimal_cell($boq_groups["budgets_allocated_total"]); 
    amount_decimal_cell($boq_groups["budgets_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_boq_item_group($myrow, $editable);
    }
}





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

    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=90%");

    if($editable == 1){
        $th = array("Id", _("BOQ Item Group"),_("Total Budgets"), _("Total Used"), _("Total Allocated"), "Total Available", "", "");  
    }
    else{
        $th = array(_("BOQ Item Group"),_("Total Budgets"), _("Total Used"), _("Total Allocated"), "Total Available");  
    }

    table_header($th);

    $k = 0; //row colour counter

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

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

function recalculate_boq_item_groups($project_id)
{
    $boq_groups_q = get_project_boq_item_groups($project_id, true);
    while($boq_group = db_fetch($boq_groups_q)){          
        recalculate_boq_item_group($boq_group['id'], $boq_group['project_id']);
    }
    
//    $value_total = $tax_total = 0;
//    $pa_totals = get_project_pa_boq_groups_total($pa_id);

}

function recalculate_boq_item_group($boq_group_id, $project_id)
{
    $budgets_total = $budgets_used_total = $budgets_allocated_total = 0;
    
//    if($boq_group_id == 184){
//        $dd = 2;
//    }
    $tasks_summary = get_boq_group_tasks_summary($boq_group_id, $project_id);       
    $budgets_total += $tasks_summary["budget"];
    $budgets_used_total += $tasks_summary["budget_used"];
    $budgets_allocated_total += $tasks_summary["budget_allocated"];
    
    $group_summary = get_child_boq_groups_budgets_summary($boq_group_id, $project_id);
    $budgets_total += $group_summary["budgets_total"];
    $budgets_used_total += $group_summary["budgets_used_total"];
    $budgets_allocated_total += $group_summary["budgets_allocated_total"];
    
    update_project_boq_group_totals($boq_group_id, $budgets_total, $budgets_used_total, $budgets_allocated_total);
}




function display_boq_groups_total($project_id, $boq_groups, $editable)
{
        
    $no_boq_group_budgets = get_no_boq_group_tasks_summary($project_id);

    alt_table_row_color($k);

    if($editable == 1){
        label_cell("");
    }
    $name = "Budgets with no BOQ Item Group";
    $s = get_project_trans_view_str(ST_PROJECT_BOQ_ITEM_GROUP, $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["budget"]); 
    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("");
    }
    
    $budgets_total = $no_boq_group_budgets["budget"];
    $budgets_used_total = $no_boq_group_budgets["budget_used"];
    $budgets_allocated_total = $no_boq_group_budgets["budget_allocated"];
    
    
//    foreach ($boq_groups as $boq_group) {
//        $budgets_total += $boq_group['budgets_total'];
//        $budgets_used_total += $boq_group['budgets_used_total'];
//        $budgets_allocated_total += $boq_group['budgets_allocated_total'];
//    }
    
    $boq_groups_top_q = get_top_level_boq_groups($project_id); 
    
    while ($boq_groups_top = db_fetch($boq_groups_top_q))
    {
        $budgets_total += $boq_groups_top['budgets_total'];
        $budgets_used_total += $boq_groups_top['budgets_used_total'];
        $budgets_allocated_total += $boq_groups_top['budgets_allocated_total'];
    }
    
    start_row();
    label_cell("");
    label_cell("Total:", "align=right");
    amount_cell($budgets_total, true);
    amount_cell($budgets_used_total, true);
    amount_cell($budgets_allocated_total, true);
    amount_cell($budgets_total - ($budgets_used_total + $budgets_allocated_total), true);
    end_row();
}

function can_process_project_boq_item_group($boq_group)
{    
    if(!($boq_group['project_id'] > 0 ))
    {
        $message = ( _("Please Select a Project."));
        $set_focus = ('project_id');
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);  
    }
        
        
    if($boq_group['code'] == '')
    {
        $message = ( _("Enter a code."));
        $set_focus = ('code');
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);  
    }     
    
    if ($boq_group['description'] == '')
    {
        $message = ( _("Enter description."));
        $set_focus = ('description');
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message); 
    }     
    
    $count = get_existing_code_from_project_boq_item_groups($boq_group['project_id'], $boq_group['code'], $boq_group['id']);
    if($count > 0)
    {
       $message = display_error("Entered code already exists.");
       $set_focus = set_focus('code');
       return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);       
    }
    
    if($boq_group['id'] > 0 && $boq_group['parent_id'] ==  $boq_group['id'])
    {
       $message = ("Can't use the same group as its parent.");
       $set_focus = ('parent_id');
       return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);      
    }    
    
    return array("success"=>true, "set_focus"=>'', "message"=>'');   
}


function recalculate_boq_item_group_positions($project_id)
{
    $boq_groups = array();
    build_boq_groups_summary_array($boq_groups, $project_id);
    
    update_boq_item_group_positions($boq_groups);
}

?>