fix overdraft account balance (PR #659 for FINERACT-753)

This commit is contained in:
andrew-dzak
2019-12-11 07:59:50 +01:00
committed by Michael Vorburger ⛑️
parent 26b4e0000c
commit 0474f59bcf
3 changed files with 96 additions and 0 deletions
@@ -44,6 +44,7 @@ import org.apache.fineract.integrationtests.common.charges.ChargesHelper;
import org.apache.fineract.integrationtests.common.savings.SavingsAccountHelper;
import org.apache.fineract.integrationtests.common.savings.SavingsProductHelper;
import org.apache.fineract.integrationtests.common.savings.SavingsStatusChecker;
import org.apache.fineract.portfolio.charge.domain.ChargeTimeType;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
@@ -2233,4 +2234,74 @@ public class ClientSavingsIntegrationTest {
assertEquals("Verifying Withdrawal Amount", new Float("300"), withdrawTransaction.get("amount"));
}
/**
* incorrect savings account balance when charge transaction is
* reversed during an overdraft recalculate Daily Balances
*/
@Test
public void testAccountBalanceAfterTransactionReversal() {
this.savingsAccountHelper = new SavingsAccountHelper(this.requestSpec, this.responseSpec);
SavingsAccountHelper savingsAccountHelperValidationError = new SavingsAccountHelper(this.requestSpec,
new ResponseSpecBuilder().build());
final Integer clientID = ClientHelper.createClient(this.requestSpec, this.responseSpec);
Assert.assertNotNull(clientID);
final String minBalanceForInterestCalculation = null;
final String minRequiredBalance = "500";
final String enforceMinRequiredBalance = "false";
final boolean allowOverdraft = true;
final String MINIMUM_OPENING_BALANCE = "0";
final Integer savingsProductID = createSavingsProduct(this.requestSpec, this.responseSpec, MINIMUM_OPENING_BALANCE,
minBalanceForInterestCalculation, minRequiredBalance, enforceMinRequiredBalance, allowOverdraft);
Assert.assertNotNull(savingsProductID);
final Integer savingsId = this.savingsAccountHelper.applyForSavingsApplication(clientID, savingsProductID, ACCOUNT_TYPE_INDIVIDUAL);
Assert.assertNotNull(savingsId);
HashMap savingsStatusHashMap = this.savingsAccountHelper.approveSavings(savingsId);
SavingsStatusChecker.verifySavingsIsApproved(savingsStatusHashMap);
savingsStatusHashMap = this.savingsAccountHelper.activateSavings(savingsId);
SavingsStatusChecker.verifySavingsIsActive(savingsStatusHashMap);
Integer depositTransactionId = (Integer) this.savingsAccountHelper.depositToSavingsAccount(savingsId, "500",
SavingsAccountHelper.TRANSACTION_DATE, CommonConstants.RESPONSE_RESOURCE_ID);
String chargeAmount = "300";
String chargeCurrency = "USD";
final Integer savingsChargeId = ChargesHelper.createCharges(this.requestSpec, this.responseSpec,
ChargesHelper.getSavingsJSON(chargeAmount, chargeCurrency, ChargeTimeType.SPECIFIED_DUE_DATE));
Assert.assertNotNull(savingsChargeId);
Integer amount = 300;
final Integer chargeId = this.savingsAccountHelper.addChargesForSavingsWithDueDate(savingsId, savingsChargeId, SavingsAccountHelper.TRANSACTION_DATE,
amount);
Assert.assertNotNull(chargeId);
final Integer payChargeId = this.savingsAccountHelper.payCharge(chargeId, savingsId, chargeAmount, SavingsAccountHelper.TRANSACTION_DATE);
final Integer undoSavingsTransaction = this.savingsAccountHelper.undoSavingsAccountTransaction(savingsId, depositTransactionId);
HashMap reversedDepositTransaction = this.savingsAccountHelper.getSavingsTransaction(savingsId, depositTransactionId);
Assert.assertTrue((Boolean) reversedDepositTransaction.get("reversed"));
HashMap summary = this.savingsAccountHelper.getSavingsSummary(savingsId);
Float balance = new Float("-300");
assertEquals("Verifying opening Balance is -300", balance, summary.get("accountBalance"));
}
}
@@ -269,6 +269,11 @@ public class SavingsAccountHelper {
getPeriodChargeRequestJSON(chargeId, addDueDate, amount), CommonConstants.RESPONSE_RESOURCE_ID);
}
public Integer addChargesForSavingsWithDueDate(final Integer savingsId, final Integer chargeId, String addDueDate,Integer amount) {
return (Integer) performSavingActions(SAVINGS_ACCOUNT_URL + "/" + savingsId + "/charges?" + Utils.TENANT_IDENTIFIER,
getPeriodChargeRequestJSONWithDueDate(chargeId, addDueDate, amount), CommonConstants.RESPONSE_RESOURCE_ID);
}
public Integer payCharge(final Integer chargeId, final Integer savingsId, String amount, String dueDate) {
return (Integer) performSavingActions(createChargesURL("paycharge", savingsId, chargeId), getSavingsPayChargeJSON(amount, dueDate),
CommonConstants.RESPONSE_RESOURCE_ID);
@@ -593,6 +598,17 @@ public class SavingsAccountHelper {
return json;
}
private String getPeriodChargeRequestJSONWithDueDate(Integer chargeId, String addDueDate, Integer amount) {
final HashMap<String, Object> map = new HashMap<>();
map.put("chargeId", chargeId);
map.put("amount", amount);
map.put("locale", CommonConstants.locale);
map.put("dateFormat", "dd MMMM yyy");
map.put("dueDate", addDueDate);
String json = new Gson().toJson(map);
return json;
}
private String getAccountActivationJSON(final String activationDate) {
final HashMap<String, Object> map = new HashMap<>();
map.put("locale", CommonConstants.locale);
@@ -851,6 +851,15 @@ public class SavingsAccount extends AbstractPersistableCustom<Long> {
transaction.updateOverdraftAmount(overdraftAmount.getAmount());
} else if (overdraftAmount.isNotEqualTo(transaction.getOverdraftAmount(getCurrency()))) {
SavingsAccountTransaction accountTransaction = SavingsAccountTransaction.copyTransaction(transaction);
if(transaction.isChargeTransaction()){
Set<SavingsAccountChargePaidBy> chargesPaidBy = transaction.getSavingsAccountChargesPaid();
final Set<SavingsAccountChargePaidBy> newChargePaidBy = new HashSet<>();
chargesPaidBy.forEach(x->
newChargePaidBy.add(SavingsAccountChargePaidBy.instance(accountTransaction,x.getSavingsAccountCharge(),
x.getAmount()))
);
accountTransaction.getSavingsAccountChargesPaid().addAll(newChargePaidBy);
}
transaction.reverse();
if (overdraftAmount.isGreaterThanZero()) {
accountTransaction.updateOverdraftAmount(overdraftAmount.getAmount());