gaoluyang
8 天以前 d841a8ea6dc2f7fdedbf0ea2b1e539842f5f3e37
src/views/erp/purchase/order/modules/item-form.vue
@@ -1,5 +1,5 @@
<script lang="ts" setup>
import type { ErpProductApi } from '#/api/erp/product/product';
import type { MdmItemApi } from '#/api/mdm/item';
import type { ErpPurchaseOrderApi } from '#/api/erp/purchase/order';
import { computed, nextTick, onMounted, ref, watch } from 'vue';
@@ -8,13 +8,12 @@
  erpCountInputFormatter,
  erpPriceInputFormatter,
  erpPriceMultiply,
} from '..\..\..\..\..\packages\utils\src';
} from '@vben/utils';
import { Input, InputNumber, Select } from 'ant-design-vue';
import { Input, InputNumber, Select, Switch } from 'ant-design-vue';
import { TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import { getProductSimpleList } from '#/api/erp/product/product';
import { getStockCount } from '#/api/erp/stock/stock';
import { getItemPage } from '#/api/mdm/item';
import { useFormItemColumns } from '../data';
@@ -37,7 +36,7 @@
]);
const tableData = ref<ErpPurchaseOrderApi.PurchaseOrderItem[]>([]); // 表格数据
const productOptions = ref<ErpProductApi.Product[]>([]); // 产品下拉选项
const productOptions = ref<MdmItemApi.Item[]>([]); // 产品下拉选项
/** 获取表格合计数据 */
const summaries = computed(() => {
@@ -124,16 +123,15 @@
  const newRow = {
    id: undefined,
    productId: undefined,
    productUnitName: undefined, // 产品单位
    productBarCode: undefined, // 产品条码
    productUnitName: undefined,
    productPrice: undefined,
    stockCount: undefined,
    count: 1,
    totalProductPrice: undefined,
    taxPercent: 0,
    taxPrice: undefined,
    totalPrice: undefined,
    remark: undefined,
    qcCheckFlag: false,
  };
  tableData.value.push(newRow);
  // 通知父组件更新
@@ -158,11 +156,9 @@
    return;
  }
  row.productId = productId;
  row.productUnitId = product.unitId;
  row.productBarCode = product.barCode;
  row.productUnitName = product.unitName;
  row.productUnitId = product.unitMeasureId;
  row.productUnitName = product.unitMeasureName;
  row.productName = product.name;
  row.stockCount = (await getStockCount(productId)) || 0;
  row.productPrice = product.purchasePrice || 0;
  row.count = row.count || 1;
  handleRowChange(row);
@@ -170,6 +166,7 @@
/** 处理行数据变更 */
function handleRowChange(row: any) {
  recalcRow(row);
  const index = tableData.value.findIndex((item) => item.seq === row.seq);
  if (index === -1) {
    tableData.value.push(row);
@@ -179,13 +176,28 @@
  emit('update:items', [...tableData.value]);
}
/** 初始化行数据 */
function initRow(row: ErpPurchaseOrderApi.PurchaseOrderItem) {
/**
 * 重算行金额(单价为**含税**单价)
 * 含税金额 = 含税单价 × 数量;税额 = 含税金额 × 税率;不含税金额 = 含税金额 - 税额
 */
function recalcRow(row: ErpPurchaseOrderApi.PurchaseOrderItem) {
  if (row.productPrice && row.count) {
    row.totalProductPrice = erpPriceMultiply(row.productPrice, row.count) ?? 0;
    const taxInclusivePrice = erpPriceMultiply(row.productPrice, row.count) ?? 0;
    row.taxPrice =
      erpPriceMultiply(row.totalProductPrice, (row.taxPercent || 0) / 100) ?? 0;
    row.totalPrice = row.totalProductPrice + row.taxPrice;
      erpPriceMultiply(taxInclusivePrice, (row.taxPercent || 0) / 100) ?? 0;
    row.totalProductPrice = taxInclusivePrice - row.taxPrice;
    row.totalPrice = taxInclusivePrice;
  }
}
/**
 * 初始化行数据
 * 已有单据的明细行(带 id)沿用后端返回的金额,避免历史单据被新口径覆盖;
 * 只有未保存的新增行才按新口径重算。
 */
function initRow(row: ErpPurchaseOrderApi.PurchaseOrderItem) {
  if (!row.id) {
    recalcRow(row);
  }
}
@@ -213,7 +225,8 @@
/** 初始化 */
onMounted(async () => {
  productOptions.value = await getProductSimpleList();
  const res = await getItemPage({ pageNo: 1, pageSize: 100, status: 0 });
  productOptions.value = res.list || [];
  // 目的:新增时,默认添加一行
  if (tableData.value.length === 0) {
    handleAdd();
@@ -225,6 +238,7 @@
  <Grid class="w-full">
    <template #productId="{ row }">
      <Select
        v-if="!disabled"
        v-model:value="row.productId"
        :options="productOptions"
        :field-names="{ label: 'name', value: 'id' }"
@@ -233,6 +247,7 @@
        show-search
        @change="handleProductChange($event, row)"
      />
      <span v-else>{{ row.productName || '-' }}</span>
    </template>
    <template #count="{ row }">
      <InputNumber
@@ -257,6 +272,16 @@
    <template #remark="{ row }">
      <Input v-if="!disabled" v-model:value="row.remark" class="w-full" />
      <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 #taxPercent="{ row }">
      <InputNumber
@@ -292,11 +317,11 @@
          <div class="flex space-x-4">
            <span>数量:{{ erpCountInputFormatter(summaries.count) }}</span>
            <span>
              金额:{{ erpPriceInputFormatter(summaries.totalProductPrice) }}
              不含税金额:{{ erpPriceInputFormatter(summaries.totalProductPrice) }}
            </span>
            <span>税额:{{ erpPriceInputFormatter(summaries.taxPrice) }}</span>
            <span>
              税额合计:{{ erpPriceInputFormatter(summaries.totalPrice) }}
              含税合计:{{ erpPriceInputFormatter(summaries.totalPrice) }}
            </span>
          </div>
        </div>