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 org.apache.commons.lang.StringUtils;
019    import org.kuali.kfs.module.purap.PurapConstants;
020    import org.kuali.kfs.module.purap.PurapKeyConstants;
021    import org.kuali.kfs.module.purap.PurapPropertyConstants;
022    import org.kuali.kfs.module.purap.PurapConstants.PODocumentsStrings;
023    import org.kuali.kfs.module.purap.document.PurchaseOrderDocument;
024    import org.kuali.kfs.module.purap.document.PurchasingAccountsPayableDocument;
025    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
026    import org.kuali.kfs.sys.service.PostalCodeValidationService;
027    import org.kuali.kfs.vnd.VendorPropertyConstants;
028    import org.kuali.kfs.vnd.VendorConstants.VendorTypes;
029    import org.kuali.kfs.vnd.businessobject.VendorDetail;
030    import org.kuali.kfs.vnd.document.service.VendorService;
031    import org.kuali.rice.kns.service.DataDictionaryService;
032    import org.kuali.rice.kns.util.GlobalVariables;
033    import org.kuali.rice.kns.util.MessageMap;
034    import org.kuali.rice.kns.util.ObjectUtils;
035    
036    public class PurchaseOrderProcessVendorValidation extends PurchasingProcessVendorValidation {
037    
038        private DataDictionaryService dataDictionaryService;
039        private PostalCodeValidationService postalCodeValidationService;
040     //   private VendorService vendorService;
041        
042        /**
043         * Validation for the Stipulation tab.
044         * 
045         * @param poDocument the purchase order document to be validated
046         * @return boolean false if the vendor stipulation description is blank.
047         */
048        public boolean validate(AttributedDocumentEvent event) {
049            boolean valid = super.validate(event);
050            PurchasingAccountsPayableDocument purapDocument = (PurchasingAccountsPayableDocument)event.getDocument();
051            PurchaseOrderDocument poDocument = (PurchaseOrderDocument) purapDocument;
052            MessageMap errorMap = GlobalVariables.getMessageMap();
053            errorMap.clearErrorPath();
054            errorMap.addToErrorPath(PurapConstants.VENDOR_ERRORS);
055    
056            // check to see if the vendor exists in the database, i.e. its ID is not null
057            Integer vendorHeaderID = poDocument.getVendorHeaderGeneratedIdentifier();
058            if (ObjectUtils.isNull(vendorHeaderID)) {
059                valid = false;
060                errorMap.putError(VendorPropertyConstants.VENDOR_NAME, PurapKeyConstants.ERROR_NONEXIST_VENDOR);
061            }
062    
063            // vendor active validation...
064            VendorDetail vendorDetail = super.getVendorService().getVendorDetail(poDocument.getVendorHeaderGeneratedIdentifier(), poDocument.getVendorDetailAssignedIdentifier());
065            if (ObjectUtils.isNull(vendorDetail))
066                return valid;
067    
068            // make sure that the vendor is active
069            if (!vendorDetail.isActiveIndicator()) {
070                valid &= false;
071                errorMap.putError(VendorPropertyConstants.VENDOR_NAME, PurapKeyConstants.ERROR_INACTIVE_VENDOR);
072            }
073            
074            // validate vendor address
075            postalCodeValidationService.validateAddress(poDocument.getVendorCountryCode(), poDocument.getVendorStateCode(), poDocument.getVendorPostalCode(), PurapPropertyConstants.VENDOR_STATE_CODE, PurapPropertyConstants.VENDOR_POSTAL_CODE);
076            
077            // Do checks for alternate payee vendor.
078            Integer alternateVendorHdrGeneratedId = poDocument.getAlternateVendorHeaderGeneratedIdentifier();
079            Integer alternateVendorHdrDetailAssignedId = poDocument.getAlternateVendorDetailAssignedIdentifier();
080            
081            VendorDetail alternateVendor = super.getVendorService().getVendorDetail(alternateVendorHdrGeneratedId,alternateVendorHdrDetailAssignedId);
082            
083            if (alternateVendor != null) {
084                if (alternateVendor.isVendorDebarred()) {
085                    errorMap.putError(PurapPropertyConstants.ALTERNATE_VENDOR_NAME,PurapKeyConstants.ERROR_PURCHASE_ORDER_ALTERNATE_VENDOR_DEBARRED);
086                    valid &= false;
087                }
088                if (StringUtils.equals(alternateVendor.getVendorHeader().getVendorTypeCode(), VendorTypes.DISBURSEMENT_VOUCHER)) {
089                    errorMap.putError(PurapPropertyConstants.ALTERNATE_VENDOR_NAME,PurapKeyConstants.ERROR_PURCHASE_ORDER_ALTERNATE_VENDOR_DV_TYPE);
090                    valid &= false;
091                }
092                if (!alternateVendor.isActiveIndicator()) {
093                    errorMap.putError(PurapPropertyConstants.ALTERNATE_VENDOR_NAME,PurapKeyConstants.ERROR_PURCHASE_ORDER_ALTERNATE_VENDOR_INACTIVE,PODocumentsStrings.ALTERNATE_PAYEE_VENDOR);
094                    valid &= false;
095                }
096            }
097    
098            errorMap.clearErrorPath();
099            return valid;
100        }
101    
102        public DataDictionaryService getDataDictionaryService() {
103            return dataDictionaryService;
104        }
105    
106        public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
107            this.dataDictionaryService = dataDictionaryService;
108        }
109        
110        public void setPostalCodeValidationService(PostalCodeValidationService postalCodeValidationService) {
111            this.postalCodeValidationService = postalCodeValidationService;
112        }
113     //   public VendorService getVendorService() {
114     //       return vendorService;
115     //   }
116    
117     //   public void setVendorService(VendorService vendorService) {
118     //       this.vendorService = vendorService;
119     //   }
120    }