import uuid from typing import List from sqlalchemy import or_ from sqlalchemy.orm import Session from barker.models import SectionPrinter, Printer, Voucher, Kot, Inventory from barker.worker import sent_to_printer def design_kot(voucher: Voucher, kot: Kot, items: List[Inventory], copy_number: int) -> str: 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: 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.delay(data, printer.address, printer.cut_code)