<?php

//C:\xampp\htdocs\saw\admin\db\list_values_db.inc
//C:\xampp\htdocs\saw\projects\includes\db\project_boq_items_db.inc
        
function 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 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 copy_task_groups_to_boq_items()
{
    write_custom_script_log('=======================================================');
    write_custom_script_log('         copy_budgets_to_boq_items Started');
    write_custom_script_log('=======================================================');
    
    $from_project_id = 107;
    $from_project = get_project($from_project_id);
    if(!($from_project['project_id'] > 0)){
        write_custom_script_log('Copy from Project id '.$from_project_id.' not found');
        return;
    }
    write_custom_script_log('Coping from Project '.$from_project['description']);
    
    $to_project_id = 107;
    $to_project = get_project($to_project_id);
    if(!($to_project['project_id'] > 0)){
        write_custom_script_log('Copy to Project id '.$to_project_id.' not found');
        return;
    }
    write_custom_script_log('Coping to Project '.$to_project['description']);       

    $q = get_project_task_groups($from_project_id);
    if(!(db_num_rows($q) > 0)){
        write_custom_script_log('No Budget Groups found in copy from project '.$from_project_id);
        return;
    }
    while ($row = db_fetch($q)) {

        $type_id = $row['type_id'];
        
        $display_order = 1;
        $code = $row['code'];
        $description = $row['description'];
        $billing_type = PBT_LUMP_SUM;
        $unit = 'Nos';
        $rate = 1;
        $max_quantity =1;
        $long_description = $row['long_description'];
        $print_long_description = 0;
        $tax_group_id = 1;
        $group_id = $row['boq_group_id'];
        
        $c = get_existing_code_from_project_boq_items($to_project_id, $code, -1);
        if(isset($c['id']) && $c['id'] > 0){
            $boq_item_id = $c['id'];
            write_custom_script_log('Code '.$code.' already exists');
//            return;
        }
        else{
            $boq_item_id = add_project_boq_item($to_project_id, $type_id, $display_order, $code, $description, $billing_type, $unit, $rate,  $max_quantity, 
                                    $long_description, $print_long_description, $tax_group_id,  $group_id) ;

            if($boq_item_id > 0){
                $message = "Code $code added";
            }
            else{
                $message = "Could not insert $code";
            }

            write_custom_script_log($message);
        }
        
        if($boq_item_id > 0){
            copy_tasks_to_boq_item_components($from_project_id, $row['id'], $to_project_id, $boq_item_id, $description);
        }
        else{
            write_custom_script_log("BOQ item id is zero for boq_item_id $boq_item_id and Code $code");
        }
        
        return;
    }
}


function copy_tasks_to_boq_item_components($from_project_id, $task_group_id, $to_project_id, $boq_item_id, $boq_item_description)
{
    $q = get_task_group_tasks($from_project_id, $task_group_id);
    while ($row = db_fetch($q)) {
        begin_transaction();
        
        $boq_cost = get_boq_item_component_record($boq_item_id, $row['detail_type_code']);
        if(isset($boq_cost['id']) && $boq_cost['id'] > 0){
            $selected_id = $boq_cost['id'];
        }
        else{
            $selected_id = -1;
        }
        $sbp_msg = save_boq_item_cost($selected_id, $boq_item_id, $row['detail_type_code'], 
                                    $boq_item_description, input_num('boq_cost_cost'), '');   
        if($sbp_msg['success'] === true){
            commit_transaction();
            $selected_id = -1;            
            write_custom_script_log("Cost saved boq item ".$boq_item_id." - ".$boq_item_description);
        }
        else{
            cancel_transaction();
            write_custom_script_log("Could not save save_boq_item_cost for boq_item ".$boq_item_id." - ".$boq_item_description);
            
        }

    }
}



        
?>