Skip to content

Commit

Permalink
Budget report must Honor fiscal year
Browse files Browse the repository at this point in the history
Performance improvement
  • Loading branch information
wolfsolver committed Dec 16, 2024
1 parent 93713d8 commit 9763696
Showing 1 changed file with 44 additions and 31 deletions.
75 changes: 44 additions & 31 deletions app/src/main/java/com/money/manager/ex/budget/BudgetAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteQueryBuilder;

import androidx.core.content.ContextCompat;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -49,6 +51,7 @@
import javax.inject.Inject;

import androidx.cursoradapter.widget.SimpleCursorAdapter;

import dagger.Lazy;
import info.javaperformance.money.MoneyFactory;
import timber.log.Timber;
Expand All @@ -57,7 +60,19 @@
* Adapter for budgets.
*/
public class BudgetAdapter
extends SimpleCursorAdapter {
extends SimpleCursorAdapter {

@Inject
Lazy<BriteDatabase> databaseLazy;
private final int mLayout;
private String mBudgetName;
private long mBudgetYearId;
private HashMap<String, BudgetEntry> mBudgetEntries;

// budget financial year
private boolean useBudgetFinancialYear = false;
private MmxDate dateFrom;
private MmxDate dateTo;

/**
* Standard constructor.
Expand Down Expand Up @@ -94,14 +109,6 @@ public BudgetAdapter(Context context, Cursor cursor, String[] from, int[] to, in

}

@Inject Lazy<BriteDatabase> databaseLazy;
private boolean useBudgetFinancialYear = false;
private final int mLayout;
private String mBudgetName;
private long mBudgetYearId;
private HashMap<String, BudgetEntry> mBudgetEntries;


@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
Expand All @@ -118,7 +125,7 @@ public void bindView(View view, Context context, Cursor cursor) {

TextView categoryTextView = view.findViewById(R.id.categoryTextView);
if (categoryTextView != null) {
int categoryColumnIndex = cursor.getColumnIndex( QueryNestedCategory.CATEGNAME );
int categoryColumnIndex = cursor.getColumnIndex(QueryNestedCategory.CATEGNAME);
categoryTextView.setText(cursor.getString(categoryColumnIndex));
}

Expand All @@ -128,7 +135,6 @@ public void bindView(View view, Context context, Cursor cursor) {
subCategoryId = -1;

// Frequency

BudgetPeriodEnum periodEnum = getBudgetPeriodFor(categoryId, subCategoryId);

TextView frequencyTextView = view.findViewById(R.id.frequencyTextView);
Expand All @@ -139,7 +145,6 @@ public void bindView(View view, Context context, Cursor cursor) {
CurrencyService currencyService = new CurrencyService(mContext);

// Amount

TextView amountTextView = view.findViewById(R.id.amountTextView);
double amount = getBudgetAmountFor(categoryId, subCategoryId);
if (amountTextView != null) {
Expand All @@ -150,8 +155,7 @@ public void bindView(View view, Context context, Cursor cursor) {
// Estimated
double estimated = isMonthlyBudget(mBudgetName)
? BudgetPeriods.getMonthlyEstimate(periodEnum, amount)
: BudgetPeriods.getYearlyEstimate(periodEnum, amount)
;
: BudgetPeriods.getYearlyEstimate(periodEnum, amount);

// Actual
TextView actualTextView = view.findViewById(R.id.actualTextView);
Expand All @@ -164,17 +168,16 @@ public void bindView(View view, Context context, Cursor cursor) {
UIHelper uiHelper = new UIHelper(context);
if ((int) (actual * 100) < (int) (estimated * 100)) {
actualTextView.setTextColor(
ContextCompat.getColor(context, uiHelper.resolveAttribute(R.attr.holo_red_color_theme))
ContextCompat.getColor(context, uiHelper.resolveAttribute(R.attr.holo_red_color_theme))
);
} else {
actualTextView.setTextColor(
ContextCompat.getColor(context, uiHelper.resolveAttribute(R.attr.holo_green_color_theme))
ContextCompat.getColor(context, uiHelper.resolveAttribute(R.attr.holo_green_color_theme))
);
}
}

// Amount Available

TextView amountAvailableTextView = view.findViewById(R.id.amountAvailableTextView);
if (amountAvailableTextView != null) {
double amountAvailable = -(estimated - actual);
Expand All @@ -183,14 +186,14 @@ public void bindView(View view, Context context, Cursor cursor) {

// colour the amount depending on whether it is above/below the budgeted amount to 2 decimal places
UIHelper uiHelper = new UIHelper(context);
long amountAvailablelong = (long)amountAvailable * 100;
long amountAvailablelong = (long) amountAvailable * 100;
if (amountAvailablelong < 0) {
amountAvailableTextView.setTextColor(
ContextCompat.getColor(context, uiHelper.resolveAttribute(R.attr.holo_red_color_theme))
ContextCompat.getColor(context, uiHelper.resolveAttribute(R.attr.holo_red_color_theme))
);
} else if (amountAvailablelong > 0) {
amountAvailableTextView.setTextColor(
ContextCompat.getColor(context, uiHelper.resolveAttribute(R.attr.holo_green_color_theme))
ContextCompat.getColor(context, uiHelper.resolveAttribute(R.attr.holo_green_color_theme))
);
}
}
Expand All @@ -202,10 +205,16 @@ public Context getContext() {

public void setBudgetName(String budgetName) {
mBudgetName = budgetName;
if (useBudgetFinancialYear) {
dateFrom = getStartDateForFinancialYear(mBudgetName);
dateTo = new MmxDate(dateFrom.toDate());
dateTo.addYear(1).minusDays(1);
}
}

/**
* As a side effect of the setter the budget entry thread cache is populated.
*
* @param budgetYearId
*/
public void setBudgetYearId(long budgetYearId) {
Expand All @@ -229,6 +238,7 @@ private double getActualAmount(boolean hasSubcategory, Cursor cursor) {

/**
* Returns the budgeted amount for the category and subcategory, or zero, if there is none.
*
* @param categoryId
* @param subCategoryId
* @return
Expand All @@ -242,6 +252,7 @@ private double getBudgetAmountFor(long categoryId, long subCategoryId) {

/**
* Returns the period of the budgeted amount or NONE if there isn't any.
*
* @param categoryId
* @param subCategoryId
* @return
Expand All @@ -256,6 +267,7 @@ private BudgetPeriodEnum getBudgetPeriodFor(long categoryId, long subCategoryId)
/**
* Builds a thread cache from the database for every category and subcategory present in
* this budget.
*
* @return
*/
private HashMap<String, BudgetEntry> populateThreadCache() {
Expand All @@ -276,18 +288,19 @@ private double getAmountForSubCategory(long subCategoryId) {
private double loadTotalFor(String where) {
double total = 0;

//use financia year
if (!useBudgetFinancialYear) {
long year = getYearFromBudgetName(mBudgetName);
// if month use month budget
// if year check if financianl or calendar
long year = getYearFromBudgetName(mBudgetName);
long month = getMonthFromBudgetName(mBudgetName);
if (month != Constants.NOT_SET) {
// month
where += " AND " + QueryMobileData.Month + "=" + month;
} else
if (!useBudgetFinancialYear || dateFrom == null || dateTo == null ) {
// annual
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);
// financial
where += " AND " + QueryMobileData.Date + " BETWEEN '" + dateFrom.toIsoDateString() + "' AND '" + dateTo.toIsoDateString() + "'";
}

Expand Down Expand Up @@ -358,7 +371,7 @@ private MmxDate getStartDateForFinancialYear(String budgetName) {
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.setYear((int) getYearFromBudgetName(budgetName));
newDate.setDate(financialYearStartDay);
newDate.setMonth(financialYearStartMonth);
} catch (Exception e) {
Expand Down

0 comments on commit 9763696

Please sign in to comment.