Commit graph

177 commits

Author SHA1 Message Date
b6886793ee Creates accounts in Fava if they don't exist
This change ensures that user-specific accounts are automatically created
in the Fava/Beancount ledger when they are first requested. It checks for
the existence of the account via a Fava query and creates it via an Open
directive if it's missing.  This simplifies account management and
ensures that all necessary accounts are available for transactions.

This implementation adds a new `add_account` method to the `FavaClient`
class which makes use of the /add_entries endpoint to create an account
using an Open Directive.
2025-11-10 15:56:22 +01:00
51ae2e8e47 Sanitizes reference links for Beancount
Ensures that user-provided reference strings for expense,
receivable, and revenue entries are sanitized before being
included as Beancount links. This prevents issues caused by
invalid characters in the links, improving compatibility with
Beancount's link format. A new utility function is introduced
to handle the sanitization process.
2025-11-10 15:04:27 +01:00
a6b67b7416 Improves Beancount entry generation and sanitization
Adds a function to sanitize strings for use as Beancount links,
ensuring compatibility with Beancount's link restrictions.

Refactors the journal entry creation process to use EUR-based
postings when fiat currency is provided, improving accuracy
and consistency. The legacy SATS-based fallback is retained for
cases without fiat currency information.

Adjusts reference generation for Beancount entries using the
sanitized description.
2025-11-10 11:35:41 +01:00
0e93fc5ffc Enhances Beancount import and API entry creation
Improves the Beancount import process to send SATS amounts with fiat metadata to the API, enabling automatic conversion to EUR-based postings.
Updates the API to store entries in Fava instead of the Castle DB, simplifying the JournalEntry creation process.
Adds error handling to the upload entry function.
Includes a note about imported transactions being stored in EUR with SATS in metadata.
2025-11-10 11:29:01 +01:00
1d605be021 Removes redundant payment recording
Removes the explicit call to the record-payment API when settling a receivable.

The webhook (on_invoice_paid in tasks.py) automatically handles recording the payment in Fava, making the API call redundant. This simplifies the frontend logic.

Also, in the `showPayUserDialog` function, it now correctly identifies users who are owed payments based on a negative balance instead of a positive balance.
2025-11-10 11:13:25 +01:00
490b361268 Adds fiat settlement entry formatting
Introduces a function to format fiat settlement entries for Beancount, handling cash, bank transfers, and other non-lightning payments.

This allows for recording transactions in fiat currency with sats as metadata.

Updates the API endpoint to use the new function when settling receivables with fiat currencies.
2025-11-10 10:51:55 +01:00
472c4e2164 Corrects receivable dialog display logic
Reverses the condition for displaying the settle receivable dialog.

It now correctly shows the dialog only for users with a positive balance,
which indicates they owe the castle.
2025-11-10 10:50:47 +01:00
8342318fde Refactors duplicate payment check in Fava
Improves payment recording logic by fetching recent entries and filtering using Python, replacing the BQL query.

This addresses issues with matching against set types in BQL, enhancing reliability.
2025-11-10 10:25:05 +01:00
fbda8e2980 Improves readability and reduces logging verbosity
Removes excessive logging to improve readability and reduce verbosity.
Streamlines balance processing and improves logging for settlement amounts.
Adds a note about Fava's internal normalization behavior to the beancount formatting.
2025-11-10 03:59:24 +01:00
313265b185 Supports new EUR/USD amount string format
Adds support for parsing direct EUR/USD amount strings in the format "37.22 EUR" or "12.34 USD".

It also retrieves the SATS equivalent from the metadata if present, for the new amount format. This ensures compatibility with both the old "SATS {EUR}" format and the newer, direct fiat formats.
2025-11-10 03:46:17 +01:00
476e9dec4b Supports new amount format and metadata tracking
Updates the amount parsing logic to support a new format where fiat amounts (EUR/USD) are specified directly.

Adds support for tracking SATS equivalents from metadata when the new format is used.

