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.fp.document.validation.impl;
017
018import static org.kuali.ole.sys.OLEConstants.ACCOUNTING_LINE_ERRORS;
019import static org.kuali.ole.sys.OLEConstants.GL_CREDIT_CODE;
020import static org.kuali.ole.sys.OLEKeyConstants.ERROR_DOCUMENT_BALANCE;
021
022import org.kuali.ole.sys.OLEConstants;
023import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry;
024import org.kuali.ole.sys.document.AccountingDocument;
025import org.kuali.ole.sys.document.validation.GenericValidation;
026import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
027import org.kuali.ole.sys.service.GeneralLedgerPendingEntryService;
028import org.kuali.rice.core.api.util.type.KualiDecimal;
029import org.kuali.rice.krad.exception.ValidationException;
030import org.kuali.rice.krad.util.GlobalVariables;
031
032/**
033 * Validation that checks that the general ledger pending entries associated with an auxiliary voucher
034 * document balance.
035 */
036public class AuxiliaryVoucherGeneralLedgerPendingEntriesBalanceValdiation extends GenericValidation {
037    private AccountingDocument accountingDocumentForValidation;
038    private GeneralLedgerPendingEntryService generalLedgerPendingEntryService;
039
040    /**
041     * Returns true if the explicit, non-DI credit and debit GLPEs derived from the document's accountingLines are in balance
042     * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
043     */
044    public boolean validate(AttributedDocumentEvent event) {
045//      generate GLPEs specifically here so that we can compare debits to credits
046        if (!getGeneralLedgerPendingEntryService().generateGeneralLedgerPendingEntries(getAccountingDocumentForValidation())) {
047            throw new ValidationException("general ledger GLPE generation failed");
048        }
049
050        // now loop through all of the GLPEs and calculate buckets for debits and credits
051        KualiDecimal creditAmount = KualiDecimal.ZERO;
052        KualiDecimal debitAmount = KualiDecimal.ZERO;
053
054        for (GeneralLedgerPendingEntry glpe : getAccountingDocumentForValidation().getGeneralLedgerPendingEntries()) {
055            // make sure we are looking at only the explicit entries that aren't DI types
056            if (!glpe.isTransactionEntryOffsetIndicator() && !glpe.getFinancialDocumentTypeCode().equals(OLEConstants.FinancialDocumentTypeCodes.DISTRIBUTION_OF_INCOME_AND_EXPENSE)) {
057                if (GL_CREDIT_CODE.equals(glpe.getTransactionDebitCreditCode())) {
058                    creditAmount = creditAmount.add(glpe.getTransactionLedgerEntryAmount());
059                }
060                else { // DEBIT
061                    debitAmount = debitAmount.add(glpe.getTransactionLedgerEntryAmount());
062                }
063            }
064        }
065
066        boolean balanced = debitAmount.equals(creditAmount);
067        if (!balanced) {
068            String errorParams[] = { creditAmount.toString(), debitAmount.toString() };
069            GlobalVariables.getMessageMap().putError(ACCOUNTING_LINE_ERRORS, ERROR_DOCUMENT_BALANCE, errorParams);
070        }
071        return balanced;
072    }
073
074    /**
075     * Gets the accountingDocumentForValidation attribute. 
076     * @return Returns the accountingDocumentForValidation.
077     */
078    public AccountingDocument getAccountingDocumentForValidation() {
079        return accountingDocumentForValidation;
080    }
081
082    /**
083     * Sets the accountingDocumentForValidation attribute value.
084     * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
085     */
086    public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
087        this.accountingDocumentForValidation = accountingDocumentForValidation;
088    }
089
090    /**
091     * Gets the generalLedgerPendingEntryService attribute. 
092     * @return Returns the generalLedgerPendingEntryService.
093     */
094    public GeneralLedgerPendingEntryService getGeneralLedgerPendingEntryService() {
095        return generalLedgerPendingEntryService;
096    }
097
098    /**
099     * Sets the generalLedgerPendingEntryService attribute value.
100     * @param generalLedgerPendingEntryService The generalLedgerPendingEntryService to set.
101     */
102    public void setGeneralLedgerPendingEntryService(GeneralLedgerPendingEntryService generalLedgerPendingEntryService) {
103        this.generalLedgerPendingEntryService = generalLedgerPendingEntryService;
104    }
105}