diff --git a/docs/config_core/core_debug_sqlite.ini b/docs/config_core/core_debug_sqlite.ini index 43f365e..2f847e3 100644 --- a/docs/config_core/core_debug_sqlite.ini +++ b/docs/config_core/core_debug_sqlite.ini @@ -7,3 +7,7 @@ Path = pos.db [FLASK] SECRET_KEY = CHANGE_ME_NOW! + +[PRINTER] +Host = 192.168.1.100 +Post = 9100 diff --git a/docs/config_core/core_production_mysql.ini b/docs/config_core/core_production_mysql.ini index af4207c..6ef2a25 100644 --- a/docs/config_core/core_production_mysql.ini +++ b/docs/config_core/core_production_mysql.ini @@ -11,3 +11,7 @@ Password = secret [FLASK] SECRET_KEY = CHANGE_ME_NOW! + +[PRINTER] +Host = 192.168.1.100 +Post = 9100 diff --git a/requirements.txt b/requirements.txt index 66f9508..920c23a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,3 +8,4 @@ tabulate PyYAML flask>=0.12.0 flask_login>=0.4.0 +python-escpos diff --git a/web.py b/web.py index b9c1584..6457edb 100644 --- a/web.py +++ b/web.py @@ -1,4 +1,6 @@ from datetime import datetime +from time import sleep +import escpos.printer from flask import Flask, redirect, request, render_template, flash from flask_login import LoginManager, login_user, logout_user, login_required @@ -11,11 +13,20 @@ from pos.database import Database, User, Product, Event, Transaction, Order config = Config() debug = config.core['GENERAL'].getboolean('Debug', False) conf_db = config.core['DATABASE'] +conf_printer = config.core['PRINTER'] conf_flask = config.core['FLASK'] init_logging(config.logging) log = get_logger('web') +printer = escpos.printer.Network( + conf_printer['Host'], + port=conf_printer.getint('Port'), + timeout=15) + + +db = Database(**conf_db) + app = Flask(__name__) app.config.update( debug=debug, @@ -27,8 +38,6 @@ login_manager.init_app(app) login_manager.login_view = 'login' login_manager.login_message_category = 'error' -db = Database(**conf_db) - @login_manager.user_loader def load_user(uid): @@ -110,6 +119,43 @@ def sell_page(): title='Sell', event=event, products=products) +def print_orders(event, orders): + printer.open() + + printer.set(align='CENTER') + printer.image('static/img/macao-logo-printer.png', impl='bitImageColumn') + + printer.set(align='CENTER', text_type='B') + printer.text(event.name.upper()) + printer.text("\n\n") + + for o in orders: + printer.set(align='LEFT', width=2, height=2) + printer.text("{} x {}".format(o.quantity, o.product.name.upper())) + printer.text("\n") + + printer.text("\n") + + printer.cut() + printer.close() + sleep(0.7) + + +def print_transaction(transaction): + categorized_orders = {} + + for o in transaction.orders: + uid = o.product.category_uid + + if not categorized_orders.get(uid, False): + categorized_orders[uid] = [] + + categorized_orders[uid].append(o) + + for cat, orders in categorized_orders.items(): + print_orders(transaction.event, orders) + + @app.route('/sell', methods=['POST']) @login_required def sell(): @@ -135,6 +181,8 @@ def sell(): transaction = Transaction(event_uid=event.uid, orders=orders) session.add(transaction) + session.flush() + print_transaction(transaction) flash("Success!", 'success') return sell_page()