+ // 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);
+ }
+ }