Commit ·
4b8a633
1
Parent(s): 2b9b19c
Update the notes
Browse files- api/invoices.py +13 -6
api/invoices.py
CHANGED
|
@@ -23,10 +23,12 @@ class InvoiceCreate(BaseModel):
|
|
| 23 |
notes: Optional[str] = None
|
| 24 |
items: List[ItemCreate]
|
| 25 |
|
|
|
|
| 26 |
@router.post("/")
|
| 27 |
def create_invoice(data: InvoiceCreate, db: Session = Depends(get_db)):
|
| 28 |
total = sum(item.quantity * item.price_per_unit for item in data.items)
|
| 29 |
|
|
|
|
| 30 |
new_invoice = Invoice(
|
| 31 |
date=data.date or datetime.utcnow(),
|
| 32 |
doctor_name=data.doctor_name,
|
|
@@ -40,23 +42,28 @@ def create_invoice(data: InvoiceCreate, db: Session = Depends(get_db)):
|
|
| 40 |
)
|
| 41 |
|
| 42 |
db.add(new_invoice)
|
| 43 |
-
db.
|
|
|
|
| 44 |
|
| 45 |
-
#
|
| 46 |
new_invoice.invoice_no = f"INV-{new_invoice.id:04d}"
|
| 47 |
-
|
| 48 |
-
#
|
| 49 |
for item in data.items:
|
| 50 |
db_item = InvoiceItem(
|
| 51 |
invoice_id=new_invoice.id,
|
| 52 |
description=item.description,
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
)
|
| 55 |
db.add(db_item)
|
| 56 |
|
| 57 |
-
db.commit()
|
|
|
|
| 58 |
return {"status": "success", "id": new_invoice.id, "invoice_no": new_invoice.invoice_no}
|
| 59 |
|
|
|
|
| 60 |
@router.get("/{invoice_id}")
|
| 61 |
def get_invoice(invoice_id: int, db: Session = Depends(get_db)):
|
| 62 |
# Look for the invoice and include its items
|
|
|
|
| 23 |
notes: Optional[str] = None
|
| 24 |
items: List[ItemCreate]
|
| 25 |
|
| 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 |
+
# 1. Create the invoice WITHOUT an invoice_no yet
|
| 32 |
new_invoice = Invoice(
|
| 33 |
date=data.date or datetime.utcnow(),
|
| 34 |
doctor_name=data.doctor_name,
|
|
|
|
| 42 |
)
|
| 43 |
|
| 44 |
db.add(new_invoice)
|
| 45 |
+
db.commit() # <--- SAVE IT NOW. This assigns the ID (e.g., 44)
|
| 46 |
+
db.refresh(new_invoice) # Now Python knows the ID is 44
|
| 47 |
|
| 48 |
+
# 2. Update the string AFTER it is saved
|
| 49 |
new_invoice.invoice_no = f"INV-{new_invoice.id:04d}"
|
| 50 |
+
|
| 51 |
+
# 3. Add the items
|
| 52 |
for item in data.items:
|
| 53 |
db_item = InvoiceItem(
|
| 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 the string and the items
|
| 63 |
+
|
| 64 |
return {"status": "success", "id": new_invoice.id, "invoice_no": new_invoice.invoice_no}
|
| 65 |
|
| 66 |
+
|
| 67 |
@router.get("/{invoice_id}")
|
| 68 |
def get_invoice(invoice_id: int, db: Session = Depends(get_db)):
|
| 69 |
# Look for the invoice and include its items
|