import { api, apiFetchBlob } from '../../api/client'; import type { AggregateKey, AllocationIn, CorrectionAggregateOut, CorrectionIn, CorrectionTableOut, StatementType, } from '../../types/corrections'; const STATEMENT_TYPE: StatementType = 'balance'; export function correctionsPath(versionId: string, key?: AggregateKey): string { const base = `/consolidations/versions/${versionId}/corrections`; if (!key) return base; return `${base}/aggregates/${key.rule_id}/${key.company_1_id}/${key.company_2_id}`; } function vgoAssignmentPath(versionId: string, resource: 'rule' | 'exclude'): string { return `/consolidations/versions/${versionId}/vgo-assignments/${resource}`; } export function getCorrectionTable(versionId: string): Promise { return api.get(correctionsPath(versionId), { statement_type: STATEMENT_TYPE }); } export function saveCorrection(versionId: string, key: AggregateKey, body: CorrectionIn): Promise { return api.put(correctionsPath(versionId, key), body); } /** * Решение по строке ОСВ: нет единой ручки — сервер (arm-core #208) разделил её на привязку/снятие правила и * включение/исключение из КФО (`vgo-assignments/rule`, `vgo-assignments/exclude`), ответ — список решений по * исходным строкам, а не таблица. Таблицу перечитываем сами, чтобы вызывающий код (CorrectionContent, * CorrectionEditorModal) остался на прежнем контракте «действие -> актуальная таблица». */ export async function allocate(versionId: string, body: AllocationIn): Promise { const { statement_type, entry_ids, action } = body; if (action === 'ASSIGN') { if (!body.rule_id) throw new Error('ASSIGN без rule_id'); await api.put(vgoAssignmentPath(versionId, 'rule'), { entry_ids, rule_id: body.rule_id }); } else if (action === 'UNASSIGN' || action === 'RESET') { await api.del(vgoAssignmentPath(versionId, 'rule'), { statement_type, entry_ids }); } else { await api.put(vgoAssignmentPath(versionId, 'exclude'), { statement_type, entry_ids, is_exclude: action === 'EXCLUDE' }); } return getCorrectionTable(versionId); } export function exportCorrectionTable(versionId: string): Promise<{ blob: Blob; filename: string | null }> { return apiFetchBlob(`${correctionsPath(versionId)}/export?statement_type=${STATEMENT_TYPE}`); }