gaoluyang
6 天以前 d841a8ea6dc2f7fdedbf0ea2b1e539842f5f3e37
src/views/erp/purchase/request/modules/item-form.vue
@@ -1,5 +1,4 @@
<script lang="ts" setup>
import type { MdmItemApi } from '#/api/mdm/item';
import type { ErpPurchaseRequestApi } from '#/api/erp/purchase/request';
import { computed, nextTick, onMounted, ref, watch } from 'vue';
@@ -8,12 +7,12 @@
  erpCountInputFormatter,
  erpPriceInputFormatter,
  erpPriceMultiply,
} from '../../../../../packages/utils/src';
} from '@vben/utils';
import { Input, InputNumber, Select, DatePicker } from 'ant-design-vue';
import { Input, InputNumber, DatePicker, Switch } from 'ant-design-vue';
import { TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import { getItemPage } from '#/api/mdm/item';
import { MdmItemSelect } from '#/views/basicData/mdm/components';
import { useFormItemColumns } from '../data';
@@ -30,21 +29,23 @@
const emit = defineEmits(['update:items']);
const tableData = ref<ErpPurchaseRequestApi.PurchaseRequestItem[]>([]);
const productOptions = ref<MdmItemApi.Item[]>([]);
/** 获取表格合计数据 */
/** 获取表格合计数据(行上 totalPrice 为不含税金额,含税合计需加上税额) */
const summaries = computed(() => {
  return {
    count: tableData.value.reduce((sum, item) => sum + (item.count || 0), 0),
    totalPrice: tableData.value.reduce(
      (sum, item) => {
        const price = erpPriceMultiply(item.productPrice || 0, item.count || 0) ?? 0;
        return sum + price;
      },
      (sum, item) => sum + (item.totalPrice || 0),
      0,
    ),
    taxPrice: tableData.value.reduce((sum, item) => sum + (item.taxPrice || 0), 0),
  };
});
/** 含税合计 = 不含税金额 + 税额 */
const totalTaxInclusivePrice = computed(
  () => summaries.value.totalPrice + summaries.value.taxPrice,
);
/** 表格配置 */
const [Grid, gridApi] = useVbenVxeGrid({
@@ -98,6 +99,7 @@
    demandTime: undefined,
    remark: undefined,
    totalPrice: 0,
    qcCheckFlag: false,
  };
  tableData.value.push(newRow);
  emit('update:items', [...tableData.value]);
@@ -113,12 +115,11 @@
}
/** 处理产品变更 */
async function handleProductChange(productId: any, row: any) {
  const product = productOptions.value.find((p) => p.id === productId);
function handleProductChange(product: any, row: any) {
  if (!product) {
    return;
  }
  row.productId = productId;
  row.productId = product.id;
  row.productBarCode = product.barCode;
  row.productUnitId = product.unitMeasureId;
  row.productUnitName = product.unitMeasureName;
@@ -128,25 +129,39 @@
  handleRowChange(row);
}
/**
 * 重算行金额(单价为**含税**单价)
 * 含税金额 = 含税单价 × 数量;税额 = 含税金额 × 税率;不含税金额 = 含税金额 - 税额
 */
function recalcRow(row: ErpPurchaseRequestApi.PurchaseRequestItem) {
  if (row.productPrice && row.count) {
    const taxInclusivePrice = erpPriceMultiply(row.productPrice, row.count) ?? 0;
    row.taxPrice =
      erpPriceMultiply(taxInclusivePrice, (row.taxPercent || 0) / 100) ?? 0;
    row.totalPrice = taxInclusivePrice - row.taxPrice;
  }
}
/** 处理行数据变更 */
function handleRowChange(row: any) {
  recalcRow(row);
  const index = tableData.value.findIndex((item) => item.seq === row.seq);
  if (index === -1) {
    tableData.value.push(row);
  } else {
    tableData.value[index] = row;
  }
  // 计算预估金额
  if (row.productPrice && row.count) {
    row.totalPrice = erpPriceMultiply(row.productPrice, row.count) ?? 0;
  }
  emit('update:items', [...tableData.value]);
}
/** 初始化行数据 */
/**
 * 初始化行数据
 * 已有单据的明细行(带 id)沿用后端返回的金额,避免历史单据被新口径覆盖;
 * 只有未保存的新增行才按新口径重算。
 */
function initRow(row: ErpPurchaseRequestApi.PurchaseRequestItem) {
  if (row.productPrice && row.count) {
    row.totalPrice = erpPriceMultiply(row.productPrice, row.count) ?? 0;
  if (!row.id) {
    recalcRow(row);
  }
}
@@ -170,9 +185,7 @@
});
/** 初始化 */
onMounted(async () => {
  const res = await getItemPage({ pageNo: 1, pageSize: 100, status: 0 });
  productOptions.value = res.list || [];
onMounted(() => {
  if (tableData.value.length === 0) {
    handleAdd();
  }
@@ -182,14 +195,11 @@
<template>
  <Grid class="w-full">
    <template #productId="{ row }">
      <Select
      <MdmItemSelect
        v-if="!disabled"
        v-model:value="row.productId"
        :options="productOptions"
        :field-names="{ label: 'name', value: 'id' }"
        class="w-full"
        :model-value="row.productId"
        placeholder="请选择产品"
        show-search
        @update:model-value="row.productId = $event"
        @change="handleProductChange($event, row)"
      />
      <span v-else>{{ row.productName || '-' }}</span>
@@ -225,6 +235,9 @@
      />
      <span v-else>{{ row.taxPercent ? `${row.taxPercent}%` : '-' }}</span>
    </template>
    <template #totalTaxInclusivePrice="{ row }">
      <span>{{ erpPriceInputFormatter((row.totalPrice || 0) + (row.taxPrice || 0)) }}</span>
    </template>
    <template #demandTime="{ row }">
      <DatePicker
        v-if="!disabled"
@@ -238,6 +251,16 @@
    <template #remark="{ row }">
      <Input v-if="!disabled" v-model:value="row.remark" class="w-full" @change="handleRowChange(row)" />
      <span v-else>{{ row.remark || '-' }}</span>
    </template>
    <template #qcCheckFlag="{ row }">
      <Switch
        v-if="!disabled"
        v-model:checked="row.qcCheckFlag"
        checked-children="是"
        un-checked-children="否"
        @change="handleRowChange(row)"
      />
      <span v-else>{{ row.qcCheckFlag ? '是' : '否' }}</span>
    </template>
    <template #actions="{ row }">
      <TableAction
@@ -261,7 +284,9 @@
          <span class="font-medium text-foreground">合计:</span>
          <div class="flex space-x-4">
            <span>数量:{{ erpCountInputFormatter(summaries.count) }}</span>
            <span>预估金额:{{ erpPriceInputFormatter(summaries.totalPrice) }}</span>
            <span>不含税金额:{{ erpPriceInputFormatter(summaries.totalPrice) }}</span>
            <span>税额:{{ erpPriceInputFormatter(summaries.taxPrice) }}</span>
            <span>含税合计:{{ erpPriceInputFormatter(totalTaxInclusivePrice) }}</span>
          </div>
        </div>
      </div>