1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.ole.vnd.document.validation.impl;
17  
18  import java.lang.reflect.Field;
19  import java.util.ArrayList;
20  import java.util.Date;
21  import java.util.HashMap;
22  import java.util.HashSet;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import org.apache.commons.lang.StringUtils;
29  import org.kuali.ole.coa.businessobject.Chart;
30  import org.kuali.ole.coa.businessobject.Organization;
31  import org.kuali.ole.module.purap.businessobject.Carrier;
32  import org.kuali.ole.module.purap.document.PaymentRequestDocument;
33  import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
34  import org.kuali.ole.select.service.OleForiegnVendorPhoneNumberService;
35  import org.kuali.ole.sys.OLEConstants;
36  import org.kuali.ole.sys.OLEKeyConstants;
37  import org.kuali.ole.sys.OLEPropertyConstants;
38  import org.kuali.ole.sys.context.SpringContext;
39  import org.kuali.ole.sys.service.PostalCodeValidationService;
40  import org.kuali.ole.vnd.VendorConstants;
41  import org.kuali.ole.vnd.VendorKeyConstants;
42  import org.kuali.ole.vnd.VendorParameterConstants;
43  import org.kuali.ole.vnd.VendorPropertyConstants;
44  import org.kuali.ole.vnd.businessobject.AddressType;
45  import org.kuali.ole.vnd.businessobject.OwnershipType;
46  import org.kuali.ole.vnd.businessobject.VendorAddress;
47  import org.kuali.ole.vnd.businessobject.VendorAlias;
48  import org.kuali.ole.vnd.businessobject.VendorCommodityCode;
49  import org.kuali.ole.vnd.businessobject.VendorContact;
50  import org.kuali.ole.vnd.businessobject.VendorContract;
51  import org.kuali.ole.vnd.businessobject.VendorContractOrganization;
52  import org.kuali.ole.vnd.businessobject.VendorCustomerNumber;
53  import org.kuali.ole.vnd.businessobject.VendorDefaultAddress;
54  import org.kuali.ole.vnd.businessobject.VendorDetail;
55  import org.kuali.ole.vnd.businessobject.VendorHeader;
56  import org.kuali.ole.vnd.businessobject.VendorPhoneNumber;
57  import org.kuali.ole.vnd.businessobject.VendorTransmissionFormatDetail;
58  import org.kuali.ole.vnd.businessobject.VendorType;
59  import org.kuali.ole.vnd.document.service.VendorService;
60  import org.kuali.ole.vnd.service.PhoneNumberService;
61  import org.kuali.ole.vnd.service.TaxNumberService;
62  import org.kuali.rice.core.api.datetime.DateTimeService;
63  import org.kuali.rice.core.api.parameter.ParameterEvaluatorService;
64  import org.kuali.rice.core.api.util.type.KualiDecimal;
65  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
66  import org.kuali.rice.kns.document.MaintenanceDocument;
67  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
68  import org.kuali.rice.kns.service.DataDictionaryService;
69  import org.kuali.rice.krad.bo.PersistableBusinessObject;
70  import org.kuali.rice.krad.service.BusinessObjectService;
71  import org.kuali.rice.krad.service.PersistenceService;
72  import org.kuali.rice.krad.util.ErrorMessage;
73  import org.kuali.rice.krad.util.GlobalVariables;
74  import org.kuali.rice.krad.util.ObjectUtils;
75  import org.springframework.util.AutoPopulatingList;
76  
77  
78  
79  
80  public class VendorRule extends MaintenanceDocumentRuleBase {
81  
82      private VendorDetail oldVendor;
83      private VendorDetail newVendor;
84  
85  
86      
87  
88  
89  
90  
91  
92  
93  
94  
95      @Override
96      public void setupBaseConvenienceObjects(MaintenanceDocument document) {
97          oldVendor = (VendorDetail) document.getOldMaintainableObject().getBusinessObject();
98          newVendor = (VendorDetail) document.getNewMaintainableObject().getBusinessObject();
99          super.setNewBo(newVendor);
100         setupConvenienceObjects();
101     }
102 
103     
104 
105 
106     @Override
107     public void setupConvenienceObjects() {
108         
109         refreshSubObjects(oldVendor);
110 
111         
112         refreshSubObjects(newVendor);
113     }
114 
115 
116     
117 
118 
119 
120 
121     void refreshSubObjects(VendorDetail vendor) {
122         if (vendor == null) {
123             return;
124         }
125 
126         
127         
128         
129         if (!vendor.isVendorParentIndicator()) {
130             vendor.refreshNonUpdateableReferences();
131             vendor.getVendorHeader().refreshNonUpdateableReferences();
132 
133         }
134         else {
135             
136             List<String> headerFieldNames = getObjectReferencesListFromBOClass(VendorHeader.class);
137             vendor.getVendorHeader().refreshNonUpdateableReferences();
138             SpringContext.getBean(PersistenceService.class).retrieveReferenceObjects(vendor.getVendorHeader(), headerFieldNames);
139 
140             
141             
142             
143             
144             List<String> detailFieldNames = getObjectReferencesListFromBOClass(vendor.getClass());
145             detailFieldNames.remove(VendorConstants.VENDOR_HEADER_ATTR);
146             SpringContext.getBean(PersistenceService.class).retrieveReferenceObjects(vendor, detailFieldNames);
147         }
148 
149         
150         if (vendor.getVendorAddresses() != null) {
151             for (VendorAddress address : vendor.getVendorAddresses()) {
152                 address.refreshNonUpdateableReferences();
153                 if (address.getVendorDefaultAddresses() != null) {
154                     for (VendorDefaultAddress defaultAddress : address.getVendorDefaultAddresses()) {
155                         defaultAddress.refreshNonUpdateableReferences();
156                     }
157                 }
158             }
159         }
160         
161         if (vendor.getVendorContacts() != null) {
162             for (VendorContact contact : vendor.getVendorContacts()) {
163                 contact.refreshNonUpdateableReferences();
164             }
165         }
166         
167         if (vendor.getVendorContracts() != null) {
168             for (VendorContract contract : vendor.getVendorContracts()) {
169                 contract.refreshNonUpdateableReferences();
170             }
171         }
172     }
173 
174     
175 
176 
177 
178 
179 
180 
181 
182     private List getObjectReferencesListFromBOClass(Class theClass) {
183         List<String> results = new ArrayList();
184         for (Field theField : theClass.getDeclaredFields()) {
185             
186             if ( PersistableBusinessObject.class.isAssignableFrom( theField.getType() ) ) {
187                     results.add(theField.getName());
188                 }
189             }
190         return results;
191     }
192 
193     
194 
195 
196     @Override
197     protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) {
198         boolean valid = processValidation(document);
199         return valid & super.processCustomApproveDocumentBusinessRules(document);
200     }
201 
202     
203 
204 
205     @Override
206     protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
207         boolean valid = processValidation(document);
208         return valid & super.processCustomRouteDocumentBusinessRules(document);
209     }
210 
211     
212 
213 
214     @Override
215     protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
216         boolean valid = true;
217         return valid & super.processCustomSaveDocumentBusinessRules(document);
218     }
219 
220     
221 
222 
223 
224 
225 
226     private boolean processValidation(MaintenanceDocument document) {
227         boolean valid = true;
228 
229         valid &= processVendorValidation(document);
230         valid &= processContactValidation(document);
231         if (ObjectUtils.isNotNull(newVendor.getVendorHeader().getVendorType())) {
232             valid &= processAddressValidation(document);
233             valid &= processContractValidation(document);
234             valid &= processCommodityCodeValidation(document);
235         }
236 
237         return valid;
238     }
239 
240     
241 
242 
243 
244 
245 
246     boolean processVendorValidation(MaintenanceDocument document) {
247         boolean valid = true;
248         VendorDetail vendorDetail = (VendorDetail) document.getNewMaintainableObject().getBusinessObject();
249         valid &= validateTaxTypeAndTaxNumberBlankness(vendorDetail);
250         valid &= validateParentVendorTaxNumber(vendorDetail);
251         valid &= validateOwnershipTypeAllowed(vendorDetail);
252         if(!vendorDetail.isNonBillable()){
253             valid &= validateTaxNumberFromTaxNumberService(vendorDetail);
254         }
255         valid &= validateRestrictedReasonRequiredness(vendorDetail);
256         valid &= validateInactiveReasonRequiredness(vendorDetail);
257         valid &= validatePreInactiveIndicator(vendorDetail);
258 
259         
260         
261 
262 
263 
264         valid &= validateVendorNames(vendorDetail);
265         valid &= validateVendorSoldToNumber(vendorDetail);
266         valid &= validateMinimumOrderAmount(vendorDetail);
267         valid &= validateOwnershipCategory(vendorDetail);
268         valid &= validateVendorWithholdingTaxDates(vendorDetail);
269         valid &= validateVendorW8BenOrW9ReceivedIndicator(vendorDetail);
270         valid &= validateSearchAliases(vendorDetail);
271         valid &= validateContracts(vendorDetail);
272         return valid;
273     }
274 
275     private boolean validateContracts(VendorDetail vendorDetail) {
276         boolean success = true;
277         int vendorPos = 0;
278         List<VendorContract> vendorContracts = vendorDetail.getVendorContracts();
279         for (VendorContract vendorContract : vendorContracts) {
280             List<VendorContractOrganization> organizations = vendorContract.getVendorContractOrganizations();
281             List<VendorContractOrganization> organizationCopy = new ArrayList<VendorContractOrganization>(organizations);
282             for (VendorContractOrganization organization :organizations ) {
283                  String chartCode = organization.getChartOfAccountsCode();
284                  String organizationCode = organization.getOrganizationCode();
285                  if (StringUtils.isNotEmpty(chartCode) && StringUtils.isNotEmpty(organizationCode)) {
286                      int counter = 0;
287                      int organizationPos = 0;
288                      for (VendorContractOrganization org : organizationCopy) {
289                          if (chartCode.equalsIgnoreCase(org.getChartOfAccountsCode()) && organizationCode.equalsIgnoreCase(org.getOrganizationCode())) {
290                              if (counter++ != 0) {
291                                  organizationCopy.remove(organization);
292                                  putFieldError(VendorPropertyConstants.VENDOR_CONTRACT + "[" + vendorPos + "]." +
293                                          VendorPropertyConstants.VENDOR_CONTRACT_ORGANIZATION + "[" + organizationPos + "]." +
294                                          VendorPropertyConstants.VENDOR_CUSTOMER_NUMBER_CHART_OF_ACCOUNTS_CODE,
295                                          VendorKeyConstants.ERROR_DUPLICATE_ENTRY_NOT_ALLOWED,chartCode + " " + organizationCode );
296                                  success = false;
297                                  break;
298                              }
299                          }
300                      }
301                      organizationPos++;
302                  }
303                  vendorPos++;
304             }
305         }
306         return success;
307     }
308 
309 
310     private boolean validateSearchAliases(VendorDetail vendorDetail) {
311         boolean success = true;
312         List<VendorAlias> searchAliases = vendorDetail.getVendorAliases();
313         List<VendorAlias> aliasList = new ArrayList<VendorAlias>(searchAliases);
314         int pos = 0;
315         for (VendorAlias searchAlias : searchAliases) {
316                 String aliasName = searchAlias.getVendorAliasName();
317                if (aliasName != null) {
318                    int counter = 0;
319                    for (VendorAlias alias : aliasList) {
320                        if (aliasName.equals(alias.getVendorAliasName())) {
321                            if (counter++ != 0) {
322                                putFieldError(VendorPropertyConstants.VENDOR_SEARCH_ALIASES + "[" + pos + "]." + VendorPropertyConstants.VENDOR_ALIAS_NAME, VendorKeyConstants.ERROR_DUPLICATE_ENTRY_NOT_ALLOWED,aliasName);
323                                aliasList.remove(searchAlias);
324                                success = false;
325                                break;
326                            }
327                        }
328                    }
329                 }
330                pos++;
331         }
332         return success;
333     }
334 
335     
336 
337 
338 
339 
340     boolean validatePreInactiveIndicator(VendorDetail vendorDetail) {
341         boolean activeIndicator = vendorDetail.isActiveIndicator();
342         if(!activeIndicator){
343             Map vendorHeaderIdMap = new HashMap();
344             vendorHeaderIdMap.put("VNDR_HDR_GNRTD_ID", vendorDetail.getVendorHeaderGeneratedIdentifier());
345             BusinessObjectService
346                 businessObjectService = SpringContext.getBean(BusinessObjectService.class);
347             List<PurchaseOrderDocument> vendorPurchaseOrderDocumentList = (List) businessObjectService.findMatching(PurchaseOrderDocument.class, vendorHeaderIdMap);
348             boolean statusFlag = false;
349             for(int i=0;i<vendorPurchaseOrderDocumentList.size();i++){
350                 PurchaseOrderDocument purchaseOrderDocument =vendorPurchaseOrderDocumentList.get(i);
351                 String status = purchaseOrderDocument.getApplicationDocumentStatus();
352                
353                     statusFlag = true;
354                
355             }
356 
357             if(!statusFlag){
358                 if(vendorPurchaseOrderDocumentList.size() > 0){
359 
360                     putFieldError(VendorPropertyConstants.VENDOR_INACTIVE_REFERENCE, VendorKeyConstants.ERROR_INACTIVE_REFERENCE);
361                     return false;
362                 }
363                 List<PurchaseOrderDocument> vendorPaymentRequestDocumentList = (List) businessObjectService.findMatching(PaymentRequestDocument.class, vendorHeaderIdMap);
364                 if(vendorPaymentRequestDocumentList.size() > 0){
365                     putFieldError(VendorPropertyConstants.VENDOR_INACTIVE_REFERENCE, VendorKeyConstants.ERROR_INACTIVE_REFERENCE);
366                     return false;
367                 }
368             }
369         }
370        return true;
371     }
372 
373     
374 
375 
376 
377 
378 
379     boolean validateInactiveReasonRequiredness(VendorDetail vendorDetail) {
380         boolean activeIndicator = vendorDetail.isActiveIndicator();
381         boolean emptyInactiveReason = StringUtils.isEmpty(vendorDetail.getVendorInactiveReasonCode());
382 
383         
384         if (!activeIndicator && emptyInactiveReason) {
385             putFieldError(VendorPropertyConstants.VENDOR_INACTIVE_REASON, VendorKeyConstants.ERROR_INACTIVE_REASON_REQUIRED);
386             return false;
387         }
388         
389         if (activeIndicator && !emptyInactiveReason) {
390             putFieldError(VendorPropertyConstants.VENDOR_INACTIVE_REASON, VendorKeyConstants.ERROR_INACTIVE_REASON_NOT_ALLOWED);
391             return false;
392         }
393         return true;
394     }
395 
396     
397 
398 
399 
400 
401 
402 
403 
404     boolean validateTaxNumberRequiredness(VendorDetail vendorDetail) {
405         if (!vendorDetail.getVendorHeader().getVendorForeignIndicator() && vendorDetail.getVendorHeader().getVendorType().isVendorTaxNumberRequiredIndicator() && StringUtils.isBlank(vendorDetail.getVendorHeader().getVendorTaxNumber())) {
406             if (vendorDetail.isVendorParentIndicator()) {
407                 putFieldError(VendorPropertyConstants.VENDOR_TAX_NUMBER, VendorKeyConstants.ERROR_VENDOR_TYPE_REQUIRES_TAX_NUMBER, vendorDetail.getVendorHeader().getVendorType().getVendorTypeDescription());
408             }
409             else {
410                 putFieldError(VendorPropertyConstants.VENDOR_TAX_NUMBER, VendorKeyConstants.ERROR_VENDOR_PARENT_NEEDS_CHANGED);
411             }
412             return false;
413         }
414         return true;
415     }
416 
417     
418 
419 
420 
421 
422 
423     boolean validateRestrictedReasonRequiredness(VendorDetail vendorDetail) {
424         if (ObjectUtils.isNotNull(vendorDetail.getVendorRestrictedIndicator()) && vendorDetail.getVendorRestrictedIndicator() && StringUtils.isEmpty(vendorDetail.getVendorRestrictedReasonText())) {
425             putFieldError(VendorPropertyConstants.VENDOR_RESTRICTED_REASON_TEXT, VendorKeyConstants.ERROR_RESTRICTED_REASON_REQUIRED);
426             return false;
427         }
428         return true;
429     }
430 
431     
432 
433 
434 
435 
436 
437 
438 
439 
440 
441 
442 
443 
444    boolean validateParentVendorTaxNumber(VendorDetail vendorDetail) {
445         boolean valid = true;
446         boolean isParent = vendorDetail.isVendorParentIndicator();
447         Map criteria = new HashMap();
448         criteria.put(VendorPropertyConstants.VENDOR_TAX_TYPE_CODE, vendorDetail.getVendorHeader().getVendorTaxTypeCode());
449         criteria.put(VendorPropertyConstants.VENDOR_TAX_NUMBER, vendorDetail.getVendorHeader().getVendorTaxNumber());
450         criteria.put(OLEPropertyConstants.ACTIVE_INDICATOR, true);
451         Map negativeCriteria = new HashMap();
452         int existingVendor = 0;
453         
454         
455         
456         if (ObjectUtils.isNotNull(vendorDetail.getVendorHeaderGeneratedIdentifier())) {
457             negativeCriteria.put(VendorPropertyConstants.VENDOR_HEADER_GENERATED_ID, vendorDetail.getVendorHeaderGeneratedIdentifier());
458             existingVendor = getBoService().countMatching(VendorDetail.class, criteria, negativeCriteria);
459         }
460         else {
461             
462             
463             
464             existingVendor = getBoService().countMatching(VendorDetail.class, criteria);
465         }
466         if (existingVendor > 0) {
467             if (isParent) {
468                 putFieldError(VendorPropertyConstants.VENDOR_TAX_NUMBER, VendorKeyConstants.ERROR_VENDOR_TAX_TYPE_AND_NUMBER_COMBO_EXISTS);
469             }
470             else {
471                 putFieldError(VendorPropertyConstants.VENDOR_TAX_NUMBER, VendorKeyConstants.ERROR_VENDOR_PARENT_NEEDS_CHANGED);
472             }
473             valid &= false;
474         }
475         return valid;
476     }
477 
478     
479 
480 
481 
482 
483 
484 
485 
486 
487 
488     boolean validateTaxTypeAndTaxNumberBlankness(VendorDetail vendorDetail) {
489         boolean valid = true;
490         boolean isParent = vendorDetail.isVendorParentIndicator();
491         if (!StringUtils.isBlank(vendorDetail.getVendorHeader().getVendorTaxNumber()) && (StringUtils.isBlank(vendorDetail.getVendorHeader().getVendorTaxTypeCode()))) {
492             if (isParent) {
493                 putFieldError(VendorPropertyConstants.VENDOR_TAX_TYPE_CODE, VendorKeyConstants.ERROR_VENDOR_TAX_TYPE_CANNOT_BE_BLANK);
494             }
495             valid &= false;
496         }
497         else if (StringUtils.isBlank(vendorDetail.getVendorHeader().getVendorTaxNumber()) && !StringUtils.isBlank(vendorDetail.getVendorHeader().getVendorTaxTypeCode())) {
498             if (isParent) {
499                 putFieldError(VendorPropertyConstants.VENDOR_TAX_TYPE_CODE, VendorKeyConstants.ERROR_VENDOR_TAX_TYPE_CANNOT_BE_SET);
500             }
501             valid &= false;
502         }
503 
504         if (!valid && !isParent) {
505             putFieldError(VendorPropertyConstants.VENDOR_TAX_TYPE_CODE, VendorKeyConstants.ERROR_VENDOR_PARENT_NEEDS_CHANGED);
506         }
507 
508         return valid;
509     }
510 
511 
512     
513 
514 
515 
516 
517 
518 
519 
520 
521     protected boolean validateVendorNames(VendorDetail vendorDetail) {
522         boolean valid = true;
523         if (StringUtils.isBlank(vendorDetail.getVendorName())) {
524             
525             if (StringUtils.isBlank(vendorDetail.getVendorFirstName()) && StringUtils.isBlank(vendorDetail.getVendorLastName())) {
526 
527                 putFieldError(VendorPropertyConstants.VENDOR_NAME, VendorKeyConstants.ERROR_VENDOR_NAME_REQUIRED);
528                 valid &= false;
529             }
530             
531             else if (StringUtils.isBlank(vendorDetail.getVendorFirstName()) || StringUtils.isBlank(vendorDetail.getVendorLastName())) {
532 
533                 putFieldError(VendorPropertyConstants.VENDOR_NAME, VendorKeyConstants.ERROR_VENDOR_BOTH_NAME_REQUIRED);
534                 valid &= false;
535             }
536             else {
537                 String vendorName = vendorDetail.getVendorLastName() + VendorConstants.NAME_DELIM + vendorDetail.getVendorFirstName();
538                 if (vendorName.length() > VendorConstants.MAX_VENDOR_NAME_LENGTH) {
539                     putFieldError(VendorPropertyConstants.VENDOR_LAST_NAME, VendorKeyConstants.ERROR_VENDOR_NAME_TOO_LONG);
540                     valid &= false;
541                 }
542 
543             }
544         }
545         else {
546             
547             if (!StringUtils.isBlank(vendorDetail.getVendorFirstName()) || !StringUtils.isBlank(vendorDetail.getVendorLastName())) {
548 
549                 putFieldError(VendorPropertyConstants.VENDOR_NAME, VendorKeyConstants.ERROR_VENDOR_NAME_INVALID);
550                 valid &= false;
551             }
552         }
553         return valid;
554     }
555 
556     
557 
558 
559 
560 
561 
562 
563     protected boolean validateVendorSoldToNumber(VendorDetail vendorDetail) {
564         boolean valid = true;
565         String vendorSoldToNumber = vendorDetail.getVendorSoldToNumber();
566 
567         
568         if (StringUtils.isEmpty(vendorSoldToNumber)) {
569             vendorDetail.setSoldToVendorDetail(null);
570             vendorDetail.setVendorSoldToGeneratedIdentifier(null);
571             vendorDetail.setVendorSoldToAssignedIdentifier(null);
572             vendorDetail.setVendorSoldToNumber(null);
573             vendorDetail.setVendorSoldToName(null);
574             return valid;
575         }
576 
577         VendorDetail vendorSoldTo = SpringContext.getBean(VendorService.class).getVendorDetail(vendorSoldToNumber);
578         if (vendorSoldTo != null) {
579             
580             vendorDetail.setSoldToVendorDetail(vendorSoldTo);
581             vendorDetail.setVendorSoldToGeneratedIdentifier(vendorSoldTo.getVendorHeaderGeneratedIdentifier());
582             vendorDetail.setVendorSoldToAssignedIdentifier(vendorSoldTo.getVendorDetailAssignedIdentifier());
583             vendorDetail.setVendorSoldToName(vendorSoldTo.getVendorName());
584         }
585         else {
586             
587             vendorDetail.setSoldToVendorDetail(null);
588             vendorDetail.setVendorSoldToName(null);
589             valid = false;
590             putFieldError(VendorPropertyConstants.VENDOR_SOLD_TO_NUMBER, VendorKeyConstants.VENDOR_SOLD_TO_NUMBER_INVALID);
591         }
592 
593         return valid;
594     }
595 
596     
597 
598 
599 
600 
601 
602 
603 
604 
605 
606     private boolean validateOwnershipTypeAllowed(VendorDetail vendorDetail) {
607         boolean valid = true;
608         boolean isParent = vendorDetail.isVendorParentIndicator();
609         String ownershipTypeCode = vendorDetail.getVendorHeader().getVendorOwnershipCode();
610         String taxTypeCode = vendorDetail.getVendorHeader().getVendorTaxTypeCode();
611         if (StringUtils.isNotEmpty(ownershipTypeCode) && StringUtils.isNotEmpty(taxTypeCode)) {
612             if (VendorConstants.TAX_TYPE_FEIN.equals(taxTypeCode)) {
613                 if (!SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(VendorDetail.class, VendorParameterConstants.FEIN_ALLOWED_OWNERSHIP_TYPES, ownershipTypeCode).evaluationSucceeds()) {
614                     valid &= false;
615                 }
616             }
617             else if (VendorConstants.TAX_TYPE_SSN.equals(taxTypeCode)) {
618                 if (!SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(VendorDetail.class, VendorParameterConstants.SSN_ALLOWED_OWNERSHIP_TYPES, ownershipTypeCode).evaluationSucceeds()) {
619                     valid &= false;
620                 }
621             }
622         }
623         if (!valid && isParent) {
624             putFieldError(VendorPropertyConstants.VENDOR_OWNERSHIP_CODE, VendorKeyConstants.ERROR_OWNERSHIP_TYPE_CODE_NOT_ALLOWED, new String[] { vendorDetail.getVendorHeader().getVendorOwnership().getVendorOwnershipDescription(), taxTypeCode });
625         }
626         else if (!valid && !isParent) {
627             putFieldError(VendorPropertyConstants.VENDOR_OWNERSHIP_CODE, VendorKeyConstants.ERROR_VENDOR_PARENT_NEEDS_CHANGED);
628         }
629         return valid;
630     }
631 
632 
633     
634 
635 
636 
637 
638 
639     private boolean validateMinimumOrderAmount(VendorDetail vendorDetail) {
640         boolean valid = true;
641         KualiDecimal minimumOrderAmount = vendorDetail.getVendorMinimumOrderAmount();
642         if (ObjectUtils.isNotNull(minimumOrderAmount)) {
643             KualiDecimal VENDOR_MIN_ORDER_AMOUNT = new KualiDecimal(SpringContext.getBean(ParameterService.class).getParameterValueAsString(VendorDetail.class, VendorParameterConstants.VENDOR_MIN_ORDER_AMOUNT));
644             if (ObjectUtils.isNotNull(VENDOR_MIN_ORDER_AMOUNT) && (VENDOR_MIN_ORDER_AMOUNT.compareTo(minimumOrderAmount) < 1) || (minimumOrderAmount.isNegative())) {
645                 putFieldError(VendorPropertyConstants.VENDOR_MIN_ORDER_AMOUNT, VendorKeyConstants.ERROR_VENDOR_MAX_MIN_ORDER_AMOUNT, VENDOR_MIN_ORDER_AMOUNT.toString());
646                 valid &= false;
647             }
648         }
649         return valid;
650     }
651 
652     
653 
654 
655 
656 
657 
658 
659 
660 
661     private boolean validateOwnershipCategory(VendorDetail vendorDetail) {
662         boolean valid = true;
663         boolean isParent = vendorDetail.isVendorParentIndicator();
664         OwnershipType ot = vendorDetail.getVendorHeader().getVendorOwnership();
665         if (ot != null && !ot.getVendorOwnershipCategoryAllowedIndicator()) {
666             if (ObjectUtils.isNotNull(vendorDetail.getVendorHeader().getVendorOwnershipCategory())) {
667                 valid &= false;
668             }
669         }
670         if (!valid && isParent) {
671             putFieldError(VendorPropertyConstants.VENDOR_OWNERSHIP_CATEGORY_CODE, VendorKeyConstants.ERROR_OWNERSHIP_CATEGORY_CODE_NOT_ALLOWED, new String[] { vendorDetail.getVendorHeader().getVendorOwnershipCategory().getVendorOwnershipCategoryDescription(), vendorDetail.getVendorHeader().getVendorOwnership().getVendorOwnershipDescription() });
672         }
673         else if (!valid && !isParent) {
674             putFieldError(VendorPropertyConstants.VENDOR_OWNERSHIP_CODE, VendorKeyConstants.ERROR_VENDOR_PARENT_NEEDS_CHANGED);
675         }
676         return valid;
677     }
678 
679     
680 
681 
682 
683 
684 
685 
686 
687 
688     private boolean validateTaxNumberFromTaxNumberService(VendorDetail vendorDetail) {
689         boolean valid = true;
690         boolean isParent = vendorDetail.isVendorParentIndicator();
691         String taxNumber = vendorDetail.getVendorHeader().getVendorTaxNumber();
692         String taxType = vendorDetail.getVendorHeader().getVendorTaxTypeCode();
693         if (!StringUtils.isEmpty(taxType) && !StringUtils.isEmpty(taxNumber)) {
694             valid = SpringContext.getBean(TaxNumberService.class).isValidTaxNumber(taxNumber, taxType);
695             if (!valid && isParent) {
696                 putFieldError(VendorPropertyConstants.VENDOR_TAX_NUMBER, VendorKeyConstants.ERROR_TAX_NUMBER_INVALID);
697             }
698             valid = SpringContext.getBean(TaxNumberService.class).isAllowedTaxNumber(taxNumber);
699             if (!valid && isParent) {
700                 putFieldError(VendorPropertyConstants.VENDOR_TAX_NUMBER, VendorKeyConstants.ERROR_TAX_NUMBER_NOT_ALLOWED);
701             }
702         }
703         if (!valid && !isParent) {
704             putFieldError(VendorPropertyConstants.VENDOR_TAX_NUMBER, VendorKeyConstants.ERROR_VENDOR_PARENT_NEEDS_CHANGED);
705         }
706 
707         return valid;
708     }
709 
710     
711 
712 
713 
714 
715 
716     boolean processCommodityCodeValidation(MaintenanceDocument document) {
717         boolean valid = true;
718         List<VendorCommodityCode> vendorCommodities = newVendor.getVendorCommodities();
719         boolean commodityCodeRequired = newVendor.getVendorHeader().getVendorType().isCommodityRequiredIndicator();
720         if (commodityCodeRequired) {
721             if (vendorCommodities.size() == 0) {
722                 
723                 String propertyName = "add." + VendorPropertyConstants.VENDOR_COMMODITIES_CODE_PURCHASING_COMMODITY_CODE;
724                 putFieldError(propertyName, VendorKeyConstants.ERROR_VENDOR_COMMODITY_CODE_IS_REQUIRED_FOR_THIS_VENDOR_TYPE);
725                 valid = false;
726             }
727             
728             
729             else if (vendorCommodities.size() > 0) {
730                 valid &= validateCommodityCodeDefaultIndicator(vendorCommodities);
731             }
732         }
733         else if (vendorCommodities.size() > 0) {
734             
735             
736             int defaultCount = 0;
737             for (int i=0; i < vendorCommodities.size(); i++) {
738                 VendorCommodityCode vcc = vendorCommodities.get(i);
739                 if (vcc.isCommodityDefaultIndicator()) {
740                     defaultCount ++;
741                     if (defaultCount > 1) {
742                         valid = false;
743                         String propertyName = VendorPropertyConstants.VENDOR_COMMODITIES_CODE + "[" + i + "]." + VendorPropertyConstants.VENDOR_COMMODITIES_DEFAULT_INDICATOR;
744                         putFieldError(propertyName, VendorKeyConstants.ERROR_VENDOR_COMMODITY_CODE_REQUIRE_ONE_DEFAULT_IND);
745                         break;
746                     }
747                 }
748             }
749         }
750 
751         return valid;
752     }
753 
754     
755 
756 
757 
758 
759 
760 
761     private boolean validateCommodityCodeDefaultIndicator(List<VendorCommodityCode> vendorCommodities) {
762         boolean valid = true;
763 
764         boolean foundDefaultIndicator = false;
765         for (int i=0; i < vendorCommodities.size(); i++) {
766             VendorCommodityCode vcc = vendorCommodities.get(i);
767             if (vcc.isCommodityDefaultIndicator()) {
768                 if (!foundDefaultIndicator) {
769                     foundDefaultIndicator = true;
770                 }
771                 else {
772                     
773                     String propertyName = VendorPropertyConstants.VENDOR_COMMODITIES_CODE + "[" + i + "]." + VendorPropertyConstants.VENDOR_COMMODITIES_DEFAULT_INDICATOR;
774                     putFieldError(propertyName, VendorKeyConstants.ERROR_VENDOR_COMMODITY_CODE_REQUIRE_ONE_DEFAULT_IND);
775                     valid = false;
776                 }
777             }
778         }
779         if (!foundDefaultIndicator && vendorCommodities.size() > 0) {
780             
781             String propertyName = VendorPropertyConstants.VENDOR_COMMODITIES_CODE + "[0]." + VendorPropertyConstants.VENDOR_COMMODITIES_DEFAULT_INDICATOR;
782             putFieldError(propertyName, VendorKeyConstants.ERROR_VENDOR_COMMODITY_CODE_REQUIRE_ONE_DEFAULT_IND);
783             valid = false;
784         }
785         return valid;
786     }
787 
788     
789 
790 
791 
792 
793 
794     boolean processAddressValidation(MaintenanceDocument document) {
795         boolean valid = true;
796         boolean validAddressType = false;
797 
798         List<VendorAddress> addresses = newVendor.getVendorAddresses();
799         String vendorTypeCode = newVendor.getVendorHeader().getVendorTypeCode();
800         String vendorAddressTypeRequiredCode = newVendor.getVendorHeader().getVendorType().getVendorAddressTypeRequiredCode();
801         if(addresses.size()==0){
802             putFieldError(VendorPropertyConstants.VENDOR_ADDRESS, VendorKeyConstants.OLE_VENDOR_ADDRESS);
803             
804             valid = false;
805             validAddressType = false;
806         }
807         if(valid){
808 
809         for (int i = 0; i < addresses.size(); i++) {
810             VendorAddress address = addresses.get(i);
811             String errorPath = MAINTAINABLE_ERROR_PREFIX + VendorPropertyConstants.VENDOR_ADDRESS + "[" + i + "]";
812             GlobalVariables.getMessageMap().clearErrorPath();
813             GlobalVariables.getMessageMap().addToErrorPath(errorPath);
814 
815             this.getDictionaryValidationService().validateBusinessObject(address);
816             if (GlobalVariables.getMessageMap().hasErrors()) {
817                 valid = false;
818             }
819 
820             if (address.getVendorAddressTypeCode().equals(vendorAddressTypeRequiredCode)) {
821                 validAddressType = true;
822             }
823 
824 
825             valid &= checkFaxNumber(address);
826 
827             valid &= checkAddressCountryEmptyStateZip(address);
828 
829             GlobalVariables.getMessageMap().clearErrorPath();
830         }
831 
832         List<VendorPhoneNumber> phoneNumbers = newVendor.getVendorPhoneNumbers();
833         for(int j=0;j<phoneNumbers.size();j++){
834             VendorPhoneNumber phoneNumber = phoneNumbers.get(j);
835             String errorPath = MAINTAINABLE_ERROR_PREFIX + VendorPropertyConstants.VENDOR_PHONE_NUMBERS + "[" + j + "]";
836             GlobalVariables.getMessageMap().clearErrorPath();
837             GlobalVariables.getMessageMap().addToErrorPath(errorPath);
838             this.getDictionaryValidationService().validateBusinessObject(phoneNumber);
839             valid &= checkPhoneNumber(phoneNumber);
840         }
841 
842         
843         String vendorAddressTabPrefix = OLEConstants.ADD_PREFIX + "." + VendorPropertyConstants.VENDOR_ADDRESS + ".";
844         if (!StringUtils.isBlank(vendorTypeCode) && !StringUtils.isBlank(vendorAddressTypeRequiredCode) && !validAddressType) {
845             String[] parameters = new String[] { vendorTypeCode, vendorAddressTypeRequiredCode };
846             putFieldError(vendorAddressTabPrefix + VendorPropertyConstants.VENDOR_ADDRESS_TYPE_CODE, VendorKeyConstants.ERROR_ADDRESS_TYPE, parameters);
847             String addressLine1Label = SpringContext.getBean(DataDictionaryService.class).getAttributeLabel(VendorAddress.class, VendorPropertyConstants.VENDOR_ADDRESS_LINE_1);
848             String addressCityLabel = SpringContext.getBean(DataDictionaryService.class).getAttributeLabel(VendorAddress.class, VendorPropertyConstants.VENDOR_ADDRESS_CITY);
849             String addressCountryLabel = SpringContext.getBean(DataDictionaryService.class).getAttributeLabel(VendorAddress.class, VendorPropertyConstants.VENDOR_ADDRESS_COUNTRY);
850             putFieldError(vendorAddressTabPrefix + VendorPropertyConstants.VENDOR_ADDRESS_LINE_1, OLEKeyConstants.ERROR_REQUIRED, addressLine1Label);
851             putFieldError(vendorAddressTabPrefix + VendorPropertyConstants.VENDOR_ADDRESS_CITY, OLEKeyConstants.ERROR_REQUIRED, addressCityLabel);
852             putFieldError(vendorAddressTabPrefix + VendorPropertyConstants.VENDOR_ADDRESS_COUNTRY, OLEKeyConstants.ERROR_REQUIRED, addressCountryLabel);
853             valid = false;
854         }
855 
856         valid &= validateDefaultAddressCampus(newVendor);
857 
858         
859         Map fieldValues = new HashMap();
860         fieldValues.put(VendorPropertyConstants.VENDOR_HEADER_GENERATED_ID, newVendor.getVendorHeaderGeneratedIdentifier());
861         
862         List<VendorAddress> vendorDivisionAddresses = new ArrayList(SpringContext.getBean(BusinessObjectService.class).findMatchingOrderBy(VendorAddress.class, fieldValues, VendorPropertyConstants.VENDOR_DETAIL_ASSIGNED_ID, true));
863 
864         
865         
866         HashSet<Integer> vendorDetailedIds = new HashSet();
867         
868         HashSet<Integer> vendorDivisionsIdsWithDesiredAddressType = new HashSet();
869 
870         for (VendorAddress vendorDivisionAddress : vendorDivisionAddresses) {
871             
872             if (vendorDivisionAddress.getVendorDetailAssignedIdentifier() != 0) {
873                 vendorDetailedIds.add(vendorDivisionAddress.getVendorDetailAssignedIdentifier());
874                 if (vendorDivisionAddress.getVendorAddressTypeCode().equalsIgnoreCase(vendorAddressTypeRequiredCode)) {
875                     vendorDivisionsIdsWithDesiredAddressType.add(vendorDivisionAddress.getVendorDetailAssignedIdentifier());
876                 }
877             }
878         }
879 
880         
881         if (vendorDivisionsIdsWithDesiredAddressType.size() < vendorDetailedIds.size()) {
882             Iterator itr = vendorDetailedIds.iterator();
883             Integer value;
884             String vendorId;
885 
886             while (itr.hasNext()) {
887                 value = (Integer) itr.next();
888                 if (!vendorDivisionsIdsWithDesiredAddressType.contains(value)) {
889                     vendorId = newVendor.getVendorHeaderGeneratedIdentifier().toString() + '-' + value.toString();
890                     String[] parameters = new String[] { vendorId, vendorTypeCode, vendorAddressTypeRequiredCode };
891 
892                     
893                     GlobalVariables.getMessageMap().putWarningWithoutFullErrorPath(MAINTAINABLE_ERROR_PREFIX + vendorAddressTabPrefix + VendorPropertyConstants.VENDOR_ADDRESS_TYPE_CODE, VendorKeyConstants.ERROR_ADDRESS_TYPE_DIVISIONS, parameters);
894                 }
895             }
896         }
897 
898         }
899         return valid;
900     }
901 
902         
903 
904 
905 
906 
907 
908 
909     boolean checkAddressCountryEmptyStateZip(VendorAddress address) {
910         
911         
912         boolean valid = SpringContext.getBean(PostalCodeValidationService.class).validateAddress(address.getVendorCountryCode(), address.getVendorStateCode(), address.getVendorZipCode(), VendorPropertyConstants.VENDOR_ADDRESS_STATE, VendorPropertyConstants.VENDOR_ADDRESS_ZIP);
913         
914 		return valid;
915     }
916 
917     
918 
919 
920 
921 
922 
923 
924     boolean findAllowDefaultAddressIndicatorHelper(VendorAddress vendorAddress) {
925 
926         AddressType addressType = new AddressType();
927 
928         addressType = vendorAddress.getVendorAddressType();
929         if (ObjectUtils.isNull(addressType)) {
930             return false;
931         }
932         
933         return addressType.getVendorDefaultIndicator();
934 
935     }
936 
937     
938 
939 
940 
941 
942 
943 
944 
945 
946 
947     boolean checkDefaultAddressCampus(VendorDetail vendorDetail, VendorDefaultAddress addedDefaultAddress, VendorAddress parent) {
948         VendorAddress vendorAddress = parent;
949         if (ObjectUtils.isNull(vendorAddress)) {
950             return false;
951         }
952 
953         int j = vendorDetail.getVendorAddresses().indexOf(vendorAddress);
954         String errorPath = MAINTAINABLE_ERROR_PREFIX + VendorPropertyConstants.VENDOR_ADDRESS + "[" + j + "]";
955         GlobalVariables.getMessageMap().addToErrorPath(errorPath);
956 
957         
958         boolean allowDefaultAddressIndicator = findAllowDefaultAddressIndicatorHelper(vendorAddress);
959         String addedAddressCampusCode = addedDefaultAddress.getVendorCampusCode();
960         String addedAddressTypeCode = vendorAddress.getVendorAddressTypeCode();
961 
962         
963         
964         if (allowDefaultAddressIndicator == false) {
965             String[] parameters = new String[] { addedAddressTypeCode };
966             GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_DEFAULT_ADDRESS + "[" + 0 + "]." + VendorPropertyConstants.VENDOR_DEFAULT_ADDRESS_CAMPUS, VendorKeyConstants.ERROR_ADDRESS_DEFAULT_CAMPUS_NOT_ALLOWED, parameters);
967             return false;
968         }
969 
970         List<VendorDefaultAddress> vendorDefaultAddresses = vendorAddress.getVendorDefaultAddresses();
971         for (int i = 0; i < vendorDefaultAddresses.size(); i++) {
972             VendorDefaultAddress vendorDefaultAddress = vendorDefaultAddresses.get(i);
973             if (vendorDefaultAddress.getVendorCampusCode().equalsIgnoreCase(addedAddressCampusCode)) {
974                 String[] parameters = new String[] { addedAddressCampusCode, addedAddressTypeCode };
975                 GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_DEFAULT_ADDRESS + "[" + i + "]." + VendorPropertyConstants.VENDOR_DEFAULT_ADDRESS_CAMPUS, VendorKeyConstants.ERROR_ADDRESS_DEFAULT_CAMPUS, parameters);
976                 return false;
977             }
978         }
979 
980         return true;
981     }
982 
983     
984 
985 
986 
987 
988 
989 
990 
991 
992     boolean validateDefaultAddressCampus(VendorDetail vendorDetail) {
993         List<VendorAddress> vendorAddresses = vendorDetail.getVendorAddresses();
994         String addressTypeCode;
995         String addressTypeDesc;
996         String campusCode;
997         boolean valid = true;
998         boolean previousValue = false;
999 
1000         
1001         HashMap addressTypeCodeDefaultIndicator = new HashMap();
1002 
1003         
1004         HashMap addressTypeDefaultCampus = new HashMap();
1005 
1006         
1007         HashSet addressTypesHavingDefaultTrue = new HashSet();
1008 
1009         int i = 0;
1010         for (VendorAddress address : vendorAddresses) {
1011             addressTypeCode = address.getVendorAddressTypeCode();
1012             addressTypeDesc = address.getVendorAddressType().getVendorAddressTypeDescription();
1013             String errorPath = MAINTAINABLE_ERROR_PREFIX + VendorPropertyConstants.VENDOR_ADDRESS + "[" + i + "]";
1014             GlobalVariables.getMessageMap().addToErrorPath(errorPath);
1015             String[] parameters = new String[] { addressTypeCode };
1016 
1017             
1018             
1019             
1020             
1021             
1022 
1023             if (findAllowDefaultAddressIndicatorHelper(address)) {
1024                 if (address.isVendorDefaultAddressIndicator()) {
1025                     addressTypesHavingDefaultTrue.add(addressTypeCode);
1026                 }
1027                 if (!addressTypeCodeDefaultIndicator.isEmpty() && addressTypeCodeDefaultIndicator.containsKey(addressTypeCode)) {
1028                     previousValue = ((Boolean) addressTypeCodeDefaultIndicator.get(addressTypeCode)).booleanValue();
1029                 }
1030 
1031                 if (addressTypeCodeDefaultIndicator.put(addressTypeCode, address.isVendorDefaultAddressIndicator()) != null && previousValue && address.isVendorDefaultAddressIndicator()) {
1032                     GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_DEFAULT_ADDRESS_INDICATOR, VendorKeyConstants.ERROR_ADDRESS_DEFAULT_INDICATOR,addressTypeDesc );
1033                     valid = false;
1034                 }
1035 
1036             }
1037             
1038             else {
1039                 if (address.isVendorDefaultAddressIndicator()) {
1040                     GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_DEFAULT_ADDRESS_INDICATOR, VendorKeyConstants.ERROR_ADDRESS_DEFAULT_ADDRESS_NOT_ALLOWED, parameters);
1041                     valid = false;
1042                 }
1043 
1044             }
1045 
1046             List<VendorDefaultAddress> vendorDefaultAddresses = address.getVendorDefaultAddresses();
1047 
1048             
1049             
1050             
1051             
1052             int j = 0;
1053             for (VendorDefaultAddress defaultAddress : vendorDefaultAddresses) {
1054                 campusCode = (String) addressTypeDefaultCampus.put(addressTypeCode, defaultAddress.getVendorCampusCode());
1055                 if (StringUtils.isNotBlank(campusCode) && campusCode.equalsIgnoreCase(defaultAddress.getVendorCampusCode())) {
1056                     String[] newParameters = new String[] { defaultAddress.getVendorCampusCode(), addressTypeCode };
1057                     GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_DEFAULT_ADDRESS + "[" + j + "]." + VendorPropertyConstants.VENDOR_DEFAULT_ADDRESS_CAMPUS, VendorKeyConstants.ERROR_ADDRESS_DEFAULT_CAMPUS, newParameters);
1058                     valid = false;
1059                 }
1060                 j++;
1061             }
1062             i++;
1063             GlobalVariables.getMessageMap().removeFromErrorPath(errorPath);
1064         }
1065 
1066         
1067         if (!addressTypeCodeDefaultIndicator.isEmpty()) {
1068             Set<String> addressTypes = addressTypeCodeDefaultIndicator.keySet();
1069 
1070             for (String addressType : addressTypes) {
1071                 if (!addressTypesHavingDefaultTrue.contains(addressType)) {
1072 
1073                     int addressIndex = 0;
1074                     for (VendorAddress address : vendorAddresses) {
1075                         String[] parameters = new String[] { address.getVendorAddressType().getVendorAddressTypeDescription() };
1076                         String propertyName = VendorPropertyConstants.VENDOR_ADDRESS + "[" + addressIndex + "]." + VendorPropertyConstants.VENDOR_DEFAULT_ADDRESS_INDICATOR;
1077                         if (address.getVendorAddressType().getVendorAddressTypeCode().equalsIgnoreCase(addressType)) {
1078                             putFieldError(propertyName, VendorKeyConstants.ERROR_ADDRESS_DEFAULT_INDICATOR, parameters);
1079                             break;
1080                         }
1081                         addressIndex++;
1082                     }
1083                     valid = false;
1084                 }
1085 
1086             }
1087         }
1088 
1089         return valid;
1090     }
1091 
1092 
1093     
1094 
1095 
1096 
1097 
1098 
1099     boolean checkFaxNumber(VendorAddress address) {
1100         boolean valid = true;
1101         String faxNumber = address.getVendorFaxNumber();
1102         boolean foriegnVendor=false;
1103         if (newVendor.getVendorHeader() != null) {
1104             if (newVendor.getVendorHeader().getVendorForeignIndicator() != null) {
1105                 foriegnVendor = newVendor.getVendorHeader().getVendorForeignIndicator();
1106             }
1107         }
1108 
1109         if(foriegnVendor) {
1110 
1111             if(StringUtils.isNotEmpty(faxNumber) && !SpringContext.getBean(OleForiegnVendorPhoneNumberService.class).isValidForiegnVendorPhoneNumber(faxNumber)){
1112                 GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_FAX_NUMBER, VendorKeyConstants.ERROR_FORIEGN_VENDOR_FAX_NUMBER);
1113                 valid &= false;
1114             }
1115         }
1116         else {
1117             if (StringUtils.isNotEmpty(faxNumber) && !SpringContext.getBean(PhoneNumberService.class).isValidPhoneNumber(faxNumber)) {
1118                 GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_FAX_NUMBER, VendorKeyConstants.ERROR_FAX_NUMBER);
1119                 valid &= false;
1120             }
1121         }
1122         return valid;
1123     }
1124 
1125     boolean checkPhoneNumber(VendorPhoneNumber vendorPhoneNumber){
1126         boolean valid = true;
1127         String phoneNumber = vendorPhoneNumber.getVendorPhoneNumber();
1128         boolean foriegnVendor=false;
1129         if (newVendor.getVendorHeader() != null) {
1130             if (newVendor.getVendorHeader().getVendorForeignIndicator() != null) {
1131                 foriegnVendor = newVendor.getVendorHeader().getVendorForeignIndicator();
1132             }
1133         }
1134         if(foriegnVendor) {
1135 
1136             if(StringUtils.isNotEmpty(phoneNumber) && !SpringContext.getBean(OleForiegnVendorPhoneNumberService.class).isValidForiegnVendorPhoneNumber(phoneNumber)){
1137                 GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_PHONE_NUMBER, VendorKeyConstants.ERROR_FORIEGN_VENDOR_PHONE_NUMBER);
1138                 valid &= false;
1139             }
1140         }
1141         else {
1142             if (StringUtils.isNotEmpty(phoneNumber) && !SpringContext.getBean(PhoneNumberService.class).isValidPhoneNumber(phoneNumber)) {
1143                 GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_PHONE_NUMBER, VendorKeyConstants.ERROR_PHONE_NUMBER);
1144                 valid &= false;
1145             }
1146         }
1147         return valid;
1148     }
1149 
1150     
1151 
1152 
1153 
1154 
1155 
1156     private boolean processContactValidation(MaintenanceDocument document) {
1157         boolean valid = true;
1158         int i = 0;
1159         for (VendorContact contact : newVendor.getVendorContacts()) {
1160             String errorPath = MAINTAINABLE_ERROR_PREFIX + VendorPropertyConstants.VENDOR_CONTACT + "[" + i + "]";
1161             GlobalVariables.getMessageMap().addToErrorPath(errorPath);
1162 
1163             this.getDictionaryValidationService().validateBusinessObject(contact);
1164             Map<String, AutoPopulatingList<ErrorMessage>> errors = GlobalVariables.getMessageMap().getErrorMessages();
1165             if ((errors != null ) && (!errors.isEmpty())) {
1166                 valid = false;
1167             }
1168             i++;
1169             GlobalVariables.getMessageMap().clearErrorPath();
1170         }
1171         return valid;
1172     }
1173 
1174     
1175 
1176 
1177 
1178 
1179 
1180     private boolean processCustomerNumberValidation(MaintenanceDocument document) {
1181         boolean valid = true;
1182 
1183         List<VendorCustomerNumber> customerNumbers = newVendor.getVendorCustomerNumbers();
1184         for (VendorCustomerNumber customerNumber : customerNumbers) {
1185             valid &= validateVendorCustomerNumber(customerNumber);
1186         }
1187         return valid;
1188     }
1189 
1190     
1191 
1192 
1193 
1194 
1195 
1196     boolean validateVendorCustomerNumber(VendorCustomerNumber customerNumber) {
1197         boolean valid = true;
1198 
1199         
1200         String chartOfAccountsCode = customerNumber.getChartOfAccountsCode();
1201         String orgCode = customerNumber.getVendorOrganizationCode();
1202         String carrierCode = customerNumber.getVendorStandardDeliveryCarrier();
1203         if (!StringUtils.isBlank(chartOfAccountsCode) && !StringUtils.isBlank(orgCode)) {
1204             Map chartOrgMap = new HashMap();
1205             chartOrgMap.put("chartOfAccountsCode", chartOfAccountsCode);
1206             if (SpringContext.getBean(BusinessObjectService.class).countMatching(Chart.class, chartOrgMap) < 1) {
1207                 GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_CUSTOMER_NUMBER_CHART_OF_ACCOUNTS_CODE, OLEKeyConstants.ERROR_EXISTENCE, chartOfAccountsCode);
1208                 valid &= false;
1209             }
1210             chartOrgMap.put("organizationCode", orgCode);
1211             if (SpringContext.getBean(BusinessObjectService.class).countMatching(Organization.class, chartOrgMap) < 1) {
1212                 GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_CUSTOMER_NUMBER_ORGANIZATION_CODE, OLEKeyConstants.ERROR_EXISTENCE, orgCode);
1213                 valid &= false;
1214             }
1215 
1216         }
1217         if (!StringUtils.isBlank(carrierCode)) {
1218             Map carrierMap = new HashMap();
1219             carrierMap.put("carrierCode", carrierCode);
1220             if (SpringContext.getBean(BusinessObjectService.class).countMatching(Carrier.class, carrierMap) < 1) {
1221                 GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_CARRIER_CODE, OLEKeyConstants.ERROR_EXISTENCE, carrierCode);
1222                 valid &= false;
1223             }
1224         }
1225 
1226         return valid;
1227     }
1228 
1229    
1230 
1231 
1232 
1233 
1234 
1235 
1236 
1237 
1238 
1239 
1240 
1241 
1242 
1243 
1244 
1245 
1246 
1247 
1248 
1249     
1250 
1251 
1252 
1253 
1254 
1255     private boolean processContractValidation(MaintenanceDocument document) {
1256         boolean valid = true;
1257         List<VendorContract> contracts = newVendor.getVendorContracts();
1258         if (ObjectUtils.isNull(contracts)) {
1259             return valid;
1260         }
1261 
1262         
1263         if (contracts.size() > 0 && !newVendor.getVendorHeader().getVendorType().isVendorContractAllowedIndicator()) {
1264             valid = false;
1265             String errorPath = MAINTAINABLE_ERROR_PREFIX + VendorPropertyConstants.VENDOR_CONTRACT + "[0]";
1266             GlobalVariables.getMessageMap().addToErrorPath(errorPath);
1267             GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_CONTRACT_NAME, VendorKeyConstants.ERROR_VENDOR_CONTRACT_NOT_ALLOWED);
1268             GlobalVariables.getMessageMap().removeFromErrorPath(errorPath);
1269             return valid;
1270         }
1271 
1272         for (int i = 0; i < contracts.size(); i++) {
1273             VendorContract contract = contracts.get(i);
1274 
1275             String errorPath = MAINTAINABLE_ERROR_PREFIX + VendorPropertyConstants.VENDOR_CONTRACT + "[" + i + "]";
1276             GlobalVariables.getMessageMap().addToErrorPath(errorPath);
1277 
1278             valid &= validateVendorContractPOLimitAndExcludeFlagCombination(contract);
1279             valid &= validateVendorContractBeginEndDates(contract);
1280             valid &= processContractB2BValidation(document, contract, i);
1281 
1282             GlobalVariables.getMessageMap().removeFromErrorPath(errorPath);
1283         }
1284 
1285 
1286         return valid;
1287     }
1288 
1289     
1290 
1291 
1292 
1293 
1294 
1295 
1296 
1297 
1298 
1299     boolean validateVendorContractPOLimitAndExcludeFlagCombination(VendorContract contract) {
1300         boolean valid = true;
1301         boolean NoOrgHasApoLimit = true;
1302 
1303         List<VendorContractOrganization> organizations = contract.getVendorContractOrganizations();
1304         if (ObjectUtils.isNotNull(organizations)) {
1305             int organizationCounter = 0;
1306             for (VendorContractOrganization organization : organizations) {
1307                 if (ObjectUtils.isNotNull(organization.getVendorContractPurchaseOrderLimitAmount())) {
1308                     NoOrgHasApoLimit = false;
1309                 }
1310                 valid &= validateVendorContractOrganization(organization);
1311                 organizationCounter++;
1312             }
1313         }
1314         if (NoOrgHasApoLimit && ObjectUtils.isNull(contract.getOrganizationAutomaticPurchaseOrderLimit())) {
1315             
1316             GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_CONTRACT_DEFAULT_APO_LIMIT, VendorKeyConstants.ERROR_VENDOR_CONTRACT_NO_APO_LIMIT);
1317             valid &= false;
1318         }
1319         return valid;
1320     }
1321 
1322     
1323 
1324 
1325 
1326 
1327 
1328 
1329 
1330 
1331     boolean validateVendorContractBeginEndDates(VendorContract contract) {
1332         boolean valid = true;
1333 
1334         if (ObjectUtils.isNotNull(contract.getVendorContractBeginningDate()) && ObjectUtils.isNull(contract.getVendorContractEndDate())) {
1335             GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_CONTRACT_END_DATE, VendorKeyConstants.ERROR_VENDOR_CONTRACT_BEGIN_DATE_NO_END_DATE);
1336             valid &= false;
1337         }
1338         else {
1339             if (ObjectUtils.isNull(contract.getVendorContractBeginningDate()) && ObjectUtils.isNotNull(contract.getVendorContractEndDate())) {
1340                 GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_CONTRACT_BEGIN_DATE, VendorKeyConstants.ERROR_VENDOR_CONTRACT_END_DATE_NO_BEGIN_DATE);
1341                 valid &= false;
1342             }
1343         }
1344         if (valid && ObjectUtils.isNotNull(contract.getVendorContractBeginningDate()) && ObjectUtils.isNotNull(contract.getVendorContractEndDate())) {
1345             if (contract.getVendorContractBeginningDate().after(contract.getVendorContractEndDate())) {
1346                 GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_CONTRACT_BEGIN_DATE, VendorKeyConstants.ERROR_VENDOR_CONTRACT_BEGIN_DATE_AFTER_END);
1347                 valid &= false;
1348             }
1349         }
1350 
1351         return valid;
1352     }
1353 
1354     
1355 
1356 
1357 
1358 
1359 
1360 
1361 
1362     boolean validateVendorContractOrganization(VendorContractOrganization organization) {
1363         boolean valid = true;
1364 
1365         boolean isExcluded = organization.isVendorContractExcludeIndicator();
1366         if (isExcluded) {
1367             if (ObjectUtils.isNotNull(organization.getVendorContractPurchaseOrderLimitAmount())) {
1368                 
1369                 GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_CONTRACT_ORGANIZATION_APO_LIMIT, VendorKeyConstants.ERROR_VENDOR_CONTRACT_ORG_EXCLUDED_WITH_APO_LIMIT);
1370                 valid &= false;
1371             }
1372         }
1373         else { 
1374             if (ObjectUtils.isNull(organization.getVendorContractPurchaseOrderLimitAmount())) {
1375                 
1376                 GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_CONTRACT_ORGANIZATION_APO_LIMIT, VendorKeyConstants.ERROR_VENDOR_CONTRACT_ORG_NOT_EXCLUDED_NO_APO_LIMIT);
1377                 valid &= false;
1378             }
1379         }
1380 
1381         
1382         String chartOfAccountsCode = organization.getChartOfAccountsCode();
1383         String orgCode = organization.getOrganizationCode();
1384         if (!StringUtils.isBlank(chartOfAccountsCode) && !StringUtils.isBlank(orgCode)) {
1385             Map chartOrgMap = new HashMap();
1386             chartOrgMap.put("chartOfAccountsCode", chartOfAccountsCode);
1387             if (SpringContext.getBean(BusinessObjectService.class).countMatching(Chart.class, chartOrgMap) < 1) {
1388                 GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_CONTRACT_CHART_OF_ACCOUNTS_CODE, OLEKeyConstants.ERROR_EXISTENCE, chartOfAccountsCode);
1389                 valid &= false;
1390             }
1391             chartOrgMap.put("organizationCode", orgCode);
1392             if (SpringContext.getBean(BusinessObjectService.class).countMatching(Organization.class, chartOrgMap) < 1) {
1393                 GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_CONTRACT_ORGANIZATION_CODE, OLEKeyConstants.ERROR_EXISTENCE, orgCode);
1394                 valid &= false;
1395             }
1396         }
1397 
1398         return valid;
1399     }
1400 
1401     
1402 
1403 
1404 
1405 
1406 
1407     private boolean processContractB2BValidation(MaintenanceDocument document, VendorContract contract, int contractPos) {
1408         boolean valid = true;
1409         List<Integer> indexOfB2BContracts = new ArrayList();
1410         
1411         List<VendorContract> contracts = newVendor.getVendorContracts();
1412         if (ObjectUtils.isNull(contracts)) {
1413             return valid;
1414         }
1415         
1416         if(contractPos == -1){
1417             if(contract.getVendorB2bIndicator()){
1418                 for (int i = 0; i < contracts.size(); i++) {
1419                     VendorContract vndrContract = contracts.get(i);
1420                     if(vndrContract.getVendorB2bIndicator()){
1421                         
1422                         if(contract.getVendorCampusCode().equals(vndrContract.getVendorCampusCode())){
1423                             valid &= false;
1424                             GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_CONTRACT_B2B_INDICATOR, VendorKeyConstants.ERROR_VENDOR_CONTRACT_B2B_LIMIT_EXCEEDED, contract.getVendorCampusCode());
1425                         }
1426                     }
1427                 }
1428             }
1429         } else
1430         {
1431             if(contract.getVendorB2bIndicator()){
1432                 for (int i = 0; i < contracts.size(); i++) {
1433                     VendorContract vndrContract = contracts.get(i);
1434                     if(vndrContract.getVendorB2bIndicator()){
1435                         
1436                         if(i != contractPos){
1437                             
1438                             if(contract.getVendorCampusCode().equals(vndrContract.getVendorCampusCode())){
1439                                 valid &= false;
1440                                 String [] errorArray = new String []{contract.getVendorContractName(), contract.getVendorCampusCode()};
1441                                 GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_CONTRACT_B2B_INDICATOR, VendorKeyConstants.ERROR_VENDOR_CONTRACT_B2B_LIMIT_EXCEEDED_DB, errorArray);
1442                             }
1443                         }
1444                     }
1445                 }
1446             }
1447         }
1448        return valid;
1449     }
1450 
1451     
1452 
1453 
1454 
1455 
1456 
1457 
1458     @Override
1459     public boolean processCustomAddCollectionLineBusinessRules(MaintenanceDocument document, String collectionName, PersistableBusinessObject bo) {
1460         boolean success = true;
1461 
1462         
1463         bo.refreshNonUpdateableReferences();
1464 
1465         if (bo instanceof VendorAddress) {
1466             VendorAddress address = (VendorAddress) bo;
1467             success &= checkAddressCountryEmptyStateZip(address);
1468             VendorDetail vendorDetail = (VendorDetail) document.getNewMaintainableObject().getBusinessObject();
1469         }
1470         if (bo instanceof VendorContract) {
1471             VendorContract contract = (VendorContract) bo;
1472             success &= validateVendorContractBeginEndDates(contract);
1473             success &= processContractB2BValidation(document,contract, -1);
1474         }
1475         if (bo instanceof VendorContractOrganization) {
1476             VendorContractOrganization contractOrg = (VendorContractOrganization) bo;
1477             success &= validateVendorContractOrganization(contractOrg);
1478         }
1479         if (bo instanceof VendorCustomerNumber) {
1480             VendorCustomerNumber customerNumber = (VendorCustomerNumber) bo;
1481             success &= validateVendorCustomerNumber(customerNumber);
1482         }
1483       
1484 
1485 
1486 
1487 
1488         if (bo instanceof VendorTransmissionFormatDetail) {
1489             VendorDetail newVendorDetail  = (VendorDetail) document.getNewMaintainableObject().getBusinessObject();
1490             newVendorDetail.refreshNonUpdateableReferences();
1491             VendorTransmissionFormatDetail vendorTransmissionFormatDetail = (VendorTransmissionFormatDetail) bo;
1492             success &= validateVendorTransmissionFormatDetail(vendorTransmissionFormatDetail,newVendorDetail);
1493         }
1494 
1495         if (bo instanceof VendorDefaultAddress) {
1496             VendorDefaultAddress defaultAddress = (VendorDefaultAddress) bo;
1497             String parentName = StringUtils.substringBeforeLast(collectionName, ".");
1498             VendorAddress parent = (VendorAddress) ObjectUtils.getPropertyValue(this.getNewBo(), parentName);
1499             VendorDetail vendorDetail = (VendorDetail) document.getNewMaintainableObject().getBusinessObject();
1500             success &= checkDefaultAddressCampus(vendorDetail, defaultAddress, parent);
1501         }
1502 
1503         return success;
1504     }
1505 
1506 
1507       boolean validateVendorTransmissionFormatDetail(VendorTransmissionFormatDetail vendorTransmissionFormatDetail,VendorDetail vendorDetail){
1508        boolean valid = true;
1509        boolean preferredTransmissionFlag = false;
1510        int oldPreferredTransmissionFlagCount = 0;
1511        boolean newVendorPreferredTransmissionFormat = vendorTransmissionFormatDetail.isVendorPreferredTransmissionFormat();
1512           for (VendorTransmissionFormatDetail oldVendorTransmissionFormatDetail : vendorDetail.getVendorTransmissionFormat()) {
1513               oldVendorTransmissionFormatDetail.refreshNonUpdateableReferences();
1514               if(oldVendorTransmissionFormatDetail.isVendorPreferredTransmissionFormat()){
1515                 preferredTransmissionFlag = true;
1516                 oldPreferredTransmissionFlagCount = oldPreferredTransmissionFlagCount + 1;
1517              }
1518           }
1519          if(oldPreferredTransmissionFlagCount > 1){
1520              GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_PREFERRED_TRANSMISSION, OLEKeyConstants.ERROR_DUPLICATE_PREFERRED_FORMAT, "preferred transmission format");
1521              valid &= false;
1522          }
1523          if(newVendorPreferredTransmissionFormat && preferredTransmissionFlag){
1524             GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_PREFERRED_TRANSMISSION, OLEKeyConstants.ERROR_DUPLICATE_PREFERRED_FORMAT, "preferred transmission format");
1525             valid &= false;
1526          }
1527 
1528        return valid;
1529     }
1530 
1531     
1532 
1533 
1534 
1535 
1536 
1537 
1538     private boolean validateVendorWithholdingTaxDates(VendorDetail vdDocument) {
1539         boolean valid = true;
1540         DateTimeService dateTimeService = SpringContext.getBean(DateTimeService.class);
1541 
1542         Date beginDate = vdDocument.getVendorHeader().getVendorFederalWithholdingTaxBeginningDate();
1543         Date endDate = vdDocument.getVendorHeader().getVendorFederalWithholdingTaxEndDate();
1544         if (ObjectUtils.isNotNull(beginDate) && ObjectUtils.isNotNull(endDate)) {
1545             if (dateTimeService.dateDiff(beginDate, endDate, false) <= 0) {
1546                 putFieldError(VendorPropertyConstants.VENDOR_FEDERAL_WITHOLDING_TAX_BEGINNING_DATE, VendorKeyConstants.ERROR_VENDOR_TAX_BEGIN_DATE_AFTER_END);
1547                 valid &= false;
1548             }
1549         }
1550         return valid;
1551 
1552     }
1553 
1554     
1555 
1556 
1557 
1558 
1559 
1560     private boolean validateVendorW8BenOrW9ReceivedIndicator(VendorDetail vdDocument) {
1561         boolean valid = true;
1562 
1563         if (ObjectUtils.isNotNull(vdDocument.getVendorHeader().getVendorW9ReceivedIndicator()) && ObjectUtils.isNotNull(vdDocument.getVendorHeader().getVendorW8BenReceivedIndicator()) && vdDocument.getVendorHeader().getVendorW9ReceivedIndicator() && vdDocument.getVendorHeader().getVendorW8BenReceivedIndicator()) {
1564             putFieldError(VendorPropertyConstants.VENDOR_W9_RECEIVED_INDICATOR, VendorKeyConstants.ERROR_VENDOR_W9_AND_W8_RECEIVED_INDICATOR_BOTH_TRUE);
1565             valid &= false;
1566         }
1567         return valid;
1568 
1569     }
1570 
1571     
1572 
1573 
1574 
1575 
1576 
1577 
1578     @Override
1579     public boolean processAddCollectionLineBusinessRules(MaintenanceDocument document, String collectionName, PersistableBusinessObject bo) {
1580         if (collectionName.equals(VendorPropertyConstants.VENDOR_CONTRACT)) {
1581             VendorDetail vendorDetail = (VendorDetail)document.getDocumentBusinessObject();
1582             vendorDetail.getVendorHeader().refreshReferenceObject("vendorType");
1583             VendorType vendorType = vendorDetail.getVendorHeader().getVendorType();
1584             if (!vendorType.isVendorContractAllowedIndicator()) {
1585                 String propertyName = "add." + collectionName + "." + VendorPropertyConstants.VENDOR_CONTRACT_NAME;
1586                 putFieldError(propertyName, VendorKeyConstants.ERROR_VENDOR_CONTRACT_NOT_ALLOWED);
1587                 return false;
1588             }
1589         }
1590         return super.processAddCollectionLineBusinessRules(document, collectionName, bo);
1591     }
1592 }