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.ar.document.validation.impl;
017
018 import static org.kuali.kfs.sys.document.validation.impl.AccountingDocumentRuleBaseConstants.ERROR_PATH.DOCUMENT_ERROR_PREFIX;
019
020 import java.sql.Timestamp;
021
022 import org.kuali.kfs.module.ar.ArConstants;
023 import org.kuali.kfs.module.ar.ArKeyConstants;
024 import org.kuali.kfs.module.ar.ArPropertyConstants;
025 import org.kuali.kfs.module.ar.document.CustomerInvoiceDocument;
026 import org.kuali.kfs.module.ar.document.service.CustomerAddressService;
027 import org.kuali.kfs.sys.context.SpringContext;
028 import org.kuali.kfs.sys.document.validation.GenericValidation;
029 import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
030 import org.kuali.rice.kns.service.DateTimeService;
031 import org.kuali.rice.kns.util.GlobalVariables;
032 import org.kuali.rice.kns.util.ObjectUtils;
033
034 public class CustomerInvoiceCustomerAddressValidation extends GenericValidation {
035
036 private CustomerInvoiceDocument customerInvoiceDocument;
037 private CustomerAddressService customerAddressService;
038
039 public boolean validate(AttributedDocumentEvent event) {
040 boolean success = true;
041 String customerNumber = customerInvoiceDocument.getAccountsReceivableDocumentHeader().getCustomerNumber();
042
043 if (ObjectUtils.isNotNull(customerInvoiceDocument.getCustomerShipToAddressIdentifier())) {
044 success &= isCustomerAddressValid(customerNumber, customerInvoiceDocument.getCustomerShipToAddressIdentifier(), true);
045 success &= isCustomerAddressActive(customerNumber, customerInvoiceDocument.getCustomerShipToAddressIdentifier(), true);
046 }
047 if (ObjectUtils.isNotNull(customerInvoiceDocument.getCustomerBillToAddressIdentifier())) {
048 success &= isCustomerAddressValid(customerNumber, customerInvoiceDocument.getCustomerBillToAddressIdentifier(), false);
049 success &= isCustomerAddressActive(customerNumber, customerInvoiceDocument.getCustomerBillToAddressIdentifier(), false);
050 }
051
052 return success;
053 }
054
055 /**
056 * This method validates if a customer address is valid
057 *
058 * @param customerInvoiceDocument
059 * @param isShipToAddress
060 * @return
061 */
062 protected boolean isCustomerAddressValid(String customerNumber, Integer customerAddressIdentifier, boolean isShipToAddress) {
063
064 if (!customerAddressService.customerAddressExists(customerNumber, customerAddressIdentifier)) {
065 if (isShipToAddress) {
066 GlobalVariables.getMessageMap().putError(DOCUMENT_ERROR_PREFIX + ArPropertyConstants.CustomerInvoiceDocumentFields.SHIP_TO_ADDRESS_IDENTIFIER, ArKeyConstants.ERROR_CUSTOMER_INVOICE_DOCUMENT_INVALID_SHIP_TO_ADDRESS_IDENTIFIER);
067 }
068 else {
069 GlobalVariables.getMessageMap().putError(DOCUMENT_ERROR_PREFIX + ArPropertyConstants.CustomerInvoiceDocumentFields.BILL_TO_ADDRESS_IDENTIFIER, ArKeyConstants.ERROR_CUSTOMER_INVOICE_DOCUMENT_INVALID_BILL_TO_ADDRESS_IDENTIFIER);
070 }
071 return false;
072
073 }
074 return true;
075 }
076
077 /**
078 * This method validates if a customer address is active
079 *
080 * @param customerInvoiceDocument
081 * @param isShipToAddress
082 * @return
083 */
084 protected boolean isCustomerAddressActive(String customerNumber, Integer customerAddressIdentifier, boolean isShipToAddress) {
085
086 if (!customerAddressService.customerAddressActive(customerNumber, customerAddressIdentifier)) {
087 if (isShipToAddress) {
088 GlobalVariables.getMessageMap().putError(DOCUMENT_ERROR_PREFIX + ArPropertyConstants.CustomerInvoiceDocumentFields.SHIP_TO_ADDRESS_IDENTIFIER, ArKeyConstants.ERROR_CUSTOMER_INVOICE_DOCUMENT_INACTIVE_SHIP_TO_ADDRESS_IDENTIFIER);
089 }
090 else {
091 GlobalVariables.getMessageMap().putError(DOCUMENT_ERROR_PREFIX + ArPropertyConstants.CustomerInvoiceDocumentFields.BILL_TO_ADDRESS_IDENTIFIER, ArKeyConstants.ERROR_CUSTOMER_INVOICE_DOCUMENT_INACTIVE_BILL_TO_ADDRESS_IDENTIFIER);
092 }
093 return false;
094 }
095 return true;
096 }
097
098 public CustomerAddressService getCustomerAddressService() {
099 return customerAddressService;
100 }
101
102 public void setCustomerAddressService(CustomerAddressService customerAddressService) {
103 this.customerAddressService = customerAddressService;
104 }
105
106 public CustomerInvoiceDocument getCustomerInvoiceDocument() {
107 return customerInvoiceDocument;
108 }
109
110 public void setCustomerInvoiceDocument(CustomerInvoiceDocument customerInvoiceDocument) {
111 this.customerInvoiceDocument = customerInvoiceDocument;
112 }
113
114 }