001/*
002 * Copyright 2009 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.kuali.ole.module.purap.businessobject.PaymentRequestItem;
019import org.kuali.ole.module.purap.document.PaymentRequestDocument;
020import org.kuali.ole.sys.document.validation.GenericValidation;
021import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
022
023import java.math.BigDecimal;
024
025public class PaymentRequestReviewValidation extends GenericValidation {
026    protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PaymentRequestReviewValidation.class);
027
028    private PaymentRequestItem itemForValidation;
029
030    public boolean validate(AttributedDocumentEvent event) {
031        boolean valid = true;
032        PaymentRequestDocument paymentRequest = (PaymentRequestDocument) event.getDocument();
033
034
035        boolean containsAccounts = false;
036        int accountLineNbr = 0;
037
038        String identifier = itemForValidation.getItemIdentifierString();
039        BigDecimal total = BigDecimal.ZERO;
040        if (LOG.isDebugEnabled()) {
041            LOG.debug("validatePaymentRequestReview() The " + identifier + " is getting the total percent field set to " + BigDecimal.ZERO);
042        }
043
044        if ((itemForValidation.getTotalAmount() != null && itemForValidation.getTotalAmount().isNonZero() && itemForValidation.getItemType().isLineItemIndicator() && ((itemForValidation.getItemType().isAmountBasedGeneralLedgerIndicator() && (itemForValidation.getPoOutstandingAmount() == null || itemForValidation.getPoOutstandingAmount().isZero())) || (itemForValidation.getItemType().isQuantityBasedGeneralLedgerIndicator() && (itemForValidation.getPoOutstandingQuantity() == null || itemForValidation.getPoOutstandingQuantity().isZero()))))) {
045            // ERROR because we have total amount and no open encumberance on the PO item
046            // this error should have been caught at an earlier level
047            if (itemForValidation.getItemType().isAmountBasedGeneralLedgerIndicator()) {
048                String error = "Payment Request " + paymentRequest.getPurapDocumentIdentifier() + ", " + identifier + " has total amount '" + itemForValidation.getTotalAmount() + "' but outstanding encumbered amount " + itemForValidation.getPoOutstandingAmount();
049                LOG.error("validatePaymentRequestReview() " + error);
050            } else {
051                String error = "Payment Request " + paymentRequest.getPurapDocumentIdentifier() + ", " + identifier + " has quantity '" + itemForValidation.getItemQuantity() + "' but outstanding encumbered quantity " + itemForValidation.getPoOutstandingQuantity();
052                LOG.error("validatePaymentRequestReview() " + error);
053            }
054        } else {
055            // not validating but ok
056            String error = "Payment Request " + paymentRequest.getPurapDocumentIdentifier() + ", " + identifier + " has total amount '" + itemForValidation.getTotalAmount() + "'";
057            if (itemForValidation.getItemType().isLineItemIndicator()) {
058                if (itemForValidation.getItemType().isAmountBasedGeneralLedgerIndicator()) {
059                    error = error + " with outstanding encumbered amount " + itemForValidation.getPoOutstandingAmount();
060                } else {
061                    error = error + " with outstanding encumbered quantity " + itemForValidation.getPoOutstandingQuantity();
062                }
063            }
064            LOG.info("validatePaymentRequestReview() " + error);
065        }
066
067        return valid;
068    }
069
070
071    public PaymentRequestItem getItemForValidation() {
072        return itemForValidation;
073    }
074
075    public void setItemForValidation(PaymentRequestItem itemForValidation) {
076        this.itemForValidation = itemForValidation;
077    }
078
079
080}