Completes Phase 1: Beancount patterns adoption
Implements core improvements from Phase 1 of the Beancount patterns adoption: - Uses Decimal for fiat amounts to prevent floating point errors - Adds a meta field to journal entries for a full audit trail - Adds a flag field to journal entries for transaction status - Migrates existing account names to a hierarchical format This commit introduces a database migration to add the `flag` and `meta` columns to the `journal_entries` table. It also includes updates to the models, CRUD operations, and API endpoints to handle the new fields.
This commit is contained in:
parent
35d2057694
commit
1a28ec59eb
7 changed files with 616 additions and 31 deletions
|
|
@ -177,3 +177,96 @@ async def m004_manual_payment_requests(db):
|
|||
CREATE INDEX idx_manual_payment_requests_status ON manual_payment_requests (status);
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
async def m005_add_flag_and_meta(db):
|
||||
"""
|
||||
Add flag and meta columns to journal_entries table.
|
||||
- flag: Transaction status (* = cleared, ! = pending, # = flagged, x = void)
|
||||
- meta: JSON metadata for audit trail (source, tags, links, notes)
|
||||
"""
|
||||
await db.execute(
|
||||
"""
|
||||
ALTER TABLE journal_entries ADD COLUMN flag TEXT DEFAULT '*';
|
||||
"""
|
||||
)
|
||||
|
||||
await db.execute(
|
||||
"""
|
||||
ALTER TABLE journal_entries ADD COLUMN meta TEXT DEFAULT '{}';
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
async def m006_hierarchical_account_names(db):
|
||||
"""
|
||||
Migrate account names to hierarchical Beancount-style format.
|
||||
- "Cash" → "Assets:Cash"
|
||||
- "Accounts Receivable" → "Assets:Receivable"
|
||||
- "Food & Supplies" → "Expenses:Food:Supplies"
|
||||
- "Accounts Receivable - af983632" → "Assets:Receivable:User-af983632"
|
||||
"""
|
||||
from .account_utils import migrate_account_name
|
||||
from .models import AccountType
|
||||
|
||||
# Get all existing accounts
|
||||
accounts = await db.fetchall("SELECT * FROM accounts")
|
||||
|
||||
# Mapping of old names to new names
|
||||
name_mappings = {
|
||||
# Assets
|
||||
"cash": "Assets:Cash",
|
||||
"bank": "Assets:Bank",
|
||||
"lightning": "Assets:Lightning:Balance",
|
||||
"accounts_receivable": "Assets:Receivable",
|
||||
|
||||
# Liabilities
|
||||
"accounts_payable": "Liabilities:Payable",
|
||||
|
||||
# Equity
|
||||
"member_equity": "Equity:MemberEquity",
|
||||
"retained_earnings": "Equity:RetainedEarnings",
|
||||
|
||||
# Revenue → Income
|
||||
"accommodation_revenue": "Income:Accommodation",
|
||||
"service_revenue": "Income:Service",
|
||||
"other_revenue": "Income:Other",
|
||||
|
||||
# Expenses
|
||||
"utilities": "Expenses:Utilities",
|
||||
"food": "Expenses:Food:Supplies",
|
||||
"maintenance": "Expenses:Maintenance",
|
||||
"other_expense": "Expenses:Other",
|
||||
}
|
||||
|
||||
# Update default accounts using ID-based mapping
|
||||
for old_id, new_name in name_mappings.items():
|
||||
await db.execute(
|
||||
"""
|
||||
UPDATE accounts
|
||||
SET name = :new_name
|
||||
WHERE id = :old_id
|
||||
""",
|
||||
{"new_name": new_name, "old_id": old_id}
|
||||
)
|
||||
|
||||
# Update user-specific accounts (those with user_id set)
|
||||
user_accounts = await db.fetchall(
|
||||
"SELECT * FROM accounts WHERE user_id IS NOT NULL"
|
||||
)
|
||||
|
||||
for account in user_accounts:
|
||||
# Parse account type
|
||||
account_type = AccountType(account["account_type"])
|
||||
|
||||
# Migrate name
|
||||
new_name = migrate_account_name(account["name"], account_type)
|
||||
|
||||
await db.execute(
|
||||
"""
|
||||
UPDATE accounts
|
||||
SET name = :new_name
|
||||
WHERE id = :id
|
||||
""",
|
||||
{"new_name": new_name, "id": account["id"]}
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue