d8315f13ac
- Introduced fields: receipt_location, calculated_tax, is_depreciable, cca_rate, useful_life, and residual_value in DBReceipt model. - Updated process_document function to handle new receipt data attributes. - Enhanced DocumentProcessResponse schema to include new fields. - Updated document processing rules to incorporate tax calculation based on location and depreciation rules.
97 lines
3.0 KiB
Python
97 lines
3.0 KiB
Python
from typing import Annotated
|
|
|
|
from fastapi import Depends
|
|
from sqlalchemy import Column, DateTime, Float, Integer, String, create_engine
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import Session, sessionmaker
|
|
|
|
SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_app.db"
|
|
|
|
engine = create_engine(
|
|
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
|
|
)
|
|
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
db_dependency = Annotated[Session, Depends(get_db)]
|
|
Base = declarative_base()
|
|
|
|
|
|
def create_db_tables():
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
|
|
def clear_all_data():
|
|
"""Clear all data from the database (useful for testing)"""
|
|
db = SessionLocal()
|
|
try:
|
|
db.query(DBTransaction).delete()
|
|
db.query(DBReceipt).delete()
|
|
db.query(DBUploadedFile).delete()
|
|
db.commit()
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
# Transactions table
|
|
class DBTransaction(Base):
|
|
__tablename__ = "transactions"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
transaction_id = Column(String, index=True)
|
|
amount = Column(Float, nullable=False)
|
|
date = Column(DateTime, nullable=False)
|
|
vendor = Column(String, nullable=False)
|
|
description = Column(String, nullable=True)
|
|
category = Column(String, nullable=True)
|
|
tax_amount = Column(Float, nullable=True)
|
|
categorisation_id = Column(String, nullable=True)
|
|
user_id = Column(String, nullable=True)
|
|
|
|
|
|
# Uploaded Files table
|
|
class DBUploadedFile(Base):
|
|
__tablename__ = "uploaded_files"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
file_id = Column(String, unique=True, index=True)
|
|
filename = Column(String, nullable=False)
|
|
file_path = Column(String, nullable=False)
|
|
file_type = Column(String, nullable=False)
|
|
upload_date = Column(DateTime, nullable=False)
|
|
status = Column(String, nullable=False, default="uploaded")
|
|
|
|
|
|
# Receipts table
|
|
class DBReceipt(Base):
|
|
__tablename__ = "receipts"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
receipt_id = Column(String, unique=True, index=True)
|
|
file_id = Column(String, unique=True, index=True)
|
|
amount = Column(Float, nullable=False)
|
|
date = Column(DateTime, nullable=False)
|
|
vendor = Column(String, nullable=False)
|
|
description = Column(String, nullable=True)
|
|
category = Column(String, nullable=True)
|
|
tax_amount = Column(Float, nullable=True)
|
|
confidence = Column(Float, nullable=True)
|
|
extraction_success = Column(String, nullable=True)
|
|
error_message = Column(String, nullable=True)
|
|
receipt_currency = Column(String, nullable=True)
|
|
receipt_location = Column(String, nullable=True)
|
|
calculated_tax = Column(Float, nullable=True)
|
|
is_depreciable = Column(String, nullable=True) # Store as string "True"/"False"
|
|
cca_rate = Column(Float, nullable=True)
|
|
useful_life = Column(Integer, nullable=True)
|
|
residual_value = Column(Float, nullable=True)
|