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.module.labor.util;
017:
018: import org.kuali.core.util.KualiDecimal;
019: import org.kuali.kfs.KFSConstants;
020: import org.kuali.kfs.rules.AccountingDocumentRuleBase.IsDebitUtils;
021: import org.kuali.module.labor.bo.ExpenseTransferAccountingLine;
022:
023: /**
024: * This class provides a set of utilities that handle the debit credit logic.
025: */
026: public class DebitCreditUtil {
027:
028: /**
029: * Determine the Debit Credit code based on the given amount. Normally (isReverse flag is set as false), the debit code returns
030: * if the amount is positive while the credit code returns if the amount is negative. When isReverse flag is set as true, the
031: * credit returns for positive amount and the debit code for negative amount.
032: *
033: * @param amount the given amount, which can be either negative or positive number.
034: * @param isReversed a flag that indicates if normal accounting practice is used. False for normal accoutning practice; true for
035: * reverse.
036: * @return the Debit Credit code based on the given transaction amount and the value of isReversed
037: */
038: public static String getDebitCreditCode(KualiDecimal amount,
039: boolean isReversed) {
040: return getDebitCreditCode(amount, "", isReversed);
041: }
042:
043: /**
044: * Determine the Debit Credit code based on the given amount. Normally (isReverse flag is set as false), the debit code returns
045: * if the amount is positive while the credit code returns if the amount is negative. When isReverse flag is set as true, the
046: * credit returns for positive amount and the debit code for negative amount.
047: *
048: * @param amount the given amount, which can be either negative or positive number.
049: * @param currentDebitCreditCode the current debit credit code
050: * @param isReversed a flag that indicates if normal accounting practice is used. False for normal accoutning practice; true for
051: * reverse.
052: * @return the Debit Credit code based on the given transaction amount and the value of isReversed
053: */
054: public static String getDebitCreditCode(KualiDecimal amount,
055: String currentDebitCreditCode, boolean isReversed) {
056: String debitCreditCode = null;
057: if (amount.isNegative()) {
058: if (KFSConstants.GL_CREDIT_CODE
059: .equals(currentDebitCreditCode)) {
060: debitCreditCode = KFSConstants.GL_DEBIT_CODE;
061: } else {
062: debitCreditCode = KFSConstants.GL_CREDIT_CODE;
063: }
064: } else {
065: if (KFSConstants.GL_CREDIT_CODE
066: .equals(currentDebitCreditCode)) {
067: debitCreditCode = KFSConstants.GL_CREDIT_CODE;
068: } else {
069: debitCreditCode = KFSConstants.GL_DEBIT_CODE;
070: }
071: }
072:
073: if (isReversed) {
074: debitCreditCode = getReverseDebitCreditCode(debitCreditCode);
075: }
076: return debitCreditCode;
077: }
078:
079: /**
080: * Determines the Debit Credit code for the expense accountine line (Salary Expense and Benefit Expense documents).
081: *
082: * @param accountingLine - line to determine code for
083: * @return String representing the debit/credit code for the line
084: */
085: public static String getDebitCreditCodeForExpenseDocument(
086: ExpenseTransferAccountingLine accountingLine) {
087: String debitCreditCode = null;
088: boolean isPositiveAmount = accountingLine.getAmount()
089: .isPositive();
090:
091: if (accountingLine.isSourceAccountingLine()) {
092: if (isPositiveAmount) {
093: debitCreditCode = KFSConstants.GL_CREDIT_CODE;
094: } else {
095: debitCreditCode = KFSConstants.GL_DEBIT_CODE;
096: }
097: } else if (accountingLine.isTargetAccountingLine()) {
098: if (isPositiveAmount) {
099: debitCreditCode = KFSConstants.GL_DEBIT_CODE;
100: } else {
101: debitCreditCode = KFSConstants.GL_CREDIT_CODE;
102: }
103: } else {
104: throw new IllegalStateException(
105: IsDebitUtils.isInvalidLineTypeIllegalArgumentExceptionMessage);
106: }
107:
108: return debitCreditCode;
109: }
110:
111: /**
112: * get the reversed debit credit code of the given code
113: *
114: * @param currentDebitCreditCode the current debit credit code
115: * @return the reversed debit credit code of the given code
116: */
117: public static String getReverseDebitCreditCode(
118: String currentDebitCreditCode) {
119: if (KFSConstants.GL_DEBIT_CODE.equals(currentDebitCreditCode)) {
120: return KFSConstants.GL_CREDIT_CODE;
121: }
122:
123: if (KFSConstants.GL_CREDIT_CODE.equals(currentDebitCreditCode)) {
124: return KFSConstants.GL_DEBIT_CODE;
125: }
126:
127: return KFSConstants.GL_CREDIT_CODE;
128: }
129:
130: /**
131: * Determine the actual amount based on Debit Credit code. If the code is credit code, then change the sign of the given amount;
132: * otherwise, do nothing
133: *
134: * @param amount the given amount, which can be either negative or positive number.
135: * @param currentDebitCreditCode the current debit credit code
136: * @return the actual numeric amount of the given amount
137: */
138: public static KualiDecimal getNumericAmount(KualiDecimal amount,
139: String currentDebitCreditCode) {
140: KualiDecimal actualAmount = amount;
141:
142: if (amount == null) {
143: actualAmount = KualiDecimal.ZERO;
144: } else if (KFSConstants.GL_CREDIT_CODE
145: .equals(currentDebitCreditCode)) {
146: actualAmount = actualAmount.multiply(new KualiDecimal(-1));
147: }
148: return actualAmount;
149: }
150: }
|