001/*
002 * Copyright 2011 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 1.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/ecl1.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.select.document.validation.impl;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.ole.module.purap.PurapConstants;
020import org.kuali.ole.module.purap.PurapKeyConstants;
021import org.kuali.ole.module.purap.PurapParameterConstants;
022import org.kuali.ole.module.purap.PurapRuleConstants;
023import org.kuali.ole.module.purap.document.PurchasingDocument;
024import org.kuali.ole.module.purap.document.PurchasingDocumentBase;
025import org.kuali.ole.module.purap.document.validation.impl.PurchasingProcessVendorValidation;
026import org.kuali.ole.select.OleSelectConstant;
027import org.kuali.ole.select.service.OleForiegnVendorPhoneNumberService;
028import org.kuali.ole.sys.OLEConstants;
029import org.kuali.ole.sys.OLEKeyConstants;
030import org.kuali.ole.sys.context.SpringContext;
031import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
032import org.kuali.ole.sys.service.impl.OleParameterConstants;
033import org.kuali.ole.vnd.VendorKeyConstants;
034import org.kuali.ole.vnd.VendorPropertyConstants;
035import org.kuali.ole.vnd.businessobject.VendorAddress;
036import org.kuali.ole.vnd.businessobject.VendorDetail;
037import org.kuali.ole.vnd.businessobject.VendorHeader;
038import org.kuali.ole.vnd.document.service.VendorService;
039import org.kuali.rice.coreservice.framework.parameter.ParameterService;
040import org.kuali.rice.kns.datadictionary.validation.fieldlevel.PhoneNumberValidationPattern;
041import org.kuali.rice.kns.service.DataDictionaryService;
042import org.kuali.rice.krad.util.GlobalVariables;
043import org.kuali.rice.krad.util.MessageMap;
044import org.kuali.rice.krad.util.ObjectUtils;
045
046import java.util.ArrayList;
047import java.util.List;
048
049public class OlePurchasingProcessVendorValidation extends PurchasingProcessVendorValidation {
050
051    private VendorService vendorService;
052    private ParameterService parameterService;
053    private boolean currencyTypeIndicator = true;
054    /**
055     * @see org.kuali.ole.module.purap.document.validation.impl.PurchasingProcessVendorValidation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
056     */
057    @Override
058    public boolean validate(AttributedDocumentEvent event) {
059        boolean valid = true;
060        PurchasingDocument purDocument = (PurchasingDocument) event.getDocument();
061        MessageMap errorMap = GlobalVariables.getMessageMap();
062        errorMap.clearErrorPath();
063        errorMap.addToErrorPath(PurapConstants.VENDOR_ERRORS);
064        VendorDetail vendorDetail = vendorService.getVendorDetail(purDocument.getVendorHeaderGeneratedIdentifier(), purDocument.getVendorDetailAssignedIdentifier());
065        if (ObjectUtils.isNull(vendorDetail)) {
066            return valid;
067        }
068        VendorHeader vendorHeader = vendorDetail.getVendorHeader();
069
070        // make sure that the vendor is not debarred
071        /*if (vendorDetail.isVendorDebarred()) {
072            valid &= false;
073            errorMap.putError(VendorPropertyConstants.VENDOR_NAME, PurapKeyConstants.ERROR_DEBARRED_VENDOR);
074        }*/
075
076        if (vendorDetail.isVendorDebarred()) {
077            if (parameterService.getParameterValueAsBoolean(OLEConstants.OptionalModuleNamespaces.PURCHASING_ACCOUNTS_PAYABLE, "Requisition", PurapParameterConstants.SHOW_DEBARRED_VENDOR_WARNING_IND)) {
078                if (StringUtils.isEmpty(((PurchasingDocumentBase) purDocument).getJustification())) {
079                    errorMap.putWarning(VendorPropertyConstants.VENDOR_NAME, PurapKeyConstants.WARNING_DEBARRED_VENDOR, vendorDetail.getVendorName());
080                    valid &= false;
081                }
082            } else {
083                errorMap.putError(VendorPropertyConstants.VENDOR_NAME, PurapKeyConstants.ERROR_DEBARRED_VENDOR);
084                valid &= false;
085            }
086        }
087
088        // make sure that the vendor is of allowed type
089        List<String> allowedVendorTypes = new ArrayList<String>(parameterService.getParameterValuesAsString(OleParameterConstants.PURCHASING_DOCUMENT.class, PurapRuleConstants.PURAP_VENDOR_TYPE_ALLOWED_ON_REQ_AND_PO));
090        if (allowedVendorTypes != null && !allowedVendorTypes.isEmpty()) {
091            if (ObjectUtils.isNotNull(vendorHeader) && ObjectUtils.isNotNull(vendorHeader.getVendorTypeCode()) && !allowedVendorTypes.contains(vendorHeader.getVendorTypeCode())) {
092                valid &= false;
093                errorMap.putError(VendorPropertyConstants.VENDOR_NAME, PurapKeyConstants.ERROR_INVALID_VENDOR_TYPE);
094            }
095        }
096
097        if (purDocument.getRequisitionSourceCode() != null && !purDocument.getRequisitionSourceCode().equals(PurapConstants.RequisitionSources.B2B)) {
098
099            //If there is a vendor and the transmission method is FAX and the fax number is blank, display
100            //error that the fax number is required.
101            if (purDocument.getVendorHeaderGeneratedIdentifier() != null && purDocument.getPurchaseOrderTransmissionMethodCode().equals(PurapConstants.POTransmissionMethods.FAX) && StringUtils.isBlank(purDocument.getVendorFaxNumber())) {
102                valid &= false;
103                String attributeLabel = SpringContext.getBean(DataDictionaryService.class).
104                        getDataDictionary().getBusinessObjectEntry(VendorAddress.class.getName()).
105                        getAttributeDefinition(VendorPropertyConstants.VENDOR_FAX_NUMBER).getLabel();
106                errorMap.putError(VendorPropertyConstants.VENDOR_FAX_NUMBER, OLEKeyConstants.ERROR_REQUIRED, attributeLabel);
107            }
108            //boolean foriegnVendor = vendorHeader.getVendorForeignIndicator();
109            if (vendorDetail.getCurrencyType()!=null){
110                if(vendorDetail.getCurrencyType().getCurrencyType().equalsIgnoreCase(OleSelectConstant.CURRENCY_TYPE_NAME)){
111                    currencyTypeIndicator=true;
112                }
113                else{
114                    currencyTypeIndicator=false;
115                }
116            }
117            if (StringUtils.isNotBlank(purDocument.getVendorFaxNumber())) {
118                PhoneNumberValidationPattern phonePattern = new PhoneNumberValidationPattern();
119                if (!currencyTypeIndicator) {
120                    if (StringUtils.isNotEmpty(purDocument.getVendorFaxNumber()) && !SpringContext.getBean(OleForiegnVendorPhoneNumberService.class).isValidForiegnVendorPhoneNumber(purDocument.getVendorFaxNumber())) {
121                        GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_FAX_NUMBER, VendorKeyConstants.ERROR_FORIEGN_VENDOR_FAX_NUMBER);
122                        valid &= false;
123                    }
124                } else {
125                    if (!phonePattern.matches(purDocument.getVendorFaxNumber())) {
126                        valid &= false;
127                        errorMap.putError(VendorPropertyConstants.VENDOR_FAX_NUMBER, PurapKeyConstants.ERROR_FAX_NUMBER_INVALID);
128                    }
129                }
130            }
131        }
132
133        // make sure that the vendor is active
134        if (!vendorDetail.isActiveIndicator()) {
135            valid &= false;
136            errorMap.putError(VendorPropertyConstants.VENDOR_NAME, PurapKeyConstants.ERROR_INACTIVE_VENDOR);
137        }
138
139        errorMap.clearErrorPath();
140        return valid;
141
142    }
143
144    /**
145     * Gets the vendorService attribute.
146     *
147     * @return Returns the vendorService.
148     */
149    @Override
150    public VendorService getVendorService() {
151        return vendorService;
152    }
153
154    /**
155     * Sets the vendorService attribute value.
156     *
157     * @param vendorService The vendorService to set.
158     */
159    @Override
160    public void setVendorService(VendorService vendorService) {
161        this.vendorService = vendorService;
162    }
163
164    /**
165     * Gets the parameterService attribute.
166     *
167     * @return Returns the parameterService.
168     */
169    @Override
170    public ParameterService getParameterService() {
171        return parameterService;
172    }
173
174    /**
175     * Sets the parameterService attribute value.
176     *
177     * @param parameterService The parameterService to set.
178     */
179    @Override
180    public void setParameterService(ParameterService parameterService) {
181        this.parameterService = parameterService;
182    }
183
184
185}