added status validation for undo transaction

This commit is contained in:
Pramod Nuthakki
2013-09-06 18:41:59 +05:30
parent e36255c030
commit 44a63683c5
4 changed files with 35 additions and 22 deletions
@@ -15,18 +15,16 @@ public class SavingsAccountTransactionDTO {
private List<Long> existingTransactionIds; private List<Long> existingTransactionIds;
private List<Long> existingReversedTransactionIds; private List<Long> existingReversedTransactionIds;
private PaymentDetail paymentDetail; private PaymentDetail paymentDetail;
private boolean checkAccountStatus;
public SavingsAccountTransactionDTO(final DateTimeFormatter formatter, final LocalDate transactionDate, public SavingsAccountTransactionDTO(final DateTimeFormatter formatter, final LocalDate transactionDate,
final BigDecimal transactionAmount, final List<Long> existingTransactionIds, final List<Long> existingReversedTransactionIds, final BigDecimal transactionAmount, final List<Long> existingTransactionIds, final List<Long> existingReversedTransactionIds,
final PaymentDetail paymentDetail, boolean checkAccountStatus) { final PaymentDetail paymentDetail) {
this.formatter = formatter; this.formatter = formatter;
this.transactionDate = transactionDate; this.transactionDate = transactionDate;
this.transactionAmount = transactionAmount; this.transactionAmount = transactionAmount;
this.existingTransactionIds = existingTransactionIds; this.existingTransactionIds = existingTransactionIds;
this.existingReversedTransactionIds = existingReversedTransactionIds; this.existingReversedTransactionIds = existingReversedTransactionIds;
this.paymentDetail = paymentDetail; this.paymentDetail = paymentDetail;
this.checkAccountStatus = checkAccountStatus;
} }
public DateTimeFormatter getFormatter() { public DateTimeFormatter getFormatter() {
@@ -52,8 +50,4 @@ public class SavingsAccountTransactionDTO {
public PaymentDetail getPaymentDetail() { public PaymentDetail getPaymentDetail() {
return this.paymentDetail; return this.paymentDetail;
} }
public boolean isCheckAccountStatus() {
return this.checkAccountStatus;
}
} }
@@ -550,7 +550,7 @@ public class SavingsAccount extends AbstractPersistable<Long> {
public SavingsAccountTransaction deposit(final SavingsAccountTransactionDTO transactionDTO) { public SavingsAccountTransaction deposit(final SavingsAccountTransactionDTO transactionDTO) {
if (transactionDTO.isCheckAccountStatus() && isNotActive()) { if (isNotActive()) {
final String defaultUserMessage = "Transaction is not allowed. Account is not active."; final String defaultUserMessage = "Transaction is not allowed. Account is not active.";
final ApiParameterError error = ApiParameterError.parameterError("error.msg.savingsaccount.transaction.account.is.not.active", final ApiParameterError error = ApiParameterError.parameterError("error.msg.savingsaccount.transaction.account.is.not.active",
defaultUserMessage, "transactionDate", transactionDTO.getTransactionDate().toString(transactionDTO.getFormatter())); defaultUserMessage, "transactionDate", transactionDTO.getTransactionDate().toString(transactionDTO.getFormatter()));
@@ -609,7 +609,7 @@ public class SavingsAccount extends AbstractPersistable<Long> {
public SavingsAccountTransaction withdraw(SavingsAccountTransactionDTO transactionDTO, final boolean applyWithdrawFee) { public SavingsAccountTransaction withdraw(SavingsAccountTransactionDTO transactionDTO, final boolean applyWithdrawFee) {
if (transactionDTO.isCheckAccountStatus() && isNotActive()) { if (isNotActive()) {
final String defaultUserMessage = "Transaction is not allowed. Account is not active."; final String defaultUserMessage = "Transaction is not allowed. Account is not active.";
final ApiParameterError error = ApiParameterError.parameterError("error.msg.savingsaccount.transaction.account.is.not.active", final ApiParameterError error = ApiParameterError.parameterError("error.msg.savingsaccount.transaction.account.is.not.active",
@@ -689,8 +689,6 @@ public class SavingsAccount extends AbstractPersistable<Long> {
} }
} }
validateAccountBalanceDoesNotBecomeNegative(retreiveListOfTransactions(), transactionAmountMoney);
this.summary.updateSummary(this.currency, this.savingsAccountTransactionSummaryWrapper, this.transactions); this.summary.updateSummary(this.currency, this.savingsAccountTransactionSummaryWrapper, this.transactions);
return transaction; return transaction;
@@ -710,9 +708,8 @@ public class SavingsAccount extends AbstractPersistable<Long> {
return transactionBeforeLastInterestPosting; return transactionBeforeLastInterestPosting;
} }
private void validateAccountBalanceDoesNotBecomeNegative(final List<SavingsAccountTransaction> transactionsSortedByDate, public void validateAccountBalanceDoesNotBecomeNegative(final BigDecimal transactionAmount) {
final Money transactionAmount) { final List<SavingsAccountTransaction> transactionsSortedByDate = retreiveListOfTransactions();
Money runningBalance = Money.zero(this.currency); Money runningBalance = Money.zero(this.currency);
for (final SavingsAccountTransaction transaction : transactionsSortedByDate) { for (final SavingsAccountTransaction transaction : transactionsSortedByDate) {
@@ -726,7 +723,7 @@ public class SavingsAccount extends AbstractPersistable<Long> {
// //
final BigDecimal withdrawalFee = null; final BigDecimal withdrawalFee = null;
throw new InsufficientAccountBalanceException("transactionAmount", getAccountBalance(), withdrawalFee, throw new InsufficientAccountBalanceException("transactionAmount", getAccountBalance(), withdrawalFee,
transactionAmount.getAmount()); transactionAmount);
} }
} }
} }
@@ -1672,8 +1669,8 @@ public class SavingsAccount extends AbstractPersistable<Long> {
// activating account. // activating account.
final Money minRequiredOpeningBalance = Money.of(this.currency, this.minRequiredOpeningBalance); final Money minRequiredOpeningBalance = Money.of(this.currency, this.minRequiredOpeningBalance);
if (minRequiredOpeningBalance.isGreaterThanZero()) { if (minRequiredOpeningBalance.isGreaterThanZero()) {
SavingsAccountTransactionDTO transactionDTO = new SavingsAccountTransactionDTO(fmt, activationDate, SavingsAccountTransactionDTO transactionDTO = new SavingsAccountTransactionDTO(fmt, activationDate, minRequiredOpeningBalance.getAmount(),
minRequiredOpeningBalance.getAmount(), existingTransactionIds, existingReversedTransactionIds, null, true); existingTransactionIds, existingReversedTransactionIds, null);
deposit(transactionDTO); deposit(transactionDTO);
} }
@@ -51,7 +51,7 @@ public class SavingsAccountDomainServiceJpa implements SavingsAccountDomainServi
final List<Long> existingTransactionIds = new ArrayList<Long>(); final List<Long> existingTransactionIds = new ArrayList<Long>();
final List<Long> existingReversedTransactionIds = new ArrayList<Long>(); final List<Long> existingReversedTransactionIds = new ArrayList<Long>();
SavingsAccountTransactionDTO transactionDTO = new SavingsAccountTransactionDTO(fmt, transactionDate, transactionAmount, SavingsAccountTransactionDTO transactionDTO = new SavingsAccountTransactionDTO(fmt, transactionDate, transactionAmount,
existingTransactionIds,existingReversedTransactionIds, paymentDetail,true); existingTransactionIds,existingReversedTransactionIds, paymentDetail);
final SavingsAccountTransaction withdrawal = account.withdraw(transactionDTO , applyWithdrawFee); final SavingsAccountTransaction withdrawal = account.withdraw(transactionDTO , applyWithdrawFee);
final MathContext mc = MathContext.DECIMAL64; final MathContext mc = MathContext.DECIMAL64;
@@ -62,7 +62,7 @@ public class SavingsAccountDomainServiceJpa implements SavingsAccountDomainServi
final LocalDate today = DateUtils.getLocalDateOfTenant(); final LocalDate today = DateUtils.getLocalDateOfTenant();
account.calculateInterestUsing(mc, today); account.calculateInterestUsing(mc, today);
} }
account.validateAccountBalanceDoesNotBecomeNegative(transactionAmount);
saveTransactionToGenerateTransactionId(withdrawal); saveTransactionToGenerateTransactionId(withdrawal);
this.savingsAccountRepository.save(account); this.savingsAccountRepository.save(account);
@@ -79,7 +79,7 @@ public class SavingsAccountDomainServiceJpa implements SavingsAccountDomainServi
final List<Long> existingTransactionIds = new ArrayList<Long>(); final List<Long> existingTransactionIds = new ArrayList<Long>();
final List<Long> existingReversedTransactionIds = new ArrayList<Long>(); final List<Long> existingReversedTransactionIds = new ArrayList<Long>();
SavingsAccountTransactionDTO transactionDTO = new SavingsAccountTransactionDTO(fmt, transactionDate, transactionAmount, SavingsAccountTransactionDTO transactionDTO = new SavingsAccountTransactionDTO(fmt, transactionDate, transactionAmount,
existingTransactionIds, existingReversedTransactionIds, paymentDetail, true); existingTransactionIds, existingReversedTransactionIds, paymentDetail);
final SavingsAccountTransaction deposit = account.deposit(transactionDTO); final SavingsAccountTransaction deposit = account.deposit(transactionDTO);
final MathContext mc = MathContext.DECIMAL64; final MathContext mc = MathContext.DECIMAL64;
@@ -5,6 +5,8 @@
*/ */
package org.mifosplatform.portfolio.savings.service; package org.mifosplatform.portfolio.savings.service;
import static org.mifosplatform.portfolio.savings.SavingsApiConstants.SAVINGS_ACCOUNT_RESOURCE_NAME;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.MathContext; import java.math.MathContext;
import java.math.RoundingMode; import java.math.RoundingMode;
@@ -20,8 +22,11 @@ import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.DateTimeFormatter;
import org.mifosplatform.accounting.journalentry.service.JournalEntryWritePlatformService; import org.mifosplatform.accounting.journalentry.service.JournalEntryWritePlatformService;
import org.mifosplatform.infrastructure.core.api.JsonCommand; import org.mifosplatform.infrastructure.core.api.JsonCommand;
import org.mifosplatform.infrastructure.core.data.ApiParameterError;
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult; import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
import org.mifosplatform.infrastructure.core.data.CommandProcessingResultBuilder; import org.mifosplatform.infrastructure.core.data.CommandProcessingResultBuilder;
import org.mifosplatform.infrastructure.core.data.DataValidatorBuilder;
import org.mifosplatform.infrastructure.core.exception.PlatformApiDataValidationException;
import org.mifosplatform.infrastructure.core.service.DateUtils; import org.mifosplatform.infrastructure.core.service.DateUtils;
import org.mifosplatform.infrastructure.jobs.annotation.CronTarget; import org.mifosplatform.infrastructure.jobs.annotation.CronTarget;
import org.mifosplatform.infrastructure.jobs.service.JobName; import org.mifosplatform.infrastructure.jobs.service.JobName;
@@ -299,6 +304,9 @@ public class SavingsAccountWritePlatformServiceJpaRepositoryImpl implements Savi
final MathContext mc = new MathContext(15, RoundingMode.HALF_EVEN); final MathContext mc = new MathContext(15, RoundingMode.HALF_EVEN);
final SavingsAccount account = this.savingAccountAssembler.assembleFrom(savingsId); final SavingsAccount account = this.savingAccountAssembler.assembleFrom(savingsId);
if(account.isNotActive()){
throwValidationForActiveStatus(SavingsApiConstants.undoTransactionAction);
}
account.undoTransaction(transactionId, reversedTransactionIds); account.undoTransaction(transactionId, reversedTransactionIds);
checkClientOrGroupActive(account); checkClientOrGroupActive(account);
if ((savingsAccountTransaction.isDeposit() || savingsAccountTransaction.isWithdrawal()) if ((savingsAccountTransaction.isDeposit() || savingsAccountTransaction.isWithdrawal())
@@ -334,6 +342,9 @@ public class SavingsAccountWritePlatformServiceJpaRepositoryImpl implements Savi
final LocalDate today = DateUtils.getLocalDateOfTenant(); final LocalDate today = DateUtils.getLocalDateOfTenant();
final SavingsAccount account = this.savingAccountAssembler.assembleFrom(savingsId); final SavingsAccount account = this.savingAccountAssembler.assembleFrom(savingsId);
if(account.isNotActive()){
throwValidationForActiveStatus(SavingsApiConstants.adjustTransactionAction);
}
final Locale locale = command.extractLocale(); final Locale locale = command.extractLocale();
final DateTimeFormatter fmt = DateTimeFormat.forPattern(command.dateFormat()).withLocale(locale); final DateTimeFormatter fmt = DateTimeFormat.forPattern(command.dateFormat()).withLocale(locale);
@@ -357,11 +368,11 @@ public class SavingsAccountWritePlatformServiceJpaRepositoryImpl implements Savi
SavingsAccountTransaction transaction = null; SavingsAccountTransaction transaction = null;
if (savingsAccountTransaction.isDeposit()) { if (savingsAccountTransaction.isDeposit()) {
SavingsAccountTransactionDTO transactionDTO = new SavingsAccountTransactionDTO(fmt, transactionDate, transactionAmount, SavingsAccountTransactionDTO transactionDTO = new SavingsAccountTransactionDTO(fmt, transactionDate, transactionAmount,
existingTransactionIds, existingReversedTransactionIds,paymentDetail, false); existingTransactionIds, existingReversedTransactionIds,paymentDetail);
transaction = account.deposit(transactionDTO); transaction = account.deposit(transactionDTO);
} else { } else {
SavingsAccountTransactionDTO transactionDTO = new SavingsAccountTransactionDTO(fmt, transactionDate, transactionAmount, SavingsAccountTransactionDTO transactionDTO = new SavingsAccountTransactionDTO(fmt, transactionDate, transactionAmount,
existingTransactionIds, existingReversedTransactionIds, paymentDetail, false); existingTransactionIds, existingReversedTransactionIds, paymentDetail);
transaction = account.withdraw(transactionDTO , true); transaction = account.withdraw(transactionDTO , true);
} }
final Long newtransactionId = saveTransactionToGenerateTransactionId(transaction); final Long newtransactionId = saveTransactionToGenerateTransactionId(transaction);
@@ -385,6 +396,17 @@ public class SavingsAccountWritePlatformServiceJpaRepositoryImpl implements Savi
.build(); .build();
} }
/**
*
*/
private void throwValidationForActiveStatus(String actionName) {
final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors)
.resource(SAVINGS_ACCOUNT_RESOURCE_NAME + actionName);
baseDataValidator.reset().failWithCodeNoParameterAddedToErrorCode("account.is.not.active");
throw new PlatformApiDataValidationException(dataValidationErrors);
}
private void postJournalEntries(final SavingsAccount savingsAccount, final List<Long> existingTransactionIds, private void postJournalEntries(final SavingsAccount savingsAccount, final List<Long> existingTransactionIds,
final List<Long> existingReversedTransactionIds) { final List<Long> existingReversedTransactionIds) {