Also tracks fiat amounts specified in metadata as a fallback for backward compatibility.
Reverses the calculation of net balance to correctly reflect receivables and liabilities.
2025-11-10 03:42:30 +01:00
ca2ce1dfcc Refactors expense tracking to use fiat amounts
Updates the expense tracking system to store payables and receivables in fiat currency within Beancount.
This ensures accurate debt representation and simplifies balance calculations.
Changes include:
- Converting `format_expense_entry` and `format_receivable_entry` to use fiat amounts.
- Introducing `format_net_settlement_entry` for net settlement payments.
- Modifying `format_payment_entry` to use cost syntax for fiat tracking.
- Adjusting Fava client to correctly process new amount formats and metadata.
- Adding average cost basis posting format

The use of fiat amounts and cost basis aims to provide better accuracy and compatibility with existing Beancount workflows.
2025-11-10 03:33:04 +01:00
8396331d5a Calculates user balance from journal entries
Refactors user balance calculation to directly parse journal
entries, enhancing accuracy and efficiency. This change
eliminates reliance on direct database queries and provides a
more reliable mechanism for determining user balances.

Adds logging for debugging purposes.

Also extracts and uses fiat metadata from invoice/payment extras.
2025-11-10 02:18:49 +01:00
5c1c7b1b05 Reverts balance perspective to castle's view
Changes the displayed balance perspective to reflect the castle's point of view instead of the user's.

This involves:
- Displaying balances as positive when the user owes the castle
- Displaying balances as negative when the castle owes the user.

This change affects how balances are calculated and displayed in both the backend logic and the frontend templates.
2025-11-10 01:40:09 +01:00
0f24833e02 Adds unique IDs to receivable and revenue entries
Ensures unique identification for receivable and
revenue entries by generating a UUID and incorporating
it into a castle reference.

This enhances tracking and linking capabilities by
providing a consistent and easily identifiable
reference across the system.
2025-11-10 01:26:59 +01:00
e154a8b427 Calculates user balances from journal entries
Refactors user balance calculation to use journal entries
instead of querying Fava's query endpoint.

