张诺
2 天以前 437a70bd86d8adda3da41853b8b1c2706a22fdd7
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<template>
  <div>
    <el-dialog
      v-model="dialogFormVisible"
      :title="title"
      width="600"
      :close-on-click-modal="false"
      @close="handleClose"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="auto"
        class="production-form"
        label-position="right"
        style="max-width: 400px; margin: 0 auto"
      >
        <el-form-item label="供应商名称" prop="supplierName">
          <el-select v-model="form.supplierName" placeholder="请选择供应商">
            <el-option :label="item.label" v-for="item in supplyList" :key="item.value" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="煤种" prop="coal">
          <el-select v-model="form.coal" placeholder="请选择煤种">
            <el-option :label="item.label" v-for="item in coalList" :key="item.value" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-select
            v-model="form.unit"
            placeholder="请选择单位"
            clearable
            style="width: 100%"
          >
            <el-option label="吨" value="吨" />
            <el-option label="千克" value="千克" />
          </el-select>
        </el-form-item>
        <el-form-item label="采购数量" prop="purchaseQuantity">
          <el-input
            v-model.number="form.purchaseQuantity"
            placeholder="请输入"
            @blur="handleQuantityBlur"
          >
            <template v-slot:suffix>
              <i style="font-style: normal">{{ form.unit ? form.unit : "" }}</i>
            </template>
          </el-input> </el-form-item
        ><el-form-item label="税率" prop="taxRate">
          <el-input
            v-model.number="form.taxRate"
            placeholder="请输入税率"
            @blur="handleTaxRateBlur"
          >
            <template v-slot:suffix>
              <i style="font-style: normal">%</i>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="单价(不含税)" prop="priceExcludingTax">
          <el-input
            v-model.number="form.priceExcludingTax"
            placeholder="请输入"
            @blur="handlePriceBlur"
          >
            <template v-slot:suffix>
              <i style="font-style: normal">元</i>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="单价(含税)" prop="priceIncludingTax">
          <el-input
            v-model.number="form.priceIncludingTax"
            placeholder="自动计算"
          >
            <template v-slot:suffix>
              <i style="font-style: normal">元</i>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="总价(不含税)" prop="totalPriceExcludingTax">
          <el-input
            v-model.number="form.totalPriceExcludingTax"
            placeholder="自动计算"
          >
            <template v-slot:suffix>
              <i style="font-style: normal">元</i>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="总价(含税)" prop="totalPriceIncludingTax">
          <el-input
            v-model.number="form.totalPriceIncludingTax"
            placeholder="自动计算"
          >
            <template v-slot:suffix>
              <i style="font-style: normal">元</i>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="登记人" prop="registrantId">
          <el-input v-model="form.registrantId" disabled placeholder="请输入" />
        </el-form-item>
        <el-form-item label="登记日期" prop="registrationDate">
          <el-date-picker
            disabled
            v-model="form.registrationDate"
            type="date"
            placeholder="YYYY-MM-DD"
            style="width: 100%"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <!-- 重置和取消 -->
          <el-button @click="handleClose" v-if="title.includes('新增')"
            >取消</el-button
          >
          <el-button @click="handleReset" v-if="title.includes('编辑')"
            >重置</el-button
          >
          <el-button type="primary" @click="handleSubmit">确认</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
 
<script setup name="ProductionDialog">
import { ref, defineProps, watch, onMounted, nextTick, computed } from "vue";
import { ElMessage } from "element-plus";
import useUserStore from "@/store/modules/user";
import { addOrEditPR } from "@/api/procureMent";
import { getSupply } from "@/api/basicInformation/supplier";
import { getCoalInfo } from "@/api/basicInformation/coal";
const props = defineProps({
  title: {
    type: String,
    default: "",
  },
});
const emit = defineEmits(["update:dialogFormVisible", "success"]);
const dialogFormVisible = defineModel("dialogFormVisible", {
  required: true,
  type: Boolean,
});
const form = defineModel("form", {
  required: true,
  type: Object,
});
const copyForm = defineModel("copyForm", {
  required: true,
  type: Object,
});
const supplyList = ref([]);
const coalList = ref([]);
// 获取供应商下拉和煤种下拉
const getDropdownData = async () => {
  try {
    const [supplyRes, coalRes] = await Promise.all([
      getSupply(),
      getCoalInfo(),
    ]);
    let supplyData = supplyRes.data.records;
    let coalData = coalRes.data.records;
    supplyList.value = supplyData.map((item) => ({
      value: item.id,
      label: item.supplierName,
    }));
    coalList.value = coalData.map((item) => ({
      value: item.id,
      label: item.coal,
    }));
    console.log(supplyList.value, coalList.value);
  } catch (error) {
    console.error("获取下拉数据失败:", error);
  }
};
 
defineExpose({
  getDropdownData,
});
const toFixed = (num, precision = 2) => {
  if (isNaN(num) || num === null || num === undefined || num === "") {
    return 0;
  }
  return (
    Math.floor(parseFloat(num) * Math.pow(10, precision)) /
    Math.pow(10, precision)
  );
};
// 含税单价计算
const unitPriceWithTax = computed(() => {
  const priceExcludingTax = parseFloat(form.value.priceExcludingTax) || 0;
  const taxRate = parseFloat(form.value.taxRate) || 0;
  if (!priceExcludingTax || !taxRate) {
    return 0;
  }
  const result = priceExcludingTax * (1 + taxRate / 100);
  return toFixed(result, 2);
});
 
