001 /*
002 * Copyright 2011 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 2.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/ecl2.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.module.ec.util;
017
018 import static org.kuali.kfs.sys.KFSConstants.CurrencyTypeAmounts.HUNDRED_DOLLAR_AMOUNT;
019
020 import org.kuali.kfs.sys.KFSConstants;
021 import org.kuali.rice.kns.util.KualiDecimal;
022
023 /**
024 * To hold the payroll amount and percent
025 */
026 public class PayrollAmountHolder {
027
028 private KualiDecimal payrollAmount;
029 private Integer payrollPercent;
030
031 private KualiDecimal totalAmount;
032
033 private KualiDecimal accumulatedAmount;
034 private Integer accumulatedPercent;
035
036 /**
037 * Constructs a PayrollAmountHolder.java.
038 *
039 * @param totalAmount the total payroll amount
040 * @param accumulatedAmount the accumulated payroll amount
041 * @param accumulatedPercent the accumulated payroll percent
042 */
043 public PayrollAmountHolder(KualiDecimal totalAmount, KualiDecimal accumulatedAmount, Integer accumulatedPercent) {
044 super();
045 this.totalAmount = totalAmount;
046 this.accumulatedAmount = accumulatedAmount;
047 this.accumulatedPercent = accumulatedPercent;
048 }
049
050 /**
051 * Gets the payrollAmount attribute.
052 *
053 * @return Returns the payrollAmount.
054 */
055 public KualiDecimal getPayrollAmount() {
056 return payrollAmount;
057 }
058
059 /**
060 * Sets the payrollAmount attribute value.
061 *
062 * @param payrollAmount The payrollAmount to set.
063 */
064 public void setPayrollAmount(KualiDecimal payrollAmount) {
065 this.payrollAmount = payrollAmount;
066 }
067
068 /**
069 * Gets the payrollPercent attribute.
070 *
071 * @return Returns the payrollPercent.
072 */
073 public Integer getPayrollPercent() {
074 return payrollPercent;
075 }
076
077 /**
078 * Sets the payrollPercent attribute value.
079 *
080 * @param payrollPercent The payrollPercent to set.
081 */
082 public void setPayrollPercent(Integer payrollPercent) {
083 this.payrollPercent = payrollPercent;
084 }
085
086 /**
087 * Gets the totalAmount attribute.
088 *
089 * @return Returns the totalAmount.
090 */
091 public KualiDecimal getTotalAmount() {
092 return totalAmount;
093 }
094
095 /**
096 * Sets the totalAmount attribute value.
097 *
098 * @param totalAmount The totalAmount to set.
099 */
100 public void setTotalAmount(KualiDecimal totalAmount) {
101 this.totalAmount = totalAmount;
102 }
103
104 /**
105 * Gets the accumulatedAmount attribute.
106 *
107 * @return Returns the accumulatedAmount.
108 */
109 public KualiDecimal getAccumulatedAmount() {
110 return accumulatedAmount;
111 }
112
113 /**
114 * Sets the accumulatedAmount attribute value.
115 *
116 * @param accumulatedAmount The accumulatedAmount to set.
117 */
118 public void setAccumulatedAmount(KualiDecimal accumulatedAmount) {
119 this.accumulatedAmount = accumulatedAmount;
120 }
121
122 /**
123 * Gets the accumulatedPercent attribute.
124 *
125 * @return Returns the accumulatedPercent.
126 */
127 public Integer getAccumulatedPercent() {
128 return accumulatedPercent;
129 }
130
131 /**
132 * Sets the accumulatedPercent attribute value.
133 *
134 * @param accumulatedPercent The accumulatedPercent to set.
135 */
136 public void setAccumulatedPercent(Integer accumulatedPercent) {
137 this.accumulatedPercent = accumulatedPercent;
138 }
139
140 /**
141 * calculate the payroll percentage based on the given information in payroll amount holder
142 *
143 * @param payrollAmountHolder the given payroll amount holder containing relating information
144 */
145 public static void calculatePayrollPercent(PayrollAmountHolder payrollAmountHolder) {
146 KualiDecimal totalAmount = payrollAmountHolder.getTotalAmount();
147 if (totalAmount.isZero()) {
148 return;
149 }
150
151 KualiDecimal payrollAmount = payrollAmountHolder.getPayrollAmount();
152 KualiDecimal accumulatedAmount = payrollAmountHolder.getAccumulatedAmount();
153 accumulatedAmount = accumulatedAmount.add(payrollAmount);
154
155 int accumulatedPercent = payrollAmountHolder.getAccumulatedPercent();
156 int quotientOne = Math.round(payrollAmount.multiply(HUNDRED_DOLLAR_AMOUNT).divide(totalAmount).floatValue());
157 accumulatedPercent = accumulatedPercent + quotientOne;
158
159 int quotientTwo = Math.round(accumulatedAmount.multiply(HUNDRED_DOLLAR_AMOUNT).divide(totalAmount).floatValue());
160 quotientTwo = quotientTwo - accumulatedPercent;
161
162 payrollAmountHolder.setAccumulatedAmount(accumulatedAmount);
163 payrollAmountHolder.setAccumulatedPercent(accumulatedPercent + quotientTwo);
164 payrollAmountHolder.setPayrollPercent(quotientOne + quotientTwo);
165 }
166
167 /**
168 * recalculate the payroll amount based on the given total amount and effort percent
169 *
170 * @param totalPayrollAmount the given total amount
171 * @param effortPercent the given effort percent
172 * @return the payroll amount calculated from the given total amount and effort percent
173 */
174 public static KualiDecimal recalculatePayrollAmount(KualiDecimal totalPayrollAmount, Integer effortPercent) {
175 double amount = totalPayrollAmount.doubleValue() * effortPercent / HUNDRED_DOLLAR_AMOUNT.doubleValue();
176
177 return new KualiDecimal(amount);
178 }
179
180 /**
181 * recalculate the effort percent based on the given total amount and payroll amount
182 *
183 * @param totalPayrollAmount the given total amount
184 * @param payrollAmount the given payroll amount
185 * @return the effort percent calculated from the given total amount and payroll amount
186 */
187 public static Double recalculateEffortPercent(KualiDecimal totalPayrollAmount, KualiDecimal payrollAmount) {
188 double percent = payrollAmount.doubleValue() * HUNDRED_DOLLAR_AMOUNT.doubleValue() / totalPayrollAmount.doubleValue();
189
190 return percent;
191 }
192
193 /**
194 * recalculate the effort percent based on the given total amount and payroll amount and return it as of type String
195 *
196 * @param totalPayrollAmount the given total amount
197 * @param payrollAmount the given payroll amount
198 * @return the effort percent as String calculated from the given total amount and payroll amount
199 */
200 public static String recalculateEffortPercentAsString(KualiDecimal totalPayrollAmount, KualiDecimal payrollAmount) {
201 double actualPercentAsDouble = 0;
202 if (totalPayrollAmount.isNonZero()) {
203 actualPercentAsDouble = recalculateEffortPercent(totalPayrollAmount, payrollAmount);
204 }
205
206 return String.format("%.4f%s", actualPercentAsDouble, KFSConstants.PERCENTAGE_SIGN);
207 }
208 }