import uuid from typing import Optional, List from fastapi import HTTPException from sqlalchemy import func from sqlalchemy.orm import Session from starlette import status from barker.models import ( VoucherType, Settlement, SettleOption, Voucher, Overview, GuestBook, ) from barker.schemas.receive_payment import ReceivePaymentItem as SettleSchema def get_tax(tax, voucher_type): if voucher_type in [VoucherType.STAFF, VoucherType.NO_CHARGE]: return 0 elif voucher_type in [VoucherType.KOT, VoucherType.REGULAR_BILL]: return tax else: raise HTTPException( status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="Unexpected Voucher Type", ) def get_bill_id(voucher_type, db): if voucher_type == VoucherType.KOT: return None bill_id = ( db.query(func.coalesce(func.max(Voucher.bill_id), 0) + 1) .filter(Voucher.voucher_type == voucher_type.value) .scalar() ) if voucher_type == VoucherType.REGULAR_BILL and bill_id % 10000 == 0: bill_id += 1 return bill_id def do_update_table(item: Voucher, guest_book: Optional[GuestBook], db: Session): status_ = "running" if item.voucher_type == VoucherType.KOT else "printed" if item.status is None: item.status = Overview( voucher_id=item.id, food_table_id=item.food_table_id, guest_book_id=guest_book.id if guest_book is not None else None, status=status_, ) db.add(item.status) else: item.status.status = status_ def check_permissions(item: Optional[Voucher], voucher_type: VoucherType, permissions: List[str]): if voucher_type == VoucherType.KOT and "print-kot" not in permissions: raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="You are not allowed to print a kot", ) if voucher_type != VoucherType.KOT and "print-bill" not in permissions: raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="You are not allowed to print bill", ) if item is None: return if item.voucher_type != VoucherType.KOT and "edit-printed-bill" not in permissions: raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="You are not allowed to edit a printed bill", ) if item.voucher_type != VoucherType.KOT and voucher_type == VoucherType.KOT: raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="This Bill is already printed\nCannot add a Kot to it.", ) if item.voucher_type == VoucherType.VOID: raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail=f"This Bill is already void.\nReason: {item.reason}", ) def get_guest_book(id_: uuid.UUID, db: Session): if id_ is None: return id_ return db.query(GuestBook).filter(GuestBook.id == id_).first() def do_update_settlements(voucher: Voucher, others: List[SettleSchema], db: Session): settlements: List[SettleSchema] = [] settlements += others total_amount = voucher.amount settlements.append(SettleSchema(id=SettleOption.AMOUNT(), amount=-total_amount)) round_off = round(total_amount) - total_amount if round_off != 0: settlements.append(SettleSchema(id=SettleOption.ROUND_OFF(), amount=-round_off)) unsettled = sum(x.amount for x in settlements) if len(others) and unsettled != 0: # This should not be allowed raise HTTPException( status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="Payment received is not equal to bill amount", ) if unsettled != 0: settlements.append(SettleSchema(id=SettleOption.UNSETTLED(), amount=unsettled)) for i in settlements: old = [s for s in voucher.settlements if s.settled == i.id_] if len(old) == 1: old[0].amount = i.amount else: s = Settlement(voucher.id, i.id_, i.amount) voucher.settlements.append(s) db.add(s) for i in (i for i in voucher.settlements if i.settled not in [x.id_ for x in settlements]): voucher.settlements.remove(i) db.delete(i)