Add user location support and tax analysis enhancements

- Introduced user location extraction from user tax info for improved matching.
- Normalized user location to province codes for tax calculations.
- Updated MatchResponse schema to include tax analysis data.
- Enhanced LLMTaxAnalyzer to handle various location formats and provide fallback logic.
This commit is contained in:
bolade
2025-10-05 18:34:35 +01:00
parent c78c4c6fe9
commit c45e3fa791
3 changed files with 141 additions and 10 deletions
+26 -8
View File
@@ -5,10 +5,6 @@ import uuid
from datetime import datetime
from typing import List
from fastapi import FastAPI, File, Form, HTTPException, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from sqlalchemy.orm import Session
from database import (
DBReceipt,
DBTransaction,
@@ -16,6 +12,8 @@ from database import (
create_db_tables,
db_dependency,
)
from fastapi import FastAPI, File, Form, HTTPException, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from schemas import (
DocumentProcessResponse,
DocumentUploadResponse,
@@ -29,6 +27,7 @@ from schemas import (
from services.ai_rules import AIRule
from services.document_processor import DocumentProcessor
from services.matching_engine import MatchingEngine
from sqlalchemy.orm import Session
create_db_tables()
@@ -410,7 +409,7 @@ async def process_document(file_id: str, db: db_dependency):
confidence=receipt_data.get("confidence", 0.0),
extraction_success=str(receipt_data.get("extraction_success", False)),
error_message=receipt_data.get("error"),
receipt_currency=receipt_data.get("currency")
receipt_currency=receipt_data.get("currency"),
)
# Add to database
@@ -429,7 +428,7 @@ async def process_document(file_id: str, db: db_dependency):
category=receipt_data.get("category", ""),
confidence=receipt_data.get("confidence", 0.0),
error=receipt_data.get("error", None),
receipt_currency=receipt_data.get("currency")
receipt_currency=receipt_data.get("currency"),
)
except Exception as e:
@@ -536,13 +535,31 @@ async def match_specific_receipts(request: MatchSpecificRequest, db: db_dependen
f"Starting matching with {len(receipts)} receipts and {len(transactions)} transactions"
)
# Extract user location from user_tax_info if provided
user_location = request.user_location # Default/fallback
if request.user_tax_info:
# Use state_code from user_tax_info (e.g., "ON", "QC", "BC")
user_location = request.user_tax_info.state.state_code
logger.info(
f"Using location from user_tax_info: {user_location} ({request.user_tax_info.state.name}, {request.user_tax_info.country.name})"
)
else:
logger.info(f"Using default/provided user_location: {user_location}")
try:
matching_results = matching_engine.process_matching(receipts, transactions, user_location=request.user_location)
matching_results = matching_engine.process_matching(
receipts, transactions, user_location=user_location
)
logger.info(f"Matching completed, got {len(matching_results)} results")
# Convert matching results to response format
match_responses = []
for result in matching_results:
# Get final tax amount from LLM analysis if available, otherwise use receipt's stated tax
final_tax = result.receipt.tax
if result.tax_analysis and "final_tax_amount" in result.tax_analysis:
final_tax = result.tax_analysis["final_tax_amount"]
match_response = MatchResponse(
receipt_id=result.receipt.id,
transaction_id=result.transaction.id
@@ -554,13 +571,14 @@ async def match_specific_receipts(request: MatchSpecificRequest, db: db_dependen
receipt_amount=result.receipt.amount,
receipt_description=result.receipt.description,
receipt_category=result.receipt.category,
receipt_tax_amount=result.receipt.tax,
receipt_tax_amount=final_tax,
transaction_vendor=result.transaction.vendor
if result.transaction
else "",
transaction_amount=result.transaction.amount
if result.transaction
else 0.0,
tax_analysis=result.tax_analysis,
)
match_responses.append(match_response)