diff --git a/barker/models/master.py b/barker/models/master.py index 266ef81..63fbd44 100644 --- a/barker/models/master.py +++ b/barker/models/master.py @@ -303,7 +303,7 @@ class SectionPrinter(Base): menu_category_id = Column("menu_category_id", UUID(as_uuid=True), ForeignKey("menu_categories.id")) section_id = Column("section_id", UUID(as_uuid=True), ForeignKey("sections.id"), nullable=False) printer_id = Column("printer_id", UUID(as_uuid=True), ForeignKey("printers.id"), nullable=False) - copies = Column("copies", Numeric, nullable=False) + copies = Column("copies", Integer, nullable=False) menu_category = relationship("MenuCategory", backref="section_printers") section = relationship("Section", backref="section_printers") diff --git a/barker/printing/__init__.py b/barker/printing/__init__.py new file mode 100644 index 0000000..cab0429 --- /dev/null +++ b/barker/printing/__init__.py @@ -0,0 +1,78 @@ +import re +import socket +import sys +import uuid + +from sqlalchemy import or_ +from sqlalchemy.orm import Session + +from barker.models import SectionPrinter, Printer, Voucher + + +def sent_to_printer(data, address, cut_code): + print(data, address, cut_code) + try: + regex = re.compile(r'pdl://(?P[\w\d.-]+):(?P[\d]+)') + match = regex.match(address) + s = socket.socket() + s.connect((match.group("host"), int(match.group("port")))) + s.send(bytearray(data + cut_code, "ascii")) + except LookupError as e: + print("Lookup error:", e) + except: + print("Unexpected error:", sys.exc_info()[0]) + finally: + s.close() + + +def design_kot(voucher, kot, items, copy_number): + s = ( + f"KOT / BOT".center(42) + + "\n\r" + f"Copy No. {copy_number}:".center(42) + + "\n\r" + "".ljust(42, "-") + + "\n\r" + f"KOT ID : K-{voucher.kot_id:>5}/S-{kot.code:>5} {kot.date:%d-%b-%Y %H:%M}" + + "\n\r" + f"Table No.: {voucher.food_table.name}" + + "\n\r" + "".ljust(42, "-") + + "\n\r" + " Qty. x Name " + + "\n\r" + "".ljust(42, "-") + ) + for item in items: + name = ( + "H H " + item.product.full_name + if item.is_happy_hour + else item.product.full_name + ) + s += "\n\r" + f"{item.quantity:6.2} x {name:>33}" + for m in item.modifiers: + s += "\n\r" + f" --- {m.name:>32}" + s += "\n\r" + "".ljust(42, "-") + return s + + +def print_kot(voucher_id: uuid.UUID, db: Session): + voucher: Voucher = db.query(Voucher).filter(Voucher.id == voucher_id).first() + my_hash = {} + kot = voucher.kots[-1] + for item in kot.inventories: + printer, copies = ( + db.query(Printer, SectionPrinter.copies) + .join(SectionPrinter.printer) + .filter(SectionPrinter.section_id == voucher.food_table.section_id) + .filter( + or_( + SectionPrinter.menu_category_id == item.product.menu_category_id, + SectionPrinter.menu_category_id == None, + ) + ).order_by(SectionPrinter.menu_category_id) + .first() + ) + key = (printer.id, copies) + if key not in my_hash: + my_hash[key] = (printer, []) + my_hash[key][1].append(item) + for key, value in my_hash.items(): + printer_id, copies = key + printer, items = value + for c in range(int(copies)): + data = design_kot(voucher, kot, items, c) + sent_to_printer(data, printer.address, printer.cut_code) diff --git a/barker/routers/voucher/save.py b/barker/routers/voucher/save.py index e02a458..25ac245 100644 --- a/barker/routers/voucher/save.py +++ b/barker/routers/voucher/save.py @@ -20,6 +20,7 @@ from ...routers.voucher import ( check_permissions, get_guest_book, ) +from barker.printing import print_kot router = APIRouter() @@ -50,6 +51,8 @@ def save( if update_table: do_update_table(item, guest_book, db) db.commit() + # print_kot(item.id, db) + except SQLAlchemyError as e: db.rollback() raise HTTPException(