Implement transaction printing.

This commit is contained in:
crudo 2017-03-25 02:38:02 +01:00
parent 67ae28fcac
commit 389ad04e19
4 changed files with 59 additions and 2 deletions

View File

@ -7,3 +7,7 @@ Path = pos.db
[FLASK]
SECRET_KEY = CHANGE_ME_NOW!
[PRINTER]
Host = 192.168.1.100
Post = 9100

View File

@ -11,3 +11,7 @@ Password = secret
[FLASK]
SECRET_KEY = CHANGE_ME_NOW!
[PRINTER]
Host = 192.168.1.100
Post = 9100

View File

@ -8,3 +8,4 @@ tabulate
PyYAML
flask>=0.12.0
flask_login>=0.4.0
python-escpos

52
web.py
View File

@ -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()