The page lists documents, so a purchase with no document at all could not appear on it. That is precisely the gap the receipt hunt exists to close, and the half a user can do something about: fetch the invoice from the supplier's portal, or ask whoever made the purchase. GET /purchases supplies it, read-only, with the portal link attached when the directory knows where that supplier keeps its invoices. Salary and tax get no link: they have no invoice to fetch, and a link there implies somewhere to go. The predicate moves into lib/transactions/purchases-without-underlag.ts, and it is not the hunt's filter copied across. `journal_entry_id IS NULL` is not the same as "not booked": bulk-booking many transactions onto one verifikat records it in transaction_voucher_links, and a payment split across invoices records it in the payment tables, and both leave that column null. The hunt tolerates the false candidate because the worst case is a proposal nobody accepts. A list shown to a person does not: those rows would sit under "saknar underlag" forever, already booked, with nothing the user could do to clear them. So the column filter stays as the cheap indexed first pass and isTransactionBooked settles it afterwards. That predicate is canonical and nothing here re-implements it. The hunt is deliberately not rewired in this change: its query is identical apart from that check, but moving a nightly cron onto new code belongs in its own PR. The thresholds are kept equal to the hunt's so the two cannot drift meanwhile. Removing the booked filter fails five of the seven predicate tests. Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
108 lines
4.5 KiB
TypeScript
108 lines
4.5 KiB
TypeScript
/**
|
|
* Purchases that have no underlag.
|
|
*
|
|
* The receipt hunt has asked this question since it was built, privately, in
|
|
* `fetchCandidates`. The Underlag page needs the same answer to show a purchase
|
|
* that is still missing its paper, so the query lives here now.
|
|
*
|
|
* The hunt is NOT yet migrated onto this: its query is byte-identical apart
|
|
* from the booked check below, but rewiring a nightly cron belongs in its own
|
|
* change with its own tests. The thresholds are deliberately kept equal to the
|
|
* hunt's so the two cannot drift in the meantime, and the hunt's constants are
|
|
* the source of that equality.
|
|
*
|
|
* One thing differs between the two callers, and it matters. The hunt filters on
|
|
* `journal_entry_id IS NULL` alone, which is not the same as "not booked": a
|
|
* bulk-booked transaction (many transactions, one verifikat, joined through
|
|
* transaction_voucher_links) and a payment split across several invoices both
|
|
* leave that column null. The hunt tolerates the false candidate, since the
|
|
* worst case is a proposal nobody accepts. A list shown to a person does not:
|
|
* those rows would sit under "saknar underlag" forever, already booked, with
|
|
* nothing the user could do to clear them.
|
|
*
|
|
* So the column filter stays as the cheap first pass the database can index,
|
|
* and `isTransactionBooked` settles it afterwards with the two link tables it
|
|
* needs. That predicate is canonical; nothing here re-implements it.
|
|
*/
|
|
import type { SupabaseClient } from '@supabase/supabase-js'
|
|
import { fetchAllRows } from '@/lib/supabase/fetch-all'
|
|
import { isTransactionBooked } from '@/lib/transactions/is-booked'
|
|
|
|
/**
|
|
* Kept equal to the receipt hunt's own thresholds (MIN_AMOUNT_SEK,
|
|
* LOOKBACK_MONTHS in lib/receipt-hunt/hunt.ts). A page that showed purchases
|
|
* the hunt never looks for would offer a row it can never resolve on its own.
|
|
*/
|
|
export const MIN_PURCHASE_AMOUNT_SEK = 100
|
|
export const PURCHASE_LOOKBACK_MONTHS = 12
|
|
|
|
export interface PurchaseWithoutUnderlag {
|
|
id: string
|
|
company_id: string
|
|
date: string
|
|
description: string | null
|
|
merchant_name: string | null
|
|
amount: number
|
|
currency: string | null
|
|
amount_sek: number | null
|
|
exchange_rate: number | null
|
|
journal_entry_id: string | null
|
|
}
|
|
|
|
const COLUMNS =
|
|
'id, company_id, date, description, merchant_name, amount, currency, amount_sek, exchange_rate, journal_entry_id'
|
|
|
|
/**
|
|
* Outflows with no document, not ignored, not privately flagged, and not booked
|
|
* by any of the three routes a transaction can be booked through.
|
|
*
|
|
* Ordered newest first: a receipt for last week is findable, one from a year ago
|
|
* usually is not.
|
|
*/
|
|
export async function fetchPurchasesWithoutUnderlag(
|
|
supabase: SupabaseClient,
|
|
companyId: string,
|
|
options?: { lookbackMonths?: number; minAmountSek?: number },
|
|
): Promise<PurchaseWithoutUnderlag[]> {
|
|
const lookback = options?.lookbackMonths ?? PURCHASE_LOOKBACK_MONTHS
|
|
const minAmount = options?.minAmountSek ?? MIN_PURCHASE_AMOUNT_SEK
|
|
|
|
const since = new Date()
|
|
since.setMonth(since.getMonth() - lookback)
|
|
const sinceDate = since.toISOString().slice(0, 10)
|
|
|
|
const rows = await fetchAllRows<PurchaseWithoutUnderlag>((range) =>
|
|
supabase
|
|
.from('transactions')
|
|
.select(COLUMNS)
|
|
.eq('company_id', companyId)
|
|
.is('journal_entry_id', null)
|
|
.is('document_id', null)
|
|
.eq('is_ignored', false)
|
|
// is_business IS DISTINCT FROM false: NULL is untriaged and true is
|
|
// "business, not yet booked". Only an explicit false means the user
|
|
// called it private, and a private purchase needs no underlag.
|
|
.not('is_business', 'is', false)
|
|
// Outflows only, and amount <= -MIN covers the floor in one filter.
|
|
.lte('amount', -minAmount)
|
|
.gte('date', sinceDate)
|
|
.order('date', { ascending: false })
|
|
.range(range.from, range.to),
|
|
)
|
|
|
|
if (rows.length === 0) return []
|
|
|
|
// The column filter above cannot see these two. Fetched once for the whole
|
|
// page rather than per row.
|
|
const ids = rows.map((r) => r.id)
|
|
const [{ data: voucherLinks }, { data: invoicePayments }, { data: supplierPayments }] =
|
|
await Promise.all([
|
|
supabase.from('transaction_voucher_links').select('transaction_id').in('transaction_id', ids),
|
|
supabase.from('invoice_payments').select('transaction_id').in('transaction_id', ids),
|
|
supabase.from('supplier_invoice_payments').select('transaction_id').in('transaction_id', ids),
|
|
])
|
|
|
|
const payments = [...(invoicePayments ?? []), ...(supplierPayments ?? [])]
|
|
return rows.filter((tx) => !isTransactionBooked(tx, payments, voucherLinks ?? []))
|
|
}
|