1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
mod types;
use std::collections::HashMap;
use std::sync::Arc;
use chrono::Utc;
use types::*;
#[tokio::main(flavor = "current_thread")]
async fn main() {
let account_repo = Arc::new(InMemoryAccountRepository::default());
// Seed a few accounts.
let cash = account_repo
.create(NewAccount {
account_type: AccountType::Asset,
name: "Cash".into(),
description: String::new(),
parent: None,
})
.await
.unwrap();
let revenue = account_repo
.create(NewAccount {
account_type: AccountType::Revenue,
name: "Sales Revenue".into(),
description: String::new(),
parent: None,
})
.await
.unwrap();
let rent = account_repo
.create(NewAccount {
account_type: AccountType::Expense,
name: "Rent Expense".into(),
description: String::new(),
parent: None,
})
.await
.unwrap();
// Post entries through the repository — it validates and checks accounts exist.
let journal_repo = InMemoryJournalEntryRepository::new(account_repo.clone());
let sale = journal_repo
.create(NewJournalEntry {
date: Utc::now(),
description: "Cash sale".into(),
postings: vec![
NewPosting {
posting_type: PostingType::Debit,
account_id: cash.id(),
amount: 500_000,
},
NewPosting {
posting_type: PostingType::Credit,
account_id: revenue.id(),
amount: 500_000,
},
],
})
.await
.unwrap();
let rent_payment = journal_repo
.create(NewJournalEntry {
date: Utc::now(),
description: "Pay office rent".into(),
postings: vec![
NewPosting {
posting_type: PostingType::Debit,
account_id: rent.id(),
amount: 200_000,
},
NewPosting {
posting_type: PostingType::Credit,
account_id: cash.id(),
amount: 200_000,
},
],
})
.await
.unwrap();
let entries = vec![sale, rent_payment];
let accounts = vec![cash, revenue, rent];
print_journal(&entries, &accounts);
println!();
print_ledger(&entries, &accounts);
}
fn account_type_name(t: &AccountType) -> &'static str {
match t {
AccountType::Asset => "Asset",
AccountType::Liability => "Liability",
AccountType::Equity => "Equity",
AccountType::Revenue => "Revenue",
AccountType::Expense => "Expense",
}
}
/// Format a whole-won amount with thousands separators.
fn group(n: u64) -> String {
let s = n.to_string();
let b = s.as_bytes();
let mut out = String::new();
for (i, c) in b.iter().enumerate() {
if i > 0 && (b.len() - i) % 3 == 0 {
out.push(',');
}
out.push(*c as char);
}
out
}
fn signed(balance: i128) -> String {
if balance < 0 {
format!("-{}", group((-balance) as u64))
} else {
group(balance as u64)
}
}
fn print_journal(entries: &[JournalEntry], accounts: &[Account]) {
let by_id: HashMap<AccountId, &Account> = accounts.iter().map(|a| (a.id(), a)).collect();
println!("JOURNAL");
println!("{}", "-".repeat(52));
for entry in entries {
println!("{} {}", entry.date.format("%Y-%m-%d"), entry.description);
for p in &entry.postings {
let name = by_id
.get(&p.account_id)
.map(|a| a.name.as_str())
.unwrap_or("<unknown>");
let (dr, cr) = match p.posting_type {
PostingType::Debit => (group(p.amount), String::new()),
PostingType::Credit => (String::new(), group(p.amount)),
};
println!(" {:<18} {:>12} {:>12}", name, dr, cr);
}
}
}
fn print_ledger(entries: &[JournalEntry], accounts: &[Account]) {
// Accumulate debit/credit totals per account.
let mut totals: HashMap<AccountId, (u64, u64)> = HashMap::new();
for entry in entries {
for p in &entry.postings {
let e = totals.entry(p.account_id).or_insert((0, 0));
match p.posting_type {
PostingType::Debit => e.0 += p.amount,
PostingType::Credit => e.1 += p.amount,
}
}
}
println!("LEDGER (KRW)");
println!("{}", "-".repeat(72));
println!(
"{:<18} {:<10} {:>12} {:>12} {:>14}",
"Account", "Type", "Debits", "Credits", "Balance"
);
println!("{}", "-".repeat(72));
let (mut total_dr, mut total_cr) = (0u64, 0u64);
for account in accounts {
let (dr, cr) = totals.get(&account.id()).copied().unwrap_or((0, 0));
total_dr += dr;
total_cr += cr;
// Balance shown on the account's normal side (debit-normal for Asset/Expense).
let balance: i128 = match &account.account_type {
AccountType::Asset | AccountType::Expense => dr as i128 - cr as i128,
_ => cr as i128 - dr as i128,
};
println!(
"{:<18} {:<10} {:>12} {:>12} {:>14}",
account.name,
account_type_name(&account.account_type),
group(dr),
group(cr),
signed(balance),
);
}
println!("{}", "-".repeat(72));
println!(
"{:<18} {:<10} {:>12} {:>12}",
"TOTAL",
"",
group(total_dr),
group(total_cr)
);
if total_dr == total_cr {
println!("\nBooks balance: debits == credits ({} KRW)", group(total_dr));
} else {
println!("\nWARNING: books do not balance");
}
}
|