Automated Action 0a65bff5f3 Implement invoice generation service
- Create FastAPI app structure
- Set up SQLAlchemy with SQLite for database management
- Implement invoice and invoice item models
- Add Alembic for database migrations
- Create invoice generation and retrieval API endpoints
- Add health check endpoint
- Set up Ruff for linting
- Update README with project details
2025-05-18 20:01:08 +00:00

49 lines
1.2 KiB
Python

import datetime
import random
import string
def generate_invoice_number(prefix="INV", length=6):
"""
Generate a unique invoice number.
Format: INV-{YEAR}{MONTH}-{RANDOM_ALPHANUMERIC}
Example: INV-202307-A1B2C3
Args:
prefix (str): Prefix for the invoice number
length (int): Length of the random string
Returns:
str: Generated invoice number
"""
# Get current date for year-month part
today = datetime.datetime.now()
year_month = today.strftime("%Y%m")
# Generate random alphanumeric string
characters = string.ascii_uppercase + string.digits
random_str = ''.join(random.choice(characters) for _ in range(length))
# Combine to form invoice number
invoice_number = f"{prefix}-{year_month}-{random_str}"
return invoice_number
def calculate_total_amount(items):
"""
Calculate the total amount of an invoice based on its items.
Args:
items (list): List of invoice items, each with quantity and unit_price
Returns:
float: Total amount
"""
total = 0.0
for item in items:
item_amount = item.quantity * item.unit_price
total += item_amount
return total