Search in sources :

Example 1 with ChartOfAccountsConfig

use of org.mifos.config.ChartOfAccountsConfig in project head by mifos.

the class FinancialInitializer method loadCOA.

/**
     * Reads chart of accounts from a configuration file and inserts into the
     * database. Deleting accounts is not currently supported, but if the user
     * tries to do this (by leaving it out of the custom chart of accounts
     * config file), there is currently no error reported.
     * <p>
     * QUESION: what if custom config was missing entries from default config,
     * but then is moved _out_ of the app server classpath?
     * <p>
     * ANSWER: once ChartOfAccountsConfig.isLoaded() returns true, <em>only
     * the custom CoA config file will be considered</em>. Using the custom
     * config will be the only way to add new accounts after the initial CoA
     * data is loaded in the database.
     */
public static void loadCOA() throws FinancialException {
    Session session = StaticHibernateUtil.getSessionTL();
    final String coaLocation;
    try {
        if (!ChartOfAccountsConfig.canLoadCoa(session)) {
            logger.info("Chart of accounts data will not be modified since " + "the custom chart of accounts configuration file was " + "not found on the classpath.");
            return;
        }
        coaLocation = ChartOfAccountsConfig.getCoaUri(session);
        logger.info("going to load or modify chart of accounts " + "configuration from " + coaLocation);
    } catch (IOException e) {
        throw new FinancialException("Charts of accounts loading failed", e);
    }
    ChartOfAccountsConfig coa;
    try {
        coa = ChartOfAccountsConfig.load(coaLocation);
    } catch (ConfigurationException e) {
        throw new FinancialException(coaLocation + " loading failed", e);
    }
    LegacyAccountDao ap = ApplicationContextProvider.getBean(LegacyAccountDao.class);
    for (GLAccount glAccount : coa.getGLAccounts()) {
        Short accountId = ap.getAccountIdFromGlCode(glAccount.glCode);
        if (null == accountId) {
            logger.info("Adding new general ledger account: " + glAccount);
            ap.addGeneralLedgerAccount(glAccount.name, glAccount.glCode, glAccount.parentGlCode, glAccount.categoryType);
        } else {
            COABO account = (COABO) session.load(COABO.class, accountId);
            if (account.getCategoryType() != glAccount.categoryType) {
                throw new FinancialException("category type change not supported");
            }
            if (!accountHierarchyMatch(account, glAccount)) {
                throw new FinancialException("chart of accounts hierarchy change not supported");
            }
            if (!account.getAccountName().equals(glAccount.name)) {
                logger.info("updating general ledger account name. code=" + account.getGlCode() + ". old name=" + account.getAccountName() + ", new name=" + glAccount.name);
                ap.updateAccountName(account, glAccount.name);
            }
        }
    }
}
Also used : ChartOfAccountsConfig(org.mifos.config.ChartOfAccountsConfig) ConfigurationException(org.mifos.config.exceptions.ConfigurationException) LegacyAccountDao(org.mifos.accounts.persistence.LegacyAccountDao) COABO(org.mifos.accounts.financial.business.COABO) GLAccount(org.mifos.config.GLAccount) IOException(java.io.IOException) FinancialException(org.mifos.accounts.financial.exceptions.FinancialException) Session(org.hibernate.Session)

Aggregations

IOException (java.io.IOException)1 Session (org.hibernate.Session)1 COABO (org.mifos.accounts.financial.business.COABO)1 FinancialException (org.mifos.accounts.financial.exceptions.FinancialException)1 LegacyAccountDao (org.mifos.accounts.persistence.LegacyAccountDao)1 ChartOfAccountsConfig (org.mifos.config.ChartOfAccountsConfig)1 GLAccount (org.mifos.config.GLAccount)1 ConfigurationException (org.mifos.config.exceptions.ConfigurationException)1