<script lang="ts" setup>
|
import type { MesWmSalesNoticeLineApi } from '#/api/mes/wm/salesnotice/line';
|
|
import { computed, ref } from 'vue';
|
|
import { useVbenModal } from '@vben/common-ui';
|
|
import { message } from 'ant-design-vue';
|
|
import { useVbenForm } from '#/adapter/form';
|
import {
|
createSalesNoticeLine,
|
getSalesNoticeLine,
|
updateSalesNoticeLine,
|
} from '#/api/mes/wm/salesnotice/line';
|
import { $t } from '#/locales';
|
|
import {
|
loadAvailableQuantity,
|
loadQualifiedStockBatchOptions,
|
loadQualifiedStockItemOptions,
|
useLineFormSchema,
|
} from '../data';
|
|
const emit = defineEmits(['success']);
|
const formData = ref<MesWmSalesNoticeLineApi.SalesNoticeLine>();
|
const noticeId = ref<number>(); // 所属通知单编号
|
|
const getTitle = computed(() => {
|
return formData.value?.id
|
? $t('ui.actionTitle.edit', ['发货通知单行'])
|
: $t('ui.actionTitle.create', ['发货通知单行']);
|
});
|
|
async function updateBatchOptions(itemId?: number) {
|
const batchOptions = itemId
|
? await loadQualifiedStockBatchOptions(itemId)
|
: [];
|
formApi.updateSchema([
|
{
|
fieldName: 'batchId',
|
componentProps: { options: batchOptions },
|
},
|
]);
|
}
|
|
async function refreshAvailableQuantity(itemId?: number, batchId?: number) {
|
const quantity = await loadAvailableQuantity(itemId, batchId);
|
await formApi.setFieldValue(
|
'availableQuantity',
|
quantity > 0 ? `${quantity}` : undefined,
|
);
|
}
|
|
const [Form, formApi] = useVbenForm({
|
commonConfig: {
|
componentProps: {
|
class: 'w-full',
|
},
|
formItemClass: 'col-span-1',
|
labelWidth: 90,
|
},
|
layout: 'horizontal',
|
schema: useLineFormSchema(),
|
showDefaultActions: false,
|
handleValuesChange: async (values, fieldsChanged) => {
|
// setValues 编辑回填时 itemId/batchId 同时变化,不清批次仅刷新选项
|
const itemChanged = fieldsChanged.includes('itemId');
|
const batchChanged = fieldsChanged.includes('batchId');
|
if (itemChanged && batchChanged) {
|
await updateBatchOptions(values.itemId as number | undefined);
|
await refreshAvailableQuantity(
|
values.itemId as number | undefined,
|
values.batchId as number | undefined,
|
);
|
return;
|
}
|
if (itemChanged) {
|
await formApi.setFieldValue('batchId', undefined);
|
await updateBatchOptions(values.itemId as number | undefined);
|
await refreshAvailableQuantity(values.itemId as number | undefined);
|
return;
|
}
|
if (batchChanged) {
|
await updateBatchOptions(values.itemId as number | undefined);
|
await refreshAvailableQuantity(
|
values.itemId as number | undefined,
|
values.batchId as number | undefined,
|
);
|
}
|
},
|
wrapperClass: 'grid-cols-3',
|
});
|
|
const [Modal, modalApi] = useVbenModal({
|
async onConfirm() {
|
const { valid } = await formApi.validate();
|
if (!valid) {
|
return;
|
}
|
modalApi.lock();
|
// 提交表单(可发货数量仅作提示,不提交到后端)
|
const values = (await formApi.getValues()) as MesWmSalesNoticeLineApi.SalesNoticeLine & {
|
availableQuantity?: unknown;
|
};
|
delete values.availableQuantity;
|
const data = values;
|
data.noticeId = noticeId.value;
|
try {
|
await (formData.value?.id
|
? updateSalesNoticeLine({ ...data, id: formData.value.id })
|
: createSalesNoticeLine(data));
|
// 关闭并提示
|
await modalApi.close();
|
emit('success');
|
message.success($t('ui.actionMessage.operationSuccess'));
|
} catch (error) {
|
const response = error as {
|
response?: { data?: { message?: string; msg?: string } };
|
};
|
message.error(
|
response.response?.data?.message ??
|
response.response?.data?.msg ??
|
'保存失败,请检查正式库库存是否充足',
|
);
|
} finally {
|
modalApi.unlock();
|
}
|
},
|
async onOpenChange(isOpen: boolean) {
|
if (!isOpen) {
|
formData.value = undefined;
|
return;
|
}
|
// 加载数据
|
const data = modalApi.getData<{ id?: number; noticeId: number }>();
|
noticeId.value = data.noticeId;
|
modalApi.lock();
|
try {
|
// 物料选项:仅展示正式库有库存的物料
|
const itemOptions = await loadQualifiedStockItemOptions();
|
formApi.updateSchema([
|
{
|
fieldName: 'itemId',
|
componentProps: { options: itemOptions },
|
},
|
]);
|
if (!data.id) {
|
return;
|
}
|
formData.value = await getSalesNoticeLine(data.id);
|
// 设置到 values,联动加载批次选项
|
await formApi.setValues(formData.value);
|
await updateBatchOptions(formData.value?.itemId);
|
await refreshAvailableQuantity(
|
formData.value?.itemId,
|
formData.value?.batchId,
|
);
|
} finally {
|
modalApi.unlock();
|
}
|
},
|
});
|
</script>
|
|
<template>
|
<Modal :title="getTitle" class="w-3/5">
|
<Form class="mx-4" />
|
</Modal>
|
</template>
|