fix: align stock and refill semantics

Squash merge PR #474
This commit is contained in:
Daniel Volz
2026-03-25 06:49:34 +01:00
committed by GitHub
parent 37fc2b8e66
commit 7059c25f1c
18 changed files with 1063 additions and 463 deletions
+26 -2
View File
@@ -15,7 +15,7 @@ export {
} from "./package-profiles";
import type { PackageType } from "./package-profiles";
import { isAmountBasedPackageType } from "./package-profiles";
import { isAmountBasedPackageType, isLiquidContainerPackageType, isTubePackageType } from "./package-profiles";
// Common medication dose units
export type DoseUnit = "mg" | "g" | "mcg" | "ml" | "units";
@@ -379,7 +379,10 @@ export function getMedDisplayName(med: { name: string; genericName?: string | nu
// Helper Functions for Medication Calculations
// =============================================================================
type MedLike = Pick<Medication, "packCount" | "blistersPerPack" | "pillsPerBlister" | "looseTablets"> & {
type MedLike = Pick<
Medication,
"packCount" | "blistersPerPack" | "pillsPerBlister" | "looseTablets" | "packageAmountValue"
> & {
stockAdjustment?: number;
packageType?: PackageType;
totalPills?: number | null;
@@ -387,6 +390,10 @@ type MedLike = Pick<Medication, "packCount" | "blistersPerPack" | "pillsPerBlist
/** Calculate total pills including stockAdjustment */
export function getMedTotal(med: MedLike): number {
if (med.packageType === "bottle") {
return med.looseTablets + (med.stockAdjustment ?? 0);
}
// Amount-based package types store their current base stock directly
// in totalPills (fallback looseTablets for legacy rows).
if (isAmountBasedPackageType(med.packageType)) {
@@ -399,6 +406,10 @@ export function getMedTotal(med: MedLike): number {
/** Get the base package size (without stockAdjustment) */
export function getPackageSize(med: MedLike): number {
if (med.packageType === "bottle") {
return med.totalPills ?? med.looseTablets;
}
// Amount-based package types use totalPills as base capacity
if (isAmountBasedPackageType(med.packageType)) {
return med.totalPills ?? med.looseTablets;
@@ -406,3 +417,16 @@ export function getPackageSize(med: MedLike): number {
// For blister type, calculate from packs + loose
return med.packCount * med.blistersPerPack * med.pillsPerBlister + med.looseTablets;
}
/** Get the configured structural capacity used for stock display/limits. */
export function getStockDisplayCapacity(med: MedLike): number {
if (isLiquidContainerPackageType(med.packageType) || isTubePackageType(med.packageType)) {
const packageCount = Math.max(1, med.packCount || 1);
const packageAmountValue = Number(med.packageAmountValue ?? 0);
if (Number.isFinite(packageAmountValue) && packageAmountValue > 0) {
return packageCount * packageAmountValue;
}
}
return getPackageSize(med);
}