1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.fp.document.validation.impl;
17
18 import static org.kuali.ole.sys.OLEConstants.ACCOUNTING_LINE_ERRORS;
19 import static org.kuali.ole.sys.OLEKeyConstants.ERROR_DOCUMENT_BALANCE_CONSIDERING_SOURCE_AND_TARGET_AMOUNTS;
20 import static org.kuali.ole.sys.OLEKeyConstants.ERROR_DOCUMENT_PC_TRANSACTION_TOTAL_ACCTING_LINE_TOTAL_NOT_EQUAL;
21
22 import java.util.List;
23
24 import org.kuali.ole.fp.businessobject.ProcurementCardTargetAccountingLine;
25 import org.kuali.ole.fp.businessobject.ProcurementCardTransactionDetail;
26 import org.kuali.ole.fp.document.ProcurementCardDocument;
27 import org.kuali.ole.sys.businessobject.TargetAccountingLine;
28 import org.kuali.ole.sys.document.validation.GenericValidation;
29 import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
30 import org.kuali.rice.core.api.util.type.KualiDecimal;
31 import org.kuali.rice.krad.util.GlobalVariables;
32
33
34
35
36 public class ProcurementCardBalanceValidation extends GenericValidation {
37 private ProcurementCardDocument accountingDocumentForValidation;
38
39
40
41
42
43
44 public boolean validate(AttributedDocumentEvent event) {
45 ProcurementCardDocument pcDocument = (ProcurementCardDocument) getAccountingDocumentForValidation();
46
47 KualiDecimal targetTotal = pcDocument.getTargetTotal();
48 KualiDecimal sourceTotal = pcDocument.getSourceTotal();
49
50 boolean isValid = targetTotal.compareTo(sourceTotal) == 0;
51
52 if (!isValid) {
53 GlobalVariables.getMessageMap().putError(ACCOUNTING_LINE_ERRORS, ERROR_DOCUMENT_BALANCE_CONSIDERING_SOURCE_AND_TARGET_AMOUNTS, new String[] { sourceTotal.toString(), targetTotal.toString() });
54 }
55
56 List<ProcurementCardTransactionDetail> pcTransactionEntries = pcDocument.getTransactionEntries();
57
58 for (ProcurementCardTransactionDetail pcTransactionDetail : pcTransactionEntries) {
59 isValid &= isTransactionBalanceValid(pcTransactionDetail);
60 }
61
62 return isValid;
63 }
64
65
66
67
68
69
70
71
72
73 protected boolean isTransactionBalanceValid(ProcurementCardTransactionDetail pcTransactionDetail) {
74 boolean inBalance = true;
75 KualiDecimal transAmount = pcTransactionDetail.getTransactionTotalAmount();
76 List<ProcurementCardTargetAccountingLine> targetAcctingLines = pcTransactionDetail.getTargetAccountingLines();
77
78 KualiDecimal targetLineTotal = KualiDecimal.ZERO;
79
80 for (TargetAccountingLine targetLine : targetAcctingLines) {
81 targetLineTotal = targetLineTotal.add(targetLine.getAmount());
82 }
83
84
85
86 inBalance = transAmount.abs().equals(targetLineTotal.abs());
87
88 if (!inBalance) {
89 GlobalVariables.getMessageMap().putError(ACCOUNTING_LINE_ERRORS, ERROR_DOCUMENT_PC_TRANSACTION_TOTAL_ACCTING_LINE_TOTAL_NOT_EQUAL, new String[] { transAmount.toString(), targetLineTotal.toString() });
90 }
91
92 return inBalance;
93 }
94
95
96
97
98
99 public ProcurementCardDocument getAccountingDocumentForValidation() {
100 return accountingDocumentForValidation;
101 }
102
103
104
105
106
107 public void setAccountingDocumentForValidation(ProcurementCardDocument accountingDocumentForValidation) {
108 this.accountingDocumentForValidation = accountingDocumentForValidation;
109 }
110
111
112 }