1 /*
2 * Copyright 2005-2006 The Kuali Foundation
3 *
4 * Licensed under the Educational Community License, Version 2.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/ecl2.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;
17
18 import static org.kuali.ole.sys.OLEConstants.BALANCE_TYPE_PRE_ENCUMBRANCE;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.kuali.ole.sys.OLEConstants;
24 import org.kuali.ole.sys.businessobject.AccountingLine;
25 import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry;
26 import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntrySourceDetail;
27 import org.kuali.ole.sys.businessobject.SufficientFundsItem;
28 import org.kuali.ole.sys.context.SpringContext;
29 import org.kuali.ole.sys.document.AccountingDocumentBase;
30 import org.kuali.ole.sys.document.AmountTotaling;
31 import org.kuali.ole.sys.document.Correctable;
32 import org.kuali.ole.sys.document.service.DebitDeterminerService;
33 import org.kuali.ole.sys.service.HomeOriginationService;
34 import org.kuali.rice.core.api.datetime.DateTimeService;
35 import org.kuali.rice.kew.api.exception.WorkflowException;
36 import org.kuali.rice.krad.document.Copyable;
37
38 /**
39 * The Pre-Encumbrance document provides the capability to record encumbrances independently of purchase orders, travel, or Physical
40 * Plant work orders. These transactions are for the use of the account manager to earmark funds for which unofficial commitments
41 * have already been made.
42 */
43 public class PreEncumbranceDocument extends AccountingDocumentBase implements Copyable, Correctable, AmountTotaling {
44 protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PreEncumbranceDocument.class);
45
46 protected java.sql.Date reversalDate;
47
48 /**
49 * Initializes the array lists and some basic info.
50 */
51 public PreEncumbranceDocument() {
52 super();
53 }
54
55 /**
56 * @see org.kuali.ole.sys.document.AccountingDocumentBase#checkSufficientFunds()
57 */
58 @Override
59 public List<SufficientFundsItem> checkSufficientFunds() {
60 LOG.debug("checkSufficientFunds() started");
61
62 // This document does not do sufficient funds checking
63 return new ArrayList<SufficientFundsItem>();
64 }
65
66
67 /**
68 * @return Timestamp
69 */
70 public java.sql.Date getReversalDate() {
71 return reversalDate;
72 }
73
74 /**
75 * @param reversalDate
76 */
77 public void setReversalDate(java.sql.Date reversalDate) {
78 this.reversalDate = reversalDate;
79 }
80
81 /**
82 * Overrides the base implementation to return "Encumbrance".
83 *
84 * @see org.kuali.ole.sys.document.AccountingDocument#getSourceAccountingLinesSectionTitle()
85 */
86 @Override
87 public String getSourceAccountingLinesSectionTitle() {
88 return OLEConstants.ENCUMBRANCE;
89 }
90
91 /**
92 * Overrides the base implementation to return "Disencumbrance".
93 *
94 * @see org.kuali.ole.sys.document.AccountingDocument#getTargetAccountingLinesSectionTitle()
95 */
96 @Override
97 public String getTargetAccountingLinesSectionTitle() {
98 return OLEConstants.DISENCUMBRANCE;
99 }
100
101 /**
102 * This method limits valid debits to only expense object type codes. Additionally, an
103 * IllegalStateException will be thrown if the accounting line passed in is not an expense,
104 * is an error correction with a positive dollar amount or is not an error correction and
105 * has a negative amount.
106 *
107 * @param transactionalDocument The document the accounting line being checked is located in.
108 * @param accountingLine The accounting line being analyzed.
109 * @return True if the accounting line given is a debit accounting line, false otherwise.
110 *
111 * @see IsDebitUtils#isDebitConsideringSection(FinancialDocumentRuleBase, FinancialDocument, AccountingLine)
112 * @see org.kuali.rice.krad.rule.AccountingLineRule#isDebit(org.kuali.rice.krad.document.FinancialDocument,
113 * org.kuali.rice.krad.bo.AccountingLine)
114 */
115 @Override
116 public boolean isDebit(GeneralLedgerPendingEntrySourceDetail postable) {
117 AccountingLine accountingLine = (AccountingLine)postable;
118 // if not expense, or positive amount on an error-correction, or negative amount on a non-error-correction, throw exception
119 DebitDeterminerService isDebitUtils = SpringContext.getBean(DebitDeterminerService.class);
120 if (!isDebitUtils.isExpense(accountingLine) || (isDebitUtils.isErrorCorrection(this) == accountingLine.getAmount().isPositive())) {
121 throw new IllegalStateException(isDebitUtils.getDebitCalculationIllegalStateExceptionMessage());
122 }
123
124 return !isDebitUtils.isDebitConsideringSection(this, accountingLine);
125 }
126
127 /**
128 * This method contains PreEncumbrance document specific general ledger pending entry explicit entry
129 * attribute assignments. These attributes include financial balance type code, reversal date and
130 * transaction encumbrance update code.
131 *
132 * @param financialDocument The document which contains the explicit entry.
133 * @param accountingLine The accounting line the explicit entry is generated from.
134 * @param explicitEntry The explicit entry being updated.
135 *
136 * @see org.kuali.module.financial.rules.FinancialDocumentRuleBase#customizeExplicitGeneralLedgerPendingEntry(org.kuali.rice.krad.document.FinancialDocument,
137 * org.kuali.rice.krad.bo.AccountingLine, org.kuali.module.gl.bo.GeneralLedgerPendingEntry)
138 */
139 @Override
140 public void customizeExplicitGeneralLedgerPendingEntry(GeneralLedgerPendingEntrySourceDetail postable, GeneralLedgerPendingEntry explicitEntry) {
141 explicitEntry.setFinancialBalanceTypeCode(BALANCE_TYPE_PRE_ENCUMBRANCE);
142 AccountingLine accountingLine = (AccountingLine)postable;
143
144 // set the reversal date to what was chosen by the user in the interface
145 if (getReversalDate() != null) {
146 explicitEntry.setFinancialDocumentReversalDate(getReversalDate());
147 }
148 explicitEntry.setTransactionEntryProcessedTs(null);
149 if (accountingLine.isSourceAccountingLine()) {
150 explicitEntry.setTransactionEncumbranceUpdateCode(OLEConstants.ENCUMB_UPDT_DOCUMENT_CD);
151 }
152 else {
153 explicitEntry.setTransactionEncumbranceUpdateCode(OLEConstants.ENCUMB_UPDT_REFERENCE_DOCUMENT_CD);
154 explicitEntry.setReferenceFinancialSystemOriginationCode(SpringContext.getBean(HomeOriginationService.class).getHomeOrigination().getFinSystemHomeOriginationCode());
155 explicitEntry.setReferenceFinancialDocumentNumber(accountingLine.getReferenceNumber());
156 explicitEntry.setReferenceFinancialDocumentTypeCode(explicitEntry.getFinancialDocumentTypeCode()); // "PE"
157 }
158 }
159
160 /**
161 * @see org.kuali.ole.sys.document.AccountingDocumentBase#toCopy()
162 */
163 @Override
164 public void toCopy() throws WorkflowException {
165 super.toCopy();
166 refreshReversalDate();
167 }
168
169 /**
170 * If the reversal date on this document is in need of refreshing, refreshes the reveral date. THIS METHOD MAY CHANGE DOCUMENT STATE!
171 * @return true if the reversal date ended up getting refreshed, false otherwise
172 */
173 protected boolean refreshReversalDate() {
174 boolean refreshed = false;
175 if (getReversalDate() != null) {
176 java.sql.Date today = SpringContext.getBean(DateTimeService.class).getCurrentSqlDateMidnight();
177 if (getReversalDate().before(today)) {
178 // set the reversal date on the document
179 setReversalDate(today);
180 refreshed = true;
181 }
182 }
183 return refreshed;
184 }
185 }