<?php

//C:\xampp\htdocs\saw\admin\db\list_values_db.inc
//C:\xampp\htdocs\saw\projects\includes\db\project_boq_items_db.inc
        
//function cs_get_boq_item_groups($project_id){
//    
//    $sql = "SELECT * FROM  ".TB_PREF."project_boq_item_groups WHERE project_id = $project_id";
//    return db_query($sql,"Could not get_boq_item_groups");
//}

function cs_get_project_task_groups($project_id)
{
    $sql = "SELECT ptg.* 
                FROM ".TB_PREF."project_task_groups ptg 
                WHERE ptg.project_id =". db_escape($project_id);          
        
	return db_query($sql,"Could not get_project_task_groups");
    
}

        
function cs_copy_budget_groups_to_boq_items()
{
    $log_filename = "copy_budget_groups_to_boq_items";
    
    write_custom_script_log($log_filename,'=======================================================');
    write_custom_script_log($log_filename,'         copy_budgets_to_boq_items Started');
    write_custom_script_log($log_filename,'=======================================================');
    
    $project_id = 0;
    if($project_id > 0){
        $dep_project = get_project_record($project_id);
        write_custom_script_log($log_filename,'Copy from Project id '.$dep_project["project_id"].' not found');  
        cs_copy_project_budget_groups_to_boq_items($log_filename, $dep_project);    
    }else{    
        $dep_res = get_all_list_values("DEPARTMENTS");
        while ($department = db_fetch($dep_res)) {
            $dep_proj_res = get_department_projects($department["id"]);
            while ($dep_project = db_fetch($dep_proj_res)) {  
                write_custom_script_log($log_filename,'Copy from Project id '.$dep_project["project_id"].' not found');  
                cs_copy_project_budget_groups_to_boq_items($log_filename, $dep_project); 
            }
        }
    }
}

function cs_copy_project_budget_groups_to_boq_items($log_filename, $project)
{

    $project_id = $project["project_id"];
    write_custom_script_log($log_filename,'Coping from Project '.$project['description']);

    $q = cs_get_project_task_groups($project_id);
    if(!(db_num_rows($q) > 0)){
        write_custom_script_log($log_filename,'No Budget Groups found in copy from project '.$project_id);
        return;
    }
    while ($task_group = db_fetch($q)) {
        $type_id = $task_group['type_id'];
        
        $display_order = 1;
        $task_group_id = $task_group['id'];
        $task_group_code = $task_group['code'];
        $task_group_description = $task_group['description'];
        $billing_type = PBT_LUMP_SUM;
        $unit = 'Nos';
        $rate = 1;
        $max_quantity =1;
        $task_group_long_description = $task_group['long_description'];
        $print_long_description = 0;
        $tax_group_id = 1;

        $group_id = $task_group['boq_group_id'];
        
        $boq_item_id = 0;
        
//        $c = get_existing_code_from_project_boq_items($project_id, $task_group_code, -1);
        $boq_item = get_project_boq_item_record($task_group_id);
        if(isset($boq_item['id']) && $boq_item['id'] > 0){
            $boq_item_id = $boq_item['id'];
            write_custom_script_log($log_filename,'Code '.$task_group_code.' already exists');
//            return;
        }
        else{
            $existing_boq_item = get_project_boq_item_record_by_code($project_id, $task_group_code);
            if(isset($existing_boq_item['id']) && $existing_boq_item['id'] > 0){ 
                $message = "A BOQ item with the code ".$task_group_code." (".$existing_boq_item['id'].") exisits in the project ".$project_id;
                write_custom_script_log($log_filename,$message);
                display_error($message);
                return;
            }
            $boq_item_id = add_project_boq_item_with_id($task_group_id, $project_id, $type_id, $display_order, $task_group_code, $task_group_description, $billing_type, $unit, $rate,  $max_quantity, 
                                $task_group_long_description, $print_long_description, $tax_group_id,  $group_id) ;

            if($boq_item_id > 0){
                $message = "Code $task_group_code added";
                write_custom_script_log($log_filename,$message);
            }
            else{
                $message = "Could not create BOQ item $task_group_code in the project ".$project_id;
                write_custom_script_log($log_filename,$message);
                display_error($message);
                return;
            }

        }
        
        if($boq_item_id > 0){
            cs_copy_tasks_to_boq_item_components($log_filename, $project_id, $task_group_id, $task_group_description);
        }
        else{
            write_custom_script_log($log_filename,"BOQ item id is zero for boq_item_id $boq_item_id and Code $task_group_code");
        }
        
//        return;
    }
}


function cs_copy_tasks_to_boq_item_components($log_filename, $project_id, $task_group_id, $task_group_description)
{
    $q = get_task_group_existing_task_records($task_group_id);
    while ($task = db_fetch($q)) {

        $task_id = $task['task_id'];        
        $task_description = $task['description'];
        $detail_type_code = $task['detail_type_code'];
        
        begin_global_transaction();
        
        $boq_component = get_boq_item_component_record_by_id($task_id);
        if(isset($boq_component['id']) && $boq_component['id'] > 0){ 
            
        }
        else{
            $primary_type = 0;
            $n = create_boq_item_component_record($task_id, $task_group_id, $detail_type_code, $primary_type);
        
            if($n > 0){
                commit_transaction();   
                write_custom_script_log($log_filename,"Component ".$task_description." (".$n.") copied");
            }
            else{
                cancel_transaction();
                $message = "Could not copy ".$task_description." (".$n.")";
                write_custom_script_log($message);
                display_error($message);

            }
        }
    }
}



        
?>