55 lines
1.5 KiB
Python
55 lines
1.5 KiB
Python
import uuid
|
|
|
|
from fastapi import APIRouter, HTTPException, status, Depends, Security
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
from sqlalchemy.orm import Session
|
|
|
|
from . import do_update_settlements
|
|
from ...schemas.auth import UserToken
|
|
from ...core.security import get_current_active_user as get_user
|
|
from ...db.session import SessionLocal
|
|
from ...models import Voucher, VoucherType, SettleOption, Overview
|
|
from barker.schemas.receive_payment import ReceivePaymentItem as SettleSchema
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
# Dependency
|
|
def get_db():
|
|
try:
|
|
db = SessionLocal()
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
@router.post("/void-bill/{id_}")
|
|
def update(
|
|
id_: uuid.UUID,
|
|
u: bool, # Update table?
|
|
reason: str,
|
|
db: Session = Depends(get_db),
|
|
user: UserToken = Security(get_user, scopes=["void-bill"]),
|
|
):
|
|
try:
|
|
item: Voucher = db.query(Voucher).filter(Voucher.id == id_).first()
|
|
|
|
item.void = True
|
|
item.reason = reason
|
|
item.voucher_type = VoucherType.VOID
|
|
|
|
do_update_settlements(item, [SettleSchema(id=SettleOption.VOID(), amount=round(item.amount))], db)
|
|
|
|
if u: # Update table
|
|
db.query(Overview).filter(Overview.voucher_id == item.id).delete()
|
|
db.commit()
|
|
return True
|
|
except SQLAlchemyError as e:
|
|
db.rollback()
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e),
|
|
)
|
|
except Exception:
|
|
db.rollback()
|
|
raise
|