<?php

function display_gl_account_types_details() {
    start_table(TABLESTYLE, "width=60%");
//        $th = array(_("Group ID"), _("Group Name"), _("Subgroup Of"), _("Class"), _("Position"), _("Depth"), "", "");
        $th = array(_("Group ID"), _("Group Name"), _("Subgroup Of"), _("Class"), "", "");
            inactive_control_column($th);
            table_header($th);

            $classes = get_account_type_classes();
                while ($class = db_fetch($classes)) {
                    start_row();
                        label_cell(text_bold($class["class_id"]), 'style=background-color:#0ec9ba');
                        
                        $c = check_value('show_inactive')==true?6:5;
                        label_cell(text_bold($class["class_name"]), 'colspan='.$c.' style=background-color:#0ec9ba');
                    end_row();
            
                    $result = get_account_class_account_types($class["class_id"], false);
                    $k = 0;
                    while ($myrow = db_fetch($result)) {
                        alt_table_row_color($k);
                            $bs_text = get_account_class_name($myrow["class_id"]);

                            if ($myrow["parent"] == '-1') {
                                $parent_text = "";
                            } else {
                                $parent_text = get_account_type_name($myrow["parent"]);
                            }

                            label_cell($myrow["id"]);
                            label_cell(spaces(($myrow["depth"]) * 10).$myrow["name"]);
                            label_cell($parent_text);
                            label_cell($bs_text);
//                            label_cell($myrow["position"], 'align=center');
//                            label_cell($myrow["depth"], 'align=center');
                            inactive_control_cell($myrow["id"], $myrow["inactive"], 'chart_types', 'id');

                            edit_button_cell("Edit".$myrow["id"], _("Edit"));
                            delete_button_cell("Delete".$myrow["id"], _("Delete"));
                        end_row();
                    }
                }

            inactive_control_row($th);
    end_table(2);
}

function display_gl_account_types_control(&$Mode, $selected_id) {
    start_table(TABLESTYLE2);

        if ($selected_id != "") {
            if ($Mode == 'Edit') {
                //editing an existing status code
                $myrow = get_account_type($selected_id);
                    $_POST['id']  = $myrow["id"];
                    $_POST['name']  = $myrow["name"];
                    $_POST['parent']  = $myrow["parent"];
                    if ($_POST['parent'] == '-1') {
                        $_POST['parent'] == "";
                    }
                    $_POST['class_id']  = $myrow["class_id"];
                    
                    hidden('selected_id', $myrow['id']);
                    hidden('old_id', $myrow["id"]);
            } else {
                hidden('selected_id', $selected_id);
                hidden('old_id', $_POST["old_id"]);
            }	
        }
    
        text_row_ex(_("ID:"), 'id', 10);
        text_row_ex(_("Name:"), 'name', 50);
        gl_account_types_list_row(_("Subgroup Of:"), 'parent', null, _("None"), true);
        class_list_row(_("Class:"), 'class_id', null);

    end_table(3);
}

function can_process($selected_id) {
    if (strlen(trim($_POST['id'])) == 0) {
        display_error( _("The account group id cannot be empty."));
        set_focus('id');
        return false;
    }
    
    if (strlen(trim($_POST['name'])) == 0) {
        display_error(_("The account group name cannot be empty."));
        set_focus('name');
        return false;
    }
    
    $type = get_account_type(trim($_POST['id']));
	if ($type && ($type['id'] != $selected_id)) {
            display_error( _("This account group id is already in use."));
            set_focus('id');
            return false;
	}

    if ($_POST['id'] === $_POST['parent']) {
        display_error(_("You cannot set an account group to be a subgroup of itself."));
        return false;
    }

	return true;
}

function can_delete($type) {
    if ($type == "") {
        return false;
    }

    if (key_in_foreign_table($type, 'chart_master', 'account_type')) {
        display_error(_("Cannot delete this account group because GL accounts have been created referring to it."));
        return false;
    }

    if (key_in_foreign_table($type, 'chart_types', 'parent')) {
        display_error(_("Cannot delete this account group because GL account groups have been created referring to it."));
        return false;
    }

	return true;
}
    
function build_calculate_upadte_gl_account_types() {
    $gl_account_types_depth_position_array = array();
    $position_value = 0;
    
        $classes = get_account_type_classes();
            while ($class = db_fetch($classes)) {
                $depth_value = -1;
                $class_id = $class["class_id"];
                $parent = '';
                $position_value += 1;
                
//                $gl_account_types_depth_position_array[$id] = array('id' => $id, 'position' => $position_value, 'depth' => $depth_value);

                $msg = build_class_gl_account_types_array($class_id, $parent, $gl_account_types_depth_position_array, $position_value, $depth_value);
                if($msg["success"] !== true){
                    display_error($msg["message"]);
                    return;
                }
//                break;
            }

            update_gl_account_types_position_depth($gl_account_types_depth_position_array);
}

function build_class_gl_account_types_array($class_id, $parent, &$gl_account_types_depth_position_array, &$position_value, $depth_value) {
    $depth_value += 1;
    
    if ($depth_value > 10) {
        return array("success"=>false, "message"=>"Maximum limit of 10 sub levels exceeded.");
    }
    
    $result = get_account_type_children($class_id, $parent);
        while ($row = db_fetch($result)) {
            $id = $row['id'];
            $position_value += 1;

            $gl_account_types_depth_position_array[$id] = array('id' => $id, 'position' => $position_value, 'depth' => $depth_value);

            $msg = build_class_gl_account_types_array($row["class_id"], $id, $gl_account_types_depth_position_array, $position_value, $depth_value);
            
            if($msg["success"] !== true){
                return $msg;
            }
        }
            
        $depth_value -=1;
        
        return array("success"=>true, "message"=>"");
        
}

//function build_class_gl_account_types_array($class_id, $parent, &$gl_account_types_depth_position_array, &$position_value) {
//    $depth_value = 0;
//    
//    $result = get_account_types_by_filters(null, $class_id, $parent, 'id');
//        while ($row = db_fetch($result)) {
//            $id = $row['id'];
//            $position_value += 1;
//            $depth_value += 1;
//
//            $gl_account_types_depth_position_array[$id] = array('id' => $id, 'position' => $position_value, 'depth' => $depth_value);
//
//            build_class_gl_account_types_array($row["class_id"], $row["parent"], $gl_account_types_depth_position_array, $position_value);
//        }
//}

function get_gl_account_types_record_depth($parent_id, &$depth_value) {
    
    $rec = get_account_type($parent_id);
        if (is_array($rec)) {
            $depth_value += 1;
            
            get_gl_account_types_record_depth($rec['parent'], $depth_value);
        }
}

function update_gl_account_types_position_depth($gl_account_types_depth_position_array) {
    
    foreach ($gl_account_types_depth_position_array as $key => $val) {
        update_gl_account_types_position_depth_by_id($val['id'], $val['position'], $val['depth']);
    }
}










//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 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);
//}

?>