function print_orders($sourceid)
{
    /*
    name:
    print_orders($sourceid)
    returns:
    0 - no error
    1 - no orders to be printed
    2 - template parsing error
    3 - error setting orders printed
    other - mysql error number
    */
    $sourceid = $_SESSION['sourceid'];
    debug_msg(__FILE__, __LINE__, "BEGIN PRINTING");
    $query = "SELECT * FROM `orders` WHERE `sourceid`='{$sourceid}' AND `printed` IS NULL AND `suspend`='0' ORDER BY dest_id ASC, priority ASC, associated_id ASC, id ASC";
    $res = common_query($query, __FILE__, __LINE__);
    if (!$res) {
        return mysql_errno();
    }
    if (!mysql_num_rows($res)) {
        return ERR_ORDER_NOT_FOUND;
    }
    $newassociated_id = "";
    $tablenum = get_db_data(__FILE__, __LINE__, $_SESSION['common_db'], 'sources', "name", $sourceid);
    $tpl_print = new template();
    $output['orders'] = '';
    $msg = "";
    while ($arr = mysql_fetch_array($res)) {
        $oldassociated_id = $newassociated_id;
        $newassociated_id = $arr['associated_id'];
        if (isset($priority)) {
            $oldpriority = $priority;
        } else {
            $oldpriority = 0;
        }
        $priority = $arr['priority'];
        if ($oldassociated_id != "") {
            $olddestid = get_db_data(__FILE__, __LINE__, $_SESSION['common_db'], 'dishes', "destid", get_db_data(__FILE__, __LINE__, $_SESSION['common_db'], 'orders', 'dishid', $oldassociated_id));
            $olddest = get_db_data(__FILE__, __LINE__, $_SESSION['common_db'], 'dests', "dest", $olddestid);
            $olddestname = get_db_data(__FILE__, __LINE__, $_SESSION['common_db'], 'dests', "name", $olddestid);
        } else {
            $olddestid = 0;
        }
        $destid = get_db_data(__FILE__, __LINE__, $_SESSION['common_db'], 'dishes', "destid", get_db_data(__FILE__, __LINE__, $_SESSION['common_db'], 'orders', 'dishid', $newassociated_id));
        $dest = get_db_data(__FILE__, __LINE__, $_SESSION['common_db'], 'dests', "dest", $destid);
        $destname = get_db_data(__FILE__, __LINE__, $_SESSION['common_db'], 'dests', "name", $destid);
        $dest_language = get_db_data(__FILE__, __LINE__, $_SESSION['common_db'], 'dests', "language", $destid);
        if ($destid != $olddestid || $priority != $oldpriority) {
            if ($destid != $olddestid && $olddestid != "") {
                $tpl_print->assign("date", printer_print_date());
                $tpl_print->assign("gonow", printer_print_gonow($oldpriority, $dest_language));
                $tpl_print->assign("page_cut", printer_print_cut());
                // strips the last newline that has been put
                $output['orders'] = substr($output['orders'], 0, strlen($output['orders']) - 1);
                if (table_is_takeaway($sourceid)) {
                    $print_tpl_file = 'ticket_takeaway';
                } else {
                    $print_tpl_file = 'ticket';
                }
                if ($err = $tpl_print->set_print_template_file($olddestid, $print_tpl_file)) {
                    return $err;
                }
                if ($err = $tpl_print->parse()) {
                    $msg = "Error in " . __FUNCTION__ . " - ";
                    $msg .= 'error: ' . $err . "\n";
                    echo nl2br($msg) . "\n";
                    error_msg(__FILE__, __LINE__, $msg);
                    return ERR_PARSING_TEMPLATE;
                }
                $tpl_print->restore_curly();
                $msg = $tpl_print->getOutput();
                $tpl_print->reset_vars();
                $output['orders'] = '';
                $msg = str_replace("'", "", $msg);
                if ($outerr = print_line($olddestid, $msg)) {
                    return $outerr;
                }
            } elseif ($priority != $oldpriority && $oldpriority != "") {
                $tpl_print->assign("date", printer_print_date());
                $tpl_print->assign("gonow", printer_print_gonow($oldpriority, $dest_language));
                $tpl_print->assign("page_cut", printer_print_cut());
                // strips the last newline that has been put
                $output['orders'] = substr($output['orders'], 0, strlen($output['orders']) - 1);
                if (table_is_takeaway($sourceid)) {
                    $print_tpl_file = 'ticket_takeaway';
                } else {
                    $print_tpl_file = 'ticket';
                }
                if ($err = $tpl_print->set_print_template_file($destid, $print_tpl_file)) {
                    return $err;
                }
                if ($err = $tpl_print->parse()) {
                    $msg = "Error in " . __FUNCTION__ . " - ";
                    $msg .= 'error: ' . $err . "\n";
                    error_msg(__FILE__, __LINE__, $msg);
                    echo nl2br($msg) . "\n";
                    return ERR_PARSING_TEMPLATE;
                }
                $tpl_print->restore_curly();
                $msg = $tpl_print->getOutput();
                $tpl_print->reset_vars();
                $output['orders'] = '';
                $msg = str_replace("'", "", $msg);
                if ($outerr = print_line($destid, $msg)) {
                    return $outerr;
                }
            }
            if (table_is_takeaway($sourceid)) {
                $takeaway_data = takeaway_get_customer_data($sourceid);
                $output['takeaway'] = ucfirst(lang_get($dest_language, 'PRINTS_TAKEAWAY')) . " - ";
                $output['takeaway'] .= $takeaway_data['takeaway_hour'] . ":" . $takeaway_data['takeaway_minute'] . "\n";
                $output['takeaway'] .= $takeaway_data['takeaway_surname'] . "\n";
                $tpl_print->assign("takeaway", $output['takeaway']);
            }
            $output['table'] = ucfirst(lang_get($dest_language, 'PRINTS_TABLE')) . ": " . $tablenum;
            $tpl_print->assign("table", $output['table']);
            $user = new user($_SESSION['userid']);
            $output['waiter'] = ucfirst(lang_get($dest_language, 'PRINTS_WAITER')) . ": " . $user->data['name'];
            $tpl_print->assign("waiter", $output['waiter']);
            $output['priority'] = ucfirst(lang_get($dest_language, 'PRINTS_PRIORITY')) . ": " . $priority . "\n";
            $tpl_print->assign("priority", $output['priority']);
            $output['people'] = ucfirst(lang_get($dest_language, 'PRINTS_PEOPLE')) . ": " . table_people_number($sourceid) . "\n";
            $tpl_print->assign("people", $output['people']);
            $table = new table($sourceid);
            $table->fetch_data(true);
            if ($cust_id = $table->data['customer']) {
                $cust = new customer($cust_id);
                $output['customer'] = ucfirst(lang_get($dest_language, 'CUSTOMER')) . ": " . $cust->data['surname'] . ' ' . $cust->data['name'];
                $tpl_print->assign("customer_name", $output['customer']);
                $output['customer'] = $cust->data['address'];
                $tpl_print->assign("customer_address", $output['customer']);
                $output['customer'] = $cust->data['zip'];
                $tpl_print->assign("customer_zip_code", $output['customer']);
                $output['customer'] = $cust->data['city'];
                $tpl_print->assign("customer_city", $output['customer']);
                $output['customer'] = ucfirst(lang_get($dest_language, 'VAT_ACCOUNT')) . ": " . $cust->data['vat_account'];
                $tpl_print->assign("customer_vat_account", $output['customer']);
            }
        }
        $output['orders'] .= printer_print_row($arr, $destid);
        $printed_orders[] = $arr['id'];
        if ($newassociated_id != $oldassociated_id) {
            // if we're in this function, it means that we changed associated_id id
            // and also that mods have been printed on the same sheet
            if (CONF_PRINT_BARCODES && $arr['dishid'] != MOD_ID) {
                $output['orders'] .= print_barcode($newassociated_id);
            }
        }
        if (CONF_PRINT_BARCODES && $arr['dishid'] != MOD_ID) {
            $output['orders'] .= print_barcode($newassociated_id);
        }
        $tpl_print->assign("orders", $output['orders']);
    }
    $destid = get_db_data(__FILE__, __LINE__, $_SESSION['common_db'], 'dishes', "destid", get_db_data(__FILE__, __LINE__, $_SESSION['common_db'], 'orders', 'dishid', $newassociated_id));
    $dest = get_db_data(__FILE__, __LINE__, $_SESSION['common_db'], 'dests', "dest", $destid);
    $destname = get_db_data(__FILE__, __LINE__, $_SESSION['common_db'], 'dests', "name", $destid);
    $dest_language = get_db_data(__FILE__, __LINE__, $_SESSION['common_db'], 'dests', "language", $destid);
    if (CONF_PRINT_BARCODES) {
        $tpl_print->assign("barcode", print_barcode($newassociated_id));
    }
    $tpl_print->assign("date", printer_print_date());
    $tpl_print->assign("gonow", printer_print_gonow($priority, $dest_language));
    $tpl_print->assign("page_cut", printer_print_cut());
    // strips the last newline that has been put
    $output['orders'] = substr($output['orders'], 0, strlen($output['orders']) - 1);
    if (table_is_takeaway($sourceid)) {
        $print_tpl_file = 'ticket_takeaway';
    } else {
        $print_tpl_file = 'ticket';
    }
    if ($err = $tpl_print->set_print_template_file($destid, $print_tpl_file)) {
        return $err;
    }
    if ($err = $tpl_print->parse()) {
        $err_msg = "Error in " . __FUNCTION__ . " - ";
        $err_msg .= 'error: ' . $err . "\n";
        error_msg(__FILE__, __LINE__, $err_msg);
        echo nl2br($err_msg) . "\n";
        return ERR_PARSING_TEMPLATE;
    }
    $tpl_print->restore_curly();
    $msg = $tpl_print->getOutput();
    $tpl_print->reset_vars();
    $output['orders'] = '';
    $msg = str_replace("'", "", $msg);
    if ($outerr = print_line($destid, $msg)) {
        return $outerr;
    }
    foreach ($printed_orders as $val) {
        if ($err = print_set_printed($val)) {
            return $err;
        }
    }
    // there was an error setting orders as printed
    if ($err) {
        return ERR_ORDER_NOT_SET_AS_PRINTED;
    }
    return 0;
}
function bill_print()
{
    /*
    name:
    bill_print()
    returns:
    0 - no error
    1 - Printer not found for output tyoe
    2 - No order selected
    3 - Printing error
    other - mysql error number
    */
    // type: 	0: reserved
    //			1: bill
    //			2. invoice
    //			3. receipt
    //	we have to translate them to the mgmt_type values in order to be correctely
    //	written and read in the log
    //	mgmt_type:	3: invoice
    //				4: bill
    //				5: receipt
    global $tpl;
    global $output_page;
    $output['orders'] = '';
    $output_page = '';
    //connect to printer by client IP
    $clientip = "";
    if (isset($clientip)) {
        unset($clientip);
    }
    $clientip = getenv('REMOTE_ADDR');
    //end:connect to printer by client IP
    if ($_SESSION['bill_printed']) {
        return 0;
    }
    $_SESSION['bill_printed'] = 1;
    $type = $_SESSION['type'];
    $keep_separated = bill_check_keep_separated();
    $type = receipt_type_waiter2mgmt($type);
    // CRYPTO
    if (!bill_check_empty()) {
        $receipt_id = receipt_insert($_SESSION['account'], $type);
    }
    $printing_enabled = $arr['print_bill'];
    $tpl_print = new template();
    switch ($_SESSION['type']) {
        case 1:
            $query = "SELECT * FROM `dests` WHERE `bill`='1' AND `deleted`='0'";
            $template_type = 'bill';
            break;
        case 2:
            $query = "SELECT * FROM `dests` WHERE `invoice`='1' AND `deleted`='0'";
            $template_type = 'invoice';
            break;
        case 3:
            $query = "SELECT * FROM `dests` WHERE `receipt`='1' AND `deleted`='0'";
            $template_type = 'receipt';
            break;
        default:
            $query = "SELECT * FROM `dests` WHERE `bill`='1' AND `deleted`='0'";
            $template_type = 'bill';
    }
    $res = common_query($query, __FILE__, __LINE__);
    if (!$res) {
        return ERR_MYSQL;
    }
    //connect to printer by client IP
    while ($row = mysql_fetch_array($res)) {
        if ($row['dest_ip'] == '') {
            if ($row['dest'] != '') {
                $destid = $row['id'];
                $dest_language = get_db_data(__FILE__, __LINE__, $_SESSION['common_db'], 'dests', "language", $destid);
            } else {
                return ERR_PRINTER_NOT_FOUND_FOR_SELECTED_TYPE;
            }
        } elseif ($row['dest'] != '' && $row['dest_ip'] != '') {
            $ippart = explode("|", $row['dest_ip']);
            if (in_array($clientip, $ippart)) {
                $destid = $row['id'];
                break;
            }
            $dest_language = get_db_data(__FILE__, __LINE__, $_SESSION['common_db'], 'dests', "language", $destid);
        } else {
            return ERR_PRINTER_NOT_FOUND_FOR_SELECTED_TYPE;
        }
    }
    if ($err = $tpl_print->set_print_template_file($destid, $template_type)) {
        return $err;
    }
    // reset the counter and the message to be sent to the printer
    $total = 0;
    $msg = "";
    $tablenum = get_db_data(__FILE__, __LINE__, $_SESSION['common_db'], 'sources', "name", $_SESSION['sourceid']);
    $output['table'] = ucfirst(lang_get($dest_language, 'PRINTS_TABLE')) . " {$tablenum} \n";
    $tpl_print->assign("table", $output['table']);
    // writes the table num to video
    $output_page .= ucfirst(phr('TABLE_NUMBER')) . ": {$tablenum}     ";
    $table = new table($_SESSION['sourceid']);
    $table->fetch_data(true);
    if ($cust_id = $table->data['customer']) {
        $cust = new customer($cust_id);
        $output['customer'] = ucfirst(lang_get($dest_language, 'CUSTOMER')) . ": " . $cust->data['surname'] . ' ' . $cust->data['name'];
        $tpl_print->assign("customer_name", $output['customer']);
        $output['customer'] = $cust->data['address'];
        $tpl_print->assign("customer_address", $output['customer']);
        $output['customer'] = $cust->data['zip'];
        $tpl_print->assign("customer_zip_code", $output['customer']);
        $output['customer'] = $cust->data['city'];
        $tpl_print->assign("customer_city", $output['customer']);
        $output['customer'] = ucfirst(lang_get($dest_language, 'VAT_ACCOUNT')) . ": " . $cust->data['vat_account'];
        $tpl_print->assign("customer_vat_account", $output['customer']);
    }
    if (bill_check_empty()) {
        return ERR_NO_ORDER_SELECTED;
    }
    //mizuko : swap qty with name
    $output_page .= "<table bgcolor=\"" . COLOR_TABLE_GENERAL . "\">\r\n\t<thead>\r\n\t<tr>\r\n\t<th scope=col>" . ucfirst(phr('NAME')) . "</th>\r\n\t<th scope=col>" . ucfirst(phr('QUANTITY_ABBR')) . "</th>\r\n\t<th scope=col>" . ucfirst(phr('PRICE')) . "</th>\r\n\t</tr>\r\n\t</thead>\r\n\t<tbody>";
    $class = COLOR_ORDER_PRINTED;
    ksort($_SESSION['separated']);
    // the next for prints the list and the chosen dishes
    for (reset($_SESSION['separated']); list($key, $value) = each($_SESSION['separated']);) {
        $output['orders'] .= bill_print_row($key, $value, $destid);
    }
    $tpl_print->assign("orders", $output['orders']);
    if ($_SESSION['discount']['type'] == "amount" || $_SESSION['discount']['type'] == "percent") {
        $output['discount'] = bill_print_discount($receipt_id, $destid);
        $tpl_print->assign("discount", $output['discount']);
    }
    $total = bill_calc_vat();
    $total_discounted = bill_calc_discount($total);
    // updates the receipt value, has to be before print totals!
    receipt_update_amounts($_SESSION['account'], $total_discounted, $receipt_id);
    $output['total'] = bill_print_total($receipt_id, $destid);
    $tpl_print->assign("total", $output['total']);
    if (SHOW_CHANGE == 1) {
        $output['change'] = bill_print_change($total_discounted['total']);
        $tpl_print->assign("change", $output['change']);
    }
    //mizuko
    $user = new user($_SESSION['userid']);
    $output['waiter'] = ucfirst(lang_get($dest_language, 'PRINTS_WAITER')) . ": " . $user->data['name'];
    $tpl_print->assign("waiter", $output['waiter']);
    $tpl_print->assign("date", printer_print_date());
    //end mizuko
    $output_page .= "\r\n\t</tbody>\r\n\t</table>";
    $output['receipt_id'] = bill_print_receipt_id($receipt_id, $destid);
    $tpl_print->assign("receipt_id", $output['receipt_id']);
    $output['taxes'] = bill_print_taxes($receipt_id, $destid);
    $tpl_print->assign("taxes", $output['taxes']);
    if ($err = $tpl_print->parse()) {
        $msg = "Error in " . __FUNCTION__ . " - ";
        $msg .= 'error: ' . $err . "\n";
        error_msg(__FILE__, __LINE__, $msg);
        echo nl2br($msg) . "\n";
        return ERR_PARSING_TEMPLATE;
    }
    $tpl_print->restore_curly();
    $msg = $tpl_print->getOutput();
    $msg = str_replace("'", "", $msg);
    if ($printing_enabled) {
        if ($err = print_line($arr['id'], $msg)) {
            // the process is stopped so we delete the created receipt
            receipt_delete($_SESSION['account'], $receipt_id);
            return $err;
        }
    }
    ksort($_SESSION['separated']);
    // sets the log
    for (reset($_SESSION['separated']); list($key, $value) = each($_SESSION['separated']);) {
        if ($err_logger = bill_logger($key, $receipt_id)) {
            debug_msg(__FILE__, __LINE__, __FUNCTION__ . ' - receipt_id: ' . $receipt_id . ' - logger return code: ' . $err_logger);
        } else {
            debug_msg(__FILE__, __LINE__, __FUNCTION__ . ' - receipt_id: ' . $receipt_id . ' - logged');
        }
    }
    return 0;
}