<script lang="ts" setup>
|
import type { CrmSaleQuotationApi } from '#/api/crm/saleQuotation';
|
|
import { computed, ref, watch } from 'vue';
|
|
import { useVbenModal } from '#/packages/effects/common-ui/src';
|
import { erpPriceMultiply } from '#/packages/utils/src';
|
|
import { message } from 'ant-design-vue';
|
|
import { useVbenForm } from '#/adapter/form';
|
import {
|
createSaleQuotation,
|
getSaleQuotation,
|
updateSaleQuotation,
|
} from '#/api/crm/saleQuotation';
|
import { $t } from '#/locales';
|
|
import ItemForm from './item-form.vue';
|
import { useFormSchema } from '../data';
|
|
const emit = defineEmits(['success']);
|
const formData = ref<CrmSaleQuotationApi.SaleQuotation>();
|
const itemFormRef = ref();
|
|
const getTitle = computed(() => {
|
return formData.value?.id
|
? $t('ui.actionTitle.edit', ['报价单'])
|
: $t('ui.actionTitle.create', ['报价单']);
|
});
|
|
const isEdit = computed(() => !!formData.value?.id);
|
const isDisabled = computed(() => isEdit.value && formData.value?.status !== 0);
|
|
const [Form, formApi] = useVbenForm({
|
commonConfig: {
|
componentProps: {
|
class: 'w-full',
|
},
|
labelWidth: 120,
|
},
|
wrapperClass: 'grid-cols-3',
|
layout: 'vertical',
|
schema: useFormSchema(),
|
showDefaultActions: false,
|
});
|
|
/** 计算物料总金额 */
|
async function calculateTotalPrice() {
|
const items = itemFormRef.value?.getData() || [];
|
const totalProductPrice = items.reduce(
|
(prev: number, curr: any) => prev + (curr.totalPrice || 0),
|
0,
|
);
|
await formApi.setFieldValue('totalProductPrice', totalProductPrice);
|
const discountPercent = (await formApi.getValues())?.discountPercent || 0;
|
const discountPrice = erpPriceMultiply(totalProductPrice, discountPercent / 100) ?? 0;
|
await formApi.setFieldValue('totalPrice', totalProductPrice - discountPrice);
|
}
|
|
const [Modal, modalApi] = useVbenModal({
|
async onConfirm() {
|
const { valid } = await formApi.validate();
|
if (!valid) {
|
return;
|
}
|
try {
|
await itemFormRef.value?.validate();
|
} catch (e: any) {
|
message.error(e.message);
|
return;
|
}
|
modalApi.lock();
|
const data = (await formApi.getValues()) as CrmSaleQuotationApi.SaleQuotation;
|
data.items = itemFormRef.value?.getData();
|
try {
|
await (formData.value?.id ? updateSaleQuotation(data) : createSaleQuotation(data));
|
await modalApi.close();
|
emit('success');
|
message.success($t('ui.actionMessage.operationSuccess'));
|
} finally {
|
modalApi.unlock();
|
}
|
},
|
async onOpenChange(isOpen: boolean) {
|
if (!isOpen) {
|
formData.value = undefined;
|
return;
|
}
|
const data = modalApi.getData<CrmSaleQuotationApi.SaleQuotation>();
|
if (!data || !data.id) {
|
formData.value = {} as CrmSaleQuotationApi.SaleQuotation;
|
await formApi.resetForm();
|
// 新增时,重置并添加一行空数据
|
itemFormRef.value?.resetData();
|
return;
|
}
|
modalApi.lock();
|
try {
|
formData.value = await getSaleQuotation(data.id);
|
await formApi.setValues(formData.value);
|
// 编辑时设置物料数据
|
itemFormRef.value?.setData(formData.value.items || []);
|
} finally {
|
modalApi.unlock();
|
}
|
},
|
});
|
</script>
|
|
<template>
|
<Modal :title="getTitle" class="w-[80%]">
|
<Form class="mx-4">
|
<template #items>
|
<ItemForm
|
ref="itemFormRef"
|
class="w-full"
|
:disabled="isDisabled"
|
@change="calculateTotalPrice"
|
/>
|
</template>
|
</Form>
|
</Modal>
|
</template>
|