import uuid from fastapi import APIRouter, HTTPException, Security, status from sqlalchemy import delete, select from sqlalchemy.exc import SQLAlchemyError from ...core.security import get_current_active_user as get_user from ...db.session import SessionFuture from ...models.overview import Overview from ...models.settle_option import SettleOption from ...models.voucher import Voucher from ...models.voucher_type import VoucherType from ...schemas.receive_payment import ReceivePaymentItem as SettleSchema from ...schemas.user_token import UserToken from . import do_update_bill_numbers, do_update_settlements router = APIRouter() @router.post("/cancel-bill/{id_}") def cancel( id_: uuid.UUID, u: bool, # Update table? reason: str, user: UserToken = Security(get_user, scopes=["void-bill"]), ): try: with SessionFuture() as db: item: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one() item.reason = f"{reason}" item.voucher_type = VoucherType.CANCEL do_update_bill_numbers(item, db) do_update_settlements(item, [SettleSchema(id=SettleOption.CANCEL(), amount=round(item.amount))], db) if u: # Update table db.execute(delete(Overview).where(Overview.voucher_id == item.id)) db.commit() return True except SQLAlchemyError as e: raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e), )