853 lines
28 KiB
Vue
853 lines
28 KiB
Vue
<template>
|
||
<div class="tui-page-layout tui-navbar" :class="{ 'in-pane': !!pane, 'wide-page': isWide && !pane }">
|
||
<t-navbar
|
||
fixed
|
||
:z-index="20"
|
||
title="直接产品下单"
|
||
left-arrow
|
||
@left-click="exit"
|
||
/>
|
||
<div v-can="permissionCodes.salesOrder.create" class="tui-page-content scroll sales-order-create">
|
||
<t-form class="form card" label-width="5.25rem" v-entrance-transition>
|
||
<t-form-item label="客户" required-mark>
|
||
<t-input
|
||
:model-value="customerDisplayText"
|
||
class="customer"
|
||
readonly
|
||
borderless
|
||
placeholder="选择或扫码客户"
|
||
@click="customerSelectorVisible = true"
|
||
>
|
||
<template #suffix-icon>
|
||
<div class="actions">
|
||
<t-button
|
||
theme="primary"
|
||
variant="outline"
|
||
size="extra-small"
|
||
@click.stop="customerSelectorVisible = true"
|
||
>选择</t-button>
|
||
<t-button theme="primary" size="extra-small" @click.stop="toggleScanner">
|
||
<span v-text="scannerVisible ? '关闭扫码' : '扫码'" />
|
||
</t-button>
|
||
</div>
|
||
</template>
|
||
</t-input>
|
||
</t-form-item>
|
||
<div v-if="scannerVisible" class="code-scanner-wrapper">
|
||
<code-scanner
|
||
visible
|
||
:status-text="scanStatus"
|
||
active-text="正在识别客户编码,请将条码或二维码移入取景框。"
|
||
@decode="handleDecodedText"
|
||
@status-change="handleScanStatusChange"
|
||
/>
|
||
</div>
|
||
<customer-profile-card
|
||
v-if="customer"
|
||
class="customer-profile"
|
||
:customer="customer"
|
||
v-entrance-transition
|
||
/>
|
||
<t-form-item label-width="0">
|
||
<div class="product-part">
|
||
<div class="header">
|
||
<strong>购买产品</strong>
|
||
<span v-text="`${items.length} 项`" />
|
||
</div>
|
||
<div class="tips">同一 SKU 单位不能重复</div>
|
||
<div class="items">
|
||
<div
|
||
class="item"
|
||
v-for="(item, index) in items"
|
||
:key="item.clientKey"
|
||
v-entrance-transition
|
||
>
|
||
<div class="header">
|
||
<span class="no" v-text="String(index + 1).padStart(2, '0')" />
|
||
<strong v-text="getItemTitle(item)" />
|
||
<t-button theme="danger" variant="text" size="extra-small" @click="removeItem(index)">删除</t-button>
|
||
</div>
|
||
<product-select-form-item
|
||
:model-value="item.skuId ? { skuId: item.skuId, skuUnitId: item.skuUnitId || '' } : undefined"
|
||
:selected-product="getProduct(item)"
|
||
label="产品"
|
||
placeholder="请选择产品"
|
||
required-mark
|
||
@update:model-value="applyProduct(index, $event)"
|
||
/>
|
||
<t-form-item v-if="canPriceOverride" label="成交单价">
|
||
<t-input
|
||
v-model.trim="item.unitPriceText"
|
||
type="number"
|
||
align="right"
|
||
borderless
|
||
:placeholder="getItemUnitPricePlaceholder(item)"
|
||
/>
|
||
<template #help>
|
||
<span
|
||
v-if="item.skuId && item.unitPriceText"
|
||
class="price-tips gray"
|
||
v-text="getItemUnitPricePlaceholder(item)"
|
||
/>
|
||
</template>
|
||
</t-form-item>
|
||
<t-form-item label="数量" content-align="right">
|
||
<t-stepper
|
||
v-model="item.quantity"
|
||
size="large"
|
||
:max="maxQuantity"
|
||
:min="getSku(item)?.allowDecimalQuantity ? .001 : 1"
|
||
:step="getQuantityStep(item)"
|
||
:integer="!getSku(item)?.allowDecimalQuantity"
|
||
theme="outline"
|
||
/>
|
||
</t-form-item>
|
||
<t-form-item label="产品备注">
|
||
<t-input
|
||
:model-value="item.remark ?? ''"
|
||
@update:model-value="value => item.remark = String(value).trim()"
|
||
borderless placeholder="选填"
|
||
/>
|
||
</t-form-item>
|
||
<div class="price">
|
||
<animated-amount
|
||
class="red text-bold"
|
||
prefix="¥"
|
||
empty-text="0.00"
|
||
:value="calculateItemAmount(item)"
|
||
/>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<t-button
|
||
class="add"
|
||
type="button"
|
||
theme="light"
|
||
variant="dashed"
|
||
block
|
||
@click="addItem"
|
||
>
|
||
<template #icon>
|
||
<t-icon name="add" size="1.125rem" />
|
||
</template>
|
||
<span>继续添加产品</span>
|
||
</t-button>
|
||
</div>
|
||
</t-form-item>
|
||
<t-form-item label="下单时间" content-align="right">
|
||
<date-time-now-picker v-model="purchasedAtText" title="下单时间" />
|
||
</t-form-item>
|
||
<t-form-item
|
||
v-if="canReceiveImmediately"
|
||
class="payment-item"
|
||
:class="{ disabled: receiveFullPayment }"
|
||
:label="receiveFullPayment ? '收款和核销金额' : '已部分收款和核销'"
|
||
label-width="9rem"
|
||
content-align="right"
|
||
>
|
||
<t-input
|
||
:model-value="receiveFullPayment ? formatProductAmount(totalAmount || 0) : paymentAmountText"
|
||
type="number"
|
||
align="right"
|
||
borderless
|
||
:disabled="receiveFullPayment"
|
||
:placeholder="paymentAmountPlaceholder"
|
||
@update:model-value="paymentAmountText = String($event)"
|
||
/>
|
||
</t-form-item>
|
||
<t-form-item v-if="canReceiveImmediately" label="已全额收款和核销" content-align="right">
|
||
<t-switch
|
||
v-model="receiveFullPayment"
|
||
size="small"
|
||
:disabled="!canReceiveImmediately"
|
||
/>
|
||
</t-form-item>
|
||
<t-form-item v-if="canFulfillImmediately" label="已全部产品履约和核销" content-align="right">
|
||
<t-switch
|
||
v-model="fulfillAll"
|
||
size="small"
|
||
:disabled="!canFulfillImmediately || isStockChecking"
|
||
/>
|
||
</t-form-item>
|
||
<t-form-item v-if="canFulfillImmediately" label-width="0">
|
||
<div class="stock-tips">
|
||
<div class="tips red">全部履约会同步启用全额收款;请先准备充足的库存,任意一步失败都会下单失败。</div>
|
||
<product-stock-preview v-if="stockPreviewList.length" :items="stockPreviewList" />
|
||
</div>
|
||
</t-form-item>
|
||
<t-form-item v-if="(paymentVerified && paymentAmount) || (receiveFullPayment && totalAmount)" label-width="0">
|
||
<attachment-selector
|
||
v-model:items="paymentPhotoItems"
|
||
:upload="uploadPhotoList"
|
||
title="收款凭证照片(必要)"
|
||
/>
|
||
</t-form-item>
|
||
<t-form-item v-if="fulfillAll" label-width="0">
|
||
<attachment-selector
|
||
v-model:items="fulfillmentPhotoItems"
|
||
:upload="uploadPhotoList"
|
||
title="履约凭证照片(必要)"
|
||
/>
|
||
</t-form-item>
|
||
<t-form-item label="订单备注">
|
||
<t-textarea v-model="remark" class="textarea" borderless autosize maxlength="500" placeholder="记录本单需要注意的内容" />
|
||
</t-form-item>
|
||
<div class="photo-part">
|
||
<attachment-selector v-model:items="photoItems" :upload="uploadPhotoList" title="订单照片(选填)" />
|
||
</div>
|
||
</t-form>
|
||
<div class="submit-wrap tui-fixed-inset glass-white bg-white">
|
||
<div class="total">
|
||
<span class="label">订单总价</span>
|
||
<div class="price">
|
||
<animated-amount
|
||
class="red text-bold"
|
||
prefix="¥"
|
||
empty-text="0.00"
|
||
:value="totalAmount"
|
||
/>
|
||
</div>
|
||
</div>
|
||
<t-button
|
||
class="submit"
|
||
theme="danger"
|
||
:loading="isSaving"
|
||
:disabled="isSaving"
|
||
@click="save"
|
||
>提交订单</t-button>
|
||
</div>
|
||
<customer-selector v-model="customerIdList" v-model:visible="customerSelectorVisible" selection-mode="single" :enable-scan="false" @change="loadCustomer" />
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import DateTimeNowPicker from "@/components/DateTimeNowPicker.vue";
|
||
import { type Attachment, useUserStore } from "timi-web-api";
|
||
import { AnimatedAmount } from "timi-web";
|
||
import { useLayout, usePane } from "timi-tdesign-mobile";
|
||
import { DialogPlugin } from "tdesign-mobile-vue";
|
||
import CustomerAPI from "@/api/CustomerAPI";
|
||
import ProductAPI from "@/api/ProductAPI";
|
||
import SalesOrderAPI from "@/api/SalesOrderAPI";
|
||
import CodeScanner from "@/components/CodeScanner.vue";
|
||
import CustomerProfileCard from "@/components/CustomerProfileCard.vue";
|
||
import CustomerSelector from "@/components/CustomerSelector.vue";
|
||
import AttachmentSelector from "@/components/attachmentSelector/AttachmentSelector.vue";
|
||
import ProductSelectFormItem from "@/components/productSelector/ProductSelectFormItem.vue";
|
||
import ProductStockPreview from "@/components/ProductStockPreview.vue";
|
||
import type { Customer } from "@/types/Customer";
|
||
import type { Product, ProductSku, ProductSkuUnit } from "@/types/Product";
|
||
import type { PhotoItem } from "@/types/Photo";
|
||
import type { DirectProductOrderItemRequest } from "@/types/SalesOrder";
|
||
import { createPackageRequestNo, normalizePackageQuantity, parsePackageAmount, toPackageBaseQuantity } from "@/utils/package";
|
||
import { formatProductAmount, getProductQuantityStep } from "@/utils/product";
|
||
import { getErrorMessage } from "@/utils/error";
|
||
import { emitListDataChanged, listDataEventKeys } from "@/utils/listDataEvents";
|
||
import { Message } from "@/utils/message";
|
||
import { permissionCodes } from "@/utils/permission";
|
||
import { buildProductStockPreview, getProductStockShortageMessage, mergeProductStockRequirements } from "@/utils/productStock";
|
||
import type { ProductStockPreviewItem } from "@/utils/productStock";
|
||
import { uploadPhotoList } from "@/utils/photo";
|
||
|
||
import { useSettingStore } from "@/store/settingStore";
|
||
|
||
type FormItem = Partial<DirectProductOrderItemRequest> & { clientKey: string; productName: string; unitPriceText: string };
|
||
defineOptions({ name: "SalesOrderCreate" });
|
||
const emit = defineEmits<{ created: [orderId: string] }>();
|
||
const router = useRouter();
|
||
const pane = usePane();
|
||
const { isWide } = useLayout();
|
||
function exit(): void {
|
||
if (pane) {
|
||
pane.close();
|
||
return;
|
||
}
|
||
void router.back();
|
||
}
|
||
const settingStore = useSettingStore();
|
||
const userStore = useUserStore();
|
||
const canPriceOverride = computed(() => userStore.hasPermissions(permissionCodes.salesOrder.priceOverride));
|
||
const customerIdList = ref<string[]>([]);
|
||
const customer = ref<Customer>();
|
||
const customerSelectorVisible = ref(false);
|
||
const items = ref<FormItem[]>([]);
|
||
const remark = ref("");
|
||
const purchasedAtText = ref("");
|
||
const isSaving = ref(false);
|
||
const photoItems = ref<PhotoItem[]>([]);
|
||
const paymentPhotoItems = ref<PhotoItem[]>([]);
|
||
const fulfillmentPhotoItems = ref<PhotoItem[]>([]);
|
||
const paymentAmountText = ref("0");
|
||
const receiveFullPayment = ref(false);
|
||
const fulfillAll = ref(false);
|
||
const isStockChecking = ref(false);
|
||
const stockPreviewList = ref<ProductStockPreviewItem[]>([]);
|
||
let stockCheckToken = 0;
|
||
const paymentAmount = computed(() => parsePackageAmount(paymentAmountText.value));
|
||
const paymentVerified = computed(() => !receiveFullPayment.value
|
||
&& 0 < paymentAmountText.value.trim().length
|
||
&& paymentAmount.value !== 0);
|
||
const maxQuantity = Number.MAX_SAFE_INTEGER;
|
||
const productCache = new Map<string, Product>();
|
||
const skuCache = new Map<string, ProductSku>();
|
||
const requestNo = ref("");
|
||
const customerDisplayText = computed(() => customer.value?.name || customer.value?.code || "选择客户");
|
||
const customerCode = ref("");
|
||
const scanCode = ref("");
|
||
const scannerVisible = ref(false);
|
||
const scanStatus = ref("点击扫码开始识别。");
|
||
let latestDecodedText = "";
|
||
|
||
// ---------- 交易直通 ----------
|
||
|
||
const canReceiveImmediately = computed(() => userStore.hasPermissions(permissionCodes.fundReceive.create)
|
||
&& userStore.hasPermissions(permissionCodes.fundReceive.verify));
|
||
const canFulfillImmediately = computed(() => userStore.hasPermissions(permissionCodes.orderFulfillment.create)
|
||
&& userStore.hasPermissions(permissionCodes.orderFulfillment.verify)
|
||
&& userStore.hasPermissions(permissionCodes.productStock.read)
|
||
&& (totalAmount.value === 0 || canReceiveImmediately.value));
|
||
|
||
watch(fulfillAll, async value => {
|
||
stockCheckToken += 1;
|
||
const token = stockCheckToken;
|
||
if (!value) {
|
||
isStockChecking.value = false;
|
||
return;
|
||
}
|
||
isStockChecking.value = true;
|
||
try {
|
||
if (!await validateDirectStock() && token === stockCheckToken && fulfillAll.value) {
|
||
fulfillAll.value = false;
|
||
return;
|
||
}
|
||
if (token === stockCheckToken && fulfillAll.value && totalAmount.value !== 0) {
|
||
receiveFullPayment.value = true;
|
||
}
|
||
} finally {
|
||
if (token === stockCheckToken) {
|
||
isStockChecking.value = false;
|
||
}
|
||
}
|
||
});
|
||
|
||
watch(receiveFullPayment, value => {
|
||
if (!value && totalAmount.value !== 0 && (!paymentVerified.value || paymentAmount.value !== totalAmount.value)) {
|
||
fulfillAll.value = false;
|
||
}
|
||
});
|
||
|
||
function getSku(item: FormItem): ProductSku | undefined {
|
||
return skuCache.get(item.skuId || "");
|
||
}
|
||
|
||
function getQuantityStep(item: FormItem): number {
|
||
return getProductQuantityStep(getSku(item));
|
||
}
|
||
|
||
function getProduct(item: FormItem): Product | undefined {
|
||
const sku = getSku(item);
|
||
return productCache.get(sku?.productId || "");
|
||
}
|
||
|
||
function getUnit(item: FormItem): ProductSkuUnit | undefined {
|
||
return getSku(item)?.unitList?.find(unit => unit.id === item.skuUnitId);
|
||
}
|
||
|
||
function getItemTitle(item: FormItem): string {
|
||
const product = getProduct(item);
|
||
const sku = getSku(item);
|
||
return sku?.specSummary || sku?.name || product?.name || item.productName || "未选择产品";
|
||
}
|
||
|
||
const totalAmount = computed(() => {
|
||
let amount = 0;
|
||
for (const item of items.value) {
|
||
const itemAmount = calculateItemAmount(item);
|
||
if (itemAmount === undefined) {
|
||
return undefined;
|
||
}
|
||
amount += itemAmount;
|
||
}
|
||
return amount;
|
||
});
|
||
const paymentAmountPlaceholder = computed(() => `订单总金额:${formatProductAmount(totalAmount.value || 0)}`);
|
||
|
||
function createItem(): FormItem {
|
||
return {
|
||
clientKey: window.crypto.randomUUID(),
|
||
quantity: "1",
|
||
productName: "",
|
||
unitPriceText: "",
|
||
remark: ""
|
||
};
|
||
}
|
||
|
||
function addItem(): void {
|
||
items.value.push(createItem());
|
||
}
|
||
|
||
function removeItem(index: number): void {
|
||
if (1 < items.value.length) {
|
||
items.value.splice(index, 1);
|
||
} else {
|
||
Message.warning("订单至少需要一项产品");
|
||
}
|
||
}
|
||
|
||
async function applyProduct(index: number, value?: { skuId: string; skuUnitId: string }): Promise<void> {
|
||
if (!value) return;
|
||
const sku = skuCache.get(value.skuId) || await ProductAPI.skuDetail(value.skuId);
|
||
skuCache.set(value.skuId, sku);
|
||
const product = productCache.get(sku.productId || "") || await ProductAPI.detail(sku.productId || "");
|
||
productCache.set(sku.productId || "", product);
|
||
const item = items.value[index];
|
||
const selectionChanged = item.skuId !== value.skuId || item.skuUnitId !== value.skuUnitId;
|
||
item.skuId = value.skuId;
|
||
item.skuUnitId = value.skuUnitId;
|
||
item.productName = product.name;
|
||
if (selectionChanged) {
|
||
item.unitPriceText = "";
|
||
}
|
||
}
|
||
function getItemUnitPrice(item: FormItem): number | undefined {
|
||
if (canPriceOverride.value && item.unitPriceText.trim()) {
|
||
return parsePackageAmount(item.unitPriceText);
|
||
}
|
||
const salePrice = getUnit(item)?.salePrice;
|
||
return salePrice === null ? undefined : salePrice;
|
||
}
|
||
function getItemUnitPricePlaceholder(item: FormItem): string {
|
||
const salePrice = getUnit(item)?.salePrice;
|
||
return salePrice === undefined || salePrice === null ? "产品定价" : `产品定价:${formatProductAmount(salePrice)}`;
|
||
}
|
||
function calculateItemAmount(item: FormItem): number | undefined {
|
||
const quantity = normalizePackageQuantity(item.quantity ?? "");
|
||
const unitPrice = getItemUnitPrice(item);
|
||
if (!quantity || quantity === "0" || unitPrice === undefined) {
|
||
return undefined;
|
||
}
|
||
const [integerPart, fractionPart = ""] = quantity.split(".");
|
||
const quantityScale = 10n ** BigInt(fractionPart.length);
|
||
const quantityValue = BigInt(`${integerPart}${fractionPart}`);
|
||
const amount = BigInt(unitPrice) * quantityValue;
|
||
if (amount % quantityScale !== 0n) {
|
||
return undefined;
|
||
}
|
||
const result = Number(amount / quantityScale);
|
||
return Number.isSafeInteger(result) ? result : undefined;
|
||
}
|
||
async function loadCustomer(value: string | string[]): Promise<void> {
|
||
const id = Array.isArray(value) ? value[0] : value;
|
||
customer.value = id ? await CustomerAPI.detail(id) : undefined;
|
||
customerCode.value = customer.value?.code || "";
|
||
scanCode.value = customerCode.value;
|
||
}
|
||
function toggleScanner(): void {
|
||
scannerVisible.value = !scannerVisible.value;
|
||
if (scannerVisible.value) {
|
||
latestDecodedText = "";
|
||
}
|
||
}
|
||
async function handleDecodedText(rawText: string): Promise<void> {
|
||
const normalizedText = rawText.trim();
|
||
if (!normalizedText || normalizedText === latestDecodedText) {
|
||
return;
|
||
}
|
||
|
||
latestDecodedText = normalizedText;
|
||
scanCode.value = normalizedText;
|
||
await settingStore.playScanSuccessSound();
|
||
await applyScanCode();
|
||
}
|
||
async function applyScanCode(): Promise<void> {
|
||
customerCode.value = scanCode.value.trim();
|
||
scanStatus.value = `已识别 ${customerCode.value},正在加载客户档案。`;
|
||
if (!customerCode.value) {
|
||
return;
|
||
}
|
||
|
||
const result = await CustomerAPI.findByCode(customerCode.value);
|
||
if (!result?.id) {
|
||
customer.value = undefined;
|
||
customerIdList.value = [];
|
||
scanStatus.value = `客户 ${customerCode.value} 不存在,请先建档。`;
|
||
return;
|
||
}
|
||
|
||
customer.value = result;
|
||
customerIdList.value = [result.id];
|
||
scanStatus.value = `已识别 ${result.code || customerCode.value},已自动选中 ${result.name || "未命名客户"}。`;
|
||
scannerVisible.value = false;
|
||
}
|
||
function handleScanStatusChange(value: string): void {
|
||
scanStatus.value = value;
|
||
}
|
||
function resolveTimestamp(value: string): number | undefined {
|
||
if (!value) return undefined;
|
||
const timestamp = new Date(value.replace(" ", "T")).getTime();
|
||
return Number.isFinite(timestamp) ? timestamp : undefined;
|
||
}
|
||
function buildAttachments(source: PhotoItem[] = photoItems.value): Partial<Attachment>[] {
|
||
return source.flatMap(item => item.tempFileId ? [{ tempFileId: item.tempFileId, attachType: "PHOTO" }] : []);
|
||
}
|
||
|
||
async function validateDirectStock(): Promise<boolean> {
|
||
stockPreviewList.value = [];
|
||
const requirementList = mergeProductStockRequirements(items.value.flatMap(item => {
|
||
const sku = getSku(item);
|
||
const unit = getUnit(item);
|
||
const quantity = normalizePackageQuantity(item.quantity ?? "");
|
||
const baseQuantity = quantity && unit ? toPackageBaseQuantity(quantity, unit.baseQuantityPerUnit) : undefined;
|
||
if (!item.skuId || !sku?.stockEnabled || !baseQuantity) {
|
||
return [];
|
||
}
|
||
return [{ skuId: item.skuId, baseQuantity, productName: getProduct(item)?.name || item.productName || "未命名产品", unitName: "基础单位", stockEnabled: sku.stockEnabled }];
|
||
}));
|
||
if (!requirementList.length) {
|
||
return true;
|
||
}
|
||
try {
|
||
const stockList = await ProductAPI.stockBatchDetail({ skuIdList: requirementList.map(item => item.skuId) });
|
||
stockPreviewList.value = buildProductStockPreview(stockList, requirementList);
|
||
const shortageMessage = getProductStockShortageMessage(stockList, requirementList);
|
||
if (shortageMessage) {
|
||
Message.warning(`${shortageMessage},不能直接履约`);
|
||
return false;
|
||
}
|
||
return true;
|
||
} catch (error) {
|
||
stockPreviewList.value = [];
|
||
Message.error(getErrorMessage(error, "库存查询失败,暂不能直接履约"));
|
||
return false;
|
||
}
|
||
}
|
||
function validate(): boolean {
|
||
if (!customer.value?.id) { Message.warning("请选择客户"); return false; }
|
||
if (!items.value.length) { Message.warning("请至少添加一项产品"); return false; }
|
||
const keys = new Set<string>();
|
||
for (const item of items.value) {
|
||
const quantity = normalizePackageQuantity(item.quantity ?? "");
|
||
const key = `${item.skuId}\u0000${item.skuUnitId}`;
|
||
if (!item.skuId || !item.skuUnitId || !quantity || quantity === "0") { Message.warning("SKU 单位和正数量不能为空"); return false; }
|
||
if (getSku(item) && !getSku(item)!.allowDecimalQuantity && quantity.includes(".")) { Message.warning("该 SKU 不允许小数数量"); return false; }
|
||
if (keys.has(key)) { Message.warning("产品单位不能重复"); return false; }
|
||
keys.add(key);
|
||
if (canPriceOverride.value && item.unitPriceText && parsePackageAmount(item.unitPriceText) === undefined) { Message.warning("成交单价必须是合法金额"); return false; }
|
||
}
|
||
if ((paymentVerified.value || receiveFullPayment.value) && !canReceiveImmediately.value) { Message.warning("当前账号没有收款登记和核销权限"); return false; }
|
||
if (paymentVerified.value && (paymentAmount.value === undefined || paymentAmount.value <= 0 || paymentAmount.value > (totalAmount.value || 0))) { Message.warning("已核销收款金额必须大于 0 且不能超过订单总价"); return false; }
|
||
if (fulfillAll.value && !canFulfillImmediately.value) { Message.warning("当前账号没有交易直通所需权限"); return false; }
|
||
if (fulfillAll.value && totalAmount.value !== 0 && !paymentVerified.value && !receiveFullPayment.value) { Message.warning("全部履约直通必须同时完成收款核销"); return false; }
|
||
if (fulfillAll.value && paymentVerified.value && paymentAmount.value !== totalAmount.value) { Message.warning("全部履约直通必须先完成全额收款核销"); return false; }
|
||
if ((paymentVerified.value || receiveFullPayment.value) && totalAmount.value && !buildAttachments(paymentPhotoItems.value).length) { Message.warning("收款核销必须上传收款凭证照片"); return false; }
|
||
if (fulfillAll.value && !buildAttachments(fulfillmentPhotoItems.value).length) { Message.warning("全部履约直通必须上传履约凭证照片"); return false; }
|
||
return true;
|
||
}
|
||
async function save(): Promise<void> {
|
||
if (isSaving.value) return;
|
||
if (isStockChecking.value) {
|
||
Message.warning("库存正在预检,请稍候再提交");
|
||
return;
|
||
}
|
||
if (!validate()) return;
|
||
if (paymentVerified.value || receiveFullPayment.value || fulfillAll.value) {
|
||
const dialog = DialogPlugin.confirm({
|
||
title: "确认直通完成交易",
|
||
content: fulfillAll.value
|
||
? "将一次性创建订单、完成收款核销,并完成全部履约和库存扣减。提交后如需更正,只能走撤销或冲正流程。"
|
||
: paymentVerified.value
|
||
? "将一次性创建订单并登记已核销收款。提交后如需更正,只能走资金冲正流程。"
|
||
: "将一次性创建订单并核销全额收款。提交后如需更正,只能走资金冲正流程。",
|
||
cancelBtn: "取消",
|
||
confirmBtn: "确认并提交",
|
||
onCancel() {
|
||
dialog.destroy();
|
||
},
|
||
async onConfirm() {
|
||
dialog.destroy();
|
||
await submitOrder();
|
||
}
|
||
});
|
||
return;
|
||
}
|
||
await submitOrder();
|
||
}
|
||
async function submitOrder(): Promise<void> {
|
||
if (!requestNo.value) requestNo.value = createPackageRequestNo();
|
||
isSaving.value = true;
|
||
try {
|
||
if (fulfillAll.value && !await validateDirectStock()) return;
|
||
const itemList = items.value.map((item, index) => ({
|
||
skuId: item.skuId!,
|
||
skuUnitId: item.skuUnitId!,
|
||
quantity: normalizePackageQuantity(item.quantity ?? "")!,
|
||
unitPrice: canPriceOverride.value && item.unitPriceText ? parsePackageAmount(item.unitPriceText) : undefined,
|
||
sort: index,
|
||
remark: item.remark?.trim() || null
|
||
}));
|
||
const result = await SalesOrderAPI.createDirectProduct({
|
||
requestNo: requestNo.value,
|
||
customerId: customer.value!.id!,
|
||
purchasedAt: resolveTimestamp(purchasedAtText.value),
|
||
itemList,
|
||
attachmentList: buildAttachments(),
|
||
straightThrough: paymentVerified.value || receiveFullPayment.value || fulfillAll.value ? {
|
||
paymentVerified: paymentVerified.value,
|
||
paymentAmount: paymentAmount.value,
|
||
receiveFullPayment: receiveFullPayment.value,
|
||
paymentAttachmentList: buildAttachments(paymentPhotoItems.value),
|
||
fulfillAll: fulfillAll.value,
|
||
fulfillmentAttachmentList: buildAttachments(fulfillmentPhotoItems.value)
|
||
} : undefined,
|
||
remark: remark.value.trim() || null
|
||
});
|
||
emitListDataChanged(listDataEventKeys.salesOrderChanged, result.id);
|
||
emitListDataChanged(listDataEventKeys.productStockChanged);
|
||
Message.success(fulfillAll.value ? "订单、收款及履约已完成" : paymentVerified.value ? "订单及已核销收款已完成" : receiveFullPayment.value ? "订单及全额收款已完成" : "交易订单已创建");
|
||
if (pane) {
|
||
if (result.id) {
|
||
emit("created", result.id);
|
||
} else {
|
||
pane.close(true);
|
||
}
|
||
return;
|
||
}
|
||
await router.replace(`/product/sales-orders/${result.id}`);
|
||
} catch (error) {
|
||
const message = getErrorMessage(error, "交易订单创建失败,重试会复用同一请求号");
|
||
Message.error(message);
|
||
}
|
||
finally { isSaving.value = false; }
|
||
}
|
||
onMounted(() => {
|
||
addItem();
|
||
});
|
||
</script>
|
||
|
||
<style scoped lang="less">
|
||
.tui-page-layout .sales-order-create {
|
||
gap: 1rem;
|
||
display: flex;
|
||
padding: calc(var(--tui-nav-height) + 1rem) 0 calc(6.5rem + var(--tui-safe-bottom, 0px));
|
||
flex-direction: column;
|
||
|
||
.form {
|
||
margin: 0 1rem;
|
||
padding: .5rem 0;
|
||
background: rgba(255, 255, 255, .92);
|
||
box-shadow: var(--td-shadow-1);
|
||
border-radius: 1.4rem;
|
||
|
||
.customer {
|
||
|
||
.actions {
|
||
gap: .5rem;
|
||
display: flex;
|
||
}
|
||
}
|
||
|
||
.code-scanner-wrapper,
|
||
.photo-part {
|
||
padding: 1rem;
|
||
}
|
||
|
||
.customer-profile {
|
||
margin: 1rem;
|
||
}
|
||
|
||
.payment-item.disabled {
|
||
color: var(--td-text-color-disabled);
|
||
|
||
:deep(.t-form__label) {
|
||
color: var(--td-text-color-disabled);
|
||
}
|
||
}
|
||
|
||
.tips {
|
||
font-size: .875rem;
|
||
}
|
||
|
||
.product-part {
|
||
width: 100%;
|
||
|
||
> .header {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
|
||
strong {
|
||
font-size: .9375rem;
|
||
}
|
||
|
||
span {
|
||
color: var(--td-brand-color);
|
||
font-size: .75rem;
|
||
}
|
||
}
|
||
|
||
.tips {
|
||
margin-top: .25rem;
|
||
color: var(--td-text-color-secondary);
|
||
font-size: .75rem;
|
||
line-height: 1.5;
|
||
}
|
||
|
||
.items {
|
||
gap: .75rem;
|
||
display: flex;
|
||
margin-top: .75rem;
|
||
flex-direction: column;
|
||
|
||
.item {
|
||
border: 1px dashed var(--td-border-level-1-color);
|
||
padding: .5rem;
|
||
border-radius: .875rem;
|
||
|
||
.header {
|
||
gap: .625rem;
|
||
display: flex;
|
||
align-items: center;
|
||
|
||
.no {
|
||
width: 1.75rem;
|
||
height: 1.75rem;
|
||
color: var(--td-brand-color);
|
||
flex: none;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
background: var(--td-brand-color-1);
|
||
border-radius: .625rem;
|
||
font-size: .6875rem;
|
||
font-weight: 700;
|
||
}
|
||
|
||
strong {
|
||
flex: 1;
|
||
min-width: 0;
|
||
overflow: hidden;
|
||
color: var(--td-text-color-primary);
|
||
font-size: .875rem;
|
||
white-space: nowrap;
|
||
text-overflow: ellipsis;
|
||
}
|
||
}
|
||
|
||
.price-tips {
|
||
flex: 1;
|
||
text-align: right;
|
||
}
|
||
|
||
.unit-list {
|
||
gap: .5rem;
|
||
width: 100%;
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
justify-content: flex-end;
|
||
}
|
||
|
||
.unit-empty {
|
||
color: var(--td-text-color-secondary);
|
||
font-size: .75rem;
|
||
}
|
||
}
|
||
}
|
||
|
||
.price {
|
||
display: flex;
|
||
font-size: 1.5rem;
|
||
align-items: baseline;
|
||
justify-content: end;
|
||
}
|
||
|
||
.add {
|
||
--td-button-border-radius: 1rem;
|
||
|
||
gap: .375rem;
|
||
color: var(--td-brand-color);
|
||
margin-top: .75rem;
|
||
}
|
||
}
|
||
|
||
.textarea {
|
||
width: 100%;
|
||
}
|
||
}
|
||
|
||
.submit-wrap {
|
||
--tui-fixed-gap: 1.5rem;
|
||
|
||
gap: 1rem;
|
||
border: 1px solid rgba(0, 0, 0, .05);
|
||
bottom: calc(var(--tui-safe-bottom, 0px) + 1rem);
|
||
z-index: 12;
|
||
display: flex;
|
||
padding: .5rem 1rem calc(.5rem + var(--tui-safe-bottom, 0px)) 1rem;
|
||
position: fixed;
|
||
align-items: center;
|
||
border-radius: 1.75rem;
|
||
backdrop-filter: blur(var(--tui-backdrop-blur, 10px));
|
||
justify-content: space-between;
|
||
|
||
.total {
|
||
flex: 1;
|
||
display: flex;
|
||
flex-direction: column;
|
||
|
||
.label {
|
||
color: var(--td-text-color-secondary);
|
||
font-size: .6875rem;
|
||
}
|
||
|
||
.price {
|
||
display: flex;
|
||
font-size: 1.5rem;
|
||
align-items: baseline;
|
||
}
|
||
}
|
||
|
||
.submit {
|
||
--td-button-border-radius: 1rem;
|
||
}
|
||
}
|
||
}
|
||
|
||
.tui-page-layout.in-pane .sales-order-create .submit-wrap {
|
||
left: 1.5rem !important;
|
||
position: absolute;
|
||
}
|
||
|
||
.tui-page-layout.wide-page .sales-order-create .product-part {
|
||
.items {
|
||
display: grid;
|
||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||
}
|
||
}
|
||
|
||
:global(html.theme-dark) {
|
||
.tui-page-layout .sales-order-create {
|
||
.form {
|
||
background: rgba(15, 23, 42, .88);
|
||
box-shadow: 0 .75rem 2rem rgba(2, 6, 23, .32);
|
||
|
||
.order-item {
|
||
background: rgba(15, 23, 42, .72);
|
||
border-color: rgba(148, 163, 184, .16);
|
||
}
|
||
|
||
:deep(.textarea .t-textarea__inner) {
|
||
color: #e2e8f0;
|
||
background: rgba(15, 23, 42, .72);
|
||
border-color: rgba(148, 163, 184, .16);
|
||
}
|
||
}
|
||
|
||
.submit-bar {
|
||
box-shadow: 0 -.25rem 1rem rgba(0, 0, 0, .2);
|
||
background: rgba(15, 23, 42, .92);
|
||
}
|
||
}
|
||
}
|
||
</style>
|