diff --git a/app/src/main/java/com/money/manager/ex/budget/BudgetAdapter.java b/app/src/main/java/com/money/manager/ex/budget/BudgetAdapter.java index 8ae5c7e27..84b2b1e32 100644 --- a/app/src/main/java/com/money/manager/ex/budget/BudgetAdapter.java +++ b/app/src/main/java/com/money/manager/ex/budget/BudgetAdapter.java @@ -31,15 +31,19 @@ import com.money.manager.ex.Constants; import com.money.manager.ex.MmexApplication; import com.money.manager.ex.R; +import com.money.manager.ex.core.InfoKeys; import com.money.manager.ex.core.UIHelper; import com.money.manager.ex.currency.CurrencyService; import com.money.manager.ex.database.QueryMobileData; import com.money.manager.ex.datalayer.BudgetEntryRepository; import com.money.manager.ex.domainmodel.BudgetEntry; import com.money.manager.ex.nestedcategory.QueryNestedCategory; +import com.money.manager.ex.servicelayer.InfoService; import com.money.manager.ex.settings.AppSettings; +import com.money.manager.ex.utils.MmxDate; import com.squareup.sqlbrite3.BriteDatabase; +import java.util.Date; import java.util.HashMap; import javax.inject.Inject; @@ -82,10 +86,16 @@ public BudgetAdapter(Context context, Cursor cursor, String[] from, int[] to, in MmexApplication.getApp().iocComponent.inject(this); + // get Budget financial + try { + useBudgetFinancialYear = (new AppSettings(getContext()).getBudgetSettings().getBudgetFinancialYear()); + } catch (Exception e) { + } + } @Inject Lazy databaseLazy; - + private boolean useBudgetFinancialYear = false; private final int mLayout; private String mBudgetName; private long mBudgetYearId; @@ -266,15 +276,23 @@ private double getAmountForSubCategory(long subCategoryId) { private double loadTotalFor(String where) { double total = 0; - long year = getYearFromBudgetName(mBudgetName); - where += " AND " + QueryMobileData.Year + "=" + year; - long month = getMonthFromBudgetName(mBudgetName); - if (month != Constants.NOT_SET) { - where += " AND " + QueryMobileData.Month + "=" + month; + //use financia year + if (!useBudgetFinancialYear) { + long year = getYearFromBudgetName(mBudgetName); + where += " AND " + QueryMobileData.Year + "=" + year; + long month = getMonthFromBudgetName(mBudgetName); + if (month != Constants.NOT_SET) { + where += " AND " + QueryMobileData.Month + "=" + month; + } + } else { + MmxDate dateFrom = getStartDateForFinancialYear(mBudgetName); + MmxDate dateTo = new MmxDate(dateFrom.toDate()); + dateTo.addYear(1).minusDays(1); + where += " AND " + QueryMobileData.Date + " BETWEEN '" + dateFrom.toIsoDateString() + "' AND '" + dateTo.toIsoDateString() + "'"; } try { - // wolfsolver todo adapt query for nested category + // wolfsolver adapt query for nested category String query = prepareQuery(where); Cursor cursor = databaseLazy.get().query(query); if (cursor == null) return 0; @@ -334,6 +352,20 @@ private long getYearFromBudgetName(String budgetName) { return year; } + private MmxDate getStartDateForFinancialYear(String budgetName) { + MmxDate newDate = MmxDate.newDate(); + try { + InfoService infoService = new InfoService(getContext()); + int financialYearStartDay = new Integer(infoService.getInfoValue(InfoKeys.FINANCIAL_YEAR_START_DAY, "1")); + int financialYearStartMonth = new Integer(infoService.getInfoValue(InfoKeys.FINANCIAL_YEAR_START_MONTH, "0")) - 1; + newDate.setYear((int)getYearFromBudgetName(budgetName)); + newDate.setDate(financialYearStartDay); + newDate.setMonth(financialYearStartMonth); + } catch (Exception e) { + } + return newDate; + } + private boolean isMonthlyBudget(String budgetName) { return budgetName.contains("-"); } diff --git a/app/src/main/java/com/money/manager/ex/settings/BudgetSettings.java b/app/src/main/java/com/money/manager/ex/settings/BudgetSettings.java index 17b27cad4..154be9379 100644 --- a/app/src/main/java/com/money/manager/ex/settings/BudgetSettings.java +++ b/app/src/main/java/com/money/manager/ex/settings/BudgetSettings.java @@ -46,4 +46,8 @@ public void setShowSimpleView(boolean value) { set(R.string.pref_budget_show_simple_view, value); } + public Boolean getBudgetFinancialYear( ) { + return get(R.string.pref_budget_financial_year, false); + } + } diff --git a/app/src/main/res/values/prefids.xml b/app/src/main/res/values/prefids.xml index c527990d6..43f08b117 100644 --- a/app/src/main/res/values/prefids.xml +++ b/app/src/main/res/values/prefids.xml @@ -96,6 +96,7 @@ pref_budgets pref_budget_show_simple_view pref_budget_load_current + pref_budget_financial_year dropbox2download diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 376bd665f..e7ce57845 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -790,6 +790,11 @@ Show simple budget view If checked budgets use the simplified view + + Use financial year in budget + If checked budgets use financial year instead of calendar year + + Shares Loan Loan Accounts diff --git a/app/src/main/res/xml/preferences_budget.xml b/app/src/main/res/xml/preferences_budget.xml index 647ba51d9..1a5d83fa2 100644 --- a/app/src/main/res/xml/preferences_budget.xml +++ b/app/src/main/res/xml/preferences_budget.xml @@ -24,4 +24,11 @@ android:summary="@string/summary_budget_show_simple_view" android:title="@string/title_budget_show_simple_view" /> + + \ No newline at end of file