001/*
002 * Copyright 2010 The Kuali Foundation.
003 * 
004 * Licensed under the Educational Community License, Version 1.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 * http://www.opensource.org/licenses/ecl1.php
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.ole.fp.document.service.impl;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.ole.coa.businessobject.ObjectCode;
020import org.kuali.ole.coa.businessobject.OffsetDefinition;
021import org.kuali.ole.coa.service.OffsetDefinitionService;
022import org.kuali.ole.fp.document.YearEndDocument;
023import org.kuali.ole.fp.document.service.YearEndPendingEntryService;
024import org.kuali.ole.gl.service.SufficientFundsService;
025import org.kuali.ole.sys.OLEConstants;
026import org.kuali.ole.sys.OLEPropertyConstants;
027import org.kuali.ole.sys.businessobject.AccountingLine;
028import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry;
029import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntrySourceDetail;
030import org.kuali.ole.sys.document.validation.impl.AccountingDocumentRuleBaseConstants;
031import org.kuali.ole.sys.service.UniversityDateService;
032import org.kuali.rice.krad.document.TransactionalDocument;
033import org.kuali.rice.krad.util.ObjectUtils;
034
035/**
036 * The default implementation of the YearEndPendingEntryService
037 */
038public class YearEndPendingEntryServiceImpl implements YearEndPendingEntryService {
039    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(YearEndPendingEntryServiceImpl.class);
040    
041    protected static final String FINAL_ACCOUNTING_PERIOD = OLEConstants.MONTH13;
042    
043    protected UniversityDateService universityDateService;
044    protected SufficientFundsService sufficientFundsService;
045    protected OffsetDefinitionService offsetDefinitionService;
046
047    /**
048     * @see org.kuali.ole.fp.document.service.YearEndPendingEntryService#customizeExplicitGeneralLedgerPendingEntry(org.kuali.rice.krad.document.TransactionalDocument, org.kuali.ole.sys.businessobject.AccountingLine, org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry)
049     */
050    @Override
051    public void customizeExplicitGeneralLedgerPendingEntry(TransactionalDocument transactionalDocument, AccountingLine accountingLine, GeneralLedgerPendingEntry explicitEntry) {
052        if (!(transactionalDocument instanceof YearEndDocument)) {
053            throw new IllegalArgumentException("invalid (not a year end document) for class:" + transactionalDocument.getClass());
054        }
055        YearEndDocument yearEndDocument = (YearEndDocument) transactionalDocument;
056        explicitEntry.setUniversityFiscalPeriodCode(getAccountingPeriodForYearEndEntry());
057        explicitEntry.setUniversityFiscalYear(getPreviousFiscalYear());
058    }
059
060    /**
061     * @see org.kuali.ole.fp.document.service.YearEndPendingEntryService#customizeOffsetGeneralLedgerPendingEntry(org.kuali.rice.krad.document.TransactionalDocument, org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntrySourceDetail, org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry, org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry)
062     */
063    @Override
064    public boolean customizeOffsetGeneralLedgerPendingEntry(TransactionalDocument transactionalDocument, GeneralLedgerPendingEntrySourceDetail accountingLine, GeneralLedgerPendingEntry explicitEntry, GeneralLedgerPendingEntry offsetEntry) {
065        if (!(transactionalDocument instanceof YearEndDocument)) {
066            throw new IllegalArgumentException("invalid (not a year end document) for class:" + transactionalDocument.getClass());
067        }
068        OffsetDefinition offsetDefinition = offsetDefinitionService.getByPrimaryId(getPreviousFiscalYear(), explicitEntry.getChartOfAccountsCode(), explicitEntry.getFinancialDocumentTypeCode(), explicitEntry.getFinancialBalanceTypeCode());
069        if (!ObjectUtils.isNull(offsetDefinition)) {
070            String offsetObjectCode = getOffsetFinancialObjectCode(offsetDefinition);
071            offsetEntry.setFinancialObjectCode(offsetObjectCode);
072            if (offsetObjectCode.equals(AccountingDocumentRuleBaseConstants.GENERAL_LEDGER_PENDING_ENTRY_CODE.getBlankFinancialObjectCode())) {
073                // no BO, so punt
074                offsetEntry.setAcctSufficientFundsFinObjCd(AccountingDocumentRuleBaseConstants.GENERAL_LEDGER_PENDING_ENTRY_CODE.getBlankFinancialObjectCode());
075            }
076            else {
077                offsetDefinition.refreshReferenceObject(OLEPropertyConstants.FINANCIAL_OBJECT);
078                ObjectCode financialObject = offsetDefinition.getFinancialObject();
079                // The ObjectCode reference may be invalid because a flexible offset account changed its chart code.
080                if (ObjectUtils.isNull(financialObject)) {
081                    throw new RuntimeException("offset object code " + offsetEntry.getUniversityFiscalYear() + "-" + offsetEntry.getChartOfAccountsCode() + "-" + offsetEntry.getFinancialObjectCode());
082                }
083                offsetEntry.refreshReferenceObject(OLEPropertyConstants.ACCOUNT);
084                offsetEntry.setAcctSufficientFundsFinObjCd(sufficientFundsService.getSufficientFundsObjectCode(financialObject, offsetEntry.getAccount().getAccountSufficientFundsCode()));
085            }
086
087            offsetEntry.setFinancialObjectTypeCode(getOffsetFinancialObjectTypeCode(offsetDefinition));
088            
089            return true;
090        }
091        return false;
092    }
093
094    /**
095     * @see org.kuali.ole.fp.document.service.YearEndPendingEntryService#getFinalAccountingPeriod()
096     */
097    @Override
098    public String getFinalAccountingPeriod() {
099        return FINAL_ACCOUNTING_PERIOD;
100    }
101    
102    /**
103     * @return the accounting period the year end entry should be populated with
104     */
105    protected String getAccountingPeriodForYearEndEntry() {
106        return getFinalAccountingPeriod();
107    }
108
109    /**
110     * @see org.kuali.ole.fp.document.service.YearEndPendingEntryService#getPreviousFiscalYear()
111     */
112    @Override
113    public Integer getPreviousFiscalYear() {
114        return universityDateService.getCurrentFiscalYear() - 1;
115    }
116    
117    /**
118     * Helper method that determines the offset entry's financial object code.
119     * 
120     * @param offsetDefinition
121     * @return String
122     */
123    protected String getOffsetFinancialObjectCode(OffsetDefinition offsetDefinition) {
124        if (null != offsetDefinition) {
125            String returnString = (!StringUtils.isBlank(offsetDefinition.getFinancialObjectCode())) ? offsetDefinition.getFinancialObjectCode() : AccountingDocumentRuleBaseConstants.GENERAL_LEDGER_PENDING_ENTRY_CODE.getBlankFinancialObjectCode();
126            return returnString;
127        } else {
128            return AccountingDocumentRuleBaseConstants.GENERAL_LEDGER_PENDING_ENTRY_CODE.getBlankFinancialObjectCode();
129        }
130    }
131    
132    /**
133     * Helper method that determines the offset entry's financial object type code.
134     * 
135     * @param offsetDefinition
136     * @return String
137     */
138    protected String getOffsetFinancialObjectTypeCode(OffsetDefinition offsetDefinition) {
139        if (null != offsetDefinition && null != offsetDefinition.getFinancialObject()) {
140            String returnString = (!StringUtils.isBlank(offsetDefinition.getFinancialObject().getFinancialObjectTypeCode())) ? offsetDefinition.getFinancialObject().getFinancialObjectTypeCode() : AccountingDocumentRuleBaseConstants.GENERAL_LEDGER_PENDING_ENTRY_CODE.getBlankFinancialObjectType();
141            return returnString;
142        } else {
143            return AccountingDocumentRuleBaseConstants.GENERAL_LEDGER_PENDING_ENTRY_CODE.getBlankFinancialObjectType();
144        }
145
146    }
147
148    public void setUniversityDateService(UniversityDateService universityDateService) {
149        this.universityDateService = universityDateService;
150    }
151
152    public void setSufficientFundsService(SufficientFundsService sufficientFundsService) {
153        this.sufficientFundsService = sufficientFundsService;
154    }
155
156    public void setOffsetDefinitionService(OffsetDefinitionService offsetDefinitionService) {
157        this.offsetDefinitionService = offsetDefinitionService;
158    }
159
160}