gaoluyang
2026-08-03 75242bbba28ae8902ddf86d33febb497d79e5fc3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<script lang="ts" setup>
import type { CrmSaleQuotationAiApi } from '#/api/crm/saleQuotation/ai';
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, Alert } 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 ocrCustomerName = 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;
      ocrCustomerName.value = '';
      return;
    }
    const data = modalApi.getData<CrmSaleQuotationApi.SaleQuotation & { ocrData?: CrmSaleQuotationAiApi.OcrResultVO }>();
    if (!data || !data.id) {
      formData.value = {} as CrmSaleQuotationApi.SaleQuotation;
      await formApi.resetForm();
      // OCR 识别数据预填
      if (data?.ocrData) {
        const ocr = data.ocrData;
        await formApi.setValues({
          name: ocr.name,
          quotationTime: ocr.quotationTime,
          validUntil: ocr.validUntil,
          taxRate: ocr.taxRate,
          discountPercent: ocr.discountPercent,
          remark: ocr.remark,
        });
        if (ocr.items?.length) {
          itemFormRef.value?.setData(ocr.items.map((item) => ({
            id: undefined as unknown as number,
            itemId: undefined as unknown as number,
            itemName: item.itemName || '',
            itemCode: '',
            itemBarCode: undefined,
            itemSpecification: item.itemSpec || '',
            itemUnitName: '',
            itemUnitName2: undefined,
            itemUnitName3: undefined,
            itemPrice: undefined as unknown as number,
            quotationPrice: item.quotationPrice || 0,
            count: item.count || 1,
            totalPrice: erpPriceMultiply(item.quotationPrice || 0, item.count || 1) ?? 0,
            taxPercent: undefined,
            taxPrice: undefined,
            remark: undefined,
          })));
        } else {
          itemFormRef.value?.resetData();
        }
        // 存储 OCR 识别的客户名称,用于界面提示
        ocrCustomerName.value = ocr.customerName || '';
      } else {
        // 新增时,重置并添加一行空数据
        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%]">
    <Alert
      v-if="ocrCustomerName"
      type="info"
      show-icon
      class="mx-4 mb-4"
      message="AI 识别到客户名称,请在客户选择器中手动匹配 CRM 客户"
      :description="`识别结果:${ocrCustomerName}`"
    />
    <Form class="mx-4">
      <template #items>
        <ItemForm
          ref="itemFormRef"
          class="w-full"
          :disabled="isDisabled"
          @change="calculateTotalPrice"
        />
      </template>
    </Form>
  </Modal>
</template>