From 1957dba4ace4184a068a08646ec72bdf508007cc Mon Sep 17 00:00:00 2001 From: Madhukar Date: Thu, 13 Sep 2012 10:04:46 +0530 Subject: [PATCH] adding maturity for deposit account --- .../api/DepositAccountsApiResource.java | 3 ++ .../saving/domain/DepositAccount.java | 43 +++++++++++++++++++ .../DepositAccountWritePlatformService.java | 3 ++ ...WritePlatformServiceJpaRepositoryImpl.java | 33 +++++++++++++- 4 files changed, 81 insertions(+), 1 deletion(-) 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 19681440c..b54cf9c63 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 @@ -205,6 +205,9 @@ public class DepositAccountsApiResource { } else if (is(commandParam, "withdrewbyclient")) { EntityIdentifier identifier = this.depositAccountWritePlatformService.withdrawDepositApplication(command); response = Response.ok().entity(identifier).build(); + } else if (is(commandParam, "matureAccount")){ + EntityIdentifier identifier = this.depositAccountWritePlatformService.matureDepositApplication(command); + response = Response.ok().entity(identifier).build(); } UndoStateTransitionCommand undoCommand = new UndoStateTransitionCommand(accountId, command.getNote()); 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 1a2160420..1f834d369 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 @@ -289,6 +289,14 @@ public class DepositAccount extends AbstractAuditableCustom { } return date; } + + public LocalDate getMaturityDate() { + LocalDate date = null; + if (this.maturesOnDate != null) { + date = new LocalDate(this.maturesOnDate); + } + return date; + } public void reject(final LocalDate rejectedOn, final DepositLifecycleStateMachine depositLifecycleStateMachine) { @@ -373,4 +381,39 @@ public class DepositAccount extends AbstractAuditableCustom { public Client client() { return this.client; } + + public void matureDepositApplication(LocalDate maturedOnDate, DepositLifecycleStateMachine depositLifecycleStateMachine) { + + if (maturedOnDate.isAfter(maturesOnDate()) || new LocalDate().equals(maturesOnDate())) { + DepositAccountStatus statusEnum = depositLifecycleStateMachine.transition( + DepositAccountEvent.DEPOSIT_MATURED, + DepositAccountStatus.fromInt(this.depositStatus)); + this.depositStatus = statusEnum.getValue(); + + if(!this.renewalAllowed){ + DepositAccountTransaction depositaccountTransaction = DepositAccountTransaction.withdraw(Money.of(this.currency, this.total), maturedOnDate); + depositaccountTransaction.updateAccount(this); + this.depositaccountTransactions.add(depositaccountTransaction); + } + + } + + if (maturesOnDate().isBefore(new LocalDate())) { + + final String errorMessage = "The date on which a deposit is rejected cannot be before its submittal date: " + + new LocalDate().toString(); + throw new InvalidDepositStateTransitionException("reject", + "cannot.be.before.submittal.date", errorMessage, + maturedOnDate, new LocalDate()); + + } + } + + public LocalDate maturesOnDate() { + LocalDate date = null; + if (this.maturesOnDate != null) { + date = new LocalDate(this.maturesOnDate); + } + return date; + } } \ No newline at end of file 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 3d9a96921..223c227b2 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 @@ -27,4 +27,7 @@ public interface DepositAccountWritePlatformService { @PreAuthorize(value = "hasAnyRole('PORTFOLIO_MANAGEMENT_SUPER_USER_ROLE', 'CAN_APPROVE_DEPOSIT_ROLE', 'CAN_APPROVE_DEPOSIT_IN_THE_PAST_ROLE')") EntityIdentifier undoDepositApproval(UndoStateTransitionCommand undoCommand); + @PreAuthorize(value = "hasAnyRole('PORTFOLIO_MANAGEMENT_SUPER_USER_ROLE', 'CAN_APPROVE_DEPOSIT_ROLE', 'CAN_APPROVE_DEPOSIT_IN_THE_PAST_ROLE')") + EntityIdentifier matureDepositApplication(DepositStateTransitionCommand 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 18c35acc1..98de99761 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 @@ -18,9 +18,9 @@ import org.mifosng.platform.exceptions.PlatformDataIntegrityException; import org.mifosng.platform.exceptions.ProductNotFoundException; import org.mifosng.platform.saving.domain.DepositAccount; import org.mifosng.platform.saving.domain.DepositAccountRepository; +import org.mifosng.platform.saving.domain.DepositAccountStatus; import org.mifosng.platform.saving.domain.DepositLifecycleStateMachine; import org.mifosng.platform.saving.domain.DepositLifecycleStateMachineImpl; -import org.mifosng.platform.saving.domain.DepositAccountStatus; import org.mifosng.platform.saving.domain.FixedTermDepositInterestCalculator; import org.mifosng.platform.security.PlatformSecurityContext; import org.mifosng.platform.user.domain.AppUser; @@ -234,4 +234,35 @@ public class DepositAccountWritePlatformServiceJpaRepositoryImpl implements Depo } return new EntityIdentifier(account.getId()); } + + @Transactional + @Override + public EntityIdentifier matureDepositApplication(DepositStateTransitionCommand command) { + + AppUser currentUser = context.authenticatedUser(); + + DepositStateTransitionCommandValidator validator = new DepositStateTransitionCommandValidator(command); + validator.validate(); + + DepositAccount account = this.depositAccountRepository.findOne(command.getAccountId()); + if (account == null || account.isDeleted()) { + throw new DepositAccountNotFoundException(command.getAccountId()); + } + + LocalDate eventDate = command.getEventDate(); + if (this.isBeforeToday(eventDate) && currentUser.canNotApproveLoanInPast()) { + throw new NoAuthorizationException("User has no authority to mature deposit with a date in the past."); + } + + account.matureDepositApplication(eventDate, defaultDepositLifecycleStateMachine()); + this.depositAccountRepository.save(account); + + 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