gongchunyi
4 小时以前 5b3cbcef771cd23ef8db1bf29dd15e2ddd98744f
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
<template>
  <div>
    <el-dialog
      v-model="isShow"
      :title="operationType === 'frozen' ? '冻结库存' : '解冻库存'"
      width="800"
      @close="closeModal"
    >
      <el-form label-width="140px" :model="formState" ref="formRef">
        <el-form-item
          label="库存类型"
          prop="stockType"
          :rules="[
            {
              required: true,
              message: '请选择库存类型',
              trigger: 'change',
            }
          ]"
        >
          <el-select v-model="formState.stockType" placeholder="请选择库存类型" style="width: 100%">
            <el-option label="合格库存" value="qualified" />
            <el-option label="不合格库存" value="unqualified" />
          </el-select>
        </el-form-item>
 
        <el-form-item :label="operationType === 'frozen' ? '冻结数量:' : '解冻数量:'" prop="lockedQuantity">
          <el-input-number
            v-model="formState.lockedQuantity"
            :step="0.01"
            :min="inputMin"
            :precision="2"
            style="width: 100%"
            :max="maxCount"
          />
        </el-form-item>
      </el-form>
 
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确认</el-button>
          <el-button @click="closeModal">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
 
<script setup>
import { ref, computed, getCurrentInstance, onMounted, watch } from "vue";
import { frozenStockInventory, thawStockInventory } from "@/api/inventoryManagement/stockInventory.js";
import { frozenStockUninventory, thawStockUninventory } from "@/api/inventoryManagement/stockUninventory.js";
 
const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
  },
  operationType: {
    type: String,
    required: true,
    default: "frozen",
  },
  type: {
    type: String,
    required: true,
    default: "qualified",
  },
  record: {
    type: Object,
    default: () => ({}),
  },
});
 
const emit = defineEmits(["update:visible", "completed"]);
 
const toNumber = (value) => {
  const num = Number(value);
  return Number.isFinite(num) ? num : 0;
};
 
const getQualifiedUnLockedStock = (row) => {
  return toNumber(
    row?.qualifiedUnLockedQuantity ??
      row?.unLockedQuantity ??
      row?.qualifiedQuantity ??
      row?.qualitity
  );
};
 
const getUnqualifiedUnLockedStock = (row) => {
  return toNumber(
    row?.unQualifiedUnLockedQuantity ??
      row?.unqualifiedUnLockedQuantity ??
      row?.unQualifiedQuantity ??
      row?.unqualifiedQuantity
  );
};
 
const getQualifiedLockedStock = (row) => {
  return toNumber(row?.qualifiedLockedQuantity ?? row?.lockedQuantity);
};
 
const getUnqualifiedLockedStock = (row) => {
  return toNumber(row?.unQualifiedLockedQuantity ?? row?.unqualifiedLockedQuantity);
};
 
const formState = ref({
  stockType: "qualified",
  lockedQuantity: 0.01,
});
 
const isShow = computed({
  get() {
    return props.visible;
  },
  set(val) {
    emit("update:visible", val);
  },
});
 
const maxCount = computed(() => {
  const isQualified = formState.value.stockType === "qualified";
  if (props.operationType === "frozen") {
    return isQualified ? getQualifiedUnLockedStock(props.record) : getUnqualifiedUnLockedStock(props.record);
  }
  return isQualified ? getQualifiedLockedStock(props.record) : getUnqualifiedLockedStock(props.record);
});
 
const inputMin = computed(() => (maxCount.value > 0 ? 0.01 : 0));
 
const targetStockId = computed(() => {
  if (formState.value.stockType === "unqualified") {
    return props.record?.unQualifiedId ?? props.record?.unqualifiedId ?? props.record?.id;
  }
  return props.record?.qualifiedId ?? props.record?.id;
});
 
const initFormData = () => {
  formState.value.stockType = props.type === "unqualified" ? "unqualified" : "qualified";
  if (props.operationType === "thaw") {
    formState.value.lockedQuantity = maxCount.value;
  } else {
    formState.value.lockedQuantity = maxCount.value > 0 ? 0.01 : 0;
  }
};
 
watch(
  () => props.record,
  () => {
    initFormData();
  },
  { deep: true }
);
 
watch(
  () => props.operationType,
  () => {
    initFormData();
  }
);
 
watch(
  maxCount,
  (val) => {
    if (val <= 0) {
      formState.value.lockedQuantity = 0;
      return;
    }
    if (!formState.value.lockedQuantity || formState.value.lockedQuantity < 0.01) {
      formState.value.lockedQuantity = 0.01;
    } else if (formState.value.lockedQuantity > val) {
      formState.value.lockedQuantity = val;
    }
  },
  { immediate: true }
);
 
const { proxy } = getCurrentInstance();
 
const closeModal = () => {
  formState.value = {
    stockType: "qualified",
    lockedQuantity: 0.01,
  };
  isShow.value = false;
};
 
const handleSubmit = () => {
  proxy.$refs["formRef"].validate((valid) => {
    if (!valid) return;
 
    if (!formState.value.stockType) {
      proxy.$modal.msgError("请选择库存类型");
      return;
    }
    if (!formState.value.lockedQuantity || formState.value.lockedQuantity <= 0) {
      proxy.$modal.msgError(props.operationType === "frozen" ? "冻结数量必须大于0" : "解冻数量必须大于0");
      return;
    }
    if (formState.value.lockedQuantity > maxCount.value) {
      proxy.$modal.msgError("操作数量不能超过可操作库存");
      return;
    }
 
    if (!targetStockId.value) {
      proxy.$modal.msgError("未找到对应库存ID,无法提交");
      return;
    }
 
    const data = Object.assign({ id: targetStockId.value }, formState.value);
    let submitApi;
    if (formState.value.stockType === "qualified") {
      submitApi = props.operationType === "frozen" ? frozenStockInventory : thawStockInventory;
    } else {
      submitApi = props.operationType === "frozen" ? frozenStockUninventory : thawStockUninventory;
    }
 
    submitApi(data).then((res) => {
      if (res.code === 200) {
        isShow.value = false;
        emit("completed");
        proxy.$modal.msgSuccess("提交成功");
      } else {
        proxy.$modal.msgError(res.msg);
      }
    });
  });
};
 
onMounted(() => {
  initFormData();
});
 
defineExpose({
  closeModal,
  handleSubmit,
  isShow,
});
</script>