From f43b881295e03b7b945cb891fc35dcb35bade1c9 Mon Sep 17 00:00:00 2001 From: safavimohadeseh78 Date: Tue, 24 Nov 2026 18:32:00 +0330 Subject: [PATCH] feat: show selected rolls and color meterage The preorder summary lists each chosen color with its roll count and meterage. Co-authored-by: Cursor --- src/utils/cartSelections.ts | 70 +++++++++++++++++++ src/views/cart/components/DesktopCartPage.vue | 18 ++++- src/views/cart/components/MobileCartPage.vue | 27 ++++++- 3 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 src/utils/cartSelections.ts diff --git a/src/utils/cartSelections.ts b/src/utils/cartSelections.ts new file mode 100644 index 0000000..fc23b25 --- /dev/null +++ b/src/utils/cartSelections.ts @@ -0,0 +1,70 @@ +import type { CartItem } from '@/types' +import { normalizeDigits } from '@/utils/format' + +export interface CartColorLine { + key: string + label: string + rolls: number + meterage: number +} + +const COLOR_ROLL = /([^:|،]+):\s*(\d+(?:\.\d+)?)\s*طاقه/g + +function allocateMeterage(total: number, rolls: number[]): number[] { + const rollTotal = rolls.reduce((sum, qty) => sum + qty, 0) + if (rollTotal <= 0) return rolls.map(() => 0) + const shares = rolls.map((qty) => Math.round((total * qty) / rollTotal)) + const drift = total - shares.reduce((sum, qty) => sum + qty, 0) + const last = shares.length - 1 + if (last >= 0 && shares[last] != null) shares[last] += drift + return shares +} + +function selectionsFromNotes(notes: string, meterage: number): { color: string; rolls: number }[] { + const found: { color: string; rolls: number }[] = [] + for (const match of normalizeDigits(notes).matchAll(COLOR_ROLL)) { + const color = match[1]?.replace(/[|]/g, '').trim() + const rolls = Number(match[2]) + if (!color || !Number.isFinite(rolls) || rolls <= 0) continue + found.push({ color, rolls }) + } + return found +} + +export function cartColorLines(items: Pick[]): CartColorLine[] { + const severalProducts = new Set(items.map((item) => item.productName)).size > 1 + const lines: CartColorLine[] = [] + + for (const item of items) { + const parsed = item.notes ? selectionsFromNotes(item.notes, item.meterage) : [] + if (!parsed.length) { + lines.push({ + key: `${item.productName}:${item.color}`, + label: item.color || item.productName, + rolls: 0, + meterage: item.meterage, + }) + continue + } + + const merged = new Map() + for (const row of parsed) { + merged.set(row.color, (merged.get(row.color) ?? 0) + row.rolls) + } + const colors = [...merged.entries()] + const meterages = allocateMeterage( + item.meterage, + colors.map(([, rolls]) => rolls), + ) + colors.forEach(([color, rolls], index) => { + lines.push({ + key: `${item.productName}:${color}`, + label: severalProducts ? `${item.productName} · ${color}` : color, + rolls, + meterage: meterages[index] ?? 0, + }) + }) + } + + return lines +} diff --git a/src/views/cart/components/DesktopCartPage.vue b/src/views/cart/components/DesktopCartPage.vue index 324aa0e..03d6382 100644 --- a/src/views/cart/components/DesktopCartPage.vue +++ b/src/views/cart/components/DesktopCartPage.vue @@ -1,4 +1,5 @@