From dc0d0614bb2b5d8108dc40b912f58a52dbdd888e Mon Sep 17 00:00:00 2001 From: ben Date: Mon, 23 Mar 2026 12:53:54 -0400 Subject: [PATCH] Add effective price to purchases --- build_purchases.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/build_purchases.py b/build_purchases.py index 3784aa6..06ae4c5 100644 --- a/build_purchases.py +++ b/build_purchases.py @@ -51,6 +51,7 @@ PURCHASE_FIELDS = [ "price_per_lb_basis", "price_per_oz", "price_per_oz_basis", + "effective_price", "is_discount_line", "is_coupon_line", "is_fee", @@ -172,6 +173,24 @@ def derive_metrics(row): } +def derive_effective_price(row): + normalized_quantity = to_decimal(row.get("normalized_quantity")) + if normalized_quantity in (None, Decimal("0")): + return "" + + net_line_total = to_decimal(row.get("net_line_total")) + line_total = to_decimal(row.get("line_total")) + numerator = ( + net_line_total + if net_line_total not in (None, Decimal("0")) + else line_total + ) + if numerator is None: + return "" + + return format_decimal(numerator / normalized_quantity) + + def order_lookup(rows, retailer): return {(retailer, row["order_id"]): row for row in rows} @@ -353,6 +372,7 @@ def build_purchase_rows( "store_number": order_row.get("store_number", ""), "store_city": order_row.get("store_city", ""), "store_state": order_row.get("store_state", ""), + "effective_price": derive_effective_price(row), "is_discount_line": row["is_discount_line"], "is_coupon_line": row["is_coupon_line"], "is_fee": row["is_fee"],