/**
 * Smart Restaurant
 *
 * An open source application to manage restaurants
 *
 * @package		SmartRestaurant
 * @author		Gjergj Sheldija
 * @copyright	Copyright (c) 2008-2012, Gjergj Sheldija
 * @license		http://www.gnu.org/licenses/gpl.txt
 * @since		Version 1.0
 * @filesource
 * 
 *  Smart Restaurant is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, version 3 of the License.
 *
 *	Smart Restaurant is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
function check_db_status()
{
    $query = "USE `" . $_SESSION['common_db'] . '`';
    $res = common_query($query, __FILE__, __LINE__, true);
    if (!$res) {
        $url = ROOTDIR . '/install/install.php';
        header('Location: ' . $url);
        $error_msg = common_header('Database not found');
        $error_msg .= redirectJS($url);
        $error_msg .= 'DB not found. Going to installation page.';
        $error_msg .= common_bottom();
        echo $error_msg;
        die;
    }
    $query = "SHOW TABLES";
    $tableslist = common_query($query, __FILE__, __LINE__, true);
    // this is unlikely to happen
    if (!$tableslist) {
        $url = ROOTDIR . '/install/install.php';
        header('Location: ' . $url);
        $error_msg = common_header('Database tables not found');
        $error_msg .= redirectJS($url);
        $error_msg .= 'No table found on the database. Going to installation page.';
        $error_msg .= common_bottom();
        echo $error_msg;
        die;
    }
    $numtables = mysql_num_rows($tableslist);
    if (!$numtables) {
        $url = ROOTDIR . '/install/install.php';
        header('Location: ' . $url);
        $error_msg = common_header('Database tables not found');
        $error_msg .= redirectJS($url);
        $error_msg .= 'No table found on the database. Going to installation page.';
        $error_msg .= common_bottom();
        echo $error_msg;
        die;
    }
}
this is done by updating a timestamp field in the sources table
and by checking (if the waiter changed)
if the elapsed time from the last update is > than the config lock_time
*/
if (isset($_SESSION['sourceid']) && $_SESSION['sourceid']) {
    if (table_lock_check($_SESSION['sourceid'])) {
        $remaining_time = table_lock_remaining_time($_SESSION['sourceid']);
        $user = new user($_SESSION['userid']);
        if ($remaining_time == 0) {
            $remaining_time = 1;
        }
        $error_msg = common_header('Tavolina ne perdorim');
        $error_msg .= navbar_lock_retry_pos('');
        $error_msg .= ucfirst(phr('TABLE_ALREADY_IN_USE_ERROR')) . ' ' . '.<br><br>' . "\n";
        $error_msg .= ucfirst(phr('IF_YOU_ARE_NOT_DISCONNECT_0')) . ' <b>' . $user->data['name'] . '</b> ' . ucfirst(phr('IF_YOU_ARE_NOT_DISCONNECT_1')) . '<br>' . "\n";
        $error_msg .= common_bottom();
        die($error_msg);
    }
}
/*
We get the printed categories flag, and write to $_SESSION['catprinted'][]
*/
if (isset($_SESSION['sourceid'])) {
    $query = "SELECT * FROM `sources` WHERE `id`='" . $_SESSION['sourceid'] . "'";
    $res = common_query($query, __FILE__, __LINE__);
    if (!$res) {
        return ERR_MYSQL;
    }
    $arr = mysql_fetch_array($res);
    $catprinted_total = $arr['catprinted'];
    $catprinted_total = explode(" ", $catprinted_total);