# finance/utils.py (create this new file)
from django.utils import timezone

def generate_invoice_number():
    """Generate a unique invoice number"""
    from .models import Invoice
    
    year = timezone.now().year
    last_invoice = Invoice.objects.filter(
        invoice_number__startswith=f"INV-{year}"
    ).order_by('-id').first()
    
    if last_invoice and last_invoice.invoice_number:
        try:
            last_num = int(last_invoice.invoice_number.split('-')[-1])
            new_num = last_num + 1
        except (ValueError, IndexError):
            new_num = 1
    else:
        new_num = 1
    
    return f"INV-{year}-{new_num:04d}"