495 lines
15 KiB
Vue
495 lines
15 KiB
Vue
<template>
|
||
<div class="page-layout navbar quick-fulfillment">
|
||
<t-navbar
|
||
fixed
|
||
:z-index="20"
|
||
title="快速履约"
|
||
left-arrow
|
||
@left-click="handleBack"
|
||
>
|
||
<template #right>
|
||
<button class="nav-action" type="button" :disabled="isSaving || isStockChecking || sourceOrderLoading" @click="save">
|
||
<span v-text="isSaving ? '提交中' : '提交'" />
|
||
</button>
|
||
</template>
|
||
</t-navbar>
|
||
<main v-can="permissionCodes.orderFulfillment.create" class="page-content scroll column content">
|
||
<customer-profile-card
|
||
v-if="selectedCustomer"
|
||
class="customer-profile"
|
||
:customer="selectedCustomer"
|
||
v-entrance-transition
|
||
/>
|
||
<t-cell-group theme="card" v-entrance-transition>
|
||
<t-cell title="履约客户" arrow @click="customerSelectorVisible = true">
|
||
<template #note>
|
||
<span v-text="selectedCustomer?.name || '选择'" />
|
||
</template>
|
||
</t-cell>
|
||
</t-cell-group>
|
||
<t-cell-group title="产品选择" theme="card" v-entrance-transition>
|
||
<t-cell v-if="customerId" title="选择产品 SKU" arrow @click="productSelectorVisible = true" />
|
||
<t-cell v-else title="选择产品 SKU" note="请先选择履约客户" />
|
||
</t-cell-group>
|
||
<div v-entrance-transition>
|
||
<order-fulfillment-quick-quantity
|
||
ref="fulfillmentQuantityRef"
|
||
:sku-list="selectedSkuList"
|
||
:is-stock-checking="isStockChecking"
|
||
@change="handleFulfillmentChange"
|
||
/>
|
||
</div>
|
||
<div v-entrance-transition>
|
||
<order-fulfillment-quick-source-orders
|
||
ref="sourceOrdersRef"
|
||
:customer-id="customerId"
|
||
:fulfillment-item-list="fulfillmentItemList"
|
||
@loading-change="sourceOrderLoading = $event"
|
||
/>
|
||
</div>
|
||
<t-cell-group title="履约信息" theme="card" v-entrance-transition>
|
||
<t-cell title="履约时间" content-align="right">
|
||
<template #note>
|
||
<div class="time-actions" @click.stop>
|
||
<t-button
|
||
theme="primary"
|
||
variant="outline"
|
||
size="extra-small"
|
||
:content="deliveredAtText || '此刻'"
|
||
@click="openDeliveredAtPicker"
|
||
/>
|
||
<t-button
|
||
v-if="deliveredAtText"
|
||
theme="primary"
|
||
variant="text"
|
||
size="extra-small"
|
||
content="此刻"
|
||
@click="resetDeliveredAt"
|
||
/>
|
||
</div>
|
||
</template>
|
||
</t-cell>
|
||
<t-cell v-if="canDirectVerify" title="直接核销履约" content-align="right">
|
||
<template #note>
|
||
<t-switch v-model="directVerify" size="small" :disabled="isSaving || isStockChecking" />
|
||
</template>
|
||
</t-cell>
|
||
<div v-if="directVerify || stockPreviewList.length" class="stock-wrap bg-white">
|
||
<center-tips>直接核销会立即扣减库存,缺货时整批提交失败</center-tips>
|
||
<product-stock-preview v-if="stockPreviewList.length" :items="stockPreviewList" />
|
||
<center-tips v-else-if="!isStockChecking" class="stock-hint">所选 SKU 无需扣减库存</center-tips>
|
||
<center-tips v-else class="stock-hint">正在检查库存…</center-tips>
|
||
</div>
|
||
<t-cell class="remark" title="备注">
|
||
<template #note>
|
||
<t-input v-model="remark" borderless placeholder="选填" />
|
||
</template>
|
||
</t-cell>
|
||
<div class="photo-wrap bg-white">
|
||
<attachment-selector
|
||
v-model:items="photoItems"
|
||
:upload="uploadPhotoList"
|
||
required
|
||
title="履约凭证照片"
|
||
/>
|
||
</div>
|
||
</t-cell-group>
|
||
</main>
|
||
<customer-selector
|
||
v-model="customerId"
|
||
v-model:visible="customerSelectorVisible"
|
||
selection-mode="single"
|
||
@change="handleCustomerChange"
|
||
/>
|
||
<product-select-popup
|
||
v-model:visible="productSelectorVisible"
|
||
selection-mode="multiple"
|
||
:selectable-product-id-list="selectableProductIdList"
|
||
:selectable-sku-id-list="selectableSkuIdList"
|
||
:selected-sku-id-list="selectedSkuIdList"
|
||
@confirm="handleProductSkuSelectionChange"
|
||
/>
|
||
<t-popup v-model="timePickerVisible" placement="bottom">
|
||
<t-date-time-picker
|
||
v-if="timePickerVisible"
|
||
v-model="draftTime"
|
||
format="YYYY-MM-DD HH:mm"
|
||
:mode="['date', 'minute']"
|
||
title="履约时间"
|
||
@cancel="timePickerVisible = false"
|
||
@confirm="confirmTime"
|
||
/>
|
||
</t-popup>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { Time } from "timi-web";
|
||
import type { Attachment } from "timi-web-api";
|
||
import { useUserStore } from "timi-web-api";
|
||
import CustomerAPI from "@/api/CustomerAPI";
|
||
import FulfillmentAPI from "@/api/FulfillmentAPI";
|
||
import ProductAPI from "@/api/ProductAPI";
|
||
import CustomerProfileCard from "@/components/CustomerProfileCard.vue";
|
||
import CustomerSelector from "@/components/CustomerSelector.vue";
|
||
import AttachmentSelector from "@/components/attachmentSelector/AttachmentSelector.vue";
|
||
import ProductSelectPopup from "@/components/productSelector/ProductSelectPopup.vue";
|
||
import ProductStockPreview from "@/components/ProductStockPreview.vue";
|
||
import OrderFulfillmentQuickQuantity from "@/pages/fulfillment/components/OrderFulfillmentQuickQuantity.vue";
|
||
import OrderFulfillmentQuickSourceOrders from "@/pages/fulfillment/components/OrderFulfillmentQuickSourceOrders.vue";
|
||
import type {
|
||
QuickFulfillmentItem,
|
||
QuickFulfillmentQuantityExpose,
|
||
QuickFulfillmentSourceOrdersExpose
|
||
} from "@/pages/fulfillment/components/OrderFulfillmentQuickTypes";
|
||
import type { Customer } from "@/types/Customer";
|
||
import type { QuickFulfillmentSku } from "@/types/Fulfillment";
|
||
import type { PhotoItem } from "@/types/Photo";
|
||
import { comparePackageQuantity, createPackageRequestNo } from "@/utils/package";
|
||
import { getErrorMessage } from "@/utils/error";
|
||
import { emitListDataChanged, listDataEventKeys } from "@/utils/listDataEvents";
|
||
import { Message } from "@/utils/message";
|
||
import { gaoRoleCodes, permissionCodes } from "@/utils/permission";
|
||
import type { ProductStockPreviewItem } from "@/utils/productStock";
|
||
import {
|
||
buildProductStockPreview,
|
||
getProductStockShortageMessage
|
||
} from "@/utils/productStock";
|
||
import { uploadPhotoList } from "@/utils/photo";
|
||
|
||
defineOptions({ name: "OrderFulfillmentQuick" });
|
||
|
||
const route = useRoute();
|
||
const router = useRouter();
|
||
const userStore = useUserStore();
|
||
const customerId = ref("");
|
||
const selectedCustomer = ref<Customer>();
|
||
const customerSelectorVisible = ref(false);
|
||
const skuList = ref<QuickFulfillmentSku[]>([]);
|
||
const selectedSkuIdList = ref<string[]>([]);
|
||
const productSelectorVisible = ref(false);
|
||
const fulfillmentItemList = ref<QuickFulfillmentItem[]>([]);
|
||
const fulfillmentQuantityRef = ref<QuickFulfillmentQuantityExpose>();
|
||
const sourceOrdersRef = ref<QuickFulfillmentSourceOrdersExpose>();
|
||
const loadingSkuList = ref(false);
|
||
const sourceOrderLoading = ref(false);
|
||
const isSaving = ref(false);
|
||
const isStockChecking = ref(false);
|
||
const directVerify = ref(false);
|
||
const stockPreviewList = ref<ProductStockPreviewItem[]>([]);
|
||
const deliveredAtText = ref("");
|
||
const timePickerVisible = ref(false);
|
||
const draftTime = ref("");
|
||
const remark = ref("");
|
||
const photoItems = ref<PhotoItem[]>([]);
|
||
const requestNo = ref("");
|
||
let stockCheckToken = 0;
|
||
let stockPreviewTimer: number | undefined;
|
||
|
||
const selectedSkuList = computed(() => selectedSkuIdList.value
|
||
.map(id => skuList.value.find(sku => sku.skuId === id))
|
||
.filter((sku): sku is QuickFulfillmentSku => !!sku));
|
||
const selectableQuickSkuList = computed(() => skuList.value.filter(canSelectSku));
|
||
const selectableSkuIdList = computed(() => selectableQuickSkuList.value.map(sku => sku.skuId));
|
||
const selectableProductIdList = computed(() => [...new Set(selectableQuickSkuList.value
|
||
.flatMap(sku => sku.productId ? [sku.productId] : []))]);
|
||
const canDirectVerify = computed(() => userStore.hasRoles(gaoRoleCodes.storeManager)
|
||
&& userStore.hasPermissions(permissionCodes.orderFulfillment.create)
|
||
&& userStore.hasPermissions(permissionCodes.orderFulfillment.verify)
|
||
&& userStore.hasPermissions(permissionCodes.productStock.read));
|
||
|
||
// ---------- SKU 选择 ----------
|
||
|
||
function canSelectSku(sku: QuickFulfillmentSku): boolean {
|
||
return !!sku.skuId
|
||
&& 0 < comparePackageQuantity(sku.availableBaseQuantity, "0")
|
||
&& sku.unitList.some(unit => !!unit.id);
|
||
}
|
||
|
||
function handleProductSkuSelectionChange(value: string[]): void {
|
||
const availableSkuMap = new Map(skuList.value
|
||
.filter(canSelectSku)
|
||
.map(sku => [sku.skuId, sku]));
|
||
const currentSkuIdSet = new Set(selectedSkuIdList.value);
|
||
const nextSkuIdList = [...new Set(value)].filter(id => availableSkuMap.has(id));
|
||
const rejectedCount = value.filter(id => !availableSkuMap.has(id) && !currentSkuIdSet.has(id)).length;
|
||
|
||
selectedSkuIdList.value = nextSkuIdList;
|
||
if (rejectedCount) {
|
||
Message.warning("部分 SKU 不在当前客户的可履约订单中,已忽略");
|
||
}
|
||
}
|
||
|
||
// ---------- 客户选择 ----------
|
||
|
||
async function handleCustomerChange(id: string): Promise<void> {
|
||
if (!id || id === customerId.value && selectedCustomer.value?.id === id) {
|
||
return;
|
||
}
|
||
customerId.value = id;
|
||
selectedCustomer.value = undefined;
|
||
productSelectorVisible.value = false;
|
||
skuList.value = [];
|
||
selectedSkuIdList.value = [];
|
||
stockPreviewList.value = [];
|
||
try {
|
||
const [customer] = await Promise.all([
|
||
CustomerAPI.detail(id),
|
||
loadSkuList(id)
|
||
]);
|
||
selectedCustomer.value = customer;
|
||
} catch (error) {
|
||
Message.error(getErrorMessage(error, "客户信息加载失败"));
|
||
}
|
||
}
|
||
|
||
async function loadSkuList(id: string): Promise<void> {
|
||
loadingSkuList.value = true;
|
||
try {
|
||
skuList.value = await FulfillmentAPI.quickSkuList(id);
|
||
} finally {
|
||
loadingSkuList.value = false;
|
||
}
|
||
}
|
||
|
||
// ---------- 履约数量状态 ----------
|
||
|
||
function handleFulfillmentChange(itemList: QuickFulfillmentItem[]): void {
|
||
fulfillmentItemList.value = itemList;
|
||
scheduleStockPreview();
|
||
}
|
||
|
||
// ---------- 来源订单状态由子组件管理 ----------
|
||
|
||
function openDeliveredAtPicker(): void {
|
||
draftTime.value = deliveredAtText.value || Time.toDateTime(new Date());
|
||
timePickerVisible.value = true;
|
||
}
|
||
|
||
function confirmTime(value: string | number): void {
|
||
deliveredAtText.value = String(value);
|
||
timePickerVisible.value = false;
|
||
}
|
||
|
||
function resetDeliveredAt(): void {
|
||
deliveredAtText.value = "";
|
||
draftTime.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(): Partial<Attachment>[] {
|
||
return photoItems.value.flatMap(item => item.tempFileId ? [{ tempFileId: item.tempFileId, attachType: "PHOTO" }] : []);
|
||
}
|
||
|
||
// ---------- 库存预检 ----------
|
||
|
||
async function validateDirectStock(showShortageMessage = true): Promise<boolean> {
|
||
stockPreviewList.value = [];
|
||
const requirementList = fulfillmentQuantityRef.value?.buildStockRequirements() || [];
|
||
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) {
|
||
if (showShortageMessage) {
|
||
Message.warning(`${shortageMessage},不能直接核销履约`);
|
||
}
|
||
return false;
|
||
}
|
||
return true;
|
||
} catch (error) {
|
||
stockPreviewList.value = [];
|
||
Message.error(getErrorMessage(error, "库存查询失败,暂不能直接核销履约"));
|
||
return false;
|
||
}
|
||
}
|
||
|
||
async function refreshStockPreview(): Promise<void> {
|
||
if (!directVerify.value || isStockChecking.value) {
|
||
return;
|
||
}
|
||
stockCheckToken += 1;
|
||
const token = stockCheckToken;
|
||
isStockChecking.value = true;
|
||
try {
|
||
if (!await validateDirectStock(true) && token === stockCheckToken && directVerify.value) {
|
||
directVerify.value = false;
|
||
}
|
||
} finally {
|
||
if (token === stockCheckToken) {
|
||
isStockChecking.value = false;
|
||
}
|
||
}
|
||
}
|
||
|
||
function scheduleStockPreview(): void {
|
||
if (!directVerify.value) {
|
||
return;
|
||
}
|
||
window.clearTimeout(stockPreviewTimer);
|
||
stockPreviewTimer = window.setTimeout(() => {
|
||
void refreshStockPreview();
|
||
}, 250);
|
||
}
|
||
|
||
watch(directVerify, async value => {
|
||
stockCheckToken += 1;
|
||
const token = stockCheckToken;
|
||
if (!value) {
|
||
isStockChecking.value = false;
|
||
return;
|
||
}
|
||
isStockChecking.value = true;
|
||
try {
|
||
if (!await validateDirectStock() && token === stockCheckToken && directVerify.value) {
|
||
directVerify.value = false;
|
||
}
|
||
} finally {
|
||
if (token === stockCheckToken) {
|
||
isStockChecking.value = false;
|
||
}
|
||
}
|
||
});
|
||
|
||
function handleBack(): void {
|
||
void router.back();
|
||
}
|
||
|
||
// ---------- 提交履约 ----------
|
||
|
||
async function save(): Promise<void> {
|
||
if (isSaving.value || isStockChecking.value || sourceOrderLoading.value) {
|
||
return;
|
||
}
|
||
if (!customerId.value) {
|
||
Message.warning("请先选择客户");
|
||
return;
|
||
}
|
||
const quantityComponent = fulfillmentQuantityRef.value;
|
||
const sourceOrdersComponent = sourceOrdersRef.value;
|
||
if (!quantityComponent || !sourceOrdersComponent) {
|
||
return;
|
||
}
|
||
if (!quantityComponent.validate() || !sourceOrdersComponent.validate()) {
|
||
return;
|
||
}
|
||
if (!buildAttachments().length) {
|
||
Message.warning("履约必须上传至少一张照片");
|
||
return;
|
||
}
|
||
if (!requestNo.value) {
|
||
requestNo.value = createPackageRequestNo();
|
||
}
|
||
isSaving.value = true;
|
||
try {
|
||
if (directVerify.value) {
|
||
isStockChecking.value = true;
|
||
const hasEnoughStock = await validateDirectStock(true);
|
||
isStockChecking.value = false;
|
||
if (!hasEnoughStock) {
|
||
directVerify.value = false;
|
||
return;
|
||
}
|
||
}
|
||
const sourceOrderIdList = sourceOrdersComponent.getSelectedOrderIdList();
|
||
const request = {
|
||
requestNo: requestNo.value,
|
||
customerId: customerId.value,
|
||
sourceOrderIdList,
|
||
itemList: quantityComponent.getRequestItemList(),
|
||
directVerify: directVerify.value,
|
||
deliveredAt: resolveTimestamp(deliveredAtText.value),
|
||
attachmentList: buildAttachments(),
|
||
remark: remark.value.trim() || null
|
||
};
|
||
const resultList = await FulfillmentAPI.quickCreate(request);
|
||
for (const orderId of sourceOrderIdList) {
|
||
emitListDataChanged(listDataEventKeys.salesOrderChanged, orderId);
|
||
}
|
||
emitListDataChanged(listDataEventKeys.productStockChanged);
|
||
Message.success(directVerify.value
|
||
? `快速履约并核销成功,共生成 ${resultList.length} 张履约单`
|
||
: `快速履约提交成功,共生成 ${resultList.length} 张履约单`);
|
||
await router.replace({ name: "OrderFulfillmentList", query: { customerId: customerId.value } });
|
||
} catch (error) {
|
||
Message.error(getErrorMessage(error, "快速履约提交失败,重试会复用同一请求号"));
|
||
} finally {
|
||
isSaving.value = false;
|
||
isStockChecking.value = false;
|
||
}
|
||
}
|
||
|
||
onMounted(async () => {
|
||
const queryCustomerId = Array.isArray(route.query.customerId) ? route.query.customerId[0] : route.query.customerId;
|
||
if (!queryCustomerId) {
|
||
return;
|
||
}
|
||
customerId.value = String(queryCustomerId);
|
||
try {
|
||
const customer = await CustomerAPI.detail(customerId.value);
|
||
selectedCustomer.value = customer;
|
||
await loadSkuList(customerId.value);
|
||
} catch (error) {
|
||
Message.error(getErrorMessage(error, "客户可履约 SKU 加载失败"));
|
||
}
|
||
});
|
||
|
||
onUnmounted(() => {
|
||
window.clearTimeout(stockPreviewTimer);
|
||
});
|
||
</script>
|
||
|
||
<style scoped lang="less">
|
||
.quick-fulfillment {
|
||
.content {
|
||
padding: calc(var(--app-nav-height) + .75rem) 0 2rem;
|
||
}
|
||
|
||
.nav-action {
|
||
color: var(--td-brand-color, #0052d9);
|
||
border: 0;
|
||
padding: .25rem .5rem;
|
||
font-size: .95rem;
|
||
background: transparent;
|
||
font-weight: 600;
|
||
|
||
&:disabled {
|
||
color: var(--td-text-color-disabled, #bbb);
|
||
}
|
||
}
|
||
|
||
.customer-profile {
|
||
margin: .75rem 1rem;
|
||
}
|
||
|
||
.time-actions {
|
||
gap: .5rem;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: flex-end;
|
||
}
|
||
|
||
.stock-wrap {
|
||
padding: .5rem 1rem;
|
||
}
|
||
|
||
.remark {
|
||
--td-input-vertical-padding: 0;
|
||
}
|
||
|
||
.photo-wrap {
|
||
padding: 1rem;
|
||
}
|
||
}
|
||
</style>
|