143 lines
5.9 KiB
JavaScript
143 lines
5.9 KiB
JavaScript
/**
|
|
* Business logic for extracting and re-checking a statement - calls
|
|
* repositories only, never touches fetch() directly. Document
|
|
* persistence itself (what used to live here, localStorage-backed)
|
|
* moved to documentsService.js/PocketBase - see CLAUDE.md's "PocketBase"
|
|
* entry for the full migration record.
|
|
*/
|
|
import * as extractionRepository from '../repositories/extractionRepository'
|
|
|
|
/**
|
|
* The docKey for one source file - keyed by the SOURCE file's own
|
|
* name+size (stable across re-selecting the same file), used as
|
|
* documentsService's own docKey field, not a localStorage key anymore.
|
|
*/
|
|
export function docKeyFor(file) {
|
|
return 'bse:doc:' + file.name + ':' + file.size
|
|
}
|
|
|
|
/**
|
|
* Whatever the user typed always wins; an intentionally blank field (no
|
|
* auto-fill, per the UI's own calculated-balance readouts) falls back to
|
|
* the statement's own auto-detected figure from the initial extract -
|
|
* otherwise a row edit's recheck/export would lose that figure entirely
|
|
* the moment the visible field is empty.
|
|
*/
|
|
export function effectiveBalance(typedValue, fallbackValue) {
|
|
return typedValue !== null && typedValue !== undefined ? typedValue : (fallbackValue ?? null)
|
|
}
|
|
|
|
/**
|
|
* Submits a chosen file (+ optional typed balances) for extraction.
|
|
*
|
|
* `forcedMapping` (layout mapper work - see useLayoutMapping.js) - when
|
|
* given, extracts directly from that mapping's own column positions
|
|
* instead of auto-detection; omitted on every normal upload. A response
|
|
* carrying `needs_mapping: true` (both the keyword detector and the
|
|
* layout_quirks fallback failed) is what useLayoutMapping.js's own
|
|
* resolveNeedsMapping() reacts to - this function itself doesn't
|
|
* special-case that response shape at all, it's just whatever JSON
|
|
* /api/process returned.
|
|
*/
|
|
export async function process(file, openingBalanceInput, closingBalanceInput, forcedMapping = null) {
|
|
const fd = new FormData()
|
|
fd.append('statement_file', file)
|
|
if (openingBalanceInput !== null) fd.append('opening_balance', openingBalanceInput)
|
|
if (closingBalanceInput !== null) fd.append('closing_balance', closingBalanceInput)
|
|
if (forcedMapping) fd.append('forced_mapping', JSON.stringify(forcedMapping))
|
|
return extractionRepository.processStatement(fd)
|
|
}
|
|
|
|
function recheckPayload(result, openingBalance, closingBalance) {
|
|
return {
|
|
transactions: result.transactions,
|
|
page_reports: result.page_reports,
|
|
narrative_direction: result.narrative_direction,
|
|
period: result.period,
|
|
opening_balance: openingBalance,
|
|
closing_balance: closingBalance,
|
|
}
|
|
}
|
|
|
|
export async function recheck(result, openingBalance, closingBalance) {
|
|
return extractionRepository.recheckTransactions(recheckPayload(result, openingBalance, closingBalance))
|
|
}
|
|
|
|
/**
|
|
* `categorization` (optional) - useCategorization.js's own
|
|
* summaryForExport, computed client-side from the active document's
|
|
* project rules (see CLAUDE.md's "Transaction categorization" entry).
|
|
* Omitted entirely for a document this wasn't computed for (e.g.
|
|
* TransactionsTable.vue's "Download All" loop, which only has the ACTIVE
|
|
* document's own categorization instance ready) - excel_service.py's
|
|
* own conditional sheet simply doesn't appear in that case, same as it
|
|
* doesn't for edit_log below.
|
|
*/
|
|
export async function exportWorkbook(result, openingBalance, closingBalance, categorization) {
|
|
const payload = {
|
|
...recheckPayload(result, openingBalance, closingBalance),
|
|
brought_forward_balance: result.brought_forward_balance,
|
|
title: result.title,
|
|
// Optional - excel_service.py only writes an "Edits" sheet when
|
|
// this is non-empty (see CLAUDE.md's "R.5" entry), so a statement
|
|
// with no edits doesn't gain an empty sheet nobody asked for.
|
|
edit_log: result.editLog || [],
|
|
categorization: categorization || null,
|
|
}
|
|
return extractionRepository.exportStatement(payload)
|
|
}
|
|
|
|
/**
|
|
* QuickBooks (Web Connect .qbo)-compatible export - `bankAccount` is a
|
|
* plain {bankId, accountId, accountType} object, resolved from a
|
|
* bankAccountProfiles record the caller already fetched (see
|
|
* bankAccountProfilesService.js) - this function doesn't touch
|
|
* PocketBase itself, same boundary extractionService.js already keeps
|
|
* everywhere else. No categorization/edit_log - the QBO format has no
|
|
* equivalent of either (a bank transaction file, not a workbook with
|
|
* extra sheets).
|
|
*
|
|
* Renamed from this function's own earlier exportQfx 2026-11-19 - see
|
|
* CLAUDE.md's ".qbo export" entry for why.
|
|
*/
|
|
export async function exportQbo(result, openingBalance, closingBalance, bankAccount) {
|
|
const payload = {
|
|
...recheckPayload(result, openingBalance, closingBalance),
|
|
brought_forward_balance: result.brought_forward_balance,
|
|
title: result.title,
|
|
bank_account: bankAccount,
|
|
}
|
|
return extractionRepository.exportStatementQbo(payload)
|
|
}
|
|
|
|
/**
|
|
* Xero-compatible statement-import CSV export - no bank-account field
|
|
* needed (Xero's own CSV import has no equivalent of QFX's
|
|
* <BANKACCTFROM> block at all), so this is simpler than exportQfx - no
|
|
* profile-picker flow required on the caller's side either.
|
|
*/
|
|
export async function exportXeroCsv(result, openingBalance, closingBalance) {
|
|
const payload = {
|
|
...recheckPayload(result, openingBalance, closingBalance),
|
|
brought_forward_balance: result.brought_forward_balance,
|
|
title: result.title,
|
|
}
|
|
return extractionRepository.exportStatementXeroCsv(payload)
|
|
}
|
|
|
|
/**
|
|
* A plain, generic transactions CSV export - same Date/Description/
|
|
* Amount/Balance/Status/Check-flags data the Excel export's own
|
|
* Transactions sheet already writes. No categorizationSummary (unlike
|
|
* exportWorkbook) - a flat CSV has no concept of a second "sheet," and
|
|
* that wasn't asked for here.
|
|
*/
|
|
export async function exportCsv(result, openingBalance, closingBalance) {
|
|
const payload = {
|
|
...recheckPayload(result, openingBalance, closingBalance),
|
|
brought_forward_balance: result.brought_forward_balance,
|
|
title: result.title,
|
|
}
|
|
return extractionRepository.exportStatementCsv(payload)
|
|
}
|