Search in sources :

Example 1 with BudgetGoal

use of jgnash.engine.budget.BudgetGoal in project jgnash by ccavanaugh.

the class Engine method updateBudgetGoals.

public void updateBudgetGoals(final Budget budget, final Account account, final BudgetGoal newGoals) {
    dataLock.writeLock().lock();
    try {
        BudgetGoal oldGoals = budget.getBudgetGoal(account);
        budget.setBudgetGoal(account, newGoals);
        // need to keep the old goal around, will be cleaned up later, orphan removal causes refresh issues
        moveObjectToTrash(oldGoals);
        updateBudgetGoals(budget, account);
    } finally {
        dataLock.writeLock().unlock();
    }
}
Also used : BudgetGoal(jgnash.engine.budget.BudgetGoal)

Example 2 with BudgetGoal

use of jgnash.engine.budget.BudgetGoal in project jgnash by ccavanaugh.

the class AccountRowHeaderPanel method showBudgetGoalDialog.

private void showBudgetGoalDialog(final Account account) {
    BudgetGoal oldGoal = budget.getBudgetGoal(account);
    BudgetGoalDialog d = new BudgetGoalDialog(account, oldGoal, budget.getWorkingYear());
    d.setVisible(true);
    if (d.getResult()) {
        BudgetGoal newGoal = d.getBudgetGoal();
        if (!newGoal.equals(oldGoal)) {
            final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
            Objects.requireNonNull(engine);
            engine.updateBudgetGoals(budget, account, newGoal);
        }
    }
}
Also used : BudgetGoal(jgnash.engine.budget.BudgetGoal) Engine(jgnash.engine.Engine)

Example 3 with BudgetGoal

use of jgnash.engine.budget.BudgetGoal in project jgnash by ccavanaugh.

the class EngineTest method testBudget.

@Test
public void testBudget() throws IOException {
    final String ACCOUNT_NAME = "testAccount";
    CurrencyNode node = e.getDefaultCurrency();
    Account a = new Account(AccountType.BANK, node);
    a.setName(ACCOUNT_NAME);
    e.addAccount(e.getRootAccount(), a);
    assertEquals(0, e.getBudgetList().size());
    Budget budget = new Budget();
    budget.setName("Default");
    budget.setDescription("Default Budget");
    BigDecimal[] budgetGoals = new BigDecimal[BudgetGoal.PERIODS];
    BudgetGoal goal = new BudgetGoal();
    // load the goals
    for (int i = 0; i < budgetGoals.length; i++) {
        budgetGoals[i] = new BigDecimal(i);
    }
    goal.setGoals(budgetGoals);
    goal.setBudgetPeriod(Period.WEEKLY);
    budget.setBudgetGoal(a, goal);
    budget.setBudgetPeriod(Period.WEEKLY);
    assertTrue(e.addBudget(budget));
    assertEquals(1, e.getBudgetList().size());
    // close and reopen to force check for persistence
    closeEngine();
    e = EngineFactory.bootLocalEngine(testFile, EngineFactory.DEFAULT, EngineFactory.EMPTY_PASSWORD);
    assertNotNull(e);
    assertEquals(1, e.getBudgetList().size());
    a = e.getAccountByName(ACCOUNT_NAME);
    assertNotNull(a);
    Budget recovered = e.getBudgetList().get(0);
    budgetGoals = recovered.getBudgetGoal(a).getGoals();
    // check the goals
    assertEquals(BudgetGoal.PERIODS, budgetGoals.length);
    // check the periods
    assertEquals(Period.WEEKLY, recovered.getBudgetPeriod());
    assertEquals(Period.WEEKLY, recovered.getBudgetGoal(a).getBudgetPeriod());
    for (int i = 0; i < budgetGoals.length; i++) {
        //assertEquals(new BigDecimal(i), budgetGoals[i]);
        assertThat(new BigDecimal(i), is(closeTo(budgetGoals[i], new BigDecimal(0.0001))));
    }
    // remove a budget
    assertTrue(e.removeBudget(e.getBudgetList().get(0)));
    assertEquals(0, e.getBudgetList().size());
    // close and reopen to force check for persistence
    closeEngine();
    e = EngineFactory.bootLocalEngine(testFile, EngineFactory.DEFAULT, EngineFactory.EMPTY_PASSWORD);
    assertEquals(0, e.getBudgetList().size());
}
Also used : BudgetGoal(jgnash.engine.budget.BudgetGoal) Budget(jgnash.engine.budget.Budget) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 4 with BudgetGoal

use of jgnash.engine.budget.BudgetGoal in project jgnash by ccavanaugh.

the class BudgetTableController method handleEditAccountGoals.

private void handleEditAccountGoals(@NotNull final Account account) {
    Objects.requireNonNull(account);
    final FXMLUtils.Pair<BudgetGoalsDialogController> pair = FXMLUtils.load(BudgetGoalsDialogController.class.getResource("BudgetGoalsDialog.fxml"), resources.getString("Title.BudgetGoal") + " - " + account.getName());
    pair.getController().accountProperty().set(account);
    pair.getController().workingYearProperty().set(yearSpinner.getValue());
    try {
        final BudgetGoal oldGoal = (BudgetGoal) budgetProperty().get().getBudgetGoal(account).clone();
        pair.getController().budgetGoalProperty().set(oldGoal);
    } catch (final CloneNotSupportedException e) {
        Logger.getLogger(BudgetTableController.class.getName()).log(Level.SEVERE, e.getLocalizedMessage(), e);
    }
    StageUtils.addBoundsListener(pair.getStage(), BudgetGoalsDialogController.class);
    pair.getStage().showAndWait();
    final Optional<BudgetGoal> optional = pair.getController().getResult();
    optional.ifPresent(budgetGoal -> {
        final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
        Objects.requireNonNull(engine);
        engine.updateBudgetGoals(budget.get(), account, budgetGoal);
    });
}
Also used : FXMLUtils(jgnash.uifx.util.FXMLUtils) BudgetGoal(jgnash.engine.budget.BudgetGoal) Engine(jgnash.engine.Engine)

Aggregations

BudgetGoal (jgnash.engine.budget.BudgetGoal)4 Engine (jgnash.engine.Engine)2 BigDecimal (java.math.BigDecimal)1 Budget (jgnash.engine.budget.Budget)1 FXMLUtils (jgnash.uifx.util.FXMLUtils)1 Test (org.junit.Test)1