/** * Finds the highest quantity that can be sold of an item adjusted to its `multipleQuantity` * @param stock item stock * @param maxQuantity maximum quantity of this item that can be added * @param multipleQuantity `multipleQuantity` for the item * @returns the highest quantity of the item that can be sold */ export const getMaximumValue = ( stock: number, maxQuantity: number, multipleQuantity: number ): number => { const maxValue = Math.min(stock, maxQuantity); const maxQuantityTimes = Math.floor(maxValue / multipleQuantity) ?? 1; const maxStockMultipleQuantity = maxQuantityTimes * multipleQuantity; return maxStockMultipleQuantity; }; /** * Find the `multipleQuantity` closest to `quantity` * @param quantity amount you want to add * @param multipleQuantity multipleQuantity of this item * @param stock item stock * @returns quantity that complies with the `multiple Quantity` */ export const roundToMultipleQuantity = ( quantity: number, multipleQuantity: number, stock: number ) => { const roundNumber = Math.round(quantity / multipleQuantity) * multipleQuantity; const roundStock = Math.floor(stock / multipleQuantity) * multipleQuantity; return stock < roundNumber ? roundStock : roundNumber; }; export const getDiscount = (regularPrice: number, discountedPrice: number) => ((regularPrice - discountedPrice) / regularPrice) * 100;