src/views/procurementManagement/purchaseReturnOrder/New.vue
@@ -3,8 +3,10 @@
    <el-dialog
        v-model="isShow"
        title="新增采购退货"
        width="1600"
        width="70%"
        top="3vh"
        @close="closeModal"
        class="purchase-return-dialog"
    >
      <el-form label-width="140px" :model="formState" label-position="top" ref="formRef" :inline="true">
        <div class="section-title">
@@ -56,6 +58,63 @@
        </el-form-item>
        <el-form-item
            label="发货类型"
            prop="shippingType"
            :rules="[
                {
                  required: true,
                  message: '请选择发货类型',
                  trigger: 'change',
                }
              ]"
        >
          <el-select
              v-model="formState.shippingType"
              placeholder="请选择发货类型"
              style="width: 240px"
              @change="handleShippingTypeChange"
          >
            <el-option label="货车" :value="1" />
            <el-option label="快递" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item
            v-if="formState.shippingType === 1"
            label="发货车牌号"
            prop="truckPlateNo"
        >
          <el-input
              v-model="formState.truckPlateNo"
              placeholder="请输入发货车牌号"
              style="width: 240px"
          />
        </el-form-item>
        <template v-else-if="formState.shippingType === 2">
          <el-form-item
              label="快递公司"
              prop="expressCompany"
          >
            <el-input
                v-model="formState.expressCompany"
                placeholder="请输入快递公司"
                style="width: 240px"
            />
          </el-form-item>
          <el-form-item
              label="快递单号"
              prop="expressNo"
          >
            <el-input
                v-model="formState.expressNo"
                placeholder="请输入快递单号"
                style="width: 240px"
            />
          </el-form-item>
        </template>
        <el-form-item
            label="供应商名称"
            prop="supplierId"
            :rules="[
@@ -70,6 +129,7 @@
              v-model="formState.supplierId"
              placeholder="请选择供应商"
              style="width: 240px"
                     filterable
              @focus="fetchSupplierOptions"
              @change="handleChangeSupplierId"
          >
@@ -220,18 +280,19 @@
          <el-input style="width: 240px" v-model="formState.remark" :rows="1" type="textarea" placeholder="请输入备注"/>
        </el-form-item>
        <div style="margin: 20px 0;">
        <div style="margin:20px 0;min-width:0;">
            <div class="section-title">
              <span class="title-dot"></span>
              <span class="title-text">产品列表</span>
            </div>
            <el-button type="primary" size="small" style="margin-bottom: 20px" @click="isShowProductsModal = true" :disabled="!formState.purchaseLedgerId">添加产品</el-button>
            <el-table :data="formState.purchaseReturnOrderProductsDtos"
            <el-button type="primary" size="small" style="margin-bottom:20px" @click="isShowProductsModal = true" :disabled="!formState.purchaseLedgerId">添加产品</el-button>
            <el-table
                      :data="formState.purchaseReturnOrderProductsDtos"
                      border
                      max-height="400"
                      :scroll-y="true"
                      show-summary
                      :summary-method="summarizeChildrenTable">
                      :summary-method="summarizeChildrenTable"
                      style="width:100%;min-width:0;">
              <el-table-column align="center"
                               type="selection"
                               width="55" />
@@ -239,6 +300,12 @@
                               label="序号"
                               type="index"
                               width="60" />
              <el-table-column label="入库单号"
                               prop="inboundBatches"
                               width="150" />
              <el-table-column label="批次号"
                               prop="batchNo"
                               width="150" />
              <el-table-column label="产品大类"
                               prop="productCategory" />
              <el-table-column label="规格型号"
@@ -247,11 +314,20 @@
                               prop="unit"
                               width="70" />
              <el-table-column label="数量"
                               prop="quantity"
                               prop="stockInNum"
                               width="100" />
              <el-table-column label="销售发货数量"
                               prop="saleOutQuantity"
                               width="120" />
                               <el-table-column label="可退货数量"
                               prop="availableQuality"
                               prop="unQuantity"
                               width="130" />
              <el-table-column label="已退货数量"
                               width="130">
                <template #default="scope">
                  {{ formattedNumber(scope.row, null, scope.row.totalReturnNum || 0) }}
                </template>
              </el-table-column>
              <el-table-column label="退货数量"
                               prop="returnQuantity"
                               width="180">
