Add bundle types: pick any, buy X get Y free, group discount
Bundles get a kind: exact (as before), mix (any setSize of the qualifying tickets for a set price), free (cheapest freeCount in each set free) and group (setSize or more qualifying tickets at a per-ticket price or % off, applied once). The supervisor page has a type picker, buyers see a one-line description of each deal, and the Complete bundle cards handle every type. Demo: Family Fun Day adds a Lunch for four deal. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -54,18 +54,15 @@ export function BundleOffers({ bundles, className = "" }: { bundles?: PublicBund
|
||||
<div className={`rounded-lg border border-emerald-200 bg-emerald-50 p-3 text-sm text-emerald-900 ${className}`}>
|
||||
<div className="flex items-center gap-1.5 font-semibold"><Gift className="h-4 w-4" /> Bundle deals</div>
|
||||
<ul className="mt-1.5 space-y-1">
|
||||
{bundles.map(b => {
|
||||
const save = b.standardValue - b.price;
|
||||
return (
|
||||
<li key={b.id}>
|
||||
<span className="font-medium">{b.name}:</span> {b.summary} for <span className="font-semibold">{fmt(b.price)}</span>
|
||||
{save > 0.005 && <span className="text-emerald-700"> (save {fmt(save)})</span>}
|
||||
{b.description && <span className="block text-xs text-emerald-800/80">{b.description}</span>}
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
{bundles.map(b => (
|
||||
<li key={b.id}>
|
||||
<span className="font-medium">{b.name}:</span> {b.headline}
|
||||
{b.savingText && <span className="text-emerald-700"> ({b.savingText})</span>}
|
||||
{b.description && <span className="block text-xs text-emerald-800/80">{b.description}</span>}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
<p className="mt-1.5 text-xs text-emerald-800/80">Picked automatically when your tickets match, as many times as they fit.</p>
|
||||
<p className="mt-1.5 text-xs text-emerald-800/80">Applied automatically when your tickets match.</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -98,24 +95,49 @@ type BuilderOption = {
|
||||
availableCount?: number | null;
|
||||
variants?: { id: string; name: string; stockLimit?: number | null; availableCount?: number | null }[] | null;
|
||||
};
|
||||
type Qty = (optionId: string, variantId: string | null) => number;
|
||||
type Change = { optionId: string; variantId: string | null; quantity: number };
|
||||
|
||||
const leftOf = (stockLimit?: number | null, availableCount?: number | null) => ((stockLimit ?? 0) > 0 && availableCount !== undefined && availableCount !== null ? availableCount : Infinity);
|
||||
|
||||
/** Tickets of this item already in the basket. An "any variant" item counts every variant. */
|
||||
function itemHave(item: BundleItem, opt: BuilderOption | undefined, qty: (optionId: string, variantId: string | null) => number) {
|
||||
function itemHave(item: BundleItem, opt: BuilderOption | undefined, qty: Qty) {
|
||||
if (!opt) return 0;
|
||||
if (item.variantId) return qty(opt.id, item.variantId);
|
||||
const variants = opt.variants || [];
|
||||
return variants.length > 0 ? variants.reduce((s, v) => s + qty(opt.id, v.id), 0) : qty(opt.id, null);
|
||||
}
|
||||
|
||||
const leftOf = (stockLimit?: number | null, availableCount?: number | null) => ((stockLimit ?? 0) > 0 && availableCount !== undefined && availableCount !== null ? availableCount : Infinity);
|
||||
/** Every basket line (option, or option + variant) that counts towards a bundle, without repeats. */
|
||||
function qualifyingKeys(bundle: PublicBundle, options: BuilderOption[]) {
|
||||
const keys: { opt: BuilderOption; variantId: string | null }[] = [];
|
||||
const seen = new Set<string>();
|
||||
for (const item of bundle.items) {
|
||||
const opt = options.find(o => o.id === item.eventOptionId);
|
||||
if (!opt) continue;
|
||||
const variants = opt.variants || [];
|
||||
const ids = variants.length === 0 ? [null] : item.variantId ? [item.variantId] : variants.map(v => v.id);
|
||||
for (const variantId of ids) {
|
||||
const k = `${opt.id}::${variantId || ""}`;
|
||||
if (!seen.has(k)) { seen.add(k); keys.push({ opt, variantId }); }
|
||||
}
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
/** Places left for one basket line, or Infinity. */
|
||||
function roomFor(opt: BuilderOption, variantId: string | null, qty: Qty) {
|
||||
const variant = variantId ? (opt.variants || []).find(v => v.id === variantId) : null;
|
||||
return Math.min(leftOf(opt.stockLimit, opt.availableCount), variant ? leftOf(variant.stockLimit, variant.availableCount) : Infinity) - qty(opt.id, variantId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Quantities that make up `sets` full bundles, or null when there aren't enough tickets
|
||||
* left. Only ever raises quantities. An "any variant" item tops up the variant already
|
||||
* picked most (or the first one with places left).
|
||||
* Exact bundles: quantities that make up `sets` full sets, or null when there aren't
|
||||
* enough tickets left. Only ever raises quantities. An "any variant" item tops up the
|
||||
* variant already picked most (or the first one with places left).
|
||||
*/
|
||||
function quantitiesForSets(bundle: PublicBundle, options: BuilderOption[], qty: (o: string, v: string | null) => number, sets: number) {
|
||||
const changes: { optionId: string; variantId: string | null; quantity: number }[] = [];
|
||||
function exactChanges(bundle: PublicBundle, options: BuilderOption[], qty: Qty, sets: number): Change[] | null {
|
||||
const changes: Change[] = [];
|
||||
for (const item of bundle.items) {
|
||||
const opt = options.find(o => o.id === item.eventOptionId);
|
||||
if (!opt) return null;
|
||||
@@ -123,75 +145,128 @@ function quantitiesForSets(bundle: PublicBundle, options: BuilderOption[], qty:
|
||||
if (short <= 0) continue;
|
||||
let variantId = item.variantId || null;
|
||||
if (!variantId && (opt.variants || []).length > 0) {
|
||||
const withRoom = (opt.variants || []).filter(v => leftOf(v.stockLimit, v.availableCount) - qty(opt.id, v.id) >= short);
|
||||
const pick = withRoom.sort((a, b) => qty(opt.id, b.id) - qty(opt.id, a.id))[0];
|
||||
const pick = (opt.variants || []).filter(v => roomFor(opt, v.id, qty) >= short).sort((a, b) => qty(opt.id, b.id) - qty(opt.id, a.id))[0];
|
||||
if (!pick) return null;
|
||||
variantId = pick.id;
|
||||
}
|
||||
const current = qty(opt.id, variantId);
|
||||
const variant = variantId ? (opt.variants || []).find(v => v.id === variantId) : null;
|
||||
if (current + short > Math.min(leftOf(opt.stockLimit, opt.availableCount), variant ? leftOf(variant.stockLimit, variant.availableCount) : Infinity)) return null;
|
||||
changes.push({ optionId: opt.id, variantId, quantity: current + short });
|
||||
if (roomFor(opt, variantId, qty) < short) return null;
|
||||
changes.push({ optionId: opt.id, variantId, quantity: qty(opt.id, variantId) + short });
|
||||
}
|
||||
return changes;
|
||||
}
|
||||
|
||||
/** Pick-any and group bundles: add `count` tickets to the qualifying line picked most. */
|
||||
function topUpChange(bundle: PublicBundle, options: BuilderOption[], qty: Qty, count: number): Change[] | null {
|
||||
if (count <= 0) return [];
|
||||
const pick = qualifyingKeys(bundle, options)
|
||||
.filter(k => roomFor(k.opt, k.variantId, qty) >= count)
|
||||
.sort((a, b) => qty(b.opt.id, b.variantId) - qty(a.opt.id, a.variantId))[0];
|
||||
if (!pick) return null;
|
||||
return [{ optionId: pick.opt.id, variantId: pick.variantId, quantity: qty(pick.opt.id, pick.variantId) + count }];
|
||||
}
|
||||
|
||||
const orList = (summary: string) => summary.replace(/, ([^,]*)$/, " or $1");
|
||||
const plural = (n: number, word: string) => `${n} ${word}${n === 1 ? "" : "s"}`;
|
||||
|
||||
type Row = {
|
||||
bundle: PublicBundle;
|
||||
// Something from the bundle is in the basket.
|
||||
touched: boolean;
|
||||
// Full sets in the basket (for a group discount: 1 once it applies).
|
||||
sets: number;
|
||||
// A set (or the group) has been started but isn't complete.
|
||||
partial: boolean;
|
||||
// What's missing, in words.
|
||||
missing: string;
|
||||
// Quantities to complete it (partial) or add another set (complete); null = no room.
|
||||
next: Change[] | null;
|
||||
// false for a group discount once it applies: it has no "another one".
|
||||
canAddMore: boolean;
|
||||
};
|
||||
|
||||
function rowFor(bundle: PublicBundle, options: BuilderOption[], qty: Qty): Row {
|
||||
const kind = bundle.kind || "exact";
|
||||
if (kind === "exact") {
|
||||
const have = bundle.items.map(item => {
|
||||
const opt = options.find(o => o.id === item.eventOptionId);
|
||||
return { item, opt, n: itemHave(item, opt, qty) };
|
||||
});
|
||||
const sets = Math.min(...have.map(h => Math.floor(h.n / h.item.quantity)));
|
||||
// Tickets beyond the full sets mean the next set has been started.
|
||||
const partial = have.some(h => h.n > sets * h.item.quantity);
|
||||
const missing = have
|
||||
.filter(h => h.n < (sets + 1) * h.item.quantity)
|
||||
.map(h => {
|
||||
const v = h.item.variantId ? (h.opt?.variants || []).find(x => x.id === h.item.variantId) : null;
|
||||
return `${(sets + 1) * h.item.quantity - h.n} × ${h.opt?.name || "ticket"}${v ? ` (${v.name})` : ""}`;
|
||||
})
|
||||
.join(" and ");
|
||||
return { bundle, touched: have.some(h => h.n > 0), sets, partial, missing, next: exactChanges(bundle, options, qty, sets + 1), canAddMore: true };
|
||||
}
|
||||
|
||||
const size = Number(bundle.setSize) || 1;
|
||||
const have = qualifyingKeys(bundle, options).reduce((s, k) => s + qty(k.opt.id, k.variantId), 0);
|
||||
const names = orList(bundle.summary);
|
||||
if (kind === "group") {
|
||||
const short = Math.max(0, size - have);
|
||||
return {
|
||||
bundle, touched: have > 0, sets: short === 0 ? 1 : 0, partial: short > 0,
|
||||
missing: `${plural(short, "more ticket")} (${names})`,
|
||||
next: topUpChange(bundle, options, qty, short), canAddMore: false,
|
||||
};
|
||||
}
|
||||
const sets = Math.floor(have / size);
|
||||
const short = have % size === 0 ? size : size - (have % size);
|
||||
return {
|
||||
bundle, touched: have > 0, sets, partial: have % size > 0,
|
||||
missing: `${plural(short, "more ticket")} (${names})`,
|
||||
next: topUpChange(bundle, options, qty, short), canAddMore: true,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* On the register form: each bundle the basket has started on, with what's missing and a
|
||||
* "Complete bundle" button, then "+" to add another full set. Buttons set the ticket
|
||||
* quantities through `onSetQuantity`.
|
||||
* On the register form, at the door, manual registration and the kiosk: each bundle the
|
||||
* basket has started on, with what's missing and a "Complete" button, then "+" to add
|
||||
* another full set. Buttons set the ticket quantities through `onSetQuantity`.
|
||||
*/
|
||||
export function BundleBuilder({ bundles, options, qty, onSetQuantity, className = "" }: {
|
||||
bundles?: PublicBundle[] | null;
|
||||
options: BuilderOption[];
|
||||
qty: (optionId: string, variantId: string | null) => number;
|
||||
qty: Qty;
|
||||
onSetQuantity: (optionId: string, variantId: string | null, quantity: number) => void;
|
||||
className?: string;
|
||||
}) {
|
||||
const rows = (bundles || []).map(bundle => {
|
||||
const have = bundle.items.map(item => ({ item, opt: options.find(o => o.id === item.eventOptionId), n: itemHave(item, options.find(o => o.id === item.eventOptionId), qty) }));
|
||||
const sets = Math.min(...have.map(h => Math.floor(h.n / h.item.quantity)));
|
||||
const touched = have.some(h => h.n > 0);
|
||||
// Tickets beyond the full sets mean the next set has been started.
|
||||
const partial = have.some(h => h.n > sets * h.item.quantity);
|
||||
const missing = partial ? have.filter(h => h.n < (sets + 1) * h.item.quantity).map(h => ({ ...h, short: (sets + 1) * h.item.quantity - h.n })) : [];
|
||||
const next = quantitiesForSets(bundle, options, qty, sets + 1);
|
||||
return { bundle, touched, sets, partial, missing, next };
|
||||
}).filter(r => r.touched);
|
||||
const rows = (bundles || []).map(b => rowFor(b, options, qty)).filter(r => r.touched);
|
||||
if (rows.length === 0) return null;
|
||||
|
||||
const apply = (changes: NonNullable<ReturnType<typeof quantitiesForSets>>) => changes.forEach(c => onSetQuantity(c.optionId, c.variantId, c.quantity));
|
||||
const itemName = (item: BundleItem, opt?: BuilderOption) => {
|
||||
const v = item.variantId ? (opt?.variants || []).find(x => x.id === item.variantId) : null;
|
||||
return `${opt?.name || "ticket"}${v ? ` (${v.name})` : ""}`;
|
||||
};
|
||||
const apply = (changes: Change[]) => changes.forEach(c => onSetQuantity(c.optionId, c.variantId, c.quantity));
|
||||
|
||||
return (
|
||||
<div className={`space-y-2 ${className}`}>
|
||||
{rows.map(({ bundle, sets, partial, missing, next }) => {
|
||||
const save = bundle.standardValue - bundle.price;
|
||||
{rows.map(({ bundle, sets, partial, missing, next, canAddMore }) => {
|
||||
const isGroup = bundle.kind === "group";
|
||||
const done = isGroup ? `✓ Group price on all your ${orList(bundle.summary)} tickets` : `✓ ${sets > 1 ? `${sets} bundles` : "Bundle"} in your basket`;
|
||||
return (
|
||||
<div key={bundle.id} className="flex flex-wrap items-center justify-between gap-3 rounded-lg border border-emerald-200 bg-emerald-50 p-3 text-sm text-emerald-900">
|
||||
<div className="min-w-0">
|
||||
<div className="min-w-0 flex-1">
|
||||
<div className="flex items-center gap-1.5 font-semibold"><Gift className="h-4 w-4 shrink-0" /> {bundle.name}</div>
|
||||
<div className="text-xs text-emerald-800">
|
||||
{bundle.summary} for {fmt(bundle.price)}{save > 0.005 ? ` (save ${fmt(save)})` : ""}
|
||||
{bundle.headline}{bundle.savingText ? ` (${bundle.savingText})` : ""}
|
||||
</div>
|
||||
{partial ? (
|
||||
<div className="text-xs text-emerald-800 mt-0.5">
|
||||
Add {missing.map(m => `${m.short} × ${itemName(m.item, m.opt)}`).join(" and ")} to {sets > 0 ? "complete another bundle" : "complete it"}.
|
||||
Add {missing} to {isGroup ? "get the group price" : sets > 0 ? "complete another bundle" : "complete it"}.
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-xs font-medium text-emerald-700 mt-0.5">✓ {sets > 1 ? `${sets} bundles` : "Bundle"} in your basket</div>
|
||||
<div className="text-xs font-medium text-emerald-700 mt-0.5">{done}</div>
|
||||
)}
|
||||
{!next && <div className="text-xs text-amber-700 mt-0.5">Not enough tickets left for {partial ? "this bundle" : "another one"}.</div>}
|
||||
{!next && (partial || canAddMore) && <div className="text-xs text-amber-700 mt-0.5">Not enough tickets left for {partial ? "this bundle" : "another one"}.</div>}
|
||||
</div>
|
||||
{partial ? (
|
||||
<button type="button" onClick={() => next && apply(next)} disabled={!next}
|
||||
className="shrink-0 rounded-md bg-emerald-600 px-3 py-1.5 text-xs font-semibold text-white hover:bg-emerald-700 disabled:opacity-50">
|
||||
Complete bundle
|
||||
{isGroup ? "Complete group" : "Complete bundle"}
|
||||
</button>
|
||||
) : (
|
||||
) : canAddMore ? (
|
||||
<div className="flex shrink-0 items-center gap-2">
|
||||
<span className="text-xs font-medium">×{sets}</span>
|
||||
<button type="button" onClick={() => next && apply(next)} disabled={!next} aria-label={`Add another ${bundle.name}`} title="Add another bundle"
|
||||
@@ -199,7 +274,7 @@ export function BundleBuilder({ bundles, options, qty, onSetQuantity, className
|
||||
+
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
|
||||
Reference in New Issue
Block a user