6 天以前 91c82965b2d987452ca276e3a03b48c8dcda2ae9
src/views/erp/sale/return/modules/item-form.vue
@@ -1,4 +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';
@@ -7,12 +9,12 @@
  erpCountInputFormatter,
  erpPriceInputFormatter,
  erpPriceMultiply,
} from '../../../../../packages/utils/src';
} from '@vben/utils';
import { Input, InputNumber, Select } from 'ant-design-vue';
import { TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import { getProductSimpleList } from '#/api/erp/product/product';
import { getItemPage } from '#/api/mdm/item';
import { getWarehouseStockCount } from '#/api/erp/stock/stock';
import { getWarehouseSimpleList } from '#/api/erp/stock/warehouse';
@@ -23,6 +25,7 @@
  disabled?: boolean;
  discountPercent?: number;
  otherPrice?: number;
  orderItems?: ErpSaleOrderApi.SaleOrderItem[];
}
const props = withDefaults(defineProps<Props>(), {
@@ -30,6 +33,7 @@
  disabled: false,
  discountPercent: 0,
  otherPrice: 0,
  orderItems: () => [],
});
const emit = defineEmits([
@@ -40,7 +44,7 @@
]);
const tableData = ref<ErpSaleReturnApi.SaleReturnItem[]>([]); // 表格数据
const productOptions = ref<any[]>([]); // 产品下拉选项
const productOptions = ref<MdmItemApi.Item[]>([]); // 物料下拉选项
const warehouseOptions = ref<any[]>([]); // 仓库下拉选项
/** 获取表格合计数据 */
@@ -140,6 +144,57 @@
  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) {
  // 优先从关联订单的物料中查找
  const orderItem = props.orderItems.find(
    (item) => item.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;
  }
  // 兜底:从全部物料中查找
  const product = productOptions.value.find((item) => item.id === row.productId);
  if (product) {
    row.productName = product.name || '';
    row.productPrice = product.salesPrice ?? 0;
    row.productUnitId = product.unitMeasureId;
    row.productUnitName = product.unitMeasureName;
    row.productBarCode = product.barCode;
    row.taxPercent = 0;
    row.count = row.count || 1;
    handleRowChange(row);
  }
}
/** 处理仓库变更 */
async function handleWarehouseChange(row: ErpSaleReturnApi.SaleReturnItem) {
  const stockCount = await getWarehouseStockCount({
@@ -176,6 +231,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} 行:仓库不能为空`);
      }
@@ -192,7 +250,8 @@
/** 初始化 */
onMounted(async () => {
  productOptions.value = await getProductSimpleList();
  const res = await getItemPage({ pageNo: 1, pageSize: 100, status: 0 });
  productOptions.value = res.list || [];
  warehouseOptions.value = await getWarehouseSimpleList();
});
</script>
@@ -213,13 +272,14 @@
    </template>
    <template #productId="{ row }">
      <Select
        disabled
        :disabled="disabled"
        v-model:value="row.productId"
        :options="productOptions"
        :field-names="{ label: 'name', value: 'id' }"
        :options="orderItems?.length ? orderItems : productOptions"
        :field-names="orderItems?.length ? { label: 'productName', value: 'productId' } : { label: 'name', value: 'id' }"
        class="w-full"
        placeholder="请选择产品"
        show-search
        @change="handleProductChange(row)"
      />
    </template>
    <template #count="{ row }">
@@ -289,6 +349,17 @@
          </div>
        </div>
      </div>
      <TableAction
        v-if="!disabled"
        class="mt-2 flex justify-center"
        :actions="[
          {
            label: '添加退货产品',
            type: 'default',
            onClick: handleAdd,
          },
        ]"
      />
    </template>
  </Grid>
</template>