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

function update_accounting_period($id, $end_date, $period_name, $closed)
{
    $user = $_SESSION['wa_current_user']->user;
    $date = date('Y-m-d H:i:s');
    
	$sql = "UPDATE ".TB_PREF."accounting_periods  
                    SET end_date =".db_escape($end_date).", 
                        period_name=".db_escape($period_name).",	
                        closed=".db_escape($closed).", 
                        updated_by = ".db_escape($user).",
                        updated_on = ".db_escape($date)."
                    WHERE id = ".db_escape($id);

	db_query($sql, "update_accounting_period");
}

//---------------------------------------------------------------------------------------------

function add_accounting_period($fiscal_year_id, $end_date, $period_name, $closed)
{
        $user = $_SESSION['wa_current_user']->user;
        $date = date('Y-m-d H:i:s');
    
	$sql = "INSERT INTO ".TB_PREF."accounting_periods (
                        fiscal_year_id, end_date, 
                        period_name, closed,  
                        created_by, created_on) 
		VALUES (".db_escape($fiscal_year_id).", ".db_escape($end_date).", "
                        .db_escape($period_name).", ".db_escape($closed).", "
                        .db_escape($user).",".db_escape($date).")";

	db_query($sql, "add_accounting_period");
}

//---------------------------------------------------------------------------------------------

function delete_accounting_period($id)
{
	$sql="DELETE FROM ".TB_PREF."accounting_periods WHERE id =".db_escape($id);
 
        db_query($sql, "delete_accounting_period");
}

//---------------------------------------------------------------------------------------------

function get_accounting_period($id)
{
	$sql = "SELECT * FROM ".TB_PREF."accounting_periods WHERE id =".db_escape($id);
        
	$result = db_query($sql, "get_accounting_period");

	$row = db_fetch($result);
	return $row;
}

//---------------------------------------------------------------------------------------------

function get_accounting_periods($fiscal_year_id)
{
	$sql = "SELECT * 
                FROM ".TB_PREF."accounting_periods 
                WHERE fiscal_year_id =".db_escape($fiscal_year_id)." 
                ORDER BY end_date ";
        
	return db_query($sql, "get_accounting_periods");

}

function get_date_accounting_period($date)
{
	$sql = "SELECT * 
                FROM ".TB_PREF."accounting_periods 
                WHERE end_date >= ".db_escape($date)." 
                ORDER BY end_date LIMIT 1";
        
	$result = db_query($sql, "get_date_accounting_period");
        return db_fetch($result);

}