barker/barker/barker/routers/voucher/update.py

122 lines
4.1 KiB
Python

import uuid
from datetime import datetime
from typing import Optional
from fastapi import APIRouter, HTTPException, status, Depends, Security
from sqlalchemy import func
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Session
from ...printing import print_kot, print_bill
from ...schemas.auth import UserToken
import barker.schemas.voucher as schemas
from ...core.security import get_current_active_user as get_user
from ...db.session import SessionLocal
from ...models import Voucher, VoucherType, Kot, Product, Inventory, InventoryModifier
from ...routers.voucher import (
get_tax,
do_update_settlements,
get_bill_id,
do_update_table,
check_permissions,
get_guest_book,
)
router = APIRouter()
# Dependency
def get_db():
try:
db = SessionLocal()
yield db
finally:
db.close()
@router.put("/update/{id_}")
def update(
id_: uuid.UUID,
data: schemas.VoucherIn,
u: bool, # Update table?
p: str, # Print type
g: Optional[uuid.UUID] = None, # Guest book id
db: Session = Depends(get_db),
user: UserToken = Security(get_user),
):
try:
now = datetime.now()
update_table = u
voucher_type = VoucherType[p]
guest_book = get_guest_book(g, db)
need_to_print_kot = False
item: Voucher = db.query(Voucher).filter(Voucher.id == id_).first()
check_permissions(item, voucher_type, user.permissions)
if guest_book is not None:
item.pax = guest_book.pax
item.food_table_id = data.table.id_
if data.customer is not None:
item.customer_id = data.customer.id_
if item.voucher_type != VoucherType.KOT:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Internal error, reprints should not reach here",
)
if item.voucher_type == VoucherType.KOT and voucher_type != VoucherType.KOT:
item.date = now
item.bill_id = get_bill_id(voucher_type, db)
item.voucher_type = voucher_type
item.user_id = user.id_
item.last_edit_date = now
for k in item.kots:
for i in k.inventories:
i.tax_rate = get_tax(i.tax_rate, voucher_type)
i.discount = next(
round(inv.discount, 5) for ko in data.kots for inv in ko.inventories if inv.id_ == i.id
)
for k in (k for k in data.kots if k.id_ is None and len(k.inventories) > 0):
need_to_print_kot = True
code = db.query(func.coalesce(func.max(Kot.code), 0) + 1).scalar()
kot = Kot(item.id, code, item.food_table_id, now, item.user_id)
item.kots.append(kot)
db.add(kot)
for index, i in enumerate(k.inventories):
product = db.query(Product).filter(Product.id == i.product.id_).first()
tax_rate = get_tax(product.sale_category.tax.rate, voucher_type)
inv = Inventory(
kot.id,
product.id,
round(i.quantity, 2),
product.price,
round(i.discount, 5),
i.is_happy_hour,
product.sale_category.tax_id,
tax_rate,
index,
)
kot.inventories.append(inv)
db.add(inv)
for m in i.modifiers:
mod = InventoryModifier(None, m.id_, 0)
inv.modifiers.append(mod)
db.add(mod)
do_update_settlements(item, [], db)
if update_table:
do_update_table(item, guest_book, db)
db.commit()
if need_to_print_kot:
print_kot(item.id, db)
if item.voucher_type != VoucherType.KOT:
print_bill(item.id, db)
except SQLAlchemyError as e:
db.rollback()
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e),
)
except Exception:
db.rollback()
raise