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

155 lines
5.2 KiB
Python
Raw Normal View History

import uuid
from datetime import datetime
from typing import Optional
import barker.schemas.voucher as schemas
from fastapi import APIRouter, Depends, HTTPException, Security, status
from sqlalchemy import func
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Session
from ...core.security import get_current_active_user as get_user
from ...db.session import SessionLocal
2020-11-08 13:39:31 +00:00
from ...models import (
Inventory,
InventoryModifier,
Kot,
ProductNew,
2020-11-08 13:39:31 +00:00
Voucher,
VoucherType,
)
from ...printing import print_bill, print_kot
from ...routers.voucher import (
check_permissions,
do_update_settlements,
do_update_table,
get_bill_id,
get_guest_book,
get_tax,
)
from ...schemas.auth import UserToken
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()
if round(i.quantity, 2) < 0:
if "edit-printed-product" not in user.permissions:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail=f"You are not allowed to delete printed products."
f"\n In this case {product.full_name}",
)
minimum = round(
sum(
inv.quantity
for ko in item.kots
for inv in ko.inventories
if ko.id and inv.product_id == product.id
)
* -1,
2,
)
if round(i.quantity, 2) < minimum:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail=f"Quantity of {product.full_name} cannot be less than {minimum}",
)
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