+
+ for (int i = 1; i < list.size(); i++) {
+ TransactionAccount item = list.get(i)
+ .toTransactionAccount();
+
+ String accName = item.getAccountName()
+ .trim();
+ String currName = item.getCurrency();
+
+ itemsForCurrency.add(currName, item);
+
+ if (accName.isEmpty()) {
+ itemsWithEmptyAccountForCurrency.add(currName, item);
+
+ if (item.isAmountSet()) {
+ // 2) each amount has account name
+ Logger.debug("submittable", String.format(
+ "Transaction not submittable: row %d has no account name, but" +
+ " has" + " amount %1.2f", i + 1, item.getAmount()));
+ submittable = false;
+ }
+ else {
+ emptyRowsForCurrency.add(currName, item);
+ }
+ }
+ else {
+ accounts++;
+ itemsWithAccountForCurrency.add(currName, item);
+ }
+
+ if (!item.isAmountValid()) {
+ Logger.debug("submittable",
+ String.format("Not submittable: row %d has an invalid amount", i + 1));
+ submittable = false;
+ }
+ else if (item.isAmountSet()) {
+ itemsWithAmountForCurrency.add(currName, item);
+ balance.add(currName, item.getAmount());
+ }
+ else {
+ itemsWithEmptyAmountForCurrency.add(currName, item);
+
+ if (!accName.isEmpty())
+ itemsWithAccountAndEmptyAmountForCurrency.add(currName, item);
+ }
+ }
+
+ // 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(Locale.US, "Resetting hint of %d:'%s' [%s]",
+ i, 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()))
+ {
+ acc.setAmountHint(null);
+ 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() ||
+ !Misc.equalStrings(acc.getAmountHint(), hint))
+ {
+ Logger.debug("submittable",
+ String.format("Setting amount hint of {%s} to %s [%s]",
+ acc.toString(), hint, balCurrency));
+ acc.setAmountHint(hint);
+ 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())) {
+ acc.setAmountHint(null);
+ listChanged = true;
+ }
+ }
+ }
+ }