// 含税总价计算
const totalUnitPriceWithTax = computed(() => {
  const priceExcludingTax = parseFloat(form.value.priceExcludingTax) || 0;
  const taxRate = parseFloat(form.value.taxRate) || 0;
  const purchaseQuantity = parseFloat(form.value.purchaseQuantity) || 0;
 
  if (!priceExcludingTax || !taxRate || !purchaseQuantity) {
    return 0;
  }
 
  const unitPriceWithTaxValue = priceExcludingTax * (1 + taxRate / 100);
  const result = unitPriceWithTaxValue * purchaseQuantity;
  return toFixed(result, 2);
});
 
// 不含税总价计算
const taxExclusiveTotalPrice = computed(() => {
  const purchaseQuantity = parseFloat(form.value.purchaseQuantity) || 0;
  const priceExcludingTax = parseFloat(form.value.priceExcludingTax) || 0;
 
  if (!purchaseQuantity || !priceExcludingTax) {
    return 0;
  }
 
  const result = purchaseQuantity * priceExcludingTax;
  return toFixed(result, 2);
});
 
// 监听计算值变化,同步到 form 对象中
watch(unitPriceWithTax, (newValue) => {
  form.value.priceIncludingTax = newValue;
});
 
watch(totalUnitPriceWithTax, (newValue) => {
  form.value.totalPriceIncludingTax = newValue;
});
 
watch(taxExclusiveTotalPrice, (newValue) => {
  form.value.totalPriceExcludingTax = newValue;
});
 
const userStore = useUserStore();
const userInfo = ref({});
 
// 处理税率输入框失焦,确保精度
const handleTaxRateBlur = () => {
  if (
    form.value.taxRate !== null &&
    form.value.taxRate !== undefined &&
    form.value.taxRate !== ""
  ) {
    form.value.taxRate = toFixed(parseFloat(form.value.taxRate), 2);
  }
};
 
// 处理不含税单价输入框失焦,确保精度
const handlePriceBlur = () => {
  if (
    form.value.priceExcludingTax !== null &&
    form.value.priceExcludingTax !== undefined &&
    form.value.priceExcludingTax !== ""
  ) {
    form.value.priceExcludingTax = toFixed(
      parseFloat(form.value.priceExcludingTax),
      2
    );
  }
};
 
// 处理采购数量输入框失焦,确保精度
const handleQuantityBlur = () => {
  if (
    form.value.purchaseQuantity !== null &&
    form.value.purchaseQuantity !== undefined &&
    form.value.purchaseQuantity !== ""
  ) {
    form.value.purchaseQuantity = toFixed(
      parseFloat(form.value.purchaseQuantity),
      3
    ); // 数量保留3位小数
  }
};
 
onMounted(async () => {
  let res = await userStore.getInfo();
  userInfo.value = res;
});
const rules = {
  supplierName: [
    { required: true, message: "请输入供应商名称", trigger: "blur" },
  ],
  coal: [{ required: true, message: "请输入煤种", trigger: "blur" }],
  unit: [{ required: true, message: "请输入单位", trigger: "blur" }],
  purchaseQuantity: [
    { required: true, message: "请输入采购数量", trigger: "blur" },
    { type: "number", message: "采购数量必须为数字", trigger: "blur" },
  ],
  priceExcludingTax: [
    { required: true, message: "请输入单价", trigger: "blur" },
  ],
  totalPriceExcludingTax: [
    { required: true, message: "请输入总价", trigger: "blur" },
  ],
  priceIncludingTax: [
    { required: true, message: "请输入含税单价", trigger: "blur" },
  ],
  totalPriceIncludingTax: [
    { required: true, message: "请输入含税总价", trigger: "blur" },
  ],
  taxRate: [{ required: true, message: "请输入税率", trigger: "blur" }],
  registrantId: [{ required: true, message: "请输入登记人", trigger: "blur" }],
  registrationDate: [
    { required: true, message: "请选择登记日期", trigger: "change" },
  ],
};
// 关闭弹窗
const handleClose = () => {
  dialogFormVisible.value = false;
};
const handleReset = async () => {
  // 重置表单数据
  form.value = JSON.parse(JSON.stringify(copyForm.value));
  // 等待DOM更新完成后清除表单验证状态
  await nextTick();
  if (formRef.value) {
    formRef.value.clearValidate();
  }
};
const formRef = ref(null);
// 提交表单
const handleSubmit = async () => {
  console.log("提交表单", form.value);
  if (!formRef.value) return;
  await formRef.value.validate(async (valid) => {
    if (valid) {
      const obj = ref({});
      if (props.title.includes("新增")) {
        let result = await addOrEditPR({
          ...form.value,
        });
        obj.value = {
          title: "新增",
          ...form.value,
          result,
        };
      } else {
        delete form.value.updateTime;
        delete form.value.createTime;
        let result = await addOrEditPR({
          ...form.value,
        });
        obj.value = {
          title: "编辑",
          ...form.value,
          result,
        };
      }
      emit("submit", obj.value);
    }
  });
};
</script>
 
<style lang="sass" scoped></style>