The forecast is the team's worksheet, not a textbook formula
Why it breaks
A generic "sales per day × days" forecast did not subtract stock on hand, treated every size as its own product, and double-counted unfulfilled orders by reading Shopify's available stock. The store buys bulk cases and repacks them into smaller bags, so a per-size forecast orders the wrong thing entirely, and its numbers never matched the purchasing team's paper worksheet.
What I did
The forecast implements the team's rule exactly: the larger of the last 90 days and a quarter of the last year, scaled to the chosen horizon, minus stock on hand. Sizes marked as repacked are converted to pounds and rolled into the bulk size the store actually buys, rounded to the nearest case to match the worksheet. Units on open orders are shown in the note but never counted as stock. The worksheet's own examples are the tests.
export function demandFromSales(
sold90: number,
sold365: number,
days: number = DEFAULT_FORECAST_DAYS,
): Demand {
const quarterlyAvg = Math.round(sold365 / 4);
const usesYear = quarterlyAvg > sold90;
const basis = Math.max(sold90, quarterlyAvg);
return {
sold90,
sold365,
quarterlyAvg,
usesYear,
need: Math.round((basis * days) / 90),
days,
};
}