Here's the Python code to implement a POWL model for a Purchase-to-Pay process in pm4py:

```python
import pm4py
from pm4py.objects.powl.obj import StrictPartialOrder, OperatorPOWL, Transition

# Define activities
create_purchase_order = Transition(label="Create Purchase Order")
receive_goods = Transition(label="Receive Goods")
approve_invoice = Transition(label="Approve Invoice")
make_payment = Transition(label="Make Payment")
goods_discrepancy = Transition(label="Goods Discrepancy (Optional)")

# Define the main flow (linear)
main_flow = StrictPartialOrder(nodes=[
    create_purchase_order,
    receive_goods,
    approve_invoice,
    make_payment
])
main_flow.order.add_edge(create_purchase_order, receive_goods)
main_flow.order.add_edge(receive_goods, approve_invoice)
main_flow.order.add_edge(approve_invoice, make_payment)

# Define the optional goods discrepancy branch
discrepancy_branch = StrictPartialOrder(nodes=[goods_discrepancy])

# Combine main flow with optional branch (partial order)
purchase_to_pay = StrictPartialOrder(nodes=[main_flow, discrepancy_branch])

# Define the order - main flow must be completed before discrepancy check
purchase_to_pay.order.add_edge(main_flow, discrepancy_branch)

print(purchase_to_pay)  # This will print the POWL model structure
```

This code defines individual activities for each step in the Purchase-to-Pay process. Then, it constructs two StrictPartialOrder objects:

* `main_flow`: Represents the main sequence of activities - creating PO, receiving goods, approving invoice, and making payment.
* `discrepancy_branch`: Represents the optional step of handling goods discrepancy.

Finally, the `purchase_to_pay` object combines both by defining a partial order. This ensures the main flow is completed before any potential discrepancy check.

This is a basic example. You can further expand it by:

* Adding loops using `OperatorPOWL` for activities that might repeat (e.g., revising a purchase order).
* Including XOR choices for alternative paths (e.g., expedite payment for critical goods).
