# Budget Import Status Audit & Workflow Hardening Pattern

Use this reference when auditing FinanceManager budget imports before adding new import sources/parsers.

## Scope boundaries

- Inventory and stabilize first; do not rebuild existing CSV/import/review logic blindly.
- No new import sources, large import engine, OCR/camera, or AKB/Raiffeisen parser unless files and processing gaps are proven.
- Runtime DBs, CSV/XLS/XLSX, credentials, reports, and real amounts stay out of Git and chat.
- No productive mass-confirm. Productive write actions remain Preview → Confirm → Audit, with runtime backup first.

## Read-only audit pattern

1. Inspect runtime DB tables: `budget_transaction_candidates`, `budget_transactions`, `budget_import_line_items`, `budget_recurring_payments`, `budget_accounts`/`accounts`, `cash_account_snapshots`, `audit_log`.
2. Report only counts, grouped by source classes:
   - Kreditkarten-CSV: `credit_card_csv`, `visa_credit_card`, VISA/Kredit labels.
   - Migros-CSV: `migros_receipt`, `migros_receipts`, Migros labels.
   - AKB-CSV: `akb_bank`, AKB source/file/account tokens.
   - Raiffeisen-CSV: `raiffeisen_bank`, Raiffeisen source/file/account tokens.
   - manual transactions: `budget_transactions.source_type='manual'`.
   - other sources: everything not matching the above.
3. For each source count: import sessions/batches, candidates total/open/confirmed/ignored, duplicate-like statuses, `covered_by_migros`, `superseded`, effective budget transactions, Migros line items, with/without category, review-needed, and 2025/reference-year candidates.
4. Data quality counts should include uncategorized candidates, low confidence, Galaxus/Digitec, Migros review, duplicates, covered-by-Migros, wrong-year/2025, missing account/source/date, confirmed transactions missing category/account/audit, open recurring candidates, active recurring without last payment, and missing expected recurring payments.

## AKB/Raiffeisen audit pattern

- Check both runtime files and Google Drive for AKB/Raiffeisen CSVs.
- Check whether candidates and confirmed transactions already exist for `akb_bank`/`raiffeisen_bank` or matching source/file/account tokens.
- Check whether cash accounts are `csv_calculated` and whether `csv_anchor_balance` snapshots exist.
- If files are absent: report only; do not fake candidates.
- If files exist but parser/processing status is ambiguous: propose a separate dry-run, do not productively import.

## Workflow hardening checks

- Normal review excludes duplicate/covered/ignored/confirmed/reference rows.
- Duplicate tab shows duplicate-like statuses and blocks normal confirm.
- Covered-by-Migros remains blocked from normal confirm and bulk confirm.
- Confirm creates budget transactions, effective expenses show confirmed-only transactions, and Budgetstatus updates from confirmed transactions.
- Fixkosten/Subscriptions GET remains read-only; detection is explicit; activation does not create budget transactions.

## Useful implementation artifact

A read-only API/service such as `/api/budget/import-status-audit` can expose the amount-free summary. The service must not return money fields and must not mutate runtime state.

## Verification checklist

- Python compile.
- Full backend pytest.
- Frontend tests and build if frontend touched or sanity required.
- Source and frontend-build secret scans.
- Git-safety: no runtime DB, CSV/XLS/XLSX, credentials, or reports staged.
- `git diff --check`.
- Browser sanity for Buchungen prüfen, Effektive Ausgaben, Budgetstatus, Fixkosten, and Budget Übersicht.
- Commit/push only if code or docs changed; verify remote hash.
