Search in sources :

Example 46 with CurrencyNode

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

the class QifImport method generateAccount.

private Account generateAccount(final QifAccount acc) {
    Account account;
    CurrencyNode defaultCurrency = engine.getDefaultCurrency();
    switch(acc.type) {
        case "Bank":
            account = new Account(AccountType.BANK, defaultCurrency);
            break;
        case "CCard":
            account = new Account(AccountType.CREDIT, defaultCurrency);
            break;
        case "Cash":
            account = new Account(AccountType.CASH, defaultCurrency);
            break;
        case "Invst":
        case "Port":
            account = new Account(AccountType.INVEST, defaultCurrency);
            break;
        case "Oth A":
            account = new Account(AccountType.ASSET, defaultCurrency);
            break;
        case "Oth L":
            account = new Account(AccountType.LIABILITY, defaultCurrency);
            break;
        default:
            logger.log(Level.SEVERE, "Could not generate an account for:\n{0}", acc.toString());
            return null;
    }
    account.setName(acc.name);
    account.setDescription(acc.description);
    return account;
}
Also used : CurrencyNode(jgnash.engine.CurrencyNode) Account(jgnash.engine.Account)

Example 47 with CurrencyNode

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

the class TransactionTagPieChartDialogController method updateChart.

private void updateChart() {
    final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
    Objects.requireNonNull(engine);
    final Account a = accountComboBox.getValue();
    if (a != null) {
        final CurrencyNode defaultCurrency = a.getCurrencyNode();
        final NumberFormat numberFormat = NumericFormats.getFullCommodityFormat(defaultCurrency);
        final ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList();
        final Map<Tag, BigDecimal> balanceMap = new HashMap<>();
        // Iterate through all the Tags in use
        for (final Tag tag : engine.getTagsInUse()) {
            BigDecimal balance = new BigDecimal(BigInteger.ZERO);
            for (final Account child : a.getChildren()) {
                balance = balance.add(getSumForTag(tag, child));
            }
            if (balance.compareTo(BigDecimal.ZERO) != 0) {
                balanceMap.put(tag, balance);
            }
        }
        // Sum of all the balances
        final BigDecimal total = balanceMap.values().stream().reduce(BigDecimal.ZERO, BigDecimal::add);
        // Iterate and crate each pie slice
        for (final Map.Entry<Tag, BigDecimal> entry : balanceMap.entrySet()) {
            final Tag tag = entry.getKey();
            final BigDecimal balance = entry.getValue();
            final String label = tag.getName() + " - " + numberFormat.format(balance.doubleValue());
            // protect against a div by zero caused by net zero of income and expense
            double value = total.compareTo(BigDecimal.ZERO) == 0 ? 0 : balance.divide(total, MathContext.DECIMAL64).multiply(ONE_HUNDRED).doubleValue();
            final PieChart.Data data = new PieChart.Data(label, value);
            // nodes are created lazily.  Set the user data (Tag) after the node is created
            data.nodeProperty().addListener((observable, oldValue, newValue) -> newValue.setUserData(tag));
            pieChartData.add(data);
        }
        pieChart.setData(pieChartData);
        final NumberFormat percentFormat = NumberFormat.getPercentInstance();
        percentFormat.setMaximumFractionDigits(1);
        percentFormat.setMinimumFractionDigits(1);
        // Install tooltips on the data after it has been added to the chart
        pieChart.getData().forEach(data -> Tooltip.install(data.getNode(), new Tooltip((((Tag) data.getNode().getUserData()).getName() + " - " + percentFormat.format(data.getPieValue() / 100d)))));
        pieChart.setTitle(resources.getString("Title.TransactionTagPieChart"));
        pieChart.centerTitleProperty().set(accountComboBox.getValue().getName());
        pieChart.centerSubTitleProperty().set(numberFormat.format(total));
    } else {
        pieChart.setData(FXCollections.emptyObservableList());
        pieChart.setTitle("No Data");
    }
}
Also used : CurrencyNode(jgnash.engine.CurrencyNode) Account(jgnash.engine.Account) HashMap(java.util.HashMap) Tooltip(javafx.scene.control.Tooltip) BigDecimal(java.math.BigDecimal) PieChart(javafx.scene.chart.PieChart) Tag(jgnash.engine.Tag) HashMap(java.util.HashMap) Map(java.util.Map) Engine(jgnash.engine.Engine) NumberFormat(java.text.NumberFormat)

