|
@@ -1,3 +1,5 @@
|
|
|
+use std::collections::HashSet;
|
|
|
+
|
|
|
use itertools::Itertools;
|
|
|
|
|
|
use crate::{check::CheckLevel, data, import::import_from, io, show};
|
|
@@ -39,13 +41,19 @@ pub enum Command {
|
|
|
Ledger {
|
|
|
account: String,
|
|
|
},
|
|
|
- Reformat,
|
|
|
+ Reformat {
|
|
|
+ #[clap(long)]
|
|
|
+ /// do not add ID annotations to transactions lacking them
|
|
|
+ skip_ids: bool,
|
|
|
+ },
|
|
|
Import {
|
|
|
account: String,
|
|
|
from: std::path::PathBuf,
|
|
|
target: Option<std::path::PathBuf>,
|
|
|
},
|
|
|
- Dedup,
|
|
|
+ Match {
|
|
|
+ account: data::AccountName
|
|
|
+ },
|
|
|
}
|
|
|
|
|
|
fn summarize(data: &data::Root) {
|
|
@@ -108,8 +116,31 @@ impl Command {
|
|
|
log::error!("account not found!");
|
|
|
}
|
|
|
}
|
|
|
- Self::Reformat => {
|
|
|
- let data = load_data(&mut fsdata, inv, CheckLevel::WellFormed)?;
|
|
|
+ Self::Reformat { skip_ids } => {
|
|
|
+ let mut data = load_data(&mut fsdata, inv, CheckLevel::WellFormed)?;
|
|
|
+
|
|
|
+ if !skip_ids {
|
|
|
+ let mut used_ids = HashSet::new();
|
|
|
+ for le in data.all_ledger_data() {
|
|
|
+ let Some(txn) = le.as_transaction() else { continue };
|
|
|
+ let Some(id) = txn.get_annotation("id") else { continue };
|
|
|
+ used_ids.insert(id.to_owned());
|
|
|
+ }
|
|
|
+
|
|
|
+ // assign IDs if to transactions lacking them
|
|
|
+ for le in data.all_ledger_data_mut() {
|
|
|
+ let Some(txn) = le.as_transaction_mut() else { continue };
|
|
|
+ if !txn.get_annotation("id").is_some() {
|
|
|
+ // generated unique ID
|
|
|
+ let mut id = nanoid::nanoid!(10);
|
|
|
+ while used_ids.contains(&id) {
|
|
|
+ id = nanoid::nanoid!(10);
|
|
|
+ }
|
|
|
+ // assign ID
|
|
|
+ txn.annotations.push(format!("id:{id}"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
data::ledger::print_ledger(&mut fsdata, &data, data.all_ledger_data().iter())?;
|
|
|
}
|
|
@@ -151,14 +182,17 @@ impl Command {
|
|
|
tt.show(Some(&data), aname, imported.iter());
|
|
|
}
|
|
|
}
|
|
|
- Self::Dedup => {
|
|
|
- /*
|
|
|
+ Self::Match { account } => {
|
|
|
let data = load_data(&mut fsdata, inv, CheckLevel::WellFormed)?;
|
|
|
|
|
|
- let is_dup = |txn1: &data::ledger::Transaction, txn2: &data::ledger::Transaction| {
|
|
|
+ let Some(acc_data) = data.ledger_data_for(*account) else {
|
|
|
+ log::error!("No ledger data available for account {account}");
|
|
|
+ return Ok(())
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
|
|
|
- };*/
|
|
|
- todo!()
|
|
|
+ show::TransactionTable::default().show(Some(&data), *account, acc_data.iter().map(|d| &d.0));
|
|
|
}
|
|
|
}
|
|
|
Ok(())
|