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

function recalcualte_boq_item_total_cost($boq_item_id, $detail_type_code, $primary_type){  
    
    $primary_type = 0;
    
        if(!exists_boq_item_component_record($boq_item_id, $detail_type_code, $primary_type)){
            create_boq_item_component_record($boq_item_id, $detail_type_code, $primary_type);
        }
    
//        delete_boq_item_component_other_primary_types($boq_item_id, $detail_type_code, $promary_type);
        $n1 = recalculate_boq_item_components($boq_item_id, $detail_type_code, $primary_type);
        if($n1 !== 1){
            return false;
        }
        $n2 = recalculate_boq_item($boq_item_id);
        if($n2 !== 1){
            return false;
        }
        
        return true;
        
      
}

function recalculate_boq_item_components($boq_item_id, $detail_type_code, $primary_type)
{
//    switch ($primary_type) {
//        case BOQPT_COST:
//            return recalculate_boq_item_component_cost($boq_item_id, $detail_type_code, $primary_type);
//
//        case BOQPT_MATERIAL:
//            return recalculate_boq_item_component_material($boq_item_id, $detail_type_code, $primary_type);
//
//        case BOQPT_LABOUR:
//            return recalculate_boq_item_component_labour($boq_item_id, $detail_type_code, $primary_type);
//
//        case BOQPT_ASSET:
//            return recalculate_boq_item_component_assets($boq_item_id, $detail_type_code, $primary_type);
//
//        case BOQPT_TRANSPORT:
//            return recalculate_boq_item_component_transport($boq_item_id, $detail_type_code, $primary_type);
//
//
//        default:
//            break;
//    }
    
    
    $costs = recalculate_boq_item_component_cost($boq_item_id, $detail_type_code, $primary_type);

    $material = recalculate_boq_item_component_material($boq_item_id, $detail_type_code, $primary_type);

    $labour = recalculate_boq_item_component_labour($boq_item_id, $detail_type_code, $primary_type);

    $assets = recalculate_boq_item_component_assets($boq_item_id, $detail_type_code, $primary_type);

    $transport = recalculate_boq_item_component_transport($boq_item_id, $detail_type_code, $primary_type);

    $boq_item_component_total_cost = $costs + $material + $labour + $assets + $transport;
            
    $primary_type = 0;
    
    return update_boq_item_component_total_cost($boq_item_id, $detail_type_code, $primary_type, $boq_item_component_total_cost);
    
}


function delete_boq_item_component_primary_types($boq_item_id, $detail_type_code)
{
    delete_boq_item_component_primary_costs($boq_item_id, $detail_type_code);

    delete_boq_item_component_primary_material($boq_item_id, $detail_type_code);

    delete_boq_item_component_primary_labour($boq_item_id, $detail_type_code);

    delete_boq_item_component_primary_assets($boq_item_id, $detail_type_code);

    delete_boq_item_component_primary_transport($boq_item_id, $detail_type_code);

     
}

function update_boq_item_component_total_cost($boq_item_id, $detail_type_code, $primary_type, $total_cost)
{
    $sql = "UPDATE ".TB_PREF."project_boq_item_components 
            SET cost=".db_escape($total_cost).",
                primary_type=".db_escape($primary_type).",
                row_state = row_state + 1  
            WHERE boq_item_id=".db_escape($boq_item_id)." AND  
                    detail_type_code=".db_escape($detail_type_code);
                    
    db_query($sql, "Could not update_boq_item_component_total_cost");    
    
    return db_num_affected_rows();
}

function get_boq_item_components_total_cost($boq_item_id)
{
    $sql = "SELECT SUM(cost) total_cost 
            FROM ".TB_PREF."project_boq_item_components 
            WHERE boq_item_id=".db_escape($boq_item_id);
    
    $result = db_query($sql);
    $total_cost = db_fetch($result); 
    if(isset($total_cost['total_cost']) && $total_cost['total_cost'] > 0){
        return $total_cost['total_cost'];
    }
    else{
        return 0;
    }
    
}



function exists_boq_item_component_record($boq_item_id, $detail_type_code, $primary_type)
{
    $sql = "SELECT id  
            FROM ".TB_PREF."project_boq_item_components 
            WHERE boq_item_id=".db_escape($boq_item_id)." AND 
                    detail_type_code=".db_escape($detail_type_code);
    
    $result = db_query($sql, "exists_boq_item_component_record");    
    $n = db_num_rows($result); 
    if($n > 0){
        return true;
    }
    else{
        return false;
    }
}


function get_boq_item_component_record_by_id($id)
{
    $sql = "SELECT *   
            FROM ".TB_PREF."project_boq_item_components 
            WHERE id = ".db_escape($id);
    
    $result = db_query($sql, "Could not get_boq_item_component_record_by_id");    
    return db_fetch($result); 

}

function get_boq_item_component_record($boq_item_id, $detail_type_code)
{
    $sql = "SELECT *   
            FROM ".TB_PREF."project_boq_item_components 
            WHERE boq_item_id = ".db_escape($boq_item_id)." AND 
                    detail_type_code = ".db_escape($detail_type_code);
    
    $result = db_query($sql, "Could not get_boq_item_component_record");    
    return db_fetch($result); 

}


function delete_boq_item_component_record( $boq_item_component_id)
{
//    $created_by = $_SESSION['wa_current_user']->user;
//    $created_on = date('Y-m-d H:i:s');
        
    
    $sql = "DELETE FROM ".TB_PREF."project_boq_item_components 
                    WHERE id = ".db_escape($boq_item_component_id); 
   
   db_query($sql, "Could not delete_boq_item_component_record");   
   
   return db_num_affected_rows();
}

?>