@startuml

(*) --> "Check if order position with\nscanned ticket secret exists"
--> if "" then
    -down->[yes] ===CHECK===
else
    -->[no] "Check if secret exists\nin revocation list"
    --> if "" then
        --> "Is this a forced upload?"
        --> if "" then
            -->[yes] ===CHECK===
        else
            -right->[no] "Return error REVOKED"
        endif
    else
        -right->[no] "Return error INVALID"
    endif

endif


===CHECK=== -down-> "Is addon_match set to true?"
--> if "" then
    -down->[no] "Is the order in status PAID or PENDING\nand is the position not canceled?"
else
    -right->[yes] "Build a list that includes the position\nas well as all its add-ons"
    -down-> "Filter list for products that are part of the check-in list"
    --> if "" then
        -down->[one found] Proceed with the matching position
        --> "Is the order in status PAID or PENDING\nand is the position not canceled?"
    else
        --> if "" then
            -right->[none found] "Return error PRODUCT  "
        else
            -down->[multiple found] Return error AMBIGUOUS
        endif
    endif
endif

"Is the order in status PAID or PENDING\nand is the position not canceled?" --> if "" then
    -right->[no && !force] "Return error CANCELED"
else
    -down->[yes || force] "Is one or more block set on the ticket?"
    --> if "" then
        -right->[no && !force] "Return error BLOCKED"
    else
        -down->[yes || force] "If this is not an exit, is the valid_from/valid_until\nconstraint on the ticket fulfilled?"
        --> if "" then
            -right->[no && !force] "Return error INVALID_TIME"
        else
            -down->[yes || force] "Is the product part of the check-in list?"
            --> if "" then
                -right->[no && !force] "Return error PRODUCT"
            else
                -down->[yes || force] "Is the subevent part of the check-in list?"
                --> if "" then
                    -right->[no && !force] "Return error PRODUCT "
                else
                    -down->[yes] "Is the order in status PAID?"
                    --> if "" then
                        -right->[no && !force] "Is Order.require_approval set?"
                        --> if "" then
                            -->[no] "Is Order.valid_if_pending set?"
                            --> if "" then
                                -down->[yes] "Is this an entry or exit?\nIs the upload forced?"
                            else
                                -right->[no] "Does the check-in list include pending orders?"
                                --> if "" then
                                    -right->[no] "Return error UNPAID "
                                else
                                    -down->[yes] "Is ignore_unpaid set on the request?\n(Has the operator confirmed\nthe checkin?)"
                                    --> if "" then
                                        -right->[no] "Return error UNPAID "
                                    else
                                        -down->[yes] "Is this an entry or exit?\nIs the upload forced?"
                                    endif
                                endif
                            endif
                        else
                            -->[yes] "Return error UNPAID "
                        endif
                    else
                        -down->[yes || force] "Is this an entry or exit?\nIs the upload forced?"
                    endif
                endif
            endif
        endif
    endif
endif

"Is this an entry or exit?\nIs the upload forced?" --> if "" then
    -right->[entry && not force] Evaluate custom logic (rules)
    --> if "" then
        -right->[error] "Return error RULES"
    else
        -down->[ok] "Are all required questions answered?"
        --> if "" then
            -right->[no && questions_supported] "Return error INCOMPLETE"
        else
            -down->[yes || not questions_supported] "Does the check-in list allow multi-entry?"
        endif
    endif
else
    -->[exit || force=true] "Return OK "
endif

"Does the check-in list allow multi-entry?" --> if "" then
    -right->[yes] "Return OK"
else
    -down->[no] "Is this the first checkin\nfor this ticket on this list?"
    --> if "" then
        -right->[yes] "Return OK"
    else
        -down->[no] "Are all previous checkins\nfor this ticket on this list exits?"
        --> if "" then
            -right->[yes] "Return OK"
        else
            -down->[no] "Does the check-in list\n allow entry after exit\nand is the last checkin\nan exit?"
            --> if "" then
                -right->[yes] "Return OK"
            else
                -down->[no] "Return error ALREADY_REDEEMED"
            endif
        endif
    endif
endif


@enduml
