Commit ·
5e6e3b3
1
Parent(s): 4b8a633
Update the notes
Browse files- api/invoices.py +13 -12
api/invoices.py
CHANGED
|
@@ -26,9 +26,11 @@ class InvoiceCreate(BaseModel):
|
|
| 26 |
|
| 27 |
@router.post("/")
|
| 28 |
def create_invoice(data: InvoiceCreate, db: Session = Depends(get_db)):
|
|
|
|
| 29 |
total = sum(item.quantity * item.price_per_unit for item in data.items)
|
| 30 |
|
| 31 |
-
#
|
|
|
|
| 32 |
new_invoice = Invoice(
|
| 33 |
date=data.date or datetime.utcnow(),
|
| 34 |
doctor_name=data.doctor_name,
|
|
@@ -38,30 +40,29 @@ def create_invoice(data: InvoiceCreate, db: Session = Depends(get_db)):
|
|
| 38 |
total_amount=total,
|
| 39 |
received_amount=data.received_amount,
|
| 40 |
remaining_balance=total - data.received_amount,
|
| 41 |
-
notes=data.notes
|
|
|
|
| 42 |
)
|
| 43 |
|
| 44 |
db.add(new_invoice)
|
| 45 |
-
db.commit()
|
| 46 |
-
db.refresh(new_invoice)
|
| 47 |
|
| 48 |
-
#
|
| 49 |
new_invoice.invoice_no = f"INV-{new_invoice.id:04d}"
|
| 50 |
|
| 51 |
-
#
|
| 52 |
for item in data.items:
|
| 53 |
-
|
| 54 |
invoice_id=new_invoice.id,
|
| 55 |
description=item.description,
|
| 56 |
quantity=item.quantity,
|
| 57 |
price_per_unit=item.price_per_unit,
|
| 58 |
total_price=item.quantity * item.price_per_unit
|
| 59 |
-
)
|
| 60 |
-
db.add(db_item)
|
| 61 |
|
| 62 |
-
db.commit() # Save
|
| 63 |
-
|
| 64 |
-
return {"status": "success", "id": new_invoice.id, "invoice_no": new_invoice.invoice_no}
|
| 65 |
|
| 66 |
|
| 67 |
@router.get("/{invoice_id}")
|
|
|
|
| 26 |
|
| 27 |
@router.post("/")
|
| 28 |
def create_invoice(data: InvoiceCreate, db: Session = Depends(get_db)):
|
| 29 |
+
# 1. Calculate total
|
| 30 |
total = sum(item.quantity * item.price_per_unit for item in data.items)
|
| 31 |
|
| 32 |
+
# 2. Create the invoice object
|
| 33 |
+
# Notice: NO db.flush() here!
|
| 34 |
new_invoice = Invoice(
|
| 35 |
date=data.date or datetime.utcnow(),
|
| 36 |
doctor_name=data.doctor_name,
|
|
|
|
| 40 |
total_amount=total,
|
| 41 |
received_amount=data.received_amount,
|
| 42 |
remaining_balance=total - data.received_amount,
|
| 43 |
+
notes=data.notes,
|
| 44 |
+
invoice_no="PENDING" # Temporary placeholder
|
| 45 |
)
|
| 46 |
|
| 47 |
db.add(new_invoice)
|
| 48 |
+
db.commit() # Click! The dispenser gives you ID 47.
|
| 49 |
+
db.refresh(new_invoice)
|
| 50 |
|
| 51 |
+
# 3. Update the string now that we officially have the ID
|
| 52 |
new_invoice.invoice_no = f"INV-{new_invoice.id:04d}"
|
| 53 |
|
| 54 |
+
# 4. Add the items using the confirmed ID
|
| 55 |
for item in data.items:
|
| 56 |
+
db.add(InvoiceItem(
|
| 57 |
invoice_id=new_invoice.id,
|
| 58 |
description=item.description,
|
| 59 |
quantity=item.quantity,
|
| 60 |
price_per_unit=item.price_per_unit,
|
| 61 |
total_price=item.quantity * item.price_per_unit
|
| 62 |
+
))
|
|
|
|
| 63 |
|
| 64 |
+
db.commit() # Save everything finally
|
| 65 |
+
return {"status": "success", "invoice_no": new_invoice.invoice_no}
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
@router.get("/{invoice_id}")
|