From c3cc584c02fabefcc576a776cb994b8b0b6c4a78 Mon Sep 17 00:00:00 2001 From: goutham-M Date: Wed, 15 May 2013 15:24:21 +0530 Subject: [PATCH] Defining accounting rules for organisation Defining Accounting Rules Permissions for accounting rules --- ...0__add_permissions_for_accounting_rule.sql | 1 + .../rule/api/AccountingRuleApiResource.java | 31 ++++- .../rule/data/AccountingRuleData.java | 127 +++++++++++------- ...ingRuleCommandFromApiJsonDeserializer.java | 3 +- .../AccountingRuleReadPlatformService.java | 2 + ...AccountingRuleReadPlatformServiceImpl.java | 39 ++++-- ...WritePlatformServiceJpaRepositoryImpl.java | 6 +- .../commands/domain/CommandWrapper.java | 4 + .../SynchronousCommandProcessingService.java | 14 +- 9 files changed, 160 insertions(+), 67 deletions(-) create mode 100644 mifosng-db/migrations/core_db/V40__add_permissions_for_accounting_rule.sql diff --git a/mifosng-db/migrations/core_db/V40__add_permissions_for_accounting_rule.sql b/mifosng-db/migrations/core_db/V40__add_permissions_for_accounting_rule.sql new file mode 100644 index 000000000..9959cfd06 --- /dev/null +++ b/mifosng-db/migrations/core_db/V40__add_permissions_for_accounting_rule.sql @@ -0,0 +1 @@ +INSERT INTO `m_permission` (`grouping`, `code`, `entity_name`, `action_name`, `can_maker_checker`) VALUES ('organistion', 'DELETE_ACCOUNTINGRULE', 'ACCOUNTINGRULE', 'DELETE', 0),('organistion', 'CREATE_ACCOUNTINGRULE', 'ACCOUNTINGRULE', 'CREATE', 0),('organistion', 'UPDATE_ACCOUNTINGRULE', 'ACCOUNTINGRULE', 'UPDATE', 0); \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/accounting/rule/api/AccountingRuleApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/accounting/rule/api/AccountingRuleApiResource.java index 039768319..ed6ef7b06 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/accounting/rule/api/AccountingRuleApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/accounting/rule/api/AccountingRuleApiResource.java @@ -5,7 +5,6 @@ */ package org.mifosplatform.accounting.rule.api; -import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; @@ -75,6 +74,21 @@ public class AccountingRuleApiResource { this.officeReadPlatformService = officeReadPlatformService; this.accountReadPlatformService = accountReadPlatformService; } + + @GET + @Path("template") + @Consumes({ MediaType.APPLICATION_JSON }) + @Produces({ MediaType.APPLICATION_JSON }) + public String retrieveTemplate(@Context final UriInfo uriInfo) { + + context.authenticatedUser().validateHasReadPermission(resourceNameForPermission); + + AccountingRuleData accountingRuleData = this.accountingRuleReadPlatformService.retrieveNewAccountingRuleDetails(); + accountingRuleData = handleTemplate(accountingRuleData); + + final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper.process(uriInfo.getQueryParameters()); + return this.apiJsonSerializerService.serialize(settings, accountingRuleData, RESPONSE_DATA_PARAMETERS); + } @GET @Consumes({ MediaType.APPLICATION_JSON }) @@ -98,11 +112,9 @@ public class AccountingRuleApiResource { final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters()); - final AccountingRuleData accountingRuleData = this.accountingRuleReadPlatformService.retrieveAccountingRuleById(accountingRuleId); + AccountingRuleData accountingRuleData = this.accountingRuleReadPlatformService.retrieveAccountingRuleById(accountingRuleId); if (settings.isTemplate()) { - accountingRuleData.setAllowedOffices(new ArrayList(this.officeReadPlatformService.retrieveAllOfficesForDropdown())); - accountingRuleData.setAllowedAccounts(new ArrayList(this.accountReadPlatformService - .retrieveAllEnabledDetailGLAccounts())); + accountingRuleData = handleTemplate(accountingRuleData); } return this.apiJsonSerializerService.serialize(settings, accountingRuleData, RESPONSE_DATA_PARAMETERS); @@ -145,4 +157,13 @@ public class AccountingRuleApiResource { return this.apiJsonSerializerService.serialize(result); } + + private AccountingRuleData handleTemplate(AccountingRuleData accountingRuleData) { + List allowedAccounts = this.accountReadPlatformService.retrieveAllEnabledDetailGLAccounts(); + List allowedOffices = (List) this.officeReadPlatformService.retrieveAllOfficesForDropdown(); + if (accountingRuleData == null) { + return new AccountingRuleData(allowedAccounts, allowedOffices); + } + return new AccountingRuleData(accountingRuleData, allowedAccounts, allowedOffices); + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/accounting/rule/data/AccountingRuleData.java b/mifosng-provider/src/main/java/org/mifosplatform/accounting/rule/data/AccountingRuleData.java index 68e6b22bb..aab885c34 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/accounting/rule/data/AccountingRuleData.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/accounting/rule/data/AccountingRuleData.java @@ -19,58 +19,89 @@ import org.mifosplatform.organisation.office.data.OfficeData; */ public class AccountingRuleData { - @SuppressWarnings("unused") - private final Long id; - @SuppressWarnings("unused") - private final Long officeId; - @SuppressWarnings("unused") - private final String officeName; - @SuppressWarnings("unused") - private final Long accountToDebitId; - @SuppressWarnings("unused") - private final Long accountToCreditId; - @SuppressWarnings("unused") - private final String name; - @SuppressWarnings("unused") - private final String description; - @SuppressWarnings("unused") - private final boolean systemDefined; + private final Long id; + private final Long officeId; + private final String officeName; + private final Long accountToDebitId; + private final Long accountToCreditId; + private final String name; + private final String description; + private final boolean systemDefined; + private final String debitAccountName; + private final String creditAccountName; + private final String debitAccountGLCode; + private final String creditAccountGLCode; - // template - @SuppressWarnings("unused") - private List allowedOffices = new ArrayList(); - @SuppressWarnings("unused") - private List allowedAccounts = new ArrayList(); + // template + @SuppressWarnings("unused") + private List allowedOffices = new ArrayList(); + @SuppressWarnings("unused") + private List allowedAccounts = new ArrayList(); - public AccountingRuleData(Long id, Long accountToDebitId, Long accountToCreditId) { - this(id, null, null, accountToDebitId, accountToCreditId, null, null, true, null, null); - } + public AccountingRuleData(final Long id, final Long accountToDebitId, final Long accountToCreditId) { + this(id, null, null, accountToDebitId, accountToCreditId, null, null, true, null, null, null, null, null, null); + } - public AccountingRuleData(Long id, Long officeId, String officeName, Long accountToDebitId, Long accountToCreditId, String name, - String description, boolean systemDefined) { - this(id, officeId, officeName, accountToDebitId, accountToCreditId, name, description, systemDefined, null, null); - } + public AccountingRuleData(final Long id, final Long officeId, final String officeName, final Long accountToDebitId, + final Long accountToCreditId, final String name, final String description, final boolean systemDefined, + final String debitAccountName, final String creditAccountName, final String debitAccountGLCode, + final String creditAccountGLCode) { + this(id, officeId, officeName, accountToDebitId, accountToCreditId, name, description, systemDefined, debitAccountName, + creditAccountName, debitAccountGLCode, creditAccountGLCode, null, null); + } - public AccountingRuleData(Long id, Long officeId, String officeName, Long accountToDebitId, Long accountToCreditId, String name, - String description, boolean systemDefined, List allowedOffices, List allowedAccounts) { - this.id = id; - this.officeId = officeId; - this.officeName = officeName; - this.accountToDebitId = accountToDebitId; - this.accountToCreditId = accountToCreditId; - this.name = name; - this.description = description; - this.systemDefined = systemDefined; - this.allowedOffices = allowedOffices; - this.allowedAccounts = allowedAccounts; - } + public AccountingRuleData(final Long id, final Long officeId, final String officeName, final Long accountToDebitId, + final Long accountToCreditId, final String name, final String description, final boolean systemDefined, + final String debitAccountName, final String creditAccountName, final String debitAccountGLCode, + final String creditAccountGLCode, final List allowedOffices, final List allowedAccounts) { + this.id = id; + this.officeId = officeId; + this.officeName = officeName; + this.accountToDebitId = accountToDebitId; + this.accountToCreditId = accountToCreditId; + this.name = name; + this.description = description; + this.systemDefined = systemDefined; + this.allowedOffices = allowedOffices; + this.allowedAccounts = allowedAccounts; + this.debitAccountName = debitAccountName; + this.creditAccountName = creditAccountName; + this.debitAccountGLCode = debitAccountGLCode; + this.creditAccountGLCode = creditAccountGLCode; + } - public void setAllowedOffices(List allowedOffices) { - this.allowedOffices = allowedOffices; - } - - public void setAllowedAccounts(List allowedAccounts) { - this.allowedAccounts = allowedAccounts; - } + public AccountingRuleData(final AccountingRuleData accountingRuleData, + final List allowedAccounts, final List allowedOffices) { + this.id = accountingRuleData.id; + this.officeId = accountingRuleData.officeId; + this.officeName = accountingRuleData.officeName; + this.accountToDebitId = accountingRuleData.accountToDebitId; + this.accountToCreditId = accountingRuleData.accountToCreditId; + this.name = accountingRuleData.name; + this.description = accountingRuleData.description; + this.systemDefined = accountingRuleData.systemDefined; + this.allowedOffices = allowedOffices; + this.allowedAccounts = allowedAccounts; + this.debitAccountName = accountingRuleData.debitAccountName; + this.creditAccountName = accountingRuleData.creditAccountName; + this.debitAccountGLCode = accountingRuleData.debitAccountGLCode; + this.creditAccountGLCode = accountingRuleData.creditAccountGLCode; + } + public AccountingRuleData(final List allowedAccounts, final List allowedOffices) { + this.id = null; + this.officeId = null; + this.officeName = null; + this.accountToDebitId = null; + this.accountToCreditId = null; + this.name = null; + this.description = null; + this.systemDefined = false; + this.allowedOffices = allowedOffices; + this.allowedAccounts = allowedAccounts; + this.debitAccountName = null; + this.creditAccountName = null; + this.debitAccountGLCode = null; + this.creditAccountGLCode = null; + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/accounting/rule/serialization/AccountingRuleCommandFromApiJsonDeserializer.java b/mifosng-provider/src/main/java/org/mifosplatform/accounting/rule/serialization/AccountingRuleCommandFromApiJsonDeserializer.java index 56192ff22..07c2a0be1 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/accounting/rule/serialization/AccountingRuleCommandFromApiJsonDeserializer.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/accounting/rule/serialization/AccountingRuleCommandFromApiJsonDeserializer.java @@ -10,7 +10,6 @@ import java.util.Map; import java.util.Set; import org.apache.commons.lang.StringUtils; -import org.mifosplatform.accounting.closure.api.GLClosureJsonInputParams; import org.mifosplatform.accounting.rule.api.AccountingRuleJsonInputParams; import org.mifosplatform.accounting.rule.command.AccountingRuleCommand; import org.mifosplatform.infrastructure.core.exception.InvalidJsonException; @@ -43,7 +42,7 @@ public final class AccountingRuleCommandFromApiJsonDeserializer extends Abstract if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); } final Type typeOfMap = new TypeToken>() {}.getType(); - final Set supportedParameters = GLClosureJsonInputParams.getAllValues(); + final Set supportedParameters = AccountingRuleJsonInputParams.getAllValues(); this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParameters); final JsonElement element = this.fromApiJsonHelper.parse(json); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/accounting/rule/service/AccountingRuleReadPlatformService.java b/mifosng-provider/src/main/java/org/mifosplatform/accounting/rule/service/AccountingRuleReadPlatformService.java index d33b7fef7..4c87e1cf1 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/accounting/rule/service/AccountingRuleReadPlatformService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/accounting/rule/service/AccountingRuleReadPlatformService.java @@ -15,4 +15,6 @@ public interface AccountingRuleReadPlatformService { AccountingRuleData retrieveAccountingRuleById(long accountingRuleId); + AccountingRuleData retrieveNewAccountingRuleDetails(); + } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/accounting/rule/service/AccountingRuleReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/accounting/rule/service/AccountingRuleReadPlatformServiceImpl.java index 7324bf630..89b3d6b10 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/accounting/rule/service/AccountingRuleReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/accounting/rule/service/AccountingRuleReadPlatformServiceImpl.java @@ -35,10 +35,17 @@ public class AccountingRuleReadPlatformServiceImpl implements AccountingRuleRead public AccountingRuleMapper() { final StringBuilder sqlBuilder = new StringBuilder(400); - sqlBuilder.append("select rule.id as id,rule.name as name, rule.office_id as officeId,office.name as officeName,") + sqlBuilder.append(" rule.id as id,rule.name as name, rule.office_id as officeId,office.name as officeName,") .append(" rule.debit_account_id as debitAccountId,rule.credit_account_id as creditAccountId,") - .append(" rule.description as description, rule.system_defined as systemDefined ").append("from ") - .append(" acc_accounting_rule rule left join m_office office on rule.office_id=office.id"); + .append(" rule.description as description, rule.system_defined as systemDefined, ") + .append("debitAccount.glName as debitAccountName, debitAccount.glCode as debitAccountGLCode,") + .append("creditAccount.glName as creditAccountName, creditAccount.glCode as creditAccountGLCode ") + .append("from (Select gl.id as id,gl.name as glName, gl.gl_code as glCode from acc_gl_account gl,acc_accounting_rule rule ") + .append("where gl.id=rule.debit_account_id ) as debitAccount,") + .append("(Select gl.id as id,gl.name as glName, gl.gl_code as glCode from acc_gl_account gl,acc_accounting_rule rule ") + .append("where gl.id=rule.credit_account_id ) as creditAccount,") + .append("acc_accounting_rule rule left join m_office office on rule.office_id=office.id ") + .append("where debitAccount.id=rule.debit_account_id and creditAccount.id = rule.credit_account_id"); this.schemaSql = sqlBuilder.toString(); } @@ -57,17 +64,27 @@ public class AccountingRuleReadPlatformServiceImpl implements AccountingRuleRead final Long accountToDebitId = rs.getLong("debitAccountId"); final Long accountToCreditId = rs.getLong("creditAccountId"); final boolean systemDefined = rs.getBoolean("systemDefined"); + final String debitAccountName = rs.getString("debitAccountName"); + final String creditAccountName = rs.getString("creditAccountName"); + final String debitAccountGLCode = rs.getString("debitAccountGLCode"); + final String creditAccountGLCode = rs.getString("creditAccountGLCode"); - return new AccountingRuleData(id, officeId, officeName, accountToDebitId, accountToCreditId, name, description, systemDefined); + return new AccountingRuleData(id, officeId, officeName, accountToDebitId, accountToCreditId, name, description, systemDefined, + debitAccountName, creditAccountName, debitAccountGLCode, creditAccountGLCode); } } @Override public List retrieveAllAccountingRules(Long officeId) { final AccountingRuleMapper rm = new AccountingRuleMapper(); - String sql = "select " + rm.schema() + " where system_defined=0 and office.id = ?"; - sql = sql + " order by rule.id asc"; - return this.jdbcTemplate.query(sql, rm, new Object[] { officeId }); + if(officeId != null) { + String sql = "select " + rm.schema() + " where system_defined=0 and office.id = ?"; + sql = sql + " order by rule.id asc"; + return this.jdbcTemplate.query(sql, rm, new Object[] { officeId }); + } + String sql = "select " + rm.schema() + " and system_defined=0 "; + sql = sql + " order by rule.id asc"; + return this.jdbcTemplate.query(sql, rm, new Object[] {}); } @Override @@ -75,7 +92,7 @@ public class AccountingRuleReadPlatformServiceImpl implements AccountingRuleRead try { final AccountingRuleMapper rm = new AccountingRuleMapper(); - final String sql = "select " + rm.schema() + " and glClosure.id = ?"; + final String sql = "select " + rm.schema() + " and rule.id = ?"; final AccountingRuleData accountingRuleData = this.jdbcTemplate.queryForObject(sql, rm, new Object[] { accountingRuleId }); return accountingRuleData; @@ -84,4 +101,10 @@ public class AccountingRuleReadPlatformServiceImpl implements AccountingRuleRead } } + @Override + public AccountingRuleData retrieveNewAccountingRuleDetails() { + // Modify if any default values required + return null; + } + } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/accounting/rule/service/AccountingRuleWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/accounting/rule/service/AccountingRuleWritePlatformServiceJpaRepositoryImpl.java index 608c07f6c..f034e06ec 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/accounting/rule/service/AccountingRuleWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/accounting/rule/service/AccountingRuleWritePlatformServiceJpaRepositoryImpl.java @@ -123,17 +123,17 @@ public class AccountingRuleWritePlatformServiceJpaRepositoryImpl implements Acco final AccountingRule accountingRule = this.accountingRuleRepositoryWrapper.findOneWithNotFoundDetection(accountingRuleId); final Map changesOnly = accountingRule.update(command); - if (changesOnly.containsKey(AccountingRuleJsonInputParams.ACCOUNT_TO_DEBIT.getValue())) { + if (accountToDebitId != null && changesOnly.containsKey(AccountingRuleJsonInputParams.ACCOUNT_TO_DEBIT.getValue())) { final GLAccount accountToDebit = accountRepositoryWrapper.findOneWithNotFoundDetection(accountToDebitId); accountingRule.setAccountToDebit(accountToDebit); } - if (changesOnly.containsKey(AccountingRuleJsonInputParams.ACCOUNT_TO_CREDIT.getValue())) { + if (accountToCreditId != null && changesOnly.containsKey(AccountingRuleJsonInputParams.ACCOUNT_TO_CREDIT.getValue())) { final GLAccount accountToCredit = accountRepositoryWrapper.findOneWithNotFoundDetection(accountToCreditId); accountingRule.setAccountToCredit(accountToCredit); } - if (changesOnly.containsKey(AccountingRuleJsonInputParams.OFFICE_ID.getValue())) { + if (officeId != null && changesOnly.containsKey(AccountingRuleJsonInputParams.OFFICE_ID.getValue())) { final Office userOffice = this.officeRepository.findOne(officeId); if (userOffice == null) { throw new OfficeNotFoundException(officeId); } accountingRule.setOffice(userOffice); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/commands/domain/CommandWrapper.java b/mifosng-provider/src/main/java/org/mifosplatform/commands/domain/CommandWrapper.java index 6236f5b83..ef609f991 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/commands/domain/CommandWrapper.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/commands/domain/CommandWrapper.java @@ -512,4 +512,8 @@ public class CommandWrapper { public boolean isDisassociateClients() { return this.actionName.equalsIgnoreCase("DISASSOCIATECLIENTS"); } + + public boolean isAccountingRuleResource() { + return this.entityName.equalsIgnoreCase("ACCOUNTINGRULE"); + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/SynchronousCommandProcessingService.java b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/SynchronousCommandProcessingService.java index b609f4c47..a65f0c1a9 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/SynchronousCommandProcessingService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/SynchronousCommandProcessingService.java @@ -443,7 +443,19 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer throw new UnsupportedCommandException(wrapper.commandName()); } - } else { + } else if (wrapper.isAccountingRuleResource()) { + + if (wrapper.isCreate()) { + handler = applicationContext.getBean("createAccountingRuleCommandHandler", NewCommandSourceHandler.class); + } else if (wrapper.isUpdate()) { + handler = applicationContext.getBean("updateAccountingRuleCommandHandler", NewCommandSourceHandler.class); + } else if (wrapper.isDelete()) { + handler = applicationContext.getBean("deleteAccountingRuleCommandHandler", NewCommandSourceHandler.class); + } else { + throw new UnsupportedCommandException(wrapper.commandName()); + } + + }else { throw new UnsupportedCommandException(wrapper.commandName()); }