001/*
002 * Copyright 2008 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 */
016package org.kuali.ole.fp.document.service.impl;
017
018import java.util.ArrayList;
019import java.util.Collection;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.commons.lang.StringUtils;
025import org.kuali.ole.fp.businessobject.DisbursementPayee;
026import org.kuali.ole.fp.businessobject.PaymentReasonCode;
027import org.kuali.ole.fp.document.DisbursementVoucherConstants;
028import org.kuali.ole.fp.document.DisbursementVoucherDocument;
029import org.kuali.ole.fp.document.service.DisbursementVoucherPayeeService;
030import org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService;
031import org.kuali.ole.sys.OLEConstants;
032import org.kuali.ole.sys.OLEKeyConstants;
033import org.kuali.ole.sys.OLEPropertyConstants;
034import org.kuali.ole.sys.context.SpringContext;
035import org.kuali.rice.core.api.parameter.ParameterEvaluatorService;
036import org.kuali.rice.coreservice.framework.parameter.ParameterService;
037import org.kuali.rice.kns.util.MessageList;
038import org.kuali.rice.krad.service.BusinessObjectService;
039
040/**
041 * implementing the service methods defined in DisbursementVoucherPaymentReasonService
042 * 
043 * @see DisbursementVoucherPaymentReasonService
044 */
045public class DisbursementVoucherPaymentReasonServiceImpl implements DisbursementVoucherPaymentReasonService {
046    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DisbursementVoucherPaymentReasonServiceImpl.class);
047
048    protected ParameterService parameterService;
049    protected BusinessObjectService businessObjectService;
050    protected DisbursementVoucherPayeeService disbursementVoucherPayeeService;
051
052    /**
053     * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#isPayeeQualifiedForPayment(org.kuali.ole.fp.businessobject.DisbursementPayee,
054     *      java.lang.String)
055     */
056    public boolean isPayeeQualifiedForPayment(DisbursementPayee payee, String paymentReasonCode) {
057        Collection<String> payeeTypeCodes = this.getPayeeTypesByPaymentReason(paymentReasonCode);
058        return this.isPayeeQualifiedForPayment(payee, paymentReasonCode, payeeTypeCodes);
059    }
060
061    /**
062     * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#isPayeeQualifiedForPayment(org.kuali.ole.fp.businessobject.DisbursementPayee,
063     *      java.lang.String, java.util.List)
064     */
065    public boolean isPayeeQualifiedForPayment(DisbursementPayee payee, String paymentReasonCode, Collection<String> payeeTypeCodes) {
066        if (payeeTypeCodes == null || payeeTypeCodes.isEmpty()) {
067            return false;
068        }
069
070        String payeeTypeCode = payee.getPayeeTypeCode();
071        if (!payeeTypeCodes.contains(payeeTypeCode)) {
072            return false;
073        }
074        
075        if (disbursementVoucherPayeeService.isVendor(payee)) {
076            Collection<String> vendorOwnershipTypeCodes = this.getVendorOwnershipTypesByPaymentReason(paymentReasonCode);
077            
078            if (vendorOwnershipTypeCodes != null && !vendorOwnershipTypeCodes.isEmpty()) {                
079                String vendorOwnershipTypeCodeOfPayee = disbursementVoucherPayeeService.getVendorOwnershipTypeCode(payee);
080                return vendorOwnershipTypeCodes.contains(vendorOwnershipTypeCodeOfPayee);
081            } 
082        }
083      //Commented for the jira issue OLE-3415
084        /*if (this.isPrepaidTravelPaymentReason(paymentReasonCode)) {
085            boolean isActiveVendorEmployee = payee.isActive();
086            isActiveVendorEmployee &= disbursementVoucherPayeeService.isVendor(payee);
087            isActiveVendorEmployee &= disbursementVoucherPayeeService.isEmployee(payee);
088
089            // Active vendor employees cannot be paid for prepaid travel
090            return !isActiveVendorEmployee;
091        }*/
092
093        return true;
094    }
095
096    /**
097     * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#isMovingPaymentReason(java.lang.String)
098     */
099    public boolean isMovingPaymentReason(String paymentReasonCode) {
100        String typeParameterName = DisbursementVoucherConstants.MOVING_PAYMENT_REASONS_PARM_NM;
101        return this.isPaymentReasonOfType(typeParameterName, paymentReasonCode);
102    }
103
104    /**
105     * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#isPrepaidTravelPaymentReason(java.lang.String)
106     */
107    public boolean isPrepaidTravelPaymentReason(String paymentReasonCode) {
108        String typeParameterName = DisbursementVoucherConstants.PREPAID_TRAVEL_PAYMENT_REASONS_PARM_NM;
109        return this.isPaymentReasonOfType(typeParameterName, paymentReasonCode);
110    }
111    
112    /**
113     * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#isNonEmployeeTravelPaymentReason(java.lang.String)
114     */
115    public boolean isNonEmployeeTravelPaymentReason(String paymentReasonCode) {
116        String typeParameterName = DisbursementVoucherConstants.NONEMPLOYEE_TRAVEL_PAY_REASONS_PARM_NM;
117        return this.isPaymentReasonOfType(typeParameterName, paymentReasonCode);
118    }
119
120    /**
121     * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#isResearchPaymentReason(java.lang.String)
122     */
123    public boolean isResearchPaymentReason(String paymentReasonCode) {
124        String typeParameterName = DisbursementVoucherConstants.RESEARCH_PAYMENT_REASONS_PARM_NM;
125        return this.isPaymentReasonOfType(typeParameterName, paymentReasonCode);
126    }
127
128    /**
129     * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#isRevolvingFundPaymentReason(java.lang.String)
130     */
131    public boolean isRevolvingFundPaymentReason(String paymentReasonCode) {
132        String typeParameterName = DisbursementVoucherConstants.REVOLVING_FUND_PAYMENT_REASONS_PARM_NM;
133        return this.isPaymentReasonOfType(typeParameterName, paymentReasonCode);
134    }
135    
136    /**
137     * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#isDecedentCompensationPaymentReason(java.lang.String)
138     */
139    public boolean isDecedentCompensationPaymentReason(String paymentReasonCode) {
140        String typeParameterName = DisbursementVoucherConstants.DECEDENT_COMPENSATION_PAYMENT_REASONS_PARM_NM;
141        return this.isPaymentReasonOfType(typeParameterName, paymentReasonCode);
142    }
143
144    /**
145     * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#isPaymentReasonOfType(java.lang.String,
146     *      java.lang.String)
147     */
148    public boolean isPaymentReasonOfType(String typeParameterName, String paymentReasonCode) {
149        return /*REFACTORME*/SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(DisbursementVoucherDocument.class, typeParameterName, paymentReasonCode).evaluationSucceeds();
150    }
151
152    /**
153     * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#getReserchNonVendorPayLimit()
154     */
155    public String getReserchNonVendorPayLimit() {
156        return parameterService.getParameterValueAsString(DisbursementVoucherDocument.class, DisbursementVoucherConstants.RESEARCH_NON_VENDOR_PAY_LIMIT_AMOUNT_PARM_NM);
157    }
158
159    /**
160     * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#getPayeeTypesByPaymentReason(java.lang.String)
161     */
162    public Collection<String> getPayeeTypesByPaymentReason(String paymentReasonCode) {
163        return parameterService.getSubParameterValuesAsString(DisbursementVoucherDocument.class, DisbursementVoucherConstants.VALID_PAYEE_TYPES_BY_PAYMENT_REASON_PARM, paymentReasonCode);
164    }
165    
166    /**
167     * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#getVendorOwnershipTypesByPaymentReason(java.lang.String)
168     */
169    public Collection<String> getVendorOwnershipTypesByPaymentReason(String paymentReasonCode) {
170        return parameterService.getSubParameterValuesAsString(DisbursementVoucherDocument.class, DisbursementVoucherConstants.VALID_VENDOR_OWNERSHIP_TYPES_BY_PAYMENT_REASON, paymentReasonCode);
171    }
172
173    /**
174     * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#getPaymentReasonByPrimaryId(java.lang.String)
175     */
176    public PaymentReasonCode getPaymentReasonByPrimaryId(String paymentReasonCode) {
177        Map<String, Object> primaryKeys = new HashMap<String, Object>();
178        primaryKeys.put(OLEPropertyConstants.CODE, paymentReasonCode);
179
180        return (PaymentReasonCode) businessObjectService.findByPrimaryKey(PaymentReasonCode.class, primaryKeys);
181    }
182    
183    /**
184     * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#isTaxReviewRequired(java.lang.String)
185     */
186    public boolean isTaxReviewRequired(String paymentReasonCode) {
187        String parameterName = DisbursementVoucherConstants.PAYMENT_REASONS_REQUIRING_TAX_REVIEW_PARM_NM;
188        List<String> values = new ArrayList<String>( parameterService.getParameterValuesAsString(DisbursementVoucherDocument.class, parameterName) );
189        
190        return values != null && values.contains(paymentReasonCode);
191    }
192
193    /**
194     * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#postPaymentReasonCodeUsage(java.lang.String,
195     *      org.kuali.rice.kns.util.MessageList)
196     */
197    public void postPaymentReasonCodeUsage(String paymentReasonCode, MessageList messageList) {
198        Collection<String> payeeTypeCodes = this.getPayeeTypesByPaymentReason(paymentReasonCode);
199        if (payeeTypeCodes == null || payeeTypeCodes.isEmpty()) {
200            return;
201        }
202
203        String descriptivePayeeTypes = this.getDescriptivePayeeTypesAsString(payeeTypeCodes);
204        String descriptivePaymentReason = this.getPaymentReasonByPrimaryId(paymentReasonCode).getCodeAndDescription();
205        if (payeeTypeCodes.size() > 1) {
206            String messageKey = OLEKeyConstants.WARNING_DV_PAYMENT_REASON_VALID_FOR_MULTIPLE_PAYEE_TYPES;
207            messageList.add(messageKey, descriptivePaymentReason, descriptivePayeeTypes);
208        }
209        else if (payeeTypeCodes.size() == 1) {
210            String messageKey = OLEKeyConstants.WARNING_DV_PAYMENT_REASON_VALID_FOR_SINGEL_PAYEE_TYPE;
211            messageList.add(messageKey, descriptivePaymentReason, descriptivePayeeTypes);
212        }
213      //Commented for the jira issue OLE-3415
214        /*if (this.isResearchPaymentReason(paymentReasonCode)) {
215            String payLimit = this.getReserchNonVendorPayLimit();
216            String messageKey = OLEKeyConstants.WARNING_DV_REASERCH_PAYMENT_REASON;
217
218            List<String> vendorTypeCodes = new ArrayList<String>();
219            vendorTypeCodes.addAll(payeeTypeCodes);
220            vendorTypeCodes.remove(DisbursementVoucherConstants.DV_PAYEE_TYPE_EMPLOYEE);
221            String vendorTypes = this.getDescriptivePayeeTypesAsString(vendorTypeCodes);
222
223            messageList.add(messageKey, descriptivePaymentReason, descriptivePayeeTypes, vendorTypes, payLimit);
224        }
225
226        if (this.isMovingPaymentReason(paymentReasonCode)) {
227            List<String> individualOwnerShipTypeCodes = new ArrayList<String>( parameterService.getParameterValuesAsString(DisbursementVoucherDocument.class, DisbursementVoucherConstants.INDIVIDUAL_OWNERSHIP_TYPES_PARM_NM) );
228            String ownerShipTypeAsString = this.convertListToString(individualOwnerShipTypeCodes);
229
230            String messageKey = OLEKeyConstants.WARNING_DV_MOVING_PAYMENT_REASON;
231            messageList.add(messageKey, ownerShipTypeAsString);
232        }
233
234        if (this.isPrepaidTravelPaymentReason(paymentReasonCode)) {
235            String messageKey = OLEKeyConstants.WARNING_DV_PREPAID_TRAVEL_PAYMENT_REASON;
236            messageList.add(messageKey, descriptivePaymentReason, descriptivePayeeTypes);
237        }*/
238    }
239
240    // get and concatenate the descriptive payee types of the given codes
241    protected String getDescriptivePayeeTypesAsString(Collection<String> payeeTypeCodes) {
242        List<String> payeeTypeDescriptions = new ArrayList<String>();
243
244        for (String payeeTypeCode : payeeTypeCodes) {
245            String description = SpringContext.getBean(DisbursementVoucherPayeeService.class).getPayeeTypeDescription(payeeTypeCode);
246            payeeTypeDescriptions.add(description);
247        }
248
249        return this.convertListToString(payeeTypeDescriptions);
250    }
251
252    protected String convertListToString(List<String> list) {
253        if (list == null || list.isEmpty()) {
254            return StringUtils.EMPTY;
255        }
256
257        String oneSpace = " ";
258        StringBuilder listAsString = new StringBuilder();
259        for (int index = 0; index < list.size(); index++) {
260            String emlement = list.get(index);
261
262            if (index == 0) {
263                listAsString.append(emlement);
264            }
265            else if (index < list.size() - 1) {
266                listAsString.append(OLEConstants.COMMA).append(oneSpace).append(emlement);
267            }
268            else if (index == list.size() - 1) {
269                listAsString.append(oneSpace).append(OLEConstants.AND).append(oneSpace).append(emlement);
270            }
271        }
272
273        return listAsString.toString();
274    }
275
276    /**
277     * Sets the parameterService attribute value.
278     * 
279     * @param parameterService The parameterService to set.
280     */
281    public void setParameterService(ParameterService parameterService) {
282        this.parameterService = parameterService;
283    }
284
285    /**
286     * Sets the businessObjectService attribute value.
287     * 
288     * @param businessObjectService The businessObjectService to set.
289     */
290    public void setBusinessObjectService(BusinessObjectService businessObjectService) {
291        this.businessObjectService = businessObjectService;
292    }
293
294    /**
295     * Sets the disbursementVoucherPayeeService attribute value.
296     * 
297     * @param disbursementVoucherPayeeService The disbursementVoucherPayeeService to set.
298     */
299    public void setDisbursementVoucherPayeeService(DisbursementVoucherPayeeService disbursementVoucherPayeeService) {
300        this.disbursementVoucherPayeeService = disbursementVoucherPayeeService;
301    }
302}