"""Data models for expense tracker."""
from dataclasses import dataclass
from typing import Optional
@dataclass
class Receipt:
"""Represents a parsed receipt."""
store_name: str
purchase_date: str # ISO format: YYYY-MM-DD
total: float
subtotal: Optional[float] = None
tax: Optional[float] = None
def __post_init__(self):
"""Validate receipt data."""
if self.total <= 0:
raise ValueError("Total must be positive")
@dataclass
class LineItem:
"""Represents a single item from a receipt."""
item_name_raw: str
item_type: str
line_total: float
quantity: float = 1.0
unit_price: Optional[float] = None
def __post_init__(self):
"""Calculate unit price if not provided."""
if self.unit_price is None and self.quantity > 0:
self.unit_price = self.line_total / self.quantity
if self.line_total <= 0:
raise ValueError("Line total must be positive")
if self.quantity <= 0:
raise ValueError("Quantity must be positive")
@dataclass
class ItemStats:
"""Statistics for a specific item type."""
item_type: str
total_purchases: int
last_purchase_date: str
first_purchase_date: str
total_spent: float
average_days_between: Optional[float] = None