Implement getting balance assertions from database

This commit is contained in:
RunasSudo 2025-05-28 00:15:52 +10:00
parent 4f1db12688
commit d326b3ae43
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
9 changed files with 87 additions and 15 deletions

View File

@ -23,8 +23,9 @@ use sqlx::sqlite::SqliteRow;
use sqlx::{Connection, Row, SqliteConnection}; use sqlx::{Connection, Row, SqliteConnection};
use crate::account_config::AccountConfiguration; use crate::account_config::AccountConfiguration;
use crate::statements::StatementLine; use crate::model::assertions::BalanceAssertion;
use crate::transaction::{Posting, Transaction, TransactionWithPostings}; use crate::model::statements::StatementLine;
use crate::model::transaction::{Posting, Transaction, TransactionWithPostings};
use crate::{util::format_date, QuantityInt}; use crate::{util::format_date, QuantityInt};
pub struct DbConnection { pub struct DbConnection {
@ -86,6 +87,31 @@ impl DbConnection {
account_configurations account_configurations
} }
/// Get balance assertions from the database
pub async fn get_balance_assertions(&self) -> Vec<BalanceAssertion> {
let mut connection = self.connect().await;
let balance_assertions = sqlx::query(
"SELECT id, dt, description, account, quantity, commodity
FROM balance_assertions
ORDER BY dt DESC, id DESC",
)
.map(|r: SqliteRow| BalanceAssertion {
id: r.get("id"),
dt: NaiveDateTime::parse_from_str(r.get("dt"), "%Y-%m-%d %H:%M:%S.%6f")
.expect("Invalid balance_assertions.dt"),
description: r.get("description"),
account: r.get("account"),
quantity: r.get("quantity"),
commodity: r.get("commodity"),
})
.fetch_all(&mut connection)
.await
.expect("SQL error");
balance_assertions
}
/// Get account balances from the database /// Get account balances from the database
pub async fn get_balances(&self, date: NaiveDate) -> HashMap<String, QuantityInt> { pub async fn get_balances(&self, date: NaiveDate) -> HashMap<String, QuantityInt> {
let mut connection = self.connect().await; let mut connection = self.connect().await;

View File

@ -1,9 +1,8 @@
pub mod account_config; pub mod account_config;
pub mod db; pub mod db;
pub mod model;
pub mod reporting; pub mod reporting;
pub mod transaction;
pub mod serde; pub mod serde;
pub mod statements;
pub mod util; pub mod util;
pub type QuantityInt = i64; pub type QuantityInt = i64;

33
src/model/assertions.rs Normal file
View File

@ -0,0 +1,33 @@
/*
DrCr: Web-based double-entry bookkeeping framework
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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use crate::QuantityInt;
#[derive(Deserialize, Serialize)]
pub struct BalanceAssertion {
pub id: Option<u64>,
#[serde(with = "crate::serde::naivedatetime_to_js")]
pub dt: NaiveDateTime,
pub description: String,
pub account: String,
pub quantity: QuantityInt,
pub commodity: String,
}

21
src/model/mod.rs Normal file
View File

@ -0,0 +1,21 @@
/*
DrCr: Web-based double-entry bookkeeping framework
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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
pub mod assertions;
pub mod statements;
pub mod transaction;

View File

@ -26,7 +26,7 @@ use std::fmt::Display;
use async_trait::async_trait; use async_trait::async_trait;
use tokio::sync::RwLock; use tokio::sync::RwLock;
use crate::transaction::update_balances_from_transactions; use crate::model::transaction::update_balances_from_transactions;
use super::calculator::{has_step_or_can_build, HasStepOrCanBuild, ReportingGraphDependencies}; use super::calculator::{has_step_or_can_build, HasStepOrCanBuild, ReportingGraphDependencies};
use super::executor::ReportingExecutionError; use super::executor::ReportingExecutionError;

View File

@ -26,10 +26,10 @@ use chrono::Datelike;
use tokio::sync::RwLock; use tokio::sync::RwLock;
use crate::account_config::kinds_for_account; use crate::account_config::kinds_for_account;
use crate::reporting::types::{BalancesAt, DateStartDateEndArgs, ReportingProductId, Transactions}; use crate::model::transaction::{
use crate::transaction::{
update_balances_from_transactions, Posting, Transaction, TransactionWithPostings, update_balances_from_transactions, Posting, Transaction, TransactionWithPostings,
}; };
use crate::reporting::types::{BalancesAt, DateStartDateEndArgs, ReportingProductId, Transactions};
use crate::util::{get_eofy, sofy_from_eofy}; use crate::util::{get_eofy, sofy_from_eofy};
use crate::QuantityInt; use crate::QuantityInt;

View File

@ -31,7 +31,7 @@ use serde::{Deserialize, Serialize};
use tokio::sync::RwLock; use tokio::sync::RwLock;
use crate::db::DbConnection; use crate::db::DbConnection;
use crate::transaction::TransactionWithPostings; use crate::model::transaction::TransactionWithPostings;
use crate::QuantityInt; use crate::QuantityInt;
use super::calculator::ReportingGraphDependencies; use super::calculator::ReportingGraphDependencies;
@ -173,13 +173,6 @@ pub struct Transactions {
pub transactions: Vec<TransactionWithPostings>, pub transactions: Vec<TransactionWithPostings>,
} }
impl Transactions {
/// Serialise the product (as JSON) using serde
pub fn to_json(&self) -> String {
serde_json::to_string(&self.transactions).unwrap()
}
}
impl ReportingProduct for Transactions {} impl ReportingProduct for Transactions {}
/// Records cumulative account balances at a particular point in time /// Records cumulative account balances at a particular point in time