diff --git a/src/components/orders/PreorderPaymentFields.vue b/src/components/orders/PreorderPaymentFields.vue new file mode 100644 index 0000000..500840c --- /dev/null +++ b/src/components/orders/PreorderPaymentFields.vue @@ -0,0 +1,127 @@ + + + + + diff --git a/src/composables/useCartPage.ts b/src/composables/useCartPage.ts index 65bbd51..6f40010 100644 --- a/src/composables/useCartPage.ts +++ b/src/composables/useCartPage.ts @@ -3,6 +3,7 @@ import { useRouter } from 'vue-router' import { useCartStore } from '@/stores/cart' import { useOrderStore } from '@/stores/order' import { confirm } from '@/composables/useConfirm' +import type { SettlementType } from '@/types' export function useCartPage() { const router = useRouter() @@ -11,6 +12,8 @@ export function useCartPage() { const showFullDialog = ref(false) const submitting = ref(false) const submitError = ref('') + const settlementType = ref(null) + const checkTermMonths = ref(null) const items = computed(() => cartStore.activeCart?.items ?? []) const isEmpty = computed(() => items.value.length === 0) @@ -33,11 +36,22 @@ export function useCartPage() { } /** - * ثبت سفارش بدون آدرس و بدون انتخاب تسویه. - * سفارش هنوز نهایی نیست؛ قیمت‌گذاری و روش تسویه بعد از بررسی مدیریت است. + * پیش‌سفارش با روش پرداخت (و مدت چک در صورت نیاز) ثبت می‌شود. + * قیمت‌گذاری مدیریت بعد از این انتخاب انجام می‌شود. */ async function submitOrder() { if (!cartStore.activeCart || items.value.length === 0 || submitting.value) return + if (!settlementType.value) { + submitError.value = 'قبل از ثبت پیش‌سفارش روش پرداخت را انتخاب کنید.' + return + } + if ( + (settlementType.value === 'check' || settlementType.value === 'cash_check') && + !checkTermMonths.value + ) { + submitError.value = 'مدت چک را از ۱ تا ۴ ماه انتخاب کنید.' + return + } submitting.value = true submitError.value = '' try { @@ -46,7 +60,10 @@ export function useCartPage() { submitError.value = 'سبد خرید خالی است یا با سرور همگام نشد.' return } - const order = await orderStore.submitOrder(cartStore.activeCart) + const order = await orderStore.submitOrder(cartStore.activeCart, { + settlementType: settlementType.value, + checkTermMonths: checkTermMonths.value, + }) await cartStore.hydrate() router.replace(`/orders/${order.id}/success`) } catch (e) { @@ -90,6 +107,8 @@ export function useCartPage() { showFullDialog, submitting, submitError, + settlementType, + checkTermMonths, removeItem, proceedToCheckout, proceedAnyway, diff --git a/src/stores/order.ts b/src/stores/order.ts index 39db6fd..aec472c 100644 --- a/src/stores/order.ts +++ b/src/stores/order.ts @@ -117,11 +117,13 @@ export const useOrderStore = defineStore('order', () => { } /** - * سفارش بدون روش تسویه ثبت می‌شود؛ مشتری بعد از قیمت‌گذاری مدیریت - * روش تسویه را انتخاب می‌کند. + * پیش‌سفارش همراه روش پرداخت ثبت می‌شود تا قیمت‌گذاری بر همان اساس انجام شود. * اقلام همه شرکت‌ها در یک فاکتور واحد ثبت می‌شوند. */ - async function submitOrder(cart: Cart): Promise { + async function submitOrder( + cart: Cart, + payment: { settlementType: SettlementType; checkTermMonths?: number | null }, + ): Promise { if (!cart.items.length) { throw new Error('سبد خرید خالی است.') } @@ -143,6 +145,9 @@ export const useOrderStore = defineStore('order', () => { method: 'POST', body: { companyId: companyIds[0], + settlementType: payment.settlementType, + checkTermMonths: + payment.settlementType === 'cash' ? undefined : payment.checkTermMonths ?? undefined, notes: notes || undefined, items: cart.items.map((item) => ({ productId: item.productId, @@ -159,7 +164,28 @@ export const useOrderStore = defineStore('order', () => { } } - /** انتخاب روش تسویه پس از صدور فاکتور. */ + async function setPaymentPlan( + orderId: string, + settlementType: SettlementType, + checkTermMonths?: number | null, + ): Promise { + try { + const order = await apiRequest(`/orders/${orderId}/payment-plan`, { + method: 'POST', + body: { + settlementType, + checkTermMonths: + settlementType === 'cash' ? undefined : checkTermMonths ?? undefined, + }, + }) + upsertOrder(order) + return order + } catch (e) { + throw new Error(e instanceof ApiError ? e.message : 'ثبت روش پرداخت ناموفق بود.') + } + } + + /** انتخاب روش تسویه پس از صدور فاکتور؛ فقط برای سفارش‌هایی که روش پرداخت ندارند. */ async function chooseSettlement( orderId: string, settlementType: SettlementType, @@ -273,6 +299,7 @@ export const useOrderStore = defineStore('order', () => { removeDraftCheck, getDraftRass, submitOrder, + setPaymentPlan, chooseSettlement, submitCashReceipt, updateOrderChecks, diff --git a/src/types/index.ts b/src/types/index.ts index b20b6ab..ce3c18c 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -187,8 +187,10 @@ export interface Order { companyName: string items: OrderItem[] totalMeterage: number - /** تا وقتی مشتری بعد از قیمت‌گذاری روش تسویه را انتخاب نکرده، خالی است. */ + /** هنگام ثبت پیش‌سفارش انتخاب می‌شود. سفارش‌های قدیمی یا ثبت‌شده توسط ادمین ممکن است خالی باشند. */ settlementType: SettlementType | null + /** مدت چک به ماه (۱ تا ۴) برای روش‌های چکی */ + checkTermMonths?: number | null checkMode?: CheckRegistrationMode checks?: CheckData[] checkRassDays?: number diff --git a/src/utils/orderTracking.ts b/src/utils/orderTracking.ts index 4d874da..c971ca6 100644 --- a/src/utils/orderTracking.ts +++ b/src/utils/orderTracking.ts @@ -48,13 +48,13 @@ const STATUS_HERO: Partial> = { variant: 'waiting', title: 'پیش‌سفارش ثبت شد', description: - 'سفارش شما برای بررسی و قیمت‌گذاری به مدیریت ارسال شد. پس از صدور فاکتور می‌توانید روش تسویه (نقد یا چک) را انتخاب کنید.', + 'سفارش شما برای بررسی و قیمت‌گذاری به مدیریت ارسال شد. روش پرداخت هنگام ثبت پیش‌سفارش مشخص شده است.', }, awaiting_review: { variant: 'waiting', title: 'در انتظار بررسی مدیریت', description: - 'پیش‌سفارش شما در صف بررسی است. پس از قیمت‌گذاری و صدور فاکتور، از طریق همین صفحه برای انتخاب تسویه مطلع می‌شوید.', + 'پیش‌سفارش شما در صف بررسی است. قیمت بر اساس روش پرداختی که انتخاب کرده‌اید اعلام می‌شود.', }, pricing: { variant: 'waiting', diff --git a/src/utils/settlement.ts b/src/utils/settlement.ts index 7eeac0f..d607d07 100644 --- a/src/utils/settlement.ts +++ b/src/utils/settlement.ts @@ -1,15 +1,15 @@ import type { OrderStatus, SettlementType } from '@/types' export const SETTLEMENT_TYPE_LABELS: Record = { - cash: 'تسویه نقدی', - check: 'تسویه چکی', - cash_check: 'تسویه نقدی-چکی', + cash: 'نقدی', + check: 'چکی', + cash_check: 'نصف نقد، نصف چکی', } export function settlementTypeLabel( type: SettlementType | null | undefined, ): string { - if (!type) return 'در انتظار انتخاب مشتری' + if (!type) return 'هنوز انتخاب نشده' return SETTLEMENT_TYPE_LABELS[type] ?? type } diff --git a/src/views/cart/CartView.vue b/src/views/cart/CartView.vue index 30e61b0..cdc3a32 100644 --- a/src/views/cart/CartView.vue +++ b/src/views/cart/CartView.vue @@ -18,6 +18,8 @@ const { showFullDialog, submitting, submitError, + settlementType, + checkTermMonths, removeItem, proceedToCheckout, proceedAnyway, @@ -43,6 +45,10 @@ function goToOrdering() { :remaining-capacity="cartStore.remainingCapacity" :is-empty="isEmpty" :submitting="submitting" + :settlement-type="settlementType" + :check-term-months="checkTermMonths" + @update:settlement-type="settlementType = $event" + @update:check-term-months="checkTermMonths = $event" @back="router.push('/')" @remove="removeItem" @checkout="proceedToCheckout" @@ -59,6 +65,10 @@ function goToOrdering() { :remaining-capacity="cartStore.remainingCapacity" :is-empty="isEmpty" :submitting="submitting" + :settlement-type="settlementType" + :check-term-months="checkTermMonths" + @update:settlement-type="settlementType = $event" + @update:check-term-months="checkTermMonths = $event" @remove="removeItem" @checkout="proceedToCheckout" @select-company="goToOrdering()" diff --git a/src/views/cart/components/DesktopCartPage.vue b/src/views/cart/components/DesktopCartPage.vue index fe758d4..8d2b186 100644 --- a/src/views/cart/components/DesktopCartPage.vue +++ b/src/views/cart/components/DesktopCartPage.vue @@ -10,7 +10,8 @@ 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 } from '@/types' +import type { CartItem, SettlementType } from '@/types' +import PreorderPaymentFields from '@/components/orders/PreorderPaymentFields.vue' defineProps<{ items: CartItem[] @@ -27,6 +28,8 @@ defineProps<{ remainingCapacity: number isEmpty: boolean submitting?: boolean + settlementType: SettlementType | null + checkTermMonths: number | null }>() const emit = defineEmits<{ @@ -34,6 +37,8 @@ const emit = defineEmits<{ checkout: [] selectCompany: [] back: [] + 'update:settlementType': [value: SettlementType] + 'update:checkTermMonths': [value: number] }>() const router = useRouter() @@ -180,6 +185,12 @@ const quickLinks = [ +