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



function get_group_company_db_name($company_code)
{
    if(strlen($company_code) > 0){
        $sql = "SELECT db_name FROM group_companies 
                WHERE company_code = ".db_escape($company_code);
    }
    else{
        $sql = "SELECT db_name FROM group_companies 
                WHERE default_company = 1 LIMIT 1";
    }

    $res = db_query($sql, "Could not get_group_company_db_name");
    $row = db_fetch($res);
    
    $db_name = '';
	if(isset($row['db_name'])){
		$db_name = $row['db_name'];
    }

	return $db_name;
}

function get_group_company_name($company_code)
{
    if(strlen($company_code) > 0){
        $sql = "SELECT company_name FROM group_companies 
                WHERE company_code = ".db_escape($company_code);
    }
    else{
        $sql = "SELECT company_name FROM group_companies 
                WHERE default_company = 1 LIMIT 1";
    }

	$res = db_query($sql, "Could not get_group_company_name");
    $row = db_fetch($res);
    
    $company_name = '';
	if(isset($row['company_name'])){
		$company_name = $row['company_name'];
    }

	return $company_name;
}

function get_group_company_employee($company_code, $employee_code, $db_name = null)
{
    if($db_name == null){
        $db_name = get_group_company_db_name($company_code);
    }
    
    if(isset($db_name) && strlen($db_name) > 0){
        $sql = "SELECT * FROM ".$db_name.".".TB_PREF."employee_master WHERE employee_code=".db_escape($employee_code);
    }
    else{
        $sql = "SELECT * FROM ".TB_PREF."employee_master WHERE employee_code=".db_escape($employee_code);
    }
    
	$result = db_query($sql,"an employee could not be retreived");

	return db_fetch($result);
}


function get_group_company_employee_by_id($company_code, $employee_id, $db_name = null)
{
    if($db_name == null){
        $db_name = get_group_company_db_name($company_code);
    }
    
    if(isset($db_name) && strlen($db_name) > 0){
        $sql = "SELECT * FROM ".$db_name.".".TB_PREF."employee_master WHERE employee_id=".db_escape($employee_id);
    }
    else{
        $sql = "SELECT * FROM ".TB_PREF."employee_master WHERE employee_id=".db_escape($employee_id);
    }
    
	$result = db_query($sql,"an employee could not be retreived");

	return db_fetch($result);
}


function get_group_company_all_employees($company_code)
{
    $db_name = get_group_company_db_name($company_code);

    $sql = "SELECT * FROM ".$db_name.".".TB_PREF."employee_master ";

    return db_query($sql,"get_group_company_all_employees");

}

function get_group_companies()
{
    $sql = "SELECT * FROM group_companies ";
    
    return db_query($sql, "Could not get_group_company_db_name");

}


function get_group_company_all_list_values($company_code, $list_code, $text1_in='')
{
    $db_name = get_group_company_db_name($company_code);
    
    $sql="SELECT lv.* 
            FROM ".$db_name.".".TB_PREF."list_values lv 
            WHERE lv.list_code=".db_escape($list_code);
            if(strlen($text1_in) > 0){
                $sql .= " AND lv.text1 IN (".$text1_in.") ";
            }

            $sql .= " ORDER BY lv.value_name";

    $result = db_query($sql,"get_group_company_all_list_values");

    return $result;
}

function get_group_company_list_value($company_code, $list_code, $value_code)
{
    $db_name = get_group_company_db_name($company_code);
    
    $sql = "SELECT lv.* 
            FROM ".$db_name.".".TB_PREF."list_values lv 
            WHERE lv.list_code=".db_escape($list_code)."  
                        AND lv.value_code=".db_escape($value_code)." 
            ORDER BY lv.value_name";

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

}

function get_worktime_calendar_details($db_name, $from_date, $to_date)
{
    $sql = "SELECT wtc.* 
            FROM ".$db_name.".".TB_PREF."work_time_calendars wtc  
            WHERE wtc.date >= ".db_escape($from_date)." 
                AND wtc.date <= ".db_escape($to_date)." 
            ORDER By wtc.date" ;
                    
    return db_query($sql, "get_worktime_calendar_details");
    
}

function get_holiday_details($db_name, $from_date, $to_date)
{
    $sql = "SELECT hd.* 
            FROM ".$db_name.".".TB_PREF."holidays hd  
            WHERE hd.holiday_date >= ".db_escape($from_date)." 
                AND hd.holiday_date <= ".db_escape($to_date)." 
            ORDER By hd.holiday_date" ;
                    
    return db_query($sql, "get_holiday_details");
    
}

function get_default_group_company_code()
{

        $sql = "SELECT company_code FROM group_companies 
                WHERE default_company = 1 LIMIT 1";
    

	$res = db_query($sql, "Could not get_default_group_company_code");
        $row = db_fetch($res);
    
        $company_code = '';
	if(isset($row['company_code'])){
		$company_code = $row['company_code'];
        }else{
            display_db_error("No default group company defined in the database", $sql, true);
        }

	return $company_code;
}

?>
