from app import create_app, db
app = create_app()
app.app_context().push()
from app.models import PurchaseOrder, User
from flask_login import login_user
import datetime

# Get a user (admin)
user = User.query.first()
if not user:
    print('No user found')
    exit(1)
login_user(user)  # simulate login

# Get a PO
po = PurchaseOrder.query.first()
if not po:
    print('No PO found')
    exit(1)
print(f'Testing PO {po.po_number} status {po.status}')

# Import the route function
from app.routes.procurement import po_receive
try:
    # Call the function with po.id
    # Note: po_receive expects a Flask request context? It uses request? No, it doesn't.
    # It only uses current_user and db.
    # So we can call it directly.
    result = po_receive(po.id)
    print('Result:', result)
except Exception as e:
    print('Error:', e)
    import traceback
    traceback.print_exc()