yuan
6 天以前 2a8dc0db849bcafba80acda057bb554fc566dc53
src/views/procurementManagement/procurementLedger/index.vue
@@ -121,13 +121,17 @@
                </template>
              </el-table-column>
              <el-table-column label="数量"
                               prop="quantity" />
                               prop="quantity"
                               :formatter="formattedNumber" />
              <el-table-column label="可用数量"
                               prop="availableQuality" />
                               prop="availableQuality"
                               :formatter="formattedNumber" />
              <el-table-column label="退货数量"
                               prop="returnQuantity" />
                               prop="returnQuantity"
                               :formatter="formattedNumber" />
              <el-table-column label="税率(%)"
                               prop="taxRate" />
                               prop="taxRate"
                               :formatter="formattedNumber" />
              <el-table-column label="含税单价(元)"
                               prop="taxInclusiveUnitPrice"
                               :formatter="formattedNumber" />
@@ -418,11 +422,13 @@
                           width="70" />
          <el-table-column label="数量"
                           prop="quantity"
                           width="70" />
                           width="70"
                           :formatter="formattedNumber" />
          <el-table-column label="库存预警数量"
                           prop="warnNum"
                           width="120"
                           show-overflow-tooltip />
                           show-overflow-tooltip
                           :formatter="formattedNumber" />
          <el-table-column label="税率(%)"
                           prop="taxRate"
                           width="80" />
@@ -599,7 +605,6 @@
            <el-form-item label="含税单价(元):"
                          prop="taxInclusiveUnitPrice">
              <el-input-number v-model="productForm.taxInclusiveUnitPrice"
                               :precision="6"
                               :step="0.1"
                               :min="0"
                               clearable
@@ -612,7 +617,6 @@
                          prop="quantity">
              <el-input-number :step="0.1"
                               clearable
                               :precision="6"
                               :min="0"
                               style="width: 100%"
                               v-model="productForm.quantity"
@@ -626,7 +630,6 @@
            <el-form-item label="含税总价(元):"
                          prop="taxInclusiveTotalPrice">
              <el-input-number v-model="productForm.taxInclusiveTotalPrice"
                               :precision="6"
                               :step="0.1"
                               :min="0"
                               clearable
@@ -638,7 +641,6 @@
            <el-form-item label="不含税总价(元):"
                          prop="taxExclusiveTotalPrice">
              <el-input-number v-model="productForm.taxExclusiveTotalPrice"
                               :precision="6"
                               :step="0.1"
                               :min="0"
                               clearable
@@ -665,11 +667,11 @@
            <el-form-item label="库存预警数量:"
                          prop="warnNum">
              <el-input-number v-model="productForm.warnNum"
                               :precision="6"
                               :step="0.1"
                               :min="0"
                               clearable
                               style="width: 100%" />
                               style="width: 100%"
                               @change="normalizeProductNumber('warnNum')" />
            </el-form-item>
          </el-col>
        </el-row>
@@ -761,7 +763,7 @@
  import { modelList, productTreeList } from "@/api/basicData/product.js";
  import dayjs from "dayjs";
  import FileUpload from "@/components/AttachmentUpload/file/index.vue";
  import { roundHalfUp, trimZeros } from "@/utils/formatNumber.js";
  import { formatDecimal6 } from "@/utils/formatNumber.js";
  const userStore = useUserStore();
@@ -1086,15 +1088,11 @@
  };
  const formattedNumber = (row, column, cellValue) => {
    return trimZeros(cellValue, 6);
    return formatDecimal6(cellValue);
  };
  // 金额显示格式化:按十进制四舍五入到 2 位小数(与数据库 ROUND 一致),再去掉末尾多余的 0
  const formatAmount = value => {
    return trimZeros(roundHalfUp(value, 2), 2);
  };
  // 金额列的表格格式化函数(单价列仍用 formattedNumber 保留 6 位小数)
  // 金额显示格式化:最多保留 6 位小数,超过 6 位的按十进制四舍五入
  const formattedAmount = (row, column, cellValue) => {
    return formatAmount(cellValue);
    return formatDecimal6(cellValue);
  };
  // 查询列表
  /** 搜索按钮操作 */
