]> git.ktnx.net Git - mobile-ledger.git/commitdiff
do not crash when sending transaction with just empty amounts
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 26 Dec 2021 21:37:36 +0000 (23:37 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 26 Dec 2021 21:37:36 +0000 (23:37 +0200)
if it is accepted by hledger-web, it must be legal

app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionModel.java

index 81f1ed07ae7d4d20753cc0304918be903cdec1a5..1fe215b291fd2d6e8a4cab24a5b86842633742b2 100644 (file)
@@ -443,7 +443,7 @@ public class NewTransactionModel extends ViewModel {
         LedgerTransaction tr = head.asLedgerTransaction();
 
         tr.setComment(head.getComment());
-        LedgerTransactionAccount emptyAmountAccount = null;
+        List<LedgerTransactionAccount> 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;
     }