# One account in; zero or more net paid item rows out. def account_rows: . as $account | .orders[] | select(.status == "paid") | . as $order | .cartItems[] | ([.returns[] | select(.status == "accepted") | .qty] | add // 0) as $returned | if $returned > .qty then error("returns exceed quantity: " + $order.id) else . end | (.qty - $returned) as $net | select($net > 0) | {accountId: $account.id, tier: $account.tier, orderId: $order.id, currency: $order.currency, orderTotalCents: $order.totalCents, sku, netQty: $net, netListCents: ($net * .unitPriceCents), promotions: ([.promotions[] | select(.applied == true) | .code] | unique)};