Search in sources :

Example 66 with Account

use of jgnash.engine.Account in project jgnash by ccavanaugh.

the class AdjustmentSlipController method convertAction.

@FXML
private void convertAction() {
    final Optional<Account> accountOptional = StaticAccountsMethods.selectAccount(null, account.get());
    accountOptional.ifPresent(opp -> {
        final Transaction t = new Transaction();
        t.setDate(datePicker.getValue());
        t.setNumber(numberComboBox.getValue());
        t.setPayee(payeeTextField.getText());
        final TransactionEntry entry = new TransactionEntry();
        entry.setMemo(memoTextField.getText());
        if (amountField.getDecimal().signum() >= 0) {
            entry.setCreditAccount(account.get());
            entry.setDebitAccount(opp);
        } else {
            entry.setDebitAccount(account.get());
            entry.setCreditAccount(opp);
        }
        entry.setCreditAmount(amountField.getDecimal().abs());
        entry.setDebitAmount(amountField.getDecimal().abs().negate());
        t.addTransactionEntry(entry);
        ReconcileManager.reconcileTransaction(account.get(), t, getReconciledState());
        TransactionDialog.showAndWait(account.get(), t, transaction -> {
            if (transaction != null) {
                final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
                Objects.requireNonNull(engine);
                if (engine.removeTransaction(modTrans)) {
                    engine.addTransaction(transaction);
                }
                clearForm();
            }
        });
    });
}
Also used : Account(jgnash.engine.Account) Transaction(jgnash.engine.Transaction) TransactionEntry(jgnash.engine.TransactionEntry) Engine(jgnash.engine.Engine) FXML(javafx.fxml.FXML)

Example 67 with Account

use of jgnash.engine.Account in project jgnash by ccavanaugh.

the class AbstractRegisterPanel method messagePosted.

@Override
public void messagePosted(final Message event) {
    final Account account = getAccount();
    // must update on EDT or a deadlock can occur
    EventQueue.invokeLater(() -> {
        Account a = event.getObject(MessageProperty.ACCOUNT);
        if (account.equals(a)) {
            switch(event.getEvent()) {
                case ACCOUNT_MODIFY:
                    updateAccountState();
                    updateAccountInfo();
                    break;
                case TRANSACTION_ADD:
                    final Transaction t = event.getObject(MessageProperty.TRANSACTION);
                    final int index = account.indexOf(t);
                    if (index == account.getTransactionCount() - 1) {
                        autoScroll();
                    }
                    setSelectedTransaction(t);
                    updateAccountInfo();
                    break;
                case TRANSACTION_REMOVE:
                    updateAccountInfo();
                    break;
                default:
                    break;
            }
        }
        if (event.getEvent() == ChannelEvent.SECURITY_HISTORY_ADD || event.getEvent() == ChannelEvent.SECURITY_HISTORY_REMOVE) {
            SecurityNode node = event.getObject(MessageProperty.COMMODITY);
            if (account.containsSecurity(node)) {
                updateAccountInfo();
            }
        }
    });
}
Also used : Account(jgnash.engine.Account) Transaction(jgnash.engine.Transaction) InvestmentTransaction(jgnash.engine.InvestmentTransaction) SecurityNode(jgnash.engine.SecurityNode)

Example 68 with Account

use of jgnash.engine.Account in project jgnash by ccavanaugh.

the class BudgetFactory method buildAverageBudget.

public static Budget buildAverageBudget(final Period budgetPeriod, final String name, final boolean round) {
    final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
    Objects.requireNonNull(engine);
    Budget budget = new Budget();
    budget.setName(name);
    budget.setBudgetPeriod(budgetPeriod);
    int year = LocalDate.now().getYear() - 1;
    List<BudgetPeriodDescriptor> descriptors = BudgetPeriodDescriptorFactory.getDescriptors(year, budgetPeriod);
    List<Account> accounts = new ArrayList<>();
    accounts.addAll(engine.getIncomeAccountList());
    accounts.addAll(engine.getExpenseAccountList());
    for (Account account : accounts) {
        budget.setBudgetGoal(account, buildAverageBudgetGoal(account, descriptors, round));
    }
    return budget;
}
Also used : Account(jgnash.engine.Account) ArrayList(java.util.ArrayList) Engine(jgnash.engine.Engine)

Example 69 with Account

use of jgnash.engine.Account in project jgnash by ccavanaugh.

the class BudgetResultsModel method buildAccountResults.

