p = "output/build_task1.py" s = open(p, encoding="utf-8").read() orig = s # 1) exception rows: add exception_type, rename amount column to row_amount_cad (raw row value, not a net impact) s = s.replace('''def exc(src, row, key, issue, action, impact): exceptions.append(dict(source_file=src, row_id=row["row_id"], record_id=key, issue=issue, action=action, amount_impact_cad=impact))''', '''def etype(reason): r = reason.lower() if "duplicate" in r or "superseded" in r: return "excluded_duplicate_or_superseded" if "outside october" in r: return "excluded_out_of_period" if "status" in r: return "excluded_status" if "not a valid october paid order" in r: return "flagged_invalid_order_reference" if "amount missing" in r: return "flagged_missing_amount" return "excluded_other" def exc(src, row, key, issue, action, amount, et=None): exceptions.append(dict(source_file=src, row_id=row["row_id"], record_id=key, exception_type=et or etype(issue), issue=issue, action=action, row_amount_cad=amount))''') s = s.replace('fieldnames=["source_file","row_id","record_id","issue","action","amount_impact_cad"]', 'fieldnames=["source_file","row_id","record_id","exception_type","issue","action","row_amount_cad"]') # 2) refunds: assign product from linked valid paid order for every row (incl. duplicates) s = s.replace(''' rid = r["refund_id"].strip() sig = tuple(v for k, v in r.items() if k in ("refund_id","order_id","settled_date","status","amount_cad"))''', ''' _o = paid_orders.get(r["order_id"].strip()) if _o: r["_prod"] = _o["_prod"] rid = r["refund_id"].strip() sig = tuple(v for k, v in r.items() if k in ("refund_id","order_id","settled_date","status","amount_cad"))''') # 3) flag included order affected by unknown settled refund; 4) scripted reconciliation asserts s = s.replace('''with open(os.path.join(OUT, "exceptions.csv"), "w"''', '''for r in refunds: if "amount missing" in r["_reason"]: o = paid_orders[r["order_id"].strip()] exc("sales.csv", o, o["order_id"], f"Included paid order has settled refund {r['refund_id']} ({r['row_id']}) with unknown amount; net revenue provisional (refund bounded 0.00-{m(o['_gross'])} by gross)", "Kept in paid orders and gross; flagged only", m(o["_gross"]), "flagged_included_provisional") with open(os.path.join(OUT, "exceptions.csv"), "w"''') s = s.replace('print("ASSERTIONS PASSED")', '''def tot(rows, key): return sum((r[key] for r in rows if r[key] is not None), Decimal(0)) for rows, key, raw in ((sales, "_gross", "4140.00"), (refunds, "_amt", "960.00"), (ads, "_amt", "630.00")): inc = [r for r in rows if not r["_reason"]]; ex = [r for r in rows if r["_reason"]] assert m(tot(rows, key)) == raw and tot(inc, key) + tot(ex, key) == tot(rows, key) print("reconcile", raw, "=", m(tot(inc, key)), "+", m(tot(ex, key)), "| rows", len(rows), "=", len(inc), "+", len(ex)) assert sum(1 for r in refunds if r["_amt"] is None) == 1 excl_rows = [e for e in exceptions if e["exception_type"] != "flagged_included_provisional"] assert len(excl_rows) == sum(1 for t in (sales, refunds, ads) for r in t if r["_reason"]) print("ASSERTIONS PASSED")''') assert s != orig and s.count("flagged_included_provisional") >= 2 and "_o = paid_orders" in s open(p, "w", encoding="utf-8").write(s) print("patched")