/**
* My Handy Restaurant
*
* http://www.myhandyrestaurant.org
*
* My Handy Restaurant is a restaurant complete management tool.
* Visit {@link http://www.myhandyrestaurant.org} for more info.
* Copyright (C) 2003-2005 Fabio De Pascale
* 
* This program 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; either version 2 of the License, or
* (at your option) any later version.
* 
* This program 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* @author		Fabio 'Kilyerd' De Pascale <*****@*****.**>
* @package		MyHandyRestaurant
* @copyright		Copyright 2003-2005, Fabio De Pascale
* @copyright	Copyright 2006-2012, Gjergj Sheldija
*/
function printing_choose($from_bill_print = false)
{
    global $tpl;
    if (!takeaway_is_set($_SESSION['sourceid'])) {
        $tmp = '<font color="Red">' . ucfirst(phr('SET_TAKEAWAY_SURNAME_FIRST')) . '</font>';
        $tpl->append('messages', $tmp);
        orders_list();
        return 0;
    }
    $user = new user($_SESSION['userid']);
    if (table_is_closed($_SESSION['sourceid']) && (!$user->level[USER_BIT_CASHIER] || $from_bill_print)) {
        table_closed_interface();
        return 0;
    }
    $tpl->set_waiter_template_file('printing');
    $tmp = printing_commands();
    $tpl->append('commands', $tmp);
    $tmp = navbar_empty();
    $tpl->assign('navbar', $tmp);
}
function orders_list()
{
    global $tpl;
    // use session to decide wether to show the orders list or not
    // TODO: add get_conf here
    if (!isset($_SESSION['show_orders_list'])) {
        $_SESSION['show_orders_list'] = false;
    }
    $show_orders = $_SESSION['show_orders_list'];
    unset($_SESSION['select_all']);
    $_SESSION['go_back_to_cat'] = 0;
    $user = new user($_SESSION['userid']);
    if (table_is_closed($_SESSION['sourceid']) && !$user->level[USER_BIT_CASHIER]) {
        table_closed_interface();
        return 0;
    }
    $_SESSION['order_added'] = 0;
    $tpl->set_waiter_template_file('orders');
    if (table_is_takeaway($_SESSION['sourceid'])) {
        $tpl->set_waiter_template_file('orders_takeaway');
        takeaway_form();
    }
    $table = new table($_SESSION['sourceid']);
    $table->fetch_data(true);
    if ($cust_id = $table->data['customer']) {
        $cust = new customer($cust_id);
        $tmp = ucphr('CUSTOMER') . ': ' . $cust->data['surname'];
        $tmp .= ' <a href="orders.php?command=customer_search">' . ucphr('EDIT') . '</a>/';
        $tmp .= '<a href="orders.php?command=set_customer&amp;data[customer]=0">' . ucphr('REMOVE') . '</a>';
        $tmp .= '<br/>';
    } else {
        $tmp = '<a href="orders.php?command=customer_search">' . ucfirst(phr('INSERT_CUSTOMER_DATA')) . '</a><br/>';
    }
    $tpl->append('commands', $tmp);
    if (!orders_service_fee_exists() && get_conf(__FILE__, __LINE__, 'service_fee_use')) {
        $tmp = '<a href="orders.php?command=create&amp;dishid=' . SERVICE_ID . '">' . ucfirst(phr('CREATE_SERVICE_FEE')) . '</a><br/>';
        $tpl->append('commands', $tmp);
    }
    $associated_waiter = table_is_associated();
    if (get_conf(__FILE__, __LINE__, "disassociation_allow") && $associated_waiter && ($associated_waiter == $_SESSION['userid'] || $user->level[USER_BIT_CASHIER])) {
        $tmp = '<a href="orders.php?command=dissociate">' . ucfirst(phr('DISSOCIATE')) . '</a><br/>';
        $tpl->append('commands', $tmp);
    }
    if ($user->level[USER_BIT_CASHIER]) {
        $tmp = '<a href="orders.php?command=ask_move">' . ucfirst(phr('MOVE_TABLE')) . '</a><br/>';
        $tpl->append('commands', $tmp);
    }
    if ($user->level[USER_BIT_CASHIER] && table_is_closed($_SESSION['sourceid'])) {
        $tmp = '<a href="orders.php?command=reopen_confirm">' . ucfirst(phr('REOPEN_TABLE')) . '</a><br/>';
        $tpl->append('commands', $tmp);
    }
    if ($_SESSION['show_orders_list'] == false) {
        $desc = ucfirst(phr('SHOW_ORDERS'));
    } else {
        $desc = ucfirst(phr('HIDE_ORDERS'));
    }
    $tmp = '<a href="orders.php?command=set_show_orders">' . $desc . '</a><br/>';
    $tpl->append('commands', $tmp);
    $tmp = categories_list();
    $tpl->assign('categories', $tmp);
    $tmp = letters_list();
    $tpl->assign('letters', $tmp);
    if (CONF_FAST_ORDER) {
        $tmp = order_fast_dishid_form();
        $tpl->assign('fast_order_id', $tmp);
    } else {
        $tmp = keys_orders();
        $tpl->append('scripts', $tmp);
    }
    // use session to decide wether to show the orders list or not
    if (!isset($_SESSION['show_toplist'])) {
        $_SESSION['show_toplist'] = get_conf(__FILE__, __LINE__, "top_list_show_top");
    }
    if ($_SESSION['show_toplist']) {
        toplist_show();
    } elseif (get_conf(__FILE__, __LINE__, "top_list_show_top")) {
        $tmp = '<a href="orders.php?command=set_show_toplist">' . ucphr('SHOW_TOPLIST') . '</a><br/>';
        $tpl->assign('toplist', $tmp);
    }
    $tmp = command_bar_table_vertical();
    $tpl->assign('vertical_navbar', $tmp);
    if ($show_orders) {
        $table->list_orders();
    }
    if (get_conf(__FILE__, __LINE__, "show_summary")) {
        $query = "SELECT * FROM `orders`WHERE `sourceid`='" . $_SESSION['sourceid'] . "' AND `id`=`associated_id` ORDER BY `timestamp` DESC LIMIT 1";
        $res = common_query($query, __FILE__, __LINE__);
        if (!$res) {
            return ERR_MYSQL;
        }
        $arr = mysql_fetch_array($res);
        $mods = get_conf(__FILE__, __LINE__, "show_mods_in_summary");
        $table->list_orders('last_order', $arr['id'], $mods);
    }
    return 0;
}
        break;
    case 'closed':
        table_closed_interface();
        break;
    case 'pay':
        $err = table_pay($start_data['paid']);
        status_report('PAYMENT', $err);
        $err = table_clear();
        status_report('CLEARING', $err);
        table_cleared_interface();
        break;
    case 'clear':
        if (!$err) {
            table_cleared_interface();
        } else {
            table_closed_interface();
        }
        break;
    case 'none':
        break;
    default:
        orders_list();
        break;
}
// this line is already in waiter_start, but it's here repeated because of possible modifications from waiter start till now
$tmp = table_people_number_line($_SESSION['sourceid']);
$tpl->assign("people_number", $tmp);
// html closing stuff and disconnect line
$tmp = disconnect_line();
$tpl->assign('logout', $tmp);
// prints page generation time
/**
* Table closing confirmation page
*
* Template: question.tpl
* Assigns: question, navbar.
*
* @return integer error code
*/
function table_ask_close()
{
    global $tpl;
    if (!takeaway_is_set($_SESSION['sourceid'])) {
        $tmp = '$.growl("","<font color="Red">' . ucfirst(phr('SET_TAKEAWAY_SURNAME_FIRST')) . '</font>")';
        $tpl->append('messages', $tmp);
        orders_list();
        return 0;
    }
    if (table_is_closed($_SESSION['sourceid'])) {
        table_closed_interface();
        return 0;
    }
    $tpl->set_waiter_template_file('question');
    $tmp = '
	<FORM ACTION="orders.php" METHOD=POST name="form1">
	<INPUT TYPE="HIDDEN" NAME="command" VALUE="close">
	' . ucfirst(phr('CLOSE_TABLE_ASK')) . '
	</FORM>
	';
    $tpl->assign('question', $tmp);
    $tmp = navbar_form('form1', 'orders.php?command=list');
    $tpl->assign('navbar', $tmp);
    return 0;
}