001: /*
002: * Copyright 2007 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: */
016: package org.kuali.kfs.document;
017:
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: import org.apache.commons.lang.StringUtils;
022: import org.kuali.core.exceptions.ValidationException;
023: import org.kuali.core.rule.event.ApproveDocumentEvent;
024: import org.kuali.core.rule.event.KualiDocumentEvent;
025: import org.kuali.core.rule.event.RouteDocumentEvent;
026: import org.kuali.core.util.GlobalVariables;
027: import org.kuali.kfs.KFSConstants;
028: import org.kuali.kfs.KFSKeyConstants;
029: import org.kuali.kfs.bo.GeneralLedgerPendingEntry;
030: import org.kuali.kfs.context.SpringContext;
031: import org.kuali.kfs.service.GeneralLedgerPendingEntryService;
032: import org.kuali.kfs.service.ParameterService;
033: import org.kuali.module.financial.bo.Bank;
034: import org.kuali.module.gl.service.SufficientFundsService;
035: import org.kuali.module.gl.util.SufficientFundsItem;
036:
037: import edu.iu.uis.eden.exception.WorkflowException;
038:
039: /**
040: * Base implementation for a general ledger posting document.
041: */
042: public class GeneralLedgerPostingDocumentBase extends
043: LedgerPostingDocumentBase implements
044: GeneralLedgerPostingDocument {
045: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
046: .getLogger(GeneralLedgerPostingDocumentBase.class);
047:
048: protected List<GeneralLedgerPendingEntry> generalLedgerPendingEntries;
049:
050: /**
051: * Default constructor.
052: */
053: public GeneralLedgerPostingDocumentBase() {
054: super ();
055: setGeneralLedgerPendingEntries(new ArrayList<GeneralLedgerPendingEntry>());
056: }
057:
058: /**
059: * @see org.kuali.kfs.document.GeneralLedgerPostingDocument#getGeneralLedgerPendingEntries()
060: */
061: public List<GeneralLedgerPendingEntry> getGeneralLedgerPendingEntries() {
062: return generalLedgerPendingEntries;
063: }
064:
065: /**
066: * @see org.kuali.kfs.document.GeneralLedgerPostingDocument#getGeneralLedgerPendingEntry(int)
067: */
068: public GeneralLedgerPendingEntry getGeneralLedgerPendingEntry(
069: int index) {
070: while (generalLedgerPendingEntries.size() <= index) {
071: generalLedgerPendingEntries
072: .add(new GeneralLedgerPendingEntry());
073: }
074: return generalLedgerPendingEntries.get(index);
075: }
076:
077: /**
078: * @see org.kuali.kfs.document.GeneralLedgerPostingDocument#setGeneralLedgerPendingEntries(java.util.List)
079: */
080: public void setGeneralLedgerPendingEntries(
081: List<GeneralLedgerPendingEntry> generalLedgerPendingEntries) {
082: this .generalLedgerPendingEntries = generalLedgerPendingEntries;
083: }
084:
085: /**
086: * @see org.kuali.kfs.document.GeneralLedgerPostingDocument#isBankCashOffsetEnabled()
087: */
088: public boolean isBankCashOffsetEnabled() {
089: return SpringContext
090: .getBean(ParameterService.class)
091: .getIndicatorParameter(
092: Bank.class,
093: KFSConstants.SystemGroupParameterNames.FLEXIBLE_CLAIM_ON_CASH_BANK_ENABLED_FLAG);
094: }
095:
096: /**
097: * @see org.kuali.kfs.document.GeneralLedgerPostingDocument#checkSufficientFunds()
098: */
099: public List<SufficientFundsItem> checkSufficientFunds() {
100: LOG.debug("checkSufficientFunds() started");
101:
102: if (documentPerformsSufficientFundsCheck()) {
103: SufficientFundsService sufficientFundsService = SpringContext
104: .getBean(SufficientFundsService.class);
105: return sufficientFundsService.checkSufficientFunds(this );
106: } else {
107: return new ArrayList<SufficientFundsItem>();
108: }
109: }
110:
111: /**
112: * This method checks to see if SF checking should be done for this document. This was originally part of
113: * SufficientFundsService.checkSufficientFunds() but was externalized so documents that need to override any of the SF methods
114: * can still explicitly check this
115: *
116: * @return
117: */
118: public boolean documentPerformsSufficientFundsCheck() {
119: // check for reversing entries generated by an error correction.
120: return StringUtils.isBlank(this .getDocumentHeader()
121: .getFinancialDocumentInErrorNumber());
122: }
123:
124: /**
125: * @see org.kuali.kfs.document.GeneralLedgerPostingDocument#getPendingLedgerEntriesForSufficientFundsChecking()
126: */
127: public List<GeneralLedgerPendingEntry> getPendingLedgerEntriesForSufficientFundsChecking() {
128: return getGeneralLedgerPendingEntries();
129: }
130:
131: /**
132: * Override to call super and then iterate over all GLPEs and update the approved code appropriately.
133: *
134: * @see Document#handleRouteStatusChange()
135: */
136: @Override
137: public void handleRouteStatusChange() {
138: super .handleRouteStatusChange();
139: if (getDocumentHeader().getWorkflowDocument()
140: .stateIsProcessed()) {
141: changeGeneralLedgerPendingEntriesApprovedStatusCode(); // update all glpes for doc and set their status to approved
142: } else if (getDocumentHeader().getWorkflowDocument()
143: .stateIsCanceled()
144: || getDocumentHeader().getWorkflowDocument()
145: .stateIsDisapproved()) {
146: removeGeneralLedgerPendingEntries();
147: }
148: }
149:
150: /**
151: * This method iterates over all of the GLPEs for a document and sets their approved status code to APPROVED "A".
152: */
153: private void changeGeneralLedgerPendingEntriesApprovedStatusCode() {
154: for (GeneralLedgerPendingEntry glpe : getGeneralLedgerPendingEntries()) {
155: glpe
156: .setFinancialDocumentApprovedCode(KFSConstants.DocumentStatusCodes.APPROVED);
157: }
158: }
159:
160: /**
161: * This method calls the service to remove all of the GLPE's associated with this document
162: */
163: protected void removeGeneralLedgerPendingEntries() {
164: GeneralLedgerPendingEntryService glpeService = SpringContext
165: .getBean(GeneralLedgerPendingEntryService.class);
166: glpeService.delete(getDocumentHeader().getDocumentNumber());
167: }
168:
169: /**
170: * @see org.kuali.core.document.DocumentBase#toCopy()
171: */
172: @Override
173: public void toCopy() throws WorkflowException {
174: super .toCopy();
175: getGeneralLedgerPendingEntries().clear();
176: }
177:
178: /**
179: * @see org.kuali.core.document.TransactionalDocumentBase#toErrorCorrection()
180: */
181: @Override
182: public void toErrorCorrection() throws WorkflowException {
183: super .toErrorCorrection();
184: getGeneralLedgerPendingEntries().clear();
185: }
186:
187: @Override
188: public void prepareForSave(KualiDocumentEvent event) {
189: super .prepareForSave(event);
190: if (event instanceof RouteDocumentEvent
191: || event instanceof ApproveDocumentEvent) {
192: // generate general ledger pending entries should be called prior to sufficient funds checking
193: List<SufficientFundsItem> sfItems = checkSufficientFunds();
194: if (!sfItems.isEmpty()) {
195: for (SufficientFundsItem sfItem : sfItems) {
196: GlobalVariables
197: .getErrorMap()
198: .putError(
199: KFSConstants.ACCOUNTING_LINE_ERRORS,
200: KFSKeyConstants.SufficientFunds.ERROR_INSUFFICIENT_FUNDS,
201: new String[] {
202: sfItem
203: .getAccount()
204: .getChartOfAccountsCode(),
205: sfItem.getAccount()
206: .getAccountNumber(),
207: StringUtils
208: .isNotBlank(sfItem
209: .getSufficientFundsObjectCode()) ? sfItem
210: .getSufficientFundsObjectCode()
211: : KFSConstants.NOT_AVAILABLE_STRING,
212: sfItem
213: .getAccountSufficientFundsCode() });
214: }
215: throw new ValidationException(
216: "Insufficient Funds on this Document:");
217: }
218: }
219: }
220: }
|