Add multiple credit and debit journalentries with accoutnting rule

This commit is contained in:
mifoscontributer
2013-05-31 18:53:22 +05:30
parent eb082d5ec5
commit b373458f57
7 changed files with 132 additions and 71 deletions
@@ -50,9 +50,10 @@ import org.springframework.stereotype.Component;
public class GLAccountsApiResource {
private static final Set<String> RESPONSE_DATA_PARAMETERS = new HashSet<String>(Arrays.asList("id", "name", "parentId", "glCode",
"disabled", "manualEntriesAllowed", "type", "usage", "description", "assetHeaderAccountOptions", "liabilityHeaderAccountOptions",
"equityHeaderAccountOptions", "incomeHeaderAccountOptions", "expenseHeaderAccountOptions", "nameDecorated", "tagId",
"allowedAssetsTagOptions", "allowedLiabilitiesTagOptions", "allowedEquityTagOptions", "allowedIncomeTagOptions", "allowedExpensesTagOptions"));
"disabled", "manualEntriesAllowed", "type", "usage", "description", "assetHeaderAccountOptions",
"liabilityHeaderAccountOptions", "equityHeaderAccountOptions", "incomeHeaderAccountOptions", "expenseHeaderAccountOptions",
"nameDecorated", "tagId", "allowedAssetsTagOptions", "allowedLiabilitiesTagOptions", "allowedEquityTagOptions",
"allowedIncomeTagOptions", "allowedExpensesTagOptions", "creditAccounts", "debitAccounts"));
private final String resourceNameForPermission = "GLACCOUNT";
@@ -63,12 +64,13 @@ public class GLAccountsApiResource {
private final PlatformSecurityContext context;
private final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService;
private final CodeValueReadPlatformService codeValueReadPlatformService;
@Autowired
public GLAccountsApiResource(final PlatformSecurityContext context, final GLAccountReadPlatformService glAccountReadPlatformService,
final DefaultToApiJsonSerializer<GLAccountData> toApiJsonSerializer, final ApiRequestParameterHelper apiRequestParameterHelper,
final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService,
final AccountingDropdownReadPlatformService dropdownReadPlatformService, final CodeValueReadPlatformService codeValueReadPlatformService) {
final AccountingDropdownReadPlatformService dropdownReadPlatformService,
final CodeValueReadPlatformService codeValueReadPlatformService) {
this.context = context;
this.apiRequestParameterHelper = apiRequestParameterHelper;
this.commandsSourceWritePlatformService = commandsSourceWritePlatformService;
@@ -82,7 +84,7 @@ public class GLAccountsApiResource {
@Path("template")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public String retrieveNewAccountDetails(@Context final UriInfo uriInfo, @QueryParam("type") final Integer type) {
public String retrieveNewAccountDetails(@Context final UriInfo uriInfo, @QueryParam("type") final Integer type) {
this.context.authenticatedUser().validateHasReadPermission(this.resourceNameForPermission);
@@ -167,22 +169,32 @@ public class GLAccountsApiResource {
private GLAccountData handleTemplate(final GLAccountData glAccountData) {
final List<EnumOptionData> accountTypeOptions = this.dropdownReadPlatformService.retrieveGLAccountTypeOptions();
final List<EnumOptionData> usageOptions = this.dropdownReadPlatformService.retrieveGLAccountUsageOptions();
final List<GLAccountData> assetHeaderAccountOptions = defaultIfEmpty(this.glAccountReadPlatformService.retrieveAllEnabledHeaderGLAccounts(GLAccountType.ASSET));
final List<GLAccountData> liabilityHeaderAccountOptions = defaultIfEmpty(this.glAccountReadPlatformService.retrieveAllEnabledHeaderGLAccounts(GLAccountType.LIABILITY));
final List<GLAccountData> equityHeaderAccountOptions = defaultIfEmpty(this.glAccountReadPlatformService.retrieveAllEnabledHeaderGLAccounts(GLAccountType.EQUITY));
final List<GLAccountData> incomeHeaderAccountOptions = defaultIfEmpty(this.glAccountReadPlatformService.retrieveAllEnabledHeaderGLAccounts(GLAccountType.INCOME));
final List<GLAccountData> expenseHeaderAccountOptions = defaultIfEmpty(this.glAccountReadPlatformService.retrieveAllEnabledHeaderGLAccounts(GLAccountType.EXPENSE));
final Collection<CodeValueData> allowedAssetsTagOptions = this.codeValueReadPlatformService.retrieveCodeValuesByCode(AccountingConstants.ASSESTS_TAG_OPTION_CODE_NAME);
final Collection<CodeValueData> allowedLiabilitiesTagOptions = this.codeValueReadPlatformService.retrieveCodeValuesByCode(AccountingConstants.LIABILITIES_TAG_OPTION_CODE_NAME);
final Collection<CodeValueData> allowedEquityTagOptions = this.codeValueReadPlatformService.retrieveCodeValuesByCode(AccountingConstants.EQUITY_TAG_OPTION_CODE_NAME);
final Collection<CodeValueData> allowedIncomeTagOptions = this.codeValueReadPlatformService.retrieveCodeValuesByCode(AccountingConstants.INCOME_TAG_OPTION_CODE_NAME);
final Collection<CodeValueData> allowedExpensesTagOptions = this.codeValueReadPlatformService.retrieveCodeValuesByCode(AccountingConstants.EXPENSES_TAG_OPTION_CODE_NAME);
final List<GLAccountData> assetHeaderAccountOptions = defaultIfEmpty(this.glAccountReadPlatformService
.retrieveAllEnabledHeaderGLAccounts(GLAccountType.ASSET));
final List<GLAccountData> liabilityHeaderAccountOptions = defaultIfEmpty(this.glAccountReadPlatformService
.retrieveAllEnabledHeaderGLAccounts(GLAccountType.LIABILITY));
final List<GLAccountData> equityHeaderAccountOptions = defaultIfEmpty(this.glAccountReadPlatformService
.retrieveAllEnabledHeaderGLAccounts(GLAccountType.EQUITY));
final List<GLAccountData> incomeHeaderAccountOptions = defaultIfEmpty(this.glAccountReadPlatformService
.retrieveAllEnabledHeaderGLAccounts(GLAccountType.INCOME));
final List<GLAccountData> expenseHeaderAccountOptions = defaultIfEmpty(this.glAccountReadPlatformService
.retrieveAllEnabledHeaderGLAccounts(GLAccountType.EXPENSE));
final Collection<CodeValueData> allowedAssetsTagOptions = this.codeValueReadPlatformService
.retrieveCodeValuesByCode(AccountingConstants.ASSESTS_TAG_OPTION_CODE_NAME);
final Collection<CodeValueData> allowedLiabilitiesTagOptions = this.codeValueReadPlatformService
.retrieveCodeValuesByCode(AccountingConstants.LIABILITIES_TAG_OPTION_CODE_NAME);
final Collection<CodeValueData> allowedEquityTagOptions = this.codeValueReadPlatformService
.retrieveCodeValuesByCode(AccountingConstants.EQUITY_TAG_OPTION_CODE_NAME);
final Collection<CodeValueData> allowedIncomeTagOptions = this.codeValueReadPlatformService
.retrieveCodeValuesByCode(AccountingConstants.INCOME_TAG_OPTION_CODE_NAME);
final Collection<CodeValueData> allowedExpensesTagOptions = this.codeValueReadPlatformService
.retrieveCodeValuesByCode(AccountingConstants.EXPENSES_TAG_OPTION_CODE_NAME);
return new GLAccountData(glAccountData, accountTypeOptions, usageOptions, assetHeaderAccountOptions, liabilityHeaderAccountOptions,
equityHeaderAccountOptions, incomeHeaderAccountOptions, expenseHeaderAccountOptions, allowedAssetsTagOptions,
equityHeaderAccountOptions, incomeHeaderAccountOptions, expenseHeaderAccountOptions, allowedAssetsTagOptions,
allowedLiabilitiesTagOptions, allowedEquityTagOptions, allowedIncomeTagOptions, allowedExpensesTagOptions);
}
private List<GLAccountData> defaultIfEmpty(final List<GLAccountData> list) {
List<GLAccountData> returnList = null;
if (list != null && !list.isEmpty()) {
@@ -190,4 +202,4 @@ public class GLAccountsApiResource {
}
return returnList;
}
}
}
@@ -48,6 +48,10 @@ public class GLAccountData {
final Collection<CodeValueData> allowedIncomeTagOptions;
final Collection<CodeValueData> allowedExpensesTagOptions;
// for predefined journal entry template
private final List<GLAccountDataForLookup> creditAccounts;
private final List<GLAccountDataForLookup> debitAccounts;
public GLAccountData(final Long id, final String name, final Long parentId, final String glCode, final boolean disabled,
final boolean manualEntriesAllowed, final EnumOptionData type, final EnumOptionData usage, final String description,
final String nameDecorated, final CodeValueData tagId) {
@@ -71,17 +75,19 @@ public class GLAccountData {
this.expenseHeaderAccountOptions = null;
this.allowedAssetsTagOptions = null;
this.allowedLiabilitiesTagOptions = null;
this.allowedEquityTagOptions=null;
this.allowedIncomeTagOptions=null;
this.allowedExpensesTagOptions=null;
this.allowedEquityTagOptions = null;
this.allowedIncomeTagOptions = null;
this.allowedExpensesTagOptions = null;
this.creditAccounts = null;
this.debitAccounts = null;
}
public GLAccountData(final GLAccountData accountData, final List<EnumOptionData> accountTypeOptions,
final List<EnumOptionData> usageOptions, final List<GLAccountData> assetHeaderAccountOptions,
final List<GLAccountData> liabilityHeaderAccountOptions, final List<GLAccountData> equityHeaderAccountOptions,
final List<GLAccountData> incomeHeaderAccountOptions, final List<GLAccountData> expenseHeaderAccountOptions,
final Collection<CodeValueData> allowedAssetsTagOptions, final Collection<CodeValueData> allowedLiabilitiesTagOptions,
final Collection<CodeValueData> allowedEquityTagOptions, final Collection<CodeValueData> allowedIncomeTagOptions,
final List<GLAccountData> incomeHeaderAccountOptions, final List<GLAccountData> expenseHeaderAccountOptions,
final Collection<CodeValueData> allowedAssetsTagOptions, final Collection<CodeValueData> allowedLiabilitiesTagOptions,
final Collection<CodeValueData> allowedEquityTagOptions, final Collection<CodeValueData> allowedIncomeTagOptions,
final Collection<CodeValueData> allowedExpensesTagOptions) {
this.id = accountData.id;
this.name = accountData.name;
@@ -106,6 +112,8 @@ public class GLAccountData {
this.allowedEquityTagOptions = allowedEquityTagOptions;
this.allowedIncomeTagOptions = allowedIncomeTagOptions;
this.allowedExpensesTagOptions = allowedExpensesTagOptions;
this.creditAccounts = accountData.creditAccounts;
this.debitAccounts = accountData.debitAccounts;
}
public static GLAccountData sensibleDefaultsForNewGLAccountCreation(final Integer glAccType) {
@@ -116,19 +124,20 @@ public class GLAccountData {
final boolean disabled = false;
final boolean manualEntriesAllowed = true;
final EnumOptionData type;
if(glAccType != null && glAccType >= GLAccountType.getMinValue() && glAccType <= GLAccountType.getMaxValue())
if (glAccType != null && glAccType >= GLAccountType.getMinValue() && glAccType <= GLAccountType.getMaxValue()) {
type = AccountingEnumerations.gLAccountType(glAccType);
else
} else {
type = AccountingEnumerations.gLAccountType(GLAccountType.ASSET);
}
final EnumOptionData usage = AccountingEnumerations.gLAccountUsage(GLAccountUsage.DETAIL);
final String description = null;
final String nameDecorated = null;
final CodeValueData tagId = null;
return new GLAccountData(id, name, parentId, glCode, disabled, manualEntriesAllowed, type, usage, description, nameDecorated, tagId);
}
public GLAccountData(final Long id, final String name, final String glCode) {
this.id = id;
this.id = id;
this.name = name;
this.parentId = null;
this.glCode = glCode;
@@ -148,23 +157,23 @@ public class GLAccountData {
this.expenseHeaderAccountOptions = null;
this.allowedAssetsTagOptions = null;
this.allowedLiabilitiesTagOptions = null;
this.allowedEquityTagOptions=null;
this.allowedIncomeTagOptions=null;
this.allowedExpensesTagOptions=null;
}
this.allowedEquityTagOptions = null;
this.allowedIncomeTagOptions = null;
this.allowedExpensesTagOptions = null;
this.creditAccounts = null;
this.debitAccounts = null;
}
public Long getId() {
return this.id;
}
public Long getId() {
return this.id;
}
public String getName() {
return this.name;
}
public String getName() {
return this.name;
}
public String getGlCode() {
return this.glCode;
}
public String getGlCode() {
return this.glCode;
}
}
@@ -1,15 +1,21 @@
package org.mifosplatform.accounting.glaccount.data;
public class GLAccountDataForLookup {
private final Long id;
public GLAccountDataForLookup(final Long id) {
this.id = id;
}
public Long getId() {
return this.id;
}
private final Long id;
@SuppressWarnings("unused")
private final String name;
@SuppressWarnings("unused")
private final String glCode;
public GLAccountDataForLookup(final Long id, final String name, final String glCode) {
this.id = id;
this.name = name;
this.glCode = glCode;
}
public Long getId() {
return this.id;
}
}
@@ -214,13 +214,15 @@ public class GLAccountReadPlatformServiceImpl implements GLAccountReadPlatformSe
private static final class GLAccountDataLookUpMapper implements RowMapper<GLAccountDataForLookup> {
public String schema() {
return " gl.id as id from acc_accounting_rule rule join acc_rule_tags tags on tags.acc_rule_id = rule.id join acc_gl_account gl on gl.tag_id=tags.tag_id";
return " gl.id as id, gl.name as name, gl.gl_code as glCode from acc_accounting_rule rule join acc_rule_tags tags on tags.acc_rule_id = rule.id join acc_gl_account gl on gl.tag_id=tags.tag_id";
}
@Override
public GLAccountDataForLookup mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException {
final Long id = JdbcSupport.getLong(rs, "id");
return new GLAccountDataForLookup(id);
final String name = rs.getString("name");
final String glCode = rs.getString("glCode");
return new GLAccountDataForLookup(id, name, glCode);
}
}
@@ -53,7 +53,7 @@ public class AccountingRuleApiResource {
private static final Set<String> RESPONSE_DATA_PARAMETERS = new HashSet<String>(Arrays.asList("id", "officeId", "officeName",
"accountToDebitId", "accountToCreditId", "name", "description", "systemDefined", "allowedAssetsTagOptions",
"allowedLiabilitiesTagOptions", "allowedEquityTagOptions", "allowedIncomeTagOptions", "allowedExpensesTagOptions", "debitTags",
"creditTags"));
"creditTags", "creditAccounts", "debitAccounts"));
private final String resourceNameForPermission = "ACCOUNTINGRULE";
@@ -10,6 +10,7 @@ import java.util.Collection;
import java.util.List;
import org.mifosplatform.accounting.glaccount.data.GLAccountData;
import org.mifosplatform.accounting.glaccount.data.GLAccountDataForLookup;
import org.mifosplatform.infrastructure.codes.data.CodeValueData;
import org.mifosplatform.organisation.office.data.OfficeData;
@@ -40,6 +41,8 @@ public class AccountingRuleData {
private final Collection<CodeValueData> allowedEquityTagOptions;
private final Collection<CodeValueData> allowedIncomeTagOptions;
private final Collection<CodeValueData> allowedExpensesTagOptions;
private final List<GLAccountDataForLookup> creditAccounts;
private final List<GLAccountDataForLookup> debitAccounts;
public AccountingRuleData(final AccountingRuleData accountingRuleData, final List<GLAccountData> allowedAccounts,
final List<OfficeData> allowedOffices, final Collection<CodeValueData> allowedAssetsTagOptions,
@@ -62,6 +65,8 @@ public class AccountingRuleData {
this.allowedExpensesTagOptions = allowedExpensesTagOptions;
this.creditTags = accountingRuleData.creditTags;
this.debitTags = accountingRuleData.debitTags;
this.creditAccounts = accountingRuleData.creditAccounts;
this.debitAccounts = accountingRuleData.debitAccounts;
}
public AccountingRuleData(final List<GLAccountData> allowedAccounts, final List<OfficeData> allowedOffices,
@@ -85,6 +90,8 @@ public class AccountingRuleData {
this.allowedExpensesTagOptions = allowedExpensesTagOptions;
this.creditTags = null;
this.debitTags = null;
this.creditAccounts = null;
this.debitAccounts = null;
}
public AccountingRuleData(final Long id, final Long officeId, final String officeName, final String name, final String description,
@@ -106,10 +113,13 @@ public class AccountingRuleData {
this.allowedExpensesTagOptions = null;
this.creditTags = null;
this.debitTags = null;
this.creditAccounts = new ArrayList<GLAccountDataForLookup>();
this.debitAccounts = new ArrayList<GLAccountDataForLookup>();
}
public AccountingRuleData(final AccountingRuleData accountingRuleData, final List<AccountingTagRuleData> creditTags,
final List<AccountingTagRuleData> debitTags) {
final List<GLAccountDataForLookup> creditAccounts, final List<AccountingTagRuleData> debitTags,
final List<GLAccountDataForLookup> debitAccounts) {
this.id = accountingRuleData.id;
this.officeId = accountingRuleData.officeId;
this.officeName = accountingRuleData.officeName;
@@ -126,7 +136,9 @@ public class AccountingRuleData {
this.allowedIncomeTagOptions = accountingRuleData.allowedIncomeTagOptions;
this.allowedExpensesTagOptions = accountingRuleData.allowedExpensesTagOptions;
this.creditTags = creditTags;
this.creditAccounts = creditAccounts;
this.debitTags = debitTags;
this.debitAccounts = debitAccounts;
}
public GLAccountData getDebitAccountHead() {
@@ -144,4 +156,12 @@ public class AccountingRuleData {
public List<AccountingTagRuleData> getDebitTags() {
return this.debitTags;
}
public List<GLAccountDataForLookup> getCreditAccounts() {
return this.creditAccounts;
}
public List<GLAccountDataForLookup> getDebitAccounts() {
return this.debitAccounts;
}
}
@@ -14,6 +14,8 @@ import java.util.Map;
import org.mifosplatform.accounting.common.AccountingEnumerations;
import org.mifosplatform.accounting.glaccount.data.GLAccountData;
import org.mifosplatform.accounting.glaccount.data.GLAccountDataForLookup;
import org.mifosplatform.accounting.glaccount.service.GLAccountReadPlatformService;
import org.mifosplatform.accounting.journalentry.domain.JournalEntryType;
import org.mifosplatform.accounting.rule.data.AccountingRuleData;
import org.mifosplatform.accounting.rule.data.AccountingTagRuleData;
@@ -33,10 +35,13 @@ import org.springframework.stereotype.Service;
public class AccountingRuleReadPlatformServiceImpl implements AccountingRuleReadPlatformService {
private final JdbcTemplate jdbcTemplate;
private final GLAccountReadPlatformService glAccountReadPlatformService;
@Autowired
public AccountingRuleReadPlatformServiceImpl(final TenantAwareRoutingDataSource dataSource) {
public AccountingRuleReadPlatformServiceImpl(final TenantAwareRoutingDataSource dataSource,
final GLAccountReadPlatformService glAccountReadPlatformService) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.glAccountReadPlatformService = glAccountReadPlatformService;
}
// Will remove the old one if everything goes well.
@@ -88,13 +93,15 @@ public class AccountingRuleReadPlatformServiceImpl implements AccountingRuleRead
}
}*/
private static final class AccountingRuleDataExtractor implements ResultSetExtractor<Object> {
private static final class AccountingRuleDataExtractor implements ResultSetExtractor<Map<Long, AccountingRuleData>> {
private final String schemaSql;
private final JdbcTemplate jdbcTemplate;
private final GLAccountReadPlatformService glAccountReadPlatformService;
public AccountingRuleDataExtractor(final JdbcTemplate jdbcTemplate) {
public AccountingRuleDataExtractor(final JdbcTemplate jdbcTemplate, final GLAccountReadPlatformService glAccountReadPlatformService) {
this.jdbcTemplate = jdbcTemplate;
this.glAccountReadPlatformService = glAccountReadPlatformService;
final StringBuilder sqlBuilder = new StringBuilder(400);
sqlBuilder
.append(" rule.id as id,rule.name as name, rule.office_id as officeId,office.name as officeName,")
@@ -141,11 +148,17 @@ public class AccountingRuleReadPlatformServiceImpl implements AccountingRuleRead
}
if (accountingRuleData.getCreditAccountHead() == null) {
final List<AccountingTagRuleData> creditTags = getCreditOrDebitTags(id, JournalEntryType.CREDIT.getValue());
accountingRuleData = new AccountingRuleData(accountingRuleData, creditTags, accountingRuleData.getDebitTags());
final List<GLAccountDataForLookup> creditAccounts = this.glAccountReadPlatformService.retrieveAccountsByTagId(id,
JournalEntryType.CREDIT.getValue());
accountingRuleData = new AccountingRuleData(accountingRuleData, creditTags, creditAccounts,
accountingRuleData.getDebitTags(), accountingRuleData.getDebitAccounts());
}
if (accountingRuleData.getDebitAccountHead() == null) {
final List<AccountingTagRuleData> debitTags = getCreditOrDebitTags(id, JournalEntryType.DEBIT.getValue());
accountingRuleData = new AccountingRuleData(accountingRuleData, accountingRuleData.getCreditTags(), debitTags);
final List<GLAccountDataForLookup> debitAccounts = this.glAccountReadPlatformService.retrieveAccountsByTagId(id,
JournalEntryType.DEBIT.getValue());
accountingRuleData = new AccountingRuleData(accountingRuleData, accountingRuleData.getCreditTags(),
accountingRuleData.getCreditAccounts(), debitTags, debitAccounts);
}
extractedData.put(id, accountingRuleData);
}
@@ -162,7 +175,8 @@ public class AccountingRuleReadPlatformServiceImpl implements AccountingRuleRead
@Override
public List<AccountingRuleData> retrieveAllAccountingRules(final Long officeId) {
final AccountingRuleDataExtractor resultSetExtractor = new AccountingRuleDataExtractor(this.jdbcTemplate);
final AccountingRuleDataExtractor resultSetExtractor = new AccountingRuleDataExtractor(this.jdbcTemplate,
this.glAccountReadPlatformService);
Object[] arguments = new Object[] {};
String sql = "select " + resultSetExtractor.schema() + " and system_defined=0 ";
if (officeId != null) {
@@ -170,21 +184,19 @@ public class AccountingRuleReadPlatformServiceImpl implements AccountingRuleRead
arguments = new Object[] { officeId };
}
sql = sql + " order by rule.id asc";
@SuppressWarnings("unchecked")
final Map<Long, AccountingRuleData> extractedData = (Map<Long, AccountingRuleData>) this.jdbcTemplate.query(sql,
resultSetExtractor, arguments);
final Map<Long, AccountingRuleData> extractedData = this.jdbcTemplate.query(sql, resultSetExtractor, arguments);
return new ArrayList<AccountingRuleData>(extractedData.values());
}
@Override
public AccountingRuleData retrieveAccountingRuleById(final Long accountingRuleId) {
try {
final AccountingRuleDataExtractor resultSetExtractor = new AccountingRuleDataExtractor(this.jdbcTemplate);
final AccountingRuleDataExtractor resultSetExtractor = new AccountingRuleDataExtractor(this.jdbcTemplate,
this.glAccountReadPlatformService);
final String sql = "select " + resultSetExtractor.schema() + " and rule.id = ?";
@SuppressWarnings("unchecked")
final Map<Long, AccountingRuleData> extractedData = (Map<Long, AccountingRuleData>) this.jdbcTemplate.query(sql,
resultSetExtractor, new Object[] { accountingRuleId });
final Map<Long, AccountingRuleData> extractedData = this.jdbcTemplate.query(sql, resultSetExtractor,
new Object[] { accountingRuleId });
final AccountingRuleData accountingRuleData = extractedData.get(accountingRuleId);
if (accountingRuleData == null) { throw new AccountingRuleNotFoundException(accountingRuleId); }
return accountingRuleData;