barker/barker/barker/routers/voucher/void.py

45 lines
1.5 KiB
Python

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_settlements
router = APIRouter()
@router.post("/void-bill/{id_}")
def void(
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} / Old Bill ID was: {item.full_bill_id}"
item.bill_id = None
item.voucher_type = VoucherType.VOID
do_update_settlements(item, [SettleSchema(id=SettleOption.VOID(), 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),
)