barker/barker/barker/routers/voucher/show.py

140 lines
5.1 KiB
Python

import re
import uuid
from typing import Optional
from fastapi import APIRouter, HTTPException, status, Depends, Security
from sqlalchemy.orm import Session
from ...schemas.auth import UserToken
from ...core.security import get_current_active_user as get_user
from ...db.session import SessionLocal
from ...models import Voucher, Overview, FoodTable, GuestBook, VoucherType
router = APIRouter()
# Dependency
def get_db():
try:
db = SessionLocal()
yield db
finally:
db.close()
@router.get("/from-id/{id_}")
def from_id(
id_: str, db: Session = Depends(get_db), user: UserToken = Security(get_user),
):
item: Voucher = db.query(Voucher).filter(Voucher.id == id_).first()
return voucher_info(item)
@router.get("/from-bill/{id_}")
def from_bill(
id_: str, db: Session = Depends(get_db), user: UserToken = Security(get_user),
):
item: Voucher = db.query(Voucher)
if re.compile(r"^\d{2,}-\d{4}$").match(id_):
item = item.filter(Voucher.bill_id == int(id_.replace("-", "")), Voucher.voucher_type.in_([1, 3]),)
elif re.compile(r"^NC-\d+$").match(id_):
item = item.filter(Voucher.bill_id == int(id_.replace("NC-", "")), Voucher.voucher_type == 2,)
elif re.compile(r"^ST-\d+$").match(id_):
item = item.filter(Voucher.bill_id == int(id_.replace("ST-", "")), Voucher.voucher_type == 4,)
item = item.first()
if item is None:
return {}
return voucher_info(item)
@router.get("/from-table/{id_}")
def from_bill(
id_: str, # table id
v: Optional[uuid.UUID] = None, # voucher id
g: Optional[uuid.UUID] = None, # guest id
db: Session = Depends(get_db),
user: UserToken = Security(get_user),
):
if v is not None:
item = db.query(Overview).filter(Overview.voucher_id == v, Overview.food_table_id == id_,).first()
if item is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Voucher not found",
)
else:
return voucher_info(item.voucher)
table = db.query(FoodTable).filter(FoodTable.id == id_).first()
if g is not None:
guest = db.query(GuestBook).filter(GuestBook.id == g).first()
else:
guest = None
return voucher_blank(table, guest)
def voucher_info(item: Voucher):
return {
"id": item.id,
"date": item.date.strftime("%H:%M"),
"dateTip": item.date.strftime("%d-%b-%Y %H:%M:%S"),
"pax": item.pax,
"user": {"id": item.user_id, "name": item.user.name},
"creationDate": item.creation_date.strftime("%H:%M"),
"creationDateTip": item.creation_date.strftime("%d-%b-%Y %H:%M:%S"),
"lastEditDate": item.last_edit_date.strftime("%H:%M"),
"lastEditDateTip": item.last_edit_date.strftime("%d-%b-%Y %H:%M:%S"),
"billId": item.full_bill_id,
"table": {"id": item.food_table_id, "name": item.food_table.name},
"customer": {"id": item.customer_id, "name": item.customer.name} if item.customer is not None else None,
"settlements": [],
"narration": item.narration,
"reason": item.reason,
"voucherType": item.voucher_type.name,
"kotId": item.kot_id,
"kots": [
{
"id": k.id,
"code": k.code,
"date": k.date.strftime("%d-%b-%Y %H:%M:%S"),
"user": {"id": k.user_id, "name": k.user.name},
"inventories": [
{
"id": i.id,
"sortOrder": i.sort_order,
"product": {
"id": i.product_id,
"name": i.product.full_name,
"menuCategory": {
"id": i.product.menu_category_id,
"name": i.product.menu_category.name,
"discountLimit": i.product.menu_category.discount_limit,
},
"saleCategory": {"id": i.product.sale_category_id, "name": i.product.sale_category.name,},
},
"quantity": i.quantity,
"price": i.price,
"isHappyHour": i.is_happy_hour,
"taxRate": i.tax_rate,
"tax": {"id": i.tax_id, "name": i.tax.name},
"discount": i.discount,
"modifiers": [
{"id": m.modifier.id, "name": m.modifier.name, "price": m.price,} for m in i.modifiers
],
}
for i in k.inventories
],
}
for k in item.kots
],
"reprints": [],
}
def voucher_blank(table: FoodTable, guest: Optional[GuestBook]):
return {
"pax": table.seats if guest is None else guest.pax,
"table": {"id": table.id, "name": table.name},
"voucherType": VoucherType.KOT.name,
"customer": {"id": guest.customer_id, "name": guest.customer.name} if guest is not None else None,
"kots": [],
}