gaoluyang
7 天以前 e4994fccfb49ebfb108ba62cbd82aea4e004f6d8
src/views/erp/sale/return/modules/item-form.vue
@@ -1,5 +1,6 @@
<script lang="ts" setup>
import type { MdmItemApi } from '#/api/mdm/item';
import type { ErpSaleOrderApi } from '#/api/erp/sale/order';
import type { ErpSaleReturnApi } from '#/api/erp/sale/return';
import { computed, nextTick, onMounted, ref, watch } from 'vue';
@@ -13,9 +14,9 @@
import { Input, InputNumber, Select } from 'ant-design-vue';
import { TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import { getItemPage } from '#/api/mdm/item';
import { getWarehouseStockCount } from '#/api/erp/stock/stock';
import { getWarehouseSimpleList } from '#/api/erp/stock/warehouse';
import { MdmItemSelect } from '#/views/basicData/mdm/components';
import { useFormItemColumns } from '../data';
@@ -24,6 +25,7 @@
  disabled?: boolean;
  discountPercent?: number;
  otherPrice?: number;
  orderItems?: ErpSaleOrderApi.SaleOrderItem[];
}
const props = withDefaults(defineProps<Props>(), {
@@ -31,6 +33,7 @@
  disabled: false,
  discountPercent: 0,
  otherPrice: 0,
  orderItems: () => [],
});
const emit = defineEmits([
@@ -41,7 +44,6 @@
]);
const tableData = ref<ErpSaleReturnApi.SaleReturnItem[]>([]); // 表格数据
const productOptions = ref<MdmItemApi.Item[]>([]); // 物料下拉选项
const warehouseOptions = ref<any[]>([]); // 仓库下拉选项
/** 获取表格合计数据 */
@@ -141,6 +143,58 @@
  emit('update:items', [...tableData.value]);
}
/** 处理新增物料行 */
function handleAdd() {
  const newRow: ErpSaleReturnApi.SaleReturnItem = {
    seq: Date.now(),
    productId: undefined,
    productName: '',
    productPrice: 0,
    productUnitId: undefined,
    productUnitName: '',
    count: 0,
    totalProductPrice: 0,
    taxPercent: 0,
    taxPrice: 0,
    totalPrice: 0,
    remark: '',
  };
  tableData.value.push(newRow);
  emit('update:items', [...tableData.value]);
}
/** 处理产品变更:优先从关联订单物料中填充,兜底使用弹框选中的物料 */
function handleProductChange(
  row: ErpSaleReturnApi.SaleReturnItem,
  item?: MdmItemApi.Item,
) {
  // 优先从关联订单的物料中查找
  const orderItem = props.orderItems.find(
    (order) => order.productId === row.productId,
  );
  if (orderItem) {
    row.productName = orderItem.productName;
    row.productPrice = orderItem.productPrice ?? 0;
    row.productUnitId = orderItem.productUnitId;
    row.productUnitName = orderItem.productUnitName;
    row.productBarCode = orderItem.productBarCode;
    row.taxPercent = orderItem.taxPercent ?? 0;
    row.count = row.count || 1;
    handleRowChange(row);
    return;
  }
  if (item) {
    row.productName = item.name || '';
    row.productPrice = item.salesPrice ?? 0;
    row.productUnitId = item.unitMeasureId;
    row.productUnitName = item.unitMeasureName;
    row.productBarCode = item.barCode;
    row.taxPercent = 0;
    row.count = row.count || 1;
    handleRowChange(row);
  }
}
/** 处理仓库变更 */
async function handleWarehouseChange(row: ErpSaleReturnApi.SaleReturnItem) {
  const stockCount = await getWarehouseStockCount({
@@ -153,6 +207,7 @@
/** 处理行数据变更 */
function handleRowChange(row: any) {
  recalcRow(row);
  const index = tableData.value.findIndex((item) => item.seq === row.seq);
  if (index === -1) {
    tableData.value.push(row);
@@ -162,13 +217,28 @@
  emit('update:items', [...tableData.value]);
}
/** 初始化行数据 */
function initRow(row: ErpSaleReturnApi.SaleReturnItem) {
/**
 * 重算行金额(单价为**含税**单价)
 * 含税金额 = 含税单价 × 数量;税额 = 含税金额 × 税率;不含税金额 = 含税金额 - 税额
 */
function recalcRow(row: ErpSaleReturnApi.SaleReturnItem) {
  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: ErpSaleReturnApi.SaleReturnItem) {
  if (!row.id) {
    recalcRow(row);
  }
}
@@ -177,6 +247,9 @@
  for (let i = 0; i < tableData.value.length; i++) {
    const item = tableData.value[i];
    if (item) {
      if (!item.productId) {
        throw new Error(`第 ${i + 1} 行:产品不能为空`);
      }
      if (!item.warehouseId) {
        throw new Error(`第 ${i + 1} 行:仓库不能为空`);
      }
@@ -193,8 +266,6 @@
/** 初始化 */
onMounted(async () => {
  const res = await getItemPage({ pageNo: 1, pageSize: 100, status: 0 });
  productOptions.value = res.list || [];
  warehouseOptions.value = await getWarehouseSimpleList();
});
</script>
@@ -215,13 +286,23 @@
    </template>
    <template #productId="{ row }">
      <Select
        disabled
        v-if="orderItems?.length"
        :disabled="disabled"
        v-model:value="row.productId"
        :options="productOptions"
        :field-names="{ label: 'name', value: 'id' }"
        :options="orderItems"
        :field-names="{ label: 'productName', value: 'productId' }"
        class="w-full"
        placeholder="请选择产品"
        show-search
        @change="handleProductChange(row)"
      />
      <MdmItemSelect
        v-else
        v-model:model-value="row.productId"
        :label="row.productName"
        :disabled="disabled"
        placeholder="请选择产品"
        @change="(item) => handleProductChange(row, item)"
      />
    </template>
    <template #count="{ row }">
@@ -282,15 +363,26 @@
          <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>
      </div>
      <TableAction
        v-if="!disabled"
        class="mt-2 flex justify-center"
        :actions="[
          {
            label: '添加退货产品',
            type: 'default',
            onClick: handleAdd,
          },
        ]"
      />
    </template>
  </Grid>
</template>