gaoluyang
昨天 b64a0deae5b5d33f9e20671a68936b27f0b9b00b
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
<script lang="ts" setup>
import type { MesWmMaterialStockApi } from '#/api/mes/wm/materialstock';
import type { VbenFormSchema } from '#/adapter/form';
 
import { markRaw, ref } from 'vue';
 
import { useVbenModal } from '@vben/common-ui';
 
import { message } from 'ant-design-vue';
 
import { z } from '#/adapter/form';
import { useVbenForm } from '#/adapter/form';
import { moveMaterialStock } from '#/api/mes/wm/materialstock';
import {
  WmWarehouseAreaSelect,
  WmWarehouseLocationSelect,
  WmWarehouseSelect,
} from '#/views/wls/warehouse/components';
 
defineOptions({ name: 'WmStockMoveModal' });
 
const emit = defineEmits<{ success: [] }>();
 
const currentRow = ref<MesWmMaterialStockApi.MaterialStock>();
 
function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'itemCode',
      label: '物料编码',
      component: 'Input',
      componentProps: { disabled: true },
    },
    {
      fieldName: 'itemName',
      label: '物料名称',
      component: 'Input',
      componentProps: { disabled: true },
    },
    {
      fieldName: 'batchCode',
      label: '批次号',
      component: 'Input',
      componentProps: { disabled: true },
    },
    {
      fieldName: 'warehouseName',
      label: '当前仓库',
      component: 'Input',
      componentProps: { disabled: true },
    },
    {
      fieldName: 'locationName',
      label: '当前库区',
      component: 'Input',
      componentProps: { disabled: true },
    },
    {
      fieldName: 'areaName',
      label: '当前库位',
      component: 'Input',
      componentProps: { disabled: true },
    },
    {
      fieldName: 'quantity',
      label: '当前数量',
      component: 'InputNumber',
      componentProps: { disabled: true, class: '!w-full', precision: 2 },
    },
    {
      fieldName: 'toWarehouseId',
      label: '目标仓库',
      component: markRaw(WmWarehouseSelect),
      componentProps: {
        placeholder: '请选择目标仓库',
        onChange: async () => {
          await formApi.setFieldValue('toLocationId', undefined);
          await formApi.setFieldValue('toAreaId', undefined);
        },
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'toLocationId',
      label: '目标库区',
      component: markRaw(WmWarehouseLocationSelect),
      componentProps: { placeholder: '请选择目标库区' },
      dependencies: {
        triggerFields: ['toWarehouseId'],
        componentProps: (values) => ({
          warehouseId: values.toWarehouseId,
          placeholder: '请选择目标库区',
          onChange: async () => {
            await formApi.setFieldValue('toAreaId', undefined);
          },
        }),
        trigger: (values, api) => {
          if (values.toLocationId !== undefined) {
            void api.setFieldValue('toLocationId', undefined);
            void api.setFieldValue('toAreaId', undefined);
          }
        },
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'toAreaId',
      label: '目标库位',
      component: markRaw(WmWarehouseAreaSelect),
      componentProps: { placeholder: '请选择目标库位' },
      dependencies: {
        triggerFields: ['toLocationId'],
        componentProps: (values) => ({
          locationId: values.toLocationId,
          placeholder: '请选择目标库位',
        }),
        trigger: (values, api) => {
          if (values.toAreaId !== undefined) {
            void api.setFieldValue('toAreaId', undefined);
          }
        },
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'moveQuantity',
      label: '移库数量',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0.01,
        placeholder: '请输入移库数量',
        precision: 2,
      },
      rules: z.number().positive('移库数量必须大于0'),
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      formItemClass: 'col-span-2',
      componentProps: { placeholder: '请输入备注', rows: 3 },
    },
  ];
}
 
const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: { class: 'w-full' },
    formItemClass: 'col-span-1',
    labelWidth: 100,
  },
  layout: 'horizontal',
  schema: useFormSchema(),
  showDefaultActions: false,
  wrapperClass: 'grid-cols-2',
});
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) return;
 
    const row = currentRow.value;
    if (!row?.id) return;
 
    const values = await formApi.getValues();
    const moveQuantity = values.moveQuantity as number;
    const quantity = (row.quantity || 0) as number;
    const availableQuantity = (row.availableQuantity || 0) as number;
 
    if (moveQuantity > quantity) {
      message.error(`移库数量不能超过当前库存数量(${quantity.toFixed(2)})`);
      return;
    }
 
    if (moveQuantity > availableQuantity) {
      message.error(`移库数量不能超过可用量(${availableQuantity.toFixed(2)}),可用量 = 在库数量 - 冻结数量 - 占用量`);
      return;
    }
 
    if (row.warehouseId === values.toWarehouseId && row.locationId === values.toLocationId && row.areaId === values.toAreaId) {
      message.error('目标库位与当前库位相同,不允许移库');
      return;
    }
 
    modalApi.lock();
    try {
      await moveMaterialStock({
        materialStockId: row.id,
        quantity: moveQuantity,
        remark: values.remark,
        toAreaId: values.toAreaId,
        toLocationId: values.toLocationId,
        toWarehouseId: values.toWarehouseId,
      });
      message.success('移库成功');
      emit('success');
      await modalApi.close();
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      currentRow.value = undefined;
      return;
    }
    const data = modalApi.getData<MesWmMaterialStockApi.MaterialStock>();
    if (!data?.id) return;
    currentRow.value = data;
 
    if (data.frozen) {
      message.warning('该库存记录已冻结,不允许移库');
      await modalApi.close();
      return;
    }
 
    await formApi.setValues({
      areaName: data.areaName,
      batchCode: data.batchCode,
      itemCode: data.itemCode,
      itemName: data.itemName,
      locationName: data.locationName,
      quantity: data.quantity,
      warehouseName: data.warehouseName,
    });
  },
  title: '库存移库',
});
</script>
 
<template>
  <Modal class="w-3/5">
    <Form class="mx-4" />
  </Modal>
</template>