This commit is contained in:
2026-08-12 16:31:46 +08:00
parent c15d21b986
commit d8bbfe05f3
40 changed files with 1541 additions and 248 deletions
@@ -0,0 +1,28 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { http } from '@/shared/api/http'
import { adjustContainerFee, approveContainerFee, createContainerFee, listContainerCostDetails, listContainerFees, rejectContainerFee, voidContainerFee } from './containerFees'
describe('container fee API', () => {
beforeEach(() => vi.restoreAllMocks())
it('passes accountId and exact controller DTOs', async () => {
const get = vi.spyOn(http, 'get').mockResolvedValue({ code: '200', data: [] })
const post = vi.spyOn(http, 'post').mockResolvedValue({ code: '200', data: { id: 3, sourceVersion: 1 } })
const values = { feeItemId: '7', direction: 'PAYABLE' as const, businessDate: '2026-08-12', amount: 100, currency: 'CNY' as const, settlementIncluded: true, remark: null }
await listContainerFees('9', '11'); await createContainerFee('9', '11', values)
await approveContainerFee('9', '11', '3'); await rejectContainerFee('9', '11', '3', '资料不全')
await adjustContainerFee('9', '11', '3', 2, values); await voidContainerFee('9', '11', '3', 3)
expect(get).toHaveBeenCalledWith('/platform/v1/loading-plans/9/fee-details', { params: { accountId: '11' } })
expect(post).toHaveBeenCalledWith('/platform/v1/loading-plans/9/fee-details', values, { params: { accountId: '11' } })
expect(post).toHaveBeenCalledWith('/platform/v1/loading-plans/9/fee-details/3/adjust', { expectedSourceVersion: 2, ...values }, { params: { accountId: '11' } })
expect(post).toHaveBeenCalledWith('/platform/v1/loading-plans/9/fee-details/3/void', { expectedSourceVersion: 3 }, { params: { accountId: '11' } })
})
it('reads allocation rows from Finance and never calculates them', async () => {
const get = vi.spyOn(http, 'get')
.mockResolvedValueOnce({ code: '200', data: [{ id: 81, containerId: 99 }] })
.mockResolvedValueOnce({ code: '200', data: { cost: { id: 81, containerId: 99, supplierName: '承运商A', feeItemName: '拖车费', payableAmount: 300 }, allocations: [{ id: 1, orderId: 8, volumeCbm: 2, totalVolumeCbm: 5, allocationRate: 0.4, allocatedAmount: 120 }] } })
const result = await listContainerCostDetails('11', '99')
expect(result[0]).toMatchObject({ supplierName: '承运商A', allocations: [{ orderId: '8', allocatedAmount: 120 }] })
expect(get).toHaveBeenNthCalledWith(2, '/platform/finance/v1/cost-entries/81', { params: { customerId: '11' } })
})
})
@@ -0,0 +1,125 @@
import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http'
export type ContainerFeeDirection = 'RECEIVABLE' | 'PAYABLE'
export type ContainerFeeReviewStatus = 'PENDING' | 'APPROVED' | 'REJECTED'
export interface ContainerFeeResource {
id: string
loadingPlanId: string
containerBookingId: string | null
containerNo: string | null
feeItemId: string
feeItemName: string
direction: ContainerFeeDirection
businessDate: string
amount: number
currency: 'CNY'
settlementIncluded: boolean
reviewStatus: ContainerFeeReviewStatus
sourceStatus: 'ACTIVE' | 'VOIDED'
sourceVersion: number
remark: string | null
reviewerName: string | null
reviewedAt: string | null
reviewReason: string | null
}
export interface ContainerFeeSaveRequest {
feeItemId: string
direction: ContainerFeeDirection
businessDate: string
amount: number
currency: 'CNY'
settlementIncluded: boolean
remark: string | null
}
export interface ApplicableFeeItem {
id: string
name: string
warehouseReceivable: boolean
warehousePayable: boolean
pickupReceivable: boolean
pickupPayable: boolean
}
export interface ContainerCostAllocation {
id: string
orderId: string
inboundId: string | null
volumeCbm: number
totalVolumeCbm: number
allocationRate: number
allocatedAmount: number
roundingAdjustment: boolean
}
export interface ContainerCostDetail {
id: string
containerId: string | null
supplierName: string | null
feeItemName: string
amount: number
allocations: ContainerCostAllocation[]
}
const id = (value: unknown) => value == null ? '' : String(value)
const numberValue = (value: unknown) => Number.isFinite(Number(value)) ? Number(value) : 0
const base = (planId: string) => `/platform/v1/loading-plans/${encodeURIComponent(planId)}/fee-details`
const params = (accountId: string) => ({ accountId })
function normalizeFee(raw: Record<string, unknown>): ContainerFeeResource {
return {
id: id(raw.id), loadingPlanId: id(raw.loadingPlanId), containerBookingId: raw.containerBookingId == null ? null : id(raw.containerBookingId),
containerNo: raw.containerNo == null ? null : String(raw.containerNo), feeItemId: id(raw.feeItemId),
feeItemName: String(raw.feeItemNameSnapshot || ''), direction: raw.direction as ContainerFeeDirection,
businessDate: String(raw.businessDate || ''), amount: numberValue(raw.amount), currency: 'CNY',
settlementIncluded: Boolean(raw.settlementIncluded), reviewStatus: raw.reviewStatus as ContainerFeeReviewStatus,
sourceStatus: raw.sourceStatus as ContainerFeeResource['sourceStatus'], sourceVersion: numberValue(raw.sourceVersion),
remark: raw.remark == null ? null : String(raw.remark), reviewerName: raw.reviewerName == null ? null : String(raw.reviewerName),
reviewedAt: raw.reviewedAt == null ? null : String(raw.reviewedAt), reviewReason: raw.reviewReason == null ? null : String(raw.reviewReason),
}
}
export async function listContainerFees(planId: string, accountId: string) {
const response = await http.get<ApiEnvelope<Record<string, unknown>[]>, ApiEnvelope<Record<string, unknown>[]>>(base(planId), { params: params(accountId) })
return unwrapApiResponse(response).map(normalizeFee)
}
export async function createContainerFee(planId: string, accountId: string, payload: ContainerFeeSaveRequest) {
const response = await http.post<ApiEnvelope<Record<string, unknown>>, ApiEnvelope<Record<string, unknown>>>(base(planId), payload, { params: params(accountId) })
return normalizeFee(unwrapApiResponse(response))
}
export async function approveContainerFee(planId: string, accountId: string, feeId: string) {
await http.post(`${base(planId)}/${encodeURIComponent(feeId)}/approve`, undefined, { params: params(accountId) })
}
export async function rejectContainerFee(planId: string, accountId: string, feeId: string, reason: string) {
await http.post(`${base(planId)}/${encodeURIComponent(feeId)}/reject`, { reason }, { params: params(accountId) })
}
export async function adjustContainerFee(planId: string, accountId: string, feeId: string, expectedSourceVersion: number, values: ContainerFeeSaveRequest) {
const response = await http.post<ApiEnvelope<Record<string, unknown>>, ApiEnvelope<Record<string, unknown>>>(`${base(planId)}/${encodeURIComponent(feeId)}/adjust`, { expectedSourceVersion, ...values }, { params: params(accountId) })
return normalizeFee(unwrapApiResponse(response))
}
export async function voidContainerFee(planId: string, accountId: string, feeId: string, expectedSourceVersion: number) {
await http.post(`${base(planId)}/${encodeURIComponent(feeId)}/void`, { expectedSourceVersion }, { params: params(accountId) })
}
export async function listApplicableFeeItems() {
const response = await http.get<ApiEnvelope<{ records: Record<string, unknown>[] }>, ApiEnvelope<{ records: Record<string, unknown>[] }>>('/platform/v1/fee-items', { params: { status: 'ACTIVE', page: 1, size: 200 } })
return (unwrapApiResponse(response).records || []).map((raw) => ({ id: id(raw.id), name: String(raw.feeTypeName || ''), warehouseReceivable: Boolean(raw.warehouseReceivable), warehousePayable: Boolean(raw.warehousePayable), pickupReceivable: Boolean(raw.pickupReceivable), pickupPayable: Boolean(raw.pickupPayable) }))
}
export async function listContainerCostDetails(customerId: string, containerId: string): Promise<ContainerCostDetail[]> {
const response = await http.get<ApiEnvelope<Record<string, unknown>[]>, ApiEnvelope<Record<string, unknown>[]>>('/platform/finance/v1/cost-entries', { params: { customerId } })
const costs = unwrapApiResponse(response).filter((raw) => id(raw.containerId) === containerId)
return Promise.all(costs.map(async (raw) => {
const costId = id(raw.id)
const detailResponse = await http.get<ApiEnvelope<{ cost: Record<string, unknown>; allocations: Record<string, unknown>[] }>, ApiEnvelope<{ cost: Record<string, unknown>; allocations: Record<string, unknown>[] }>>(`/platform/finance/v1/cost-entries/${encodeURIComponent(costId)}`, { params: { customerId } })
const detail = unwrapApiResponse(detailResponse)
return { id: costId, containerId: detail.cost.containerId == null ? null : id(detail.cost.containerId), supplierName: detail.cost.supplierName == null ? null : String(detail.cost.supplierName), feeItemName: String(detail.cost.feeItemName || ''), amount: numberValue(detail.cost.payableAmount), allocations: (detail.allocations || []).map((item) => ({ id: id(item.id), orderId: id(item.orderId), inboundId: item.inboundId == null ? null : id(item.inboundId), volumeCbm: numberValue(item.volumeCbm), totalVolumeCbm: numberValue(item.totalVolumeCbm), allocationRate: numberValue(item.allocationRate), allocatedAmount: numberValue(item.allocatedAmount), roundingAdjustment: Boolean(item.roundingAdjustment) })) }
}))
}