Skip to content

Commit

Permalink
feat: add trial balance ledger boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
vindard committed Jan 21, 2025
1 parent 5f6b002 commit 0b46d93
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
11 changes: 11 additions & 0 deletions core/statements/src/trial_balance/ledger/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use thiserror::Error;

#[derive(Error, Debug)]
pub enum TrialBalanceStatementLedgerError {
#[error("TrialBalanceStatementLedgerError - Sqlx: {0}")]
Sqlx(#[from] sqlx::Error),
#[error("TrialBalanceStatementLedgerError - CalaLedger: {0}")]
CalaLedger(#[from] cala_ledger::error::LedgerError),
#[error("TrialBalanceStatementLedgerError - CalaAccountSetError: {0}")]
CalaAccountSet(#[from] cala_ledger::account_set::error::AccountSetError),
}
65 changes: 65 additions & 0 deletions core/statements/src/trial_balance/ledger/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
pub mod error;

use cala_ledger::{account_set::NewAccountSet, CalaLedger, DebitOrCredit, JournalId};

use crate::primitives::LedgerAccountSetId;

use error::*;

#[derive(Clone)]
pub struct TrialBalanceStatementLedger {
cala: CalaLedger,
journal_id: JournalId,
}

impl TrialBalanceStatementLedger {
pub fn new(cala: &CalaLedger, journal_id: JournalId) -> Self {
Self {
cala: cala.clone(),
journal_id,
}
}

pub async fn create(
&self,
op: es_entity::DbOp<'_>,
statement_id: impl Into<LedgerAccountSetId>,
name: &str,
) -> Result<(), TrialBalanceStatementLedgerError> {
let mut op = self.cala.ledger_operation_from_db_op(op);

let new_account_set = NewAccountSet::builder()
.id(statement_id)
.journal_id(self.journal_id)
.name(name)
.description(name)
.normal_balance_type(DebitOrCredit::Debit)
.build()
.expect("Could not build new account set");
self.cala
.account_sets()
.create_in_op(&mut op, new_account_set)
.await?;

op.commit().await?;
Ok(())
}

pub async fn add_member(
&self,
op: es_entity::DbOp<'_>,
statement_id: impl Into<LedgerAccountSetId>,
member: LedgerAccountSetId,
) -> Result<(), TrialBalanceStatementLedgerError> {
let statement_id = statement_id.into();

let mut op = self.cala.ledger_operation_from_db_op(op);
self.cala
.account_sets()
.add_member_in_op(&mut op, statement_id, member)
.await?;

op.commit().await?;
Ok(())
}
}
1 change: 1 addition & 0 deletions core/statements/src/trial_balance/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod entity;
pub mod error;
pub mod ledger;
mod repo;

pub(super) use entity::*;
Expand Down

0 comments on commit 0b46d93

Please sign in to comment.