From 9b826ea98ec07cf7d6e474cc6839ef35fee744a8 Mon Sep 17 00:00:00 2001 From: Alex Ivanov Date: Tue, 2 Dec 2014 21:06:53 +0200 Subject: [PATCH] MIFOSX-1575 Add optional 'comments' parameter to Journal Reverse command --- api-docs/apiLive.htm | 9 +++++++ .../api/JournalEntriesApiResource.java | 5 ++-- ...WritePlatformServiceJpaRepositoryImpl.java | 24 +++++++++++++++++-- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/api-docs/apiLive.htm b/api-docs/apiLive.htm index 16e6ab529..34d070eb5 100644 --- a/api-docs/apiLive.htm +++ b/api-docs/apiLive.htm @@ -30807,6 +30807,15 @@ GET https://DomainName/api/v1/journalentries/{entryId}
'reverse' : Reverse the Journal Entry
+
Optional Arguments
+
+
comments
+
+ String optional +
+
Optional string to be applied as a comment for the created reversal entry, + default comment is used if this field is not supplied
+
diff --git a/mifosng-provider/src/main/java/org/mifosplatform/accounting/journalentry/api/JournalEntriesApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/accounting/journalentry/api/JournalEntriesApiResource.java index 683405a88..39bb0c6da 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/accounting/journalentry/api/JournalEntriesApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/accounting/journalentry/api/JournalEntriesApiResource.java @@ -142,11 +142,12 @@ public class JournalEntriesApiResource { @Path("{transactionId}") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) - public String createReversalJournalEntry(@PathParam("transactionId") final String transactionId, + public String createReversalJournalEntry(final String jsonRequestBody, + @PathParam("transactionId") final String transactionId, @QueryParam("command") final String commandParam) { CommandProcessingResult result = null; if (is(commandParam, "reverse")) { - final CommandWrapper commandRequest = new CommandWrapperBuilder().reverseJournalEntry(transactionId).build(); + final CommandWrapper commandRequest = new CommandWrapperBuilder().reverseJournalEntry(transactionId).withJson(jsonRequestBody).build(); result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); } else { throw new UnrecognizedQueryParamException("command", commandParam); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/accounting/journalentry/service/JournalEntryWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/accounting/journalentry/service/JournalEntryWritePlatformServiceJpaRepositoryImpl.java index 64ea21d64..16b1e4fd5 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/accounting/journalentry/service/JournalEntryWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/accounting/journalentry/service/JournalEntryWritePlatformServiceJpaRepositoryImpl.java @@ -37,8 +37,11 @@ import org.mifosplatform.accounting.rule.domain.AccountingRule; import org.mifosplatform.accounting.rule.domain.AccountingRuleRepository; import org.mifosplatform.accounting.rule.exception.AccountingRuleNotFoundException; 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.CommandProcessingResultBuilder; +import org.mifosplatform.infrastructure.core.data.DataValidatorBuilder; +import org.mifosplatform.infrastructure.core.exception.PlatformApiDataValidationException; import org.mifosplatform.infrastructure.core.exception.PlatformDataIntegrityException; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; import org.mifosplatform.organisation.office.domain.Office; @@ -279,11 +282,17 @@ public class JournalEntryWritePlatformServiceJpaRepositoryImpl implements Journa final Long officeId = journalEntries.get(0).getOffice().getId(); final String reversalTransactionId = generateTransactionId(officeId); final boolean manualEntry = true; + String reversalComment = command.stringValueOfParameterNamed("comments"); + final boolean useDefaultComment = StringUtils.isBlank(reversalComment); + + validateCommentForReversal(reversalComment); for (final JournalEntry journalEntry : journalEntries) { JournalEntry reversalJournalEntry; - final String reversalComment = "Reversal entry for Journal Entry with Entry Id :" + journalEntry.getId() - + " and transaction Id " + command.getTransactionId(); + if (useDefaultComment) { + reversalComment = "Reversal entry for Journal Entry with Entry Id :" + journalEntry.getId() + + " and transaction Id " + command.getTransactionId(); + } if (journalEntry.isDebitEntry()) { reversalJournalEntry = JournalEntry.createNew(journalEntry.getOffice(), journalEntry.getPaymentDetails(), journalEntry.getGlAccount(), journalEntry.getCurrencyCode(), reversalTransactionId, manualEntry, @@ -305,6 +314,17 @@ public class JournalEntryWritePlatformServiceJpaRepositoryImpl implements Journa return new CommandProcessingResultBuilder().withTransactionId(reversalTransactionId).build(); } + private void validateCommentForReversal(final String reversalComment) { + final List dataValidationErrors = new ArrayList<>(); + + final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("GLJournalEntry"); + + baseDataValidator.reset().parameter("comments").value(reversalComment).notExceedingLengthOf(500); + + if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", + "Validation errors exist.", dataValidationErrors); } + } + @Transactional @Override public void createJournalEntriesForLoan(final Map accountingBridgeData) {