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

function copy_est_project_to_sys_project($est_project_id, $filtering_est_group_id, $project_id)
{
    if($filtering_est_group_id > 0){
        $cepbg_msg = copy_est_project_boq_group_to_sys_project_boq_group($est_project_id, $filtering_est_group_id, $project_id);
        if($cepbg_msg["success"] !== true){
            return $cepbg_msg;
        }
    }
    else{
        $cepbi_msg = copy_est_project_boq_items_to_sys_project_boq_items($est_project_id, 0, $project_id);
        if($cepbi_msg["success"] !== true){
            return $cepbi_msg;
        }
    }
    
    return array("success" => true, "message"=>""); 
}
function copy_est_project_boq_group_to_sys_project_boq_group($est_project_id, $filtering_est_group_id, $project_id)
{
//    $result = get_project_est_boq_item_group_records($est_project_id);
    $est_boq_group = get_est_project_boq_item_group_record($filtering_est_group_id);
    
//    while($est_boq_group = db_fetch($row)){
        $sys_project_boq_group = get_project_boq_item_group_record_by_code($project_id, $est_boq_group['code']);
    
        if(isset($sys_project_boq_group['id']) && $sys_project_boq_group['id'] > 0){
            $boq_item_group['id'] = $sys_project_boq_group['id'];
        }
        else{
            $boq_item_group['id'] = -1;
        }
        
        $boq_item_group['project_id'] = $project_id;
        $boq_item_group['code'] = $est_boq_group['code'];
        $boq_item_group['description'] = $est_boq_group['description'];
        $boq_item_group['display_order'] = $est_boq_group['display_order'];
        
        $parent_id = 0;
        if($est_boq_group['parent_id'] > 0){
            $parent_est_group = get_est_project_boq_item_group_record($est_boq_group['parent_id']);
            $sys_project_parent_boq_group = get_project_boq_item_group_record_by_code($project_id, $parent_est_group['code']);
            if(isset($sys_project_parent_boq_group['id']) && $sys_project_parent_boq_group['id'] > 0){
                $parent_id = $sys_project_parent_boq_group['id'];
            }
            else{
                $parent_id = 0;
            }            
        }
        $boq_item_group['parent_id'] = $parent_id;
    
        $cpboqig_msg = can_process_project_boq_item_group($boq_item_group);
        
        if($cpboqig_msg["success"] !== true){
            return $cpboqig_msg;
        }

        $msg = save_est_boq_group_to_sys_project($boq_item_group, $project_id);
        if($msg["success"] !== true){
            return $msg;
        }
        
        $sys_boq_group_id = $msg["trans_no"];
        
        recalculate_boq_item_group_positions($boq_item_group['project_id']);
    
    return copy_est_project_boq_items_to_sys_project_boq_items($est_project_id, $filtering_est_group_id, $project_id);
    
}


