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.List;
019
020 import org.apache.commons.lang.StringUtils;
021 import org.kuali.kfs.module.purap.PurapConstants;
022 import org.kuali.kfs.module.purap.PurapKeyConstants;
023 import org.kuali.kfs.module.purap.PurapPropertyConstants;
024 import org.kuali.kfs.module.purap.businessobject.PurchaseOrderItem;
025 import org.kuali.kfs.module.purap.document.PaymentRequestDocument;
026 import org.kuali.kfs.module.purap.document.PurchaseOrderDocument;
027 import org.kuali.kfs.sys.KFSPropertyConstants;
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.util.GlobalVariables;
031 import org.kuali.rice.kns.util.KualiDecimal;
032 import org.kuali.rice.kns.util.ObjectUtils;
033
034 public class PaymentRequestPurchaseOrderIdValidation extends GenericValidation {
035
036 public boolean validate(AttributedDocumentEvent event) {
037 boolean valid = true;
038 PaymentRequestDocument document = (PaymentRequestDocument)event.getDocument();
039 GlobalVariables.getMessageMap().clearErrorPath();
040 GlobalVariables.getMessageMap().addToErrorPath(KFSPropertyConstants.DOCUMENT);
041
042 Integer POID = document.getPurchaseOrderIdentifier();
043
044 PurchaseOrderDocument purchaseOrderDocument = document.getPurchaseOrderDocument();
045 if (ObjectUtils.isNull(purchaseOrderDocument)) {
046 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_PURCHASE_ORDER_NOT_EXIST);
047 valid &= false;
048 }
049 else if (purchaseOrderDocument.isPendingActionIndicator()) {
050 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_PURCHASE_PENDING_ACTION);
051 valid &= false;
052 }
053 else if (!StringUtils.equals(purchaseOrderDocument.getStatusCode(), PurapConstants.PurchaseOrderStatuses.OPEN)) {
054 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_PURCHASE_ORDER_NOT_OPEN);
055 valid &= false;
056 // if the PO is pending and it is not a Retransmit, we cannot generate a Payment Request for it
057 }
058 else {
059 // Verify that there exists at least 1 item left to be invoiced
060 //valid &= encumberedItemExistsForInvoicing(purchaseOrderDocument);
061 }
062 GlobalVariables.getMessageMap().clearErrorPath();
063 return valid;
064 }
065
066 /**
067 * Determines if there are items with encumbrances to be invoiced on passed in
068 * purchase order document.
069 *
070 * @param document - purchase order document
071 * @return
072 */
073 protected boolean encumberedItemExistsForInvoicing(PurchaseOrderDocument document) {
074 boolean zeroDollar = true;
075 GlobalVariables.getMessageMap().clearErrorPath();
076 GlobalVariables.getMessageMap().addToErrorPath(KFSPropertyConstants.DOCUMENT);
077 for (PurchaseOrderItem poi : (List<PurchaseOrderItem>) document.getItems()) {
078 // Quantity-based items
079 if (poi.getItemType().isLineItemIndicator() && poi.getItemType().isQuantityBasedGeneralLedgerIndicator()) {
080 KualiDecimal encumberedQuantity = poi.getItemOutstandingEncumberedQuantity() == null ? KualiDecimal.ZERO : poi.getItemOutstandingEncumberedQuantity();
081 if (encumberedQuantity.compareTo(KualiDecimal.ZERO) == 1) {
082 zeroDollar = false;
083 break;
084 }
085 }
086 // Service Items or Below-the-line Items
087 else if (poi.getItemType().isAmountBasedGeneralLedgerIndicator() || poi.getItemType().isAdditionalChargeIndicator()) {
088 KualiDecimal encumberedAmount = poi.getItemOutstandingEncumberedAmount() == null ? KualiDecimal.ZERO : poi.getItemOutstandingEncumberedAmount();
089 if (encumberedAmount.compareTo(KualiDecimal.ZERO) == 1) {
090 zeroDollar = false;
091 break;
092 }
093 }
094 }
095 if (zeroDollar) {
096 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_NO_ITEMS_TO_INVOICE);
097 }
098 GlobalVariables.getMessageMap().clearErrorPath();
099 return !zeroDollar;
100 }
101
102 }