@@ -267,27 +343,27 @@
                            placeholder="请输入退货数量" />
                </template>
              </el-table-column>
              <el-table-column label="库存预警数量"
              <!-- <el-table-column label="库存预警数量"
                               prop="warnNum"
                               width="120"
                               show-overflow-tooltip />
              <el-table-column label="税率(%)"
                               prop="taxRate"
                               width="80" />
                               width="80" /> -->
              <el-table-column label="含税单价(元)"
                               prop="taxInclusiveUnitPrice"
                               :formatter="formattedNumber"
                               width="150" />
                               width="120" />
              <el-table-column label="退货总价(元)"
                               prop="taxInclusiveTotalPrice"
                               width="180">
                               width="120">
                <template #default="scope">
                  {{ formatAmount(getReturnTotal(scope.row)) || '--' }}
                </template>
              </el-table-column>
              <el-table-column label="是否质检"
                               prop="isChecked"
                               width="150">
                               width="100">
                <template #default="scope">
                  <el-tag :type="scope.row.isChecked ? 'success' : 'info'">
                    {{ scope.row.isChecked ? '是' : '否' }}
@@ -334,12 +410,17 @@
            label="整单折扣率:"
            prop="totalDiscountAmount"
        >
          <el-input-number v-model="formState.totalDiscountRate"
          <el-input v-model="formState.totalDiscountRate"
                           controls-position="right"
                           :step="0.01"
                           :precision="2"
                           style="width: 100%;"
                           placeholder="请输入整单折扣率"/>
                           @change="totalDiscount"
                           placeholder="请输入整单折扣率">
            <template #append>
                %
            </template>
          </el-input>
        </el-form-item>
        <el-form-item
@@ -399,12 +480,9 @@
<script setup>
import {ref, computed, getCurrentInstance, watch, defineAsyncComponent} from "vue";
import {createPurchaseReturnOrder} from "@/api/procurementManagement/purchase_return_order.js";
import {getOptions, purchaseList} from "@/api/procurementManagement/procurementLedger.js";
import {getOptions, purchaseReturnableList} from "@/api/procurementManagement/procurementLedger.js";
import {userListNoPageByTenantId} from "@/api/system/user.js";
const ProductList = defineAsyncComponent(() => import("@/views/procurementManagement/purchaseReturnOrder/ProductList.vue"));
  import {
    productList,
  } from "@/api/procurementManagement/procurementLedger.js";
const props = defineProps({
  visible: {
    type: Boolean,
@@ -412,7 +490,24 @@
  }
});
let { proxy } = getCurrentInstance()
const { payment_methods } = proxy.useDict("payment_methods");
const payment_methods  = [
    {
        "label": "现金",
        "value": "0",
    },
    {
        "label": "支票",
        "value": "1",
    },
    {
        "label": "银行转账",
        "value": "2",
    },
    {
        "label": "其他",
        "value": "3",
    },
]
const emit = defineEmits(['update:visible', 'completed']);
// 响应式数据(替代选项式的 data)
@@ -420,6 +515,10 @@
  no: '',
  isDefaultNo: true,
  returnType: 0,
  shippingType: undefined,
  truckPlateNo: '',
  expressCompany: '',
  expressNo: '',
  incomeType: undefined,
  remark: '',
  supplierId: undefined,
