<?php
/**********************************************************************
***********************************************************************/

function copy_est_project_to_another_est_project(&$from_est_project_id, $filtering_est_group_id, &$to_est_project_id)
{
//    $result = get_project_est_boq_item_group_records($from_est_project_id);
    $from_est_boq_item_group = get_est_project_boq_item_group_record($filtering_est_group_id);
    
//    while($from_est_boq_item_group = db_fetch($result)){
        $to_project_boq_group = get_est_project_boq_item_group_record_by_code($to_est_project_id, $from_est_boq_item_group['code']);
    
        if(isset($to_project_boq_group['id']) && $to_project_boq_group['id'] > 0){
            $to_est_boq_item_group['id'] = $to_project_boq_group['id'];
        }
        else{
            $to_est_boq_item_group['id'] = -1;
        }
        
        $to_est_boq_item_group['est_project_id'] = $to_est_project_id;
        $to_est_boq_item_group['code'] = $from_est_boq_item_group['code'];
        $to_est_boq_item_group['description'] = $from_est_boq_item_group['description'];
        $to_est_boq_item_group['display_order'] = $from_est_boq_item_group['display_order'];
        $to_est_boq_item_group['parent_id'] = 0;
    
        $cpboqig_msg = can_process_est_boq_item_group($to_est_boq_item_group);
        
        if($cpboqig_msg["success"] !== true){
            return $cpboqig_msg;
        }

        $msg = save_est_boq_group_to_another_est_boq_group($to_est_boq_item_group, $to_est_project_id);
        if($msg["success"] !== true){
            return $msg;
        }
//    }
    
    return copy_est_project_boq_item_to_another_est_project_boq_item($from_est_project_id, $filtering_est_group_id, $to_est_project_id);
    
}


