Account transactions view using libdrcr

This commit is contained in:
RunasSudo 2025-05-28 00:26:12 +10:00
parent ffef2d16dc
commit a40e2f81ba
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
3 changed files with 46 additions and 20 deletions

View File

@ -91,6 +91,7 @@ pub fn run() {
get_open_filename,
set_open_filename,
libdrcr_bridge::get_all_transactions_except_earnings_to_equity,
libdrcr_bridge::get_all_transactions_except_earnings_to_equity_for_account,
libdrcr_bridge::get_balance_sheet,
libdrcr_bridge::get_income_statement,
libdrcr_bridge::get_trial_balance,

View File

@ -73,7 +73,7 @@ async fn get_report(
pub(crate) async fn get_all_transactions_except_earnings_to_equity(
state: State<'_, Mutex<AppState>>,
) -> Result<String, ()> {
Ok(get_report(
let transactions = get_report(
state,
&ReportingProductId {
name: "AllTransactionsExceptEarningsToEquity",
@ -84,9 +84,40 @@ pub(crate) async fn get_all_transactions_except_earnings_to_equity(
},
)
.await
.downcast_ref::<Transactions>()
.downcast::<Transactions>()
.unwrap()
.to_json())
.transactions;
Ok(serde_json::to_string(&transactions).unwrap())
}
#[tauri::command]
pub(crate) async fn get_all_transactions_except_earnings_to_equity_for_account(
state: State<'_, Mutex<AppState>>,
account: String,
) -> Result<String, ()> {
let transactions = get_report(
state,
&ReportingProductId {
name: "AllTransactionsExceptEarningsToEquity",
kind: ReportingProductKind::Transactions,
args: Box::new(DateArgs {
date: NaiveDate::from_ymd_opt(9999, 12, 31).unwrap(),
}),
},
)
.await
.downcast::<Transactions>()
.unwrap()
.transactions;
// Filter only transactions affecting this account
let filtered_transactions = transactions
.into_iter()
.filter(|t| t.postings.iter().any(|p| p.account == account))
.collect::<Vec<_>>();
Ok(serde_json::to_string(&filtered_transactions).unwrap())
}
#[tauri::command]

View File

@ -1,6 +1,6 @@
<!--
DrCr: Web-based double-entry bookkeeping framework
Copyright (C) 20222025 Lee Yingtong Li (RunasSudo)
Copyright (C) 2022-2025 Lee Yingtong Li (RunasSudo)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
@ -38,15 +38,13 @@
</template>
<script setup lang="ts">
import { PlusIcon } from '@heroicons/vue/24/outline';
import { PlusIcon } from '@heroicons/vue/24/outline';
import { invoke } from '@tauri-apps/api/core';
import { UnlistenFn, listen } from '@tauri-apps/api/event';
import { onUnmounted, ref } from 'vue';
import { useRoute } from 'vue-router';
import { Transaction, db } from '../db.ts';
import { ReportingStage, ReportingWorkflow } from '../reporting.ts';
import { Transaction } from '../db.ts';
import TransactionsWithCommodityView from './TransactionsWithCommodityView.vue';
import TransactionsWithoutCommodityView from './TransactionsWithoutCommodityView.vue';
@ -56,24 +54,20 @@
const transactions = ref([] as Transaction[]);
async function load() {
const session = await db.load();
const reportingWorkflow = new ReportingWorkflow();
await reportingWorkflow.generate(session); // This also ensures running balances are up to date
const transactionsRaw = reportingWorkflow.getTransactionsAtStage(ReportingStage.FINAL_STAGE);
// Filter only transactions affecting this account
const filteredTransactions = transactionsRaw.filter((t) => t.postings.some((p) => p.account === route.params.account));
const transactionsRaw = JSON.parse(await invoke(
'get_all_transactions_except_earnings_to_equity_for_account',
{ account: route.params.account }
)) as Transaction[];
// In order to correctly sort API transactions, we need to remember their indexes
const filteredTxnsWithIndexes = filteredTransactions.map((t, index) => [t, index] as [Transaction, number]);
const transactionsRawWithIndexes = transactionsRaw.map((t, index) => [t, index] as [Transaction, number]);
// Sort transactions in reverse chronological order
// We must sort here because they are returned by reportingWorkflow in order of ReportingStage
// Use Number.MAX_SAFE_INTEGER as ID for API transactions
filteredTxnsWithIndexes.sort(([t1, i1], [t2, i2]) => (t2.dt.localeCompare(t1.dt)) || ((t2.id ?? Number.MAX_SAFE_INTEGER) - (t1.id ?? Number.MAX_SAFE_INTEGER) || (i2 - i1)));
transactionsRawWithIndexes.sort(([t1, i1], [t2, i2]) => (t2.dt.localeCompare(t1.dt)) || ((t2.id ?? Number.MAX_SAFE_INTEGER) - (t1.id ?? Number.MAX_SAFE_INTEGER) || (i2 - i1)));
transactions.value = filteredTxnsWithIndexes.map(([t, _idx]) => t);
transactions.value = transactionsRawWithIndexes.map(([t, _idx]) => t);
}
load();