diff --git a/mifosng-provider/src/main/java/org/mifosplatform/accounting/glaccount/domain/GLAccount.java b/mifosng-provider/src/main/java/org/mifosplatform/accounting/glaccount/domain/GLAccount.java index 527564160..106c6c56c 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/accounting/glaccount/domain/GLAccount.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/accounting/glaccount/domain/GLAccount.java @@ -62,7 +62,6 @@ public class GLAccount extends AbstractPersistable { @Column(name = "description", nullable = true, length = 500) private String description; - @SuppressWarnings("unused") @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "tag_id") private CodeValue tagId; @@ -106,6 +105,7 @@ public class GLAccount extends AbstractPersistable { handlePropertyUpdate(command, actualChanges, GLAccountJsonInputParams.PARENT_ID.getValue(), 0L); handlePropertyUpdate(command, actualChanges, GLAccountJsonInputParams.TYPE.getValue(), this.type, true); handlePropertyUpdate(command, actualChanges, GLAccountJsonInputParams.USAGE.getValue(), this.usage, true); + handlePropertyUpdate(command, actualChanges, GLAccountJsonInputParams.TAGID.getValue(), this.tagId==null?0L:this.tagId.getId()); return actualChanges; } @@ -224,4 +224,13 @@ public class GLAccount extends AbstractPersistable { public boolean isDetailAccount() { return GLAccountUsage.DETAIL.getValue().equals(this.usage); } + + public void updateTagId(CodeValue tagID) { + this.tagId = tagID; + } + + public void updateParentAccount(GLAccount parentAccount) { + this.parent=parentAccount; + this.generateHierarchy(); + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/accounting/glaccount/exception/GLAccountNotFoundException.java b/mifosng-provider/src/main/java/org/mifosplatform/accounting/glaccount/exception/GLAccountNotFoundException.java index a41bd592f..b0f590453 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/accounting/glaccount/exception/GLAccountNotFoundException.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/accounting/glaccount/exception/GLAccountNotFoundException.java @@ -13,6 +13,6 @@ import org.mifosplatform.infrastructure.core.exception.AbstractPlatformResourceN public class GLAccountNotFoundException extends AbstractPlatformResourceNotFoundException { public GLAccountNotFoundException(final Long id) { - super("error.msg.glaccount.id.invalid", "General Ledger account with identifier " + id + " does not exist", id); + super("error.msg.glaccount.id.invalid", "General Ledger account with identifier " + id + " does not exist ", id); } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/accounting/glaccount/exception/InvalidParentGLAccountHeadException.java b/mifosng-provider/src/main/java/org/mifosplatform/accounting/glaccount/exception/InvalidParentGLAccountHeadException.java new file mode 100644 index 000000000..353bbbb42 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/accounting/glaccount/exception/InvalidParentGLAccountHeadException.java @@ -0,0 +1,11 @@ +package org.mifosplatform.accounting.glaccount.exception; + +import org.mifosplatform.infrastructure.core.exception.AbstractPlatformDomainRuleException; + +public class InvalidParentGLAccountHeadException extends AbstractPlatformDomainRuleException { + + public InvalidParentGLAccountHeadException(Long glAccountId, Long parentId) { + super("error.msg.glaccount.id.and.parentid.must.not.same", "parentId:"+parentId+", id"+glAccountId+" should not be same", glAccountId, parentId); + } + +} diff --git a/mifosng-provider/src/main/java/org/mifosplatform/accounting/glaccount/service/GLAccountReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/accounting/glaccount/service/GLAccountReadPlatformServiceImpl.java index 8419094c4..45fabdd15 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/accounting/glaccount/service/GLAccountReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/accounting/glaccount/service/GLAccountReadPlatformServiceImpl.java @@ -63,7 +63,7 @@ public class GLAccountReadPlatformServiceImpl implements GLAccountReadPlatformSe final EnumOptionData usage = AccountingEnumerations.gLAccountUsage(usageId); final String description = rs.getString("description"); final String nameDecorated = rs.getString("nameDecorated"); - final Long codeId = rs.getLong("codeId"); + final Long codeId = rs.wasNull()? null : rs.getLong("codeId"); final String codeValue = rs.getString("codeValue"); CodeValueData tagId = CodeValueData.instance(codeId, codeValue); return new GLAccountData(id, name, parentId, glCode, disabled, manualEntriesAllowed, accountType, usage, description, diff --git a/mifosng-provider/src/main/java/org/mifosplatform/accounting/glaccount/service/GLAccountWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/accounting/glaccount/service/GLAccountWritePlatformServiceJpaRepositoryImpl.java index 232c21139..6b975c89b 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/accounting/glaccount/service/GLAccountWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/accounting/glaccount/service/GLAccountWritePlatformServiceJpaRepositoryImpl.java @@ -21,6 +21,7 @@ import org.mifosplatform.accounting.glaccount.exception.GLAccountInvalidParentEx import org.mifosplatform.accounting.glaccount.exception.GLAccountInvalidUpdateException; import org.mifosplatform.accounting.glaccount.exception.GLAccountInvalidUpdateException.GL_ACCOUNT_INVALID_UPDATE_REASON; import org.mifosplatform.accounting.glaccount.exception.GLAccountNotFoundException; +import org.mifosplatform.accounting.glaccount.exception.InvalidParentGLAccountHeadException; import org.mifosplatform.accounting.glaccount.serialization.GLAccountCommandFromApiJsonDeserializer; import org.mifosplatform.accounting.journalentry.domain.JournalEntry; import org.mifosplatform.accounting.journalentry.domain.JournalEntryRepository; @@ -75,31 +76,11 @@ public class GLAccountWritePlatformServiceJpaRepositoryImpl implements GLAccount final Long tagId = command.longValueOfParameterNamed(GLAccountJsonInputParams.TAGID.getValue()); final Long type = command.longValueOfParameterNamed(GLAccountJsonInputParams.TYPE.getValue()); GLAccountType accountType = GLAccountType.fromInt(type.intValue()); - if (accountType == null) { - // TODO: Vishwas - should throw appropriate class that extends - // AbstractPlatformResourceNotFoundException - throw new RuntimeException("Invalid GLAccount Type"); - } - - if (tagId != null) { - if (accountType.isAssetType()) { - glAccountTagType = this.codeValueRepositoryWrapper.findOneByCodeNameAndIdWithNotFoundDetection( - AccountingConstants.ASSESTS_TAG_OPTION_CODE_NAME, tagId); - } else if (accountType.isLiabilityType()) { - glAccountTagType = this.codeValueRepositoryWrapper.findOneByCodeNameAndIdWithNotFoundDetection( - AccountingConstants.LIABILITIES_TAG_OPTION_CODE_NAME, tagId); - } else if (accountType.isEquityType()) { - glAccountTagType = this.codeValueRepositoryWrapper.findOneByCodeNameAndIdWithNotFoundDetection( - AccountingConstants.EQUITY_TAG_OPTION_CODE_NAME, tagId); - } else if (accountType.isIncomeType()) { - glAccountTagType = this.codeValueRepositoryWrapper.findOneByCodeNameAndIdWithNotFoundDetection( - AccountingConstants.INCOME_TAG_OPTION_CODE_NAME, tagId); - } else if (accountType.isExpenseType()) { - glAccountTagType = this.codeValueRepositoryWrapper.findOneByCodeNameAndIdWithNotFoundDetection( - AccountingConstants.EXPENSES_TAG_OPTION_CODE_NAME, tagId); - } - } - + + if(tagId != null) { + glAccountTagType = retrieveTagId(tagId, accountType); + } + final GLAccount glAccount = GLAccount.fromJson(parentGLAccount, command, glAccountTagType); this.glAccountRepository.saveAndFlush(glAccount); @@ -122,6 +103,10 @@ public class GLAccountWritePlatformServiceJpaRepositoryImpl implements GLAccount final GLAccountCommand accountCommand = this.fromApiJsonDeserializer.commandFromApiJson(command.json()); accountCommand.validateForUpdate(); + final Long parentId = command.longValueOfParameterNamed(GLAccountJsonInputParams.PARENT_ID.getValue()); + if (glAccountId.equals(parentId)) { + throw new InvalidParentGLAccountHeadException(glAccountId,parentId); + } // is the glAccount valid final GLAccount glAccount = this.glAccountRepository.findOne(glAccountId); if (glAccount == null) { throw new GLAccountNotFoundException(glAccountId); } @@ -130,8 +115,18 @@ public class GLAccountWritePlatformServiceJpaRepositoryImpl implements GLAccount // is the new parent valid if (changesOnly.containsKey(GLAccountJsonInputParams.PARENT_ID.getValue())) { - final Long parentId = command.longValueOfParameterNamed(GLAccountJsonInputParams.PARENT_ID.getValue()); - validateParentGLAccount(parentId); + GLAccount parentAccount = validateParentGLAccount(parentId); + glAccount.updateParentAccount(parentAccount); + } + + if (changesOnly.containsKey(GLAccountJsonInputParams.TAGID.getValue())) { + final Long tagIdLongValue = command.longValueOfParameterNamed(GLAccountJsonInputParams.TAGID.getValue()); + final GLAccountType accountType = GLAccountType.fromInt(glAccount.getType()); + CodeValue tagID = null; + if (tagIdLongValue != null) { + tagID = retrieveTagId(tagIdLongValue, accountType); + } + glAccount.updateTagId(tagID); } /** @@ -184,11 +179,13 @@ public class GLAccountWritePlatformServiceJpaRepositoryImpl implements GLAccount * @return */ private GLAccount validateParentGLAccount(final Long parentAccountId) { - GLAccount parentGLAccount; - parentGLAccount = this.glAccountRepository.findOne(parentAccountId); - if (parentGLAccount == null) { throw new GLAccountNotFoundException(parentAccountId); } - // ensure parent is not a detail account - if (parentGLAccount.isDetailAccount()) { throw new GLAccountInvalidParentException(parentAccountId); } + GLAccount parentGLAccount = null; + if (parentAccountId != null){ + parentGLAccount = this.glAccountRepository.findOne(parentAccountId); + if (parentGLAccount == null) { throw new GLAccountNotFoundException(parentAccountId); } + // ensure parent is not a detail account + if (parentGLAccount.isDetailAccount()) { throw new GLAccountInvalidParentException(parentAccountId); } + } return parentGLAccount; } @@ -207,5 +204,26 @@ public class GLAccountWritePlatformServiceJpaRepositoryImpl implements GLAccount throw new PlatformDataIntegrityException("error.msg.glAccount.unknown.data.integrity.issue", "Unknown data integrity issue with resource GL Account: " + realCause.getMessage()); } + + private CodeValue retrieveTagId(Long tagId, GLAccountType accountType) { + CodeValue glAccountTagType = null; + if (accountType.isAssetType()) { + glAccountTagType = this.codeValueRepositoryWrapper.findOneByCodeNameAndIdWithNotFoundDetection( + AccountingConstants.ASSESTS_TAG_OPTION_CODE_NAME, tagId); + } else if (accountType.isLiabilityType()) { + glAccountTagType = this.codeValueRepositoryWrapper.findOneByCodeNameAndIdWithNotFoundDetection( + AccountingConstants.LIABILITIES_TAG_OPTION_CODE_NAME, tagId); + } else if (accountType.isEquityType()) { + glAccountTagType = this.codeValueRepositoryWrapper.findOneByCodeNameAndIdWithNotFoundDetection( + AccountingConstants.EQUITY_TAG_OPTION_CODE_NAME, tagId); + } else if (accountType.isIncomeType()) { + glAccountTagType = this.codeValueRepositoryWrapper.findOneByCodeNameAndIdWithNotFoundDetection( + AccountingConstants.INCOME_TAG_OPTION_CODE_NAME, tagId); + } else if (accountType.isExpenseType()) { + glAccountTagType = this.codeValueRepositoryWrapper.findOneByCodeNameAndIdWithNotFoundDetection( + AccountingConstants.EXPENSES_TAG_OPTION_CODE_NAME, tagId); + } + return glAccountTagType; + } }