1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.select.document.service.impl;
17
18 import org.kuali.ole.coa.businessobject.Account;
19 import org.kuali.ole.gl.businessobject.Balance;
20 import org.kuali.ole.module.purap.businessobject.PaymentRequestAccount;
21 import org.kuali.ole.select.businessobject.OlePaymentRequestItem;
22 import org.kuali.ole.select.businessobject.OleSufficientFundCheck;
23 import org.kuali.ole.select.document.OlePaymentRequestDocument;
24 import org.kuali.ole.select.document.service.OlePaymentRequestFundCheckService;
25 import org.kuali.ole.sys.OLEConstants;
26 import org.kuali.ole.sys.OLEPropertyConstants;
27 import org.kuali.ole.sys.businessobject.SourceAccountingLine;
28 import org.kuali.ole.sys.context.SpringContext;
29 import org.kuali.ole.sys.service.UniversityDateService;
30 import org.kuali.rice.core.api.util.type.KualiDecimal;
31 import org.kuali.rice.krad.service.BusinessObjectService;
32
33 import java.text.DateFormat;
34 import java.text.SimpleDateFormat;
35 import java.util.Date;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39
40 public class OlePaymentRequestFundCheckServiceImpl implements OlePaymentRequestFundCheckService {
41
42 @Override
43 public boolean hasSufficientFundCheckRequired(SourceAccountingLine accLine) {
44 boolean hasSufficientFundRequired = false;
45 Map searchMap = new HashMap();
46 Map<String, Object> key = new HashMap<String, Object>();
47 String chartCode = accLine.getChartOfAccountsCode();
48 String accNo = accLine.getAccountNumber();
49 String objectCd = accLine.getFinancialObjectCode();
50 String fundCodeType = getFundCode(chartCode, accNo);
51 KualiDecimal budgetAllocation = KualiDecimal.ZERO;
52 KualiDecimal expenseAmt = KualiDecimal.ZERO;
53 if (fundCodeType != null && fundCodeType.equals(OLEConstants.ACCOUNT_FUND_CODE)) {
54 budgetAllocation = getBudgetAllocationForAccount(chartCode, accNo, objectCd);
55 expenseAmt = getSumPaidInvoicesForAccount(chartCode, accNo, objectCd);
56 expenseAmt = expenseAmt.subtract(accLine.getAmount());
57 if (expenseAmt.isNegative()) {
58 budgetAllocation = budgetAllocation.subtract(expenseAmt.negated());
59 } else {
60 budgetAllocation = budgetAllocation.subtract(expenseAmt);
61 }
62 if (accLine.getAmount().isGreaterThan(budgetAllocation)) {
63 hasSufficientFundRequired = true;
64 } else {
65 hasSufficientFundRequired = false;
66 }
67 } else if (fundCodeType != null && fundCodeType.equals(OLEConstants.OBJECT_FUND_CODE)) {
68 budgetAllocation = getBudgetAllocationForObject(chartCode, accNo, objectCd);
69 expenseAmt = getSumPaidInvoicesForObject(chartCode, accNo, objectCd);
70 expenseAmt = expenseAmt.subtract(accLine.getAmount());
71 if (expenseAmt.isNegative()) {
72 budgetAllocation = budgetAllocation.subtract(expenseAmt.negated());
73 } else {
74 budgetAllocation = budgetAllocation.subtract(expenseAmt);
75 }
76 if (accLine.getAmount().isGreaterThan(budgetAllocation)) {
77 hasSufficientFundRequired = true;
78 } else {
79 hasSufficientFundRequired = false;
80 }
81 }
82 return hasSufficientFundRequired;
83 }
84
85 private String getFundCode(String chartCode, String accountNumber) {
86 Map accountMap = new HashMap();
87 accountMap.put("chartOfAccountsCode", chartCode);
88 accountMap.put("accountNumber", accountNumber);
89 Account account = SpringContext.getBean(BusinessObjectService.class)
90 .findByPrimaryKey(Account.class, accountMap);
91 boolean fundCheckIndication = false;
92 if (account != null) {
93 fundCheckIndication = account.isPendingAcctSufficientFundsIndicator();
94 }
95
96 if (fundCheckIndication) {
97 String fundCode = account.getAccountSufficientFundsCode();
98 return fundCode;
99 }
100 return null;
101 }
102
103 public KualiDecimal getSumPaidInvoicesForAccount(String chartCode, String accountNo, String objectCode) {
104 KualiDecimal paidInvoices = KualiDecimal.ZERO;
105 List<OlePaymentRequestDocument> payDocList = (List<OlePaymentRequestDocument>) SpringContext.getBean(
106 BusinessObjectService.class).findAll(OlePaymentRequestDocument.class);
107 if (payDocList.size() > 0) {
108 for (OlePaymentRequestDocument olePaymentRequestDocument : payDocList) {
109 Integer payReqId = olePaymentRequestDocument.getPurapDocumentIdentifier();
110 Map docMap = new HashMap();
111 docMap.put(OLEConstants.PUR_AP_IDEN, payReqId);
112 docMap.put(OLEConstants.ITM_TYP_CD_KEY, OLEConstants.ITM_TYP_CD);
113 List<OlePaymentRequestItem> itemList = (List<OlePaymentRequestItem>) SpringContext.getBean(
114 BusinessObjectService.class).findMatching(OlePaymentRequestItem.class, docMap);
115 HashMap acctMap = new HashMap();
116 for (OlePaymentRequestItem olePaymentRequestItem : itemList) {
117 Integer itemIdentifier = olePaymentRequestItem.getItemIdentifier();
118 acctMap.put("itemIdentifier", olePaymentRequestItem.getItemIdentifier());
119 acctMap.put("chartOfAccountsCode", chartCode);
120 acctMap.put("accountNumber", accountNo);
121 List<PaymentRequestAccount> olePaymentRequestAccount = (List<PaymentRequestAccount>) SpringContext
122 .getBean(BusinessObjectService.class).findMatching(PaymentRequestAccount.class, acctMap);
123 for (PaymentRequestAccount payReqAcct : olePaymentRequestAccount) {
124 paidInvoices = paidInvoices.add(payReqAcct.getAmount());
125 }
126 }
127 }
128 }
129 return paidInvoices;
130 }
131
132
133 public KualiDecimal getSumPaidInvoicesForObject(String chartCode, String accountNo, String objectCode) {
134 KualiDecimal paidInvoices = KualiDecimal.ZERO;
135 List<OlePaymentRequestDocument> payDocList = (List<OlePaymentRequestDocument>) SpringContext.getBean(
136 BusinessObjectService.class).findAll(OlePaymentRequestDocument.class);
137 if (payDocList.size() > 0) {
138 for (OlePaymentRequestDocument olePaymentRequestDocument : payDocList) {
139 Integer payReqId = olePaymentRequestDocument.getPurapDocumentIdentifier();
140 Map docMap = new HashMap();
141 docMap.put(OLEConstants.PUR_AP_IDEN, payReqId);
142 docMap.put(OLEConstants.ITM_TYP_CD_KEY, OLEConstants.ITM_TYP_CD);
143 List<OlePaymentRequestItem> itemList = (List<OlePaymentRequestItem>) SpringContext.getBean(
144 BusinessObjectService.class).findMatching(OlePaymentRequestItem.class, docMap);
145 HashMap acctMap = new HashMap();
146 for (OlePaymentRequestItem olePaymentRequestItem : itemList) {
147 acctMap.put("itemIdentifier", olePaymentRequestItem.getItemIdentifier());
148 acctMap.put("chartOfAccountsCode", chartCode);
149 acctMap.put("accountNumber", accountNo);
150 acctMap.put("financialObjectCode", objectCode);
151 List<PaymentRequestAccount> olePaymentRequestAccount = (List<PaymentRequestAccount>) SpringContext
152 .getBean(BusinessObjectService.class).findMatching(PaymentRequestAccount.class, acctMap);
153 for (PaymentRequestAccount payReqAcct : olePaymentRequestAccount) {
154 paidInvoices = paidInvoices.add(payReqAcct.getAmount());
155 }
156 }
157 }
158 }
159 return paidInvoices;
160 }
161
162 private KualiDecimal getBudgetAllocationForObject(String chartCode, String accountNumber, String objectCode) {
163 Map budgetMap = new HashMap();
164 Date date = new Date();
165 DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
166 String univFiscalYear = df.format(date);
167 UniversityDateService universityDateService = SpringContext.getBean(UniversityDateService.class);
168 budgetMap.put("universityFiscalYear",
169 universityDateService.getCurrentUniversityDate().getUniversityFiscalYear());
170 budgetMap.put("chartOfAccountsCode", chartCode);
171 budgetMap.put("accountNumber", accountNumber);
172 budgetMap.put("objectCode", objectCode);
173 budgetMap.put(OLEPropertyConstants.BALANCE_TYPE_CODE, OLEPropertyConstants.BAL_TYP_CODE);
174 Balance balance = SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(Balance.class, budgetMap);
175 KualiDecimal initialBudgetAmount = KualiDecimal.ZERO;
176 if (balance != null) {
177 initialBudgetAmount = balance.getAccountLineAnnualBalanceAmount();
178 }
179 Map encMap = new HashMap();
180 encMap.put("chartOfAccountsCode", chartCode);
181 encMap.put("accountNumber", accountNumber);
182 OleSufficientFundCheck oleSufficientFundCheck = SpringContext.getBean(BusinessObjectService.class)
183 .findByPrimaryKey(OleSufficientFundCheck.class, encMap);
184 KualiDecimal amount = KualiDecimal.ZERO;
185 if (oleSufficientFundCheck != null) {
186 if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_TYP_PERCENTAGE.equals(oleSufficientFundCheck
187 .getEncumbExpenseConstraintType())) {
188 amount = new KualiDecimal(oleSufficientFundCheck.getExpenseAmount());
189 if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_OVER.equals(oleSufficientFundCheck
190 .getEncumbExpenseMethod())) {
191 initialBudgetAmount = initialBudgetAmount.add((initialBudgetAmount.multiply(amount))
192 .divide(new KualiDecimal(100)));
193 } else if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_UNDER.equals(oleSufficientFundCheck
194 .getEncumbExpenseMethod())) {
195 initialBudgetAmount = initialBudgetAmount.subtract((initialBudgetAmount.multiply(amount))
196 .divide(new KualiDecimal(100)));
197
198 }
199 } else if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_TYP_CASH.equals(oleSufficientFundCheck
200 .getEncumbExpenseConstraintType())) {
201 amount = new KualiDecimal(oleSufficientFundCheck.getExpenseAmount());
202 if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_OVER.equals(oleSufficientFundCheck
203 .getEncumbExpenseMethod())) {
204 initialBudgetAmount = initialBudgetAmount.add(amount);
205 } else if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_UNDER.equals(oleSufficientFundCheck
206 .getEncumbExpenseMethod())) {
207 initialBudgetAmount = initialBudgetAmount.subtract(amount);
208 }
209 }
210 }
211 return initialBudgetAmount;
212 }
213
214 private KualiDecimal getBudgetAllocationForAccount(String chartCode, String accountNumber, String objectCode) {
215 Map budgetMap = new HashMap();
216 KualiDecimal initialBudgetAmount = KualiDecimal.ZERO;
217 UniversityDateService universityDateService = SpringContext.getBean(UniversityDateService.class);
218 budgetMap.put("universityFiscalYear",
219 universityDateService.getCurrentUniversityDate().getUniversityFiscalYear());
220 budgetMap.put("chartOfAccountsCode", chartCode);
221 budgetMap.put("accountNumber", accountNumber);
222 budgetMap.put(OLEPropertyConstants.BALANCE_TYPE_CODE, OLEPropertyConstants.BAL_TYP_CODE);
223 List<Balance> balance = (List<Balance>) SpringContext.getBean(BusinessObjectService.class).findMatching(
224 Balance.class, budgetMap);
225 if (balance.size() > 0) {
226 for (Balance budgetBalance : balance) {
227 initialBudgetAmount = initialBudgetAmount.add(budgetBalance.getAccountLineAnnualBalanceAmount());
228 }
229 }
230 Map encMap = new HashMap();
231 encMap.put("chartOfAccountsCode", chartCode);
232 encMap.put("accountNumber", accountNumber);
233 OleSufficientFundCheck oleSufficientFundCheck = SpringContext.getBean(BusinessObjectService.class)
234 .findByPrimaryKey(OleSufficientFundCheck.class, encMap);
235 KualiDecimal amount = KualiDecimal.ZERO;
236 if (oleSufficientFundCheck != null) {
237 if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_TYP_PERCENTAGE.equals(oleSufficientFundCheck
238 .getEncumbExpenseConstraintType())) {
239 amount = new KualiDecimal(oleSufficientFundCheck.getExpenseAmount());
240 if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_OVER.equals(oleSufficientFundCheck
241 .getEncumbExpenseMethod())) {
242 initialBudgetAmount = initialBudgetAmount.add((initialBudgetAmount.multiply(amount))
243 .divide(new KualiDecimal(100)));
244 } else if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_UNDER.equals(oleSufficientFundCheck
245 .getEncumbExpenseMethod())) {
246 initialBudgetAmount = initialBudgetAmount.subtract((initialBudgetAmount.multiply(amount))
247 .divide(new KualiDecimal(100)));
248
249 }
250 } else if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_TYP_CASH.equals(oleSufficientFundCheck
251 .getEncumbExpenseConstraintType())) {
252 amount = new KualiDecimal(oleSufficientFundCheck.getExpenseAmount());
253 if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_OVER.equals(oleSufficientFundCheck
254 .getEncumbExpenseMethod())) {
255 initialBudgetAmount = initialBudgetAmount.add(amount);
256 } else if (OLEPropertyConstants.SUFFICIENT_FUND_ENC_UNDER.equals(oleSufficientFundCheck
257 .getEncumbExpenseMethod())) {
258 initialBudgetAmount = initialBudgetAmount.subtract(amount);
259 }
260 }
261 }
262 return initialBudgetAmount;
263 }
264
265
266 }