function copy_est_project_boq_item_to_another_est_project_boq_item($from_est_project_id, $filtering_est_group_id, $to_est_project_id)
{
    $from_est_boq_group = get_est_project_and_another_est_project_boq_item_group($from_est_project_id, $filtering_est_group_id, $to_est_project_id);
    
//    while($from_est_boq_group = db_fetch($result))
//    {
        $result = get_est_project_group_boq_items($from_est_project_id, $from_est_boq_group['from_est_boq_group_id']);
        
        while($from_est_boq_item = db_fetch($result))
        {
            $to_est_project_boq_item = get_est_project_boq_item_record_by_code($to_est_project_id, $from_est_boq_item['code']);
            
            if(isset($to_est_project_boq_item['id']) && $to_est_project_boq_item['id'] > 0){
                $to_est_boq_item['id'] = $to_est_project_boq_item['id'];
            }
            else{
                $to_est_boq_item['id'] = -1;
            }
            
            $to_est_boq_item['est_project_id'] = $to_est_project_id;
            $to_est_boq_item['display_order'] = $from_est_boq_item['display_order'];
            $to_est_boq_item['type_id'] = $from_est_boq_item['type_id'];
            $to_est_boq_item['code'] = $from_est_boq_item['code'];
            $to_est_boq_item['description'] = $from_est_boq_item['description'];
            $to_est_boq_item['billing_type'] = $from_est_boq_item['billing_type'];
            $to_est_boq_item['rate'] = $from_est_boq_item['rate'];
            $to_est_boq_item['unit'] = $from_est_boq_item['unit'];
            $to_est_boq_item['max_quantity'] = $from_est_boq_item['max_quantity'];
            $to_est_boq_item['long_description'] = $from_est_boq_item['long_description'];
            $to_est_boq_item['print_long_description'] = $from_est_boq_item['print_long_description'];
            $to_est_boq_item['tax_group_id'] = $from_est_boq_item['tax_group_id'];
            $to_est_boq_item['calculated_cost'] = $from_est_boq_item['calculated_cost'];
            $to_est_boq_item['group_id'] = $from_est_boq_group['to_est_boq_group_id'];
            
            $cpboqig_msg = can_process_est_boq_item($to_est_boq_item);

            if($cpboqig_msg["success"] !== true){
                return $cpboqig_msg;
            }
            
            $to_est_boq_item_id = save_est_boq_item_to_another_est_boq_item($to_est_boq_item, $to_est_project_id);
            if(!($to_est_boq_item_id > 0)){
                return array("success" => false, "message"=>"Could not save Estimation BOQ item ".$to_est_boq_item['code']." - ".$to_est_boq_item['description']); 
            }
            
            $to_est_boq_item['boq_item_id'] = $to_est_boq_item_id;
            
            $cepc_msg = copy_est_project_component_to_another_est_project_component($to_est_boq_item, $from_est_boq_item['id']);
            if($cepc_msg["success"] !== true){
                   return $cepc_msg;
            }
        }
//    }
    
    return array("success" => true, "message"=>""); 
}
    
    
function copy_est_project_component_to_another_est_project_component($to_est_boq_item, $from_est_boq_item_id)
{
    $to_est_boq_item_id = $to_est_boq_item['boq_item_id'];
    
    $result = get_est_boq_item_components($from_est_boq_item_id);

        while($from_est_boq_item_component = db_fetch($result))
        {
            $to_est_component = get_est_boq_item_component_record($to_est_boq_item_id, $from_est_boq_item_component['detail_type_code']);

            if(isset($to_est_component['id']) && $to_est_component['id'] > 0){
                $to_est_boq_item_component['id'] = $to_est_component['id'];
            }
            else{
                $to_est_boq_item_component['id'] = -1;
            }

            $to_est_boq_item_component['boq_item_id'] = $to_est_boq_item_id;
            $to_est_boq_item_component['detail_type_code'] = $from_est_boq_item_component['detail_type_code'];
            $to_est_boq_item_component['primary_type'] = $from_est_boq_item_component['primary_type'];
            $to_est_boq_item_component['cost'] = $from_est_boq_item_component['cost'];
            $to_est_boq_item_component['row_state'] = $from_est_boq_item_component['row_state'];


//                $cpboqig_msg = can_process_boq_item($boq_item);
//
//                if($cpboqig_msg["success"] !== true){
//                    return $cpboqig_msg;
//                }

            $to_est_boq_component_id = save_est_boq_item_component_to_another_est_boq_item_component($to_est_boq_item_component);
            if(!($to_est_boq_component_id > 0)){
                return array("success" => false, "message"=>"Could not save Estimation BOQ item component ".$to_est_boq_item_component['detail_type_code']
                                                            ." of BOQ item ".$to_est_boq_item['code']." - ".$to_est_boq_item['description']); 
            }

            $cepd_msg = copy_est_project_component_details_to_another_est_project_component_details($from_est_boq_item_component['id'],
                                                                                                    $from_est_boq_item_component['boq_item_id'], 
                                                                                                    $from_est_boq_item_component['detail_type_code'], 
                                                                                                    $to_est_boq_item_id);
            if($cepd_msg["success"] !== true){
                   return $cepd_msg;
            }
        }
        
        return array("success" => true, "message"=>""); 
           
}


