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;
}
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");
}
}
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);
}
});
}
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);
}
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;
}
Aggregations