DEV Community

fulfillmentyx
fulfillmentyx

Posted on Edited on

Grouping returns records when check-in and inspection are separate events

By YX Fulfillment

A returned package can be checked in by the receiving team on Tuesday and inspected on Wednesday. A daily report built from only the inspection date cannot tell receiving staff how much work arrived Tuesday. Use two event timestamps and two measures. Keep a separate count for records still awaiting inspection.

The synthetic sample uses Asia/Shanghai as the reporting zone. Each tuple contains one return ID, a UTC check-in timestamp, and its optional UTC inspection timestamp. Report dates come from converting the event instant to the chosen reporting zone. This design counts returns, not returned item quantities.

from collections import Counter
from datetime import datetime
from zoneinfo import ZoneInfo

zone = ZoneInfo("Asia/Shanghai")
records = [
    ("R01", "2026-05-12T15:40:00+00:00", "2026-05-12T17:10:00+00:00"),
    ("R02", "2026-05-12T16:10:00+00:00", None),
    ("R03", "2026-05-12T10:20:00+00:00", "2026-05-12T12:00:00+00:00"),
]

def reporting_day(raw):
    return datetime.fromisoformat(raw).astimezone(zone).date().isoformat()

checked_in = Counter(reporting_day(arrived) for _, arrived, _ in records)
inspected = Counter(
    reporting_day(reviewed) for _, _, reviewed in records if reviewed is not None
)
awaiting = [return_id for return_id, _, reviewed in records if reviewed is None]
assert dict(checked_in) == {"2026-05-12": 2, "2026-05-13": 1}
assert dict(inspected) == {"2026-05-13": 1, "2026-05-12": 1}
assert awaiting == ["R02"]
for day in sorted(checked_in.keys() | inspected.keys()):
    print(day, "check-ins", checked_in[day], "inspections", inspected[day])
print("Awaiting inspection:", awaiting)
Enter fullscreen mode Exit fullscreen mode

Output from a local run on Python 3.9.6

2026-05-12 check-ins 2 inspections 1
2026-05-13 check-ins 1 inspections 1
Awaiting inspection: ['R02']
Enter fullscreen mode Exit fullscreen mode

R01 arrives on May 12 in the reporting zone and is inspected on May 13. R02 contributes one arrival on May 13 and appears in the outstanding list. No inspection date needs to be invented. The two daily counts can cross even in a working process because inspections can draw on earlier arrivals.

The outstanding list above is a current-state snapshot. For backlog at the end of a historical day, count all returns checked in before the next local day and exclude only those inspected before that same boundary. If a return can move through multiple inspections, name the measure precisely and decide whether it counts inspection events or distinct returns.

Top comments (0)