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.purap.document.validation.impl;
017    
018    import java.util.Date;
019    
020    import org.kuali.kfs.module.purap.PurapConstants;
021    import org.kuali.kfs.module.purap.PurapKeyConstants;
022    import org.kuali.kfs.module.purap.PurapPropertyConstants;
023    import org.kuali.kfs.module.purap.document.PurchasingDocument;
024    import org.kuali.kfs.sys.context.SpringContext;
025    import org.kuali.kfs.sys.document.validation.GenericValidation;
026    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
027    import org.kuali.kfs.sys.service.UniversityDateService;
028    import org.kuali.rice.kns.service.DateTimeService;
029    import org.kuali.rice.kns.util.GlobalVariables;
030    import org.kuali.rice.kns.util.ObjectUtils;
031    
032    public class PurchasingPaymentInfoValidation extends GenericValidation {
033    
034        DateTimeService dateTimeService;
035        UniversityDateService universityDateService;
036        
037        public boolean validate(AttributedDocumentEvent event) {
038            boolean valid = true;
039            PurchasingDocument purDocument = (PurchasingDocument)event.getDocument();
040            
041            GlobalVariables.getMessageMap().addToErrorPath(PurapConstants.PAYMENT_INFO_ERRORS);
042            valid &= checkBeginDateBeforeEndDate(purDocument);
043            if (valid && (ObjectUtils.isNotNull(purDocument.getPurchaseOrderBeginDate()) || ObjectUtils.isNotNull(purDocument.getPurchaseOrderEndDate()))) {
044                if (ObjectUtils.isNotNull(purDocument.getPurchaseOrderBeginDate()) && ObjectUtils.isNull(purDocument.getPurchaseOrderEndDate())) {
045                    GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_END_DATE, PurapKeyConstants.ERROR_PURCHASE_ORDER_BEGIN_DATE_NO_END_DATE);
046                    valid &= false;
047                }
048                else {
049                    if (ObjectUtils.isNull(purDocument.getPurchaseOrderBeginDate()) && ObjectUtils.isNotNull(purDocument.getPurchaseOrderEndDate())) {
050                        GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_BEGIN_DATE, PurapKeyConstants.ERROR_PURCHASE_ORDER_END_DATE_NO_BEGIN_DATE);
051                        valid &= false;
052                    }
053                }
054            }
055            if (valid && ObjectUtils.isNotNull(purDocument.getPurchaseOrderBeginDate()) && ObjectUtils.isNotNull(purDocument.getPurchaseOrderEndDate())) {
056                if (ObjectUtils.isNull(purDocument.getRecurringPaymentTypeCode())) {
057                    GlobalVariables.getMessageMap().putError(PurapPropertyConstants.RECURRING_PAYMENT_TYPE_CODE, PurapKeyConstants.ERROR_RECURRING_DATE_NO_TYPE);
058    
059                    valid &= false;
060                }
061            }
062            else if (valid && ObjectUtils.isNotNull(purDocument.getRecurringPaymentTypeCode())) {
063                GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_BEGIN_DATE, PurapKeyConstants.ERROR_RECURRING_TYPE_NO_DATE);
064                valid &= false;
065            }
066    
067            //checks for when FY is set to next FY
068            if (purDocument.isPostingYearNext()) {
069                Integer currentFY = universityDateService.getCurrentFiscalYear();
070                Date closingDate = universityDateService.getLastDateOfFiscalYear(currentFY);
071    
072                //if recurring payment begin dates entered, begin date must be > closing date
073                if (ObjectUtils.isNotNull(purDocument.getPurchaseOrderBeginDate()) &&
074                       (purDocument.getPurchaseOrderBeginDate().before(closingDate) ||
075                        purDocument.getPurchaseOrderBeginDate().equals(closingDate))) {
076                    GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_BEGIN_DATE, PurapKeyConstants.ERROR_NEXT_FY_BEGIN_DATE_INVALID);
077                    valid &= false;
078                }
079            }
080    
081            GlobalVariables.getMessageMap().removeFromErrorPath(PurapConstants.PAYMENT_INFO_ERRORS);
082            return valid;
083        }
084    
085        /**
086         * Implementation of the rule that if a document has a recurring payment begin date and end date, the begin date should come
087         * before the end date. We needed to play around with this order if the fiscal year is the next fiscal year, since we
088         * were dealing just with month and day, but we don't need to do that here; we're dealing with the whole Date object.
089         * 
090         * @param purDocument the purchasing document to be validated
091         * @return boolean false if the begin date is not before the end date.
092         */
093        protected boolean checkBeginDateBeforeEndDate(PurchasingDocument purDocument) {
094            boolean valid = true;
095    
096            Date beginDate = purDocument.getPurchaseOrderBeginDate();
097            Date endDate = purDocument.getPurchaseOrderEndDate();
098            if (ObjectUtils.isNotNull(beginDate) && ObjectUtils.isNotNull(endDate)) {
099                if (dateTimeService.dateDiff(beginDate, endDate, false) <= 0) {
100                    valid &= false;
101                    GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_END_DATE, PurapKeyConstants.ERROR_PURCHASE_ORDER_BEGIN_DATE_AFTER_END);
102                }
103            }
104    
105            return valid;
106        }
107    
108        public DateTimeService getDateTimeService() {
109            return dateTimeService;
110        }
111    
112        public void setDateTimeService(DateTimeService dateTimeService) {
113            this.dateTimeService = dateTimeService;
114        }
115    
116        public UniversityDateService getUniversityDateService() {
117            return universityDateService;
118        }
119    
120        public void setUniversityDateService(UniversityDateService universityDateService) {
121            this.universityDateService = universityDateService;
122        }
123    
124    }