simulation_state = context.getSimulationState()

if simulation_state in ("cancelled", "deleted", "draft"):
  result = "Cancelled"

elif simulation_state in ("planned", "confirmed", "ordered", "started"):
  result = "Ongoing"

else:
  portal = context.getPortalObject()

  paid = True
  for line in context.getMovementList(portal.getPortalAccountingMovementTypeList()):
    node_value = line.getSourceValue(portal_type='Account')
    if node_value.getAccountType() == 'asset/receivable':
      if not line.hasGroupingReference():
        paid = False
        break

  reversal = portal.portal_catalog.getResultValue(
      portal_type="Sale Invoice Transaction",
      simulation_state="stopped",
      default_causality_uid=context.getUid()
    )
  if reversal is not None and (context.getTotalPrice() + reversal.getTotalPrice()) == 0:
    result = "Cancelled"
  elif paid:
    result = "Paid"
  elif context.getTotalPrice() == 0:
    result = "Free!"
  else:
    # Check if there is an ongoing SlapOS payment
    payment = context.SaleInvoiceTransaction_getSlapOSPaymentRelatedValue()
    if payment is None:
      result = "Unpaid"
    else:
      # Check if mapping exists
      person = portal.portal_membership.getAuthenticatedMember().getUserValue()
      external_payment_id = person.Person_restrictMethodAsShadowUser(
        shadow_document=person,
        callable_object=payment.PaymentTransaction_getExternalPaymentId,
        argument_list=[])[0]
      if external_payment_id is None:
        result = "Pay now"
      else:
        result = "Waiting for payment confirmation"

return result
