WIP gettnig there
This commit is contained in:
@@ -211,6 +211,7 @@ class DrugVariantResponse(BaseModel):
|
||||
unit: str
|
||||
base_unit: str
|
||||
low_stock_threshold: float
|
||||
has_inventory_history: bool = False
|
||||
packs: List[VariantPackResponse] = []
|
||||
batches: List[BatchResponse] = []
|
||||
|
||||
@@ -318,6 +319,19 @@ def write_audit_log(
|
||||
|
||||
def enrich_variant_with_batches(db: Session, variant: DrugVariant) -> Dict[str, Any]:
|
||||
"""Return variant data with active batch details for API responses."""
|
||||
has_batch_history = (
|
||||
db.query(Batch.id)
|
||||
.filter(Batch.drug_variant_id == variant.id)
|
||||
.first()
|
||||
is not None
|
||||
)
|
||||
has_dispense_history = (
|
||||
db.query(Dispensing.id)
|
||||
.filter(Dispensing.drug_variant_id == variant.id)
|
||||
.first()
|
||||
is not None
|
||||
)
|
||||
|
||||
variant_dict = {
|
||||
"id": variant.id,
|
||||
"drug_id": variant.drug_id,
|
||||
@@ -326,6 +340,7 @@ def enrich_variant_with_batches(db: Session, variant: DrugVariant) -> Dict[str,
|
||||
"unit": variant.unit,
|
||||
"base_unit": variant.unit,
|
||||
"low_stock_threshold": variant.low_stock_threshold,
|
||||
"has_inventory_history": has_batch_history or has_dispense_history,
|
||||
}
|
||||
packs = (
|
||||
db.query(VariantPack)
|
||||
@@ -861,6 +876,26 @@ def delete_drug(drug_id: int, db: Session = Depends(get_db), current_user: User
|
||||
raise HTTPException(status_code=404, detail="Drug not found")
|
||||
|
||||
variant_ids = [row[0] for row in db.query(DrugVariant.id).filter(DrugVariant.drug_id == drug_id).all()]
|
||||
|
||||
if variant_ids:
|
||||
has_batch_history = (
|
||||
db.query(Batch.id)
|
||||
.filter(Batch.drug_variant_id.in_(variant_ids))
|
||||
.first()
|
||||
is not None
|
||||
)
|
||||
has_dispense_history = (
|
||||
db.query(Dispensing.id)
|
||||
.filter(Dispensing.drug_variant_id.in_(variant_ids))
|
||||
.first()
|
||||
is not None
|
||||
)
|
||||
|
||||
if has_batch_history or has_dispense_history:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Cannot delete drug with variants that have batch or dispensing history. Archive or manage records first.",
|
||||
)
|
||||
if variant_ids:
|
||||
batch_ids = [row[0] for row in db.query(Batch.id).filter(Batch.drug_variant_id.in_(variant_ids)).all()]
|
||||
if batch_ids:
|
||||
@@ -972,6 +1007,39 @@ def update_drug_variant(variant_id: int, variant_update: DrugVariantUpdate, db:
|
||||
payload["unit"] = cleaned_base_unit
|
||||
payload.pop("base_unit", None)
|
||||
|
||||
has_batch_history = (
|
||||
db.query(Batch.id)
|
||||
.filter(Batch.drug_variant_id == variant_id)
|
||||
.first()
|
||||
is not None
|
||||
)
|
||||
has_dispense_history = (
|
||||
db.query(Dispensing.id)
|
||||
.filter(Dispensing.drug_variant_id == variant_id)
|
||||
.first()
|
||||
is not None
|
||||
)
|
||||
is_locked = has_batch_history or has_dispense_history
|
||||
|
||||
locked_field_changes = []
|
||||
if is_locked:
|
||||
if "strength" in payload and payload["strength"] != variant.strength:
|
||||
locked_field_changes.append("strength")
|
||||
if "unit" in payload and payload["unit"] != variant.unit:
|
||||
locked_field_changes.append("base_unit")
|
||||
if "quantity" in payload and payload["quantity"] != variant.quantity:
|
||||
locked_field_changes.append("quantity")
|
||||
|
||||
if locked_field_changes:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=(
|
||||
"Cannot change "
|
||||
+ ", ".join(locked_field_changes)
|
||||
+ " after batches or dispensing history exist for this variant"
|
||||
),
|
||||
)
|
||||
|
||||
for field, value in payload.items():
|
||||
setattr(variant, field, value)
|
||||
|
||||
@@ -995,6 +1063,25 @@ def delete_drug_variant(variant_id: int, db: Session = Depends(get_db), current_
|
||||
if not variant:
|
||||
raise HTTPException(status_code=404, detail="Drug variant not found")
|
||||
|
||||
has_batch_history = (
|
||||
db.query(Batch.id)
|
||||
.filter(Batch.drug_variant_id == variant_id)
|
||||
.first()
|
||||
is not None
|
||||
)
|
||||
has_dispense_history = (
|
||||
db.query(Dispensing.id)
|
||||
.filter(Dispensing.drug_variant_id == variant_id)
|
||||
.first()
|
||||
is not None
|
||||
)
|
||||
|
||||
if has_batch_history or has_dispense_history:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Cannot delete variant with batch or dispensing history. Archive or manage records first.",
|
||||
)
|
||||
|
||||
batch_ids = [row[0] for row in db.query(Batch.id).filter(Batch.drug_variant_id == variant_id).all()]
|
||||
if batch_ids:
|
||||
db.query(DispensingAllocation).filter(DispensingAllocation.batch_id.in_(batch_ids)).delete(synchronize_session=False)
|
||||
|
||||
Reference in New Issue
Block a user