from brewman.models.master import CostCentre from fastapi import HTTPException, status def validate(voucher): journals_valid(voucher) if voucher.type in [2, 3, 6]: inventory_valid(voucher) if voucher.id is None and voucher.type == 3: # Issue issue_new(voucher) if voucher.id is None and voucher.type == 6: # Purchase Return purchase_return_new(voucher) if voucher.id is not None and voucher.type == 3: # Issue issue_update(voucher) if voucher.id is not None and voucher.type == 6: # Purchase Return purchase_return_update(voucher) def check_batch_insert(voucher): if voucher.type == 9: raise HTTPException( status_code=status.HTTP_410_GONE, detail="Verification Vouchers have been disabled", ) if voucher.type == 6: # Purchase Return for item in voucher.inventories: batch = item.batch if batch.quantity_remaining < item.quantity: raise HTTPException( status_code=status.HTTP_409_CONFLICT, detail=f"Quantity of {item.product.name} ({item.product.units}) cannot be more than {batch.quantity_remaining}", ) if voucher.type == 3: # Issue purchase = None for item in voucher.journals: if item.cost_centre_id == CostCentre.cost_centre_purchase(): purchase = item if purchase is None: return if purchase.debit == 1: return for item in voucher.inventories: batch = item.batch if batch.quantity_remaining < item.quantity: raise HTTPException( status_code=status.HTTP_409_CONFLICT, detail=f"Quantity of {item.product.name} ({item.product.units}) cannot be more than {batch.quantity_remaining}", ) if voucher.type == 2: # Purchase pass def issue_new(voucher): consuming = filter( lambda x: x.cost_centre_id == CostCentre.cost_centre_purchase(), voucher.journals, ) if not len(consuming): consuming = False elif consuming[0].debit == 1: consuming = False else: consuming = True if not consuming: return for item in voucher.inventories: batch = item.batch if batch.quantity_remaining < item.quantity: raise HTTPException( status_code=status.HTTP_409_CONFLICT, detail=f"Quantity of {item.product.name} ({item.product.units}) cannot be more than {batch.quantity_remaining}", ) def issue_update(voucher): consuming = filter( lambda x: x.cost_centre_id == CostCentre.cost_centre_purchase(), voucher.journals, ) if not len(consuming): consuming = False elif consuming[0].debit == 1: consuming == False else: consuming == True if not consuming: return for item in voucher.inventories: batch = item.batch if batch.quantity_remaining < item.quantity: raise HTTPException( status_code=status.HTTP_409_CONFLICT, detail=f"Quantity of {item.product.name} ({item.product.units}) cannot be more than {batch.quantity_remaining}", ) def purchase_return_new(voucher): for item in voucher.inventories: batch = item.batch if batch.quantity_remaining < item.quantity: raise HTTPException( status_code=status.HTTP_409_CONFLICT, detail=f"Quantity of {item.product.name} ({item.product.units}) cannot be more than {batch.quantity_remaining}", ) def purchase_return_update(voucher): for item in voucher.inventories: batch = item.batch if batch.quantity_remaining < item.quantity: raise HTTPException( status_code=status.HTTP_409_CONFLICT, detail=f"Quantity of {item.product.name} ({item.product.units}) cannot be more than {batch.quantity_remaining}", ) def batch_valid(voucher): pass def is_date_allowed(voucher): pass