AhmadYarAI commited on
Commit
2b9b19c
·
1 Parent(s): a2c408a

Update the notes

Browse files
Files changed (1) hide show
  1. api/invoices.py +9 -18
api/invoices.py CHANGED
@@ -25,12 +25,10 @@ class InvoiceCreate(BaseModel):
25
 
26
  @router.post("/")
27
  def create_invoice(data: InvoiceCreate, db: Session = Depends(get_db)):
28
- # 1. Calculate totals
29
  total = sum(item.quantity * item.price_per_unit for item in data.items)
30
 
31
- # 2. Create Invoice (ID is generated automatically by Neon)
32
  new_invoice = Invoice(
33
- date=data.date or datetime.utcnow(), # Use selected date or 'now'
34
  doctor_name=data.doctor_name,
35
  clinic_name=data.clinic_name,
36
  patient_name=data.patient_name,
@@ -38,33 +36,26 @@ 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.flush()
46
 
47
- # 3. Add the items
 
 
 
48
  for item in data.items:
49
  db_item = InvoiceItem(
50
  invoice_id=new_invoice.id,
51
  description=item.description,
52
- quantity=item.quantity,
53
- price_per_unit=item.price_per_unit,
54
- total_price=item.quantity * item.price_per_unit
55
  )
56
  db.add(db_item)
57
 
58
  db.commit()
59
- db.refresh(new_invoice)
60
-
61
- # Return the real ID and the formatted INV number
62
- return {
63
- "id": new_invoice.id,
64
- "invoice_no": new_invoice.invoice_no, # Use the column name 'invoice_no'
65
- "status": "success"
66
- }
67
-
68
 
69
  @router.get("/{invoice_id}")
70
  def get_invoice(invoice_id: int, db: Session = Depends(get_db)):
 
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,
33
  clinic_name=data.clinic_name,
34
  patient_name=data.patient_name,
 
36
  total_amount=total,
37
  received_amount=data.received_amount,
38
  remaining_balance=total - data.received_amount,
39
+ notes=data.notes
40
  )
41
 
42
  db.add(new_invoice)
43
+ db.flush() # This assigns the REAL ID from the database
44
 
45
+ # Set the invoice number directly in Python using the ID we just got
46
+ new_invoice.invoice_no = f"INV-{new_invoice.id:04d}"
47
+
48
+ # Now add items
49
  for item in data.items:
50
  db_item = InvoiceItem(
51
  invoice_id=new_invoice.id,
52
  description=item.description,
53
+ # ... rest of item code ...
 
 
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)):