import os

file_path = 'core/views.py'

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

old_block = """    products = Product.objects.all().filter(stock_quantity__gt=0, is_active=True)
    customers = Customer.objects.all()
    categories = Category.objects.all()
    payment_methods = PaymentMethod.objects.filter(is_active=True)
    settings = SystemSetting.objects.first()"""

new_block = """    settings = SystemSetting.objects.first()
    products = Product.objects.filter(is_active=True)
    if not settings or not settings.allow_zero_stock_sales:
        products = products.filter(stock_quantity__gt=0)

    customers = Customer.objects.all()
    categories = Category.objects.all()
    payment_methods = PaymentMethod.objects.filter(is_active=True)"""

if old_block in content:
    new_content = content.replace(old_block, new_block)
    with open(file_path, 'w') as f:
        f.write(new_content)
    print("Successfully patched core/views.py")
else:
    print("Could not find the target block in core/views.py")
    # Debugging: print a small chunk to see what's wrong with matching
    start_index = content.find("def pos(request):")
    if start_index != -1:
        print("Context around pos view:")
        print(content[start_index:start_index+500])
