from zExceptions import Unauthorized
if REQUEST is not None:
  raise Unauthorized

state = context.getSimulationState()
person = context.getDestinationDecisionValue(portal_type="Person")
if (state not in ('suspended', 'validated')) or \
   (person is None):
  return

outstanding_amount = person.Entity_statSlapOSOutstandingAmount()

# It can't be smaller, we are considernig all open invoices are from unpaid_payment_amount
if round(float(outstanding_amount), 2) == round(float(0), 2):
  context.invalidate(comment="Automatically disabled as balance is %s" % outstanding_amount)
  return

if (int(outstanding_amount) > 0):
  return

context.invalidate(comment="Automatically disabled as balance is %s" % outstanding_amount)
