<?php 

//
/**********************************************************************
***********************************************************************/

function save_boq_item_cost($selected_id, $boq_item_id, $detail_type_code, 
                                $description, $unit_name, $rate,   
                                $quantity, $cost, $comment)
{  
    
    //if(can_commit_bom_labour($selected_id)){
        if($selected_id > 0){
            update_boq_item_cost($selected_id, $boq_item_id, $detail_type_code, $description, $unit_name, $rate, $quantity, $cost, $comment);
        }
        else{         
            add_boq_item_cost($boq_item_id, $detail_type_code, $description, $unit_name, $rate, $quantity, $cost, $comment);
        }        
        
        $result = recalcualte_boq_item_total_cost($boq_item_id, $detail_type_code, BOQPT_COST);
        if($result){
            return array("success" => true); 
        }
        else{
            return array("success" => false); 
        }
        
      
}

function add_boq_item_cost($boq_item_id, $detail_type_code, 
                                $description, $unit_name, $rate,   
                                $quantity, $cost, $comment)  
{
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
        
    $sql = "INSERT INTO ".TB_PREF."project_boq_item_costs
                    (boq_item_id, detail_type_code, 
                    description, unit_name, 
                    rate, quantity, 
                    cost, comment, 
                    created_by, created_on ) 
                    
            VALUES(".db_escape($boq_item_id).", ".db_escape($detail_type_code).", "
                    .db_escape($description).", ".db_escape($unit_name).", "
                    .db_escape($rate).", ".db_escape($quantity).", "
                    .db_escape($cost).", ".db_escape($comment).", "
                    .db_escape($created_by).", ".db_escape($created_on).")"; 
   
   db_query($sql, "Boq cost could not be added");    
    
    $result = recalcualte_boq_item_total_cost($boq_item_id, $detail_type_code, BOQPT_COST);
    if($result){
        return array("success" => true); 
    }
    else{
        return array("success" => false); 
    } 
}


function update_boq_item_cost($selected_id, $boq_item_id, $detail_type_code, 
                                $description, $unit_name, $rate,   
                                $quantity, $cost, $comment) 
{
    $sql = "UPDATE ".TB_PREF."project_boq_item_costs
            SET detail_type_code=".db_escape($detail_type_code).", 
                description=".db_escape($description).", 
                unit_name=".db_escape($unit_name).", 
                rate=".db_escape($rate).", 
                quantity=".db_escape($quantity).", 
                description=".db_escape($description).", 
                cost=".db_escape($cost).", 
                comment=".db_escape($comment)."
                    
            WHERE id=".db_escape($selected_id);
                    
    db_query($sql, "Could not update_boq_item_cost");    
    
    $result = recalcualte_boq_item_total_cost($boq_item_id, $detail_type_code, BOQPT_COST);
    if($result){
        return array("success" => true); 
    }
    else{
        return array("success" => false); 
    }
    
}

function delete_boq_item_cost($selected_id, $boq_item_id, $detail_type_code)
{
    $sql="DELETE FROM ".TB_PREF."project_boq_item_costs WHERE id= ".db_escape($selected_id);
	db_query($sql,"Could not delete_boq_cost");		
    
    $result = recalcualte_boq_item_total_cost($boq_item_id, $detail_type_code, BOQPT_COST);
    if($result){
        return array("success" => true); 
    }
    else{
        return array("success" => false); 
    }
}


function get_boq_item_costs($boq_item_id, $detail_type_code)
{
    $sql = "SELECT *
            FROM ".TB_PREF."project_boq_item_costs
            WHERE boq_item_id=".db_escape($boq_item_id)." AND 
                    detail_type_code=".db_escape($detail_type_code);
    
   return db_query($sql, "BOQ costs could not retrieved");    
}

function get_boq_cost_detail($id)
{
    $sql = "SELECT *
            FROM ".TB_PREF."project_boq_item_costs
            WHERE id=".db_escape($id);
    
    $result = db_query($sql);
    return db_fetch($result);    
}


function recalculate_boq_item_component_cost($boq_item_id, $detail_type_code, $primary_type=0)
{
    $primary_cost_total = recalculate_boq_item_component_primary_cost_sum($boq_item_id, $detail_type_code);
    return $primary_cost_total;
//    return update_boq_item_component_total_cost($boq_item_id, $detail_type_code, $primary_type, $primary_cost_total);
}

function recalculate_boq_item_component_primary_cost_sum($boq_item_id, $detail_type_code)
{
    $sql = "SELECT SUM(cost) total_cost 
            FROM ".TB_PREF."project_boq_item_costs
            WHERE boq_item_id=".db_escape($boq_item_id)." AND 
                    detail_type_code=".db_escape($detail_type_code);
    
    $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 delete_boq_item_component_primary_costs($boq_item_id, $detail_type_code)
{
    $sql="DELETE FROM ".TB_PREF."project_boq_item_costs 
            WHERE boq_item_id = ".db_escape($boq_item_id)." AND 
                    detail_type_code = ".db_escape($detail_type_code);
	db_query($sql,"Could not delete_boq_item_component_primary_costs");		
}
?>