function copy_est_project_boq_items_to_sys_project_boq_items($est_project_id, $filtering_est_group_id, $project_id)
{
    if($filtering_est_group_id > 0){
        $est_boq_group = get_project_and_est_project_boq_item_group($est_project_id, $filtering_est_group_id, $project_id);
        $sys_boq_group_id = $est_boq_group['sys_boq_group_id'];
    }
    else{
        $sys_boq_group_id = 0;
    }
    
//    while($est_boq_group = db_fetch($result))
//    {
        $result = get_est_project_group_boq_items($est_project_id, $filtering_est_group_id);
        
        while($est_boq_item = db_fetch($result))
        {
            $sys_project_boq_item = get_project_boq_item_record_by_code($project_id, $est_boq_item['code']);
            
            if(isset($sys_project_boq_item['id']) && $sys_project_boq_item['id'] > 0){
                if($sys_project_boq_item['group_id'] != $sys_boq_group_id){// Boq item exists but under a different BOQ item group
                        
                        $exisiting_boq_item = get_project_boq_item_record_by_code($project_id, $est_boq_item['code']);
                        $exisiting_boq_item_group = get_project_boq_item_group_record($exisiting_boq_item['group_id']);
                        $current_boq_item_group = get_project_boq_item_group_record($sys_boq_group_id);
                        
                        $message = "System project BOQ item ".$est_boq_item['code']." - ".$exisiting_boq_item['description']
                                    ." already exists under the BOQ item group ".$exisiting_boq_item_group['code']." - ".$exisiting_boq_item_group['description']
                                    .". It can not be copied again in to BOQ item group ".$current_boq_item_group['code']." - ".$current_boq_item_group['description'];

                        return array("success"=>false, "message"=> $message);
                }
                
                $boq_item['id'] = $sys_project_boq_item['id'];
            }
            else{
                $boq_item['id'] = -1;
            }
            
            $boq_item['project_id'] = $project_id;
            $boq_item['display_order'] = $est_boq_item['display_order'];
            $boq_item['type_id'] = $est_boq_item['type_id'];
            $boq_item['code'] = $est_boq_item['code'];
            $boq_item['description'] = $est_boq_item['description'];
            $boq_item['billing_type'] = $est_boq_item['billing_type'];
            $boq_item['rate'] = $est_boq_item['rate'];
            $boq_item['unit'] = $est_boq_item['unit'];
            $boq_item['max_quantity'] = $est_boq_item['max_quantity'];
            $boq_item['long_description'] = $est_boq_item['long_description'];
            $boq_item['print_long_description'] = $est_boq_item['print_long_description'];
            $boq_item['tax_group_id'] = $est_boq_item['tax_group_id'];
            $boq_item['calculated_cost'] = $est_boq_item['calculated_cost'];
            $boq_item['group_id'] = $sys_boq_group_id;
            
            $cpboqig_msg = can_process_boq_item($boq_item);

            if($cpboqig_msg["success"] !== true){
                return $cpboqig_msg;
            }
            
            begin_transaction();
            $sys_boq_item_id = save_est_boq_item_to_sys_boq_item($boq_item, $project_id);
            if(!($sys_boq_item_id > 0)){
                cancel_transaction();
                return array("success" => false, "message"=>"Could not save BOQ item ".$boq_item['code']." - ".$boq_item['description']); 
            }
            
            $boq_item['boq_item_id'] = $sys_boq_item_id;
            
            $cepc_msg = copy_est_project_component_to_sys_project_component($boq_item, $est_boq_item['id']);
            if($cepc_msg["success"] !== true){
                cancel_transaction();
                return $cepc_msg;
            }
            
            commit_transaction();
        }
//    }
    
    return array("success" => true, "message"=>""); 
}
    
    
function copy_est_project_component_to_sys_project_component($sys_boq_item, $est_boq_item_id)
{
    global $project_boq_item_component_primary_types;
    
    $sys_boq_item_id = $sys_boq_item['boq_item_id'];
    
    $result = get_est_boq_item_active_components($est_boq_item_id);

        while($est_boq_item_component = db_fetch($result))
        {
            
            $sys_component = get_boq_item_component_record($sys_boq_item_id, $est_boq_item_component['detail_type_code']);

            if(isset($sys_component['id']) && $sys_component['id'] > 0){
                $boq_item_component['id'] = $sys_component['id'];
                
                if($sys_component['primary_type'] != $est_boq_item_component['primary_type']){// Primary types are different. Can not copy
                        if($sys_boq_item['group_id'] > 0){
                            $sys_boq_item_group = get_project_boq_item_group_record($sys_boq_item['group_id']);
                            $sys_boq_item_group_name = $sys_boq_item_group['code']." - ".$sys_boq_item_group['description'];
                        }
                        else{
                            $sys_boq_item_group_name = "No BOQ Item Group";
                        }
                        
                        $sys_detail_type = get_listvalue_by_code('PROJECT_TASK_DETAIL_TYPES', $sys_component['detail_type_code']);
                        $sys_primary_type_name = $project_boq_item_component_primary_types[$sys_component['primary_type']];
                        
                        $est_boq_item = get_est_project_boq_item_record($est_boq_item_id);
                        if($est_boq_item['group_id'] > 0){
                            $est_boq_item_group = get_project_boq_item_group_record($est_boq_item_id);
                            $est_boq_item_group_name = $est_boq_item_group['code']." - ".$est_boq_item_group['description'];
                        }
                        else{
                            $est_boq_item_group_name = "No BOQ Item Group";
                        }
                            
                        $est_detail_type = get_listvalue_by_code('PROJECT_TASK_DETAIL_TYPES', $est_boq_item_component['detail_type_code']);
                        $est_primary_type_name = $project_boq_item_component_primary_types[$est_boq_item_component['primary_type']];
                        
                        $message = "System project BOQ item component primary type is different to estimation project BOQ item component primary type. Copying terminated."
                                    .'<br>'.'<br>'
                                    ."System project: BOQ item group: ".$sys_boq_item_group_name
                                    ." -> BOQ item: ".$sys_boq_item['code']." - ".$sys_boq_item['description']
                                    ." -> Component - Detail type: ".$sys_detail_type['value_name']." - Primary type: ".$sys_primary_type_name
                                    .'<br>'
                                    ."Estimation project: BOQ item group: ".$est_boq_item_group_name
                                    ." -> BOQ item: ".$est_boq_item['code']." - ".$est_boq_item['description']
                                    ." -> Component - Detail type: ".$est_detail_type['value_name']." - Primary type: ".$est_primary_type_name;
                                    
                        return array("success"=>false, "message"=> $message);
                }
            }
            else{
                $boq_item_component['id'] = -1;
            }

            $boq_item_component['boq_item_id'] = $sys_boq_item_id;
            $boq_item_component['detail_type_code'] = $est_boq_item_component['detail_type_code'];
            $boq_item_component['primary_type'] = $est_boq_item_component['primary_type'];
            $boq_item_component['cost'] = $est_boq_item_component['cost'];
            $boq_item_component['row_state'] = $est_boq_item_component['row_state'];


//                $cpboqig_msg = can_process_boq_item($boq_item);
//
//                if($cpboqig_msg["success"] !== true){
//                    return $cpboqig_msg;
//                }

            $sys_boq_component_id = save_est_boq_item_component_to_sys_boq_item_component($boq_item_component);
            if(!($sys_boq_component_id > 0)){
                return array("success" => false, "message"=>"Could not save BOQ item component ".$boq_item_component['detail_type_code']." of BOQ item ".$sys_boq_item['code']." - ".$sys_boq_item['description']); 
            }

            $cepd_msg = copy_est_project_component_details_to_sys_project_component_details($sys_boq_component_id, $est_boq_item_component['id'],
                                                $est_boq_item_component['boq_item_id'], $est_boq_item_component['detail_type_code'], $sys_boq_item_id);
            if($cepd_msg["success"] !== true){
                   return $cepd_msg;
            }
        }
        
        return array("success" => true, "message"=>""); 
           
}


