Files
syops/frontend/src/features/workspace/api/voucherCheck.api.ts
T

295 lines
8.7 KiB
TypeScript

/**
* features/workspace/api/voucherCheck.api.ts
* 用途:工作台立账凭证检核任务列表、最近一次检核、批次明细、企微发送记录、通知配置与强制重检 API 客户端。
* 依赖:@/shared/api/client、getAuthHeaders。
* 逻辑:AccountingVoucherCheckView / DetailView / VoucherCheckNotifyConfigPanel 调用;
* 对应 /api/workspace/accounting-voucher-check/* 与排程 Excel 下载。
*/
import { requestJson } from '@/shared/api/client';
import { getAuthHeaders } from '@/shared/utils/auth';
export interface VoucherCheckRunItem {
id: number;
schedule_run_id?: number | null;
run_no: string;
profile_code: string;
period_month: string | null;
mode: string;
status: string;
subject_count: number;
missing_file_count: number;
missing_invoice_count: number;
technical_error_count: number;
staged_file_count: number;
finished_at?: string | null;
error_message?: string | null;
}
export interface VoucherCheckRunsResponse {
page: number;
page_size: number;
total: number;
items: VoucherCheckRunItem[];
}
export interface VoucherCheckRunInvoiceItem {
id: number;
invoice_no: string;
result: string;
status_label?: string | null;
missing_materials?: Array<{ code?: string; name?: string }>;
missing_count?: number | null;
cells?: Record<string, string>;
checked_at?: string | null;
}
export interface VoucherCheckFileTypeSummary {
rule_code: string;
rule_name: string;
missing_file_count: number;
missing_invoice_count: number;
missing_ratio: number;
}
export interface VoucherCheckRunItemsResponse {
run_id: number;
engine_run_id?: number;
run_no: string;
page: number;
page_size: number;
total: number;
items: VoucherCheckRunInvoiceItem[];
subject_count?: number;
by_file_type?: VoucherCheckFileTypeSummary[];
}
export interface VoucherCheckForceRunResponse {
schedule_task_id: number;
schedule_run_id: number;
run_no: string;
job_id: number;
mode: string;
status: string;
}
export interface VoucherCheckLatestStep {
step: string;
label?: string | null;
processed_count: number;
}
export interface VoucherCheckLatestRun {
schedule_run_id: number | null;
engine_run_id?: number | null;
run_no: string;
period_month: string | null;
mode: string;
status: string;
progress_percent: number;
subject_count: number;
archived_file_count: number;
missing_file_count: number;
missing_invoice_count: number;
processed_count: number;
started_at?: string | null;
finished_at?: string | null;
error_message?: string | null;
current_step?: VoucherCheckLatestStep | null;
}
/**
* 读取最近一次立账凭证检核(含排队/执行中),不跟列表 period/status。
* @returns 无任何批次时为 null
*/
export function getLatestVoucherCheckRun() {
return requestJson<VoucherCheckLatestRun | null>(
'/api/workspace/accounting-voucher-check/runs/latest',
{ headers: getAuthHeaders() },
);
}
/**
* 分页查询检核任务列表。
* @param params.period - YYYY 表示该年全部月份,YYYY-MM 表示单月
*/
export function listVoucherCheckRuns(params: {
period?: string;
profile_code?: string;
status?: string;
page?: number;
page_size?: number;
}) {
const q = new URLSearchParams();
if (params.period) q.set('period', params.period);
if (params.profile_code) q.set('profile_code', params.profile_code);
if (params.status) q.set('status', params.status);
if (params.page) q.set('page', String(params.page));
if (params.page_size) q.set('page_size', String(params.page_size));
const qs = q.toString();
return requestJson<VoucherCheckRunsResponse>(
`/api/workspace/accounting-voucher-check/runs${qs ? `?${qs}` : ''}`,
{ headers: getAuthHeaders() },
);
}
/**
* 分页查询指定排程批次的 invoice 明细,可按 result 和 invoice 号过滤;同时返回 by_file_type。
* @param params.invoice_no - 包含匹配,忽略大小写;空表示不按号筛
*/
export function listVoucherCheckRunItems(runId: number, params?: { result?: string; invoice_no?: string; page?: number; page_size?: number }) {
const q = new URLSearchParams();
if (params?.result) q.set('result', params.result);
if (params?.invoice_no) q.set('invoice_no', params.invoice_no);
if (params?.page) q.set('page', String(params.page));
if (params?.page_size) q.set('page_size', String(params.page_size));
const qs = q.toString();
return requestJson<VoucherCheckRunItemsResponse>(
`/api/workspace/accounting-voucher-check/runs/${runId}/items${qs ? `?${qs}` : ''}`,
{ headers: getAuthHeaders() },
);
}
export interface VoucherCheckNotificationItem {
id: number;
run_id: number;
schedule_run_id?: number | null;
run_no?: string | null;
period_month?: string | null;
subject_count: number;
missing_file_count: number;
missing_invoice_count: number;
send_status: string;
skip_reason?: string | null;
excel_filename?: string | null;
has_excel?: boolean;
recipient_names?: string[];
sent_at?: string | null;
created_at?: string | null;
}
export interface VoucherCheckNotificationsResponse {
page: number;
page_size: number;
total: number;
items: VoucherCheckNotificationItem[];
}
/**
* 分页查询企微缺件汇总发送记录。
* @param params.period - YYYY 表示该年全部月份,YYYY-MM 表示单月
*/
export function listVoucherCheckNotifications(params?: {
period?: string;
send_status?: string;
page?: number;
page_size?: number;
}) {
const q = new URLSearchParams();
if (params?.period) q.set('period', params.period);
if (params?.send_status) q.set('send_status', params.send_status);
if (params?.page) q.set('page', String(params.page));
if (params?.page_size) q.set('page_size', String(params.page_size));
const qs = q.toString();
return requestJson<VoucherCheckNotificationsResponse>(
`/api/workspace/accounting-voucher-check/notifications${qs ? `?${qs}` : ''}`,
{ headers: getAuthHeaders() },
);
}
/**
* 下载该排程批次的检核结果 Excel。
* @param scheduleRunId - ScheduleRun.id
* @param filename - 回退文件名(无 Content-Disposition 时使用)
*/
export async function downloadVoucherCheckExcel(scheduleRunId: number, filename?: string | null) {
const res = await fetch(`/api/workspace/schedule-runs/${scheduleRunId}/excel`, { headers: getAuthHeaders() });
if (!res.ok) {
throw new Error('excel');
}
const blob = await res.blob();
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
const disposition = res.headers.get('content-disposition') || '';
const encoded = disposition.match(/filename\*=UTF-8''([^;]+)/i)?.[1];
const plain = disposition.match(/filename="?([^";]+)"?/i)?.[1];
const raw = encoded || plain || filename || 'voucher_check.xlsx';
try {
link.download = decodeURIComponent(raw);
} catch {
link.download = raw;
}
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
}
export interface VoucherCheckNotifyUser {
id: number;
username: string;
real_name?: string | null;
employee_id?: string | null;
department?: string | null;
wecom_bound: boolean;
}
export interface VoucherCheckNotifyRule {
code: string;
name: string;
type_folder: string;
user_ids: number[];
}
export interface VoucherCheckNotifyConfig {
profile_code: string;
rules: VoucherCheckNotifyRule[];
users: VoucherCheckNotifyUser[];
updated_at?: string | null;
updated_by?: number | null;
}
/**
* 读取按文件类型的企微接收人(仅平台管理员)。
*/
export function getVoucherCheckNotifyConfig(profileCode?: string) {
const q = new URLSearchParams();
if (profileCode) q.set('profile_code', profileCode);
const qs = q.toString();
return requestJson<VoucherCheckNotifyConfig>(
`/api/workspace/accounting-voucher-check/notify-config${qs ? `?${qs}` : ''}`,
{ headers: getAuthHeaders() },
);
}
/**
* 保存按文件类型的企微接收人(仅平台管理员)。
*/
export function saveVoucherCheckNotifyConfig(payload: {
profile_code?: string;
recipients: Record<string, number[]>;
}) {
return requestJson<VoucherCheckNotifyConfig>(
'/api/workspace/accounting-voucher-check/notify-config',
{
method: 'PUT',
headers: { ...getAuthHeaders(), 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
},
);
}
/**
* 强制重检上一个自然月(不走增量跳过)。
*/
export function triggerVoucherCheckForceAll() {
return requestJson<VoucherCheckForceRunResponse>(
'/api/workspace/accounting-voucher-check/runs',
{
method: 'POST',
headers: { ...getAuthHeaders(), 'Content-Type': 'application/json' },
body: JSON.stringify({ mode: 'force_all' }),
},
);
}