brewman/brewman/brewman/models/validations.py

56 lines
2.0 KiB
Python

from fastapi import HTTPException, status
from .voucher import Voucher
def check_journals_are_valid(voucher: Voucher) -> None:
if len(voucher.journals) < 2:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="Not enough journals",
)
if sum(x.debit * round(x.amount, 2) for x in voucher.journals) != 0:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="Journal amounts do no match",
)
if len([x for x in voucher.journals if x.amount < 0]) > 0:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="Journal amounts cannot be negative",
)
journal_set = set(hash(x.account_id) ^ hash(x.cost_centre_id) for x in voucher.journals)
if len(voucher.journals) != len(journal_set):
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="Duplicate journals are not allowed",
)
def check_duplicate_batches(voucher: Voucher) -> None:
if len(voucher.inventories) < 1:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="Not enough inventories",
)
product_set = set(x.batch.id for x in voucher.inventories)
if len(voucher.inventories) != len(product_set):
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="Duplicate inventories are not allowed",
)
def check_duplicate_skus(voucher: Voucher) -> None:
if len(voucher.inventories) < 1:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="Not enough inventories",
)
product_set = set(x.batch.sku_id for x in voucher.inventories)
if len(voucher.inventories) != len(product_set):
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="Duplicate inventories are not allowed",
)