+ // 3) for each commodity:
+ // 3a) amount must balance to 0, or
+ // 3b) there must be exactly one empty amount (with account)
+ for (String balCurrency : itemsForCurrency.currencies()) {
+ float currencyBalance = balance.get(balCurrency);
+ if (Misc.isZero(currencyBalance)) {
+ // remove hints from all amount inputs in that currency
+ for (Item item : items) {
+ if (Currency.equal(item.getCurrency(), balCurrency))
+ item.setAmountHint(null);
+ }
+ }
+ else {
+ List<Item> list =
+ itemsWithAccountAndEmptyAmountForCurrency.getList(balCurrency);
+ int balanceReceiversCount = list.size();
+ if (balanceReceiversCount != 1) {
+ if (BuildConfig.DEBUG) {
+ if (balanceReceiversCount == 0)
+ Logger.debug("submittable", String.format(
+ "Transaction not submittable [%s]: non-zero balance " +
+ "with no empty amounts with accounts", balCurrency));
+ else
+ Logger.debug("submittable", String.format(
+ "Transaction not submittable [%s]: non-zero balance " +
+ "with multiple empty amounts with accounts", balCurrency));
+ }
+ submittable = false;
+ }
+
+ List<Item> emptyAmountList =
+ itemsWithEmptyAmountForCurrency.getList(balCurrency);
+
+ // suggest off-balance amount to a row and remove hints on other rows
+ Item receiver = null;
+ if (!list.isEmpty())
+ receiver = list.get(0);
+ else if (!emptyAmountList.isEmpty())
+ receiver = emptyAmountList.get(0);
+
+ for (Item item : items) {
+ if (!Currency.equal(item.getCurrency(), balCurrency))
+ continue;
+
+ if (item.equals(receiver)) {
+ if (BuildConfig.DEBUG)
+ Logger.debug("submittable",
+ String.format("Setting amount hint to %1.2f [%s]",
+ -currencyBalance, balCurrency));
+ item.setAmountHint(String.format("%1.2f", -currencyBalance));
+ }
+ else {
+ if (BuildConfig.DEBUG)
+ Logger.debug("submittable",
+ String.format("Resetting hint of '%s' [%s]",
+ (item.getAccount() == null) ? "" : item.getAccount()
+ .getAccountName(),
+ balCurrency));
+ item.setAmountHint(null);
+ }
+ }
+ }
+ }
+
+ // 5) a row with an empty account name or empty amount is guaranteed to exist for
+ // each commodity
+ for (String balCurrency : balance.currencies()) {
+ int currEmptyRows = itemsWithEmptyAccountForCurrency.size(balCurrency);
+ int currRows = itemsForCurrency.size(balCurrency);
+ int currAccounts = itemsWithAccountForCurrency.size(balCurrency);
+ int currAmounts = itemsWithAmountForCurrency.size(balCurrency);
+ if ((currEmptyRows == 0) &&
+ ((currRows == currAccounts) || (currRows == currAmounts)))
+ {
+ // perhaps there already is an unused empty row for another currency that
+ // is not used?
+// boolean foundIt = false;
+// for (Item item : emptyRows) {
+// Currency itemCurrency = item.getCurrency();
+// String itemCurrencyName =
+// (itemCurrency == null) ? "" : itemCurrency.getName();
+// if (Misc.isZero(balance.get(itemCurrencyName))) {
+// item.setCurrency(Currency.loadByName(balCurrency));
+// item.setAmountHint(
+// String.format("%1.2f", -balance.get(balCurrency)));
+// foundIt = true;
+// break;
+// }
+// }
+//
+// if (!foundIt)
+ adapter.addRow(balCurrency);