function copy_est_project_component_details_to_another_est_project_component_details($from_est_boq_component_id,$from_est_boq_item_id, 
                                                                                     $from_est_detail_type_code, $to_est_boq_item_id)
{
    $row = get_est_boq_item_component_record_by_id($from_est_boq_component_id);
           
        switch ($row['primary_type']) {
            case BOQPT_COST:
                $sboqic_msg = save_est_project_boq_item_costs($from_est_boq_item_id, $from_est_detail_type_code, $to_est_boq_item_id);
                if($sboqic_msg["success"] !== true){
                    return $sboqic_msg;
                }
                break;

            case BOQPT_MATERIAL:
                $sboqim_msg = save_est_project_boq_item_materials($from_est_boq_item_id, $from_est_detail_type_code, $to_est_boq_item_id);
                if($sboqim_msg["success"] !== true){
                    return $sboqim_msg;
                }
                break;
    
            case BOQPT_LABOUR:
                $sboqil_msg = save_est_project_boq_item_labour($from_est_boq_item_id, $from_est_detail_type_code, $to_est_boq_item_id);
                if($sboqil_msg["success"] !== true){
                    return $sboqil_msg;
                }
                break;
    
            case BOQPT_ASSET:
                $sboqia_msg = save_est_project_boq_item_assets($from_est_boq_item_id, $from_est_detail_type_code, $to_est_boq_item_id);
                if($sboqia_msg["success"] !== true){
                    return $sboqia_msg;
                }
                break;
    
            case BOQPT_TRANSPORT:
                $sboqit_msg = save_est_project_boq_item_transport($from_est_boq_item_id, $from_est_detail_type_code, $to_est_boq_item_id);
                if($sboqit_msg["success"] !== true){
                    return $sboqit_msg;
                }
                break;

            default:
                break;
        }
        
        return array("success" => true, "message"=>""); 

}


function save_est_project_boq_item_costs($from_est_boq_item_id, $from_est_detail_type_code, $to_est_boq_item_id)
{
    
    delete_est_boq_item_component_primary_costs($to_est_boq_item_id, $from_est_detail_type_code);
    
    $result = get_est_boq_item_costs($from_est_boq_item_id, $from_est_detail_type_code);
    while($from_est_boq_item_cost = db_fetch($result)){
   
        $to_est_boq_item_cost['description'] = $from_est_boq_item_cost['description'];
        $to_est_boq_item_cost['cost'] = $from_est_boq_item_cost['cost'];
        $to_est_boq_item_cost['comment'] = $from_est_boq_item_cost['comment'];

        $aboqic_msg = add_est_boq_item_cost($to_est_boq_item_id, $from_est_detail_type_code, $to_est_boq_item_cost['description'],
                                        $to_est_boq_item_cost['cost'], $to_est_boq_item_cost['comment']); 
        if($aboqic_msg["success"] !== true){
            return $aboqic_msg;
        }
        
    }
    
    return array("success" => true, "message"=>""); 
}

