feat: show selected rolls and color meterage
The preorder summary lists each chosen color with its roll count and meterage. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -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<CartItem, 'productName' | 'color' | 'meterage' | 'notes'>[]): 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<string, number>()
|
||||
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
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import CheckoutStepper from '@/components/layout/CheckoutStepper.vue'
|
||||
import IconCart from '@/components/icons/IconCart.vue'
|
||||
@@ -10,10 +11,11 @@ import IconLightbulb from '@/components/icons/IconLightbulb.vue'
|
||||
import IconLayoutGrid from '@/components/icons/IconLayoutGrid.vue'
|
||||
import IconDiamond from '@/components/icons/IconDiamond.vue'
|
||||
import { formatMeterage, formatNumber } from '@/utils/format'
|
||||
import { cartColorLines } from '@/utils/cartSelections'
|
||||
import type { CartItem, PaymentLimits, SettlementType } from '@/types'
|
||||
import PreorderPaymentFields from '@/components/orders/PreorderPaymentFields.vue'
|
||||
|
||||
defineProps<{
|
||||
const props = defineProps<{
|
||||
items: CartItem[]
|
||||
companyName: string
|
||||
companySummaries?: {
|
||||
@@ -33,6 +35,9 @@ defineProps<{
|
||||
paymentLimits?: PaymentLimits
|
||||
}>()
|
||||
|
||||
const colorLines = computed(() => cartColorLines(props.items))
|
||||
const totalRolls = computed(() => colorLines.value.reduce((sum, line) => sum + line.rolls, 0))
|
||||
|
||||
const emit = defineEmits<{
|
||||
remove: [id: string]
|
||||
checkout: []
|
||||
@@ -165,6 +170,17 @@ const quickLinks = [
|
||||
</span>
|
||||
<strong>{{ formatMeterage(totalMeterage) }}</strong>
|
||||
</div>
|
||||
<div v-if="totalRolls > 0" class="cart-summary-card__row">
|
||||
<span class="cart-summary-card__row-label">تعداد طاقه</span>
|
||||
<strong>{{ formatNumber(totalRolls) }} طاقه</strong>
|
||||
</div>
|
||||
<div v-for="line in colorLines" :key="line.key" class="cart-summary-card__row">
|
||||
<span class="cart-summary-card__row-label">{{ line.label }}</span>
|
||||
<strong>
|
||||
<template v-if="line.rolls">{{ formatNumber(line.rolls) }} طاقه · </template>
|
||||
{{ formatMeterage(line.meterage) }}
|
||||
</strong>
|
||||
</div>
|
||||
<div class="cart-summary-card__row cart-summary-card__row--hint">
|
||||
<span class="cart-summary-card__row-label">
|
||||
<IconOrders :size="16" />
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue'
|
||||
import BaseButton from '@/components/ui/BaseButton.vue'
|
||||
import EmptyState from '@/components/ui/EmptyState.vue'
|
||||
import IconTrash from '@/components/icons/IconTrash.vue'
|
||||
import IconChevronBack from '@/components/icons/IconChevronBack.vue'
|
||||
import { formatMeterage, formatNumber } from '@/utils/format'
|
||||
import { cartColorLines } from '@/utils/cartSelections'
|
||||
import type { CartItem, PaymentLimits, SettlementType } from '@/types'
|
||||
import PreorderPaymentFields from '@/components/orders/PreorderPaymentFields.vue'
|
||||
|
||||
defineProps<{
|
||||
const props = defineProps<{
|
||||
items: CartItem[]
|
||||
companyName: string
|
||||
companySummaries?: {
|
||||
@@ -27,6 +29,9 @@ defineProps<{
|
||||
paymentLimits?: PaymentLimits
|
||||
}>()
|
||||
|
||||
const colorLines = computed(() => cartColorLines(props.items))
|
||||
const totalRolls = computed(() => colorLines.value.reduce((sum, line) => sum + line.rolls, 0))
|
||||
|
||||
const emit = defineEmits<{
|
||||
back: []
|
||||
remove: [id: string]
|
||||
@@ -82,6 +87,21 @@ const emit = defineEmits<{
|
||||
<span>جمع متراژ کل</span>
|
||||
<strong>{{ formatMeterage(totalMeterage) }}</strong>
|
||||
</div>
|
||||
<div v-if="totalRolls > 0" class="cart-mobile-summary__row">
|
||||
<span>تعداد طاقه</span>
|
||||
<strong>{{ formatNumber(totalRolls) }} طاقه</strong>
|
||||
</div>
|
||||
<div
|
||||
v-for="line in colorLines"
|
||||
:key="line.key"
|
||||
class="cart-mobile-summary__row cart-mobile-summary__row--color"
|
||||
>
|
||||
<span>{{ line.label }}</span>
|
||||
<strong>
|
||||
<template v-if="line.rolls">{{ formatNumber(line.rolls) }} طاقه · </template>
|
||||
{{ formatMeterage(line.meterage) }}
|
||||
</strong>
|
||||
</div>
|
||||
</div>
|
||||
<PreorderPaymentFields
|
||||
:settlement-type="settlementType"
|
||||
@@ -275,6 +295,11 @@ const emit = defineEmits<{
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
|
||||
.cart-mobile-summary__row--color {
|
||||
color: var(--color-text-muted);
|
||||
font-size: 0.8125rem;
|
||||
}
|
||||
|
||||
.cart-mobile-summary__row:not(:last-child) {
|
||||
border-bottom: 1px solid #e8edf3;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user