barker/barker/barker/printing/kot.py

125 lines
4.4 KiB
Python

import asyncio
import uuid
from datetime import timedelta
from typing import List
from arq import ArqRedis, create_pool
from barker.core.config import settings
from sqlalchemy import and_, or_
from sqlalchemy.orm import Session
from ..core.arq import settings as redis_settings
from ..models.inventory import Inventory
from ..models.kot import Kot
from ..models.printer import Printer
from ..models.product_version import ProductVersion
from ..models.section_printer import SectionPrinter
from ..models.voucher import Voucher
def design_kot(voucher: Voucher, kot: Kot, items: List[Inventory], copy_number: int, db: Session) -> str:
date = voucher.date + timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)
s = (
"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} {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:
product: ProductVersion = (
db.query(ProductVersion)
.filter(
and_(
ProductVersion.product_id == item.product_id,
or_(
ProductVersion.valid_from == None, # noqa: E711
ProductVersion.valid_from
<= (voucher.date - timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)).date(),
),
or_(
ProductVersion.valid_till == None, # noqa: E711
ProductVersion.valid_till
>= (voucher.date - timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)).date(),
),
)
)
.first()
)
name = "H H " + product.full_name if item.is_happy_hour else product.full_name
s += "\n\r" + f"{item.quantity:6.2} x {name:<33}"
for m in item.modifiers:
s += "\n\r" + f" --- {m.modifier.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:
product: ProductVersion = (
db.query(ProductVersion)
.filter(
and_(
ProductVersion.product_id == item.product_id,
or_(
ProductVersion.valid_from == None, # noqa: E711
ProductVersion.valid_from
<= (voucher.date - timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)).date(),
),
or_(
ProductVersion.valid_till == None, # noqa: E711
ProductVersion.valid_till
>= (voucher.date - timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)).date(),
),
)
)
.first()
)
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 == product.menu_category_id,
SectionPrinter.menu_category_id == None, # noqa: E711
)
)
.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)
redis: ArqRedis = asyncio.run(create_pool(redis_settings))
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, db)
asyncio.run(
redis.enqueue_job(
"sent_to_printer",
data,
printer.address,
"\x1dV\x41\x03",
_queue_name=f"barker:print:{printer.name}",
)
)