from proteus import config, Model

# Connect
config.set_xmlrpc(f'https://admin:dsproject@itsa.open-squared.tech/tradon/')

Purchase = Model.get('purchase.purchase')

# Test: Set number on draft purchase
print("=== Testing Number Field Persistence ===")
draft = Purchase(682)  # The ID from your previous test

print(f"Before: number = {draft.number}, state = {draft.state}")

# Set number
draft.number = "MANUAL_TEST_001"
draft.save()
print(f"After save: number = {draft.number}")

# Reload by fetching again from database
draft_reloaded = Purchase(682)
print(f"After reload: number = {draft_reloaded.number}")

if draft_reloaded.number == "MANUAL_TEST_001":
    print("✓ SUCCESS: Number WAS persisted via Proteus!")
else:
    print(f"✗ FAILED: Number NOT persisted. Got: {draft_reloaded.number}")
    print("\nThis means the 'number' field is likely:")
    print("  1. Read-only (controlled by Tryton workflow)")
    print("  2. Auto-generated by a sequence")
    print("  3. Overwritten by server-side logic")

# Now verify in PostgreSQL
print("\n=== Verify in PostgreSQL ===")
print("Run this SQL query to confirm:")
print("SELECT id, number, state FROM purchase_purchase WHERE id = 682;")