Example 48 with CurrencyNode

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

the class BudgetGoalsDialogController method initialize.

@FXML
private void initialize() {
    buttonBar.buttonOrderProperty().bind(Options.buttonOrderProperty());
    endRowSpinner.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(1, 1));
    startRowSpinner.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(1, 1));
    periodComboBox.getItems().addAll(Period.values());
    patternComboBox.getItems().addAll(Pattern.values());
    patternComboBox.setValue(Pattern.EveryRow);
    fillAllDecimalTextField.emptyWhenZeroProperty().set(false);
    fillPatternAmountDecimalTextField.emptyWhenZeroProperty().set(false);
    fillAllDecimalTextField.setDecimal(BigDecimal.ZERO);
    fillPatternAmountDecimalTextField.setDecimal(BigDecimal.ZERO);
    goalTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
    goalTable.setEditable(true);
    final TableColumn<BudgetPeriodDescriptor, String> periodColumn = new TableColumn<>(resources.getString("Column.Period"));
    periodColumn.setEditable(false);
    periodColumn.setCellValueFactory(param -> {
        if (param != null) {
            return new SimpleStringProperty(param.getValue().getPeriodDescription());
        }
        return new SimpleStringProperty("");
    });
    periodColumn.setSortable(false);
    goalTable.getColumns().add(periodColumn);
    final TableColumn<BudgetPeriodDescriptor, BigDecimal> amountColumn = new TableColumn<>(resources.getString("Column.Amount"));
    amountColumn.setEditable(true);
    amountColumn.setSortable(false);
    amountColumn.setCellValueFactory(param -> {
        if (param != null) {
            final BudgetPeriodDescriptor descriptor = param.getValue();
            final BigDecimal goal = budgetGoal.get().getGoal(descriptor.getStartPeriod(), descriptor.getEndPeriod(), descriptor.getStartDate().isLeapYear());
            return new SimpleObjectProperty<>(goal.setScale(accountProperty().get().getCurrencyNode().getScale(), MathConstants.roundingMode));
        }
        return new SimpleObjectProperty<>(BigDecimal.ZERO);
    });
    amountColumn.setCellFactory(cell -> new BigDecimalTableCell<>(numberFormat));
    amountColumn.setOnEditCommit(event -> {
        final BudgetPeriodDescriptor descriptor = event.getTableView().getItems().get(event.getTablePosition().getRow());
        budgetGoalProperty().get().setGoal(descriptor.getStartPeriod(), descriptor.getEndPeriod(), event.getNewValue(), descriptor.getStartDate().isLeapYear());
    });
    goalTable.getColumns().add(amountColumn);
    periodComboBox.valueProperty().addListener((observable, oldValue, newValue) -> {
        if (newValue != null) {
            budgetGoalProperty().get().setBudgetPeriod(newValue);
            final List<BudgetPeriodDescriptor> descriptors = getDescriptors();
            goalTable.getItems().setAll(descriptors);
            descriptorSize.set(descriptors.size());
        }
    });
    budgetGoalProperty().addListener((observable, oldValue, newValue) -> {
        if (newValue != null) {
            periodComboBox.setValue(newValue.getBudgetPeriod());
        }
    });
    // the spinner factory max values do not like being bound; Set value instead
    descriptorSize.addListener((observable, oldValue, newValue) -> {
        ((SpinnerValueFactory.IntegerSpinnerValueFactory) endRowSpinner.getValueFactory()).setMax(newValue.intValue());
        ((SpinnerValueFactory.IntegerSpinnerValueFactory) startRowSpinner.getValueFactory()).setMax(newValue.intValue());
        endRowSpinner.getValueFactory().setValue(newValue.intValue());
    });
    // account has changed; update currency related properties
    accountProperty().addListener((observable, oldValue, newValue) -> {
        if (newValue != null) {
            final CurrencyNode currencyNode = newValue.getCurrencyNode();
            currencyLabel.setText(currencyNode.getSymbol());
            fillAllDecimalTextField.scaleProperty().set(currencyNode.getScale());
            fillAllDecimalTextField.minScaleProperty().set(currencyNode.getScale());
            fillPatternAmountDecimalTextField.scaleProperty().set(currencyNode.getScale());
            fillPatternAmountDecimalTextField.minScaleProperty().set(currencyNode.getScale());
            final NumberFormat decimalFormat = NumberFormat.getInstance();
            if (decimalFormat instanceof DecimalFormat) {
                decimalFormat.setMinimumFractionDigits(currencyNode.getScale());
                decimalFormat.setMaximumFractionDigits(currencyNode.getScale());
            }
            numberFormat.set(decimalFormat);
        }
    });
}
Also used : CurrencyNode(jgnash.engine.CurrencyNode) DecimalFormat(java.text.DecimalFormat) SimpleStringProperty(javafx.beans.property.SimpleStringProperty) TableColumn(javafx.scene.control.TableColumn) BigDecimal(java.math.BigDecimal) SpinnerValueFactory(javafx.scene.control.SpinnerValueFactory) SimpleObjectProperty(javafx.beans.property.SimpleObjectProperty) BudgetPeriodDescriptor(jgnash.engine.budget.BudgetPeriodDescriptor) NumberFormat(java.text.NumberFormat) FXML(javafx.fxml.FXML)

