# Ledger Integration Walkthrough This document outlines the changes made to integrate Ledger functionality into the Transaction flow and provides steps for verification. ## Changes Overview ### Backend (Go) - **`TransactionInput`**: Added `LedgerID` field to potential input. - **`TransactionService`**: - Updated `CreateTransaction` to assign the user's default ledger if no `LedgerID` is provided. - Updated `CreateTransaction` and `UpdateTransaction` to validate and link the `LedgerID`. - Injected `LedgerRepository` to allow fetching default ledgers and validating IDs. - **`Router`**: Updated dependency injection to pass `LedgerRepository` to `TransactionService`. ### Frontend (React/TypeScript) - **Global State**: Created `SettingsContext` to provide global access to the currently selected `ledgerId`. - **Types**: Updated `TransactionFormInput`, `TransactionFilter`, and other interfaces to support `this.ledgerId`. - **Service (`transactionService.ts`)**: Updated `getTransactions`, `createTransaction`, and `updateTransaction` to map `ledgerId` to the backend API parameters (`ledger_id`). - **Components**: - **`TransactionForm`**: Now accepts `currentLedgerId` and submits it with new transactions. - **`CommandPalette`**: Now filters transaction search results by the current active ledger. - **Pages**: - **`Home`**: Updated to use `SettingsContext` for ledger selection and data refreshing. - **`Transactions`**: Updated to filter the transaction list by the global `currentLedgerId` and pass it to the creation form. ## Verification Steps ### 1. Ledger Switching 1. Navigate to the **Home** page. 2. Click the Ledger selector in the header (top left pill). 3. Select a different ledger (e.g., "Business" vs "Personal"). 4. **Verify**: The "Recent Transactions" list update to show only transactions for the selected ledger. 5. **Verify**: The Dashboard stats (Income, Expense) update to reflect the selected ledger. ### 2. Transaction Creation with Ledger 1. Ensure you are in a specific ledger (e.g., "Personal"). 2. Click **"记一笔" (Add Transaction)**. 3. Create a new transaction. 4. **Verify**: The transaction appears in the "Personal" ledger list. 5. Switch to a "Business" ledger. 6. **Verify**: The newly created transaction does **NOT** appear in the "Business" ledger list. ### 3. Transaction List Filtering 1. Go to the **Transactions** page. 2. **Verify**: The list only shows transactions for the currently selected ledger. 3. Switch ledger using the global context (if accessible) or go back Home to switch, then return. 4. **Verify**: The list refreshes to show the new ledger's transactions. ### 4. Global Search 1. Press `Ctrl+K` (or `Cmd+K`) to open the Command Palette. 2. Type a search term for a transaction that exists in the *current* ledger. 3. **Verify**: The transaction appears in results. 4. Type a search term for a transaction that exists in a *different* ledger. 5. **Verify**: The transaction does **NOT** appear in results. ### 5. Exchange Rates (Regression Test) 1. Go to **Exchange Rates** page. 2. **Verify**: Net Worth and History still load correctly (this calculates based on *global* accounts, so it should remain unaffected by ledger selection). ## Technical Notes - **Default Behavior**: If a transaction is created via API without a `ledger_id` (e.g., older clients), the backend now automatically assigns it to the user's **Default Ledger**. - **Context Persistence**: The selected ledger ID is stored in User Settings, so it persists across sessions if the backend supports saving settings (which it does).