@@ -1214,10 +1212,10 @@
        "futureTicketsAmount",
      ],
      {
        taxInclusiveUnitPrice: { decimalPlaces: 6, trimZeros: true },
        // 金额列:每行先按 2 位小数四舍五入再求和,保证合计与列内展示值一致
        taxInclusiveTotalPrice: { roundEach: 2, decimalPlaces: 2, trimZeros: true },
        taxExclusiveTotalPrice: { roundEach: 2, decimalPlaces: 2, trimZeros: true },
        taxInclusiveUnitPrice: { formatter: formatDecimal6 },
        // 金额列:合计后最多保留 6 位小数,超过 6 位按十进制四舍五入
        taxInclusiveTotalPrice: { formatter: formatDecimal6 },
        taxExclusiveTotalPrice: { formatter: formatDecimal6 },
        ticketsAmount: { roundEach: 2, decimalPlaces: 2, trimZeros: true },
        futureTicketsAmount: { roundEach: 2, decimalPlaces: 2, trimZeros: true },
        ticketsNum: { noDecimal: true }, // 不保留小数
@@ -1289,9 +1287,9 @@
      param,
      ["contractAmount", "netContractAmount"],
      {
        // 金额列:每行先按 2 位小数四舍五入再求和,保证合计与列内展示值一致
        contractAmount: { roundEach: 2, decimalPlaces: 2, trimZeros: true },
        netContractAmount: { roundEach: 2, decimalPlaces: 2, trimZeros: true },
        // 金额列:合计后最多保留 6 位小数,超过 6 位按十进制四舍五入
        contractAmount: { formatter: formatDecimal6 },
        netContractAmount: { formatter: formatDecimal6 },
      }
    );
  };
@@ -1305,10 +1303,10 @@
        "taxExclusiveTotalPrice",
      ],
      {
        taxInclusiveUnitPrice: { decimalPlaces: 6, trimZeros: true },
        // 金额列:每行先按 2 位小数四舍五入再求和,保证合计与列内展示值一致
        taxInclusiveTotalPrice: { roundEach: 2, decimalPlaces: 2, trimZeros: true },
        taxExclusiveTotalPrice: { roundEach: 2, decimalPlaces: 2, trimZeros: true },
        taxInclusiveUnitPrice: { formatter: formatDecimal6 },
        // 金额列:合计后最多保留 6 位小数,超过 6 位按十进制四舍五入
        taxInclusiveTotalPrice: { formatter: formatDecimal6 },
        taxExclusiveTotalPrice: { formatter: formatDecimal6 },
      }
    );
  };
