Files
admin-web/src/modules/loading-lists/api/containerFees.ts
T

126 lines
6.7 KiB
TypeScript
Raw Normal View History

2026-08-12 16:31:46 +08:00
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) })) }
}))
}