huminmin
昨天 b31c4a85ee6d08958dc44bc824169580dc87efaa
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
<template>
  <div>
    <el-dialog
        v-model="isShow"
        title="新增库存"
        width="800"
        @close="closeModal"
    >
      <el-form label-width="140px" :model="formState" label-position="top" ref="formRef">
        <el-form-item
            label="产品名称"
            prop="productModelId"
            :rules="[
                {
                required: true,
                message: '请选择产品',
                trigger: 'change',
              }
            ]"
        >
          <el-button type="primary" @click="showProductSelectDialog = true">
            {{ formState.productName ? formState.productName : '选择产品' }}
          </el-button>
        </el-form-item>
 
        <el-form-item
            label="规格"
            prop="productModelName"
        >
          <el-input v-model="formState.productModelName"  disabled />
        </el-form-item>
 
        <el-form-item
            label="单位"
            prop="unit"
        >
          <el-input v-model="formState.unit"  disabled />
        </el-form-item>
 
        <!-- productType === 0:原材料 -->
        <el-form-item
            v-if="type === 'qualified' && formState.productType === 0"
            label="车牌号"
            prop="licensePlateNo"
        >
          <el-input v-model="formState.licensePlateNo" />
        </el-form-item>
 
        <el-form-item
            v-if="type === 'qualified' && formState.productType === 0"
            label="毛重(吨)"
            prop="grossWeight"
        >
          <el-input-number
              v-model="formState.grossWeight"
              :step="0.01"
              :min="0"
              style="width: 100%"
              @change="computeNetWeight"
          />
        </el-form-item>
 
        <el-form-item
            v-if="type === 'qualified' && formState.productType === 0"
            label="皮重(吨)"
            prop="tareWeight"
        >
          <el-input-number
              v-model="formState.tareWeight"
              :step="0.01"
              :min="0"
              style="width: 100%"
              @change="computeNetWeight"
          />
        </el-form-item>
 
        <el-form-item
            v-if="type === 'qualified' && formState.productType === 0"
            label="净重(吨)"
            prop="netWeight"
        >
          <el-input-number
              v-model="formState.netWeight"
              :step="0.01"
              :min="0"
              style="width: 100%"
              disabled
          />
        </el-form-item>
 
        <el-form-item
            v-if="type === 'qualified' && formState.productType === 0"
            label="过磅日期"
            prop="weighingDate"
        >
          <el-date-picker
              style="width: 100%"
              v-model="formState.weighingDate"
              value-format="YYYY-MM-DD HH:mm:ss"
              format="YYYY-MM-DD HH:mm:ss"
              type="datetime"
              placeholder="请选择过磅日期"
              clearable
          />
        </el-form-item>
 
        <el-form-item
            v-if="type === 'qualified' && formState.productType === 0"
            label="过磅员"
            prop="weighingOperator"
        >
          <el-input v-model="formState.weighingOperator" />
        </el-form-item>
 
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formState.remark" type="textarea" />
        </el-form-item>
      </el-form>
 
      <!-- 产品选择弹窗 -->
      <ProductSelectDialog
          v-model="showProductSelectDialog"
          @confirm="handleProductSelect"
          single
      />
      <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} from "vue";
import ProductSelectDialog from "@/views/basicData/product/ProductSelectDialog.vue";
import {createStockInventory} from "@/api/inventoryManagement/stockInventory.js";
import {createStockUnInventory} from "@/api/inventoryManagement/stockUninventory.js";
 
const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
  },
 
  type: {
    type: String,
    required: true,
    default: 'qualified',
  },
});
 
const emit = defineEmits(['update:visible', 'completed']);
 
// 响应式数据(替代选项式的 data)
const formState = ref({
  productId: undefined,
  productModelId: undefined,
  productName: "",
  productModelName: "",
  unit: "",
  productType: undefined,
  // 过磅相关字段(仅原材料合格品使用)
  licensePlateNo: "",
  grossWeight: undefined,
  tareWeight: undefined,
  netWeight: undefined,
  weighingDate: undefined,
  weighingOperator: "",
  remark: '',
});
 
const isShow = computed({
  get() {
    return props.visible;
  },
  set(val) {
    emit('update:visible', val);
  },
});
 
const showProductSelectDialog = ref(false);
 
let { proxy } = getCurrentInstance()
 
const closeModal = () => {
  // 重置表单数据
  formState.value = {
    productId: undefined,
    productModelId: undefined,
    productName: "",
    productModelName: "",
    description: '',
  };
  isShow.value = false;
};
 
// 产品选择处理
const handleProductSelect = async (products) => {
  formState.value.weighingDate = undefined;
  formState.value.grossWeight = undefined;
  formState.value.tareWeight = undefined;
  formState.value.netWeight = undefined;
  if (products && products.length > 0) {
    const product = products[0];
    formState.value.productId = product.productId;
    formState.value.productName = product.productName;
    formState.value.productModelName = product.model;
    formState.value.productModelId = product.id;
    formState.value.unit = product.unit;
    formState.value.productType = product.productType;
    showProductSelectDialog.value = false;
    // 触发表单验证更新
    proxy.$refs["formRef"]?.validateField('productModelId');
  }
};
 
// 净重 = 毛重 - 皮重
const computeNetWeight = () => {
  const { grossWeight, tareWeight } = formState.value;
  if (grossWeight != null && tareWeight != null) {
    const net = Number(grossWeight) - Number(tareWeight);
    // 保留两位小数,且不为负
    const safeNet = Number(net.toFixed(2));
    formState.value.netWeight = safeNet > 0 ? safeNet : 0;
  } else {
    formState.value.netWeight = undefined;
  }
};
 
const handleSubmit = () => {
  proxy.$refs["formRef"].validate(valid => {
    if (valid) {
      // 验证是否选择了产品和规格
      if (!formState.value.productModelId) {
        proxy.$modal.msgError("请选择产品");
        return;
      }
      if (!formState.value.productModelId) {
        proxy.$modal.msgError("请选择规格");
        return;
      }
      if (props.type === 'qualified') {
        createStockInventory(formState.value).then(res => {
          // 关闭模态框
          isShow.value = false;
          // 告知父组件已完成
          emit('completed');
          proxy.$modal.msgSuccess("提交成功");
        })
      } else {
        createStockUnInventory(formState.value).then(res => {
          // 关闭模态框
          isShow.value = false;
          // 告知父组件已完成
          emit('completed');
          proxy.$modal.msgSuccess("提交成功");
        })
      }
 
    }
  })
};
 
 
defineExpose({
  closeModal,
  handleSubmit,
  isShow,
});
</script>