feat: limit checkout payment options from product rules
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue'
|
||||
import type { SettlementType } from '@/types'
|
||||
|
||||
defineProps<{
|
||||
const props = defineProps<{
|
||||
settlementType: SettlementType | null
|
||||
checkTermMonths: number | null
|
||||
allowedMethods?: SettlementType[]
|
||||
maxCheckMonths?: number
|
||||
}>()
|
||||
|
||||
const emit = defineEmits<{
|
||||
@@ -11,20 +14,30 @@ const emit = defineEmits<{
|
||||
'update:checkTermMonths': [value: number]
|
||||
}>()
|
||||
|
||||
const methods: { value: SettlementType; title: string; hint: string }[] = [
|
||||
const methodCatalog: { value: SettlementType; title: string; hint: string }[] = [
|
||||
{ value: 'cash', title: 'نقدی', hint: 'کل مبلغ بهصورت نقدی' },
|
||||
{ value: 'cash_check', title: 'نصف نقد، نصف چکی', hint: 'نیمی نقد و نیمی چک' },
|
||||
{ value: 'check', title: 'چکی', hint: 'کل مبلغ با چک' },
|
||||
]
|
||||
|
||||
const terms = [1, 2, 3, 4]
|
||||
const methods = computed(() => {
|
||||
const allowed = props.allowedMethods
|
||||
if (!allowed?.length) return allowed ? [] : methodCatalog
|
||||
return methodCatalog.filter((method) => allowed.includes(method.value))
|
||||
})
|
||||
|
||||
const terms = computed(() => {
|
||||
const max = Math.min(4, Math.max(1, props.maxCheckMonths ?? 4))
|
||||
return Array.from({ length: max }, (_, index) => index + 1)
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<fieldset class="pay-plan">
|
||||
<legend class="pay-plan__legend">روش پرداخت</legend>
|
||||
<p class="pay-plan__note">قیمت پیشسفارش به روش پرداخت بستگی دارد. قبل از اعلام قیمت باید مشخص شود.</p>
|
||||
<div class="pay-plan__methods">
|
||||
<p v-if="!methods.length" class="pay-plan__note">روش پرداخت مشترکی برای محصولات این سفارش وجود ندارد.</p>
|
||||
<div v-else class="pay-plan__methods">
|
||||
<button
|
||||
v-for="method in methods"
|
||||
:key="method.value"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { computed, ref, onMounted } from 'vue'
|
||||
import { computed, ref, onMounted, watch } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { useCartStore } from '@/stores/cart'
|
||||
import { useOrderStore } from '@/stores/order'
|
||||
@@ -14,6 +14,22 @@ export function useCartPage() {
|
||||
const submitError = ref('')
|
||||
const settlementType = ref<SettlementType | null>(null)
|
||||
const checkTermMonths = ref<number | null>(null)
|
||||
const paymentLimits = computed(
|
||||
() =>
|
||||
cartStore.activeCart?.paymentLimits ?? {
|
||||
methods: ['cash', 'cash_check', 'check'] as SettlementType[],
|
||||
maxCheckMonths: 4,
|
||||
},
|
||||
)
|
||||
|
||||
watch(paymentLimits, (limits) => {
|
||||
if (settlementType.value && !limits.methods.includes(settlementType.value)) {
|
||||
settlementType.value = null
|
||||
}
|
||||
if (checkTermMonths.value && checkTermMonths.value > limits.maxCheckMonths) {
|
||||
checkTermMonths.value = null
|
||||
}
|
||||
})
|
||||
|
||||
const items = computed(() => cartStore.activeCart?.items ?? [])
|
||||
const isEmpty = computed(() => items.value.length === 0)
|
||||
@@ -45,11 +61,15 @@ export function useCartPage() {
|
||||
submitError.value = 'قبل از ثبت پیشسفارش روش پرداخت را انتخاب کنید.'
|
||||
return
|
||||
}
|
||||
if (!paymentLimits.value.methods.includes(settlementType.value)) {
|
||||
submitError.value = 'این روش پرداخت برای محصولات سبد مجاز نیست.'
|
||||
return
|
||||
}
|
||||
if (
|
||||
(settlementType.value === 'check' || settlementType.value === 'cash_check') &&
|
||||
!checkTermMonths.value
|
||||
(!checkTermMonths.value || checkTermMonths.value > paymentLimits.value.maxCheckMonths)
|
||||
) {
|
||||
submitError.value = 'مدت چک را از ۱ تا ۴ ماه انتخاب کنید.'
|
||||
submitError.value = `مدت چک را تا ${paymentLimits.value.maxCheckMonths} ماه انتخاب کنید.`
|
||||
return
|
||||
}
|
||||
submitting.value = true
|
||||
@@ -109,6 +129,7 @@ export function useCartPage() {
|
||||
submitError,
|
||||
settlementType,
|
||||
checkTermMonths,
|
||||
paymentLimits,
|
||||
removeItem,
|
||||
proceedToCheckout,
|
||||
proceedAnyway,
|
||||
|
||||
@@ -101,6 +101,11 @@ export interface Product {
|
||||
isActive: boolean
|
||||
}
|
||||
|
||||
export interface PaymentLimits {
|
||||
methods: SettlementType[]
|
||||
maxCheckMonths: number
|
||||
}
|
||||
|
||||
export interface CartItem {
|
||||
id: string
|
||||
productId: string
|
||||
@@ -121,6 +126,7 @@ export interface Cart {
|
||||
companyName: string
|
||||
items: CartItem[]
|
||||
notes?: string
|
||||
paymentLimits?: PaymentLimits
|
||||
}
|
||||
|
||||
export interface CheckData {
|
||||
@@ -191,6 +197,8 @@ export interface Order {
|
||||
settlementType: SettlementType | null
|
||||
/** مدت چک به ماه (۱ تا ۴) برای روشهای چکی */
|
||||
checkTermMonths?: number | null
|
||||
/** اشتراک محدودیت پرداخت محصولات همین سفارش */
|
||||
paymentLimits?: PaymentLimits
|
||||
checkMode?: CheckRegistrationMode
|
||||
checks?: CheckData[]
|
||||
checkRassDays?: number
|
||||
|
||||
@@ -20,6 +20,7 @@ const {
|
||||
submitError,
|
||||
settlementType,
|
||||
checkTermMonths,
|
||||
paymentLimits,
|
||||
removeItem,
|
||||
proceedToCheckout,
|
||||
proceedAnyway,
|
||||
@@ -47,6 +48,7 @@ function goToOrdering() {
|
||||
:submitting="submitting"
|
||||
:settlement-type="settlementType"
|
||||
:check-term-months="checkTermMonths"
|
||||
:payment-limits="paymentLimits"
|
||||
@update:settlement-type="settlementType = $event"
|
||||
@update:check-term-months="checkTermMonths = $event"
|
||||
@back="router.push('/')"
|
||||
@@ -67,6 +69,7 @@ function goToOrdering() {
|
||||
:submitting="submitting"
|
||||
:settlement-type="settlementType"
|
||||
:check-term-months="checkTermMonths"
|
||||
:payment-limits="paymentLimits"
|
||||
@update:settlement-type="settlementType = $event"
|
||||
@update:check-term-months="checkTermMonths = $event"
|
||||
@remove="removeItem"
|
||||
|
||||
@@ -10,7 +10,7 @@ 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 type { CartItem, SettlementType } from '@/types'
|
||||
import type { CartItem, PaymentLimits, SettlementType } from '@/types'
|
||||
import PreorderPaymentFields from '@/components/orders/PreorderPaymentFields.vue'
|
||||
|
||||
defineProps<{
|
||||
@@ -30,6 +30,7 @@ defineProps<{
|
||||
submitting?: boolean
|
||||
settlementType: SettlementType | null
|
||||
checkTermMonths: number | null
|
||||
paymentLimits?: PaymentLimits
|
||||
}>()
|
||||
|
||||
const emit = defineEmits<{
|
||||
@@ -188,6 +189,8 @@ const quickLinks = [
|
||||
<PreorderPaymentFields
|
||||
:settlement-type="settlementType"
|
||||
:check-term-months="checkTermMonths"
|
||||
:allowed-methods="paymentLimits?.methods"
|
||||
:max-check-months="paymentLimits?.maxCheckMonths"
|
||||
@update:settlement-type="emit('update:settlementType', $event)"
|
||||
@update:check-term-months="emit('update:checkTermMonths', $event)"
|
||||
/>
|
||||
|
||||
@@ -4,7 +4,7 @@ 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 type { CartItem, SettlementType } from '@/types'
|
||||
import type { CartItem, PaymentLimits, SettlementType } from '@/types'
|
||||
import PreorderPaymentFields from '@/components/orders/PreorderPaymentFields.vue'
|
||||
|
||||
defineProps<{
|
||||
@@ -24,6 +24,7 @@ defineProps<{
|
||||
submitting?: boolean
|
||||
settlementType: SettlementType | null
|
||||
checkTermMonths: number | null
|
||||
paymentLimits?: PaymentLimits
|
||||
}>()
|
||||
|
||||
const emit = defineEmits<{
|
||||
@@ -106,6 +107,8 @@ const emit = defineEmits<{
|
||||
<PreorderPaymentFields
|
||||
:settlement-type="settlementType"
|
||||
:check-term-months="checkTermMonths"
|
||||
:allowed-methods="paymentLimits?.methods"
|
||||
:max-check-months="paymentLimits?.maxCheckMonths"
|
||||
@update:settlement-type="emit('update:settlementType', $event)"
|
||||
@update:check-term-months="emit('update:checkTermMonths', $event)"
|
||||
/>
|
||||
|
||||
@@ -174,8 +174,17 @@ async function savePaymentPlan() {
|
||||
formError.value = 'روش پرداخت را انتخاب کنید.'
|
||||
return
|
||||
}
|
||||
if ((planType.value === 'check' || planType.value === 'cash_check') && !planMonths.value) {
|
||||
formError.value = 'مدت چک را از ۱ تا ۴ ماه انتخاب کنید.'
|
||||
const maxMonths = current.paymentLimits?.maxCheckMonths ?? 4
|
||||
const allowed = current.paymentLimits?.methods
|
||||
if (allowed && !allowed.includes(planType.value)) {
|
||||
formError.value = 'این روش پرداخت برای محصولات سفارش مجاز نیست.'
|
||||
return
|
||||
}
|
||||
if (
|
||||
(planType.value === 'check' || planType.value === 'cash_check') &&
|
||||
(!planMonths.value || planMonths.value > maxMonths)
|
||||
) {
|
||||
formError.value = `مدت چک را تا ${maxMonths} ماه انتخاب کنید.`
|
||||
return
|
||||
}
|
||||
planSaving.value = true
|
||||
@@ -352,6 +361,8 @@ async function submitReceipt() {
|
||||
<PreorderPaymentFields
|
||||
:settlement-type="planType"
|
||||
:check-term-months="planMonths"
|
||||
:allowed-methods="order.paymentLimits?.methods"
|
||||
:max-check-months="order.paymentLimits?.maxCheckMonths"
|
||||
@update:settlement-type="planType = $event"
|
||||
@update:check-term-months="planMonths = $event"
|
||||
/>
|
||||
@@ -391,6 +402,8 @@ async function submitReceipt() {
|
||||
<PreorderPaymentFields
|
||||
:settlement-type="planType"
|
||||
:check-term-months="planMonths"
|
||||
:allowed-methods="order.paymentLimits?.methods"
|
||||
:max-check-months="order.paymentLimits?.maxCheckMonths"
|
||||
@update:settlement-type="planType = $event"
|
||||
@update:check-term-months="planMonths = $event"
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user