- /*
- A transaction is submittable if:
- 0) has description
- 1) has at least two account names
- 2) each row with amount has account name
- 3) for each commodity:
- 3a) amounts must balance to 0, or
- 3b) there must be exactly one empty amount (with account)
- 4) empty accounts with empty amounts are ignored
- Side effects:
- 5) a row with an empty account name or empty amount is guaranteed to exist for each commodity
- 6) at least two rows need to be present in the ledger
-
- */
- @SuppressLint("DefaultLocale")
- void checkTransactionSubmittable(NewTransactionItemsAdapter adapter) {
- if (checkHoldCounter > 0)
- return;
-
- int accounts = 0;
- final BalanceForCurrency balance = new BalanceForCurrency();
- final String descriptionText = getDescription();
- boolean submittable = true;
- final ItemsForCurrency itemsForCurrency = new ItemsForCurrency();
- final ItemsForCurrency itemsWithEmptyAmountForCurrency = new ItemsForCurrency();
- final ItemsForCurrency itemsWithAccountAndEmptyAmountForCurrency = new ItemsForCurrency();
- final ItemsForCurrency itemsWithEmptyAccountForCurrency = new ItemsForCurrency();
- final ItemsForCurrency itemsWithAmountForCurrency = new ItemsForCurrency();
- final ItemsForCurrency itemsWithAccountForCurrency = new ItemsForCurrency();
- final ItemsForCurrency emptyRowsForCurrency = new ItemsForCurrency();
- final List<Item> emptyRows = new ArrayList<>();
-
- try {
- if ((descriptionText == null) || descriptionText.trim()
- .isEmpty())
- {
- Logger.debug("submittable", "Transaction not submittable: missing description");
- submittable = false;
- }
-
- for (int i = 0; i < this.items.size(); i++) {
- Item item = this.items.get(i);
-
- LedgerTransactionAccount acc = item.getAccount();
- String acc_name = acc.getAccountName()
- .trim();
- String currName = acc.getCurrency();
-
- itemsForCurrency.add(currName, item);
-
- if (acc_name.isEmpty()) {
- itemsWithEmptyAccountForCurrency.add(currName, item);
-
- if (acc.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, acc.getAmount()));
- submittable = false;
- }
- else {
- emptyRowsForCurrency.add(currName, item);
- }
- }
- else {
- accounts++;
- itemsWithAccountForCurrency.add(currName, item);
- }
-
- if (acc.isAmountSet()) {
- itemsWithAmountForCurrency.add(currName, item);
- balance.add(currName, acc.getAmount());
- }
- else {
- itemsWithEmptyAmountForCurrency.add(currName, item);
-
- if (!acc_name.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 (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);
- }
- }
-
- // drop extra empty rows, not needed
- for (String currName : emptyRowsForCurrency.currencies()) {
- List<Item> emptyItems = emptyRowsForCurrency.getList(currName);
- while ((this.items.size() > 2) && (emptyItems.size() > 1)) {
- Item item = emptyItems.get(1);
- emptyItems.remove(1);
- removeRow(item, adapter);
- }
-
- // unused currency, remove last item (which is also an empty one)
- if ((items.size() > 2) && (emptyItems.size() == 1)) {
- List<Item> currItems = itemsForCurrency.getList(currName);
-
- if (currItems.size() == 1) {
- Item item = emptyItems.get(0);
- removeRow(item, adapter);
- }
- }
- }
-
- // 6) at least two rows need to be present in the ledger
- while (this.items.size() < 2)
- adapter.addRow();
-
-
- debug("submittable", submittable ? "YES" : "NO");
- isSubmittable.setValue(submittable);
-
- if (BuildConfig.DEBUG) {
- debug("submittable", "== Dump of all items");
- for (int i = 0; i < items.size(); i++) {
- Item item = items.get(i);
- LedgerTransactionAccount acc = item.getAccount();
- debug("submittable", String.format("Item %2d: [%4.2f(%s) %s] %s ; %s", i,
- acc.isAmountSet() ? acc.getAmount() : 0,
- item.isAmountHintSet() ? item.getAmountHint() : "ΓΈ", acc.getCurrency(),
- acc.getAccountName(), acc.getComment()));
- }
- }
- }
- catch (NumberFormatException e) {
- debug("submittable", "NO (because of NumberFormatException)");
- isSubmittable.setValue(false);
- }
- catch (Exception e) {
- e.printStackTrace();
- debug("submittable", "NO (because of an Exception)");
- isSubmittable.setValue(false);
- }
- }
- private void removeRow(Item item, NewTransactionItemsAdapter adapter) {