import os

file_path = 'core/views.py'

old_content = """@login_required
def invoice_list(request):
    sales = Sale.objects.all().order_by('-created_at')
    paginator = Paginator(sales, 25)
    return render(request, 'core/invoices.html', {'sales': paginator.get_page(request.GET.get('page'))})"""

new_content = """@login_required
def invoice_list(request):
    sales = Sale.objects.all().order_by('-created_at')
    
    # Filter by date range
    start_date = request.GET.get('start_date')
    end_date = request.GET.get('end_date')
    if start_date:
        sales = sales.filter(created_at__date__gte=start_date)
    if end_date:
        sales = sales.filter(created_at__date__lte=end_date)
        
    # Filter by customer
    customer_id = request.GET.get('customer')
    if customer_id:
        sales = sales.filter(customer_id=customer_id)
        
    # Filter by status
    status = request.GET.get('status')
    if status:
        sales = sales.filter(status=status)

    paginator = Paginator(sales, 25)
    
    context = {
        'sales': paginator.get_page(request.GET.get('page')),
        'customers': Customer.objects.all(),
        'payment_methods': PaymentMethod.objects.filter(is_active=True),
        'site_settings': SystemSetting.objects.first(),
    }
    return render(request, 'core/invoices.html', context)"""

with open(file_path, 'r') as f:
    content = f.read()

if old_content in content:
    content = content.replace(old_content, new_content)
    with open(file_path, 'w') as f:
        f.write(content)
    print("Successfully patched invoice_list")
else:
    print("Could not find exact match for invoice_list function")
