Stock for sweets that are made fresh every day
A box of gulab jamun isn't sitting on a shelf; it's made that morning, and the kitchen can only make so many. So each product either has a counted stock that goes down with each order, or is made to order with an optional daily limit.
The day runs midnight to midnight Pakistan time, and cancelled orders don't count toward the limit. When a customer asks for more than is available, they get a clear reason: sold out for today, or only a few left.
const PK_OFFSET_MS = 5 * 60 * 60 * 1000 // Pakistan time, no DST
export function startOfShopDay(now = new Date()) {
const pk = new Date(now.getTime() + PK_OFFSET_MS)
pk.setUTCHours(0, 0, 0, 0)
return new Date(pk.getTime() - PK_OFFSET_MS)
}
export async function limitsFor(db, product) {
const stockLeft = product.madeToOrder ? Infinity : product.stockQty
const dailyLeft = product.dailyLimit == null
? Infinity
: Math.max(0, product.dailyLimit - (await orderedToday(db, product.id)))
return { stockLeft, dailyLeft, left: Math.min(stockLeft, dailyLeft) }
}


