View Javadoc
1   /*
2    * Copyright 2010 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 1.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl1.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.fp.document.service.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.coa.businessobject.ObjectCode;
20  import org.kuali.ole.coa.businessobject.OffsetDefinition;
21  import org.kuali.ole.coa.service.OffsetDefinitionService;
22  import org.kuali.ole.fp.document.YearEndDocument;
23  import org.kuali.ole.fp.document.service.YearEndPendingEntryService;
24  import org.kuali.ole.gl.service.SufficientFundsService;
25  import org.kuali.ole.sys.OLEConstants;
26  import org.kuali.ole.sys.OLEPropertyConstants;
27  import org.kuali.ole.sys.businessobject.AccountingLine;
28  import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry;
29  import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntrySourceDetail;
30  import org.kuali.ole.sys.document.validation.impl.AccountingDocumentRuleBaseConstants;
31  import org.kuali.ole.sys.service.UniversityDateService;
32  import org.kuali.rice.krad.document.TransactionalDocument;
33  import org.kuali.rice.krad.util.ObjectUtils;
34  
35  /**
36   * The default implementation of the YearEndPendingEntryService
37   */
38  public class YearEndPendingEntryServiceImpl implements YearEndPendingEntryService {
39      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(YearEndPendingEntryServiceImpl.class);
40      
41      protected static final String FINAL_ACCOUNTING_PERIOD = OLEConstants.MONTH13;
42      
43      protected UniversityDateService universityDateService;
44      protected SufficientFundsService sufficientFundsService;
45      protected OffsetDefinitionService offsetDefinitionService;
46  
47      /**
48       * @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)
49       */
50      @Override
51      public void customizeExplicitGeneralLedgerPendingEntry(TransactionalDocument transactionalDocument, AccountingLine accountingLine, GeneralLedgerPendingEntry explicitEntry) {
52          if (!(transactionalDocument instanceof YearEndDocument)) {
53              throw new IllegalArgumentException("invalid (not a year end document) for class:" + transactionalDocument.getClass());
54          }
55          YearEndDocument yearEndDocument = (YearEndDocument) transactionalDocument;
56          explicitEntry.setUniversityFiscalPeriodCode(getAccountingPeriodForYearEndEntry());
57          explicitEntry.setUniversityFiscalYear(getPreviousFiscalYear());
58      }
59  
60      /**
61       * @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)
62       */
63      @Override
64      public boolean customizeOffsetGeneralLedgerPendingEntry(TransactionalDocument transactionalDocument, GeneralLedgerPendingEntrySourceDetail accountingLine, GeneralLedgerPendingEntry explicitEntry, GeneralLedgerPendingEntry offsetEntry) {
65          if (!(transactionalDocument instanceof YearEndDocument)) {
66              throw new IllegalArgumentException("invalid (not a year end document) for class:" + transactionalDocument.getClass());
67          }
68          OffsetDefinition offsetDefinition = offsetDefinitionService.getByPrimaryId(getPreviousFiscalYear(), explicitEntry.getChartOfAccountsCode(), explicitEntry.getFinancialDocumentTypeCode(), explicitEntry.getFinancialBalanceTypeCode());
69          if (!ObjectUtils.isNull(offsetDefinition)) {
70              String offsetObjectCode = getOffsetFinancialObjectCode(offsetDefinition);
71              offsetEntry.setFinancialObjectCode(offsetObjectCode);
72              if (offsetObjectCode.equals(AccountingDocumentRuleBaseConstants.GENERAL_LEDGER_PENDING_ENTRY_CODE.getBlankFinancialObjectCode())) {
73                  // no BO, so punt
74                  offsetEntry.setAcctSufficientFundsFinObjCd(AccountingDocumentRuleBaseConstants.GENERAL_LEDGER_PENDING_ENTRY_CODE.getBlankFinancialObjectCode());
75              }
76              else {
77                  offsetDefinition.refreshReferenceObject(OLEPropertyConstants.FINANCIAL_OBJECT);
78                  ObjectCode financialObject = offsetDefinition.getFinancialObject();
79                  // The ObjectCode reference may be invalid because a flexible offset account changed its chart code.
80                  if (ObjectUtils.isNull(financialObject)) {
81                      throw new RuntimeException("offset object code " + offsetEntry.getUniversityFiscalYear() + "-" + offsetEntry.getChartOfAccountsCode() + "-" + offsetEntry.getFinancialObjectCode());
82                  }
83                  offsetEntry.refreshReferenceObject(OLEPropertyConstants.ACCOUNT);
84                  offsetEntry.setAcctSufficientFundsFinObjCd(sufficientFundsService.getSufficientFundsObjectCode(financialObject, offsetEntry.getAccount().getAccountSufficientFundsCode()));
85              }
86  
87              offsetEntry.setFinancialObjectTypeCode(getOffsetFinancialObjectTypeCode(offsetDefinition));
88              
89              return true;
90          }
91          return false;
92      }
93  
94      /**
95       * @see org.kuali.ole.fp.document.service.YearEndPendingEntryService#getFinalAccountingPeriod()
96       */
97      @Override
98      public String getFinalAccountingPeriod() {
99          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 }