"""Synthetic local application example. No network or database calls."""
import json
orders = [dict(id=f"P{i}", supplier=s, qty=q) for i,s,q in [(1,"S1",10),(2,"S1",8),(3,"S2",5),(4,"S9",4),(5,"S2",9),(6,"S1",2)]]
receipts = {"P1":10,"P3":7,"P4":4,"P5":3,"P6":2}
suppliers = {"S1","S2"}
result = []
for o in orders:
    reasons = []
    if o["id"] not in receipts: reasons.append("receipt_follow_up")
    elif receipts[o["id"]] > o["qty"]: reasons.append("excess_review")
    elif receipts[o["id"]] < o["qty"]: reasons.append("short_receipt_review")
    if o["supplier"] not in suppliers: reasons.append("supplier_validation")
    owners = {"receipt_follow_up":"Receiving", "excess_review":"Order owner", "supplier_validation":"Supplier review", "short_receipt_review":"Receiving"}
    if reasons: result.append({"order_id":o["id"], "reasons":reasons, "owners":[owners[r] for r in reasons]})
assert [x["order_id"] for x in result] == ["P2","P3","P4","P5"]

print(json.dumps(result, indent=2, ensure_ascii=False))
