Initial commit
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
"""
|
||||
Repository for PoolResource operations
|
||||
"""
|
||||
from sqlalchemy.orm import Session
|
||||
from app.models import PoolResource, PoolReceipt, PoolAllocation
|
||||
|
||||
class PoolResourceRepository:
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
|
||||
def get_current_balance(self, pool_resource_id: int) -> int:
|
||||
"""Calculate current balance from receipts and allocations"""
|
||||
# Get all receipts for this resource
|
||||
receipts = self.db.query(PoolReceipt).filter(
|
||||
PoolReceipt.pool_resource_id == pool_resource_id
|
||||
).all()
|
||||
|
||||
# Get all allocations for this resource
|
||||
allocations = self.db.query(PoolAllocation).filter(
|
||||
PoolAllocation.pool_resource_id == pool_resource_id
|
||||
).all()
|
||||
|
||||
# Calculate total receipt quantity
|
||||
total_receipts = sum(r.quantity for r in receipts)
|
||||
|
||||
# Calculate total allocation quantity
|
||||
total_allocations = sum(a.quantity for a in allocations)
|
||||
|
||||
# Return balance
|
||||
return total_receipts - total_allocations
|
||||
|
||||
def can_allocate(self, pool_resource_id: int, quantity: int) -> bool:
|
||||
"""Check if allocation of specified quantity is possible"""
|
||||
current_balance = self.get_current_balance(pool_resource_id)
|
||||
return quantity <= current_balance
|
||||
Reference in New Issue
Block a user