+
+ // 1) has at least two account names
+ if (accounts < 2) {
+ if (accounts == 0)
+ Logger.debug("submittable", "Transaction not submittable: no account names");
+ else if (accounts == 1)
+ Logger.debug("submittable",
+ "Transaction not submittable: only one account name");
+ else
+ Logger.debug("submittable",
+ String.format("Transaction not submittable: only %d account names",
+ accounts));
+ submittable = false;
+ }
+
+ // 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 (int i = 1; i < list.size(); i++) {
+ TransactionAccount acc = list.get(i)
+ .toTransactionAccount();
+ if (Misc.equalStrings(acc.getCurrency(), balCurrency)) {
+ if (BuildConfig.DEBUG)
+ Logger.debug("submittable",
+ String.format("Resetting hint of '%s' [%s]",
+ Misc.nullIsEmpty(acc.getAccountName()),
+ balCurrency));
+ // skip if the amount is set, in which case the hint is not
+ // important/visible
+ if (!acc.isAmountSet() && acc.amountHintIsSet &&
+ !TextUtils.isEmpty(acc.getAmountHint()))
+ {
+ if (workingWithLiveList && !liveListCopied) {
+ list = copyList(list);
+ liveListCopied = true;
+ }
+ final TransactionAccount newAcc = new TransactionAccount(acc);
+ newAcc.setAmountHint(null);
+ list.set(i, newAcc);
+ listChanged = true;
+ }
+ }
+ }
+ }
+ else {
+ List<Item> tmpList =
+ itemsWithAccountAndEmptyAmountForCurrency.getList(balCurrency);
+ int balanceReceiversCount = tmpList.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 (!tmpList.isEmpty())
+ receiver = tmpList.get(0);
+ else if (!emptyAmountList.isEmpty())
+ receiver = emptyAmountList.get(0);
+
+ for (int i = 0; i < list.size(); i++) {
+ Item item = list.get(i);
+ if (!(item instanceof TransactionAccount))
+ continue;
+
+ TransactionAccount acc = item.toTransactionAccount();
+ if (!Misc.equalStrings(acc.getCurrency(), balCurrency))
+ continue;
+
+ if (item == receiver) {
+ final String hint = String.format("%1.2f", -currencyBalance);
+ if (!acc.isAmountHintSet() ||
+ !TextUtils.equals(acc.getAmountHint(), hint))
+ {
+ Logger.debug("submittable",
+ String.format("Setting amount hint of {%s} to %s [%s]",
+ acc.toString(), hint, balCurrency));
+ if (workingWithLiveList & !liveListCopied) {
+ list = copyList(list);
+ liveListCopied = true;
+ }
+ final TransactionAccount newAcc = new TransactionAccount(acc);
+ newAcc.setAmountHint(hint);
+ list.set(i, newAcc);
+ listChanged = true;
+ }
+ }
+ else {
+ if (BuildConfig.DEBUG)
+ Logger.debug("submittable",
+ String.format("Resetting hint of '%s' [%s]",
+ Misc.nullIsEmpty(acc.getAccountName()),
+ balCurrency));
+ if (acc.amountHintIsSet && !TextUtils.isEmpty(acc.getAmountHint())) {
+ if (workingWithLiveList && !liveListCopied) {
+ list = copyList(list);
+ liveListCopied = true;
+ }
+ final TransactionAccount newAcc = new TransactionAccount(acc);
+ newAcc.setAmountHint(null);
+ list.set(i, newAcc);
+ listChanged = true;
+ }
+ }
+ }
+ }
+ }
+
+ // 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)
+ if (workingWithLiveList && !liveListCopied) {
+ list = copyList(list);
+ liveListCopied = true;
+ }
+ final TransactionAccount newAcc = new TransactionAccount("", balCurrency);
+ final float bal = balance.get(balCurrency);
+ if (!Misc.isZero(bal) && currAmounts == currRows)
+ newAcc.setAmountHint(String.format("%4.2f", -bal));
+ Logger.debug("submittable",
+ String.format("Adding new item with %s for currency %s",
+ newAcc.getAmountHint(), balCurrency));
+ list.add(newAcc);
+ listChanged = true;
+ }