From 329318327962437254badfdf4c88c901b1d2b5f7 Mon Sep 17 00:00:00 2001 From: Madhukar Date: Mon, 17 Sep 2012 10:58:26 +0530 Subject: [PATCH] Withdraw of deposit amount --- .../api/DepositAccountsApiResource.java | 10 +++- .../DepositAccountWithdrawalCommand.java | 35 +++++++++++++ .../ApiDataConversionService.java | 3 ++ .../ApiDataConversionServiceImpl.java | 24 +++++++++ .../saving/domain/DepositAccount.java | 50 +++++++++++++++++++ .../service/DepositAccountAssembler.java | 4 +- .../DepositAccountWritePlatformService.java | 5 ++ ...WritePlatformServiceJpaRepositoryImpl.java | 39 ++++++++++++++- 8 files changed, 164 insertions(+), 6 deletions(-) create mode 100644 mifosng-provider/src/main/java/org/mifosng/platform/api/commands/DepositAccountWithdrawalCommand.java diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/DepositAccountsApiResource.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/DepositAccountsApiResource.java index a627e3afe..a3686e4b4 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/DepositAccountsApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/DepositAccountsApiResource.java @@ -21,6 +21,7 @@ import javax.ws.rs.core.UriInfo; import org.apache.commons.lang.StringUtils; import org.mifosng.platform.api.commands.DepositAccountCommand; +import org.mifosng.platform.api.commands.DepositAccountWithdrawalCommand; import org.mifosng.platform.api.commands.DepositStateTransitionApprovalCommand; import org.mifosng.platform.api.commands.DepositStateTransitionCommand; import org.mifosng.platform.api.commands.UndoStateTransitionCommand; @@ -196,6 +197,10 @@ public class DepositAccountsApiResource { DepositStateTransitionApprovalCommand command=apiDataConversionService.convertJsonToDepositStateTransitionApprovalCommand(accountId, jsonRequestBody); EntityIdentifier identifier = this.depositAccountWritePlatformService.approveDepositApplication(command); response = Response.ok().entity(identifier).build(); + } else if(is(commandParam, "withdrawal")){ + DepositAccountWithdrawalCommand command = apiDataConversionService.convertJsonToDepositWithdrawalCommand(accountId, jsonRequestBody); + EntityIdentifier identifier = this.depositAccountWritePlatformService.withdrawDepositAccountMoney(command); + response = Response.ok().entity(identifier).build(); } else { DepositStateTransitionCommand command=apiDataConversionService.convertJsonToDepositStateTransitionCommand(accountId, jsonRequestBody); if (is(commandParam, "reject")) { @@ -204,9 +209,10 @@ public class DepositAccountsApiResource { } else if (is(commandParam, "withdrewbyclient")) { EntityIdentifier identifier = this.depositAccountWritePlatformService.withdrawDepositApplication(command); response = Response.ok().entity(identifier).build(); - } else if (is(commandParam, "mature")){ + /*} else if (is(commandParam, "withdrawal")){ EntityIdentifier identifier = this.depositAccountWritePlatformService.matureDepositApplication(command); - response = Response.ok().entity(identifier).build(); } + response = Response.ok().entity(identifier).build();*/ + } UndoStateTransitionCommand undoCommand = new UndoStateTransitionCommand(accountId, command.getNote()); diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/DepositAccountWithdrawalCommand.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/DepositAccountWithdrawalCommand.java new file mode 100644 index 000000000..fd2c677b3 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/DepositAccountWithdrawalCommand.java @@ -0,0 +1,35 @@ +package org.mifosng.platform.api.commands; + +import java.math.BigDecimal; + +public class DepositAccountWithdrawalCommand { + + private final Long accountId; + private final boolean renewAccount; + private final BigDecimal deposit; + + private final String note; + + public DepositAccountWithdrawalCommand(final Long accountId, final boolean renewAccount, final BigDecimal deposit, final String note) { + this.accountId = accountId; + this.renewAccount = renewAccount; + this.deposit = deposit; + this.note = note; + } + + public Long getAccountId() { + return accountId; + } + + public boolean isRenewAccount() { + return renewAccount; + } + + public String getNote() { + return note; + } + + public BigDecimal getDeposit() { + return deposit; + } +} diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionService.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionService.java index a2cd88043..01530ccb7 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionService.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionService.java @@ -5,6 +5,7 @@ import org.mifosng.platform.api.commands.BranchMoneyTransferCommand; import org.mifosng.platform.api.commands.ChargeCommand; import org.mifosng.platform.api.commands.ClientCommand; import org.mifosng.platform.api.commands.DepositAccountCommand; +import org.mifosng.platform.api.commands.DepositAccountWithdrawalCommand; import org.mifosng.platform.api.commands.DepositProductCommand; import org.mifosng.platform.api.commands.DepositStateTransitionApprovalCommand; import org.mifosng.platform.api.commands.DepositStateTransitionCommand; @@ -66,4 +67,6 @@ public interface ApiDataConversionService { DepositStateTransitionApprovalCommand convertJsonToDepositStateTransitionApprovalCommand(Long resourceIdentifier, String jsonRequestBody); StaffCommand convertJsonToStaffCommand(Long resourceIdentifier, String json); + + DepositAccountWithdrawalCommand convertJsonToDepositWithdrawalCommand(Long resourceIdentifier, String jsonRequestBody); } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java index c2d511509..94b383f13 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java @@ -22,6 +22,7 @@ import org.mifosng.platform.api.commands.BranchMoneyTransferCommand; import org.mifosng.platform.api.commands.ChargeCommand; import org.mifosng.platform.api.commands.ClientCommand; import org.mifosng.platform.api.commands.DepositAccountCommand; +import org.mifosng.platform.api.commands.DepositAccountWithdrawalCommand; import org.mifosng.platform.api.commands.DepositProductCommand; import org.mifosng.platform.api.commands.DepositStateTransitionApprovalCommand; import org.mifosng.platform.api.commands.DepositStateTransitionCommand; @@ -1144,4 +1145,27 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService { return new DepositStateTransitionApprovalCommand(resourceIdentifier, productId, commencementDate, tenureInMonths, deposit, interestCompoundedEveryPeriodType,interestCompoundedEvery, note); } + + @Override + public DepositAccountWithdrawalCommand convertJsonToDepositWithdrawalCommand(final Long resourceIdentifier, final String json) { + + if (StringUtils.isBlank(json)) { + throw new InvalidJsonException(); + } + + Type typeOfMap = new TypeToken>(){}.getType(); + Map requestMap = gsonConverter.fromJson(json, typeOfMap); + + Set supportedParams = new HashSet(Arrays.asList("renewAccount","deposit", "note")); + + checkForUnsupportedParameters(requestMap, supportedParams); + + Set modifiedParameters = new HashSet(); + + BigDecimal deposit=extractBigDecimalParameter("deposit", requestMap, modifiedParameters); + boolean renewAccount=extractBooleanParameter("renewAccount", requestMap, modifiedParameters); + String note = extractStringParameter("note", requestMap, modifiedParameters); + + return new DepositAccountWithdrawalCommand(resourceIdentifier,renewAccount, deposit, note); + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/saving/domain/DepositAccount.java b/mifosng-provider/src/main/java/org/mifosng/platform/saving/domain/DepositAccount.java index ac52eb3a4..1c64337d5 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/saving/domain/DepositAccount.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/saving/domain/DepositAccount.java @@ -300,6 +300,10 @@ public class DepositAccount extends AbstractAuditableCustom { return date; } + public BigDecimal getProjectedTotalOnMaturity() { + return projectedTotalOnMaturity; + } + public void reject(final LocalDate rejectedOn, final DepositLifecycleStateMachine depositLifecycleStateMachine) { @@ -466,4 +470,50 @@ public class DepositAccount extends AbstractAuditableCustom { this.renewdAccount = account; } + + public void withdrawDepositAccountMoney(boolean renewAccount, DepositLifecycleStateMachine depositLifecycleStateMachine) { + + if (new LocalDate().isAfter(maturesOnDate()) || new LocalDate().equals(maturesOnDate())) { + DepositAccountStatus statusEnum = depositLifecycleStateMachine.transition( + DepositAccountEvent.DEPOSIT_MATURED, + DepositAccountStatus.fromInt(this.depositStatus)); + this.depositStatus = statusEnum.getValue(); + + if(!renewAccount){ + DepositAccountTransaction depositaccountTransaction = DepositAccountTransaction.withdraw(Money.of(this.currency, this.total), new LocalDate()); + depositaccountTransaction.updateAccount(this); + this.depositaccountTransactions.add(depositaccountTransaction); + + DepositAccountStatus statusEnumForClose = depositLifecycleStateMachine.transition( + DepositAccountEvent.DEPOSIT_CLOSED, + DepositAccountStatus.fromInt(this.depositStatus)); + this.depositStatus = statusEnumForClose.getValue(); + + this.depositAmount = null; + this.interestRate = null; + this.tenureInMonths = null; + this.interestCompoundedEvery = null; + this.interestCompoundedFrequencyType = null; + this.closedOnDate = new LocalDate().toDate(); + this.withdrawnOnDate = null; + this.rejectedOnDate = null; + + }else if(renewAccount){ + DepositAccountStatus statusEnumForClose = depositLifecycleStateMachine.transition( + DepositAccountEvent.DEPOSIT_CLOSED, + DepositAccountStatus.fromInt(this.depositStatus)); + this.depositStatus = statusEnumForClose.getValue(); + + DepositAccountTransaction depositaccountTransaction = DepositAccountTransaction.withdraw(Money.of(this.currency, this.total), new LocalDate()); + depositaccountTransaction.updateAccount(this); + this.depositaccountTransactions.add(depositaccountTransaction); + + this.closedOnDate = new LocalDate().toDate(); + this.withdrawnOnDate = null; + this.rejectedOnDate = null; + } + } + + + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/saving/service/DepositAccountAssembler.java b/mifosng-provider/src/main/java/org/mifosng/platform/saving/service/DepositAccountAssembler.java index ed6f0699a..2d5fd9450 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/saving/service/DepositAccountAssembler.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/saving/service/DepositAccountAssembler.java @@ -110,13 +110,13 @@ public class DepositAccountAssembler { return new DepositLifecycleStateMachineImpl(allowedDepositStatuses); } - public DepositAccount assembleFrom(DepositAccount account) { + public DepositAccount assembleFrom(DepositAccount account, BigDecimal newDepositAmount) { Client client = account.client(); DepositProduct product = account.product(); - Money deposit = Money.of(product.getCurrency(), account.getDeposit().getAmount()); + Money deposit = Money.of(product.getCurrency(), newDepositAmount); Integer tenureInMonths = product.getTenureInMonths(); diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/saving/service/DepositAccountWritePlatformService.java b/mifosng-provider/src/main/java/org/mifosng/platform/saving/service/DepositAccountWritePlatformService.java index 223c227b2..eac73f313 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/saving/service/DepositAccountWritePlatformService.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/saving/service/DepositAccountWritePlatformService.java @@ -1,6 +1,7 @@ package org.mifosng.platform.saving.service; import org.mifosng.platform.api.commands.DepositAccountCommand; +import org.mifosng.platform.api.commands.DepositAccountWithdrawalCommand; import org.mifosng.platform.api.commands.DepositStateTransitionApprovalCommand; import org.mifosng.platform.api.commands.DepositStateTransitionCommand; import org.mifosng.platform.api.commands.UndoStateTransitionCommand; @@ -30,4 +31,8 @@ public interface DepositAccountWritePlatformService { @PreAuthorize(value = "hasAnyRole('PORTFOLIO_MANAGEMENT_SUPER_USER_ROLE', 'CAN_APPROVE_DEPOSIT_ROLE', 'CAN_APPROVE_DEPOSIT_IN_THE_PAST_ROLE')") EntityIdentifier matureDepositApplication(DepositStateTransitionCommand command); + @PreAuthorize(value = "hasAnyRole('PORTFOLIO_MANAGEMENT_SUPER_USER_ROLE', 'CAN_APPROVE_DEPOSIT_ROLE', 'CAN_APPROVE_DEPOSIT_IN_THE_PAST_ROLE')") + EntityIdentifier withdrawDepositAccountMoney(DepositAccountWithdrawalCommand command); + + } diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/saving/service/DepositAccountWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/saving/service/DepositAccountWritePlatformServiceJpaRepositoryImpl.java index 2e8491c3e..1c3efdea2 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/saving/service/DepositAccountWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/saving/service/DepositAccountWritePlatformServiceJpaRepositoryImpl.java @@ -1,11 +1,13 @@ package org.mifosng.platform.saving.service; +import java.math.BigDecimal; import java.util.Arrays; import java.util.List; import org.apache.commons.lang.StringUtils; import org.joda.time.LocalDate; import org.mifosng.platform.api.commands.DepositAccountCommand; +import org.mifosng.platform.api.commands.DepositAccountWithdrawalCommand; import org.mifosng.platform.api.commands.DepositStateTransitionApprovalCommand; import org.mifosng.platform.api.commands.DepositStateTransitionCommand; import org.mifosng.platform.api.commands.UndoStateTransitionCommand; @@ -257,11 +259,12 @@ public class DepositAccountWritePlatformServiceJpaRepositoryImpl implements Depo account.matureDepositApplication(eventDate, defaultDepositLifecycleStateMachine()); this.depositAccountRepository.save(account); - if(account.isRenewalAllowed()){ + /*if(account.isRenewalAllowed()){ final DepositAccount renewedAccount = this.depositAccountAssembler.assembleFrom(account); this.depositAccountRepository.save(renewedAccount); return new EntityIdentifier(renewedAccount.getId()); //returns the new deposit application id - } + }*/ + String noteText = command.getNote(); if (StringUtils.isNotBlank(noteText)) { Note note = Note.depositNote(account, noteText); @@ -270,4 +273,36 @@ public class DepositAccountWritePlatformServiceJpaRepositoryImpl implements Depo return new EntityIdentifier(account.getId()); } + + @Transactional + @Override + public EntityIdentifier withdrawDepositAccountMoney(DepositAccountWithdrawalCommand command) { + + context.authenticatedUser(); + + DepositAccount account = this.depositAccountRepository.findOne(command.getAccountId()); + if (account == null || account.isDeleted()) { + throw new DepositAccountNotFoundException(command.getAccountId()); + } + + account.withdrawDepositAccountMoney(command.isRenewAccount(), defaultDepositLifecycleStateMachine()); + this.depositAccountRepository.save(account); + + BigDecimal deposit = account.getProjectedTotalOnMaturity(); + if(command.getDeposit() != null) + deposit = command.getDeposit(); + + if(command.isRenewAccount()){ + final DepositAccount renewedAccount = this.depositAccountAssembler.assembleFrom(account,deposit); + this.depositAccountRepository.save(renewedAccount); + return new EntityIdentifier(renewedAccount.getId()); //returns the new deposit application id + } + + String noteText = command.getNote(); + if (StringUtils.isNotBlank(noteText)) { + Note note = Note.depositNote(account, noteText); + this.noteRepository.save(note); + } + return new EntityIdentifier(account.getId()); + } } \ No newline at end of file