@@ -1517,6 +1515,20 @@
      // 复制行数据
      productForm.value = { ...row };
      // 后端数量/金额字段按 6 位小数返回(如 10.000000),转成普通数值,
      // 避免输入框回显多余的 0
      [
        "quantity",
        "taxInclusiveUnitPrice",
        "taxInclusiveTotalPrice",
        "taxExclusiveTotalPrice",
        "warnNum",
      ].forEach(key => {
        const val = productForm.value[key];
        if (val === null || val === undefined || val === "") return;
        productForm.value[key] = toDisplayNumber(val);
      });
      // 如果是从模板加载的数据,可能没有 productId 和 productModelId
      // 需要根据 productCategory 和 specificationModel 来查找对应的 ID
      if (!productForm.value.productId && productForm.value.productCategory) {
@@ -1645,6 +1657,8 @@
  const submitProduct = () => {
    proxy.$refs["productFormRef"].validate(valid => {
      if (valid) {
        // 提交前统一把数值收敛到最多 6 位小数(超出四舍五入)
        normalizeProductNumbers();
        if (operationType.value === "edit") {
          submitProductEdit();
        } else {
@@ -1803,7 +1817,33 @@
    return `${year}-${month}-${day}`;
  }
  // 计算结果转成数字:最多保留 6 位小数(超过 6 位的四舍五入),并去掉末尾无意义的 0,
  // 避免回填到 el-input-number 时出现 100.000000 这类展示
  const toDisplayNumber = value => {
    const num = Number(formatDecimal6(value));
    return Number.isFinite(num) ? num : 0;
  };
  // 产品弹窗单个数值字段归一化:输入超过 6 位小数时四舍五入到 6 位
  const normalizeProductNumber = key => {
    const val = productForm.value[key];
    if (val === null || val === undefined || val === "") return;
    productForm.value[key] = toDisplayNumber(val);
  };
  // 产品弹窗参与联动计算的数值字段统一归一化
  const normalizeProductNumbers = () => {
    [
      "quantity",
      "taxInclusiveUnitPrice",
      "taxInclusiveTotalPrice",
      "taxExclusiveTotalPrice",
      "warnNum",
    ].forEach(normalizeProductNumber);
  };
  const mathNum = () => {
    normalizeProductNumbers();
    if (!productForm.value.taxRate) {
      proxy.$modal.msgWarning("请先选择税率");
      return;
@@ -1815,23 +1855,26 @@
      return;
    }
    // 含税总价计算
    productForm.value.taxInclusiveTotalPrice =
    productForm.value.taxInclusiveTotalPrice = toDisplayNumber(
      proxy.calculateTaxIncludeTotalPrice(
        productForm.value.taxInclusiveUnitPrice,
        productForm.value.quantity,
        6
      )
      );
    if (productForm.value.taxRate) {
      // 不含税总价计算
      productForm.value.taxExclusiveTotalPrice =
      productForm.value.taxExclusiveTotalPrice = toDisplayNumber(
        proxy.calculateTaxExclusiveTotalPrice(
          productForm.value.taxInclusiveTotalPrice,
          productForm.value.taxRate,
          6
        )
        );
    }
  };
  const reverseMathNum = field => {
    normalizeProductNumbers();
    if (!productForm.value.taxRate) {
      proxy.$modal.msgWarning("请先选择税率");
      return;
@@ -1846,7 +1889,7 @@
    ) {
      const value = Number(productForm.value[field]);
      if (value < 0) {
        productForm.value[field] = "0";
        productForm.value[field] = 0;
        proxy.$modal.msgWarning("值不能小于0");
        return;
      }
@@ -1855,65 +1898,63 @@
    if (field === "taxInclusiveTotalPrice") {
      // 已知含税总价和数量,反算含税单价
      if (productForm.value.quantity) {
        productForm.value.taxInclusiveUnitPrice = (
        productForm.value.taxInclusiveUnitPrice = toDisplayNumber(
          Number(productForm.value.taxInclusiveTotalPrice) /
          Number(productForm.value.quantity)
        ).toFixed(6);
        );
        // 确保结果不为负数
        if (Number(productForm.value.taxInclusiveUnitPrice) < 0) {
          productForm.value.taxInclusiveUnitPrice = "0";
          productForm.value.taxInclusiveUnitPrice = 0;
        }
      }
      // 已知含税总价和含税单价,反算数量
      else if (productForm.value.taxInclusiveUnitPrice) {
        productForm.value.quantity = (
        productForm.value.quantity = toDisplayNumber(
          Number(productForm.value.taxInclusiveTotalPrice) /
          Number(productForm.value.taxInclusiveUnitPrice)
        ).toFixed(6);
        );
        // 确保结果不为负数
        if (Number(productForm.value.quantity) < 0) {
          productForm.value.quantity = "0";
          productForm.value.quantity = 0;
        }
      }
      // 反算不含税总价
      productForm.value.taxExclusiveTotalPrice = (
        Number(productForm.value.taxInclusiveTotalPrice) /
        (1 + taxRate / 100)
      ).toFixed(6);
      productForm.value.taxExclusiveTotalPrice = toDisplayNumber(
        Number(productForm.value.taxInclusiveTotalPrice) / (1 + taxRate / 100)
      );
      // 确保结果不为负数
      if (Number(productForm.value.taxExclusiveTotalPrice) < 0) {
        productForm.value.taxExclusiveTotalPrice = "0";
        productForm.value.taxExclusiveTotalPrice = 0;
      }
    } else if (field === "taxExclusiveTotalPrice") {
      // 反算含税总价
      productForm.value.taxInclusiveTotalPrice = (
        Number(productForm.value.taxExclusiveTotalPrice) *
        (1 + taxRate / 100)
      ).toFixed(6);
      productForm.value.taxInclusiveTotalPrice = toDisplayNumber(
        Number(productForm.value.taxExclusiveTotalPrice) * (1 + taxRate / 100)
      );
      // 确保结果不为负数
      if (Number(productForm.value.taxInclusiveTotalPrice) < 0) {
        productForm.value.taxInclusiveTotalPrice = "0";
        productForm.value.taxInclusiveTotalPrice = 0;
      }
      // 已知数量,反算含税单价
      if (productForm.value.quantity) {
        productForm.value.taxInclusiveUnitPrice = (
        productForm.value.taxInclusiveUnitPrice = toDisplayNumber(
          Number(productForm.value.taxInclusiveTotalPrice) /
          Number(productForm.value.quantity)
        ).toFixed(6);
        );
        // 确保结果不为负数
        if (Number(productForm.value.taxInclusiveUnitPrice) < 0) {
          productForm.value.taxInclusiveUnitPrice = "0";
          productForm.value.taxInclusiveUnitPrice = 0;
        }
      }
      // 已知含税单价,反算数量
      else if (productForm.value.taxInclusiveUnitPrice) {
        productForm.value.quantity = (
        productForm.value.quantity = toDisplayNumber(
          Number(productForm.value.taxInclusiveTotalPrice) /
          Number(productForm.value.taxInclusiveUnitPrice)
        ).toFixed(6);
        );
        // 确保结果不为负数
        if (Number(productForm.value.quantity) < 0) {
          productForm.value.quantity = "0";
          productForm.value.quantity = 0;
        }
      }
    }