<?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)){
            $row = get_boq_item_component_record($boq_item_id, $detail_type_code);
                if (isset($row['id']) && $row['id'] > 0) {
                    create_boq_item_component_record($row['id'], $boq_item_id, $detail_type_code, $primary_type);
                } else {
                    display_error("Could not find project BOQ items component");
                    return false;
                }
        }
    
//        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 = $assets = $transport = 0;

//    $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();
}

function add_project_boq_item_component($id, $boq_item_id, $detail_type_code, $primary_type, $cost, $row_state) {
    $id_user = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');
                
        $sql = "INSERT INTO ".TB_PREF."project_boq_item_components ( ";
        
            if ($id > 0) {
                $sql .= " id, ";
            }
            
                $sql .= " boq_item_id, detail_type_code, 
                          primary_type, cost, 
                          row_state, 
                          created_by, created_on ) 
                  
                VALUES (";
                
            if ($id > 0) {
                $sql .= db_escape($id).", ";
            }
                
                $sql .= db_escape($boq_item_id).", ".db_escape($detail_type_code).", "
                         .db_escape($primary_type).", ".db_escape($cost).", "
                         .db_escape($row_state).", " 
                         .db_escape($id_user).", ".db_escape($datetime).")";

        db_query($sql, "add_project_boq_item_component");	
        return db_insert_id();
}

function get_project_boq_item_component_items($id) {
    $sql_1 = "SELECT 'Item' item_or_cost, lv.value_name details_type, mt.id, mt.item_type, mt.stock_id, sm.description item_description, u.name unit_name, mt.tran_qty, 
                    mt.tran_unit, mt.tran_unit_cost, (mt.tran_qty * mt.tran_unit_cost) cost, mt.detail_type_code 
                FROM ".TB_PREF."project_boq_item_components cmp  
                    JOIN ".TB_PREF."project_boq_item_material mt  
                        ON cmp.boq_item_id = mt.boq_item_id  
                            AND cmp.detail_type_code = mt.detail_type_code   
                    JOIN ".TB_PREF."list_values lv    
                        ON mt.detail_type_code = lv.value_code    
                            AND lv.list_code = 'PROJECT_TASK_DETAIL_TYPES'   
                    JOIN ".TB_PREF."stock_master sm   
                        ON mt.stock_id = sm.stock_id   
                    JOIN ".TB_PREF."item_units u  
                        ON mt.tran_unit = u.abbr  
                WHERE cmp.id = ".db_escape($id);
    
    $sql_2 = "SELECT 'Cost' item_or_cost, lv.value_name details_type, mt.id, '' item_type, '' stock_id, mt.description item_description, mt.unit_name, mt.quantity tran_qty, 
                    mt.unit_name tran_unit, mt.rate tran_unit_cost, mt.cost, mt.detail_type_code 
                FROM ".TB_PREF."project_boq_item_components cmp  
                    JOIN ".TB_PREF."project_boq_item_costs mt  
                        ON cmp.boq_item_id = mt.boq_item_id  
                            AND cmp.detail_type_code = mt.detail_type_code  
                    JOIN ".TB_PREF."list_values lv    
                        ON mt.detail_type_code = lv.value_code    
                            AND lv.list_code = 'PROJECT_TASK_DETAIL_TYPES'   
                WHERE cmp.id = ".db_escape($id);
    
        $sql = "SELECT al.*  
                FROM ($sql_1 UNION $sql_2) al 
                ORDER BY al.item_or_cost DESC, al.id";
        
	return db_query($sql, "get_project_boq_item_component_items");
}

function get_boq_item_component_item_by_filters($project_id, $task_code, $detail_type_code, $item_type, $item_code, $tran_unit, $tran_qty, $tran_unit_cost, $item_or_cost) {
    if ($item_or_cost == 'Item') {
        $itm_sql = " JOIN ".TB_PREF."project_boq_item_material itm  
                        ON cmp.boq_item_id = itm.boq_item_id  
                            AND cmp.detail_type_code = itm.detail_type_code 
                            AND itm.item_type = ".db_escape($item_type)." 
                            AND itm.stock_id = ".db_escape($item_code)." 
                            AND itm.tran_unit = ".db_escape($tran_unit)." 
                            AND itm.tran_qty = ".db_escape($tran_qty)." 
                            AND itm.tran_unit_cost = ".db_escape($tran_unit_cost);
    } elseif ($item_or_cost == 'Cost') {
        $itm_sql = " JOIN ".TB_PREF."project_boq_item_costs itm  
                        ON cmp.boq_item_id = itm.boq_item_id  
                            AND cmp.detail_type_code = itm.detail_type_code 
                            AND itm.description = ".db_escape($item_code)." 
                            AND itm.unit_name = ".db_escape($tran_unit)." 
                            AND itm.quantity = ".db_escape($tran_qty)." 
                            AND itm.rate = ".db_escape($tran_unit_cost);
    } 
    
        $sql = "SELECT itm.* 
                FROM ".TB_PREF."project_tasks pt  
                    JOIN ".TB_PREF."project_boq_item_components cmp 
                        ON pt.task_id = cmp.id   
                    $itm_sql
                WHERE pt.project_id = ".db_escape($project_id)." 
                    AND pt.task_code = ".db_escape($task_code)." 
                    AND cmp.detail_type_code = ".db_escape($detail_type_code);

            $result = db_query($sql, "get_boq_item_component_item_by_filters");
            return db_fetch($result);
}

function get_boq_item_component_by_project_and_task_code($project_id, $task_code, $detail_type_code, $item_code, $tran_unit, $tran_qty, $tran_unit_cost, $item_or_cost) {
    if ($item_or_cost == 'Item') {
        $itm_sql = " JOIN ".TB_PREF."project_boq_item_material itm  
                        ON cmp.boq_item_id = itm.boq_item_id  
                            AND cmp.detail_type_code = itm.detail_type_code 
                            AND itm.stock_id = ".db_escape($item_code)." 
                            AND itm.tran_unit = ".db_escape($tran_unit)." 
                            AND itm.tran_qty = ".db_escape($tran_qty)." 
                            AND itm.tran_unit_cost = ".db_escape($tran_unit_cost);
    } elseif ($item_or_cost == 'Cost') {
        $itm_sql = " JOIN ".TB_PREF."project_boq_item_costs itm  
                        ON cmp.boq_item_id = itm.boq_item_id  
                            AND cmp.detail_type_code = itm.detail_type_code 
                            AND itm.description = ".db_escape($item_code)." 
                            AND itm.unit_name = ".db_escape($tran_unit)." 
                            AND itm.quantity = ".db_escape($tran_qty)." 
                            AND itm.rate = ".db_escape($tran_unit_cost);
    } 
    
        $sql = "SELECT itm.* 
                FROM ".TB_PREF."project_tasks pt  
                    JOIN ".TB_PREF."project_boq_item_components cmp 
                        ON pt.task_id = cmp.id   
                    $itm_sql
                WHERE pt.project_id = ".db_escape($project_id)." 
                    AND pt.task_code = ".db_escape($task_code)." 
                    AND cmp.detail_type_code = ".db_escape($detail_type_code);

            $result = db_query($sql, "get_boq_item_component_item_by_filters");
            return db_fetch($result);
}

?>