Spaces:
Sleeping
Sleeping
Commit ·
b9ec576
1
Parent(s): f18c092
Done the Update request
Browse files- api/invoices.py +46 -0
api/invoices.py
CHANGED
|
@@ -35,6 +35,14 @@ class InvoiceCreate(BaseModel):
|
|
| 35 |
# The list of rows containing the patient info
|
| 36 |
items: List[ItemCreate]
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
@router.post("/")
|
| 39 |
def create_invoice(data: InvoiceCreate, db: Session = Depends(get_db)):
|
| 40 |
# 1. Calculate the total by summing all items in the list
|
|
@@ -130,3 +138,41 @@ def delete_invoice(invoice_id: int, db: Session = Depends(get_db)):
|
|
| 130 |
db.delete(db_invoice)
|
| 131 |
db.commit()
|
| 132 |
return {"message": "Deleted successfully"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# The list of rows containing the patient info
|
| 36 |
items: List[ItemCreate]
|
| 37 |
|
| 38 |
+
class InvoiceUpdate(BaseModel):
|
| 39 |
+
date: Optional[datetime] = None
|
| 40 |
+
doctor_name: str
|
| 41 |
+
clinic_name: str
|
| 42 |
+
received_amount: float = 0.0
|
| 43 |
+
notes: Optional[str] = None
|
| 44 |
+
items: List[ItemCreate]
|
| 45 |
+
|
| 46 |
@router.post("/")
|
| 47 |
def create_invoice(data: InvoiceCreate, db: Session = Depends(get_db)):
|
| 48 |
# 1. Calculate the total by summing all items in the list
|
|
|
|
| 138 |
db.delete(db_invoice)
|
| 139 |
db.commit()
|
| 140 |
return {"message": "Deleted successfully"}
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
@router.put("/{invoice_id}")
|
| 144 |
+
def update_invoice(invoice_id: int, data: InvoiceUpdate, db: Session = Depends(get_db)):
|
| 145 |
+
invoice = db.query(Invoice).filter(Invoice.id == invoice_id).first()
|
| 146 |
+
if not invoice:
|
| 147 |
+
raise HTTPException(status_code=404, detail="Invoice not found")
|
| 148 |
+
|
| 149 |
+
total_amount = sum(item.quantity * item.price_per_unit for item in data.items)
|
| 150 |
+
|
| 151 |
+
invoice.date = data.date or datetime.utcnow()
|
| 152 |
+
invoice.doctor_name = data.doctor_name
|
| 153 |
+
invoice.clinic_name = data.clinic_name
|
| 154 |
+
invoice.total_amount = total_amount
|
| 155 |
+
invoice.received_amount = data.received_amount
|
| 156 |
+
invoice.remaining_balance = total_amount - data.received_amount
|
| 157 |
+
invoice.notes = data.notes
|
| 158 |
+
|
| 159 |
+
# Delete old items and replace with new ones
|
| 160 |
+
db.query(InvoiceItem).filter(InvoiceItem.invoice_id == invoice_id).delete()
|
| 161 |
+
for item in data.items:
|
| 162 |
+
db.add(InvoiceItem(
|
| 163 |
+
invoice_id=invoice.id,
|
| 164 |
+
patient_name=item.patient_name,
|
| 165 |
+
shade=item.shade,
|
| 166 |
+
description=item.description,
|
| 167 |
+
quantity=item.quantity,
|
| 168 |
+
price_per_unit=item.price_per_unit,
|
| 169 |
+
total_price=item.quantity * item.price_per_unit
|
| 170 |
+
))
|
| 171 |
+
|
| 172 |
+
try:
|
| 173 |
+
db.commit()
|
| 174 |
+
db.refresh(invoice)
|
| 175 |
+
return {"status": "success", "invoice_no": invoice.invoice_no, "id": invoice.id}
|
| 176 |
+
except Exception as e:
|
| 177 |
+
db.rollback()
|
| 178 |
+
raise HTTPException(status_code=400, detail=str(e))
|