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

155 lines
8.2 KiB
TypeScript
Raw Normal View History

2026-08-12 16:31:46 +08:00
import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http'
2026-08-19 09:14:35 +08:00
import { listSuppliers, type SupplierCategory } from '@/modules/suppliers'
2026-08-12 16:31:46 +08:00
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
2026-08-19 09:14:35 +08:00
supplierId: string | null
supplierName: string | null
supplierCategory: SupplierCategory | null
2026-08-12 16:31:46 +08:00
businessDate: string
amount: number
currency: 'CNY'
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
2026-08-19 09:14:35 +08:00
supplierId: string | null
2026-08-12 16:31:46 +08:00
businessDate: string
amount: number
currency: 'CNY'
remark: string | null
}
2026-08-18 17:40:40 +08:00
export interface ContainerFeeAdjustRequest {
expectedSourceVersion: number
businessDate: string
amount: number
}
2026-08-12 16:31:46 +08:00
export interface ApplicableFeeItem {
id: string
name: string
2026-08-19 09:14:35 +08:00
feeCode: string
supplierCategory: SupplierCategory | null
2026-08-12 16:31:46 +08:00
warehouseReceivable: boolean
warehousePayable: boolean
pickupReceivable: boolean
pickupPayable: boolean
}
2026-08-19 09:14:35 +08:00
export interface ContainerFeeSupplierOption {
2026-08-12 16:58:52 +08:00
id: string
2026-08-19 09:14:35 +08:00
supplierName: string
category: SupplierCategory
2026-08-12 16:58:52 +08:00
}
2026-08-12 16:31:46 +08:00
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
2026-08-19 09:14:35 +08:00
const supplierCategories: SupplierCategory[] = ['BOOKING', 'CUSTOMS', 'TRUCKING', 'CERTIFICATE']
const supplierCategory = (value: unknown): SupplierCategory | null => supplierCategories.includes(value as SupplierCategory) ? value as SupplierCategory : null
2026-08-12 16:31:46 +08:00
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,
2026-08-19 09:14:35 +08:00
supplierId: raw.supplierId == null ? null : id(raw.supplierId), supplierName: raw.supplierName == null ? null : String(raw.supplierName),
supplierCategory: supplierCategory(raw.supplierCategory),
2026-08-12 16:31:46 +08:00
businessDate: String(raw.businessDate || ''), amount: numberValue(raw.amount), currency: 'CNY',
2026-08-19 09:14:35 +08:00
reviewStatus: raw.reviewStatus as ContainerFeeReviewStatus,
2026-08-12 16:31:46 +08:00
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) })
}
2026-08-18 17:40:40 +08:00
export async function adjustContainerFee(planId: string, accountId: string, feeId: string, payload: ContainerFeeAdjustRequest) {
const response = await http.post<ApiEnvelope<Record<string, unknown>>, ApiEnvelope<Record<string, unknown>>>(`${base(planId)}/${encodeURIComponent(feeId)}/adjust`, payload, { params: params(accountId) })
2026-08-12 16:31:46 +08:00
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 } })
2026-08-19 09:14:35 +08:00
return (unwrapApiResponse(response).records || []).map((raw) => ({ id: id(raw.id), name: String(raw.feeTypeName || ''), feeCode: String(raw.feeCode || ''), supplierCategory: supplierCategory(raw.supplierCategory), warehouseReceivable: Boolean(raw.warehouseReceivable), warehousePayable: Boolean(raw.warehousePayable), pickupReceivable: Boolean(raw.pickupReceivable), pickupPayable: Boolean(raw.pickupPayable) }))
2026-08-12 16:31:46 +08:00
}
2026-08-19 09:14:35 +08:00
export async function listContainerFeeSupplierOptions(category: SupplierCategory): Promise<ContainerFeeSupplierOption[]> {
const page = await listSuppliers({ status: 'ACTIVE', category, page: 1, size: 200 })
return page.records.filter((supplier) => supplier.status === 'ACTIVE' && supplier.category === category).map((supplier) => ({ id: supplier.id, supplierName: supplier.name, category: supplier.category }))
2026-08-12 16:58:52 +08:00
}
2026-08-12 16:31:46 +08:00
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 } })
2026-08-12 17:29:35 +08:00
const costs = unwrapApiResponse(response).filter((raw) => id(raw.customerId) === customerId && id(raw.containerId) === containerId)
2026-08-12 16:31:46 +08:00
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)
2026-08-12 17:29:35 +08:00
if (id(detail.cost.id) !== costId || id(detail.cost.customerId) !== customerId || id(detail.cost.containerId) !== containerId) {
throw new Error('Finance 成本详情与柜次或客户上下文不一致')
}
2026-08-12 16:31:46 +08:00
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) })) }
}))
}