function copy_est_project_component_details_to_sys_project_component_details($sys_boq_component_id, $est_boq_component_id, 
                                                                             $est_boq_item_id, $est_detail_type_code, $sys_boq_item_id)
{
    $row = get_est_boq_item_component_record_by_id($est_boq_component_id);
           
        switch ($row['primary_type']) {
            case BOQPT_COST:
                $sboqic_msg = save_project_boq_item_costs($est_boq_component_id, $sys_boq_component_id, $est_boq_item_id, 
                                                          $est_detail_type_code, $sys_boq_item_id);
                if($sboqic_msg["success"] !== true){
                    return $sboqic_msg;
                }
                break;

            case BOQPT_MATERIAL:
                $sboqim_msg = save_project_boq_item_materials($est_boq_component_id, $sys_boq_component_id, $est_boq_item_id, 
                                                              $est_detail_type_code, $sys_boq_item_id);
                if($sboqim_msg["success"] !== true){
                    return $sboqim_msg;
                }
                break;
    
            case BOQPT_LABOUR:
                $sboqil_msg = save_project_boq_item_labour($est_boq_component_id, $sys_boq_component_id, $est_boq_item_id, 
                                                           $est_detail_type_code, $sys_boq_item_id);
                if($sboqil_msg["success"] !== true){
                    return $sboqil_msg;
                }
                break;
    
            case BOQPT_ASSET:
                $sboqia_msg = save_project_boq_item_assets($est_boq_component_id, $sys_boq_component_id, $est_boq_item_id, 
                                                          $est_detail_type_code, $sys_boq_item_id);
                if($sboqia_msg["success"] !== true){
                    return $sboqia_msg;
                }
                break;
    
            case BOQPT_TRANSPORT:
                $sboqit_msg = save_project_boq_item_transport($est_boq_component_id, $sys_boq_component_id, $est_boq_item_id, 
                                                              $est_detail_type_code, $sys_boq_item_id);
                if($sboqit_msg["success"] !== true){
                    return $sboqit_msg;
                }
                break;

            default:
                break;
        }
        
        return array("success" => true, "message"=>""); 

}