This change allows for exclusion of voided transactions
(tagged with #voided) in addition to pending transactions
when calculating user balances, providing more accurate
balance information.

Additionally the change improves parsing of the amounts in journal entries by using regular expressions.
2025-11-10 01:16:04 +01:00
3cb3b23a8d Improves pending entry amount parsing
Updates the pending entries API to correctly parse the amount and fiat values from the amount string, which can now contain both SATS and fiat information.

This change handles different formats of the amount string, including cases where the fiat amount is present within curly braces.
2025-11-10 01:09:49 +01:00
0c7356e228 Parses amount string for SATS and fiat
Improves handling of the amount field in user entries by parsing string formats that include both SATS and fiat currency information.

This change allows extracting the SATS amount and fiat amount/currency directly from the string, accommodating different display formats.
2025-11-10 01:06:51 +01:00
63d851ce94 Refactors user entry retrieval from Fava
Switches to retrieving all journal entries from Fava and filtering in the application to allow filtering by account type and user.
This provides more flexibility and control over the data being presented to the user.
Also extracts and includes relevant metadata such as entry ID, fiat amounts, and references for improved frontend display.
2025-11-10 01:06:51 +01:00
7f545ea88e Excludes voided transactions from pending entries
Ensures that voided transactions are not included in the
list of pending entries. This prevents displaying
transactions that have been cancelled or reversed,
providing a more accurate view of truly pending items.
2025-11-10 01:06:51 +01:00
1ebe066773 Simplifies entry and posting metadata formatting
Removes redundant metadata from entries and postings.
The cost syntax already contains fiat/exchange rate information.
Metadata such as 'created-via', 'is-equity', and payer/payee
can be inferred from transaction direction, tags, and account names.
2025-11-10 01:06:51 +01:00
1362ada362 Rejects pending expense entries by voiding them
Instead of deleting pending expense entries, marks them as voided by adding a #voided tag.
This ensures an audit trail while excluding them from balances.

Updates the Fava client to use 'params' for the delete request.
2025-11-10 01:06:51 +01:00
cfca10b782 Enables Fava integration for entry management
Adds functionality to interact with Fava for managing
Beancount entries, including fetching, updating, and
deleting entries directly from the Beancount ledger.

This allows for approving/rejecting pending entries
via the API by modifying the source file through Fava.

The changes include:
- Adds methods to the Fava client for fetching all journal
  entries, retrieving entry context (source and hash),
  updating the entry source, and deleting entries.
- Updates the pending entries API to use the Fava journal
  endpoint instead of querying transactions.
- Implements entry approval and rejection using the new
  Fava client methods to modify the underlying Beancount file.
2025-11-10 01:06:51 +01:00
57e6b3de1d Excludes pending transactions from balance queries
Modifies balance queries to exclude pending transactions (flag='!')
and only include cleared/completed transactions (flag='*'). This
ensures accurate balance calculations by reflecting only settled transactions.
2025-11-10 01:06:51 +01:00
56a3e9d4e9 Refactors pending entries and adds fiat amounts
Improves the handling of pending entries by extracting and deduplicating data from Fava's query results.

Adds support for displaying fiat amounts alongside entries and extracts them from the position data in Fava.

Streamlines receivables/payables/equity checks on the frontend by relying on BQL query to supply account type metadata and tags.
2025-11-10 01:06:51 +01:00
37fe34668f Adjusts balance calculation for user perspective
Inverts the sign of Beancount balances to represent the user's perspective, where liabilities are positive and receivables are negative.

This change ensures that user balances accurately reflect the amount the castle owes the user (positive) or the amount the user owes the castle (negative). It simplifies the logic by consistently negating the Beancount balance rather than using conditional checks based on account type.
2025-11-10 01:06:51 +01:00
9350f05d74 Removes voided/flagged entry flags
Updates journal entry flags to align with Beancount's limited flag support.
Beancount only uses cleared (*) and pending (!) flags.

Removes the VOID and FLAGGED flags and recommends using tags instead
(e.g., "! + #voided" for voided entries, "! + #review" for flagged entries).

Updates the API to reflect this change, removing the ability to directly
"reject" an expense entry via the void flag.  Instead, instructs users to
add the #voided tag in Fava.

Updates reconciliation summary to count entries with voided/review tags
instead of voided/flagged flags.
2025-11-10 01:06:51 +01:00
de3e4e65af Refactors transaction retrieval to use Fava API
Replaces direct database queries for transactions with calls to the Fava API,
centralizing transaction logic and improving data consistency.

This change removes redundant code and simplifies the API by relying on Fava
for querying transactions based on account patterns and other criteria.

Specifically, the commit introduces new methods in the FavaClient class for
querying transactions, retrieving account transactions, and retrieving user
transactions. The API endpoints are updated to utilize these methods.
2025-11-10 01:06:51 +01:00
88ff3821ce Removes core balance calculation logic
Migrates balance calculation and inventory tracking to
Fava/Beancount, leveraging Fava's query API for all
accounting calculations. This simplifies the core module
and centralizes accounting logic in Fava.
2025-11-10 01:06:51 +01:00
efc09aa5ce Migrates payment processing to Fava
Removes direct journal entry creation in favor of using Fava for accounting.

This change centralizes accounting logic in Fava, improving auditability and consistency.
It replaces direct database interactions for recording payments and settlements with calls to the Fava client.
The changes also refactor balance retrieval to fetch data from Fava.
2025-11-10 01:06:51 +01:00
e3acc53e20 Adds Fava integration for journal entries
Integrates Fava/Beancount for managing journal entries.

This change introduces functions to format entries into Beancount
format and submit them to a Fava instance.

It replaces the previous direct database entry creation with Fava
submission for expense, receivable, and revenue entries. The existing
create_journal_entry function is also updated to submit generic
journal entries to Fava.
2025-11-10 01:06:51 +01:00
a88d7b4ea0 Fetches account balances from Fava/Beancount
Refactors account balance retrieval to fetch data from Fava/Beancount
for improved accounting accuracy.

Updates user balance retrieval to use Fava/Beancount data source.

Updates Castle settings ledger slug name.
2025-11-10 01:06:51 +01:00
ff27f7ba01 Submits Castle payments to Fava
Refactors the payment processing logic to submit journal entries directly to
Fava/Beancount instead of storing them in the Castle database. It queries
Fava to prevent duplicate entries. The changes include extracting fiat
metadata from the invoice, formatting the data as a Beancount transaction
using a dedicated formatting function, and submitting it to the Fava API.
2025-11-10 01:06:51 +01:00
3c925abe9e Adds Fava integration for invoice payments
Implements a new handler to process Castle invoice payments by submitting them to Fava, which in turn writes them to a Beancount file.

This approach avoids storing payment data directly in the Castle database. The handler formats the payment as a Beancount transaction, includes fiat currency if available, and queries Fava to prevent duplicate entries.

The commit also updates documentation to reflect the changes to the invoice processing workflow.
2025-11-10 01:06:51 +01:00
750692a2f0 Initializes Fava client on startup
Initializes the Fava client with default settings when the Castle extension starts.

This ensures the client is ready to interact with Fava immediately and provides feedback if Fava is not configured correctly.

The client is re-initialized if the admin updates settings later.
2025-11-10 01:06:51 +01:00
2e862d0ebd Adds Beancount formatting utilities
Introduces utilities to format Castle data models into Beancount
transactions for Fava API compatibility.

Provides functions to format transactions, postings with cost basis,
expense entries, receivable entries, and payment entries.

These functions ensure data is correctly formatted for Fava's
add_entries API, including cost basis, flags, and metadata.
2025-11-10 01:06:45 +01:00
1bce6b86cf Adds async client for Fava REST API
Implements an asynchronous HTTP client to interact with the Fava accounting API.
This client provides methods for adding journal entries, retrieving account balances,
and querying user balances, allowing the application to delegate all accounting
logic to Fava and Beancount.
2025-11-10 01:06:37 +01:00
13dd5c7143 Adds Fava/Beancount integration settings
Adds settings to the Castle extension for integration
with a Fava/Beancount accounting system. This enables
all accounting operations to be managed through Fava.
It includes settings for the Fava URL, ledger slug,
and request timeout.
2025-11-10 01:06:30 +01:00
b9efd166a6 FInal commit before stripping down to use FAVA 2025-11-09 21:12:19 +01:00
0b64ffa54f feat: Add equity account support to transaction filtering and Beancount import
Improvements to equity account handling across the Castle extension:

  Transaction Categorization (views_api.py):
  - Prioritize equity accounts when enriching transaction entries
  - Use two-pass lookup: first search for equity accounts, then fall back to liability/asset accounts
  - Ensures transactions with Equity:User-<id> accounts are correctly categorized as equity

UI Enhancements (index.html, index.js):
  - Add 'Equity' filter option to Recent Transactions table
  - Display blue "Equity" badge for equity entries (before receivable/payable badges)
  - Add isEquity() helper function to identify equity account entries

Beancount Import (import_beancount.py):
  - Support importing Beancount Equity:<name> accounts
  - Map Beancount "Equity:Pat" to Castle "Equity:User-<id>" accounts
  - Update extract_user_from_user_account() to handle Equity: prefix
  - Improve error messages to include equity account examples
  - Add equity account lookup in get_account_id() with helpful error if equity not enabled

These changes ensure equity accounts (representing user capital contributions) are properly distinguished from payables and receivables throughout the system.
2025-11-09 21:09:43 +01:00
6f1fa7203b change Recent Transactions pagination limit from 20 to 10 2025-11-09 00:32:54 +01:00
3af93c3479 Add receivable/payable filtering with database-level query optimization
Add account type filtering to Recent Transactions table and fix pagination issue where filters were applied after fetching results, causing incomplete data display.

Database layer (crud.py):
  - Add get_journal_entries_by_user_and_account_type() to filter entries by
    both user_id and account_type at SQL query level
  - Add count_journal_entries_by_user_and_account_type() for accurate counts
  - Filters apply before pagination, ensuring all matching records are fetched

API layer (views_api.py):
  - Add filter_account_type parameter ('asset' for receivable, 'liability' for payable)
  - Refactor filtering logic to use new database-level filter functions
  - Support filter combinations: user only, account_type only, user+account_type, or all
  - Enrich entries with account_type metadata for UI display

Frontend (index.js):
  - Add account_type to transactionFilter state
  - Add accountTypeOptions computed property with receivable/payable choices
  - Reorder table columns to show User before Date
  - Update loadTransactions to send account_type filter parameter
  - Update clearTransactionFilter to clear both user and account_type filters

UI (index.html):
  - Add second filter dropdown for account type (Receivable/Payable)
  - Show clear button when either filter is active
  - Update button label from "Clear Filter" to "Clear Filters"

This fixes the critical bug where filtering for receivables would only show a subset of results (e.g., 2 out of 20 entries fetched) instead of all matching receivables. Now filters are applied at the database level before pagination, ensuring users see all relevant transactions.
2025-11-09 00:28:54 +01:00
f3d0d8652b Add Recent Transactions pagination and table view to with filtering
Convert the Recent Transactions card from a list view to a paginated table
  with enhanced filtering capabilities for super users.

Frontend changes:
  - Replace q-list with q-table for better data presentation
  - Add pagination with configurable page size (default: 20 items)
  - Add transaction filter dropdown for super users to filter by username
  - Define table columns: Status, Date, Description, User, Amount, Fiat, Reference
  - Implement prev/next page navigation with page info display
  - Add filter controls with clear filter button

Backend changes (views_api.py):
  - Add pagination support with limit/offset parameters
  - Add filter_user_id parameter for filtering by user (super user only)
  - Enrich transaction entries with user_id and username from account lookups
  - Return paginated response with total count and pagination metadata

Database changes (crud.py):
  - Update get_all_journal_entries() to support offset parameter
  - Update get_journal_entries_by_user() to support offset parameter
  - Add count_all_journal_entries() for total count
  - Add count_journal_entries_by_user() for user-specific count

This improves the Recent Transactions UX by providing better organization, easier navigation through large transaction lists, and the ability for admins to filter transactions by user.
2025-11-09 00:27:17 +01:00
093cecbff2 Ensures transaction offset is a valid number
Addresses an issue where the transaction offset could be non-numeric, causing errors in pagination.

Adds validation and parsing to ensure the offset is always an integer, falling back to 0 if necessary.  Also ensures that limit is parsed into an Int.
2025-11-09 00:06:07 +01:00
69b8f6e2d3 Adds pagination to transaction history
Implements pagination for the transaction history, enabling users
to navigate through their transactions in manageable chunks. This
improves performance and user experience, especially for users
with a large number of transactions. It also introduces total entry counts.
2025-11-08 23:51:12 +01:00
4b327a0aab Extends account lookup for user accounts
Implements account lookup logic for user-specific accounts,
specifically Liabilities:Payable and Assets:Receivable.

This allows the system to automatically map Beancount accounts
to corresponding accounts in the Castle system based on user ID.

Improves error messages when user accounts are not properly configured.
2025-11-08 23:51:07 +01:00
992a8fe554 Creates default user accounts on wallet setup
Ensures consistent user account structure by proactively
creating core liability and asset accounts when a user
configures their wallet settings.
2025-11-08 23:41:17 +01:00
9054b3eb62 Adds Beancount import helper script
Implements a script to import Beancount ledger transactions into the Castle accounting extension.

The script fetches BTC/EUR rates, retrieves accounts from the Castle API, maps users, parses Beancount transactions, converts EUR to sats, and uploads the data to Castle.

Adds error handling, dry-run mode, and detailed logging for improved usability.
Displays equity account status and validates the existence of user equity accounts.
2025-11-08 23:18:42 +01:00
4ae6a8f7d2 Refactors journal entry lines to use single amount
Simplifies the representation of journal entry lines by replacing separate debit and credit fields with a single 'amount' field.

Positive amounts represent debits, while negative amounts represent credits, aligning with Beancount's approach. This change improves code readability and simplifies calculations for balancing entries.
2025-11-08 11:48:08 +01:00
d0bec3ea5a Adapts receivable/payable logic for Beancount
Modifies receivable and payable checks to align with Beancount's accounting principles.
This adjustment ensures that the system correctly identifies receivables and payables based on the sign of the amounts, rather than just debit/credit.
Also, calculates transaction sizes using the absolute value of amounts.
2025-11-08 10:52:43 +01:00