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


function hangle_display_project_boq_item_groups($from_project_id, $to_project_id, $copy_type, &$line_count){

    global $Ajax;
    
    if(!($from_project_id > 0)){
        display_error("Please select From Project.");
        return;        
    }
    if(!($to_project_id > 0)){
        display_error("Please select To Project.");
        return;        
    }
    
    
    $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');
    
    $to_boq_result = get_project_boq_item_group_records($to_project_id);
    if(db_num_rows($to_boq_result) > 0){
        $msg = 'WARNING : "To Project" has already created BOQ Item Groups. Copying new BOQ Item Groups can distort the "To Project" BOQ Item Group positions. You will need to manually update these positions.';
//        display_note($msg);
        display_note($msg, 0, 0, "style=color:red");
        br(2);
    }

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


    $th = array(_("From BOQ Item Group"), _("From Group Parent"), _("To BOQ Item Group"), _("Copy"));   
    

    table_header($th);

    $cost_total = 0;
//    $k = 0; //row colour counter
        
    display_boq_item_groups($from_project_id, $to_project_id, $cost_total, $copy_type, $line_count);

    end_table();
    div_end();
    
    
}


function display_boq_item_groups($from_project_id, $to_project_id, &$cost_total, $copy_type, &$component_count)
{
    global $project_boq_item_component_primary_types;
    
    $result = get_project_boq_item_groups_with_parent($from_project_id);
        
    $k = 0;
    
    while ($row = db_fetch($result)) {
        
        $depth = spaces(($row["depth"] - 1) * 5);
        
        $boq_group_id = $row["id"];
        $boq_group_code = $row["code"];
        $boq_group_description = $row["description"];
        $parent_code = $row["parent_code"];
         
            start_row();               
                $from_boq_item_group_name = $boq_group_code." - ".$boq_group_description." (".$boq_group_id.")";
                label_cell($depth." ".text_bold($from_boq_item_group_name), 'colspan=1');  
                label_cell(text_bold($parent_code), 'colspan=1');  
                
                display_to_boq_item_group($from_project_id, $boq_group_id, $to_project_id, $boq_group_code);
                
            end_row();

        
        $component_count++;
        
        alt_table_row_color($k);
        $k = 0;


    }
 
    
    end_row();
    

}

function display_to_boq_item_group($from_project_id, $from_boq_group_id, $to_project_id, $from_boq_group_code)
{
    $depth ="";
    $can_copy = $new = false;
    $error_message = ""; 
    
    $to_group = get_project_boq_item_group_record_by_code_with_parent($to_project_id, $from_boq_group_code);
    if(!(isset($to_group['id']) && $to_group['id'] > 0)){
        $to_boq_item_group_name = "No matching BOQ Item Group found.";
//        $to_boq_item_group_parent_code = "";
        $can_copy = true;
        $new = true;
    }else{
        $depth = spaces(($to_group["depth"] - 1) * 5);
        $to_boq_item_group_name = $to_group["code"]." - ".$to_group["description"]." (".$to_group['id'].")";
//        $to_boq_item_group_parent_code = $to_group["parent_code"];
    }
    
    if($new){
        label_cell($depth." ".$to_boq_item_group_name, 'colspan=1'); 
//        label_cell($to_boq_item_group_parent_code, 'colspan=1'); 
    }else{
        label_cell($depth." ".text_bold($to_boq_item_group_name), 'colspan=1');         
//        label_cell(text_bold($to_boq_item_group_parent_code), 'colspan=1'); 
    }
    
//    if($can_copy && $to_boq_item_group_parent_code != ""){
//        if($to_boq_item_group_parent_code != $from_boq_group_code){
//            $error_message = "Parents not matching";
//            $can_copy = false;
//        }
//    }
    
    $check_name = "check_".$from_project_id."_".$from_boq_group_id;
    if($can_copy){
//        set_post_not_set_numeric($check_name, 1);
        check_cells("", $check_name, null, false, false, "align=center");
    }else{
        label_cell($error_message);
    }
}



function copy_selected_boq_item_groups($from_project_id, $to_project_id)
{
    $from_result = get_project_boq_item_groups_with_parent($from_project_id);
        
//    $a = $u = 0;
    
    while ($from_group = db_fetch($from_result)) {       
            
        $from_group_id = $from_group["id"];
        $from_group_code = $from_group["code"];
        
        $check_name = "check_".$from_project_id."_".$from_group_id;     
        
        if(isset($_POST[$check_name]) && $_POST[$check_name] == true){
            $from_group_description = $from_group["description"];
//            $from_group_display_order = $from_group["display_order"];
//            $from_group_depth = $from_group["depth"];
//            $from_group_parent_id = $from_group["parent_id"];
//            $from_group_parent_code = $from_group["parent_code"];
//            $from_group_position = $from_group["position"];
         
//            $from_parent_group = get_project_boq_item_group_record($from_parent_id);
//            $to_group_parent_code = $from_group_parent_code;
            
//            $to_parent_id = 0;
//            if($from_group_parent_id > 0){
//                $to_parent_group = get_project_boq_item_group_record_by_code($to_project_id, $to_group_parent_code);
//                if(isset($to_parent_group["id"]) && $to_parent_group["id"] > 0){
//                    $to_parent_id = $to_parent_group["id"];
//                }else{ 
//                    $message = "BOQ Item Group ".$from_group_code." : Parent BOQ Item Group [".$to_group_parent_code."] not found in the To Project.";
//                    return array("success"=>false, "message"=>$message); 
//                }
//            }
                        
            $to_group = get_project_boq_item_group_record_by_code($to_project_id, $from_group_code); 
            if(isset($to_group["id"]) && $to_group["id"] > 0){
//                update_project_boq_item_groups($to_group["id"], $from_group_code, $from_group_description, $from_group_display_order, $to_parent_id, $from_group_depth);
//                $u++;
                
                continue;
            } else {                 
                $ins_id = add_project_boq_item_groups($to_project_id, $from_group_code, $from_group_description, 0, 0, 0);
                    if (! ($ins_id > 0)) {
                        return array('success' => false, 'message' => "Could not add project BOQ item group code $from_group_code.");
                    }
//                $a++;
            }
        }

    }

    $message = " BOQ Item Groups added.";
    return array("success"=>true, "message"=>$message);
    
}


function handle_copy_BOQ_group($from_project_id, $to_project_id){
    
    $msg = copy_selected_boq_item_groups($from_project_id, $to_project_id);
    
    return $msg;
    
}