diff --git a/core/chart-of-accounts/src/chart_of_accounts/entity.rs b/core/chart-of-accounts/src/chart_of_accounts/entity.rs index f2c793feb..f32656a4c 100644 --- a/core/chart-of-accounts/src/chart_of_accounts/entity.rs +++ b/core/chart-of-accounts/src/chart_of_accounts/entity.rs @@ -7,7 +7,7 @@ use es_entity::*; use crate::{ path::*, - primitives::{ChartAccountDetails, ChartCreationDetails, ChartId, LedgerAccountId}, + primitives::{ChartAccountDetails, ChartCreationDetails, ChartId}, }; pub use super::error::*; @@ -35,14 +35,6 @@ pub enum ChartEvent { reference: String, audit_info: AuditInfo, }, - TransactionAccountAdded { - id: LedgerAccountId, - encoded_path: String, - path: TransactionAccountPath, - name: String, - description: String, - audit_info: AuditInfo, - }, } #[derive(EsEntity, Builder)] @@ -167,46 +159,19 @@ impl Chart { Ok(path) } - fn next_transaction_account( - &self, - control_sub_account: ControlSubAccountPath, - ) -> Result { - Ok(self - .events - .iter_all() - .rev() - .find_map(|event| match event { - ChartEvent::TransactionAccountAdded { path, .. } - if path.category == control_sub_account.category - && path.control_account() == control_sub_account.control_account() - && path.control_sub_account() == control_sub_account => - { - Some(path.next()) - } - _ => None, - }) - .unwrap_or(Ok(control_sub_account.first_transaction_account()))?) - } - pub fn add_transaction_account( &mut self, creation_details: ChartCreationDetails, - audit_info: AuditInfo, + _audit_info: AuditInfo, ) -> Result { - let path = self.next_transaction_account(creation_details.control_sub_account)?; - self.events.push(ChartEvent::TransactionAccountAdded { - id: creation_details.account_id, - encoded_path: path.path_encode(self.id), - path, - name: creation_details.name.clone(), - description: creation_details.description.clone(), - audit_info, - }); + // TODO: implement by potentially adding account to ControlSubAccount nested entity Ok(ChartAccountDetails { account_id: creation_details.account_id, - encoded_path: path.path_encode(self.id), - path, + encoded_path: format!( + "{}.{}", + creation_details.control_sub_account, creation_details.account_id + ), name: creation_details.name, description: creation_details.description, }) @@ -223,7 +188,6 @@ impl TryFromEvents for Chart { } ChartEvent::ControlAccountAdded { .. } => (), ChartEvent::ControlSubAccountAdded { .. } => (), - ChartEvent::TransactionAccountAdded { .. } => (), } } builder.events(events).build() @@ -415,52 +379,6 @@ mod tests { } } - #[test] - fn test_create_transaction_account() { - let mut chart = init_chart_of_events(); - let control_account = chart - .create_control_account( - ChartCategory::Assets, - "Assets".to_string(), - "assets".to_string(), - dummy_audit_info(), - ) - .unwrap(); - let control_sub_account = chart - .create_control_sub_account( - control_account, - "Current Assets".to_string(), - "current-assets".to_string(), - dummy_audit_info(), - ) - .unwrap(); - - let ChartAccountDetails { - path: - TransactionAccountPath { - category, - control_index, - control_sub_index, - index, - }, - .. - } = chart - .add_transaction_account( - ChartCreationDetails { - account_id: LedgerAccountId::new(), - control_sub_account, - name: "Cash".to_string(), - description: "Cash account".to_string(), - }, - dummy_audit_info(), - ) - .unwrap(); - assert_eq!(category, ChartCategory::Assets); - assert_eq!(control_index, AccountIdx::FIRST); - assert_eq!(control_sub_index, AccountIdx::FIRST); - assert_eq!(index, AccountIdx::FIRST); - } - #[test] fn test_create_sequential_control_accounts() { let mut chart = init_chart_of_events(); @@ -523,62 +441,4 @@ mod tests { assert_eq!(control_index, AccountIdx::FIRST); assert_eq!(index, AccountIdx::FIRST.next()); } - - #[test] - fn test_create_sequential_transaction_accounts() { - let mut chart = init_chart_of_events(); - let control_account = chart - .create_control_account( - ChartCategory::Assets, - "Assets".to_string(), - "assets".to_string(), - dummy_audit_info(), - ) - .unwrap(); - let control_sub_account = chart - .create_control_sub_account( - control_account, - "Current Assets".to_string(), - "current-assets".to_string(), - dummy_audit_info(), - ) - .unwrap(); - - chart - .add_transaction_account( - ChartCreationDetails { - account_id: LedgerAccountId::new(), - control_sub_account, - name: "First".to_string(), - description: "First transaction account".to_string(), - }, - dummy_audit_info(), - ) - .unwrap(); - - let ChartAccountDetails { - path: - TransactionAccountPath { - category, - control_index, - control_sub_index, - index, - }, - .. - } = chart - .add_transaction_account( - ChartCreationDetails { - account_id: LedgerAccountId::new(), - control_sub_account, - name: "Second".to_string(), - description: "Second transaction account".to_string(), - }, - dummy_audit_info(), - ) - .unwrap(); - assert_eq!(category, ChartCategory::Assets); - assert_eq!(control_index, AccountIdx::FIRST); - assert_eq!(control_sub_index, AccountIdx::FIRST); - assert_eq!(index, AccountIdx::FIRST.next()); - } } diff --git a/core/chart-of-accounts/src/path/error.rs b/core/chart-of-accounts/src/path/error.rs index 4a2a037fa..7131832bc 100644 --- a/core/chart-of-accounts/src/path/error.rs +++ b/core/chart-of-accounts/src/path/error.rs @@ -10,8 +10,6 @@ pub enum ChartPathError { InvalidCategoryForNewControlAccount, #[error("ChartError - InvalidControlAccountPathForNewControlSubAccount")] InvalidControlAccountPathForNewControlSubAccount, - #[error("ChartError - InvalidSubControlAccountPathForNewTransactionAccount")] - InvalidSubControlAccountPathForNewTransactionAccount, #[error("ChartError - ControlIndexOverflowForCategory: Category '{0}'")] ControlIndexOverflowForCategory(ChartCategory), #[error( diff --git a/core/chart-of-accounts/src/path/mod.rs b/core/chart-of-accounts/src/path/mod.rs index 15ad8c411..049c4d941 100644 --- a/core/chart-of-accounts/src/path/mod.rs +++ b/core/chart-of-accounts/src/path/mod.rs @@ -153,72 +153,6 @@ impl ControlSubAccountPath { format!("{}::{}", chart_id, self) } - pub fn control_account(&self) -> ControlAccountPath { - ControlAccountPath { - category: self.category, - index: self.control_index, - } - } - - pub fn first_transaction_account(&self) -> TransactionAccountPath { - TransactionAccountPath { - category: self.category, - control_index: self.control_index, - control_sub_index: self.index, - index: AccountIdx::FIRST, - } - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] -pub struct TransactionAccountPath { - pub category: ChartCategory, - pub control_index: AccountIdx, - pub control_sub_index: AccountIdx, - pub index: AccountIdx, -} - -impl std::fmt::Display for TransactionAccountPath { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{:0 Result { - let next_index = self.index.next(); - if next_index > AccountIdx::MAX_THREE_DIGIT { - Err( - ChartPathError::TransactionIndexOverflowForControlSubAccount( - self.category, - self.control_index, - self.control_sub_index, - ), - ) - } else { - Ok(Self { - category: self.category, - control_index: self.control_index, - control_sub_index: self.control_sub_index, - index: next_index, - }) - } - } - - pub fn path_encode(&self, chart_id: ChartId) -> String { - format!("{}::{}", chart_id, self) - } - pub fn normal_balance_type(&self) -> DebitOrCredit { match self.category { ChartCategory::Assets | ChartCategory::Expenses => DebitOrCredit::Debit, @@ -232,14 +166,6 @@ impl TransactionAccountPath { index: self.control_index, } } - - pub fn control_sub_account(&self) -> ControlSubAccountPath { - ControlSubAccountPath { - category: self.category, - control_index: self.control_index, - index: self.control_index, - } - } } #[cfg(test)] @@ -273,17 +199,6 @@ mod tests { }; assert_eq!(path.to_string(), "30102000"); } - - #[test] - fn test_transaction_account_formatting() { - let path = TransactionAccountPath { - category: ChartCategory::Revenues, - control_index: 1.into(), - control_sub_index: 2.into(), - index: 3.into(), - }; - assert_eq!(path.to_string(), "40102003"); - } } mod control_account_extraction_tests { @@ -296,18 +211,6 @@ mod tests { index: CONTROL_INDEX, }; - #[test] - fn test_control_account_from_transaction_account() { - let transaction = TransactionAccountPath { - category: CATEGORY, - control_index: CONTROL_INDEX, - control_sub_index: 2.into(), - index: 3.into(), - }; - - assert_eq!(transaction.control_account(), EXPECTED); - } - #[test] fn test_control_account_from_control_sub_account() { let sub_account = ControlSubAccountPath { @@ -320,31 +223,6 @@ mod tests { } } - mod control_sub_account_extraction_tests { - use super::*; - - const CATEGORY: ChartCategory = ChartCategory::Assets; - const CONTROL_INDEX: AccountIdx = AccountIdx::FIRST; - const SUB_INDEX: AccountIdx = AccountIdx::FIRST; - const EXPECTED: ControlSubAccountPath = ControlSubAccountPath { - category: CATEGORY, - control_index: CONTROL_INDEX, - index: SUB_INDEX, - }; - - #[test] - fn test_control_sub_account_from_transaction_account() { - let transaction = TransactionAccountPath { - category: CATEGORY, - control_index: CONTROL_INDEX, - control_sub_index: SUB_INDEX, - index: 3.into(), - }; - - assert_eq!(transaction.control_sub_account(), EXPECTED); - } - } - mod first_account_create { use super::*; @@ -379,26 +257,6 @@ mod tests { } ); } - - #[test] - fn test_first_transaction_account_creation() { - let sub_account = ControlSubAccountPath { - category: ChartCategory::Assets, - control_index: AccountIdx::FIRST, - index: AccountIdx::FIRST, - }; - - let transaction = sub_account.first_transaction_account(); - assert_eq!( - transaction, - TransactionAccountPath { - category: ChartCategory::Assets, - control_index: AccountIdx::FIRST, - control_sub_index: AccountIdx::FIRST, - index: AccountIdx::FIRST, - } - ); - } } mod next_account_create { @@ -458,37 +316,5 @@ mod tests { }; assert!(max_sub.next().is_err()); } - - #[test] - fn test_next_transaction_account_success() { - let transaction = TransactionAccountPath { - category: ChartCategory::Assets, - control_index: 1.into(), - control_sub_index: 1.into(), - index: 1.into(), - }; - - let next_transaction = transaction.next().unwrap(); - assert_eq!( - next_transaction, - TransactionAccountPath { - category: ChartCategory::Assets, - control_index: 1.into(), - control_sub_index: 1.into(), - index: 2.into(), - } - ); - } - - #[test] - fn test_next_transaction_account_overflow() { - let max_transaction = TransactionAccountPath { - category: ChartCategory::Assets, - control_index: 1.into(), - control_sub_index: 1.into(), - index: AccountIdx::MAX_THREE_DIGIT, - }; - assert!(max_transaction.next().is_err()); - } } } diff --git a/core/chart-of-accounts/src/primitives.rs b/core/chart-of-accounts/src/primitives.rs index f4ec825c7..c1843168d 100644 --- a/core/chart-of-accounts/src/primitives.rs +++ b/core/chart-of-accounts/src/primitives.rs @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize}; pub use cala_ledger::{primitives::AccountId as LedgerAccountId, DebitOrCredit}; pub use crate::path::ChartCategory; -use crate::path::{ControlSubAccountPath, TransactionAccountPath}; +use crate::path::ControlSubAccountPath; es_entity::entity_id! { ChartId, @@ -75,8 +75,6 @@ impl CoreChartOfAccountsAction { CoreChartOfAccountsAction::ChartAction(ChartAction::CreateControlSubAccount); pub const CHART_FIND_CONTROL_SUB_ACCOUNT: Self = CoreChartOfAccountsAction::ChartAction(ChartAction::FindControlSubAccount); - pub const CHART_FIND_TRANSACTION_ACCOUNT: Self = - CoreChartOfAccountsAction::ChartAction(ChartAction::FindTransactionAccount); } impl Display for CoreChartOfAccountsAction { @@ -112,7 +110,6 @@ pub enum ChartAction { FindControlAccount, CreateControlSubAccount, FindControlSubAccount, - FindTransactionAccount, } impl From for CoreChartOfAccountsAction { @@ -124,7 +121,6 @@ impl From for CoreChartOfAccountsAction { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ChartAccountDetails { pub account_id: LedgerAccountId, - pub path: TransactionAccountPath, pub encoded_path: String, pub name: String, pub description: String, diff --git a/core/chart-of-accounts/src/transaction_account_factory.rs b/core/chart-of-accounts/src/transaction_account_factory.rs index 726a5b2ed..4cf4aab64 100644 --- a/core/chart-of-accounts/src/transaction_account_factory.rs +++ b/core/chart-of-accounts/src/transaction_account_factory.rs @@ -61,7 +61,7 @@ impl TransactionAccountFactory { .name(account_details.name.to_string()) .description(account_details.description.to_string()) .code(account_details.encoded_path.to_string()) - .normal_balance_type(account_details.path.normal_balance_type()) + .normal_balance_type(self.control_sub_account.normal_balance_type()) .build() .expect("Could not build new account");