Skip to main content

Function: redistributeTotal()

redistributeTotal(current, newTotalCents): number[]

Defined in: src/lib/invoice/index.ts:114

Re-spread a corrected invoice total across its existing line amounts, preserving each line's proportion, with largest-remainder rounding so the parts sum EXACTLY to the new total (no penny drift, header always equals Σlines). When ALL current amounts are zero (or non-positive) the new total is split equally; a single zero line mixed with non-zero lines just receives 0. Used when an operator edits a multi-line (backfilled) invoice's total — a single line just becomes the whole total. Exact summing assumes an INTEGER newTotalCents (the only caller passes integer cents).

Parameters

current

readonly number[]

The existing per-line amounts in integer cents; negatives are treated as 0 for the proportion. An empty array yields an empty result.

newTotalCents

number

The corrected invoice total to spread, in integer cents (expected positive).

Returns

number[]

A new array of integer-cent line amounts, same length as current, summing EXACTLY to newTotalCents. Leftover pennies go to the lines with the largest fractional remainder.