<?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 copy_boq_groups_to_items()
{
    write_custom_script_log('Start');
    
    $from_project_id = 239;
    $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 = 239;
    $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']);
    
    $value_code = 'GEN_AD_3404';        

    $q = get_boq_item_groups($from_project_id);
    if(!(db_num_rows($q) > 0)){
        write_custom_script_log('No BOQ Groups found in copy from project '.$from_project_id);
        return;
    }
    while ($row = db_fetch($q)) {
        $lv = get_listvalue_by_code('PROJECT_TASK_TYPES', $value_code);
        $type_id = $lv['id'];
        if(!($type_id > 0)){
            write_custom_script_log('Copy to Project id '.$to_project_id.' not found');
            return;
        }
        
        $display_order = $row['display_order'];
        $code = $row['code'];
        $description = $row['description'];
        $billing_type = PBT_LUMP_SUM;
        $unit = 'Nos';
        $rate = 1;
        $max_quantity =1;
        $long_description = '';
        $print_long_description = 0;
        $tax_group_id = 1;
        $group_id = 0;
        
        $c = get_existing_code_from_project_boq_items($to_project_id, $code, -1);
        if($c > 0){
            write_custom_script_log('Code '.$code.' already exists');
//            return;
        }
        else{
            $new_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($new_id > 0){
                $message = "Code $code added";
            }
            else{
                $message = "Could not insert $code";
            }

            write_custom_script_log($message);
        }
        //return;
    }
}

?>