-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add trial balance ledger boilerplate
- Loading branch information
Showing
4 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub(super) const TRIAL_BALANCE_STATEMENT_NAME: &str = "Trial Balance"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
pub mod error; | ||
|
||
use cala_ledger::{account_set::NewAccountSet, CalaLedger, DebitOrCredit, JournalId}; | ||
|
||
use crate::primitives::LedgerAccountSetId; | ||
|
||
use error::*; | ||
|
||
use super::constants::TRIAL_BALANCE_STATEMENT_NAME; | ||
|
||
#[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>, | ||
) -> 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(TRIAL_BALANCE_STATEMENT_NAME) | ||
.description(TRIAL_BALANCE_STATEMENT_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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
mod constants; | ||
mod entity; | ||
pub mod error; | ||
pub mod ledger; | ||
mod repo; | ||
|
||
pub(super) use entity::*; | ||
|