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.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.module.purap.document.validation.PurchasingAccountsPayableItemPreCalculationRule;
023import org.kuali.rice.kns.rules.DocumentRuleBase;
024import org.kuali.rice.krad.util.GlobalVariables;
025
026import java.math.BigDecimal;
027
028public class PurchasingAccountsPayablesItemPreCalculateDocumentRule extends DocumentRuleBase implements PurchasingAccountsPayableItemPreCalculationRule {
029
030    public boolean checkPercentOrTotalAmountsEqual(PurApItem item) {
031        boolean valid = true;
032
033        valid &= validatePercent(item);
034
035        if (valid) {
036            valid &= validateTotalAmount(item);
037        }
038
039        return valid;
040    }
041
042    /**
043     * Verifies account percent. If the total percent does not equal 100, the validation fails.
044     */
045    public boolean validatePercent(PurApItem item) {
046        boolean valid = true;
047
048        // validate that the percents total 100 for each item
049        BigDecimal totalPercent = BigDecimal.ZERO;
050        BigDecimal desiredPercent = new BigDecimal("100");
051        for (PurApAccountingLine account : item.getSourceAccountingLines()) {
052            if (account.getAccountLinePercent() != null) {
053                totalPercent = totalPercent.add(account.getAccountLinePercent());
054            } else {
055                totalPercent = totalPercent.add(BigDecimal.ZERO);
056            }
057        }
058        if (desiredPercent.compareTo(totalPercent) != 0) {
059            GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNTING_TOTAL, item.getItemIdentifierString());
060            valid = false;
061        }
062
063        return valid;
064    }
065
066    /**
067     * Verifies account total. If the total does not equal item total,
068     * the validation fails.
069     */
070    public boolean validateTotalAmount(PurApItem item) {
071        boolean valid = true;
072
073        // validate that the amount total
074        BigDecimal totalAmount = BigDecimal.ZERO;
075        BigDecimal desiredAmount =
076                (item.getTotalAmount() == null) ? new BigDecimal(0) : item.getTotalAmount().bigDecimalValue();
077        for (PurApAccountingLine account : item.getSourceAccountingLines()) {
078            if (account.getAmount() != null) {
079                totalAmount = totalAmount.add(account.getAmount().bigDecimalValue());
080            } else {
081                totalAmount = totalAmount.add(BigDecimal.ZERO);
082            }
083        }
084        if (desiredAmount.compareTo(totalAmount) != 0) {
085            GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNTING_TOTAL_AMOUNT, item.getItemIdentifierString(), desiredAmount.toString());
086            valid = false;
087        }
088
089        return valid;
090    }
091}