Skip to content

Commit

Permalink
Merge branch '#876_balance_liability' into tw_develop
Browse files Browse the repository at this point in the history
# Conflicts:
#	app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java
#	app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java
#	app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java
  • Loading branch information
JeanGarf committed Mar 7, 2020
2 parents ee91abb + b31daad commit d698d71
Show file tree
Hide file tree
Showing 15 changed files with 294 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,18 @@ private Money calculateSplitBalance(List<String> accountUIDList,
+ " = 0";

if (startTimestamp != -1 && endTimestamp != -1) {

selection += " AND " + TransactionEntry.TABLE_NAME + "_" + TransactionEntry.COLUMN_TIMESTAMP + " BETWEEN ? AND ? ";
selectionArgs = new String[]{String.valueOf(startTimestamp),
String.valueOf(endTimestamp)};

} else if (startTimestamp == -1 && endTimestamp != -1) {

selection += " AND " + TransactionEntry.TABLE_NAME + "_" + TransactionEntry.COLUMN_TIMESTAMP + " <= ?";
selectionArgs = new String[]{String.valueOf(endTimestamp)};

} else if (startTimestamp != -1/* && endTimestamp == -1*/) {

selection += " AND " + TransactionEntry.TABLE_NAME + "_" + TransactionEntry.COLUMN_TIMESTAMP + " >= ?";
selectionArgs = new String[]{String.valueOf(startTimestamp)};
}
Expand Down
58 changes: 58 additions & 0 deletions app/src/main/java/org/gnucash/android/model/AccountType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package org.gnucash.android.model;

import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;

import org.gnucash.android.R;
import org.gnucash.android.app.GnuCashApplication;

/**
* The type of account
* This are the different types specified by the OFX format and
Expand Down Expand Up @@ -37,6 +43,48 @@ public enum AccountType {
//nothing to see here, move along
}

// TODO TW C 2020-03-06 : Enlever le static

/**
* Compute red/green color according to accountType and isCredit
*
* @param isCredit
* @param accountType
*
* @return
*/
@ColorInt
public static int getAmountColor(final boolean isCredit,
final AccountType accountType) {

AccountType tmpAccountType = ((accountType != null)
? accountType
: AccountType.ASSET);

@ColorRes final int colorRes;

// TODO TW C 2020-03-06 : Trouver un meilleur nom
final boolean specialAccountType = tmpAccountType.isEquityAccount() || tmpAccountType.isResultAccount();

if ((!specialAccountType && isCredit) || (specialAccountType && !isCredit)) {
// TODO TW C 2020-03-02 : commenter
// CREDIT

// RED
colorRes = R.color.debit_red;

} else {
// DEBIT

// GREEN
colorRes = R.color.credit_green;
}

return GnuCashApplication.getAppContext()
.getResources()
.getColor(colorRes);
}

public boolean hasDebitNormalBalance() {

return mNormalBalance == TransactionType.DEBIT;
Expand All @@ -51,6 +99,16 @@ public TransactionType getNormalBalanceType() {
return mNormalBalance;
}

public boolean isAssetAccount() {

return ASSET.equals(this) || BANK.equals(this) || CASH.equals(this);
}

public boolean isEquityAccount() {

return EQUITY.equals(this);
}

// TODO TW C 2020-03-03 : A renommer en anglais
public boolean isResultAccount() {

Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/org/gnucash/android/model/Money.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ public double asDouble(){
public String asString(){
return toPlainString();
}


// TODO TW C 2020-03-06 : A factoriser avec Split.getFormattedAmount
/**
* Returns a string representation of the Money object formatted according to
* the <code>locale</code> and includes the currency symbol.
Expand Down
90 changes: 58 additions & 32 deletions app/src/main/java/org/gnucash/android/model/Split.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,37 +301,6 @@ public Money getFormattedQuantity(){
return getFormattedAmount(mQuantity, mAccountUID, mSplitType);
}

/**
* Splits are saved as absolute values to the database, with no negative numbers.
* The type of movement the split causes to the balance of an account determines
* its sign, and that depends on the split type and the account type
* @param amount Money amount to format
* @param accountUID GUID of the account
* @param splitType Transaction type of the split
* @return -{@code amount} if the amount would reduce the balance of
* {@code account}, otherwise +{@code amount}
*/
private static Money getFormattedAmount(Money amount, String accountUID, TransactionType
splitType){
boolean isDebitAccount = AccountsDbAdapter.getInstance().getAccountType(accountUID).hasDebitNormalBalance();
Money absAmount = amount.abs();

boolean isDebitSplit = splitType == TransactionType.DEBIT;
if (isDebitAccount) {
if (isDebitSplit) {
return absAmount;
} else {
return absAmount.negate();
}
} else {
if (isDebitSplit) {
return absAmount.negate();
} else {
return absAmount;
}
}
}

/**
* Return the reconciled state of this split
* <p>
Expand All @@ -351,6 +320,54 @@ public char getReconcileState() {
return mReconcileState;
}

/**
* Splits are saved as absolute values to the database, with no negative numbers.
* The type of movement the split causes to the balance of an account determines
* its sign, and that depends on the split type and the account type
* @param amount Money amount to format
* @param accountUID GUID of the account
* @param splitType Transaction type of the split
* @return -{@code amount} if the amount would reduce the balance of
* {@code account}, otherwise +{@code amount}
*/
private static Money getFormattedAmount(Money amount,
String accountUID,
TransactionType splitType) {

// boolean isDebitAccount = AccountsDbAdapter.getInstance()
// .getAccountType(accountUID)
// .hasDebitNormalBalance();

Money absAmount = amount.abs();

boolean isDebitSplit = splitType == TransactionType.DEBIT;

// if (isDebitAccount) {
// if (isDebitSplit) {
// return absAmount;
// } else {
// return absAmount.negate();
// }
// } else {
// if (isDebitSplit) {
// return absAmount.negate();
// } else {
// return absAmount;
// }
// }

if (isDebitSplit) {
// It is a Debit Split

return absAmount;

} else {
// It is not a Debit Split

return absAmount.negate();
}
}

/**
* Check if this split is reconciled
* @return {@code true} if the split is reconciled, {@code false} otherwise
Expand Down Expand Up @@ -394,7 +411,16 @@ public void setReconcileDate(Timestamp reconcileDate) {

@Override
public String toString() {
return mSplitType.name() + " of " + mValue.toString() + " in account: " + mAccountUID;

return mSplitType.name()
+ " of "
+ mValue.toString()
+ " in account: "
+ mAccountUID
+ " ("
+ AccountsDbAdapter.getInstance()
.getAccountFullName(mAccountUID)
+ ")";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,10 @@ protected void displayReport() {
mChart.highlightValues(null);
mChart.invalidate();

TransactionsActivity.displayBalance(mTotalAssets, mAssetsBalance);
TransactionsActivity.displayBalance(mTotalLiabilities, mLiabilitiesBalance);
TransactionsActivity.displayBalance(mNetWorth, mAssetsBalance.subtract(mLiabilitiesBalance));
TransactionsActivity.displayBalance(mTotalAssets, mAssetsBalance, AccountType.ASSET);
TransactionsActivity.displayBalance(mTotalLiabilities, mLiabilitiesBalance, AccountType.LIABILITY);
// #8xx
TransactionsActivity.displayBalance(mNetWorth, mAssetsBalance.add(mLiabilitiesBalance), null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public ReportType getReportType() {
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

// TODO TW C 2020-03-06 : A mettre dans AccountType sous formes de constantes
mAssetAccountTypes = new ArrayList<>();
mAssetAccountTypes.add(AccountType.ASSET);
mAssetAccountTypes.add(AccountType.CASH);
Expand Down Expand Up @@ -109,11 +111,15 @@ protected void generateReport() {

@Override
protected void displayReport() {

loadAccountViews(mAssetAccountTypes, mAssetsTableLayout);
loadAccountViews(mLiabilityAccountTypes, mLiabilitiesTableLayout);
loadAccountViews(mEquityAccountTypes, mEquityTableLayout);

TransactionsActivity.displayBalance(mNetWorth, mAssetsBalance.subtract(mLiabilitiesBalance));
TransactionsActivity.displayBalance(mNetWorth,
// #8xx
mAssetsBalance.add(mLiabilitiesBalance),
null);
}

@Override
Expand All @@ -128,21 +134,27 @@ public void onPrepareOptionsMenu(Menu menu) {
* @param tableLayout Table layout into which to load the rows
*/
private void loadAccountViews(List<AccountType> accountTypes, TableLayout tableLayout){

LayoutInflater inflater = LayoutInflater.from(getActivity());

Cursor cursor = mAccountsDbAdapter.fetchAccounts(DatabaseSchema.AccountEntry.COLUMN_TYPE
+ " IN ( '" + TextUtils.join("' , '", accountTypes) + "' ) AND "
+ DatabaseSchema.AccountEntry.COLUMN_PLACEHOLDER + " = 0",
null, DatabaseSchema.AccountEntry.COLUMN_FULL_NAME + " ASC");

AccountType accountType = null;

while (cursor.moveToNext()){
String accountUID = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseSchema.AccountEntry.COLUMN_UID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseSchema.AccountEntry.COLUMN_NAME));
Money balance = mAccountsDbAdapter.getAccountBalance(accountUID);
View view = inflater.inflate(R.layout.row_balance_sheet, tableLayout, false);
((TextView)view.findViewById(R.id.account_name)).setText(name);
TextView balanceTextView = (TextView) view.findViewById(R.id.account_balance);
TransactionsActivity.displayBalance(balanceTextView, balance);
TextView balanceTextView = (TextView) view.findViewById(R.id.account_balance);
accountType = AccountType.valueOf(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseSchema.AccountEntry.COLUMN_TYPE)));
TransactionsActivity.displayBalance(balanceTextView,
balance,
accountType);
tableLayout.addView(view);
}

Expand All @@ -157,7 +169,11 @@ private void loadAccountViews(List<AccountType> accountTypes, TableLayout tableL
TextView accountBalance = (TextView) totalView.findViewById(R.id.account_balance);
accountBalance.setTextSize(16);
accountBalance.setTypeface(null, Typeface.BOLD);
TransactionsActivity.displayBalance(accountBalance, mAccountsDbAdapter.getAccountBalance(accountTypes, -1, System.currentTimeMillis()));
TransactionsActivity.displayBalance(accountBalance,
mAccountsDbAdapter.getAccountBalance(accountTypes,
-1,
System.currentTimeMillis()),
accountType);

tableLayout.addView(totalView);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ public void onActivityCreated(Bundle savedInstanceState) {
View view = addSplitView(split);
view.findViewById(R.id.input_accounts_spinner).setEnabled(false);
view.findViewById(R.id.btn_remove_split).setVisibility(View.GONE);
TransactionsActivity.displayBalance(mImbalanceTextView, new Money(mBaseAmount.negate(), mCommodity));

TransactionsActivity.displayBalance(mImbalanceTextView,
new Money(mBaseAmount.negate(),
mCommodity),
accountType);
}

}
Expand Down Expand Up @@ -594,6 +598,7 @@ public void afterTextChanged(Editable editable) {
BigDecimal imbalance = BigDecimal.ZERO;

for (View splitItem : mSplitItemViewList) {

SplitViewHolder viewHolder = (SplitViewHolder) splitItem.getTag();

// Get the absolute value of the amount
Expand Down Expand Up @@ -640,7 +645,8 @@ public void afterTextChanged(Editable editable) {

TransactionsActivity.displayBalance(mImbalanceTextView,
new Money(imbalance,
mCommodity));
mCommodity),
null);
}
}

Expand Down
Loading

0 comments on commit d698d71

Please sign in to comment.