优化
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http'
|
||||
import type { FinanceBillResource, FinanceCostResource, FinancePaymentMethod, FinanceReceiptResource } from '../types'
|
||||
import type { FinanceBillDetail, FinanceBillLine, FinanceBillResource, FinanceCostResource, FinancePaymentMethod, FinanceReceiptResource } from '../types'
|
||||
|
||||
const basePath = '/platform/finance/v1'
|
||||
function idempotencyKey(operation: string) { return `finance-${operation}-${Date.now()}-${Math.random().toString(36).slice(2)}` }
|
||||
@@ -18,24 +18,59 @@ function normalizePage<T>(payload: RawPage, map: (item: RawResource) => T): Page
|
||||
const records = Array.isArray(payload) ? payload : payload.records || []
|
||||
return { records: records.map(map), total: Array.isArray(payload) ? records.length : Number(payload.total ?? records.length), serverPaged: !Array.isArray(payload) }
|
||||
}
|
||||
|
||||
export async function listFinanceBills(params: { customerId: string; status?: string; page: number; pageSize: number }) {
|
||||
const response = await http.get<ApiEnvelope<RawPage>, ApiEnvelope<RawPage>>(`${basePath}/bills`, { params })
|
||||
return normalizePage<FinanceBillResource>(unwrapApiResponse(response), (item) => ({
|
||||
id: stringValue(item.id), billNo: stringValue(item.bill_no ?? item.billNo), customerId: params.customerId,
|
||||
customerName: stringValue(item.customer_name_snapshot ?? item.customerName) || `客户 ${params.customerId}`,
|
||||
function normalizeBill(item: RawResource, customerId = stringValue(item.tenant_id ?? item.customerId)): FinanceBillResource {
|
||||
return {
|
||||
id: stringValue(item.id), billNo: stringValue(item.bill_no ?? item.billNo), customerId,
|
||||
customerName: stringValue(item.customer_name_snapshot ?? item.customerName) || `客户 ${customerId}`,
|
||||
billDate: stringValue(item.bill_date ?? item.billDate), dueDate: stringValue(item.due_date ?? item.dueDate), currency: 'CNY',
|
||||
totalAmount: numberValue(item.total_amount ?? item.totalAmount), paidAmount: numberValue(item.received_amount ?? item.paidAmount),
|
||||
outstandingAmount: numberValue(item.outstanding_amount ?? item.outstandingAmount), status: stringValue(item.status) as FinanceBillResource['status'],
|
||||
version: numberValue(item.version),
|
||||
}))
|
||||
}
|
||||
}
|
||||
function normalizeBillLine(item: RawResource): FinanceBillLine {
|
||||
return {
|
||||
sourceFeeId: stringValue(item.source_id ?? item.sourceFeeId), sourceFeeVersion: numberValue(item.source_version ?? item.sourceFeeVersion) || 1,
|
||||
orderId: item.order_id == null && item.orderId == null ? null : stringValue(item.order_id ?? item.orderId),
|
||||
inboundId: item.inbound_id == null && item.inboundId == null ? null : stringValue(item.inbound_id ?? item.inboundId),
|
||||
feeItemName: stringValue(item.fee_item_name ?? item.feeItemName), amount: numberValue(item.amount),
|
||||
}
|
||||
}
|
||||
function normalizeBillDetail(item: RawResource): FinanceBillDetail {
|
||||
return { ...normalizeBill(item), remark: stringValue(item.remark), lines: Array.isArray(item.lines) ? item.lines.map((line) => normalizeBillLine(line as RawResource)) : [] }
|
||||
}
|
||||
function mutationHeaders(operation: string) { return { headers: { 'Idempotency-Key': idempotencyKey(operation) } } }
|
||||
|
||||
export async function listFinanceBills(params: { customerId: string; status?: string; page: number; pageSize: number }) {
|
||||
const response = await http.get<ApiEnvelope<RawPage>, ApiEnvelope<RawPage>>(`${basePath}/bills`, { params })
|
||||
return normalizePage<FinanceBillResource>(unwrapApiResponse(response), (item) => normalizeBill(item, params.customerId))
|
||||
}
|
||||
|
||||
export async function createFinanceBill(payload: Record<string, unknown>) {
|
||||
const response = await http.post<ApiEnvelope<FinanceBillResource>, ApiEnvelope<FinanceBillResource>>(`${basePath}/bills`, { ...payload }, { headers: { 'Idempotency-Key': idempotencyKey('bill') } })
|
||||
const response = await http.post<ApiEnvelope<FinanceBillResource>, ApiEnvelope<FinanceBillResource>>(`${basePath}/bills`, { ...payload }, mutationHeaders('bill'))
|
||||
return unwrapApiResponse(response)
|
||||
}
|
||||
|
||||
export async function getFinanceBill(billId: string) {
|
||||
const response = await http.get<ApiEnvelope<RawResource>, ApiEnvelope<RawResource>>(`${basePath}/bills/${billId}`)
|
||||
return normalizeBillDetail(unwrapApiResponse(response))
|
||||
}
|
||||
|
||||
export async function updateFinanceBill(billId: string, payload: Record<string, unknown>) {
|
||||
const response = await http.put<ApiEnvelope<RawResource>, ApiEnvelope<RawResource>>(`${basePath}/bills/${billId}`, payload, mutationHeaders('bill-update'))
|
||||
return normalizeBillDetail(unwrapApiResponse(response))
|
||||
}
|
||||
|
||||
export async function adjustFinanceBill(billId: string, payload: Record<string, unknown>) {
|
||||
const response = await http.post<ApiEnvelope<RawResource>, ApiEnvelope<RawResource>>(`${basePath}/bills/${billId}/adjust`, payload, mutationHeaders('bill-adjust'))
|
||||
return normalizeBillDetail(unwrapApiResponse(response))
|
||||
}
|
||||
|
||||
export async function voidFinanceBill(billId: string, expectedVersion: number, reason: string) {
|
||||
const response = await http.post<ApiEnvelope<RawResource>, ApiEnvelope<RawResource>>(`${basePath}/bills/${billId}/void`, { expectedVersion, reason }, mutationHeaders('bill-void'))
|
||||
return normalizeBillDetail(unwrapApiResponse(response))
|
||||
}
|
||||
|
||||
export async function listFinanceReceipts(customerId?: string) {
|
||||
const response = await http.get<ApiEnvelope<RawPage>, ApiEnvelope<RawPage>>(`${basePath}/receipts`, { params: { customerId: customerId || undefined } })
|
||||
const payload = unwrapApiResponse(response)
|
||||
@@ -64,7 +99,7 @@ export async function listFinanceCosts(customerId?: string) {
|
||||
return normalizePage<FinanceCostResource>(unwrapApiResponse(response), (item) => ({
|
||||
id: stringValue(item.id), customerId: stringValue(item.tenant_id), costType: stringValue(item.cost_type) as FinanceCostResource['costType'],
|
||||
orderId: item.order_id == null ? null : stringValue(item.order_id), containerId: item.container_id == null ? null : stringValue(item.container_id),
|
||||
expenseName: stringValue(item.expense_name), amount: numberValue(item.amount), currency: 'CNY',
|
||||
expenseName: stringValue(item.fee_item_name ?? item.expense_name), amount: numberValue(item.cost_amount ?? item.amount), currency: 'CNY',
|
||||
voucherFileIds: fileIds(item.voucher_file_ids), createdAt: stringValue(item.created_at),
|
||||
}))
|
||||
}
|
||||
@@ -75,6 +110,6 @@ export async function createFinanceCost(payload: Record<string, unknown>, vouche
|
||||
}
|
||||
|
||||
export async function issueFinanceBill(billId: string, expectedVersion: number) {
|
||||
const response = await http.post<ApiEnvelope<FinanceBillResource>, ApiEnvelope<FinanceBillResource>>(`${basePath}/bills/${billId}/issue`, { expectedVersion }, { headers: { 'Idempotency-Key': idempotencyKey('bill-issue') } })
|
||||
const response = await http.post<ApiEnvelope<FinanceBillResource>, ApiEnvelope<FinanceBillResource>>(`${basePath}/bills/${billId}/issue`, { expectedVersion }, mutationHeaders('bill-issue'))
|
||||
return unwrapApiResponse(response)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user