1 /*
2 * Copyright 2008 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.validation.impl;
17
18 import java.util.List;
19
20 import org.apache.commons.lang.StringUtils;
21 import org.kuali.ole.fp.document.DisbursementVoucherConstants;
22 import org.kuali.ole.fp.document.DisbursementVoucherDocument;
23 import org.kuali.ole.fp.document.service.DisbursementVoucherTaxService;
24 import org.kuali.ole.sys.OLEKeyConstants;
25 import org.kuali.ole.sys.OLEPropertyConstants;
26 import org.kuali.ole.sys.businessobject.AccountingLine;
27 import org.kuali.ole.sys.businessobject.SourceAccountingLine;
28 import org.kuali.ole.sys.context.SpringContext;
29 import org.kuali.ole.sys.document.AccountingDocument;
30 import org.kuali.ole.sys.document.validation.GenericValidation;
31 import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
32 import org.kuali.rice.core.api.parameter.ParameterEvaluatorService;
33 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
34 import org.kuali.rice.krad.util.GlobalVariables;
35 import org.kuali.rice.krad.util.MessageMap;
36 import org.kuali.rice.krad.util.ObjectUtils;
37
38 public class DisbursementVoucherAccountingLineValidation extends GenericValidation {
39 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DisbursementVoucherAccountingLineValidation.class);
40
41 private ParameterService parameterService;
42
43 private AccountingDocument accountingDocumentForValidation;
44 private AccountingLine accountingLineForValidation;
45
46 protected static final String DV_PAYMENT_REASON_PROPERTY_PATH = OLEPropertyConstants.DV_PAYEE_DETAIL + "." + OLEPropertyConstants.DISB_VCHR_PAYMENT_REASON_CODE;
47 protected static final String DV_PAYEE_ID_NUMBER_PROPERTY_PATH = OLEPropertyConstants.DV_PAYEE_DETAIL + "." + OLEPropertyConstants.DISB_VCHR_PAYEE_ID_NUMBER;
48
49 /**
50 * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
51 */
52 @Override
53 public boolean validate(AttributedDocumentEvent event) {
54 LOG.debug("validate start");
55
56 boolean valid = true;
57 DisbursementVoucherDocument dvDocument = (DisbursementVoucherDocument) accountingDocumentForValidation;
58 MessageMap errors = GlobalVariables.getMessageMap();
59
60 // don't validate generated tax lines
61 if (dvDocument.getDvNonResidentAlienTax() != null) {
62 String lineText = dvDocument.getDvNonResidentAlienTax().getFinancialDocumentAccountingLineText();
63 List<Integer> taxLineNumbers = SpringContext.getBean(DisbursementVoucherTaxService.class).getNRATaxLineNumbers(lineText);
64
65 if (taxLineNumbers.contains(accountingLineForValidation.getSequenceNumber())) {
66 return true;
67 }
68 }
69
70 /* payment reason must be selected before an accounting line can be entered */
71 if (StringUtils.isBlank(dvDocument.getDvPayeeDetail().getDisbVchrPaymentReasonCode())) {
72 if (!errors.containsMessageKey(OLEKeyConstants.ERROR_DV_ADD_LINE_MISSING_PAYMENT_REASON)) {
73 errors.putErrorWithoutFullErrorPath(OLEPropertyConstants.DOCUMENT + "." + DV_PAYMENT_REASON_PROPERTY_PATH, OLEKeyConstants.ERROR_DV_ADD_LINE_MISSING_PAYMENT_REASON);
74 }
75 valid = false;
76 }
77
78 /*
79 * payee must be selected before an accounting line can be entered NOTE: This should never be possible given the new flow
80 * that requires selection of the payee prior to DV creation, but I'm leaving the code in for validity sake. See KFSMI-714
81 * for details on new flow.
82 */
83 if (StringUtils.isBlank(dvDocument.getDvPayeeDetail().getDisbVchrPayeeIdNumber())) {
84 if (!errors.containsMessageKey(OLEKeyConstants.ERROR_DV_ADD_LINE_MISSING_PAYEE)) {
85 errors.putErrorWithoutFullErrorPath(OLEPropertyConstants.DOCUMENT + "." + DV_PAYEE_ID_NUMBER_PROPERTY_PATH, OLEKeyConstants.ERROR_DV_ADD_LINE_MISSING_PAYEE);
86 }
87 valid = false;
88 }
89
90 if (valid) {
91 valid = valid & validateAccountNumber(accountingDocumentForValidation, accountingLineForValidation);
92 valid = valid & validateObjectCode(accountingDocumentForValidation, accountingLineForValidation);
93 }
94
95 return valid;
96 }
97
98 /**
99 * Checks object codes restrictions, including restrictions in parameters table.
100 *
101 * @param FinancialDocument submitted accounting document
102 * @param accountingLine accounting line in accounting document
103 * @return true if object code exists, is active, and object level and code exist for a provided payment reason
104 */
105 public boolean validateObjectCode(AccountingDocument financialDocument, AccountingLine accountingLine) {
106 LOG.debug("beginning object code validation ");
107
108 DisbursementVoucherDocument dvDocument = (DisbursementVoucherDocument) financialDocument;
109 MessageMap errors = GlobalVariables.getMessageMap();
110
111 boolean objectCodeAllowed = true;
112
113 // object code exist done in super, check we have a valid object
114 if (ObjectUtils.isNull(accountingLineForValidation.getAccount()) || ObjectUtils.isNull(accountingLine.getObjectCode())) {
115 return false;
116 }
117
118 // make sure object code is active
119 if (!accountingLine.getObjectCode().isFinancialObjectActiveCode()) {
120 errors.putError(OLEPropertyConstants.FINANCIAL_OBJECT_CODE, OLEKeyConstants.ERROR_INACTIVE, "Object Code");
121 objectCodeAllowed = false;
122 }
123
124 String documentPaymentReason = dvDocument.getDvPayeeDetail().getDisbVchrPaymentReasonCode();
125 if (StringUtils.isBlank(documentPaymentReason)) {
126 return objectCodeAllowed;
127 }
128
129 // check object level is in permitted list for payment reason
130 objectCodeAllowed = objectCodeAllowed && /*REFACTORME*/SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(DisbursementVoucherDocument.class, DisbursementVoucherConstants.VALID_OBJ_LEVEL_BY_PAYMENT_REASON_PARM, DisbursementVoucherConstants.INVALID_OBJ_LEVEL_BY_PAYMENT_REASON_PARM, documentPaymentReason, accountingLine.getObjectCode().getFinancialObjectLevelCode()).evaluateAndAddError(SourceAccountingLine.class, "objectCode.financialObjectLevelCode", OLEPropertyConstants.FINANCIAL_OBJECT_CODE);
131
132 // check object code is in permitted list for payment reason
133 objectCodeAllowed = objectCodeAllowed && /*REFACTORME*/SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(DisbursementVoucherDocument.class, DisbursementVoucherConstants.VALID_OBJ_CODE_BY_PAYMENT_REASON_PARM, DisbursementVoucherConstants.INVALID_OBJ_CODE_BY_PAYMENT_REASON_PARM, documentPaymentReason, accountingLine.getFinancialObjectCode()).evaluateAndAddError(SourceAccountingLine.class, OLEPropertyConstants.FINANCIAL_OBJECT_CODE);
134
135 objectCodeAllowed = objectCodeAllowed && /*REFACTORME*/SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(DisbursementVoucherDocument.class, DisbursementVoucherConstants.VALID_OBJECT_SUB_TYPES_BY_SUB_FUND_GROUP_PARM, DisbursementVoucherConstants.INVALID_OBJECT_SUB_TYPES_BY_SUB_FUND_GROUP_PARM, accountingLine.getAccount().getSubFundGroupCode(), accountingLine.getObjectCode().getFinancialObjectSubTypeCode()).evaluateAndAddError(SourceAccountingLine.class, "objectCode.financialObjectSubTypeCode", OLEPropertyConstants.FINANCIAL_OBJECT_CODE);
136
137 return objectCodeAllowed;
138 }
139
140 /**
141 * Checks account number restrictions, including restrictions in parameters table.
142 *
143 * @param FinancialDocument submitted financial document
144 * @param accountingLine accounting line in submitted accounting document
145 * @return true if account exists, falls within global function code restrictions, and account's sub fund is in permitted list
146 * for payment reason
147 */
148 public boolean validateAccountNumber(AccountingDocument financialDocument, AccountingLine accountingLine) {
149 LOG.debug("beginning account number validation ");
150
151 DisbursementVoucherDocument dvDocument = (DisbursementVoucherDocument) financialDocument;
152 MessageMap errors = GlobalVariables.getMessageMap();
153
154 String errorKey = OLEPropertyConstants.ACCOUNT_NUMBER;
155 boolean accountNumberAllowed = true;
156
157 // account exist and object exist done in super, check we have a valid object
158 if (ObjectUtils.isNull(accountingLine.getAccount()) || ObjectUtils.isNull(accountingLine.getObjectCode())) {
159 return false;
160 }
161 //Commented for the jira issue OLE-3415
162 // global function code restrictions
163 /*if (accountNumberAllowed) {
164 ParameterEvaluator evaluator = SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(DisbursementVoucherDocument.class, DisbursementVoucherConstants.FUNCTION_CODE_GLOBAL_RESTRICTION_PARM_NM, accountingLine.getAccount().getFinancialHigherEdFunctionCd());
165 // accountNumberAllowed is true now
166 accountNumberAllowed = evaluator.evaluateAndAddError(SourceAccountingLine.class, "account.financialHigherEdFunctionCd", OLEPropertyConstants.ACCOUNT_NUMBER);
167 }*/
168
169 String documentPaymentReason = dvDocument.getDvPayeeDetail().getDisbVchrPaymentReasonCode();
170 if (StringUtils.isBlank(documentPaymentReason)) {
171 return accountNumberAllowed;
172 }
173
174 // check sub fund is in permitted list for payment reason
175 accountNumberAllowed = accountNumberAllowed && /*REFACTORME*/SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(DisbursementVoucherDocument.class, DisbursementVoucherConstants.VALID_SUB_FUND_GROUPS_BY_PAYMENT_REASON_PARM, DisbursementVoucherConstants.INVALID_SUB_FUND_GROUPS_BY_PAYMENT_REASON_PARM, documentPaymentReason, accountingLine.getAccount().getSubFundGroupCode()).evaluateAndAddError(SourceAccountingLine.class, "account.subFundGroupCode", OLEPropertyConstants.ACCOUNT_NUMBER);
176
177 return accountNumberAllowed;
178 }
179
180 /**
181 * Sets the accountingDocumentForValidation attribute value.
182 *
183 * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
184 */
185 public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
186 this.accountingDocumentForValidation = accountingDocumentForValidation;
187 }
188
189 /**
190 * Sets the accountingLineForValidation attribute value.
191 *
192 * @param accountingLineForValidation The accountingLineForValidation to set.
193 */
194 public void setAccountingLineForValidation(AccountingLine accountingLineForValidation) {
195 this.accountingLineForValidation = accountingLineForValidation;
196 }
197
198 /**
199 * Sets the parameterService attribute value.
200 * @param parameterService The parameterService to set.
201 */
202 public void setParameterService(ParameterService parameterService) {
203 this.parameterService = parameterService;
204 }
205
206 /**
207 * Gets the accountingDocumentForValidation attribute.
208 * @return Returns the accountingDocumentForValidation.
209 */
210 public AccountingDocument getAccountingDocumentForValidation() {
211 return accountingDocumentForValidation;
212 }
213
214 /**
215 * Gets the accountingLineForValidation attribute.
216 * @return Returns the accountingLineForValidation.
217 */
218 public AccountingLine getAccountingLineForValidation() {
219 return accountingLineForValidation;
220 }
221
222 }