Greatly improve performance of balance assertions checking
This commit is contained in:
parent
024ddc0468
commit
226ef6dac0
@ -1,6 +1,6 @@
|
|||||||
<!--
|
<!--
|
||||||
DrCr: Web-based double-entry bookkeeping framework
|
DrCr: Web-based double-entry bookkeeping framework
|
||||||
Copyright (C) 2022–2024 Lee Yingtong Li (RunasSudo)
|
Copyright (C) 2022–2025 Lee Yingtong Li (RunasSudo)
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
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
|
it under the terms of the GNU Affero General Public License as published by
|
||||||
@ -66,8 +66,10 @@
|
|||||||
|
|
||||||
import { ref } from 'vue';
|
import { ref } from 'vue';
|
||||||
|
|
||||||
import { db, totalBalancesAtDate } from '../db.ts';
|
import { asCost } from '../amounts.ts';
|
||||||
|
import { db } from '../db.ts';
|
||||||
import { pp } from '../display.ts';
|
import { pp } from '../display.ts';
|
||||||
|
import { ReportingStage, ReportingWorkflow } from '../reporting.ts';
|
||||||
import { CheckIcon, PencilIcon, XMarkIcon } from '@heroicons/vue/24/outline';
|
import { CheckIcon, PencilIcon, XMarkIcon } from '@heroicons/vue/24/outline';
|
||||||
import { PlusIcon } from '@heroicons/vue/16/solid';
|
import { PlusIcon } from '@heroicons/vue/16/solid';
|
||||||
|
|
||||||
@ -92,34 +94,27 @@
|
|||||||
ORDER BY dt DESC, id DESC`
|
ORDER BY dt DESC, id DESC`
|
||||||
);
|
);
|
||||||
|
|
||||||
/*
|
// Get transactions
|
||||||
// Cache trial balances in case there are multiple assertions per date
|
const reportingWorkflow = new ReportingWorkflow();
|
||||||
const trialBalanceForDate = new Map<string, TrialBalanceReport>();
|
await reportingWorkflow.generate(session);
|
||||||
|
const transactions = reportingWorkflow.getTransactionsAtStage(ReportingStage.OrdinaryAPITransactions);
|
||||||
|
|
||||||
for (const balanceAssertion of rawBalanceAssertions) {
|
for (const balanceAssertion of rawBalanceAssertions) {
|
||||||
// Check assertion status
|
// Check assertion status
|
||||||
// TODO: This is very inefficient because API transactions are generated multiple times
|
const balanceAssertionDt = dayjs(balanceAssertion.dt);
|
||||||
if (!trialBalanceForDate.has(balanceAssertion.dt)) {
|
|
||||||
const reportingWorkflow = new ReportingWorkflow();
|
let accountBalance = 0;
|
||||||
await reportingWorkflow.generate(session, balanceAssertion.dt);
|
for (const transaction of transactions) {
|
||||||
const trialBalance = reportingWorkflow.getReportAtStage(ReportingStage.OrdinaryAPITransactions, TrialBalanceReport) as TrialBalanceReport;
|
if (dayjs(transaction.dt) <= balanceAssertionDt) {
|
||||||
trialBalanceForDate.set(balanceAssertion.dt, trialBalance);
|
for (const posting of transaction.postings) {
|
||||||
|
if (posting.account === balanceAssertion.account) {
|
||||||
|
accountBalance += asCost(posting.quantity, posting.commodity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const trialBalance = trialBalanceForDate.get(balanceAssertion.dt)!;
|
balanceAssertion.isValid = balanceAssertion.quantity === accountBalance && balanceAssertion.commodity === db.metadata.reporting_commodity;
|
||||||
balanceAssertion.isValid = balanceAssertion.quantity === trialBalance.balances.get(balanceAssertion.account) && balanceAssertion.commodity === db.metadata.reporting_commodity;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Check assertion status
|
|
||||||
const balancesForDate = new Map<string, Map<string, number>>();
|
|
||||||
|
|
||||||
for (const balanceAssertion of rawBalanceAssertions) {
|
|
||||||
if (!balancesForDate.has(balanceAssertion.dt)) {
|
|
||||||
// FIXME: This is quite slow
|
|
||||||
balancesForDate.set(balanceAssertion.dt, await totalBalancesAtDate(session, balanceAssertion.dt));
|
|
||||||
}
|
|
||||||
balanceAssertion.isValid = balanceAssertion.quantity === balancesForDate.get(balanceAssertion.dt)!.get(balanceAssertion.account) && balanceAssertion.commodity === db.metadata.reporting_commodity;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
balanceAssertions.value = rawBalanceAssertions as ValidatedBalanceAssertion[];
|
balanceAssertions.value = rawBalanceAssertions as ValidatedBalanceAssertion[];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user