46 lines
1.6 KiB
Python
46 lines
1.6 KiB
Python
import asyncio
|
|
import locale
|
|
import uuid
|
|
|
|
from arq import ArqRedis, create_pool
|
|
from sqlalchemy import select
|
|
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.execute(select(Device.section_id).where(Device.id == device_id)).scalar_one()
|
|
printer = db.execute(
|
|
select(Printer)
|
|
.join(SectionPrinter.printer)
|
|
.where(SectionPrinter.section_id == section_id)
|
|
.where(SectionPrinter.sale_category_id == None) # noqa: E711
|
|
).scalar_one()
|
|
|
|
loop = asyncio.new_event_loop()
|
|
redis: ArqRedis = loop.run_until_complete(create_pool(redis_settings))
|
|
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()
|
|
|
|
|
|
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
|