<?php
/**********************************************************************
***********************************************************************/
function write_category($selected, $category_id, $category_code, $description, $next_number)
{
    
    if($selected > 0){
		$sql = "UPDATE ".TB_PREF."project_serial_numbers SET
	 	description = ".db_escape($description).",
                    next_number = ".db_escape($next_number)."
        	WHERE id = ".db_escape($selected);
    }
    else{
		$sql = "INSERT INTO ".TB_PREF."project_serial_numbers
			(category_id,category_code,description,next_number) 
                        VALUES( ".db_escape($category_id).",".db_escape($category_code).",".db_escape($description).
                                    ",".db_escape($next_number).")";
    }
    
    db_query($sql,"New category code could not created");
    
}
////
function delete_category_code($id)
{
	$sql="DELETE FROM ".TB_PREF."project_serial_numbers WHERE id=".db_escape($id);

	db_query($sql,"A code could not be deleted");
}
////
function get_category_details($id)
{
	$sql="SELECT * FROM ".TB_PREF."project_serial_numbers WHERE id=".db_escape($id);

	$result = db_query($sql,"A code could not be retrieved");

	return db_fetch($result);
}

function get_next_number($id)
{
	$sql="SELECT next_number FROM ".TB_PREF."project_serial_numbers WHERE category_code=".db_escape($id);

	$result = db_query($sql,"A code could not be retrieved");

	return db_fetch($result);
}

function save_next_number($id, $next_number)
{
    $sql = "UPDATE ".TB_PREF."project_serial_numbers SET next_number=" . db_escape($next_number) 
		. " WHERE category_code = ".db_escape($id);

	db_query($sql, "The next transaction number could not be updated");
}
function get_categories($id) {
    $sql = "SELECT * FROM ".TB_PREF."project_serial_numbers WHERE category_id = $id";
        
    return  db_query($sql, "Could not get category or codes");
}

function check_existing_category_code($category_id, $category_code)
{
    $sql = "SELECT category_code FROM ".TB_PREF."project_serial_numbers 
        WHERE category_id = ".db_escape($category_id)." AND category_code = ".db_escape($category_code);
        
    $result = db_query($sql, "Could not retrieve existing category code");
    
    return db_fetch($result);
}
?>