function save_est_project_boq_item_materials($from_est_boq_item_id, $from_est_detail_type_code, $to_est_boq_item_id)
{
    
    delete_est_boq_item_component_primary_material($to_est_boq_item_id, $from_est_detail_type_code);
    
    $result = get_est_boq_item_material($from_est_boq_item_id, $from_est_detail_type_code);
    while($from_est_boq_item_material = db_fetch($result)){
   
        $to_est_boq_item_material['stock_id'] = $from_est_boq_item_material['stock_id'];
        $to_est_boq_item_material['tran_unit_name'] = $from_est_boq_item_material['tran_unit_name'];
        $to_est_boq_item_material['tran_qty'] = $from_est_boq_item_material['tran_qty'];
        $to_est_boq_item_material['tran_unit'] = $from_est_boq_item_material['tran_unit'];
        $to_est_boq_item_material['tran_unit_cost'] = $from_est_boq_item_material['tran_unit_cost'];
        $to_est_boq_item_material['comment'] = $from_est_boq_item_material['comment'];
        $to_est_boq_item_material['item_type'] = $from_est_boq_item_material['item_type'];
        

        $aboqim_msg = add_est_boq_item_material($to_est_boq_item_id, $from_est_detail_type_code, $to_est_boq_item_material['stock_id'], 
                                                $to_est_boq_item_material['tran_unit'], $to_est_boq_item_material['tran_unit_cost'], 
                                                $to_est_boq_item_material['tran_qty'], $to_est_boq_item_material['comment'], 
                                                $to_est_boq_item_material['item_type'] ); 
        
        if($aboqim_msg["success"] !== true){
            return $aboqim_msg;
        }
        
    }
    
    return array("success" => true, "message"=>""); 
}

            
function save_est_project_boq_item_labour($from_est_boq_item_id, $from_est_detail_type_code, $to_est_boq_item_id)
{
    
    delete_est_boq_item_component_primary_labour($to_est_boq_item_id, $from_est_detail_type_code);
    
    $result = get_est_boq_item_labour($from_est_boq_item_id, $from_est_detail_type_code);
    while($from_est_boq_item_labour = db_fetch($result)){
   
        $to_est_boq_item_labour['rate_category_id'] = $from_est_boq_item_labour['rate_category_id'];
        $to_est_boq_item_labour['rate'] = $from_est_boq_item_labour['rate'];
        $to_est_boq_item_labour['days'] = $from_est_boq_item_labour['days'];
        $to_est_boq_item_labour['comment'] = $from_est_boq_item_labour['comment'];


        $aboqil_msg = add_est_boq_item_labour($to_est_boq_item_id, $from_est_detail_type_code, $to_est_boq_item_labour['rate_category_id'], 
                                              $to_est_boq_item_labour['rate'], $to_est_boq_item_labour['days'], $to_est_boq_item_labour['comment']); 
        
        
        if($aboqil_msg["success"] !== true){
            return $aboqil_msg;
        }
        
    }
    
    return array("success" => true, "message"=>""); 
}

            
function save_est_project_boq_item_assets($from_est_boq_item_id, $from_est_detail_type_code, $to_est_boq_item_id)
{
    
    delete_est_boq_item_component_primary_assets($to_est_boq_item_id, $from_est_detail_type_code);
    
    $result = get_est_boq_item_assets($from_est_boq_item_id, $from_est_detail_type_code);
    while($from_est_boq_item_asset = db_fetch($result)){
   
        $to_est_boq_item_asset['asset_id'] = $from_est_boq_item_asset['asset_id'];
        $to_est_boq_item_asset['rate_category_id'] = $from_est_boq_item_asset['rate_category_id'];
        $to_est_boq_item_asset['rate'] = $from_est_boq_item_asset['rate'];
        $to_est_boq_item_asset['days'] = $from_est_boq_item_asset['days'];
        $to_est_boq_item_asset['comment'] = $from_est_boq_item_asset['comment'];
     
        
        $aboqia_msg = add_est_boq_item_asset($to_est_boq_item_id, $from_est_detail_type_code, $to_est_boq_item_asset['asset_id'], 
                                         $to_est_boq_item_asset['rate_category_id'], $to_est_boq_item_asset['rate'], 
                                         $to_est_boq_item_asset['days'], $to_est_boq_item_asset['comment']); 
        
        
        if($aboqia_msg["success"] !== true){
            return $aboqia_msg;
        }
        
    }
    
    return array("success" => true, "message"=>""); 
}


function save_est_project_boq_item_transport($from_est_boq_item_id, $from_est_detail_type_code, $to_est_boq_item_id)
{
    
    delete_est_boq_item_component_primary_transport($to_est_boq_item_id, $from_est_detail_type_code);
    
    $result = get_est_boq_item_transport($from_est_boq_item_id, $from_est_detail_type_code);
    while($from_est_boq_item_transport = db_fetch($result)){
   
        $to_est_boq_item_transport['vehicle_type_id'] = $from_est_boq_item_transport['vehicle_type_id'];
        $to_est_boq_item_transport['rate'] = $from_est_boq_item_transport['rate'];
        $to_est_boq_item_transport['days'] = $from_est_boq_item_transport['days'];
        $to_est_boq_item_transport['comment'] = $from_est_boq_item_transport['comment'];        
     
        
        $aboqit_msg = add_est_boq_item_transport($to_est_boq_item_id, $from_est_detail_type_code, $to_est_boq_item_transport['vehicle_type_id'], 
                                             $to_est_boq_item_transport['rate'], $to_est_boq_item_transport['days'], $to_est_boq_item_transport['comment']); 
       
        
        if($aboqit_msg["success"] !== true){
            return $aboqit_msg;
        }
        
    }
    
    return array("success" => true, "message"=>""); 
}

 
?>




























































































































































