Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get the correct unit when processing txn #349

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions integration-tests/multiple_commodity_balance/main.zhang
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
option "operating_currency" "CNY"

1970-01-01 commodity USD

2022-08-27 open Assets:AppStore:US USD,CNY
2022-08-27 open Liabilities:CreditCard USD,CNY
2022-08-27 open Equity:Opening-Balances USD,CNY

2024-04-07 * "" "初始AppStore账户"
Equity:Opening-Balances -82.03 CNY
Assets:AppStore:US 11.33 USD @@ 82.03 CNY

2024-04-08 * "" "充值AppleStore"
Liabilities:CreditCard -72.51 CNY
Assets:AppStore:US 10.00 USD @@ 72.51 CNY


2024-04-22 balance Assets:AppStore:US 21.33 USD
35 changes: 35 additions & 0 deletions integration-tests/multiple_commodity_balance/validations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[
{
"uri": "/api/store",
"validations": [
[
"$.data.errors.length()",
0
],
[
"$.data.commodity_lots.['Assets:AppStore:US'].[0].amount",
"21.33"
],
[
"$.data.commodity_lots.['Assets:AppStore:US'].[0].commodity",
"USD"
],
[
"$.data.commodity_lots.['Equity:Opening-Balances'].[0].amount",
"-82.03"
],
[
"$.data.commodity_lots.['Equity:Opening-Balances'].[0].commodity",
"CNY"
],
[
"$.data.commodity_lots.['Liabilities:CreditCard'].[0].amount",
"-72.51"
],
[
"$.data.commodity_lots.['Liabilities:CreditCard'].[0].commodity",
"CNY"
]
]
}
]
4 changes: 4 additions & 0 deletions zhang-ast/src/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ impl CalculatedAmount {
detail,
}
}
pub fn persist_commodity(mut self, commodity: &str) -> Self {
self.detail.entry(commodity.to_owned()).or_default();
self
}
Kilerd marked this conversation as resolved.
Show resolved Hide resolved
}

#[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)]
Expand Down
9 changes: 5 additions & 4 deletions zhang-core/src/process/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ impl DirectiveProcess for Transaction {
trace!("new balance checker starting with {}", &balance_checker);

for (posting_idx, txn_posting) in self.txn_postings().into_iter().enumerate() {
let inferred_amount = txn_posting
.infer_trade_amount()
.map_err(|kind| ZhangError::ProcessError { span: span.clone(), kind })?;
let inferred_amount = txn_posting.units().unwrap_or(
txn_posting
.infer_trade_amount()
.map_err(|kind| ZhangError::ProcessError { span: span.clone(), kind })?,
);
Kilerd marked this conversation as resolved.
Show resolved Hide resolved

let option = operations.account_target_day_balance(
txn_posting.posting.account.name(),
Expand All @@ -85,7 +87,6 @@ impl DirectiveProcess for Transaction {
commodity: inferred_amount.currency.clone(),
});
let after_number = (&previous.number).add(&inferred_amount.number);

operations.insert_transaction_posting(
&id,
posting_idx,
Expand Down
8 changes: 6 additions & 2 deletions zhang-server/src/routes/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ pub async fn get_account_list(ledger: State<Arc<RwLock<Ledger>>>) -> ApiResult<V
.into_iter()
.map(|balance| Amount::new(balance.balance_number, balance.balance_commodity))
.collect_vec();
let amount = account_balances.calculate(Utc::now().with_timezone(timezone), &mut operations)?;
let amount = account_balances
.calculate(Utc::now().with_timezone(timezone), &mut operations)?
.persist_commodity(&ledger.options.operating_currency);

ret.push(AccountResponse {
name: account,
Expand Down Expand Up @@ -60,7 +62,9 @@ pub async fn get_account_info(ledger: State<Arc<RwLock<Ledger>>>, path: Path<(St
.into_iter()
.map(|balance| Amount::new(balance.balance_number, balance.balance_commodity))
.collect_vec();
let amount = vec.calculate(Utc::now().with_timezone(timezone), &mut operations)?;
let amount = vec
.calculate(Utc::now().with_timezone(timezone), &mut operations)?
.persist_commodity(&ledger.options.operating_currency);

ResponseWrapper::json(AccountInfoResponse {
date: account_info.date,
Expand Down
Loading