- // rules:
- // 1) at least two account names
- // 2) each amount must have account name
- // 3) amounts must balance to 0, or
- // 3a) there must be exactly one empty amount
- // 4) empty accounts with empty amounts are ignored
- // 5) a row with an empty account name or empty amount is guaranteed to exist
- public void checkTransactionSubmittable(NewTransactionItemsAdapter adapter) {
- int accounts = 0;
- int accounts_with_values = 0;
- int amounts = 0;
- int amounts_with_accounts = 0;
- int empty_rows = 0;
- Item empty_amount = null;
- boolean single_empty_amount = false;
- boolean single_empty_amount_has_account = false;
- float running_total = 0f;
- final String descriptionText = getDescription();
- final boolean have_description = ((descriptionText != null) && !descriptionText.isEmpty());
-
- try {
- 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();
- if (!acc_name.isEmpty()) {
- accounts++;
-
- if (acc.isAmountSet()) {
- accounts_with_values++;
- }
- }
- else empty_rows++;
-
- if (!acc.isAmountSet()) {
- if (empty_amount == null) {
- empty_amount = item;
- single_empty_amount = true;
- single_empty_amount_has_account = !acc_name.isEmpty();
- }
- else if (!acc_name.isEmpty()) single_empty_amount = false;
- }
- else {
- amounts++;
- if (!acc_name.isEmpty()) amounts_with_accounts++;
- running_total += acc.getAmount();
- }
- }
-
- if ((empty_rows == 0) &&
- ((this.items.size() == accounts) || (this.items.size() == amounts)))
- {
- adapter.addRow();
- }
-
- if (single_empty_amount) {
- empty_amount.setAmountHint(String.format(Locale.US, "%1.2f",
- Misc.isZero(running_total) ? 0f : -running_total));
- }
-
- debug("submittable", String.format(Locale.US,
- "%s, accounts=%d, accounts_with_values=%s, " +
- "amounts_with_accounts=%d, amounts=%d, running_total=%1.2f, " +
- "single_empty_with_acc=%s", have_description ? "description" : "NO description",
- accounts, accounts_with_values, amounts_with_accounts, amounts, running_total,
- (single_empty_amount && single_empty_amount_has_account) ? "true" : "false"));
-
- if (have_description && (accounts >= 2) && (accounts_with_values >= (accounts - 1)) &&
- (amounts_with_accounts == amounts) &&
- (single_empty_amount && single_empty_amount_has_account || isZero(running_total)))
- {
- debug("submittable", "YES");
- isSubmittable.setValue(true);
- }
- else {
- debug("submittable", "NO");
- isSubmittable.setValue(false);
- }
-
- }
- 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);