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

function add_month_description($description, $year_code, $month_code,                               
                                $display_order, $active_id, $short_name,
                                $month_begin_date, $month_end_date) 
                               
{
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
    $sql = "INSERT INTO ".TB_PREF."months
            (description, year_code, month_code, 
            display_order, active,
            short_name,
            month_begin_date, month_end_date,
            created_by, created_on)
            
            VALUES 
            (".db_escape($description).",".db_escape($year_code).",".db_escape($month_code).",
            ".db_escape($display_order).", ".db_escape($active_id).", ".db_escape($short_name).",   
            ".db_escape($month_begin_date).", ".db_escape($month_end_date).",
            ".db_escape($created_by).",".db_escape($created_on).")";
           
    db_query($sql);        
}


function update_month_description($selected_id, $description, $year_code, $month_code,
                                    $display_order, $active_id, $short_name,
                                    $month_begin_date, $month_end_date)
                                 
{
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');

    $sql = "UPDATE ".TB_PREF."months
            SET description=".db_escape($description).", 
                year_code=".db_escape($year_code).", 
                month_code=".db_escape($month_code).", 
                display_order=".db_escape($display_order).", 
                month_begin_date=".db_escape($month_begin_date).", 
                month_end_date=".db_escape($month_end_date).", 
                active=".db_escape($active_id).",
                short_name=".db_escape($short_name).",
                updated_by=".db_escape($updated_by).", 
                updated_on=".db_escape($updated_on)."
            WHERE id=".db_escape($selected_id); 
    
    db_query($sql);
    
}

function get_months()
{
    $sql = "SELECT mo.*,
                    yr.value_name year_name,
                    mn.value_name month_name
            
            FROM ".TB_PREF."months mo
                JOIN ".TB_PREF."list_values mn ON
                    mo.month_code = mn.value_code AND mn.list_code = 'MONTHS'                    
                JOIN ".TB_PREF."list_values yr ON
                    mo.year_code = yr.value_code AND yr.list_code = 'YEAR'   
            ORDER BY mo.year_code, mo.display_order ";
                
         
   
   return db_query($sql);   
}

function get_month_description($selected_id)
{
    $sql = "SELECT *
            FROM ".TB_PREF."months
            WHERE id=".db_escape($selected_id);
    
    $result = db_query($sql);
    $m = db_fetch($result);
    return $m['description'];

}

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


function get_month_by_display_order($display_order)
{
    $sql = "SELECT *
            FROM ".TB_PREF."months
            WHERE display_order=".db_escape($display_order);
    
    $result = db_query($sql);
    return db_fetch($result);
}


function get_next_month_by_display_order($display_order)
{
    $sql = "SELECT *
            FROM ".TB_PREF."months
            WHERE display_order > ".db_escape($display_order)." 
                ORDER BY display_order 
                LIMIT 1";
    
    $result = db_query($sql);
    return db_fetch($result);
}


function get_month_by_date_within($date)
{
    $sql = "SELECT *
            FROM ".TB_PREF."months
            WHERE month_begin_date <=".db_escape($date)."  
                    AND month_end_date >=".db_escape($date)." 
                  LIMIT 1 ";
        
    $result = db_query($sql);
    return db_fetch($result);
}


function get_before_month_by_display_order($display_order)
{
    $sql = "SELECT *
            FROM ".TB_PREF."months
            WHERE display_order < ".db_escape($display_order)." 
            ORDER BY display_order DESC LIMIT 1";
    
    $result = db_query($sql);
    return db_fetch($result);

}

function delete_month_description($selected_id)
{
    $sql = "DELETE FROM ".TB_PREF."months WHERE id=".db_escape($selected_id);
    
    db_query($sql);

}
 
function get_months_range_by_display_order($from_month_display_order, $to_month_display_order)
{
    $sql = "SELECT * 
            FROM ".TB_PREF."months 
            WHERE display_order >= ".db_escape($from_month_display_order)."
                                       
              AND display_order <= ".db_escape($to_month_display_order)." 
            ORDER BY display_order";
    
    return db_query($sql);    
}

function get_current_month()
{
 
    $today = date('Y-m-d');
    $sql = "SELECT *  
            FROM ".TB_PREF."months 
            WHERE month_end_date >= ".db_escape($today)."                                       
              AND month_begin_date <= ".db_escape($today);
    
    $result = db_query($sql);   
    
    return db_fetch($result);
}



function get_offset_month($starting_month_display_order, $offset_months)
{
    
    if($offset_months > 0){
        $offset_months -= 1;
        $sql = "SELECT * 
                FROM ".TB_PREF."months 
                WHERE display_order >= ".db_escape($starting_month_display_order)." 
                ORDER BY display_order ASC 
                LIMIT $offset_months, 1";
    }
    elseif($offset_months < 0){
        $offset_months = abs($offset_months);
        $offset_months -= 1;
        $sql = "SELECT * 
                FROM ".TB_PREF."months 
                WHERE display_order <= ".db_escape($starting_month_display_order)." 
                ORDER BY display_order DESC 
                LIMIT $offset_months, 1";
    }else{
        $sql = "SELECT * 
                FROM ".TB_PREF."months 
                WHERE display_order = ".db_escape($starting_month_display_order)."  
                LIMIT 1";
    }
    
    $result = db_query($sql);    
    
    return db_fetch($result);
}