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 java.util.ArrayList;
019 import java.util.Iterator;
020 import java.util.List;
021
022 import org.kuali.kfs.fp.document.TransferOfFundsDocument;
023 import org.kuali.kfs.fp.document.service.TransferOfFundsService;
024 import org.kuali.kfs.sys.KFSKeyConstants;
025 import org.kuali.kfs.sys.businessobject.AccountingLine;
026 import org.kuali.kfs.sys.document.AccountingDocument;
027 import org.kuali.kfs.sys.document.validation.GenericValidation;
028 import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
029 import org.kuali.rice.kns.util.GlobalVariables;
030 import org.kuali.rice.kns.util.KualiDecimal;
031
032 /**
033 * Transfer of Funds document validation which checks that mandatory and non-mandatory transfer totals are in balance.
034 */
035 public class TransferOfFundsTransferTotalsBalancedValidation extends GenericValidation {
036 private AccountingDocument accountingDocumentForValidation;
037 private TransferOfFundsService transferOfFundsService;
038
039 /**
040 * This method checks the sum of all of the "From" accounting lines with mandatory transfer object codes against the sum of all
041 * of the "To" accounting lines with mandatory transfer object codes. In addition, it does the same, but for accounting lines
042 * with non-mandatory transfer object code. This is to enforce the rule that the document must balance within the object code
043 * object sub-type codes of mandatory transfers and non-mandatory transfers.
044 * @see org.kuali.kfs.sys.document.validation.Validation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent)
045 */
046 public boolean validate(AttributedDocumentEvent event) {
047 TransferOfFundsDocument tofDoc = (TransferOfFundsDocument)accountingDocumentForValidation;
048 List lines = new ArrayList();
049
050 lines.addAll(tofDoc.getSourceAccountingLines());
051 lines.addAll(tofDoc.getTargetAccountingLines());
052
053 // sum the from lines.
054 KualiDecimal mandatoryTransferFromAmount = KualiDecimal.ZERO;
055 KualiDecimal nonMandatoryTransferFromAmount = KualiDecimal.ZERO;
056 KualiDecimal mandatoryTransferToAmount = KualiDecimal.ZERO;
057 KualiDecimal nonMandatoryTransferToAmount = KualiDecimal.ZERO;
058
059 for (Iterator i = lines.iterator(); i.hasNext();) {
060 AccountingLine line = (AccountingLine) i.next();
061 String objectSubTypeCode = line.getObjectCode().getFinancialObjectSubTypeCode();
062
063 if (transferOfFundsService.isNonMandatoryTransfersSubType(objectSubTypeCode)) {
064 if (line.isSourceAccountingLine()) {
065 nonMandatoryTransferFromAmount = nonMandatoryTransferFromAmount.add(line.getAmount());
066 }
067 else {
068 nonMandatoryTransferToAmount = nonMandatoryTransferToAmount.add(line.getAmount());
069 }
070 }
071 else if (transferOfFundsService.isMandatoryTransfersSubType(objectSubTypeCode)) {
072 if (line.isSourceAccountingLine()) {
073 mandatoryTransferFromAmount = mandatoryTransferFromAmount.add(line.getAmount());
074 }
075 else {
076 mandatoryTransferToAmount = mandatoryTransferToAmount.add(line.getAmount());
077 }
078 }
079 }
080
081 // check that the amounts balance across mandatory transfers and non-mandatory transfers
082 boolean isValid = true;
083
084 if (mandatoryTransferFromAmount.compareTo(mandatoryTransferToAmount) != 0) {
085 isValid = false;
086 GlobalVariables.getMessageMap().putError("document.sourceAccountingLines", KFSKeyConstants.ERROR_DOCUMENT_TOF_MANDATORY_TRANSFERS_DO_NOT_BALANCE);
087 }
088
089 if (nonMandatoryTransferFromAmount.compareTo(nonMandatoryTransferToAmount) != 0) {
090 isValid = false;
091 GlobalVariables.getMessageMap().putError("document.sourceAccountingLines", KFSKeyConstants.ERROR_DOCUMENT_TOF_NON_MANDATORY_TRANSFERS_DO_NOT_BALANCE);
092 }
093
094 return isValid;
095 }
096
097 /**
098 * Gets the accountingDocumentForValidation attribute.
099 * @return Returns the accountingDocumentForValidation.
100 */
101 public AccountingDocument getAccountingDocumentForValidation() {
102 return accountingDocumentForValidation;
103 }
104
105 /**
106 * Sets the accountingDocumentForValidation attribute value.
107 * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
108 */
109 public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
110 this.accountingDocumentForValidation = accountingDocumentForValidation;
111 }
112
113 /**
114 * Gets the transferOfFundsService attribute.
115 * @return Returns the transferOfFundsService.
116 */
117 public TransferOfFundsService getTransferOfFundsService() {
118 return transferOfFundsService;
119 }
120
121 /**
122 * Sets the transferOfFundsService attribute value.
123 * @param transferOfFundsService The transferOfFundsService to set.
124 */
125 public void setTransferOfFundsService(TransferOfFundsService transferOfFundsService) {
126 this.transferOfFundsService = transferOfFundsService;
127 }
128 }