001: /*
002: * Copyright 2006-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.module.financial.rules;
017:
018: import java.util.HashMap;
019: import java.util.Map;
020:
021: import org.kuali.core.document.TransactionalDocument;
022: import org.kuali.core.service.DataDictionaryService;
023: import org.kuali.core.service.DocumentService;
024: import org.kuali.core.service.DocumentTypeService;
025: import org.kuali.core.util.KualiDecimal;
026: import org.kuali.kfs.bo.AccountingLine;
027: import org.kuali.kfs.bo.SourceAccountingLine;
028: import org.kuali.kfs.bo.TargetAccountingLine;
029: import org.kuali.kfs.document.AccountingDocument;
030: import org.kuali.kfs.rule.AccountingLineRule;
031: import org.kuali.kfs.rules.AccountingDocumentRuleBase.IsDebitUtils;
032: import org.kuali.module.financial.document.AdvanceDepositDocument;
033: import org.kuali.module.financial.document.CashReceiptDocument;
034: import org.kuali.module.financial.document.CreditCardReceiptDocument;
035: import org.kuali.module.financial.document.DisbursementVoucherDocument;
036: import org.kuali.module.financial.document.DistributionOfIncomeAndExpenseDocument;
037: import org.kuali.module.financial.document.GeneralErrorCorrectionDocument;
038: import org.kuali.module.financial.document.IndirectCostAdjustmentDocument;
039: import org.kuali.module.financial.document.InternalBillingDocument;
040: import org.kuali.module.financial.document.NonCheckDisbursementDocument;
041: import org.kuali.module.financial.document.PreEncumbranceDocument;
042: import org.kuali.module.financial.document.ServiceBillingDocument;
043: import org.kuali.module.financial.document.TransferOfFundsDocument;
044:
045: import edu.iu.uis.eden.exception.WorkflowException;
046:
047: /**
048: * IsDebitTestUtils
049: */
050: public class IsDebitTestUtils {
051: /**
052: * arbitrary amount constants
053: */
054: public static class Amount {
055: public static final KualiDecimal POSITIVE = new KualiDecimal(
056: "10");
057: public static final KualiDecimal NEGATIVE = new KualiDecimal(
058: "-5");
059: }
060:
061: private static Map<Class<? extends TransactionalDocument>, String> sourceLines = new HashMap<Class<? extends TransactionalDocument>, String>();
062: private static Map<Class<? extends TransactionalDocument>, String> targetLines = new HashMap<Class<? extends TransactionalDocument>, String>();
063:
064: private static class BaChartObjectCodes {
065: public static final String EXPENSE = "5000";
066: public static final String ASSET = "8010";
067: public static final String INCOME = "1301";
068: public static final String LIABILITY = "9120";
069: }
070:
071: private static class ImportLines {
072: public static final String DEFAULT = "BA,6044900,x,objc,x,x,x,0";
073: public static final String WITH_DESCRIPTION = "BA,6044900,x,objc,x,x,x,description,0";
074: public static final String WITH_REF_NUM_AND_DESCRIPTION = "BA,6044900,x,objc,x,x,x,refnum,description,0";
075: public static final String WITH_REF_NUM = "BA,6044900,x,objc,x,x,x,refnum,0";
076: public static final String WITH_ORIGIN_CODE_AND_REF_NUM_AND_DESCRIPTION = "BA,6044900,x,objc,x,x,x,01,refnum,description,0";
077: public static final String WITHOUT_OBJECT_CODE = "BA,6044900,x,x,x,x,0";
078: }
079:
080: static {
081: sourceLines.put(AdvanceDepositDocument.class,
082: ImportLines.WITH_DESCRIPTION);
083: sourceLines.put(CashReceiptDocument.class,
084: ImportLines.WITH_DESCRIPTION);
085: sourceLines.put(CreditCardReceiptDocument.class,
086: ImportLines.WITH_DESCRIPTION);
087: sourceLines.put(DisbursementVoucherDocument.class,
088: ImportLines.WITH_DESCRIPTION);
089: sourceLines.put(DistributionOfIncomeAndExpenseDocument.class,
090: ImportLines.DEFAULT);
091: sourceLines
092: .put(
093: GeneralErrorCorrectionDocument.class,
094: ImportLines.WITH_ORIGIN_CODE_AND_REF_NUM_AND_DESCRIPTION);
095: sourceLines.put(IndirectCostAdjustmentDocument.class,
096: ImportLines.WITHOUT_OBJECT_CODE);
097: sourceLines.put(InternalBillingDocument.class,
098: ImportLines.DEFAULT);
099: sourceLines.put(NonCheckDisbursementDocument.class,
100: ImportLines.WITH_REF_NUM_AND_DESCRIPTION);
101: sourceLines.put(PreEncumbranceDocument.class,
102: ImportLines.DEFAULT);
103: sourceLines.put(ServiceBillingDocument.class,
104: ImportLines.WITH_DESCRIPTION);
105: sourceLines.put(TransferOfFundsDocument.class,
106: ImportLines.DEFAULT);
107: }
108:
109: static {
110: targetLines.put(DistributionOfIncomeAndExpenseDocument.class,
111: ImportLines.DEFAULT);
112: targetLines
113: .put(
114: GeneralErrorCorrectionDocument.class,
115: ImportLines.WITH_ORIGIN_CODE_AND_REF_NUM_AND_DESCRIPTION);
116: targetLines.put(IndirectCostAdjustmentDocument.class,
117: ImportLines.WITHOUT_OBJECT_CODE);
118: targetLines.put(InternalBillingDocument.class,
119: ImportLines.DEFAULT);
120: targetLines.put(PreEncumbranceDocument.class,
121: ImportLines.WITH_REF_NUM);
122: targetLines.put(ServiceBillingDocument.class,
123: ImportLines.WITH_DESCRIPTION);
124: targetLines.put(TransferOfFundsDocument.class,
125: ImportLines.DEFAULT);
126: }
127:
128: /**
129: * @param documentService
130: * @param documentClass
131: * @return TransactionalDocument
132: * @throws WorkflowException
133: */
134: public static AccountingDocument getDocument(
135: DocumentService documentService,
136: Class<? extends AccountingDocument> documentClass)
137: throws WorkflowException {
138: return (AccountingDocument) documentService
139: .getNewDocument(documentClass);
140: }
141:
142: /**
143: * @param documentService
144: * @param documentClass
145: * @return TransactionalDocument
146: * @throws WorkflowException
147: */
148: public static AccountingDocument getErrorCorrectionDocument(
149: DocumentService documentService,
150: Class<? extends AccountingDocument> documentClass)
151: throws WorkflowException {
152: AccountingDocument financialDocument = getDocument(
153: documentService, documentClass);
154: financialDocument.getDocumentHeader()
155: .setFinancialDocumentInErrorNumber(
156: "fakeErrorCorrection");
157:
158: return financialDocument;
159: }
160:
161: private static AccountingLine getAccountingLine(
162: AccountingDocument financialDocument,
163: Class<? extends AccountingLine> lineClass,
164: KualiDecimal amount, String objectCode) {
165: String unparsedLine = null;
166: AccountingLine line = null;
167: if (SourceAccountingLine.class.isAssignableFrom(lineClass)) {
168: unparsedLine = sourceLines
169: .get(financialDocument.getClass());
170: if (unparsedLine == null) {
171: throw new IllegalArgumentException(
172: "no value found in sourceMap for: "
173: + financialDocument.getClass() + ";"
174: + lineClass);
175: }
176: line = financialDocument.getAccountingLineParser()
177: .parseSourceAccountingLine(financialDocument,
178: unparsedLine);
179: } else if (TargetAccountingLine.class
180: .isAssignableFrom(lineClass)) {
181: unparsedLine = targetLines
182: .get(financialDocument.getClass());
183: if (unparsedLine == null) {
184: throw new IllegalArgumentException(
185: "no value found in targetMap for: "
186: + financialDocument.getClass() + ";"
187: + lineClass);
188: }
189: line = financialDocument.getAccountingLineParser()
190: .parseTargetAccountingLine(financialDocument,
191: unparsedLine);
192: } else {
193: throw new IllegalArgumentException(
194: "invalid accounting line type (" + lineClass + ")");
195: }
196:
197: line.setAmount(amount);
198: line.setFinancialObjectCode(objectCode);
199: return line;
200: }
201:
202: /**
203: * @param financialDocument
204: * @param lineClass
205: * @param amount
206: * @return AccountingLine
207: */
208: public static AccountingLine getExpenseLine(
209: AccountingDocument financialDocument,
210: Class<? extends AccountingLine> lineClass,
211: KualiDecimal amount) {
212: return getAccountingLine(financialDocument, lineClass, amount,
213: BaChartObjectCodes.EXPENSE);
214: }
215:
216: /**
217: * @param financialDocument
218: * @param lineClass
219: * @param amount
220: * @return AccountingLine
221: */
222: public static AccountingLine getAssetLine(
223: AccountingDocument financialDocument,
224: Class<? extends AccountingLine> lineClass,
225: KualiDecimal amount) {
226: return getAccountingLine(financialDocument, lineClass, amount,
227: BaChartObjectCodes.ASSET);
228: }
229:
230: /**
231: * @param financialDocument
232: * @param lineClass
233: * @param amount
234: * @return AccountingLine
235: */
236: public static AccountingLine getIncomeLine(
237: AccountingDocument financialDocument,
238: Class<? extends AccountingLine> lineClass,
239: KualiDecimal amount) {
240: return getAccountingLine(financialDocument, lineClass, amount,
241: BaChartObjectCodes.INCOME);
242: }
243:
244: /**
245: * @param financialDocument
246: * @param lineClass
247: * @param amount
248: * @return AccountingLine
249: */
250: public static AccountingLine getLiabilityLine(
251: AccountingDocument financialDocument,
252: Class<? extends AccountingLine> lineClass,
253: KualiDecimal amount) {
254: return getAccountingLine(financialDocument, lineClass, amount,
255: BaChartObjectCodes.LIABILITY);
256: }
257:
258: /**
259: * @param documentTypeService
260: * @param dataDicitionaryService
261: * @param financialDocument
262: * @param accountingLine
263: * @return
264: * @throws InstantiationException
265: * @throws IllegalAccessException
266: */
267: public static boolean isDebit(
268: DocumentTypeService documentTypeService,
269: DataDictionaryService dataDicitionaryService,
270: AccountingDocument financialDocument,
271: AccountingLine accountingLine)
272: throws InstantiationException, IllegalAccessException {
273: String documentTypeName = documentTypeService
274: .getDocumentTypeNameByClass(financialDocument
275: .getClass());
276: AccountingLineRule rule = (AccountingLineRule) dataDicitionaryService
277: .getDataDictionary().getDocumentEntry(documentTypeName)
278: .getBusinessRulesClass().newInstance();
279:
280: return rule.isDebit(financialDocument, accountingLine);
281: }
282:
283: /**
284: * @param documentTypeService
285: * @param dataDicitionaryService
286: * @param financialDocument
287: * @param accountingLine
288: * @return
289: * @throws InstantiationException
290: * @throws IllegalAccessException
291: */
292: public static boolean isDebitIllegalStateException(
293: DocumentTypeService documentTypeService,
294: DataDictionaryService dataDicitionaryService,
295: AccountingDocument financialDocument,
296: AccountingLine accountingLine)
297: throws InstantiationException, IllegalAccessException {
298: boolean failedAsExpected = false;
299: try {
300: IsDebitTestUtils.isDebit(documentTypeService,
301: dataDicitionaryService, financialDocument,
302: accountingLine);
303:
304: } catch (IllegalStateException e) {
305: failedAsExpected = IsDebitUtils.isDebitCalculationIllegalStateExceptionMessage
306: .equals(e.getMessage());
307: }
308:
309: return failedAsExpected;
310: }
311:
312: /**
313: * @param documentTypeService
314: * @param dataDicitionaryService
315: * @param financialDocument
316: * @param accountingLine
317: * @return
318: * @throws InstantiationException
319: * @throws IllegalAccessException
320: */
321: public static boolean isErrorCorrectionIllegalStateException(
322: DocumentTypeService documentTypeService,
323: DataDictionaryService dataDicitionaryService,
324: AccountingDocument financialDocument,
325: AccountingLine accountingLine)
326: throws InstantiationException, IllegalAccessException {
327: boolean failedAsExpected = false;
328: try {
329: IsDebitTestUtils.isDebit(documentTypeService,
330: dataDicitionaryService, financialDocument,
331: accountingLine);
332:
333: } catch (IllegalStateException e) {
334: failedAsExpected = IsDebitUtils.isErrorCorrectionIllegalStateExceptionMessage
335: .equals(e.getMessage());
336: }
337:
338: return failedAsExpected;
339: }
340: }
|