bettersight-statement-extra.../frontend/src/components/TransactionsTable.vue

212 lines
11 KiB
Vue

<script setup>
/**
* TransactionsTable.vue
*
* Opening/closing balance inputs + their calculated readouts, the
* Hide/Show Source Doc toggle, and the transactions table itself. Always
* rendered (not gated on an extraction result existing) so the balance
* fields are enterable BEFORE clicking Extract, since they're submitted
* as part of that same request - the table itself shows an empty state
* until there's a result to display.
*
* The opening-balance readout is deliberately a snapshot of the
* ORIGINAL extraction's own opening_balance - the backend never returns
* a new one on recheck (only status/checks change), so this stays
* correct without needing special-casing; only its green/not-green
* "match" state follows the live statement_balance check outcome, same
* asymmetry the original vanilla-JS version had (confirmed by reading
* it: openingCalc's text is set once in renderResult and never touched
* again by applyCheckResults, while closingCalc's text IS recomputed on
* every recheck).
*/
import { ref, computed } from 'vue'
import { useExtractionStore } from '../stores/extractionStore'
import { useUiStore } from '../stores/uiStore'
import { fmtMoney } from '../services/formatService'
import * as extractionService from '../services/extractionService'
import { useTransactionFilters } from '../composables/useTransactionFilters'
import TransactionRow from './TransactionRow.vue'
const extractionStore = useExtractionStore()
const uiStore = useUiStore()
const result = computed(() => extractionStore.currentResult)
const transactions = computed(() => result.value?.transactions || [])
const { sortKey, sortDir, setSort, filters, clearFilters, hasActiveFilters, statusOptions, visibleRows } =
useTransactionFilters(transactions)
const balanceCheck = computed(() => result.value?.checks?.find((c) => c.name === 'statement_balance'))
const balanceMatches = computed(() => balanceCheck.value?.outcome === 'pass')
const openingCalcText = computed(() => {
const v = result.value?.opening_balance
return v !== null && v !== undefined ? 'Calculated: ' + fmtMoney(v) : ''
})
const closingCalcText = computed(() => {
const v = balanceCheck.value?.calculated_closing
return v !== undefined && v !== null ? 'Calculated: ' + fmtMoney(v) : ''
})
const toggleLabel = computed(() => (uiStore.pdfColHidden ? 'Show Source Doc' : 'Hide Source Doc'))
// The grey (non-matching) calc-readout on its own doesn't say WHY -
// surface the statement_balance check's own detail text (it already
// has the full math and the "off by $X" amount, no need to re-derive
// that client-side) right under the fields where the mismatch is
// actually visible, instead of leaving the user to find the same line
// in the (collapsed-by-default) checks list elsewhere on the page.
// Also covers "not_available" (neither a typed nor an auto-detected
// balance exists at all) - that outcome has its own explanatory detail
// text too and was previously left just as silent as "fail" used to be.
const balanceMismatchText = computed(() => {
const check = balanceCheck.value
return check?.outcome === 'fail' || check?.outcome === 'not_available' ? check.detail : ''
})
// "fail" gets a leading "Balance doesn't match the statement —" since the
// detail text itself is just the raw math; "not_available"'s own detail
// text already reads as a complete sentence, so it needs no prefix.
const balanceMismatchPrefix = computed(() => (balanceCheck.value?.outcome === 'fail' ? "Balance doesn't match the statement — " : ''))
// ── Balance recheck ──────────────────────────────────────────────────
// Entering opening/closing balance AFTER extraction (e.g. the user
// forgot to type it before clicking Extract) doesn't update anything on
// its own - typing alone was never wired to a recheck, same as the
// original vanilla-JS version (it only rechecked on a row save or a
// download). This button is the explicit, deliberate trigger - same
// recheck-only (no re-extraction) call a row Save already makes.
const rechecking = ref(false)
async function handleRecheck() {
if (!result.value || rechecking.value) return
rechecking.value = true
extractionStore.clearError()
try {
const checkResult = await extractionService.recheck(
result.value,
extractionStore.effectiveOpeningBalance,
extractionStore.effectiveClosingBalance,
)
if (checkResult.error) {
extractionStore.setError(checkResult.error)
} else {
extractionStore.applyCheckResults(checkResult)
}
} catch (e) {
extractionStore.setError('Network error: ' + e.message)
}
rechecking.value = false
}
</script>
<template>
<div class="txn-col-head">
<h2>Transactions</h2>
<button class="btn-toggle" type="button" @click="uiStore.togglePdfCol">{{ toggleLabel }}</button>
</div>
<div class="fields-row">
<div class="fields">
<div>
<label>Opening balance (optional)</label>
<input v-model="extractionStore.openingBalanceInput" type="text" inputmode="decimal" placeholder="e.g. 90,569.13">
<div class="calc-readout" :class="{ match: balanceMatches }">{{ openingCalcText }}</div>
</div>
<div>
<label>Closing balance (optional)</label>
<input v-model="extractionStore.closingBalanceInput" type="text" inputmode="decimal" placeholder="e.g. 88,479.50">
<div class="calc-readout" :class="{ match: balanceMatches }">{{ closingCalcText }}</div>
</div>
</div>
<div class="recheck-wrap">
<label class="recheck-spacer">&nbsp;</label>
<button class="btn-toggle btn-recheck" type="button" :disabled="!result || rechecking" @click="handleRecheck">
{{ rechecking ? 'Checking…' : 'Recheck Balance' }}
</button>
</div>
</div>
<div v-if="balanceMismatchText" class="error-note visible">{{ balanceMismatchPrefix }}{{ balanceMismatchText }}</div>
<div class="note">This statement format may not print a clean opening/closing balance — enter them here to run the full statement balance check; otherwise it's marked "not available" rather than guessed.</div>
<div v-if="result" class="note">Click the pencil on a row to edit it, then Save to commit - edits are saved to this browser as you confirm them, row by row.</div>
<div v-if="!result" class="empty-panel empty-panel-table">
<p>No Transactions to Display.<br>Upload a source document to get started.</p>
</div>
<div v-else class="table-wrap">
<table>
<colgroup>
<col style="width:34px">
<col style="width:120px">
<col>
<col style="width:120px">
<col style="width:135px">
<col style="width:95px">
</colgroup>
<thead>
<tr>
<th></th>
<th>
Date
<span class="sort-arrows">
<button type="button" class="sort-arrow" :class="{ active: sortKey === 'date' && sortDir === 'asc' }" title="Sort by date, oldest first" @click="setSort('date', 'asc')">&#9650;</button>
<button type="button" class="sort-arrow" :class="{ active: sortKey === 'date' && sortDir === 'desc' }" title="Sort by date, newest first" @click="setSort('date', 'desc')">&#9660;</button>
</span>
</th>
<th>
Description
<span class="sort-arrows">
<button type="button" class="sort-arrow" :class="{ active: sortKey === 'description' && sortDir === 'asc' }" title="Sort A-Z" @click="setSort('description', 'asc')">&#9650;</button>
<button type="button" class="sort-arrow" :class="{ active: sortKey === 'description' && sortDir === 'desc' }" title="Sort Z-A" @click="setSort('description', 'desc')">&#9660;</button>
</span>
</th>
<th class="amt">
Amount
<span class="sort-arrows">
<button type="button" class="sort-arrow" :class="{ active: sortKey === 'amount' && sortDir === 'asc' }" title="Sort lowest first" @click="setSort('amount', 'asc')">&#9650;</button>
<button type="button" class="sort-arrow" :class="{ active: sortKey === 'amount' && sortDir === 'desc' }" title="Sort highest first" @click="setSort('amount', 'desc')">&#9660;</button>
</span>
</th>
<th class="amt">
Balance
<span class="sort-arrows">
<button type="button" class="sort-arrow" :class="{ active: sortKey === 'balance' && sortDir === 'asc' }" title="Sort lowest first" @click="setSort('balance', 'asc')">&#9650;</button>
<button type="button" class="sort-arrow" :class="{ active: sortKey === 'balance' && sortDir === 'desc' }" title="Sort highest first" @click="setSort('balance', 'desc')">&#9660;</button>
</span>
</th>
<th>
Status
<span class="sort-arrows">
<button type="button" class="sort-arrow" :class="{ active: sortKey === 'status' && sortDir === 'asc' }" title="Sort A-Z" @click="setSort('status', 'asc')">&#9650;</button>
<button type="button" class="sort-arrow" :class="{ active: sortKey === 'status' && sortDir === 'desc' }" title="Sort Z-A" @click="setSort('status', 'desc')">&#9660;</button>
</span>
</th>
</tr>
<tr class="filter-row">
<th class="row-actions">
<button v-if="hasActiveFilters" type="button" class="clear-filters-btn" title="Clear all filters" @click="clearFilters">&#10005;</button>
</th>
<th><input v-model="filters.date" type="text" class="filter-input" placeholder="Filter…"></th>
<th><input v-model="filters.description" type="text" class="filter-input" placeholder="Search description…"></th>
<th class="amt"><input v-model="filters.amount" type="text" class="filter-input" placeholder="Filter…"></th>
<th class="amt"><input v-model="filters.balance" type="text" class="filter-input" placeholder="Filter…"></th>
<th>
<select v-model="filters.status" class="filter-input filter-select">
<option value="">All</option>
<option v-for="s in statusOptions" :key="s" :value="s">{{ s }}</option>
</select>
</th>
</tr>
</thead>
<tbody>
<tr v-if="visibleRows.length === 0">
<td colspan="6" class="no-rows-note">No transactions match the current filters.</td>
</tr>
<TransactionRow
v-for="entry in visibleRows"
:key="entry.index"
:index="entry.index"
:transaction="entry.transaction"
:is-flagged="extractionStore.flaggedIndexes.has(entry.index)"
/>
</tbody>
</table>
</div>
</template>