function save_project_boq_item_costs($est_boq_component_id, $sys_boq_component_id, $est_boq_item_id, $est_detail_type_code, $sys_boq_item_id)
{
    
    delete_boq_item_component_primary_costs($sys_boq_item_id, $est_detail_type_code); //delete cost detail records of sys boq item component
    
    $result = get_est_boq_item_costs($est_boq_item_id, $est_detail_type_code);
    while($est_boq_item_cost = db_fetch($result)){
   
        $sys_boq_item_cost['description'] = $est_boq_item_cost['description'];
        $sys_boq_item_cost['cost'] = $est_boq_item_cost['cost'];
        $sys_boq_item_cost['comment'] = $est_boq_item_cost['comment'];

        $aboqic_msg = add_boq_item_cost($sys_boq_item_id, $est_detail_type_code, $sys_boq_item_cost['description'],
                          $sys_boq_item_cost['cost'], $sys_boq_item_cost['comment']); 
        if($aboqic_msg["success"] !== true){
            return $aboqic_msg;
        }
        
    }
    
    return array("success" => true, "message"=>""); 
}

function save_project_boq_item_materials($est_boq_component_id, $sys_boq_component_id, $est_boq_item_id, $est_detail_type_code, $sys_boq_item_id)
{
    
    delete_boq_item_component_primary_material($sys_boq_item_id, $est_detail_type_code);
    
    $result = get_est_boq_item_material($est_boq_item_id, $est_detail_type_code);
    while($est_boq_item_material = db_fetch($result)){
   
        $sys_boq_item_material['stock_id'] = $est_boq_item_material['stock_id'];
        $sys_boq_item_material['tran_unit_name'] = $est_boq_item_material['tran_unit_name'];
        $sys_boq_item_material['tran_qty'] = $est_boq_item_material['tran_qty'];
        $sys_boq_item_material['tran_unit'] = $est_boq_item_material['tran_unit'];
        $sys_boq_item_material['tran_unit_cost'] = $est_boq_item_material['tran_unit_cost'];
        $sys_boq_item_material['comment'] = $est_boq_item_material['comment'];
        $sys_boq_item_material['item_type'] = $est_boq_item_material['item_type'];
        

        $aboqim_msg = add_boq_item_material($sys_boq_item_id, $est_detail_type_code, $sys_boq_item_material['stock_id'], 
                                            $sys_boq_item_material['tran_unit'], $sys_boq_item_material['tran_unit_cost'], 
                                            $sys_boq_item_material['tran_qty'], $sys_boq_item_material['comment'], $sys_boq_item_material['item_type'] ); 
        
        if($aboqim_msg["success"] !== true){
            return $aboqim_msg;
        }
        
    }
    
    return array("success" => true, "message"=>""); 
}

            
function save_project_boq_item_labour($est_boq_component_id, $sys_boq_component_id, $est_boq_item_id, $est_detail_type_code, $sys_boq_item_id)
{
    
    delete_boq_item_component_primary_labour($sys_boq_item_id, $est_detail_type_code);
    
    $result = get_est_boq_item_labour($est_boq_item_id, $est_detail_type_code);
    while($est_boq_item_labour = db_fetch($result)){
   
        $sys_boq_item_labour['rate_category_id'] = $est_boq_item_labour['rate_category_id'];
        $sys_boq_item_labour['rate'] = $est_boq_item_labour['rate'];
        $sys_boq_item_labour['days'] = $est_boq_item_labour['days'];
        $sys_boq_item_labour['comment'] = $est_boq_item_labour['comment'];


        $aboqil_msg = add_boq_item_labour($sys_boq_item_id, $est_detail_type_code, $sys_boq_item_labour['rate_category_id'], 
                                            $sys_boq_item_labour['rate'], $sys_boq_item_labour['days'], $sys_boq_item_labour['comment']); 
        
        
        if($aboqil_msg["success"] !== true){
            return $aboqil_msg;
        }
        
    }
    
    return array("success" => true, "message"=>""); 
}

            
function save_project_boq_item_assets($est_boq_component_id, $sys_boq_component_id, $est_boq_item_id, $est_detail_type_code, $sys_boq_item_id)
{
    
    delete_boq_item_component_primary_assets($sys_boq_item_id, $est_detail_type_code);
    
    $result = get_est_boq_item_assets($est_boq_item_id, $est_detail_type_code);
    while($est_boq_item_asset = db_fetch($result)){
   
        $sys_boq_item_asset['asset_id'] = $est_boq_item_asset['asset_id'];
        $sys_boq_item_asset['rate_category_id'] = $est_boq_item_asset['rate_category_id'];
        $sys_boq_item_asset['rate'] = $est_boq_item_asset['rate'];
        $sys_boq_item_asset['days'] = $est_boq_item_asset['days'];
        $sys_boq_item_asset['comment'] = $est_boq_item_asset['comment'];
     
        
        $aboqia_msg = add_boq_item_asset($sys_boq_item_id, $est_detail_type_code, $sys_boq_item_asset['asset_id'], 
                                         $sys_boq_item_asset['rate_category_id'], $sys_boq_item_asset['rate'], 
                                         $sys_boq_item_asset['days'], $sys_boq_item_asset['comment']); 
        
        
        if($aboqia_msg["success"] !== true){
            return $aboqia_msg;
        }
        
    }
    
    return array("success" => true, "message"=>""); 
}


function save_project_boq_item_transport($est_boq_component_id, $sys_boq_component_id, $est_boq_item_id, $est_detail_type_code, $sys_boq_item_id)
{
    
    delete_boq_item_component_primary_transport($sys_boq_item_id, $est_detail_type_code);
    
    $result = get_est_boq_item_transport($est_boq_item_id, $est_detail_type_code);
    while($est_boq_item_transport = db_fetch($result)){
   
        $sys_boq_item_transport['vehicle_type_id'] = $est_boq_item_transport['vehicle_type_id'];
        $sys_boq_item_transport['rate'] = $est_boq_item_transport['rate'];
        $sys_boq_item_transport['days'] = $est_boq_item_transport['days'];
        $sys_boq_item_transport['comment'] = $est_boq_item_transport['comment'];        
     
        
        $aboqit_msg = add_boq_item_transport($sys_boq_item_id, $est_detail_type_code, $sys_boq_item_transport['vehicle_type_id'], 
                                             $sys_boq_item_transport['rate'], $sys_boq_item_transport['days'], $sys_boq_item_transport['comment']); 
       
        
        if($aboqit_msg["success"] !== true){
            return $aboqit_msg;
        }
        
    }
    
    return array("success" => true, "message"=>""); 
}

 
?>



