Example 49 with CurrencyNode

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

the class BudgetPropertiesDialogController method initialize.

@FXML
private void initialize() {
    periodComboBox.getItems().setAll(Period.values());
    startMonthComboBox.getItems().setAll(MonthName.values());
    Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
    Objects.requireNonNull(engine);
    // max scale is a function of available currencies
    int maxScale = 0;
    for (final CurrencyNode currencyNode : engine.getCurrencies()) {
        maxScale = Math.max(maxScale, currencyNode.getScale());
    }
    roundingMethodComboBox.setCellFactory(param -> new RoundModeListCell());
    scaleSpinner.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(-10, maxScale, maxScale, 1));
    roundingMethodComboBox.getItems().setAll(RoundMode.values());
    roundingMethodComboBox.setValue(RoundMode.FLOOR);
    startMonthComboBox.setValue(MonthName.JANUARY);
}
Also used : CurrencyNode(jgnash.engine.CurrencyNode) Engine(jgnash.engine.Engine) SpinnerValueFactory(javafx.scene.control.SpinnerValueFactory) FXML(javafx.fxml.FXML)

Example 50 with CurrencyNode

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

the class IncomeExpensePayeePieChartDialogController method createPieDataSet.

private ObservableList<PieChart.Data>[] createPieDataSet(@NotNull final Account account) {
    @SuppressWarnings("unchecked") final ObservableList<PieChart.Data>[] chartData = new ObservableList[2];
    chartData[CREDIT] = FXCollections.observableArrayList();
    chartData[DEBIT] = FXCollections.observableArrayList();
    final Map<String, BigDecimal> names = new HashMap<>();
    final List<TranTuple> list = getTransactions(account, new ArrayList<>(), startDatePicker.getValue(), endDatePicker.getValue());
    final CurrencyNode currency = account.getCurrencyNode();
    // Create a list of predicates
    final List<Predicate<Transaction>> predicates = getPayeeTextFields().stream().filter(textField -> !textField.getText().isEmpty()).map(textField -> new PayeePredicate(textField.getText(), Options.regexForFiltersProperty().get())).collect(Collectors.toList());
    // Iterate through the list and add up filtered payees
    for (final TranTuple tranTuple : list) {
        final String payee = tranTuple.transaction.getPayee();
        BigDecimal sum = tranTuple.transaction.getAmount(tranTuple.account);
        sum = sum.multiply(tranTuple.account.getCurrencyNode().getExchangeRate(currency));
        boolean keep = false;
        if (predicates.isEmpty()) {
            keep = true;
        } else {
            for (final Predicate<Transaction> predicate : predicates) {
                if (predicate.test(tranTuple.transaction)) {
                    keep = true;
                    break;
                }
            }
        }
        if (keep) {
            if (names.containsKey(payee)) {
                sum = sum.add(names.get(payee));
            }
            names.put(payee, sum);
        }
    }
    final Map<String, BigDecimal> sortedNames = CollectionUtils.sortMapByValue(names);
    for (final Map.Entry<String, BigDecimal> entry : sortedNames.entrySet()) {
        final PieChart.Data data = new PieChart.Data(truncateString(entry.getKey()), entry.getValue().abs().doubleValue());
        // nodes are created lazily.  Set the user data (Account) after the node is created
        data.nodeProperty().addListener((observable, oldValue, newValue) -> newValue.setUserData(entry.getKey()));
        if (entry.getValue().signum() == -1) {
            chartData[DEBIT].add(data);
        } else {
            chartData[CREDIT].add(data);
        }
    }
    return chartData;
}
Also used : CurrencyNode(jgnash.engine.CurrencyNode) DoughnutChart(jgnash.uifx.control.DoughnutChart) Scene(javafx.scene.Scene) Engine(jgnash.engine.Engine) TextInputControl(javafx.scene.control.TextInputControl) Transaction(jgnash.engine.Transaction) EngineFactory(jgnash.engine.EngineFactory) FXCollections(javafx.collections.FXCollections) HashMap(java.util.HashMap) VBox(javafx.scene.layout.VBox) Side(javafx.geometry.Side) AccountComboBox(jgnash.uifx.control.AccountComboBox) NumberFormat(java.text.NumberFormat) ArrayList(java.util.ArrayList) EncodeDecode(jgnash.util.EncodeDecode) BigDecimal(java.math.BigDecimal) ResourceBundle(java.util.ResourceBundle) Map(java.util.Map) Tooltip(javafx.scene.control.Tooltip) GridPane(javafx.scene.layout.GridPane) CurrencyNode(jgnash.engine.CurrencyNode) ObjectProperty(javafx.beans.property.ObjectProperty) TextField(javafx.scene.control.TextField) InjectFXML(jgnash.uifx.util.InjectFXML) TitledPane(javafx.scene.control.TitledPane) NotNull(jgnash.util.NotNull) Comparators(jgnash.engine.Comparators) Predicate(java.util.function.Predicate) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) JavaFXUtils(jgnash.uifx.util.JavaFXUtils) Preferences(java.util.prefs.Preferences) Objects(java.util.Objects) FXML(javafx.fxml.FXML) PieChart(javafx.scene.chart.PieChart) List(java.util.List) Stage(javafx.stage.Stage) SimpleObjectProperty(javafx.beans.property.SimpleObjectProperty) LocalDate(java.time.LocalDate) Account(jgnash.engine.Account) ObservableList(javafx.collections.ObservableList) PayeePredicate(jgnash.util.function.PayeePredicate) ChangeListener(javafx.beans.value.ChangeListener) Collections(java.util.Collections) NumericFormats(jgnash.text.NumericFormats) DatePickerEx(jgnash.uifx.control.DatePickerEx) CollectionUtils(jgnash.util.CollectionUtils) Options(jgnash.uifx.Options) HashMap(java.util.HashMap) BigDecimal(java.math.BigDecimal) Predicate(java.util.function.Predicate) PayeePredicate(jgnash.util.function.PayeePredicate) PieChart(javafx.scene.chart.PieChart) Transaction(jgnash.engine.Transaction) ObservableList(javafx.collections.ObservableList) PayeePredicate(jgnash.util.function.PayeePredicate) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

CurrencyNode (jgnash.engine.CurrencyNode)58 Account (jgnash.engine.Account)28 Engine (jgnash.engine.Engine)28 BigDecimal (java.math.BigDecimal)10 ArrayList (java.util.ArrayList)10 LocalDate (java.time.LocalDate)8 NumberFormat (java.text.NumberFormat)7 List (java.util.List)7 FXML (javafx.fxml.FXML)7 AccountGroup (jgnash.engine.AccountGroup)7 Transaction (jgnash.engine.Transaction)6 ResourceBundle (java.util.ResourceBundle)5 SecurityNode (jgnash.engine.SecurityNode)5 InjectFXML (jgnash.uifx.util.InjectFXML)5 Test (org.junit.jupiter.api.Test)5 IOException (java.io.IOException)4 JasperPrint (net.sf.jasperreports.engine.JasperPrint)4 HashMap (java.util.HashMap)3 Objects (java.util.Objects)3 ExecutionException (java.util.concurrent.ExecutionException)3