Files
medassist-ng/frontend/src/utils/stock.ts
T
Daniel Volz 612aa007aa fix: unify stock semantics across planner and scheduler (#245)
* fix: unify stock semantics across planner and scheduler

* fix: stabilize dashboard hmr and align stock helper tests
2026-02-21 15:24:53 +01:00

44 lines
1.4 KiB
TypeScript

import type { Medication } from "../types";
export type BlisterStockSplit = {
fullBlisters: number;
openBlisterPills: number;
loosePills: number;
};
/**
* Split current blister stock into sealed full blisters, open blister pills,
* and loose pills using the configured loose-tablets baseline.
*/
export function splitCurrentBlisterStock(
currentPills: number,
pillsPerBlister: number,
configuredLooseTablets: number
): BlisterStockSplit {
if (pillsPerBlister <= 0 || pillsPerBlister === 1) {
return { fullBlisters: 0, openBlisterPills: 0, loosePills: Math.max(0, currentPills) };
}
const safeCurrent = Math.max(0, currentPills);
const loosePills = Math.min(safeCurrent, Math.max(0, configuredLooseTablets));
const sealedPills = Math.max(0, safeCurrent - loosePills);
return {
fullBlisters: Math.floor(sealedPills / pillsPerBlister),
openBlisterPills: sealedPills % pillsPerBlister,
loosePills,
};
}
/**
* Convenience helper when medication object already contains stock fields.
*/
export function getBlisterStockFromMedication(med: Medication): BlisterStockSplit {
const total =
(med.packageType === "bottle"
? med.looseTablets + (med.stockAdjustment ?? 0)
: med.packCount * med.blistersPerPack * med.pillsPerBlister + med.looseTablets + (med.stockAdjustment ?? 0)) ?? 0;
return splitCurrentBlisterStock(total, med.pillsPerBlister, med.looseTablets);
}