124 lines
4.5 KiB
Python
124 lines
4.5 KiB
Python
import asyncio
|
|
import uuid
|
|
|
|
from datetime import timedelta
|
|
from typing import Any
|
|
|
|
from arq import ArqRedis, create_pool
|
|
from barker.core.config import settings
|
|
from sqlalchemy import and_, or_, select
|
|
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_ = kot.date + timedelta(minutes=settings.TIMEZONE_OFFSET_MINUTES)
|
|
product_date = (
|
|
voucher.date + timedelta(minutes=settings.TIMEZONE_OFFSET_MINUTES - settings.NEW_DAY_OFFSET_MINUTES)
|
|
).date()
|
|
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.execute(
|
|
select(ProductVersion).where(
|
|
and_(
|
|
ProductVersion.product_id == item.product_id,
|
|
or_(
|
|
ProductVersion.valid_from == None, # noqa: E711
|
|
ProductVersion.valid_from <= product_date,
|
|
),
|
|
or_(
|
|
ProductVersion.valid_till == None, # noqa: E711
|
|
ProductVersion.valid_till >= product_date,
|
|
),
|
|
)
|
|
)
|
|
).scalar_one()
|
|
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.execute(select(Voucher).where(Voucher.id == voucher_id)).scalar_one()
|
|
my_hash: dict[tuple[uuid.UUID, int], tuple[Printer, list[Any]]] = {}
|
|
kot: Kot = voucher.kots[-1]
|
|
product_date = (
|
|
voucher.date + timedelta(minutes=settings.TIMEZONE_OFFSET_MINUTES - settings.NEW_DAY_OFFSET_MINUTES)
|
|
).date()
|
|
for item in kot.inventories:
|
|
product: ProductVersion = db.execute(
|
|
select(ProductVersion).where(
|
|
and_(
|
|
ProductVersion.product_id == item.product_id,
|
|
or_(
|
|
ProductVersion.valid_from == None, # noqa: E711
|
|
ProductVersion.valid_from <= product_date,
|
|
),
|
|
or_(
|
|
ProductVersion.valid_till == None, # noqa: E711
|
|
ProductVersion.valid_till >= product_date,
|
|
),
|
|
)
|
|
)
|
|
).scalar_one()
|
|
|
|
printer, copies = db.execute(
|
|
select(Printer, SectionPrinter.copies)
|
|
.join(SectionPrinter.printer)
|
|
.where(SectionPrinter.section_id == voucher.food_table.section_id)
|
|
.where(
|
|
or_(
|
|
SectionPrinter.sale_category_id == product.sale_category_id,
|
|
SectionPrinter.sale_category_id == None, # noqa: E711
|
|
)
|
|
)
|
|
.order_by(SectionPrinter.sale_category_id)
|
|
).first()
|
|
key = (printer.id, copies)
|
|
if key not in my_hash:
|
|
my_hash[key] = (printer, [])
|
|
my_hash[key][1].append(item)
|
|
loop = asyncio.new_event_loop()
|
|
redis: ArqRedis = loop.run_until_complete(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)
|
|
loop.run_until_complete(
|
|
redis.enqueue_job(
|
|
"sent_to_printer",
|
|
data,
|
|
printer.address,
|
|
printer.cut_code,
|
|
_queue_name=f"barker:print:{printer.name}",
|
|
)
|
|
)
|
|
loop.close()
|