gaoluyang
6 天以前 3972a27ebe534def8c231491913c1036af09ae31
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<script lang="ts" setup>
import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MesWmReturnIssueApi } from '#/api/mes/wm/returnissue';
import type { MesWmReturnIssueLineApi } from '#/api/mes/wm/returnissue/line';
 
import { computed, h, markRaw, ref } from 'vue';
 
import {
  DICT_TYPE,
  MesAutoCodeRuleCode,
  MesProWorkOrderStatusEnum,
  MesWmReturnIssueStatusEnum,
} from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
 
import { Button, Divider, message, Popconfirm } from 'ant-design-vue';
 
import { useVbenForm } from '#/adapter/form';
import { useVbenModal } from '@vben/common-ui';
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import { generateAutoCode } from '#/api/mes/md/autocode/record';
import {
  getReturnIssue,
  submitReturnIssue,
  updateReturnIssue,
} from '#/api/mes/wm/returnissue';
import {
  deleteReturnIssueLine,
  getReturnIssueLinePage,
} from '#/api/mes/wm/returnissue/line';
import { MdWorkstationSelect } from '#/views/mes/md/workstation/components';
import { ProWorkOrderSelect } from '#/views/mes/pro/workorder/components';
 
import ReturnIssueLineFormModal from './ReturnIssueLineFormModal.vue';
 
const emit = defineEmits(['success']);
const formData = ref<MesWmReturnIssueApi.ReturnIssue>();
 
const canSubmit = computed(
  () => formData.value?.status === MesWmReturnIssueStatusEnum.PREPARE,
);
 
/** 构建编辑表单 schema */
function buildFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: { triggerFields: [''], show: () => false },
    },
    {
      fieldName: 'status',
      component: 'Input',
      dependencies: { triggerFields: [''], show: () => false },
    },
    {
      fieldName: 'code',
      label: '退料单编号',
      component: 'Input',
      componentProps: { placeholder: '请输入退料单编号' },
      rules: 'required',
      suffix: () =>
        h(
          Button,
          {
            type: 'default',
            size: 'small',
            onClick: async () => {
              const code = await generateAutoCode(
                MesAutoCodeRuleCode.WM_RETURN_ISSUE_CODE,
              );
              await formApi.setFieldValue('code', code);
            },
          },
          { default: () => '生成' },
        ),
    },
    {
      fieldName: 'name',
      label: '退料单名称',
      component: 'Input',
      componentProps: { placeholder: '请输入退料单名称' },
      rules: 'required',
    },
    {
      fieldName: 'type',
      label: '退料类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_WM_RETURN_ISSUE_TYPE, 'number'),
        placeholder: '请选择退料类型',
      },
      rules: 'required',
    },
    {
      fieldName: 'workOrderId',
      label: '生产订单',
      component: markRaw(ProWorkOrderSelect),
      componentProps: {
        status: MesProWorkOrderStatusEnum.CONFIRMED,
      },
    },
    {
      fieldName: 'workstationId',
      label: '工作站',
      component: markRaw(MdWorkstationSelect),
      componentProps: { placeholder: '请选择工作站' },
    },
    {
      fieldName: 'returnDate',
      label: '退料日期',
      component: 'DatePicker',
      componentProps: {
        class: '!w-full',
        format: 'YYYY-MM-DD HH:mm:ss',
        placeholder: '请选择退料日期',
        showTime: true,
        valueFormat: 'x',
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      formItemClass: 'col-span-3',
      componentProps: { placeholder: '请输入备注', rows: 2 },
    },
  ];
}
 
const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: { class: 'w-full' },
    formItemClass: 'col-span-1',
    labelWidth: 100,
  },
  layout: 'horizontal',
  schema: buildFormSchema(),
  showDefaultActions: false,
  wrapperClass: 'grid-cols-3',
});
 
// --- 物料行列表 ---
const lineGridColumns: VxeTableGridOptions<MesWmReturnIssueLineApi.ReturnIssueLine>['columns'] = [
  { field: 'itemCode', title: '物料编码', minWidth: 120 },
  { field: 'itemName', title: '物料名称', minWidth: 140 },
  { field: 'specification', title: '规格型号', minWidth: 120 },
  { field: 'unitMeasureName', title: '单位', width: 80 },
  { field: 'quantity', title: '退料数量', width: 100 },
  { field: 'batchCode', title: '批次号', minWidth: 120 },
  { title: '操作', width: 140, fixed: 'right', slots: { default: 'lineActions' } },
];
 
