10 天以前 fdd158306bf6dc3584f5110a385323f4a8dfb463
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
<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>