130 lines
3.6 KiB
Python
130 lines
3.6 KiB
Python
import uuid
|
|
from decimal import Decimal
|
|
from typing import Optional
|
|
|
|
from fastapi import APIRouter, HTTPException, status, Depends, Security
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
from sqlalchemy.orm import Session
|
|
|
|
from ..schemas.auth import UserToken
|
|
import barker.schemas.master as schemas
|
|
from ..core.security import get_current_active_user as get_user
|
|
from ..db.session import SessionLocal
|
|
from ..models.master import Tax
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
# Dependency
|
|
def get_db():
|
|
try:
|
|
db = SessionLocal()
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
@router.post("", response_model=schemas.Tax)
|
|
def save(
|
|
data: schemas.TaxIn, db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["taxes"]),
|
|
):
|
|
try:
|
|
item = Tax(name=data.name, rate=round(data.rate / Decimal(100), 4))
|
|
db.add(item)
|
|
db.commit()
|
|
return tax_info(item)
|
|
except SQLAlchemyError as e:
|
|
db.rollback()
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e),
|
|
)
|
|
except Exception:
|
|
db.rollback()
|
|
raise
|
|
|
|
|
|
@router.put("/{id_}", response_model=schemas.Tax)
|
|
def update(
|
|
id_: uuid.UUID,
|
|
data: schemas.TaxIn,
|
|
db: Session = Depends(get_db),
|
|
user: UserToken = Security(get_user, scopes=["taxes"]),
|
|
):
|
|
try:
|
|
item: Tax = db.query(Tax).filter(Tax.id == id_).first()
|
|
if item.is_fixture:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_423_LOCKED, detail=f"{item.name} is a fixture and cannot be edited or deleted.",
|
|
)
|
|
item.name = data.name
|
|
item.rate = round(data.rate / Decimal(100), 4)
|
|
db.commit()
|
|
return tax_info(item)
|
|
except SQLAlchemyError as e:
|
|
db.rollback()
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e),
|
|
)
|
|
except Exception:
|
|
db.rollback()
|
|
raise
|
|
|
|
|
|
@router.delete("/{id_}")
|
|
def delete(
|
|
id_: uuid.UUID, db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["taxes"]),
|
|
):
|
|
try:
|
|
item: Tax = db.query(Tax).filter(Tax.id == id_).first()
|
|
if item is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND, detail="Tax not found",
|
|
)
|
|
elif item.is_fixture:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_423_LOCKED, detail=f"{item.name} is a fixture and cannot be edited or deleted.",
|
|
)
|
|
else:
|
|
db.delete(item)
|
|
db.commit()
|
|
return tax_info(None)
|
|
except Exception:
|
|
db.rollback()
|
|
raise
|
|
|
|
|
|
@router.get("")
|
|
def show_blank(
|
|
db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["taxes"]),
|
|
):
|
|
return tax_info(None)
|
|
|
|
|
|
@router.get("/list")
|
|
def show_list(db: Session = Depends(get_db), user: UserToken = Depends(get_user)):
|
|
return [
|
|
{"id": item.id, "name": item.name, "rate": item.rate, "isFixture": item.is_fixture}
|
|
for item in db.query(Tax).order_by(Tax.name).all()
|
|
]
|
|
|
|
|
|
@router.get("/{id_}")
|
|
def show_id(
|
|
id_: uuid.UUID, db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["taxes"]),
|
|
):
|
|
item: Tax = db.query(Tax).filter(Tax.id == id_).first()
|
|
return tax_info(item)
|
|
|
|
|
|
def tax_info(item: Optional[Tax]):
|
|
if item is None:
|
|
tax = {"name": "", "rate": 0, "isFixture": False}
|
|
else:
|
|
tax = {
|
|
"id": item.id,
|
|
"name": item.name,
|
|
"rate": item.rate * 100,
|
|
"isFixture": item.is_fixture,
|
|
}
|
|
return tax
|