44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
import asyncio
|
|
import locale
|
|
import uuid
|
|
|
|
from arq import ArqRedis, create_pool
|
|
from sqlalchemy.orm import Session
|
|
|
|
from ..core.arq import settings as redis_settings
|
|
from ..models.device import Device
|
|
from ..models.printer import Printer
|
|
from ..models.section_printer import SectionPrinter
|
|
from ..schemas.discount_report import DiscountReport
|
|
from . import currency_format
|
|
|
|
|
|
def print_discount_report(report: DiscountReport, device_id: uuid.UUID, db: Session):
|
|
locale.setlocale(locale.LC_MONETARY, "en_IN")
|
|
data = design_discount_report(report)
|
|
section_id = db.query(Device.section_id).filter(Device.id == device_id).scalar()
|
|
printer = (
|
|
db.query(Printer)
|
|
.join(SectionPrinter.printer)
|
|
.filter(SectionPrinter.section_id == section_id)
|
|
.filter(SectionPrinter.menu_category_id == None) # noqa: E711
|
|
.first()
|
|
)
|
|
|
|
redis: ArqRedis = asyncio.run(create_pool(redis_settings))
|
|
asyncio.run(
|
|
redis.enqueue_job(
|
|
"sent_to_printer", data, printer.address, "\x1dV\x41\x03", _queue_name=f"barker:print:{printer.name}"
|
|
)
|
|
)
|
|
|
|
|
|
def design_discount_report(report: DiscountReport):
|
|
s = f"{report.start_date:%d-%b-%Y} To {report.finish_date:%d-%b-%Y}".center(42)
|
|
s += "\n\r" + "-" * 42
|
|
s += f"\n\r{'Name': <29.29} {'Amount': >12.12}"
|
|
for item in report.amounts:
|
|
s += f"\n\r{item.name: <29.29} {currency_format(item.amount): >12}"
|
|
s += "\n\r" + "=" * 42
|
|
return s
|