优化
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import { apiRequest } from '@/api/http'
|
||||
import type { FinanceBillDetail, FinanceBillPage, FinanceBillQuery, FinanceReceiptRecord } from '../contracts'
|
||||
import {
|
||||
mapFinanceBillDetail,
|
||||
mapFinanceBills,
|
||||
mapFinanceReceipts,
|
||||
sumDecimalStrings,
|
||||
type FinanceBillWire,
|
||||
type FinanceReceiptWire,
|
||||
} from './adapters'
|
||||
|
||||
/** The customer scope is resolved by the authenticated merchant session. */
|
||||
export async function getMerchantFinanceBills(params: FinanceBillQuery = {}): Promise<FinanceBillPage> {
|
||||
const response = await apiRequest<FinanceBillWire[]>({
|
||||
method: 'GET',
|
||||
url: '/client/finance/v1/bills',
|
||||
})
|
||||
const keyword = params.keyword?.trim().toLocaleLowerCase('zh-CN')
|
||||
const filtered = mapFinanceBills(response).filter((bill) => {
|
||||
const matchesStatus = !params.status || bill.status === params.status
|
||||
const matchesKeyword = !keyword || bill.billNo.toLocaleLowerCase('zh-CN').includes(keyword)
|
||||
|| bill.customerName.toLocaleLowerCase('zh-CN').includes(keyword)
|
||||
return matchesStatus && matchesKeyword
|
||||
})
|
||||
const page = Math.max(1, params.page ?? 1)
|
||||
const size = Math.max(1, params.size ?? 20)
|
||||
const start = (page - 1) * size
|
||||
return {
|
||||
records: filtered.slice(start, start + size),
|
||||
total: filtered.length,
|
||||
page,
|
||||
size,
|
||||
pages: Math.ceil(filtered.length / size),
|
||||
totalBilledAmount: sumDecimalStrings(filtered.map((bill) => bill.totalAmount)),
|
||||
totalReceivedAmount: sumDecimalStrings(filtered.map((bill) => bill.receivedAmount)),
|
||||
totalOutstandingAmount: sumDecimalStrings(filtered.map((bill) => bill.outstandingAmount)),
|
||||
currencyCode: 'CNY',
|
||||
}
|
||||
}
|
||||
|
||||
export async function getMerchantFinanceBill(id: string): Promise<FinanceBillDetail> {
|
||||
const response = await apiRequest<FinanceBillWire>({
|
||||
method: 'GET',
|
||||
url: `/client/finance/v1/bills/${encodeURIComponent(id)}`,
|
||||
})
|
||||
return mapFinanceBillDetail(response)
|
||||
}
|
||||
|
||||
export async function getMerchantFinanceReceipts(billId: string): Promise<FinanceReceiptRecord[]> {
|
||||
const response = await apiRequest<FinanceReceiptWire[]>({
|
||||
method: 'GET',
|
||||
url: '/client/finance/v1/receipts',
|
||||
})
|
||||
return mapFinanceReceipts(response, billId)
|
||||
}
|
||||
Reference in New Issue
Block a user