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 */
016package org.kuali.ole.select.document.validation.impl;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.ole.module.purap.PurapConstants;
020import org.kuali.ole.module.purap.PurapConstants.ItemFields;
021import org.kuali.ole.module.purap.PurapKeyConstants;
022import org.kuali.ole.module.purap.businessobject.InvoiceItem;
023import org.kuali.ole.module.purap.document.validation.impl.InvoiceProcessItemValidation;
024import org.kuali.rice.krad.util.GlobalVariables;
025import org.kuali.rice.krad.util.ObjectUtils;
026
027public class OleInvoiceProcessItemValidation extends InvoiceProcessItemValidation {
028
029    /**
030     * Validates above the line items.
031     *
032     * @param item             - invoice item
033     * @param identifierString - identifier string used to mark in an error map
034     * @return
035     */
036    @Override
037    protected boolean validateAboveTheLineItems(InvoiceItem item, String identifierString, boolean isReceivingDocumentRequiredIndicator) {
038        boolean valid = true;
039        // Currently Quantity is allowed to be NULL on screen;
040        // must be either a positive number or NULL for DB
041        if (ObjectUtils.isNotNull(item.getItemQuantity())) {
042            if (item.getItemQuantity().isNegative()) {
043                // if quantity is negative give an error
044                valid = false;
045                GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_AMOUNT_BELOW_ZERO, ItemFields.INVOICE_QUANTITY, identifierString);
046            }
047            if (!isReceivingDocumentRequiredIndicator) {
048                if (!StringUtils.equalsIgnoreCase(item.getItemTypeCode(), PurapConstants.ItemTypeCodes.ITEM_TYPE_UNORDERED_ITEM_CODE)) {
049                   /* if (item.getPoOutstandingQuantity().isLessThan(item.getItemQuantity())) {
050                        valid = false;
051                        GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_QUANTITY_TOO_MANY, ItemFields.INVOICE_QUANTITY, identifierString, ItemFields.OPEN_QUANTITY);
052                    }*/
053                }
054            }
055
056        }
057        //if (ObjectUtils.isNotNull(item.getExtendedPrice()) && item.getExtendedPrice().isPositive() && ObjectUtils.isNotNull(item.getPoOutstandingQuantity()) && item.getPoOutstandingQuantity().isPositive()) {
058
059        if (ObjectUtils.isNotNull(item.getExtendedPrice())) {
060
061            // here we must require the user to enter some value for quantity if they want a credit amount associated
062            if (ObjectUtils.isNull(item.getItemQuantity()) || item.getItemQuantity().isZero()) {
063                // here we have a user not entering a quantity with an extended amount but the PO has a quantity...require user to
064                // enter a quantity
065                valid = false;
066                GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_QUANTITY_REQUIRED, ItemFields.INVOICE_QUANTITY, identifierString, ItemFields.OPEN_QUANTITY);
067            }
068        }
069
070        // check that non-quantity based items are not trying to pay on a zero encumbrance amount (check only prior to ap approval)
071        if ((ObjectUtils.isNull(item.getInvoice().getPurapDocumentIdentifier())) || (PurapConstants.InvoiceStatuses.APPDOC_IN_PROCESS.equals(item.getInvoice().getApplicationDocumentStatus()))) {
072            if ((item.getItemType().isAmountBasedGeneralLedgerIndicator()) && ((item.getExtendedPrice() != null) && item.getExtendedPrice().isNonZero())) {
073                if (item.getPoOutstandingAmount() == null || item.getPoOutstandingAmount().isZero()) {
074                    valid = false;
075                    GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_AMOUNT_ALREADY_PAID, identifierString);
076                }
077            }
078        }
079       /* if(StringUtils.equalsIgnoreCase(item.getItemTypeCode(),PurapConstants.ItemTypeCodes.ITEM_TYPE_UNORDERED_ITEM_CODE)){
080            if(item.getItemQuantity()==null || item.getItemQuantity().isLessEqual(KualiDecimal.ZERO)){
081                valid=false;
082                GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY,OleSelectConstant.ERROR_ITEM_QUANTITY_REQUIRED , identifierString);
083            }
084        }
085        if(StringUtils.equalsIgnoreCase(item.getItemTypeCode(),PurapConstants.ItemTypeCodes.ITEM_TYPE_UNORDERED_ITEM_CODE) || StringUtils.equalsIgnoreCase(item.getItemTypeCode(),PurapConstants.ItemTypeCodes.ITEM_TYPE_ITEM_CODE) ){
086            if (item.getItemDescription() == null || item.getItemDescription().trim().length() <= 0) {
087                GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, OleSelectConstant.ERROR_REQUIRED, identifierString);
088                valid= false;
089            }
090        }
091        OleInvoiceItem oleItem = (OleInvoiceItem) item;
092        if(oleItem.getItemQuantity()!=null && oleItem.getItemQuantity().isGreaterThan(KualiDecimal.ZERO)){   
093            if(oleItem.getItemNoOfParts()==null ||new KualiDecimal(oleItem.getItemNoOfParts()).isLessEqual(KualiDecimal.ZERO)){
094                GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, OleSelectConstant.ERROR_NO_OF_PARTS_REQUIRED, identifierString);
095                valid=false;
096            }
097        }*/
098        return valid;
099    }
100
101
102}