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


function get_year_dates($start_date,$end_date)
{
	$sql = "SELECT "
                        . "wtc.*, "
                        . "lv.value_name as work_time_name "
                . "FROM "
                        .TB_PREF."work_time_calendars wtc, "
                        .TB_PREF."list_values lv "
                . "WHERE "
                        . "wtc.work_time_code = lv.value_code AND lv.list_code = 'WORKING_TIME' AND "
                        . "date >= ".db_escape($start_date)." AND "
                        . "date <= ".db_escape($end_date);
	
	$result = db_query($sql,"Could not retrieve working time code");	
        
        return $result;		
}

function add_work_time_calendar_date($date, $work_time_code, $closed)
{
        $created_by = $_SESSION['wa_current_user']->user;
        $created_on = date('Y-m-d H:i:s');
        
        $sql = "INSERT INTO ".TB_PREF."work_time_calendars (date, work_time_code,"
                            . "closed, created_by, created_on) "
                    . "VALUES (".db_escape($date).",".db_escape($work_time_code).","
                            .$closed.",".$created_by.",".db_escape($created_on).")";
        
        db_query($sql,"Could not add work time calendar date");
        
        return db_insert_id();
}


function update_work_time_calendar_date($id,$work_time_code,
                        $comment)
{
        $sql = "UPDATE ".TB_PREF."work_time_calendars "
                ."SET work_time_code = ".db_escape($work_time_code).", "
                        ."comment = ".  db_escape($comment)." "
                ."WHERE id = ".$id; 
        
        $result = db_query($sql,"Could not update work time calendar date");
        
        return $result;
            
}


function delete_work_time_calendar($start_date,$end_date)
{
        $sql = "DELETE FROM ".TB_PREF."work_time_calendars "
                ."WHERE date >= ".db_escape($start_date)." AND "
                        ."date <= ".db_escape($end_date); 
        
        $result = db_query($sql,"Could not update work time calendar date");
        
        return $result;
            
}

function get_calendar_date($id)
{
	$sql = "SELECT * FROM ".TB_PREF."work_time_calendars "
                . "WHERE id = ".$id;
	
	$result = db_query($sql,"Could not retrieve work time calendar date");	
        
        return db_fetch($result);		
}


function check_processed_dates($start_date,$end_date)
{
        $sql = "SELECT date FROM ".TB_PREF."work_time_calendars "
                . " WHERE date >= ".db_escape($start_date)." AND "
                        . " date <= ".db_escape($end_date)." AND "
                        . " closed = 1 "
                . "LIMIT 1 ";
	
	$result = db_query($sql,"Could not retrieve work time calendar processed dates");	
        
        return db_fetch($result);		
}

?>