001/*
002 * Copyright 2008 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 */
016package org.kuali.ole.module.purap.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.PurapPropertyConstants;
022import org.kuali.ole.module.purap.businessobject.PurchaseOrderItem;
023import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
024import org.kuali.ole.select.businessobject.OleInvoiceItem;
025import org.kuali.ole.select.document.OleInvoiceDocument;
026import org.kuali.ole.select.document.OlePurchaseOrderDocument;
027import org.kuali.ole.sys.OLEPropertyConstants;
028import org.kuali.ole.sys.context.SpringContext;
029import org.kuali.ole.sys.document.validation.GenericValidation;
030import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
031import org.kuali.rice.core.api.util.type.KualiDecimal;
032import org.kuali.rice.krad.service.DocumentHeaderService;
033import org.kuali.rice.krad.service.KRADServiceLocator;
034import org.kuali.rice.krad.util.GlobalVariables;
035
036import java.util.HashMap;
037import java.util.List;
038import java.util.Map;
039
040public class InvoicePurchaseOrderIdValidation extends GenericValidation {
041
042    public boolean validate(AttributedDocumentEvent event) {
043        boolean valid = true;
044        boolean lineItemtypeIndicator = false;
045        OleInvoiceDocument document = (OleInvoiceDocument) event.getDocument();
046        GlobalVariables.getMessageMap().clearErrorPath();
047        GlobalVariables.getMessageMap().addToErrorPath(OLEPropertyConstants.DOCUMENT);
048
049        Integer POID = document.getPurchaseOrderIdentifier();
050        if (document.getItems().size() > 0) {
051            for (OleInvoiceItem invoiceItem : (List<OleInvoiceItem>) document.getItems()) {
052                if (invoiceItem.getItemType().isLineItemIndicator()) {
053                    lineItemtypeIndicator = true;
054                }
055                //PurchaseOrderDocument purchaseOrderDocument =document.getPurchaseOrderDocument(invoiceItem.getPurchaseOrderIdentifier());
056                PurchaseOrderDocument purchaseOrderDocument = null ;
057                Map map = new HashMap();
058                map.put("purapDocumentIdentifier",invoiceItem.getPurchaseOrderIdentifier());
059                List<OlePurchaseOrderDocument> purchaseOrderDocumentList = (List<OlePurchaseOrderDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePurchaseOrderDocument.class, map);
060                if(purchaseOrderDocumentList!=null && purchaseOrderDocumentList.size()>0){
061                    for (OlePurchaseOrderDocument poDoc : purchaseOrderDocumentList) {
062                        if(poDoc.getPurchaseOrderCurrentIndicatorForSearching()){
063                            purchaseOrderDocument = poDoc;
064                        }
065                    }
066                }
067                if (purchaseOrderDocument != null && purchaseOrderDocument.getDocumentHeader() == null) {
068                    purchaseOrderDocument.setDocumentHeader(SpringContext.getBean(DocumentHeaderService.class).getDocumentHeaderById(purchaseOrderDocument.getDocumentNumber()));
069
070                }
071                //OlePurchaseOrderDocument purchaseOrderDocument = (OlePurchaseOrderDocument) purchaseOrderDocument1;
072                if (purchaseOrderDocument != null && purchaseOrderDocument.isPendingActionIndicator()) {
073                    GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_PURCHASE_PENDING_ACTION);
074                    valid &= false;
075                } else if (purchaseOrderDocument != null && !StringUtils.equals(purchaseOrderDocument.getApplicationDocumentStatus(), PurapConstants.PurchaseOrderStatuses.APPDOC_OPEN)) {
076                    GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_PURCHASE_ORDER_NOT_OPEN);
077                    valid &= false;
078                    // if the PO is pending and it is not a Retransmit, we cannot generate a Invoice for it
079                }
080            }
081        } else {
082            GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_PURCHASE_ORDER_NOT_EXIST);
083            valid &= false;
084        }
085
086
087        //for (OlePurchaseOrderDocument purchaseOrderDocument : document.getPurchaseOrderDocuments()) {
088        //PurchaseOrderDocument purchaseOrderDocument = document.getPurchaseOrderDocument();
089            /*if (!lineItemtypeIndicator) {
090                GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_PURCHASE_ORDER_NOT_EXIST);
091                valid &= false;
092            }*/
093        return valid;
094    }
095
096    /**
097     * Determines if there are items with encumbrances to be invoiced on passed in
098     * purchase order document.
099     *
100     * @param document - purchase order document
101     * @return
102     */
103    protected boolean encumberedItemExistsForInvoicing(PurchaseOrderDocument document) {
104        boolean zeroDollar = true;
105        GlobalVariables.getMessageMap().clearErrorPath();
106        GlobalVariables.getMessageMap().addToErrorPath(OLEPropertyConstants.DOCUMENT);
107        for (PurchaseOrderItem poi : (List<PurchaseOrderItem>) document.getItems()) {
108            // Quantity-based items
109            if (poi.getItemType().isLineItemIndicator() && poi.getItemType().isQuantityBasedGeneralLedgerIndicator()) {
110                KualiDecimal encumberedQuantity = poi.getItemOutstandingEncumberedQuantity() == null ? KualiDecimal.ZERO : poi.getItemOutstandingEncumberedQuantity();
111                if (encumberedQuantity.compareTo(KualiDecimal.ZERO) == 1) {
112                    zeroDollar = false;
113                    break;
114                }
115            }
116            // Service Items or Below-the-line Items
117            else if (poi.getItemType().isAmountBasedGeneralLedgerIndicator() || poi.getItemType().isAdditionalChargeIndicator()) {
118                KualiDecimal encumberedAmount = poi.getItemOutstandingEncumberedAmount() == null ? KualiDecimal.ZERO : poi.getItemOutstandingEncumberedAmount();
119                if (encumberedAmount.compareTo(KualiDecimal.ZERO) == 1) {
120                    zeroDollar = false;
121                    break;
122                }
123            }
124        }
125        if (zeroDollar) {
126            GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_NO_ITEMS_TO_INVOICE);
127        }
128        //GlobalVariables.getMessageMap().clearErrorPath();
129        return !zeroDollar;
130    }
131
132}