import os, csv from decimal import Decimal OUT = os.path.dirname(os.path.abspath(__file__)) # Independent recomputation from the cleaned files (not from reconcile.py internals) def rd(n): with open(os.path.join(OUT, n), newline="", encoding="utf-8") as f: return list(csv.DictReader(f)) s, r, a = rd("cleaned-sales.csv"), rd("cleaned-refunds.csv"), rd("cleaned-ads.csv") ex = rd("exceptions.csv") for p in ("Workshop", "Course", None): ss = [x for x in s if x["included"] == "yes" and (p is None or x["product"] == p)] rr = [x for x in r if x["applied"] == "yes" and (p is None or x["product"] == p)] aa = [x for x in a if x["included"] == "yes" and (p is None or x["product"] == p)] g = sum(Decimal(x["gross_cad"]) for x in ss); rf = sum(Decimal(x["amount_cad"]) for x in rr) ad = sum(Decimal(x["spend_cad"]) for x in aa) print(p or "Combined", len(ss), g, rf, g - rf, ad, g - rf - ad, (g - rf) / len(ss)) # uniqueness and coverage checks inc_orders = [x["order_id"] for x in s if x["included"] == "yes"] print("unique included orders:", len(inc_orders) == len(set(inc_orders))) print("unique applied refund ids:", len({x["refund_id"] for x in r if x["applied"] == "yes"}) == sum(x["applied"] == "yes" for x in r)) print("unique included ad ids:", len({x["ad_id"] for x in a if x["included"] == "yes"}) == sum(x["included"] == "yes" for x in a)) excluded_ids = {x["row_id"] for x in s if x["included"] != "yes"} | {x["row_id"] for x in r if x["applied"] != "yes"} | {x["row_id"] for x in a if x["included"] != "yes"} print("every excluded row in exceptions.csv:", excluded_ids == {x["row_id"] for x in ex}, len(excluded_ids)) # refund <= order gross gross = {x["order_id"]: Decimal(x["gross_cad"]) for x in s if x["included"] == "yes"} print("refund<=gross:", all(Decimal(x["amount_cad"]) <= gross[x["order_id"]] for x in r if x["applied"] == "yes")) # raw inputs untouched? compare row counts and a checksum listing import hashlib for f in ("sales.csv", "refunds.csv", "ads.csv", "policy.md"): b = open(os.path.join(os.path.dirname(OUT), "inputs", f), "rb").read() print(f, hashlib.sha256(b).hexdigest()[:12], len(b))