private BudgetPeriodResults buildAccountResults(final BudgetPeriodDescriptor descriptor, final Account account, final boolean includeBaseAccountResults) {
    final BudgetPeriodResults results = new BudgetPeriodResults();
    accountLock.readLock().lock();
    try {
        // calculate this account's results
        if (accounts.contains(account)) {
            final BudgetGoal goal = budget.getBudgetGoal(account);
            results.setBudgeted(goal.getGoal(descriptor.getStartPeriod(), descriptor.getEndPeriod()));
            // calculate the change and remaining amount for the budget
            if (account.getAccountType() == AccountType.INCOME) {
                results.setChange(account.getBalance(descriptor.getStartDate(), descriptor.getEndDate()).negate());
                results.setRemaining(results.getChange().subtract(results.getBudgeted()));
            } else {
                results.setChange(account.getBalance(descriptor.getStartDate(), descriptor.getEndDate()));
                results.setRemaining(results.getBudgeted().subtract(results.getChange()));
            }
            final int index = descriptorList.indexOf(descriptor);
            // per account running total
            if (useRunningTotals && index > 0 && includeBaseAccountResults) {
                final BudgetPeriodResults priorResults = getResults(descriptorList.get(index - 1), account);
                results.setBudgeted(results.getBudgeted().add(priorResults.getBudgeted()));
                results.setChange(results.getChange().add(priorResults.getChange()));
                results.setRemaining(results.getRemaining().add(priorResults.getRemaining()));
            }
        }
        // recursive decent to add child account results and handle exchange rates
        for (final Account child : account.getChildren(Comparators.getAccountByCode())) {
            final BudgetPeriodResults childResults = buildAccountResults(descriptor, child, false);
            final BigDecimal exchangeRate = child.getCurrencyNode().getExchangeRate(account.getCurrencyNode());
            // reverse sign if the parent account is an income account but the child is not, or vice versa
            final BigDecimal sign = ((account.getAccountType() == AccountType.INCOME) != (child.getAccountType() == AccountType.INCOME)) ? BigDecimal.ONE.negate() : BigDecimal.ONE;
            results.setChange(results.getChange().add(childResults.getChange().multiply(exchangeRate).multiply(sign)));
            results.setBudgeted(results.getBudgeted().add(childResults.getBudgeted().multiply(exchangeRate).multiply(sign)));
            results.setRemaining(results.getRemaining().add(childResults.getRemaining().multiply(exchangeRate)));
        }
    } finally {
        accountLock.readLock().unlock();
    }
    // rescale the results
    results.setChange(results.getChange().setScale(account.getCurrencyNode().getScale(), MathConstants.roundingMode));
    results.setBudgeted(results.getBudgeted().setScale(account.getCurrencyNode().getScale(), MathConstants.roundingMode));
    results.setRemaining(results.getRemaining().setScale(account.getCurrencyNode().getScale(), MathConstants.roundingMode));
    return results;
}
Also used : RootAccount(jgnash.engine.RootAccount) Account(jgnash.engine.Account) BigDecimal(java.math.BigDecimal)

Example 70 with Account

use of jgnash.engine.Account in project jgnash by ccavanaugh.

the class BudgetResultsModel method buildResults.

private BudgetPeriodResults buildResults(final BudgetPeriodDescriptor descriptor, final AccountGroup group) {
    BigDecimal remainingTotal = BigDecimal.ZERO;
    BigDecimal totalChange = BigDecimal.ZERO;
    BigDecimal totalBudgeted = BigDecimal.ZERO;
    accountLock.readLock().lock();
    try {
        for (final Account account : getAccounts(group)) {
            // top level account if the parent is not included in the budget model
            if (!accounts.contains(account.getParent())) {
                final BudgetPeriodResults periodResults = getResults(descriptor, account);
                final BigDecimal remaining = periodResults.getRemaining();
                remainingTotal = remainingTotal.add(remaining.multiply(baseCurrency.getExchangeRate(account.getCurrencyNode())));
                final BigDecimal change = periodResults.getChange();
                totalChange = totalChange.add(change.multiply(baseCurrency.getExchangeRate(account.getCurrencyNode())));
                final BigDecimal budgeted = periodResults.getBudgeted();
                totalBudgeted = totalBudgeted.add(budgeted.multiply(baseCurrency.getExchangeRate(account.getCurrencyNode())));
            }
        }
    } finally {
        accountLock.readLock().unlock();
    }
    final BudgetPeriodResults results = new BudgetPeriodResults();
    if (useRunningTotals) {
        final int index = descriptorList.indexOf(descriptor);
        if (index > 0) {
            final BudgetPeriodResults priorResults = getResults(descriptorList.get(index - 1), group);
            results.setBudgeted(results.getBudgeted().add(priorResults.getBudgeted()));
            results.setChange(results.getChange().add(priorResults.getChange()));
            results.setRemaining(results.getRemaining().add(priorResults.getRemaining()));
        }
    }
    // rescale the results
    results.setBudgeted(totalBudgeted.setScale(baseCurrency.getScale(), MathConstants.roundingMode));
    results.setRemaining(remainingTotal.setScale(baseCurrency.getScale(), MathConstants.roundingMode));
    results.setChange(totalChange.setScale(baseCurrency.getScale(), MathConstants.roundingMode));
    return results;
}
Also used : RootAccount(jgnash.engine.RootAccount) Account(jgnash.engine.Account) BigDecimal(java.math.BigDecimal)

Aggregations

Account (jgnash.engine.Account)132 Engine (jgnash.engine.Engine)44 BigDecimal (java.math.BigDecimal)40 CurrencyNode (jgnash.engine.CurrencyNode)28 Transaction (jgnash.engine.Transaction)27 ArrayList (java.util.ArrayList)22 LocalDate (java.time.LocalDate)21 ResourceBundle (java.util.ResourceBundle)19 RootAccount (jgnash.engine.RootAccount)18 List (java.util.List)15 AccountType (jgnash.engine.AccountType)15 NumberFormat (java.text.NumberFormat)14 FXML (javafx.fxml.FXML)13 EngineFactory (jgnash.engine.EngineFactory)13 Objects (java.util.Objects)12 Collections (java.util.Collections)11 InvestmentTransaction (jgnash.engine.InvestmentTransaction)11 Tooltip (javafx.scene.control.Tooltip)10 Preferences (java.util.prefs.Preferences)9 Platform (javafx.application.Platform)9