retail.refund-calculate
Refund for returned items, sharing basket discounts back across the sale so every refund is exact to the penny.
1.0.0 · published 2026-10-03 by charlie · Anterra
Pinned by 16 tests, run in TypeScript, Python and Rust.
What it does
Works out what to refund when a customer brings items back, so that the refund matches what they actually paid for those items, including their share of any discount on the whole basket, to the penny.
## Two steps
For example
calculate_refund(lines ×3, £5.00, returns ×1)→ lines ×1, total £4.29 one of three teas, with its share of a 5.00 couponcalculate_refund(lines ×3, £5.00, returns ×1)→ lines ×1, total £6.86 a single mug refunds its whole paid pricecalculate_refund(lines ×3, £5.00, returns ×1)→ lines ×1, total £5.14 one of two books; the coupon's spare penny went to the book line
The function
The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.
def calculate_refund(lines: Sequence[SaleLine], basket_discount: Money, returns: Sequence[ReturnLine]) -> Refund
| lines | SaleLine[] | the original sale, every line, whether returned or not |
| basket_discount | Money | discounts taken off the whole basket (a coupon, a staff discount); 0 for none |
| returns | ReturnLine[] | what is coming back now |
| returns | Refund |
The types it declares, generated into your project
@dataclass(frozen=True)
class SaleLine:
"""One line of the original sale."""
sku: str
#: units bought, 1 or more
quantity: int
#: what the line cost after its own promotions, before basket discounts
net: Money
#: units of this line already refunded by earlier returns
returned_before: int
@dataclass(frozen=True)
class ReturnLine:
"""Units of one sale line being returned."""
#: 0-based index into lines
line: int
#: 1 or more
quantity: int
@dataclass(frozen=True)
class RefundLine:
"""The refund for one returned line."""
line: int
sku: str
quantity: int
#: what the whole sale line cost after its share of the basket discount
paid: Money
#: the refund for the units returned now
amount: Money
@dataclass(frozen=True)
class Refund:
#: in the order the returns were given
lines: List[RefundLine]
total: Money
Your code names it in one line, in the file that uses it
from fune.retail.refund_calculate import calculate_refund # retail.refund-calculate@^1
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
from typing import List, Sequence
from .math_round_div import round_div ← from math.round-div ^1.0.0 · built alongside by fune
from .money_allocate import allocate ← from money.allocate ^1.0.0 · built alongside by fune
from .money_amount import Money, money ← from money.amount ^1.0.0 · built alongside by fune
from .money_sum import sum_money ← from money.sum ^1.0.0 · built alongside by fune
from .retail_refund_calculate_types import SaleLine, ReturnLine, RefundLine, Refund
def calculate_refund(lines: Sequence[SaleLine], basket_discount: Money, returns: Sequence[ReturnLine]) -> Refund:
"""Refund returned units at what they were actually paid: the basket
discount is shared across every sale line first, then each line refunds by
the difference of cumulative shares, so a line fully returned in several
visits refunds exactly what it cost.
"""
if len(lines) == 0:
raise ValueError("a sale needs at least one line")
currency = lines[0].net.currency
for line in lines:
if line.net.currency != currency:
raise ValueError("currency mismatch: %s and %s" % (currency, line.net.currency))
if line.quantity < 1:
raise ValueError("quantity must be 1 or more, received %d" % line.quantity)
if line.net.minor < 0:
raise ValueError("net must not be negative, received %d" % line.net.minor)
if line.returned_before < 0 or line.returned_before > line.quantity:
raise ValueError(
"returnedBefore must be from 0 to %d, received %d" % (line.quantity, line.returned_before)
)
if basket_discount.currency != currency:
raise ValueError("currency mismatch: %s and %s" % (currency, basket_discount.currency))
basket = sum_money([line.net for line in lines], currency)
if basket_discount.minor < 0:
raise ValueError("basketDiscount must not be negative, received %d" % basket_discount.minor)
if basket_discount.minor > basket.minor:
raise ValueError(
"basketDiscount %d is more than the lines' total %d" % (basket_discount.minor, basket.minor)
)
if basket_discount.minor == 0:
shares = [money(0, currency) for _ in lines]
else:
shares = allocate(basket_discount, [line.net.minor for line in lines])
paid = [line.net.minor - share.minor for line, share in zip(lines, shares)]
seen = set()
refunded: List[RefundLine] = []
for r in returns:
if isinstance(r.line, bool) or not isinstance(r.line, int) or r.line < 0 or r.line >= len(lines):
raise ValueError("no line %s in the sale" % (r.line,))
if r.line in seen:
raise ValueError("line %d is returned twice in one refund" % r.line)
seen.add(r.line)
if isinstance(r.quantity, bool) or not isinstance(r.quantity, int) or r.quantity < 1:
raise ValueError("return quantity must be 1 or more, received %r" % (r.quantity,))
line = lines[r.line]
if line.returned_before + r.quantity > line.quantity:
raise ValueError(
"cannot return %d of line %d: %d bought, %d already returned"
% (r.quantity, r.line, line.quantity, line.returned_before)
)
before = round_div(paid[r.line] * line.returned_before, line.quantity, "half-up")
after = round_div(paid[r.line] * (line.returned_before + r.quantity), line.quantity, "half-up")
refunded.append(
RefundLine(
line=r.line,
sku=line.sku,
quantity=r.quantity,
paid=money(paid[r.line], currency),
amount=money(after - before, currency),
)
)
return Refund(lines=refunded, total=sum_money([r.amount for r in refunded], currency))Install
fune build
With that line in your source, in a Python project (language python in fune.project), fune build resolves it and its 4 dependencies, pins them in fune.lock, downloads only the Python package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:
fune add retail.refund-calculate
The manifest, vectors and README with only the Python implementation. Install it without the registry with fune add ./retail.refund-calculate-1.0.0-python.fune, or fetch it from a terminal with fune pull retail.refund-calculate@1.0.0:python.
The whole function, every language, is one file too: retail.refund-calculate-1.0.0.fune, 29,559 bytes, sha256 1df6a683263a2e3f77ad9a3d4985b7433ada680a5e0188e673c69be3b18822e1. It installs into a project of any language.
Customise it in your app
The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.
before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.
# fune: before retail.refund-calculate
after — your function gets the result and the arguments, and returns the final result.
# fune: after retail.refund-calculate
replace — inside this capability’s code only, calls to a dependency go to your function, with the same signature. Other capabilities that use it are unaffected; write in * to replace it everywhere.
# fune: replace math.round-div in retail.refund-calculate
# fune: replace money.allocate in retail.refund-calculate
# fune: replace money.amount in retail.refund-calculate
# fune: replace money.sum in retail.refund-calculate
step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show retail.refund-calculate --steps.
# fune: step retail.refund-calculate after <n|label>
Tests
A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.
| Case | Arguments | Expected | |
|---|---|---|---|
| one of three teas, with its share of a 5.00 coupon | lines ×3, £5.00, returns ×1 | → | lines ×1, total £4.29 |
| a single mug refunds its whole paid price | lines ×3, £5.00, returns ×1 | → | lines ×1, total £6.86 |
| one of two books; the coupon's spare penny went to the book line | lines ×3, £5.00, returns ×1 | → | lines ×1, total £5.14 |
| returning everything refunds exactly what was paid | lines ×3, £5.00, returns ×3 | → | lines ×3, total £30.00 |
| the second tea of three, after one came back earlier | lines ×3, £5.00, returns ×1 | → | lines ×1, total £4.28 |
| the last tea: 4.29 + 4.28 + 4.29 is exactly 12.86 | lines ×3, £5.00, returns ×1 | → | lines ×1, total £4.29 |
| returns are listed in the order given | lines ×3, £5.00, returns ×2 | → | lines ×2, total £13.71 |
| no basket discount refunds the line net share | lines ×3, £0.00, returns ×1 | → | lines ×1, total £5.00 |
| nothing returned refunds nothing | lines ×3, £5.00, | → | lines , total £0.00 |
| a discount equal to the whole basket leaves nothing to refund | lines ×3, £35.00, returns ×1 | → | lines ×1, total £0.00 |
Show the other 6 tests
| Case | Arguments | Expected | |
|---|---|---|---|
| returning more than is left is an error | lines ×1, £0.00, returns ×1 | → | error: cannot return 2 of line 0: 3 bought, 2 already returned |
| an unknown line is an error | lines ×3, £0.00, returns ×1 | → | error: no line 5 in the sale |
| the same line twice in one return is an error | lines ×3, £0.00, returns ×2 | → | error: line 0 is returned twice in one refund |
| a discount bigger than the basket is an error | lines ×3, £40.00, returns ×1 | → | error: basketDiscount 4000 is more than the lines' total 3500 |
| a discount in another currency is an error | lines ×3, €5.00, returns ×1 | → | error: currency mismatch: GBP and EUR |
| a zero return quantity is an error | lines ×3, £0.00, returns ×1 | → | error: return quantity must be 1 or more |
More from the author
1. **Share the basket discount across every line of the sale**, returned or not, in proportion to each line's net, with `money.allocate`. A £5 coupon on a £35 basket takes £2.14 off the £15 line, £1.14 off the £8 line and £1.72 off the £12 line (the spare penny goes to the largest remainder), so each line's `paid` adds up to exactly what was taken at the till. 2. **Refund units of a line by cumulative share.** Returning `q` units when `r` have already come back refunds
`round(paid x (r + q) / quantity) - round(paid x r / quantity)`
rounding half up. Because each refund is the difference of two running totals, returning every unit, in any number of visits, refunds exactly `paid`. A line of three that cost £12.86 refunds £4.29, £4.28 and £4.29, never 3 x £4.29 = £12.87.
The line nets should already include line promotions (`retail.promotion-apply` allocates each deal back to the lines in its groups for exactly this reason), so a returned item from a 3 for 2 is refunded at its share of the deal, not at full price.
The naive refund, unit price times quantity, ignores the coupon and refunds £5.00 for an item that cost £4.29 after it.
## What it does not do
- It does not decide whether a refund is due; returns policies and consumer law (the 14-day cancellation period under the Consumer Contracts Regulations 2013, for instance) are the caller's. - Delivery charges are not refunded here. Under the Consumer Contracts Regulations a trader cancelling a whole distance order refunds the standard delivery too; add it when the whole order comes back. - It does not re-check promotions. If returning an item breaks a deal (one of a BOGOF pair), whether the kept item should now cost more is a policy decision; this refunds what the returned item was actually paid.
## Errors
Returning more units than remain, an unknown line, the same line twice in one return, a basket discount bigger than the basket, or mixed currencies.
Files
| Path | Bytes |
|---|---|
| README.md | 2,243 |
| impl/python.py | 3,608 |
| impl/rust.rs | 5,445 |
| impl/typescript.ts | 3,355 |
| vectors.json | 9,237 |