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     */
016    package org.kuali.kfs.fp.document.validation.impl;
017    
018    import org.kuali.kfs.fp.businessobject.BudgetAdjustmentAccountingLine;
019    import org.kuali.kfs.sys.KFSKeyConstants;
020    import org.kuali.kfs.sys.KFSPropertyConstants;
021    import org.kuali.kfs.sys.document.AccountingDocument;
022    import org.kuali.kfs.sys.document.service.DebitDeterminerService;
023    import org.kuali.kfs.sys.document.validation.GenericValidation;
024    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
025    import org.kuali.rice.kns.util.GlobalVariables;
026    import org.kuali.rice.kns.util.KualiDecimal;
027    
028    /**
029     * Validation that checks the amounts on budget adjustment document accounting lines.
030     */
031    public class BudgetAdjustmentAccountingLineAmountValidation extends GenericValidation {
032        private BudgetAdjustmentAccountingLine accountingLineForValidation;
033        private AccountingDocument accountingDocumentForValidation;
034        private DebitDeterminerService debitDeterminerService;
035    
036        /**
037         * Validates the amounts on a budget adjustment accounting line, making sure that either the current adjustment amount or the base adjustment amount are not zero,
038         * and that all given amounts are positive.
039         * @see org.kuali.kfs.sys.document.validation.Validation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent)
040         */
041        public boolean validate(AttributedDocumentEvent event) {
042            boolean amountValid = true;
043    
044            // check amounts both current and base amounts are not zero
045            if (getAccountingLineForValidation().getCurrentBudgetAdjustmentAmount().isZero() && getAccountingLineForValidation().getBaseBudgetAdjustmentAmount().isZero()) {
046                GlobalVariables.getMessageMap().putError(KFSPropertyConstants.BASE_BUDGET_ADJUSTMENT_AMOUNT, KFSKeyConstants.ERROR_BA_AMOUNT_ZERO);
047                amountValid = false;
048            }
049    
050            // if not an error correction, all amounts must be positive
051            if (!debitDeterminerService.isErrorCorrection(getAccountingDocumentForValidation())) {
052                amountValid &= checkAmountSign(getAccountingLineForValidation().getCurrentBudgetAdjustmentAmount(), KFSPropertyConstants.CURRENT_BUDGET_ADJUSTMENT_AMOUNT, "Current");
053                amountValid &= checkAmountSign(getAccountingLineForValidation().getBaseBudgetAdjustmentAmount().kualiDecimalValue(), KFSPropertyConstants.BASE_BUDGET_ADJUSTMENT_AMOUNT, "Base");
054                amountValid &= checkAmountSign(getAccountingLineForValidation().getFinancialDocumentMonth1LineAmount(), KFSPropertyConstants.FINANCIAL_DOCUMENT_MONTH_1_LINE_AMOUNT, "Month 1");
055                amountValid &= checkAmountSign(getAccountingLineForValidation().getFinancialDocumentMonth2LineAmount(), KFSPropertyConstants.FINANCIAL_DOCUMENT_MONTH_2_LINE_AMOUNT, "Month 2");
056                amountValid &= checkAmountSign(getAccountingLineForValidation().getFinancialDocumentMonth3LineAmount(), KFSPropertyConstants.FINANCIAL_DOCUMENT_MONTH_3_LINE_AMOUNT, "Month 3");
057                amountValid &= checkAmountSign(getAccountingLineForValidation().getFinancialDocumentMonth4LineAmount(), KFSPropertyConstants.FINANCIAL_DOCUMENT_MONTH_4_LINE_AMOUNT, "Month 4");
058                amountValid &= checkAmountSign(getAccountingLineForValidation().getFinancialDocumentMonth5LineAmount(), KFSPropertyConstants.FINANCIAL_DOCUMENT_MONTH_5_LINE_AMOUNT, "Month 5");
059                amountValid &= checkAmountSign(getAccountingLineForValidation().getFinancialDocumentMonth6LineAmount(), KFSPropertyConstants.FINANCIAL_DOCUMENT_MONTH_6_LINE_AMOUNT, "Month 6");
060                amountValid &= checkAmountSign(getAccountingLineForValidation().getFinancialDocumentMonth7LineAmount(), KFSPropertyConstants.FINANCIAL_DOCUMENT_MONTH_7_LINE_AMOUNT, "Month 7");
061                amountValid &= checkAmountSign(getAccountingLineForValidation().getFinancialDocumentMonth8LineAmount(), KFSPropertyConstants.FINANCIAL_DOCUMENT_MONTH_8_LINE_AMOUNT, "Month 8");
062                amountValid &= checkAmountSign(getAccountingLineForValidation().getFinancialDocumentMonth8LineAmount(), KFSPropertyConstants.FINANCIAL_DOCUMENT_MONTH_9_LINE_AMOUNT, "Month 9");
063                amountValid &= checkAmountSign(getAccountingLineForValidation().getFinancialDocumentMonth10LineAmount(), KFSPropertyConstants.FINANCIAL_DOCUMENT_MONTH_10_LINE_AMOUNT, "Month 10");
064                amountValid &= checkAmountSign(getAccountingLineForValidation().getFinancialDocumentMonth10LineAmount(), KFSPropertyConstants.FINANCIAL_DOCUMENT_MONTH_11_LINE_AMOUNT, "Month 11");
065                amountValid &= checkAmountSign(getAccountingLineForValidation().getFinancialDocumentMonth12LineAmount(), KFSPropertyConstants.FINANCIAL_DOCUMENT_MONTH_12_LINE_AMOUNT, "Month 12");
066            }
067    
068            return amountValid;
069        }
070    
071        /**
072         * Helper method to check if an amount is negative and add an error if not.
073         * 
074         * @param amount to check
075         * @param propertyName to add error under
076         * @param label for error
077         * @return boolean indicating if the value has the requested sign
078         */
079        protected boolean checkAmountSign(KualiDecimal amount, String propertyName, String label) {
080            boolean correctSign = true;
081    
082            if (amount.isNegative()) {
083                GlobalVariables.getMessageMap().putError(propertyName, KFSKeyConstants.ERROR_BA_AMOUNT_NEGATIVE, label);
084                correctSign = false;
085            }
086    
087            return correctSign;
088        }
089    
090    
091        /**
092         * Gets the accountingLineForValidation attribute. 
093         * @return Returns the accountingLineForValidation.
094         */
095        public BudgetAdjustmentAccountingLine getAccountingLineForValidation() {
096            return accountingLineForValidation;
097        }
098    
099        /**
100         * Sets the accountingLineForValidation attribute value.
101         * @param accountingLineForValidation The accountingLineForValidation to set.
102         */
103        public void setAccountingLineForValidation(BudgetAdjustmentAccountingLine accountingLineForValidation) {
104            this.accountingLineForValidation = accountingLineForValidation;
105        }
106    
107        /**
108         * Gets the accountingDocumentForValidation attribute. 
109         * @return Returns the accountingDocumentForValidation.
110         */
111        public AccountingDocument getAccountingDocumentForValidation() {
112            return accountingDocumentForValidation;
113        }
114    
115        /**
116         * Sets the accountingDocumentForValidation attribute value.
117         * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
118         */
119        public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
120            this.accountingDocumentForValidation = accountingDocumentForValidation;
121        }
122    
123        /**
124         * Gets the debitDeterminerService attribute. 
125         * @return Returns the debitDeterminerService.
126         */
127        public DebitDeterminerService getDebitDeterminerService() {
128            return debitDeterminerService;
129        }
130    
131        /**
132         * Sets the debitDeterminerService attribute value.
133         * @param debitDeterminerService The debitDeterminerService to set.
134         */
135        public void setDebitDeterminerService(DebitDeterminerService debitDeterminerService) {
136            this.debitDeterminerService = debitDeterminerService;
137        }
138    }