2026-05-13 03:37:47 +00:00
|
|
|
import { apiRequest } from './api.js'
|
|
|
|
|
|
2026-06-06 17:19:07 +08:00
|
|
|
export const REIMBURSEMENT_LIST_PREVIEW_PARAMS = Object.freeze({ page: 1, pageSize: 100 })
|
2026-06-09 08:32:00 +00:00
|
|
|
export const REIMBURSEMENT_LIST_FULL_PAGE_SIZE = 100
|
|
|
|
|
const REIMBURSEMENT_LIST_MAX_PAGES = 200
|
2026-06-06 17:19:07 +08:00
|
|
|
|
2026-05-29 14:11:06 +08:00
|
|
|
function buildListQuery(params = {}) {
|
|
|
|
|
const search = new URLSearchParams()
|
|
|
|
|
const page = params.page
|
|
|
|
|
const pageSize = params.pageSize || params.page_size
|
|
|
|
|
|
|
|
|
|
if (page) {
|
|
|
|
|
search.set('page', String(page))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pageSize) {
|
|
|
|
|
search.set('page_size', String(pageSize))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const query = search.toString()
|
|
|
|
|
return query ? `?${query}` : ''
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-06 17:19:07 +08:00
|
|
|
export function extractExpenseClaimItems(payload) {
|
|
|
|
|
if (Array.isArray(payload)) {
|
|
|
|
|
return payload
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Array.isArray(payload?.items) ? payload.items : []
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-09 08:32:00 +00:00
|
|
|
function isPaginatedExpenseClaimPayload(payload) {
|
|
|
|
|
return Boolean(
|
|
|
|
|
payload
|
|
|
|
|
&& typeof payload === 'object'
|
|
|
|
|
&& Array.isArray(payload.items)
|
|
|
|
|
&& Number.isFinite(Number(payload.page))
|
|
|
|
|
&& Number.isFinite(Number(payload.page_size))
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function fetchAllExpenseClaimPages(fetchPage, params = {}) {
|
|
|
|
|
const pageSize = Number(params.pageSize || params.page_size || REIMBURSEMENT_LIST_FULL_PAGE_SIZE)
|
|
|
|
|
const normalizedPageSize = Number.isFinite(pageSize) && pageSize > 0
|
|
|
|
|
? Math.min(REIMBURSEMENT_LIST_FULL_PAGE_SIZE, Math.floor(pageSize))
|
|
|
|
|
: REIMBURSEMENT_LIST_FULL_PAGE_SIZE
|
|
|
|
|
let page = Math.max(1, Math.floor(Number(params.page || 1) || 1))
|
|
|
|
|
const items = []
|
|
|
|
|
|
|
|
|
|
for (let index = 0; index < REIMBURSEMENT_LIST_MAX_PAGES; index += 1) {
|
|
|
|
|
const payload = await fetchPage({
|
|
|
|
|
...params,
|
|
|
|
|
page,
|
|
|
|
|
pageSize: normalizedPageSize
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (!isPaginatedExpenseClaimPayload(payload)) {
|
|
|
|
|
return extractExpenseClaimItems(payload)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
items.push(...extractExpenseClaimItems(payload))
|
|
|
|
|
|
|
|
|
|
const total = Number(payload.total || 0)
|
|
|
|
|
const totalPages = Number(payload.total_pages || 0)
|
|
|
|
|
const hasNext = Boolean(payload.has_next)
|
|
|
|
|
if (!hasNext || (totalPages && page >= totalPages) || (total && items.length >= total)) {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
page += 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return items
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-22 15:56:06 +08:00
|
|
|
export function fetchExpenseClaims(params = {}, options = {}) {
|
|
|
|
|
return apiRequest(`/reimbursements/claims${buildListQuery(params)}`, options)
|
2026-05-13 03:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
2026-06-09 08:32:00 +00:00
|
|
|
export function fetchAllExpenseClaims(params = {}) {
|
|
|
|
|
return fetchAllExpenseClaimPages(fetchExpenseClaims, params)
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-22 15:56:06 +08:00
|
|
|
export function fetchApprovalExpenseClaims(params = {}, options = {}) {
|
|
|
|
|
return apiRequest(`/reimbursements/claims/approvals${buildListQuery(params)}`, options)
|
2026-05-20 21:00:47 +08:00
|
|
|
}
|
|
|
|
|
|
2026-06-09 08:32:00 +00:00
|
|
|
export function fetchAllApprovalExpenseClaims(params = {}) {
|
|
|
|
|
return fetchAllExpenseClaimPages(fetchApprovalExpenseClaims, params)
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-22 15:56:06 +08:00
|
|
|
export function fetchArchivedExpenseClaims(params = {}, options = {}) {
|
|
|
|
|
return apiRequest(`/reimbursements/claims/archives${buildListQuery(params)}`, options)
|
2026-05-22 16:00:19 +08:00
|
|
|
}
|
|
|
|
|
|
2026-06-09 08:32:00 +00:00
|
|
|
export function fetchAllArchivedExpenseClaims(params = {}) {
|
|
|
|
|
return fetchAllExpenseClaimPages(fetchArchivedExpenseClaims, params)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-13 03:37:47 +00:00
|
|
|
export function fetchExpenseClaimDetail(claimId) {
|
|
|
|
|
return apiRequest(`/reimbursements/claims/${encodeURIComponent(String(claimId || '').trim())}`)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-27 17:31:27 +08:00
|
|
|
export function fetchExpenseClaimBudgetAnalysis(claimId) {
|
|
|
|
|
return apiRequest(`/reimbursements/claims/${encodeURIComponent(String(claimId || '').trim())}/budget-analysis`)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-28 12:09:49 +08:00
|
|
|
export function fetchEmployeeLatestProfile(employeeId, params = {}) {
|
|
|
|
|
const query = new URLSearchParams()
|
|
|
|
|
Object.entries(params || {}).forEach(([key, value]) => {
|
|
|
|
|
const normalized = String(value ?? '').trim()
|
|
|
|
|
if (normalized) {
|
|
|
|
|
query.set(key, normalized)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
const suffix = query.toString() ? `?${query.toString()}` : ''
|
|
|
|
|
return apiRequest(`/employee-profiles/${encodeURIComponent(String(employeeId || '').trim())}/latest${suffix}`)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-28 16:24:59 +08:00
|
|
|
export function fetchCurrentEmployeeLatestProfile(params = {}) {
|
|
|
|
|
const query = new URLSearchParams()
|
|
|
|
|
Object.entries(params || {}).forEach(([key, value]) => {
|
|
|
|
|
const normalized = String(value ?? '').trim()
|
|
|
|
|
if (normalized) {
|
|
|
|
|
query.set(key, normalized)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
const suffix = query.toString() ? `?${query.toString()}` : ''
|
|
|
|
|
return apiRequest(`/employee-profiles/me/latest${suffix}`)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-21 10:57:06 +08:00
|
|
|
export function updateExpenseClaim(claimId, payload = {}) {
|
|
|
|
|
return apiRequest(`/reimbursements/claims/${encodeURIComponent(String(claimId || '').trim())}`, {
|
|
|
|
|
method: 'PATCH',
|
|
|
|
|
body: JSON.stringify(payload)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-03 17:31:40 +08:00
|
|
|
export function acceptExpenseClaimStandardAdjustment(claimId, payload = {}) {
|
|
|
|
|
return apiRequest(`/reimbursements/claims/${encodeURIComponent(String(claimId || '').trim())}/standard-adjustment`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: JSON.stringify(payload)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-21 09:28:33 +08:00
|
|
|
export function calculateTravelReimbursement(payload = {}) {
|
|
|
|
|
return apiRequest('/reimbursements/travel-calculator', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: JSON.stringify(payload)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-13 06:49:58 +00:00
|
|
|
export function createExpenseClaimItem(claimId, payload = {}) {
|
|
|
|
|
return apiRequest(`/reimbursements/claims/${encodeURIComponent(String(claimId || '').trim())}/items`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: JSON.stringify(payload)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function deleteExpenseClaimItem(claimId, itemId) {
|
|
|
|
|
return apiRequest(`/reimbursements/claims/${encodeURIComponent(String(claimId || '').trim())}/items/${encodeURIComponent(String(itemId || '').trim())}`, {
|
|
|
|
|
method: 'DELETE'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function uploadExpenseClaimItemAttachment(claimId, itemId, file) {
|
|
|
|
|
const formData = new FormData()
|
|
|
|
|
formData.append('file', file)
|
2026-05-29 14:51:18 +08:00
|
|
|
if (file?.receiptId) {
|
|
|
|
|
formData.append('receipt_id', String(file.receiptId))
|
|
|
|
|
}
|
2026-05-13 06:49:58 +00:00
|
|
|
|
|
|
|
|
return apiRequest(`/reimbursements/claims/${encodeURIComponent(String(claimId || '').trim())}/items/${encodeURIComponent(String(itemId || '').trim())}/attachment`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: formData,
|
|
|
|
|
contentType: null
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchExpenseClaimItemAttachmentMeta(claimId, itemId) {
|
|
|
|
|
return apiRequest(`/reimbursements/claims/${encodeURIComponent(String(claimId || '').trim())}/items/${encodeURIComponent(String(itemId || '').trim())}/attachment/meta`)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-14 15:42:47 +00:00
|
|
|
export function fetchExpenseClaimItemAttachmentPreview(claimId, itemId) {
|
|
|
|
|
return apiRequest(`/reimbursements/claims/${encodeURIComponent(String(claimId || '').trim())}/items/${encodeURIComponent(String(itemId || '').trim())}/attachment/preview`, {
|
|
|
|
|
responseType: 'blob'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-13 06:49:58 +00:00
|
|
|
export function fetchExpenseClaimItemAttachment(claimId, itemId) {
|
|
|
|
|
return apiRequest(`/reimbursements/claims/${encodeURIComponent(String(claimId || '').trim())}/items/${encodeURIComponent(String(itemId || '').trim())}/attachment`, {
|
|
|
|
|
responseType: 'blob'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-14 15:42:47 +00:00
|
|
|
export async function fetchExpenseClaimAttachmentAsset(pathOrUrl) {
|
|
|
|
|
const target = String(pathOrUrl || '').trim()
|
|
|
|
|
if (!target) {
|
|
|
|
|
throw new Error('预览地址为空。')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (/^https?:\/\//i.test(target)) {
|
|
|
|
|
const response = await fetch(target)
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`预览加载失败(${response.status})。`)
|
|
|
|
|
}
|
|
|
|
|
return response.blob()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return apiRequest(target, {
|
|
|
|
|
responseType: 'blob'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-13 06:49:58 +00:00
|
|
|
export function deleteExpenseClaimItemAttachment(claimId, itemId) {
|
|
|
|
|
return apiRequest(`/reimbursements/claims/${encodeURIComponent(String(claimId || '').trim())}/items/${encodeURIComponent(String(itemId || '').trim())}/attachment`, {
|
|
|
|
|
method: 'DELETE'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-13 03:37:47 +00:00
|
|
|
export function updateExpenseClaimItem(claimId, itemId, payload) {
|
|
|
|
|
return apiRequest(`/reimbursements/claims/${encodeURIComponent(String(claimId || '').trim())}/items/${encodeURIComponent(String(itemId || '').trim())}`, {
|
|
|
|
|
method: 'PATCH',
|
|
|
|
|
body: JSON.stringify(payload)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function submitExpenseClaim(claimId) {
|
|
|
|
|
return apiRequest(`/reimbursements/claims/${encodeURIComponent(String(claimId || '').trim())}/submit`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: JSON.stringify({})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-20 14:21:56 +08:00
|
|
|
export function returnExpenseClaim(claimId, payload = {}) {
|
|
|
|
|
return apiRequest(`/reimbursements/claims/${encodeURIComponent(String(claimId || '').trim())}/return`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: JSON.stringify(payload)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-20 21:00:47 +08:00
|
|
|
export function approveExpenseClaim(claimId, payload = {}) {
|
|
|
|
|
return apiRequest(`/reimbursements/claims/${encodeURIComponent(String(claimId || '').trim())}/approve`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: JSON.stringify(payload)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-28 12:09:49 +08:00
|
|
|
export function payExpenseClaim(claimId) {
|
|
|
|
|
return apiRequest(`/reimbursements/claims/${encodeURIComponent(String(claimId || '').trim())}/pay`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: JSON.stringify({})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-13 03:37:47 +00:00
|
|
|
export function deleteExpenseClaim(claimId) {
|
|
|
|
|
return apiRequest(`/reimbursements/claims/${encodeURIComponent(String(claimId || '').trim())}`, {
|
|
|
|
|
method: 'DELETE'
|
|
|
|
|
})
|
|
|
|
|
}
|