001/*
002 * Copyright 2013 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 1.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/ecl1.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.kuali.ole.module.purap.PurapConstants;
019import org.kuali.ole.module.purap.PurapKeyConstants;
020import org.kuali.ole.module.purap.businessobject.PurApAccountingLine;
021import org.kuali.ole.module.purap.businessobject.PurApItem;
022import org.kuali.ole.select.businessobject.OleCreditMemoItem;
023import org.kuali.ole.select.document.OleVendorCreditMemoDocument;
024import org.kuali.ole.sys.document.validation.GenericValidation;
025import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
026import org.kuali.rice.core.api.util.type.KualiDecimal;
027import org.kuali.rice.krad.util.GlobalVariables;
028
029import java.math.BigDecimal;
030
031public class OleVendorCreditMemoAccountTotalValidation extends GenericValidation {
032
033    private PurApItem itemForValidation;
034
035    /**
036     * Verifies account percent. If the total percent does not equal 100, the validation fails.
037     */
038    @Override
039    public boolean validate(AttributedDocumentEvent event) {
040        boolean valid = true;
041
042        // validate that the amount total
043        OleVendorCreditMemoDocument cmDocument = (OleVendorCreditMemoDocument) event.getDocument();
044        BigDecimal totalAmount = BigDecimal.ZERO;
045        BigDecimal desiredAmount = (itemForValidation.getTotalAmount() == null) ? new BigDecimal(0) : itemForValidation
046                .getTotalAmount().bigDecimalValue();
047
048        KualiDecimal prorateSurcharge = KualiDecimal.ZERO;
049
050        OleCreditMemoItem crdtItem = (OleCreditMemoItem) itemForValidation;
051        if (crdtItem.getItemType().isQuantityBasedGeneralLedgerIndicator() && crdtItem.getExtendedPrice() != null
052                && crdtItem.getExtendedPrice().compareTo(KualiDecimal.ZERO) != 0) {
053            if (crdtItem.getItemSurcharge() != null
054                    && (crdtItem.getItemTypeCode().equals("ITEM") || crdtItem.getItemTypeCode()
055                    .equalsIgnoreCase("UNOR"))) {
056                prorateSurcharge = new KualiDecimal(crdtItem.getItemSurcharge()).multiply(crdtItem.getItemQuantity());
057            }
058            if (prorateSurcharge.isNegative()) {
059                prorateSurcharge = prorateSurcharge.negated();
060                desiredAmount = desiredAmount.add(new BigDecimal(prorateSurcharge.toString()));
061            } else {
062                desiredAmount = desiredAmount.subtract(new BigDecimal(prorateSurcharge.toString()));
063            }
064
065        }
066        for (PurApAccountingLine account : itemForValidation.getSourceAccountingLines()) {
067            if (account.getAmount() != null) {
068                totalAmount = totalAmount.add(account.getAmount().bigDecimalValue());
069            } else {
070                totalAmount = totalAmount.add(BigDecimal.ZERO);
071            }
072        }
073        if (desiredAmount.compareTo(totalAmount) != 0 && cmDocument.getInvoiceIdentifier() == null) {
074            GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY,
075                    PurapKeyConstants.ERROR_ITEM_ACCOUNTING_TOTAL_AMOUNT, itemForValidation.getItemIdentifierString(),
076                    desiredAmount.toString());
077            valid = false;
078        }
079
080
081        return valid;
082    }
083
084
085    public PurApItem getItemForValidation() {
086        return itemForValidation;
087    }
088
089
090    public void setItemForValidation(PurApItem itemForValidation) {
091        this.itemForValidation = itemForValidation;
092    }
093
094}