1 /*
2 * The Kuali Financial System, a comprehensive financial management system for higher education.
3 *
4 * Copyright 2005-2014 The Kuali Foundation
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 package org.kuali.kfs.module.ld.document.validation.impl;
20
21 import java.util.Map;
22
23 import org.kuali.kfs.module.ld.LaborKeyConstants;
24 import org.kuali.kfs.module.ld.document.LaborExpenseTransferDocumentBase;
25 import org.kuali.kfs.sys.KFSPropertyConstants;
26 import org.kuali.kfs.sys.document.AccountingDocument;
27 import org.kuali.kfs.sys.document.validation.GenericValidation;
28 import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
29 import org.kuali.rice.core.api.util.type.KualiDecimal;
30 import org.kuali.rice.krad.document.Document;
31 import org.kuali.rice.krad.util.GlobalVariables;
32
33 /**
34 * target accounting lines must have the same amounts as source accounting lines for each object code in the document
35 *
36 * @param document the given document
37 * @return true if target accounting lines have the same amounts as source accounting lines for each object code; otherwise, false
38 */
39 public class LaborExpenseTransferValidAmountTransferredByObjectCodeValidation extends GenericValidation {
40 private Document documentForValidation;
41
42 /**
43 * Validates before the document routes
44 * @see org.kuali.kfs.validation.Validation#validate(java.lang.Object[])
45 */
46 public boolean validate(AttributedDocumentEvent event) {
47 boolean result = true;
48
49 Document documentForValidation = getDocumentForValidation();
50
51 LaborExpenseTransferDocumentBase expenseTransferDocument = (LaborExpenseTransferDocumentBase) documentForValidation;
52
53 // check to ensure totals of accounting lines in source and target sections match
54 if (!isValidAmountTransferredByObjectCode(expenseTransferDocument)) {
55 GlobalVariables.getMessageMap().putError(KFSPropertyConstants.TARGET_ACCOUNTING_LINES, LaborKeyConstants.ERROR_TRANSFER_AMOUNT_NOT_BALANCED_BY_OBJECT);
56 return false;
57 }
58
59 return result;
60 }
61
62 /**
63 * Determine whether target accounting lines have the same amounts as source accounting lines for each object code
64 *
65 * @param accountingDocument the given accounting document
66 * @return true if target accounting lines have the same amounts as source accounting lines for each object code; otherwise,
67 * false
68 */
69 protected boolean isValidAmountTransferredByObjectCode(AccountingDocument accountingDocument) {
70 LaborExpenseTransferDocumentBase expenseTransferDocument = (LaborExpenseTransferDocumentBase) accountingDocument;
71
72 boolean isValid = true;
73
74 Map<String, KualiDecimal> unbalancedObjectCodes = expenseTransferDocument.getUnbalancedObjectCodes();
75 if (!unbalancedObjectCodes.isEmpty()) {
76 isValid = false;
77 }
78
79 return isValid;
80 }
81
82 /**
83 * Gets the documentForValidation attribute.
84 * @return Returns the documentForValidation.
85 */
86 public Document getDocumentForValidation() {
87 return documentForValidation;
88 }
89
90 /**
91 * Sets the accountingDocumentForValidation attribute value.
92 * @param documentForValidation The documentForValidation to set.
93 */
94 public void setDocumentForValidation(Document documentForValidation) {
95 this.documentForValidation = documentForValidation;
96 }
97 }