@@ -466,6 +565,19 @@
// 是否展示产品列表数据
const isShowProductsModal = ref(false)
const handleShippingTypeChange = (val) => {
  if (val === 1) {
    formState.value.expressCompany = '';
    formState.value.expressNo = '';
  } else if (val === 2) {
    formState.value.truckPlateNo = '';
  } else {
    formState.value.truckPlateNo = '';
    formState.value.expressCompany = '';
    formState.value.expressNo = '';
  }
};
const isShow = computed({
  get() {
    return props.visible;
@@ -509,8 +621,28 @@
  row.taxInclusiveTotalPrice = getReturnTotal(row)
}
const getBaseAmount = () => {
  const rows = formState.value.purchaseReturnOrderProductsDtos || []
  return rows.reduce((sum, item) => {
    return sum + toNumber(item.taxInclusiveTotalPrice)
  }, 0)
}
// 同步折扣额
const totalDiscount = () => {
  const discountRate = toNumber(formState.value.totalDiscountRate)
  if (discountRate < 0 || discountRate > 100) {
    proxy.$modal.msgError("请输入0-100之间的折扣率")
    return
  }
  const baseAmount = getBaseAmount()
  // 折扣额 = 产品退货总价合计 * 折扣率
  formState.value.totalDiscountAmount = Number((baseAmount * (discountRate / 100)).toFixed(2))
  syncTotalAmount()
}
const getReturnQtyMax = (row) => {
  const max = Number(row?.availableQuality)
  const max = Number(row?.unQuantity)
  if (Number.isNaN(max) || max < 0) {
    return 0
  }
@@ -518,6 +650,10 @@
}
const closeModal = () => {
  formState.value.shippingType = undefined;
  formState.value.truckPlateNo = '';
  formState.value.expressCompany = '';
  formState.value.expressNo = '';
  isShow.value = false;
};
@@ -525,30 +661,55 @@
  return proxy.summarizeTable(
      param,
      [
        "quantity",
        "availableQuality",
        "stockInNum",
        "unQuantity",
        "returnQuantity",
        "taxInclusiveUnitPrice",
        "taxInclusiveTotalPrice",
        "taxExclusiveTotalPrice",
      ],
      {
        quantity: { noDecimal: true }, // 不保留小数
        stockInNum: { noDecimal: true }, // 不保留小数
        returnQuantity: { noDecimal: true }, // 不保留小数
        availableQuality: { noDecimal: true }, // 不保留小数
        unQuantity: { noDecimal: true }, // 不保留小数
      }
  );
};
const handleChangeTotalDiscountAmount= () => {
  const discountAmount = toNumber(formState.value.totalDiscountAmount)
  if (discountAmount < 0) {
    proxy.$modal.msgError("整单折扣额不能小于0")
    formState.value.totalDiscountAmount = 0
  }
  const baseAmount = getBaseAmount()
  const normalizedAmount = toNumber(formState.value.totalDiscountAmount)
  if (baseAmount <= 0) {
    formState.value.totalDiscountRate = 0
    syncTotalAmount()
    return
  }
  if (normalizedAmount > baseAmount) {
    proxy.$modal.msgError("整单折扣额不能大于产品退货总价合计")
    formState.value.totalDiscountAmount = Number(baseAmount.toFixed(2))
  }
  const discountRate = (toNumber(formState.value.totalDiscountAmount) / baseAmount) * 100
  formState.value.totalDiscountRate = Number(discountRate.toFixed(2))
  syncTotalAmount()
}
const resetFeeInfo = () => {
  formState.value.totalDiscountAmount = 0
  formState.value.totalDiscountRate = undefined
  formState.value.totalAmount = 0
  formState.value.incomeType = undefined
}
const syncTotalAmount = () => {
  const rows = formState.value.purchaseReturnOrderProductsDtos || []
  const baseAmount = rows.reduce((sum, item) => {
    return sum + toNumber(item.taxInclusiveTotalPrice)
  }, 0)
  const baseAmount = getBaseAmount()
  const discount = toNumber(formState.value.totalDiscountAmount)
  // 成交金额 = 产品退货总价合计 - 折扣额
  formState.value.totalAmount = Number((baseAmount - discount).toFixed(2))
@@ -586,21 +747,16 @@
const fetchPurchaseLedgerOptions = () => {
  purchaseLedgerOptions.value = []
  if (formState.value.supplierId) {
    purchaseList({supplierId: formState.value.supplierId,approvalStatus:3}).then((res) => {
    purchaseReturnableList({ supplierId: formState.value.supplierId }).then((res) => {
      purchaseLedgerOptions.value = res.rows;
    });
  }
}
// 处理改变采购台账数据
const handleChangePurchaseLedgerId = async () => {
  const res = await productList({ salesLedgerId: formState.value.purchaseLedgerId, type: 2 });
  formState.value.purchaseReturnOrderProductsDtos = res.data.map(item => ({
    ...item,
    returnQuantity: undefined,
    taxInclusiveTotalPrice: 0,
    salesLedgerProductId: item.id,
  }))
// 处理改变采购台账数据(不请求接口回显产品,产品仅在「添加产品」弹窗勾选后写入)
const handleChangePurchaseLedgerId = () => {
  resetFeeInfo()
  formState.value.purchaseReturnOrderProductsDtos = []
  syncTotalAmount()
}
@@ -618,7 +774,7 @@
    ...item,
    returnQuantity: undefined,
    taxInclusiveTotalPrice: 0,
    salesLedgerProductId: item.id,
    // salesLedgerProductId: item.salesLedgerProductId,
  }));
  formState.value.purchaseReturnOrderProductsDtos.push(...newProducts);
  syncTotalAmount()
@@ -644,7 +800,7 @@
  // 逐行校验退货数量:任意一行未填/非法/超限都不允许提交
  const invalidRowIndex = productList.findIndex((item) => {
    const qty = Number(item.returnQuantity)
    const maxQty = Number(item.availableQuality)
    const maxQty = Number(item.unQuantity)
    if (item.returnQuantity === null || item.returnQuantity === undefined || item.returnQuantity === "") {
      return true
@@ -665,7 +821,15 @@
  proxy.$refs["formRef"].validate(valid => {
    if (valid) {
      createPurchaseReturnOrder(formState.value).then(res => {
      console.log(productList)
      const submitPayload = {
        ...formState.value,
        purchaseReturnOrderProductsDtos: productList.map((row) => ({
          ...row,
          stockInRecordId: row.id,
        })),
      }
      createPurchaseReturnOrder(submitPayload).then(res => {
        // 关闭模态框
        isShow.value = false;
        // 告知父组件已完成
@@ -712,4 +876,6 @@
  border-radius: 50%;
  margin-right: 8px;
}
</style>
</style>