Spaces:
Sleeping
Sleeping
Commit ·
ea7352f
1
Parent(s): 6153aab
feat: add invoice creation logic with SQLAlchemy
Browse files- api/invoices.py +18 -9
- models/invoice.py +13 -3
api/invoices.py
CHANGED
|
@@ -1,20 +1,20 @@
|
|
| 1 |
-
from fastapi import APIRouter, Depends
|
| 2 |
from sqlalchemy.orm import Session
|
|
|
|
|
|
|
|
|
|
| 3 |
from core.database import get_db
|
| 4 |
from models.invoice import Invoice, InvoiceItem
|
| 5 |
-
from pydantic import BaseModel
|
| 6 |
-
from typing import List
|
| 7 |
|
| 8 |
router = APIRouter(prefix="/invoices", tags=["Invoices"])
|
| 9 |
|
| 10 |
-
# Pydantic Schemas for validation
|
| 11 |
class ItemCreate(BaseModel):
|
| 12 |
description: str
|
| 13 |
quantity: int
|
| 14 |
price_per_unit: float
|
| 15 |
|
| 16 |
class InvoiceCreate(BaseModel):
|
| 17 |
-
|
| 18 |
doctor_name: str
|
| 19 |
clinic_name: str
|
| 20 |
patient_name: str
|
|
@@ -24,11 +24,12 @@ class InvoiceCreate(BaseModel):
|
|
| 24 |
|
| 25 |
@router.post("/")
|
| 26 |
def create_invoice(data: InvoiceCreate, db: Session = Depends(get_db)):
|
| 27 |
-
# Calculate totals
|
| 28 |
total = sum(item.quantity * item.price_per_unit for item in data.items)
|
| 29 |
|
|
|
|
| 30 |
new_invoice = Invoice(
|
| 31 |
-
|
| 32 |
doctor_name=data.doctor_name,
|
| 33 |
clinic_name=data.clinic_name,
|
| 34 |
patient_name=data.patient_name,
|
|
@@ -39,8 +40,9 @@ def create_invoice(data: InvoiceCreate, db: Session = Depends(get_db)):
|
|
| 39 |
)
|
| 40 |
|
| 41 |
db.add(new_invoice)
|
| 42 |
-
db.flush()
|
| 43 |
|
|
|
|
| 44 |
for item in data.items:
|
| 45 |
db_item = InvoiceItem(
|
| 46 |
invoice_id=new_invoice.id,
|
|
@@ -52,4 +54,11 @@ def create_invoice(data: InvoiceCreate, db: Session = Depends(get_db)):
|
|
| 52 |
db.add(db_item)
|
| 53 |
|
| 54 |
db.commit()
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Depends
|
| 2 |
from sqlalchemy.orm import Session
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
from typing import Optional, List
|
| 5 |
+
from pydantic import BaseModel
|
| 6 |
from core.database import get_db
|
| 7 |
from models.invoice import Invoice, InvoiceItem
|
|
|
|
|
|
|
| 8 |
|
| 9 |
router = APIRouter(prefix="/invoices", tags=["Invoices"])
|
| 10 |
|
|
|
|
| 11 |
class ItemCreate(BaseModel):
|
| 12 |
description: str
|
| 13 |
quantity: int
|
| 14 |
price_per_unit: float
|
| 15 |
|
| 16 |
class InvoiceCreate(BaseModel):
|
| 17 |
+
date: Optional[datetime] = None # User can select this
|
| 18 |
doctor_name: str
|
| 19 |
clinic_name: str
|
| 20 |
patient_name: str
|
|
|
|
| 24 |
|
| 25 |
@router.post("/")
|
| 26 |
def create_invoice(data: InvoiceCreate, db: Session = Depends(get_db)):
|
| 27 |
+
# 1. Calculate totals
|
| 28 |
total = sum(item.quantity * item.price_per_unit for item in data.items)
|
| 29 |
|
| 30 |
+
# 2. Create Invoice (ID is generated automatically by Neon)
|
| 31 |
new_invoice = Invoice(
|
| 32 |
+
date=data.date or datetime.utcnow(), # Use selected date or 'now'
|
| 33 |
doctor_name=data.doctor_name,
|
| 34 |
clinic_name=data.clinic_name,
|
| 35 |
patient_name=data.patient_name,
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
db.add(new_invoice)
|
| 43 |
+
db.flush()
|
| 44 |
|
| 45 |
+
# 3. Add the items
|
| 46 |
for item in data.items:
|
| 47 |
db_item = InvoiceItem(
|
| 48 |
invoice_id=new_invoice.id,
|
|
|
|
| 54 |
db.add(db_item)
|
| 55 |
|
| 56 |
db.commit()
|
| 57 |
+
db.refresh(new_invoice)
|
| 58 |
+
|
| 59 |
+
# Return the real ID and the formatted INV number
|
| 60 |
+
return {
|
| 61 |
+
"id": new_invoice.id,
|
| 62 |
+
"invoice_no": new_invoice.invoice_number,
|
| 63 |
+
"status": "success"
|
| 64 |
+
}
|
models/invoice.py
CHANGED
|
@@ -1,12 +1,17 @@
|
|
| 1 |
from sqlalchemy import Column, Integer, String, Float, DateTime, ForeignKey
|
| 2 |
from sqlalchemy.orm import relationship
|
| 3 |
from datetime import datetime
|
| 4 |
-
from core.database import Base
|
| 5 |
|
| 6 |
class Invoice(Base):
|
| 7 |
__tablename__ = "invoices"
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
doctor_name = Column(String)
|
| 11 |
clinic_name = Column(String)
|
| 12 |
patient_name = Column(String)
|
|
@@ -17,6 +22,11 @@ class Invoice(Base):
|
|
| 17 |
|
| 18 |
items = relationship("InvoiceItem", back_populates="invoice", cascade="all, delete-orphan")
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
class InvoiceItem(Base):
|
| 21 |
__tablename__ = "invoice_items"
|
| 22 |
id = Column(Integer, primary_key=True, index=True)
|
|
|
|
| 1 |
from sqlalchemy import Column, Integer, String, Float, DateTime, ForeignKey
|
| 2 |
from sqlalchemy.orm import relationship
|
| 3 |
from datetime import datetime
|
| 4 |
+
from core.database import Base
|
| 5 |
|
| 6 |
class Invoice(Base):
|
| 7 |
__tablename__ = "invoices"
|
| 8 |
+
|
| 9 |
+
# Auto-incrementing ID (this is your sequence)
|
| 10 |
+
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
|
| 11 |
+
|
| 12 |
+
# User selected date (defaults to now)
|
| 13 |
+
date = Column(DateTime, default=datetime.utcnow)
|
| 14 |
+
|
| 15 |
doctor_name = Column(String)
|
| 16 |
clinic_name = Column(String)
|
| 17 |
patient_name = Column(String)
|
|
|
|
| 22 |
|
| 23 |
items = relationship("InvoiceItem", back_populates="invoice", cascade="all, delete-orphan")
|
| 24 |
|
| 25 |
+
@property
|
| 26 |
+
def invoice_number(self):
|
| 27 |
+
return f"INV-{self.id:04d}"
|
| 28 |
+
|
| 29 |
+
|
| 30 |
class InvoiceItem(Base):
|
| 31 |
__tablename__ = "invoice_items"
|
| 32 |
id = Column(Integer, primary_key=True, index=True)
|