const [LineGrid, lineGridApi] = useVbenVxeGrid({
  gridOptions: {
    columns: lineGridColumns,
    height: 260,
    keepSource: true,
    proxyConfig: {
      ajax: {
        query: async ({ page }) => {
          if (!formData.value?.id) return { list: [], total: 0 };
          return await getReturnIssueLinePage({
            issueId: formData.value.id,
            pageNo: page.currentPage,
            pageSize: page.pageSize,
          });
        },
      },
    },
    rowConfig: { keyField: 'id', isHover: true },
    toolbarConfig: { refresh: true },
    pagerConfig: { pageSize: 10 },
  } as VxeTableGridOptions<MesWmReturnIssueLineApi.ReturnIssueLine>,
});
 
/** 添加物料行 */
function handleCreateLine() {
  lineFormModalApi.setData({ issueId: formData.value?.id }).open();
}
 
/** 编辑物料行 */
function handleEditLine(row: MesWmReturnIssueLineApi.ReturnIssueLine) {
  lineFormModalApi.setData({ id: row.id, issueId: formData.value?.id }).open();
}
 
/** 删除物料行 */
async function handleDeleteLine(row: MesWmReturnIssueLineApi.ReturnIssueLine) {
  try {
    await deleteReturnIssueLine(row.id!);
    message.success('删除成功');
    lineGridApi.query();
  } catch {
    message.error('删除失败');
  }
}
 
/** 物料行操作成功 */
function onLineSuccess() {
  lineGridApi.query();
}
 
const [LineFormModal, lineFormModalApi] = useVbenModal({
  connectedComponent: ReturnIssueLineFormModal,
  destroyOnClose: true,
});
 
/** 提交退料单 */
async function handleSubmit() {
  if (!formData.value?.id) return;
  modalApi.lock();
  try {
    // 先保存表单
    const data =
      (await formApi.getValues()) as MesWmReturnIssueApi.ReturnIssue;
    await updateReturnIssue({ ...formData.value, ...data });
    // 再提交
    await submitReturnIssue(formData.value.id);
    message.success('提交成功');
    await modalApi.close();
    emit('success');
  } finally {
    modalApi.unlock();
  }
}
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) return;
    modalApi.lock();
    try {
      const data =
        (await formApi.getValues()) as MesWmReturnIssueApi.ReturnIssue;
      await updateReturnIssue({ ...formData.value, ...data });
      formData.value = { ...formData.value, ...data };
      emit('success');
      message.success('保存成功');
      await modalApi.close();
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = undefined;
      return;
    }
    const data = modalApi.getData<{ id: number }>();
    if (data?.id) {
      modalApi.lock();
      try {
        formData.value = await getReturnIssue(data.id);
        await formApi.setValues(formData.value);
        // 加载后刷新物料行
        lineGridApi.query();
      } finally {
        modalApi.unlock();
      }
    }
  },
});
</script>
 
<template>
  <Modal title="编辑退料单" class="w-3/5">
    <Form class="mx-4" />
    <!-- 物料信息 -->
    <template v-if="formData?.id">
      <Divider style="margin: 12px 0">
        <span style="font-weight: 600; color: #fa8c16;">物料信息</span>
      </Divider>
      <div class="mx-4" style="height: 300px">
        <LineGrid table-title="物料列表">
          <template #toolbar-tools>
            <TableAction
              :actions="[
                {
                  label: '添加物料',
                  type: 'primary',
                  icon: ACTION_ICON.ADD,
                  onClick: handleCreateLine,
                },
              ]"
            />
          </template>
          <template #lineActions="{ row }">
            <TableAction
              :actions="[
                {
                  label: '编辑',
                  type: 'link',
                  icon: ACTION_ICON.EDIT,
                  onClick: handleEditLine.bind(null, row),
                },
                {
                  label: '删除',
                  type: 'link',
                  danger: true,
                  icon: ACTION_ICON.DELETE,
                  popConfirm: {
                    title: `确认删除物料 ${row.itemName}?`,
                    confirm: handleDeleteLine.bind(null, row),
                  },
                },
              ]"
            />
          </template>
        </LineGrid>
      </div>
    </template>
    <template #prepend-footer>
      <div class="flex flex-auto items-center gap-2">
        <Popconfirm
          v-if="canSubmit"
          title="确认提交该退料单?【提交后将不能修改】"
          @confirm="handleSubmit"
        >
          <Button type="primary">提交</Button>
        </Popconfirm>
      </div>
    </template>
  </Modal>
  <LineFormModal @success="onLineSuccess" />
</template>