From f3a51462230bae2734179aa7eeef30901daaee30 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sun, 26 Dec 2021 23:37:36 +0200 Subject: [PATCH] do not crash when sending transaction with just empty amounts if it is accepted by hledger-web, it must be legal --- .../ui/new_transaction/NewTransactionModel.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionModel.java index 81f1ed07..1fe215b2 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionModel.java @@ -443,7 +443,7 @@ public class NewTransactionModel extends ViewModel { LedgerTransaction tr = head.asLedgerTransaction(); tr.setComment(head.getComment()); - LedgerTransactionAccount emptyAmountAccount = null; + List emptyAmountAccounts = new ArrayList<>(); float emptyAmountAccountBalance = 0; for (int i = 1; i < list.size(); i++) { TransactionAccount item = list.get(i) @@ -462,14 +462,20 @@ public class NewTransactionModel extends ViewModel { emptyAmountAccountBalance += item.getAmount(); } else { - emptyAmountAccount = acc; + emptyAmountAccounts.add(acc); } tr.addAccount(acc); } - if (emptyAmountAccount != null) - emptyAmountAccount.setAmount(-emptyAmountAccountBalance); + if (emptyAmountAccounts.size() > 0) { + if (emptyAmountAccounts.size() > 1 && !Misc.isZero(emptyAmountAccountBalance)) + throw new RuntimeException(String.format(Locale.US, + "Should not happen. %d accounts with non zero amount to distribute (%5" + + ".3f)")); + for (LedgerTransactionAccount a : emptyAmountAccounts) + a.setAmount(-emptyAmountAccountBalance); + } return tr; } -- 2.39.2