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

157 lines
6.8 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 TransactionRow from './TransactionRow.vue'
const extractionStore = useExtractionStore()
const uiStore = useUiStore()
const result = computed(() => extractionStore.currentResult)
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>
<button class="btn-toggle btn-recheck" type="button" :disabled="!result || rechecking" @click="handleRecheck">
{{ rechecking ? 'Checking…' : 'Recheck Balance' }}
</button>
</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</th>
<th>Description</th>
<th class="amt">Amount</th>
<th class="amt">Balance</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<TransactionRow
v-for="(t, i) in result.transactions"
:key="i"
:index="i"
:transaction="t"
:is-flagged="extractionStore.flaggedIndexes.has(i)"
/>
</tbody>
</table>
</div>
</template>