调整结算模块
This commit is contained in:
@@ -102,9 +102,9 @@
|
||||
>
|
||||
<el-option
|
||||
v-for="item in feeCategoryOptions"
|
||||
:key="item.id || item.dictValue"
|
||||
:label="item.dictKey || item.dictValue"
|
||||
:value="item.dictValue"
|
||||
:key="item.id || item.dictKey"
|
||||
:label="item.dictValue || item.dictKey"
|
||||
:value="item.dictKey"
|
||||
/>
|
||||
</el-select>
|
||||
<el-select
|
||||
@@ -701,6 +701,7 @@
|
||||
:min="0"
|
||||
:precision="2"
|
||||
:controls="false"
|
||||
@change="recalculateAdjustRow(row, 'transportQuantity')"
|
||||
/>
|
||||
</template>
|
||||
</el-table-column>
|
||||
@@ -712,6 +713,7 @@
|
||||
:precision="2"
|
||||
:controls="false"
|
||||
:disabled="adjustDialog.readonly"
|
||||
@change="recalculateAdjustRow(row, 'mileage')"
|
||||
/>
|
||||
</template>
|
||||
</el-table-column>
|
||||
@@ -723,6 +725,7 @@
|
||||
:precision="2"
|
||||
:controls="false"
|
||||
:disabled="adjustDialog.readonly"
|
||||
@change="recalculateAdjustRow(row, 'unitPrice')"
|
||||
/>
|
||||
</template>
|
||||
</el-table-column>
|
||||
@@ -921,6 +924,7 @@ import {
|
||||
save,
|
||||
submit,
|
||||
} from '@/api/settlement/preSettlement';
|
||||
import { calculateAdjustedFee, getFeeDetail } from '@/api/settlement/receivable-payable-detail';
|
||||
import { getDictionary } from '@/api/system/dictbiz';
|
||||
import {
|
||||
emptyPreSettlementForm,
|
||||
@@ -968,10 +972,6 @@ export default {
|
||||
type: Object,
|
||||
default: null,
|
||||
},
|
||||
deferSave: {
|
||||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
},
|
||||
emits: ['update:modelValue', 'success'],
|
||||
data() {
|
||||
@@ -1071,10 +1071,13 @@ export default {
|
||||
readonly: false,
|
||||
activeTab: 'adjust',
|
||||
detailId: '',
|
||||
sourceDetailId: '',
|
||||
detailLineNo: '',
|
||||
reason: '',
|
||||
},
|
||||
adjustRows: [],
|
||||
pendingAdjustments: {},
|
||||
sourceFeeSnapshots: {},
|
||||
adjustChangeRecordVisible: false,
|
||||
adjustChangeRecord: null,
|
||||
adjustChangeRecordDetailRows: [],
|
||||
@@ -1149,24 +1152,11 @@ export default {
|
||||
return this.form.settlementType === 'receivable' ? '应收' : '应付';
|
||||
},
|
||||
summaryTotal() {
|
||||
// 与结算合计表格的合计行保持一致:包含手工添加的费用与明细调整后的金额。
|
||||
if (this.summaryFees.length) {
|
||||
return this.summaryFees.reduce(
|
||||
(total, row) => total + Number(row.settlementAmount || 0),
|
||||
0
|
||||
);
|
||||
return this.summaryFees.reduce((total, row) => total + Number(row.settlementAmount || 0), 0);
|
||||
}
|
||||
return this.details.reduce(
|
||||
(total, row) =>
|
||||
total +
|
||||
Number(
|
||||
row.settlementAmountTax ??
|
||||
row.totalAmount ??
|
||||
row.afterAmount ??
|
||||
row.settlementAmount ??
|
||||
0
|
||||
),
|
||||
0
|
||||
);
|
||||
return this.details.reduce((total, row) => total + this.detailSettlementAmount(row), 0);
|
||||
},
|
||||
visibleDetailColumns() {
|
||||
if (!this.readonly && !this.pageMode) return this.detailColumns;
|
||||
@@ -1226,9 +1216,9 @@ export default {
|
||||
await this.loadFeeCategoryOptions();
|
||||
await this.loadTransportTypeOptions();
|
||||
if (this.recordId) await this.loadDetail();
|
||||
else if (this.initialData) this.applyInitialData();
|
||||
else if (this.initialData) await this.applyInitialData();
|
||||
},
|
||||
applyInitialData() {
|
||||
async applyInitialData() {
|
||||
const rows = Array.isArray(this.initialData?.rows) ? this.initialData.rows : [];
|
||||
if (!rows.length) return;
|
||||
const first = rows[0];
|
||||
@@ -1264,25 +1254,55 @@ export default {
|
||||
localCurrency: first.localCurrency || 'RMB',
|
||||
});
|
||||
this.details = rows.map(row => this.normalizeSourceDetail(row));
|
||||
this.loading = true;
|
||||
try {
|
||||
await this.ensureSourceFeeSnapshots();
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
this.buildSummaryFeesFromDetails();
|
||||
},
|
||||
normalizeSourceDetail(row) {
|
||||
const originalAmount =
|
||||
row.originalAmount ?? row.totalAmount ?? row.afterAmount ?? row.settlementAmount ?? 0;
|
||||
const settlementAmount = this.detailSettlementAmount(row);
|
||||
const originalValue =
|
||||
row.originalAmount ?? row.originalSettlementAmount ?? row.totalAmount ?? settlementAmount;
|
||||
const originalAmount = Number(originalValue || 0);
|
||||
const adjustValue = row.adjustAmount ?? row.adjustmentAmount;
|
||||
const adjustAmount =
|
||||
adjustValue === undefined || adjustValue === null
|
||||
? settlementAmount - originalAmount
|
||||
: Number(adjustValue || 0);
|
||||
return {
|
||||
...row,
|
||||
sourceDetailId: row.sourceDetailId || row.id,
|
||||
originalAmount,
|
||||
adjustAmount: row.adjustAmount ?? 0,
|
||||
settlementAmountTax:
|
||||
row.settlementAmountTax ??
|
||||
row.totalAmount ??
|
||||
row.afterAmount ??
|
||||
row.settlementAmount ??
|
||||
originalAmount,
|
||||
originalAmount: Number(originalAmount.toFixed(2)),
|
||||
adjustAmount: Number(adjustAmount.toFixed(2)),
|
||||
settlementAmountTax: Number(settlementAmount.toFixed(2)),
|
||||
feeItemsJson: row.feeItemsJson || JSON.stringify(row.feeItems || {}),
|
||||
};
|
||||
},
|
||||
detailOriginalAmount(row) {
|
||||
return Number(row?.originalAmount ?? 0);
|
||||
},
|
||||
detailAdjustAmount(row) {
|
||||
const value = row?.adjustAmount ?? row?.adjustmentAmount;
|
||||
return Number(
|
||||
Number(
|
||||
value === undefined || value === null
|
||||
? this.detailSettlementAmount(row) - this.detailOriginalAmount(row)
|
||||
: value
|
||||
).toFixed(2)
|
||||
);
|
||||
},
|
||||
detailSettlementAmount(row) {
|
||||
return Number(
|
||||
row?.settlementAmountTax ??
|
||||
row?.totalAmount ??
|
||||
row?.afterAmount ??
|
||||
row?.settlementAmount ??
|
||||
0
|
||||
);
|
||||
},
|
||||
resetEditor() {
|
||||
this.form = emptyPreSettlementForm();
|
||||
this.summaryFees = [];
|
||||
@@ -1300,8 +1320,11 @@ export default {
|
||||
this.attachments = [];
|
||||
this.selectedAttachmentRows = [];
|
||||
this.adjustRows = [];
|
||||
this.pendingAdjustments = {};
|
||||
this.sourceFeeSnapshots = {};
|
||||
this.adjustDialog.activeTab = 'adjust';
|
||||
this.adjustDialog.detailId = '';
|
||||
this.adjustDialog.sourceDetailId = '';
|
||||
this.adjustDialog.detailLineNo = '';
|
||||
this.adjustChangeRecordVisible = false;
|
||||
this.adjustChangeRecord = null;
|
||||
@@ -1323,7 +1346,7 @@ export default {
|
||||
const detail = data?.data || {};
|
||||
this.form = { ...emptyPreSettlementForm(), ...detail };
|
||||
this.summaryFees = (detail.summaryFees || []).map(row => ({ ...row }));
|
||||
this.details = (detail.details || []).map(row => ({ ...row }));
|
||||
this.details = (detail.details || []).map(row => this.normalizeSourceDetail(row));
|
||||
this.advances = (detail.advances || []).map(row => ({
|
||||
...row,
|
||||
createUserName: row.createUserName || detail.createUserName,
|
||||
@@ -1499,6 +1522,8 @@ export default {
|
||||
try {
|
||||
const { data } = await save(this.buildSavePayload());
|
||||
this.form.id = data?.data || this.form.id;
|
||||
await this.loadDetail();
|
||||
await this.persistPendingAdjustments();
|
||||
if (shouldSubmit) {
|
||||
await submit({ id: this.form.id });
|
||||
this.$message.success('审批流程已发起');
|
||||
@@ -1507,7 +1532,6 @@ export default {
|
||||
return;
|
||||
}
|
||||
this.$message.success('保存成功');
|
||||
await this.loadDetail();
|
||||
this.$emit('success', this.form.id);
|
||||
} finally {
|
||||
this[stateKey] = false;
|
||||
@@ -1555,19 +1579,22 @@ export default {
|
||||
)?.feeItems || []
|
||||
);
|
||||
},
|
||||
feeCategoryValue(value) {
|
||||
if (value === undefined || value === null || value === '') return '';
|
||||
const option = this.feeCategoryOptions.find(
|
||||
findFeeCategory(value) {
|
||||
return this.feeCategoryOptions.find(
|
||||
item => String(item.dictKey) === String(value) || String(item.dictValue) === String(value)
|
||||
);
|
||||
return option?.dictValue || value;
|
||||
},
|
||||
// fee_category 业务字典:dictKey 为字典键值(落库值),dictValue 为字典名称(展示值)。
|
||||
feeCategoryValue(value) {
|
||||
if (value === undefined || value === null || value === '') return '';
|
||||
return this.findFeeCategory(value)?.dictKey ?? value;
|
||||
},
|
||||
feeCategoryName(value) {
|
||||
if (value === undefined || value === null || value === '') return '';
|
||||
const option = this.feeCategoryOptions.find(
|
||||
item => String(item.dictKey) === String(value) || String(item.dictValue) === String(value)
|
||||
);
|
||||
return option?.dictKey || value;
|
||||
const option = this.findFeeCategory(value);
|
||||
if (option) return option.dictValue ?? value;
|
||||
const feeOption = this.feeOptions.find(item => String(item.feeType) === String(value));
|
||||
return feeOption?.feeTypeName || value;
|
||||
},
|
||||
transportTypeName(value) {
|
||||
if (value === undefined || value === null || value === '') return '';
|
||||
@@ -1607,71 +1634,139 @@ export default {
|
||||
return totalText;
|
||||
});
|
||||
},
|
||||
// 结算合计按费用项归集:原金额取来源费用明细的初始金额,结算金额取调整后的金额,调整金额为两者之差。
|
||||
buildSummaryFeesFromDetails() {
|
||||
const summaryMap = new Map();
|
||||
const defaultFreightItem =
|
||||
this.feeOptions
|
||||
.flatMap(item => item.feeItems || [])
|
||||
.find(name => this.isFreightFeeItem(name)) || '运输费';
|
||||
const appendSummary = (feeItem, value, fallbackFeeType = '') => {
|
||||
const amount = Number(value || 0);
|
||||
if (!feeItem || !Number.isFinite(amount) || Math.abs(amount) < 0.005) return;
|
||||
const generatedFees = new Map(
|
||||
this.summaryFees
|
||||
.filter(row => Number(row.manualFlag) !== 1)
|
||||
.map(row => [`${row.feeType}\u0000${row.feeItem}`, row])
|
||||
);
|
||||
const appendSummary = (feeItem, originalValue, settlementValue, fallbackFeeType = '') => {
|
||||
const originalAmount = Number(originalValue || 0);
|
||||
const settlementAmount = Number(settlementValue || 0);
|
||||
if (!feeItem || !Number.isFinite(originalAmount) || !Number.isFinite(settlementAmount)) {
|
||||
return;
|
||||
}
|
||||
if (Math.abs(originalAmount) < 0.005 && Math.abs(settlementAmount) < 0.005) return;
|
||||
const feeOption = this.feeOptions.find(item =>
|
||||
(item.feeItems || []).some(name => String(name) === String(feeItem))
|
||||
);
|
||||
const feeType = this.feeCategoryValue(feeOption?.feeType || fallbackFeeType);
|
||||
const key = `${feeType}\u0000${feeItem}`;
|
||||
const generated = generatedFees.get(key);
|
||||
const current = summaryMap.get(key) || {
|
||||
id: '',
|
||||
id: generated?.id || '',
|
||||
feeType,
|
||||
feeItem,
|
||||
originalAmount: 0,
|
||||
adjustAmount: 0,
|
||||
settlementAmount: 0,
|
||||
remark: '',
|
||||
remark: generated?.remark || '',
|
||||
manualFlag: 0,
|
||||
};
|
||||
current.originalAmount = Number((current.originalAmount + amount).toFixed(2));
|
||||
current.settlementAmount = current.originalAmount;
|
||||
current.originalAmount = Number((current.originalAmount + originalAmount).toFixed(2));
|
||||
current.settlementAmount = Number((current.settlementAmount + settlementAmount).toFixed(2));
|
||||
current.adjustAmount = Number(
|
||||
(current.settlementAmount - current.originalAmount).toFixed(2)
|
||||
);
|
||||
summaryMap.set(key, current);
|
||||
};
|
||||
|
||||
this.details.forEach(row => {
|
||||
const feeItems = this.parseFeeItems(row.feeItemsJson || row.feeItems);
|
||||
const feeItemEntries = Object.entries(feeItems);
|
||||
feeItemEntries.forEach(([feeItem, amount]) => appendSummary(feeItem, amount, row.feeType));
|
||||
|
||||
let knownAmount = feeItemEntries.reduce(
|
||||
(total, [, amount]) => total + Number(amount || 0),
|
||||
0
|
||||
this.details.forEach(detail => {
|
||||
this.resolveDetailFeePairs(detail).forEach(([baseRow, currentRow]) =>
|
||||
this.appendFeeRowSummary(baseRow, currentRow, detail, appendSummary)
|
||||
);
|
||||
if (!feeItemEntries.some(([feeItem]) => this.isFreightFeeItem(feeItem))) {
|
||||
const freightAmount = Number(row.freightAmount || 0);
|
||||
appendSummary(defaultFreightItem, freightAmount, row.feeType);
|
||||
knownAmount += freightAmount;
|
||||
}
|
||||
if (!feeItemEntries.some(([feeItem]) => !this.isFreightFeeItem(feeItem))) {
|
||||
const otherFeeAmount = Number(row.otherFeeAmount || 0);
|
||||
appendSummary('其他费用', otherFeeAmount, row.feeType);
|
||||
knownAmount += otherFeeAmount;
|
||||
}
|
||||
|
||||
const totalAmount = Number(
|
||||
row.settlementAmountTax ?? row.totalAmount ?? row.afterAmount ?? row.settlementAmount ?? 0
|
||||
);
|
||||
const residualAmount = Number((totalAmount - knownAmount).toFixed(2));
|
||||
if (Math.abs(residualAmount) >= 0.005) {
|
||||
appendSummary('其他费用', residualAmount, row.feeType);
|
||||
}
|
||||
});
|
||||
|
||||
const manualFees = this.summaryFees.filter(row => row.manualFlag === 1);
|
||||
const manualFees = this.summaryFees.filter(row => Number(row.manualFlag) === 1);
|
||||
this.summaryFees = [...summaryMap.values(), ...manualFees];
|
||||
this.form.settlementAmount = this.summaryFees
|
||||
.reduce((total, row) => total + Number(row.settlementAmount || 0), 0)
|
||||
.toFixed(2);
|
||||
this.recalculateLocalAmount();
|
||||
},
|
||||
// 每条明细对应的费用行:[调整前基准行, 调整后当前行],缺少来源费用快照时退化为明细自身的汇总行。
|
||||
resolveDetailFeePairs(detail) {
|
||||
const sourceDetailId = String(detail.sourceDetailId || detail.id || '');
|
||||
const baseRows = this.sourceFeeSnapshots[sourceDetailId] || [];
|
||||
if (!baseRows.length) return [this.buildDetailFeePair(detail)];
|
||||
const pendingRows = this.pendingAdjustments[sourceDetailId]?.rows || [];
|
||||
return baseRows.map((baseRow, index) => {
|
||||
const matched =
|
||||
pendingRows.find(row => baseRow.id && String(row.id) === String(baseRow.id)) ||
|
||||
pendingRows[index];
|
||||
return [baseRow, matched || baseRow];
|
||||
});
|
||||
},
|
||||
buildDetailFeePair(detail) {
|
||||
const baseRow = {
|
||||
feeItems: this.parseFeeItems(detail.feeItemsJson || detail.feeItems),
|
||||
feeType: detail.feeType,
|
||||
freightAmount: Number(detail.freightAmount || 0),
|
||||
settlementAmountTax: this.detailOriginalAmount(detail),
|
||||
};
|
||||
return [baseRow, { ...baseRow, settlementAmountTax: this.detailSettlementAmount(detail) }];
|
||||
},
|
||||
appendFeeRowSummary(baseRow, currentRow, detail, appendSummary) {
|
||||
const fallbackFeeType = currentRow.feeType || detail.feeType || '';
|
||||
const feeItemNames = Array.from(
|
||||
new Set([...Object.keys(baseRow.feeItems || {}), ...Object.keys(currentRow.feeItems || {})])
|
||||
);
|
||||
feeItemNames.forEach(name =>
|
||||
appendSummary(name, baseRow.feeItems?.[name], currentRow.feeItems?.[name], fallbackFeeType)
|
||||
);
|
||||
const hasFreightItem = feeItemNames.some(name => this.isFreightFeeItem(name));
|
||||
if (!hasFreightItem) {
|
||||
appendSummary(
|
||||
this.defaultFreightFeeItem(),
|
||||
baseRow.freightAmount,
|
||||
currentRow.freightAmount,
|
||||
fallbackFeeType
|
||||
);
|
||||
}
|
||||
const knownAmount = row =>
|
||||
this.sumFeeItems(row.feeItems) + (hasFreightItem ? 0 : Number(row.freightAmount || 0));
|
||||
const baseResidual = Number(
|
||||
(Number(baseRow.settlementAmountTax || 0) - knownAmount(baseRow)).toFixed(2)
|
||||
);
|
||||
const currentResidual = Number(
|
||||
(Number(currentRow.settlementAmountTax || 0) - knownAmount(currentRow)).toFixed(2)
|
||||
);
|
||||
if (Math.abs(baseResidual) < 0.005 && Math.abs(currentResidual) < 0.005) return;
|
||||
// 结算金额被手工改写且无法拆分到具体费用项时,计入金额占比最大的费用项,否则归入其他费用。
|
||||
appendSummary(
|
||||
this.residualFeeItem(baseRow, currentRow),
|
||||
baseResidual,
|
||||
currentResidual,
|
||||
fallbackFeeType
|
||||
);
|
||||
},
|
||||
defaultFreightFeeItem() {
|
||||
return (
|
||||
this.feeOptions
|
||||
.flatMap(item => item.feeItems || [])
|
||||
.find(name => this.isFreightFeeItem(name)) || '运输费'
|
||||
);
|
||||
},
|
||||
residualFeeItem(baseRow, currentRow) {
|
||||
const feeItems = { ...(baseRow.feeItems || {}), ...(currentRow.feeItems || {}) };
|
||||
const dominant = Object.entries(feeItems)
|
||||
.filter(([, amount]) => Math.abs(Number(amount || 0)) >= 0.005)
|
||||
.sort((a, b) => Math.abs(Number(b[1] || 0)) - Math.abs(Number(a[1] || 0)))[0];
|
||||
if (dominant) return dominant[0];
|
||||
return (
|
||||
currentRow.billingRule?.feeItem ||
|
||||
currentRow.feeItem ||
|
||||
baseRow.billingRule?.feeItem ||
|
||||
baseRow.feeItem ||
|
||||
'其他费用'
|
||||
);
|
||||
},
|
||||
sumFeeItems(feeItems) {
|
||||
return Number(
|
||||
Object.values(feeItems || {})
|
||||
.reduce((total, value) => total + Number(value || 0), 0)
|
||||
.toFixed(2)
|
||||
);
|
||||
},
|
||||
recalculateLocalAmount() {
|
||||
const rate = this.form.currency === 'RMB' ? 1 : Number(this.form.exchangeRate || 0);
|
||||
this.form.localSettlementAmount = (Number(this.form.settlementAmount || 0) * rate).toFixed(2);
|
||||
@@ -1731,9 +1826,11 @@ export default {
|
||||
this.details.push(this.normalizeSourceDetail(row));
|
||||
}
|
||||
});
|
||||
this.buildSummaryFeesFromDetails();
|
||||
this.candidateDialog.confirming = true;
|
||||
if (this.deferSave || (this.pageMode && !this.form.id)) {
|
||||
await this.ensureSourceFeeSnapshots();
|
||||
this.buildSummaryFeesFromDetails();
|
||||
// 页面模式选择明细先更新本地数据,保存/提交时再统一落库。
|
||||
if (this.pageMode) {
|
||||
this.candidateDialog.visible = false;
|
||||
this.candidateDialog.confirming = false;
|
||||
this.$message.success('结算明细添加成功,请点击保存提交');
|
||||
@@ -1774,48 +1871,129 @@ export default {
|
||||
};
|
||||
this.appliedDetailQuery = { ...this.detailQuery };
|
||||
},
|
||||
async persistDetailForAdjustment(row) {
|
||||
const sourceDetailId = row.sourceDetailId || row.id;
|
||||
const isPersistedDetail =
|
||||
row.id && sourceDetailId && String(row.id) !== String(sourceDetailId);
|
||||
if (isPersistedDetail) return row;
|
||||
|
||||
await this.$refs.formRef?.validate();
|
||||
this.loading = true;
|
||||
try {
|
||||
const { data } = await save(this.buildSavePayload());
|
||||
this.form.id = data?.data || this.form.id;
|
||||
await this.loadDetail();
|
||||
const savedRow = this.details.find(
|
||||
item => String(item.sourceDetailId || '') === String(sourceDetailId || '')
|
||||
);
|
||||
if (!savedRow) {
|
||||
this.$message.warning('结算明细保存失败,请重试');
|
||||
return null;
|
||||
}
|
||||
this.$emit('success', this.form.id);
|
||||
return savedRow;
|
||||
} finally {
|
||||
this.loading = false;
|
||||
normalizeAdjustRow(item, feeItemNames = []) {
|
||||
const rawFeeItems =
|
||||
item.feeItems && typeof item.feeItems === 'object'
|
||||
? item.feeItems
|
||||
: this.parseFeeItems(item.feeItemsJson);
|
||||
const names = feeItemNames.length ? feeItemNames : Object.keys(rawFeeItems);
|
||||
const adjusted = {
|
||||
...item,
|
||||
transportQuantity: Number(item.transportQuantity || 0),
|
||||
mileage:
|
||||
item.mileage === null || item.mileage === undefined || Number(item.mileage) === -1
|
||||
? null
|
||||
: Number(item.mileage),
|
||||
freightAmount: Number(item.freightAmount || 0),
|
||||
originalAmount: Number(item.originalAmount || 0),
|
||||
feeItems: Object.fromEntries(names.map(name => [name, Number(rawFeeItems[name] || 0)])),
|
||||
settlementAmountTax: Number(item.settlementAmountTax ?? item.afterAmount ?? 0),
|
||||
settlementAmountNoTax: item.settlementAmountNoTax,
|
||||
billingRule: this.normalizeBillingRule(item),
|
||||
calculating: false,
|
||||
calculateError: '',
|
||||
adjustCalculateTimer: null,
|
||||
adjustCalculateVersion: 0,
|
||||
};
|
||||
if (item.settlementAmountTax === undefined && item.afterAmount === undefined) {
|
||||
this.recalculateAdjustRow(adjusted);
|
||||
}
|
||||
return adjusted;
|
||||
},
|
||||
cloneAdjustRows(rows) {
|
||||
return (rows || []).map(row => ({
|
||||
...row,
|
||||
feeItems: { ...row.feeItems },
|
||||
calculating: false,
|
||||
calculateError: '',
|
||||
adjustCalculateTimer: null,
|
||||
adjustCalculateVersion: 0,
|
||||
}));
|
||||
},
|
||||
async loadSourceFeeRows(sourceDetailId) {
|
||||
const response = await getFeeDetail(sourceDetailId);
|
||||
const data = response.data?.data || response.data || {};
|
||||
return (data.records || []).map(item =>
|
||||
this.normalizeAdjustRow(item, data.feeItemNames || [])
|
||||
);
|
||||
},
|
||||
// 明细列表只有汇总金额,费用项级别的原始金额需要按来源明细单独拉取并缓存为调整基准。
|
||||
async ensureSourceFeeSnapshots() {
|
||||
const targets = this.details.filter(row => {
|
||||
const sourceDetailId = String(row.sourceDetailId || row.id || '');
|
||||
return sourceDetailId && !this.sourceFeeSnapshots[sourceDetailId];
|
||||
});
|
||||
if (!targets.length) return;
|
||||
await Promise.all(
|
||||
targets.map(async row => {
|
||||
const sourceDetailId = String(row.sourceDetailId || row.id || '');
|
||||
try {
|
||||
const feeRows = await this.loadSourceFeeRows(sourceDetailId);
|
||||
if (!feeRows.length) return;
|
||||
this.sourceFeeSnapshots[sourceDetailId] = feeRows;
|
||||
this.applySnapshotToDetail(row, feeRows);
|
||||
} catch (error) {
|
||||
// 取不到费用明细时退回按明细汇总金额归集,后续操作会再次尝试拉取。
|
||||
delete this.sourceFeeSnapshots[sourceDetailId];
|
||||
}
|
||||
})
|
||||
);
|
||||
},
|
||||
applySnapshotToDetail(detail, feeRows) {
|
||||
if (!feeRows.length) return;
|
||||
const feeItems = {};
|
||||
feeRows.forEach(row =>
|
||||
Object.entries(row.feeItems || {}).forEach(([name, amount]) => {
|
||||
feeItems[name] = Number((Number(feeItems[name] || 0) + Number(amount || 0)).toFixed(2));
|
||||
})
|
||||
);
|
||||
const settlementAmount = feeRows.reduce(
|
||||
(total, row) => total + Number(row.settlementAmountTax || 0),
|
||||
0
|
||||
);
|
||||
const freightAmount = feeRows.reduce(
|
||||
(total, row) => total + Number(row.freightAmount || 0),
|
||||
0
|
||||
);
|
||||
Object.assign(detail, {
|
||||
feeItemsJson: JSON.stringify(feeItems),
|
||||
freightAmount: Number(freightAmount.toFixed(2)),
|
||||
originalAmount: Number(settlementAmount.toFixed(2)),
|
||||
adjustAmount: 0,
|
||||
settlementAmountTax: Number(settlementAmount.toFixed(2)),
|
||||
});
|
||||
},
|
||||
async openAdjustDialog(row, readonly) {
|
||||
const detailRow = await this.persistDetailForAdjustment(row);
|
||||
if (!detailRow) return;
|
||||
const detailRow = row;
|
||||
this.adjustDialog.visible = true;
|
||||
this.adjustDialog.loading = true;
|
||||
this.adjustDialog.readonly = readonly;
|
||||
this.adjustDialog.activeTab = 'adjust';
|
||||
this.adjustDialog.detailId = detailRow.id;
|
||||
this.adjustDialog.detailLineNo = detailRow.lineNo;
|
||||
this.adjustDialog.reason = '';
|
||||
this.adjustDialog.sourceDetailId =
|
||||
detailRow.id && String(detailRow.id) === String(detailRow.sourceDetailId)
|
||||
? detailRow.sourceDetailId
|
||||
: '';
|
||||
const sourceDetailId = String(this.adjustDialog.sourceDetailId || '');
|
||||
const pending = sourceDetailId ? this.pendingAdjustments[sourceDetailId] : null;
|
||||
this.adjustDialog.reason = pending?.reason || '';
|
||||
try {
|
||||
const { data } = await getDetailFees(detailRow.id);
|
||||
this.adjustRows = (data?.data || []).map(item => ({
|
||||
...item,
|
||||
feeItems: this.parseFeeItems(item.feeItemsJson),
|
||||
billingRule: this.normalizeBillingRule(item),
|
||||
}));
|
||||
// 已暂存的调整结果优先回显,避免重新打开时又回到调整前的数据。
|
||||
if (pending?.rows?.length) {
|
||||
this.adjustRows = this.cloneAdjustRows(pending.rows);
|
||||
return;
|
||||
}
|
||||
if (sourceDetailId) {
|
||||
if (!this.sourceFeeSnapshots[sourceDetailId]) {
|
||||
this.sourceFeeSnapshots[sourceDetailId] = await this.loadSourceFeeRows(sourceDetailId);
|
||||
}
|
||||
this.adjustRows = this.cloneAdjustRows(this.sourceFeeSnapshots[sourceDetailId]);
|
||||
return;
|
||||
}
|
||||
const response = await getDetailFees(detailRow.id);
|
||||
const rows = response.data?.data || response.data || [];
|
||||
this.adjustRows = rows.map(item => this.normalizeAdjustRow(item));
|
||||
} finally {
|
||||
this.adjustDialog.loading = false;
|
||||
}
|
||||
@@ -1957,6 +2135,45 @@ export default {
|
||||
row.settlementAmountTax = Number(
|
||||
(hasFreightItem ? feeItemTotal : Number(row.freightAmount || 0) + feeItemTotal).toFixed(2)
|
||||
);
|
||||
if (changedField && this.adjustDialog.sourceDetailId && row.id) {
|
||||
this.scheduleSourceAdjustCalculation(row);
|
||||
}
|
||||
},
|
||||
scheduleSourceAdjustCalculation(row) {
|
||||
if (row.adjustCalculateTimer) clearTimeout(row.adjustCalculateTimer);
|
||||
row.adjustCalculateVersion += 1;
|
||||
row.calculating = true;
|
||||
const version = row.adjustCalculateVersion;
|
||||
row.adjustCalculateTimer = setTimeout(async () => {
|
||||
row.adjustCalculateTimer = null;
|
||||
try {
|
||||
const response = await calculateAdjustedFee({
|
||||
detailId: this.adjustDialog.sourceDetailId,
|
||||
feeId: row.id,
|
||||
transportQuantity: Number(row.transportQuantity || 0),
|
||||
mileage: Number(row.mileage || 0),
|
||||
freightAmount: Number(row.freightAmount || 0),
|
||||
feeItems: Object.fromEntries(
|
||||
Object.entries(row.feeItems || {}).map(([name, amount]) => [
|
||||
name,
|
||||
Number(amount || 0),
|
||||
])
|
||||
),
|
||||
});
|
||||
if (version !== row.adjustCalculateVersion || !this.adjustDialog.visible) return;
|
||||
const data = response.data?.data || response.data || {};
|
||||
row.freightAmount = Number(data.freightAmount || 0);
|
||||
row.feeItems = Object.fromEntries(
|
||||
Object.entries(data.feeItems || {}).map(([name, amount]) => [name, Number(amount || 0)])
|
||||
);
|
||||
row.adjustAmount = Number(data.adjustAmount || 0);
|
||||
row.settlementAmountTax = Number(data.afterAmount || 0);
|
||||
} catch (error) {
|
||||
row.calculateError = error.message || '费用试算失败';
|
||||
} finally {
|
||||
if (version === row.adjustCalculateVersion) row.calculating = false;
|
||||
}
|
||||
}, 300);
|
||||
},
|
||||
isFreightFeeItem(name) {
|
||||
return String(name || '').includes('运费') || String(name || '').includes('运输费');
|
||||
@@ -1966,8 +2183,22 @@ export default {
|
||||
this.$message.warning('请输入调整原因');
|
||||
return;
|
||||
}
|
||||
if (this.adjustRows.some(row => row.calculating)) {
|
||||
this.$message.warning('费用正在重新计算,请稍候');
|
||||
return;
|
||||
}
|
||||
this.adjustDialog.saving = true;
|
||||
try {
|
||||
if (this.adjustDialog.sourceDetailId) {
|
||||
this.pendingAdjustments[this.adjustDialog.sourceDetailId] = {
|
||||
reason: this.adjustDialog.reason,
|
||||
rows: this.adjustRows.map(row => ({ ...row, feeItems: { ...row.feeItems } })),
|
||||
};
|
||||
this.applyLocalAdjustment(this.adjustDialog.sourceDetailId, this.adjustRows);
|
||||
this.adjustDialog.visible = false;
|
||||
this.$message.success('结算明细调整已暂存,保存预结算单后生效');
|
||||
return;
|
||||
}
|
||||
await adjustDetail({
|
||||
detailId: this.adjustDialog.detailId,
|
||||
changeReason: this.adjustDialog.reason,
|
||||
@@ -1991,6 +2222,92 @@ export default {
|
||||
this.adjustDialog.saving = false;
|
||||
}
|
||||
},
|
||||
applyLocalAdjustment(sourceDetailId, rows) {
|
||||
const detail = this.details.find(
|
||||
item => String(item.sourceDetailId || item.id) === String(sourceDetailId)
|
||||
);
|
||||
if (!detail) return;
|
||||
// 原金额以进入预结算单时的费用快照为基准,调整金额 = 结算金额 - 原金额。
|
||||
const baseRows = this.sourceFeeSnapshots[String(sourceDetailId)] || [];
|
||||
const originalAmount = baseRows.length
|
||||
? baseRows.reduce((sum, row) => sum + Number(row.settlementAmountTax || 0), 0)
|
||||
: rows.reduce((sum, row) => sum + Number(row.originalAmount || 0), 0);
|
||||
const settlementAmountTax = rows.reduce(
|
||||
(sum, row) => sum + Number(row.settlementAmountTax || 0),
|
||||
0
|
||||
);
|
||||
const freightAmount = rows.reduce((sum, row) => sum + Number(row.freightAmount || 0), 0);
|
||||
const feeItems = {};
|
||||
rows.forEach(row =>
|
||||
Object.entries(row.feeItems || {}).forEach(([name, amount]) => {
|
||||
feeItems[name] = Number((Number(feeItems[name] || 0) + Number(amount || 0)).toFixed(2));
|
||||
})
|
||||
);
|
||||
Object.assign(detail, {
|
||||
originalAmount: Number(originalAmount.toFixed(2)),
|
||||
adjustAmount: Number((settlementAmountTax - originalAmount).toFixed(2)),
|
||||
settlementAmountTax: Number(settlementAmountTax.toFixed(2)),
|
||||
freightAmount: Number(freightAmount.toFixed(2)),
|
||||
feeItemsJson: JSON.stringify(feeItems),
|
||||
});
|
||||
this.buildSummaryFeesFromDetails();
|
||||
},
|
||||
async persistPendingAdjustments() {
|
||||
const pendingEntries = Object.entries(this.pendingAdjustments);
|
||||
if (!pendingEntries.length || !this.form.id) return;
|
||||
for (const [sourceDetailId, pending] of pendingEntries) {
|
||||
const detail = this.details.find(
|
||||
item => String(item.sourceDetailId || '') === String(sourceDetailId)
|
||||
);
|
||||
if (!detail) continue;
|
||||
const persistedFees = await getDetailFees(detail.id);
|
||||
const feeRows = persistedFees.data?.data || persistedFees.data || [];
|
||||
const pendingRows = pending.rows || [];
|
||||
const rows = feeRows.map(fee => {
|
||||
const pendingRow = this.findPendingAdjustmentRow(fee, pendingRows);
|
||||
if (!pendingRow) {
|
||||
throw new Error(`费用行“${fee.cargoName || fee.lineNo || fee.id}”保存匹配失败`);
|
||||
}
|
||||
return {
|
||||
id: fee.id,
|
||||
transportQuantity: pendingRow.transportQuantity,
|
||||
mileage: pendingRow.mileage,
|
||||
unitPrice: pendingRow.unitPrice,
|
||||
freightAmount: pendingRow.freightAmount,
|
||||
feeItems: pendingRow.feeItems,
|
||||
settlementAmountTax: pendingRow.settlementAmountTax,
|
||||
settlementAmountNoTax: pendingRow.settlementAmountNoTax,
|
||||
remark: pendingRow.remark,
|
||||
};
|
||||
});
|
||||
if (rows.length) {
|
||||
await adjustDetail({ detailId: detail.id, changeReason: pending.reason, rows });
|
||||
}
|
||||
}
|
||||
this.pendingAdjustments = {};
|
||||
await this.loadDetail();
|
||||
},
|
||||
findPendingAdjustmentRow(fee, pendingRows) {
|
||||
const exactIdMatch = pendingRows.find(
|
||||
row => row.id && fee.sourceFeeId && String(row.id) === String(fee.sourceFeeId)
|
||||
);
|
||||
if (exactIdMatch) return exactIdMatch;
|
||||
const exactLineMatch = pendingRows.find(
|
||||
row =>
|
||||
row.lineNo &&
|
||||
fee.lineNo &&
|
||||
String(row.lineNo) === String(fee.lineNo) &&
|
||||
String(row.cargoName || '') === String(fee.cargoName || '') &&
|
||||
String(row.cargoType || '') === String(fee.cargoType || '')
|
||||
);
|
||||
if (exactLineMatch) return exactLineMatch;
|
||||
const cargoMatches = pendingRows.filter(
|
||||
row =>
|
||||
String(row.cargoName || '') === String(fee.cargoName || '') &&
|
||||
String(row.cargoType || '') === String(fee.cargoType || '')
|
||||
);
|
||||
return cargoMatches.length === 1 ? cargoMatches[0] : null;
|
||||
},
|
||||
exportDetails() {
|
||||
if (!this.filteredDetails.length) {
|
||||
this.$message.warning('暂无可导出的结算明细');
|
||||
|
||||
Reference in New Issue
Block a user