Skip to content

Commit

Permalink
refactor: swap core/statements for lana/trial_balances in seed
Browse files Browse the repository at this point in the history
  • Loading branch information
vindard committed Jan 22, 2025
1 parent ee38b6b commit e8800a1
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 73 deletions.
2 changes: 0 additions & 2 deletions lana/app/src/accounting_init/constants/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ pub(super) const CHART_NAME: &str = "Chart of Accounts";
pub const OBS_CHART_REF: &str = "off-balance-sheet-chart";
pub(super) const OBS_CHART_NAME: &str = "Off-Balance-Sheet Chart of Accounts";

pub const TRIAL_BALANCE_STATEMENT_REF: &str = "trial-balance-statement";
pub(super) const TRIAL_BALANCE_STATEMENT_NAME: &str = "Trial Balance";

pub const OBS_TRIAL_BALANCE_STATEMENT_REF: &str = "off-balance-sheet-trial-balance-statement";
pub(super) const OBS_TRIAL_BALANCE_STATEMENT_NAME: &str = "Off-Balance-Sheet Trial Balance";
4 changes: 2 additions & 2 deletions lana/app/src/accounting_init/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use thiserror::Error;
pub enum AccountingInitError {
#[error("AccountingInitError - CoreChartOfAccountsError: {0}")]
CoreChartOfAccountsError(#[from] chart_of_accounts::error::CoreChartOfAccountsError),
#[error("AccountingInitError - CoreStatementsError: {0}")]
CoreStatementsError(#[from] statements::error::CoreStatementsError),
#[error("ApplicationError - JournalError: {0}")]
JournalError(#[from] cala_ledger::journal::error::JournalError),
#[error("ApplicationError - TrialBalanceError: {0}")]
TrialBalanceError(#[from] crate::trial_balance::error::TrialBalanceError),
}
14 changes: 7 additions & 7 deletions lana/app/src/accounting_init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ pub mod error;

use chart_of_accounts::ChartId;

use crate::{chart_of_accounts::ChartOfAccounts, statements::Statements};
use crate::{chart_of_accounts::ChartOfAccounts, trial_balance::TrialBalances};

use cala_ledger::CalaLedger;

use error::*;
pub use primitives::CreditFacilitiesAccountPaths;
use primitives::{ChartIds, DepositsAccountPaths, LedgerJournalId, TrialBalanceStatementIds};
use primitives::*;

#[derive(Clone)]
pub struct JournalInit {
Expand All @@ -27,12 +27,12 @@ impl JournalInit {

#[derive(Clone)]
pub struct StatementsInit {
trial_balance_ids: TrialBalanceStatementIds,
trial_balance_ids: TrialBalanceIds,
}

impl StatementsInit {
pub async fn statements(statements: &Statements) -> Result<Self, AccountingInitError> {
seed::statements::init(statements).await
pub async fn statements(trial_balances: &TrialBalances) -> Result<Self, AccountingInitError> {
seed::statements::init(trial_balances).await
}
}

Expand All @@ -45,9 +45,9 @@ pub struct ChartsInit {

impl ChartsInit {
pub async fn charts_of_accounts(
statements: &Statements,
trial_balances: &TrialBalances,
chart_of_accounts: &ChartOfAccounts,
) -> Result<Self, AccountingInitError> {
seed::charts_of_accounts::init(statements, chart_of_accounts).await
seed::charts_of_accounts::init(trial_balances, chart_of_accounts).await
}
}
9 changes: 5 additions & 4 deletions lana/app/src/accounting_init/primitives.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
pub use cala_ledger::primitives::JournalId as LedgerJournalId;

use chart_of_accounts::{ChartId, ControlSubAccountDetails};
use statements::TrialBalanceStatementId;

pub use crate::primitives::TrialBalanceId;

#[derive(Clone, Copy)]
pub struct ChartIds {
Expand All @@ -10,9 +11,9 @@ pub struct ChartIds {
}

#[derive(Clone, Copy)]
pub struct TrialBalanceStatementIds {
pub primary: TrialBalanceStatementId,
pub off_balance_sheet: TrialBalanceStatementId,
pub struct TrialBalanceIds {
pub primary: TrialBalanceId,
pub off_balance_sheet: TrialBalanceId,
}

#[derive(Clone)]
Expand Down
59 changes: 29 additions & 30 deletions lana/app/src/accounting_init/seed/charts_of_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ use crate::{
};

pub(crate) async fn init(
statements: &Statements,
trial_balances: &TrialBalances,
chart_of_accounts: &ChartOfAccounts,
) -> Result<ChartsInit, AccountingInitError> {
let chart_ids = &create_charts_of_accounts(chart_of_accounts).await?;

let deposits = create_deposits_account_paths(statements, chart_of_accounts, chart_ids).await?;
let deposits =
create_deposits_account_paths(trial_balances, chart_of_accounts, chart_ids).await?;

let credit_facilities =
create_credit_facilities_account_paths(statements, chart_of_accounts, chart_ids).await?;
create_credit_facilities_account_paths(trial_balances, chart_of_accounts, chart_ids)
.await?;

Ok(ChartsInit {
chart_ids: *chart_ids,
Expand Down Expand Up @@ -119,17 +121,17 @@ async fn create_control_sub_account(
}

async fn create_deposits_account_paths(
statements: &Statements,
trial_balances: &TrialBalances,
chart_of_accounts: &ChartOfAccounts,
chart_ids: &ChartIds,
) -> Result<DepositsAccountPaths, AccountingInitError> {
let trial_balance = statements
.find_by_reference(TRIAL_BALANCE_STATEMENT_REF.to_string())
let trial_balance_id = trial_balances
.find_by_name(TRIAL_BALANCE_STATEMENT_NAME.to_string())
.await?
.unwrap_or_else(|| {
panic!(
"Trial balance for reference '{}' not found",
TRIAL_BALANCE_STATEMENT_REF
"Trial balance for name '{}' not found",
TRIAL_BALANCE_STATEMENT_NAME
)
});

Expand All @@ -147,25 +149,25 @@ async fn create_deposits_account_paths(
DEPOSITS_CONTROL_SUB_ACCOUNT_REF.to_string(),
)
.await?;
statements
.add_to_trial_balance_statement(trial_balance.id, deposits_control.account_set_id)
trial_balances
.add_to_trial_balance(trial_balance_id, deposits_control.account_set_id)
.await?;

Ok(DepositsAccountPaths { deposits })
}

async fn create_credit_facilities_account_paths(
statements: &Statements,
trial_balances: &TrialBalances,
chart_of_accounts: &ChartOfAccounts,
chart_ids: &ChartIds,
) -> Result<CreditFacilitiesAccountPaths, AccountingInitError> {
let trial_balance = statements
.find_by_reference(TRIAL_BALANCE_STATEMENT_REF.to_string())
let trial_balance_id = trial_balances
.find_by_name(TRIAL_BALANCE_STATEMENT_NAME.to_string())
.await?
.unwrap_or_else(|| {
panic!(
"Trial balance for reference '{}' not found",
TRIAL_BALANCE_STATEMENT_REF
TRIAL_BALANCE_STATEMENT_NAME
)
});

Expand All @@ -183,8 +185,8 @@ async fn create_credit_facilities_account_paths(
CREDIT_FACILITIES_COLLATERAL_CONTROL_SUB_ACCOUNT_REF.to_string(),
)
.await?;
statements
.add_to_trial_balance_statement(trial_balance.id, collateral_control.account_set_id)
trial_balances
.add_to_trial_balance(trial_balance_id, collateral_control.account_set_id)
.await?;

let (facility_control, facility) = create_control_sub_account(
Expand All @@ -201,8 +203,8 @@ async fn create_credit_facilities_account_paths(
CREDIT_FACILITIES_FACILITY_CONTROL_SUB_ACCOUNT_REF.to_string(),
)
.await?;
statements
.add_to_trial_balance_statement(trial_balance.id, facility_control.account_set_id)
trial_balances
.add_to_trial_balance(trial_balance_id, facility_control.account_set_id)
.await?;

let (disbursed_receivable_control, disbursed_receivable) = create_control_sub_account(
Expand All @@ -219,9 +221,9 @@ async fn create_credit_facilities_account_paths(
CREDIT_FACILITIES_DISBURSED_RECEIVABLE_CONTROL_SUB_ACCOUNT_REF.to_string(),
)
.await?;
statements
.add_to_trial_balance_statement(
trial_balance.id,
trial_balances
.add_to_trial_balance(
trial_balance_id,
disbursed_receivable_control.account_set_id,
)
.await?;
Expand All @@ -240,11 +242,8 @@ async fn create_credit_facilities_account_paths(
CREDIT_FACILITIES_INTEREST_RECEIVABLE_CONTROL_SUB_ACCOUNT_REF.to_string(),
)
.await?;
statements
.add_to_trial_balance_statement(
trial_balance.id,
interest_receivable_control.account_set_id,
)
trial_balances
.add_to_trial_balance(trial_balance_id, interest_receivable_control.account_set_id)
.await?;

let (interest_income_control, interest_income) = create_control_sub_account(
Expand All @@ -261,8 +260,8 @@ async fn create_credit_facilities_account_paths(
CREDIT_FACILITIES_INTEREST_INCOME_CONTROL_SUB_ACCOUNT_REF.to_string(),
)
.await?;
statements
.add_to_trial_balance_statement(trial_balance.id, interest_income_control.account_set_id)
trial_balances
.add_to_trial_balance(trial_balance_id, interest_income_control.account_set_id)
.await?;

let (fee_income_control, fee_income) = create_control_sub_account(
Expand All @@ -279,8 +278,8 @@ async fn create_credit_facilities_account_paths(
CREDIT_FACILITIES_FEE_INCOME_CONTROL_SUB_ACCOUNT_REF.to_string(),
)
.await?;
statements
.add_to_trial_balance_statement(trial_balance.id, fee_income_control.account_set_id)
trial_balances
.add_to_trial_balance(trial_balance_id, fee_income_control.account_set_id)
.await?;

Ok(CreditFacilitiesAccountPaths {
Expand Down
43 changes: 19 additions & 24 deletions lana/app/src/accounting_init/seed/statements.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,50 @@
use constants::{
OBS_TRIAL_BALANCE_STATEMENT_NAME, OBS_TRIAL_BALANCE_STATEMENT_REF,
TRIAL_BALANCE_STATEMENT_NAME, TRIAL_BALANCE_STATEMENT_REF,
};
use primitives::TrialBalanceStatementIds;
use statements::TrialBalanceStatementId;
use constants::{OBS_TRIAL_BALANCE_STATEMENT_NAME, TRIAL_BALANCE_STATEMENT_NAME};

use crate::accounting_init::*;

pub(crate) async fn init(statements: &Statements) -> Result<StatementsInit, AccountingInitError> {
let trial_balance_ids = create_trial_balances(statements).await?;
pub(crate) async fn init(
trial_balances: &TrialBalances,
) -> Result<StatementsInit, AccountingInitError> {
let trial_balance_ids = create_trial_balances(trial_balances).await?;

Ok(StatementsInit { trial_balance_ids })
}

async fn create_trial_balances(
statements: &Statements,
) -> Result<TrialBalanceStatementIds, AccountingInitError> {
let primary = match statements
.find_by_reference(TRIAL_BALANCE_STATEMENT_REF.to_string())
trial_balances: &TrialBalances,
) -> Result<TrialBalanceIds, AccountingInitError> {
let primary_id = match trial_balances
.find_by_name(TRIAL_BALANCE_STATEMENT_NAME.to_string())
.await?
{
Some(statement) => statement,
Some(trial_balance_id) => trial_balance_id,
None => {
statements
trial_balances
.create_trial_balance_statement(
TrialBalanceStatementId::new(),
TrialBalanceId::new(),
TRIAL_BALANCE_STATEMENT_NAME.to_string(),
TRIAL_BALANCE_STATEMENT_REF.to_string(),
)
.await?
}
};

let off_balance_sheet = match statements
.find_by_reference(OBS_TRIAL_BALANCE_STATEMENT_REF.to_string())
let off_balance_sheet_id = match trial_balances
.find_by_name(OBS_TRIAL_BALANCE_STATEMENT_NAME.to_string())
.await?
{
Some(chart) => chart,
None => {
statements
trial_balances
.create_trial_balance_statement(
TrialBalanceStatementId::new(),
TrialBalanceId::new(),
OBS_TRIAL_BALANCE_STATEMENT_NAME.to_string(),
OBS_TRIAL_BALANCE_STATEMENT_REF.to_string(),
)
.await?
}
};

Ok(TrialBalanceStatementIds {
primary: primary.id,
off_balance_sheet: off_balance_sheet.id,
Ok(TrialBalanceIds {
primary: primary_id.into(),
off_balance_sheet: off_balance_sheet_id.into(),
})
}
10 changes: 6 additions & 4 deletions lana/app/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ use crate::{
price::Price,
primitives::Subject,
report::Reports,
statements::Statements,
storage::Storage,
terms_template::TermsTemplates,
trial_balance::TrialBalances,
user::Users,
};

Expand Down Expand Up @@ -76,11 +76,13 @@ impl LanaApp {
.expect("cala config");
let cala = cala_ledger::CalaLedger::init(cala_config).await?;
let journal_init = JournalInit::journal(&cala).await?;
let statements = Statements::init(&pool, &authz, &cala, journal_init.journal_id).await?;
let _statements_init = StatementsInit::statements(&statements).await?;
let trial_balances =
TrialBalances::init(&pool, &authz, &cala, journal_init.journal_id).await?;
let _statements_init = StatementsInit::statements(&trial_balances).await?;
let chart_of_accounts =
ChartOfAccounts::init(&pool, &authz, &cala, journal_init.journal_id).await?;
let charts_init = ChartsInit::charts_of_accounts(&statements, &chart_of_accounts).await?;
let charts_init =
ChartsInit::charts_of_accounts(&trial_balances, &chart_of_accounts).await?;

let deposits_factory =
chart_of_accounts.transaction_account_factory(charts_init.deposits.deposits);
Expand Down

0 comments on commit e8800a1

Please sign in to comment.