1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.module.purap.document.validation.impl;
17
18 import org.kuali.ole.module.purap.PurapConstants;
19 import org.kuali.ole.module.purap.PurapKeyConstants;
20 import org.kuali.ole.module.purap.businessobject.PurApAccountingLine;
21 import org.kuali.ole.module.purap.businessobject.PurApItem;
22 import org.kuali.ole.module.purap.document.PurchasingAccountsPayableDocumentBase;
23 import org.kuali.ole.sys.document.validation.GenericValidation;
24 import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
25 import org.kuali.rice.core.api.util.type.KualiDecimal;
26 import org.kuali.rice.krad.util.GlobalVariables;
27
28 import java.math.BigDecimal;
29
30 public class PurchasingAccountsPayablesItemPreCalculateValidations extends GenericValidation {
31
32 private PurApItem item;
33
34
35
36
37 @Override
38 public boolean validate(AttributedDocumentEvent event) {
39
40 PurchasingAccountsPayableDocumentBase purApDocument = (PurchasingAccountsPayableDocumentBase) event.getDocument();
41 String accountDistributionMethod = purApDocument.getAccountDistributionMethod();
42
43
44
45
46
47 return this.checkTotalPercentAndTotalAmountsEqual(item);
48
49
50
51 }
52
53
54
55
56
57
58
59 public boolean checkTotalPercentAndTotalAmountsEqual(PurApItem item) {
60 boolean valid = true;
61
62 valid &= validateTotalPercent(item, true);
63
64 if (valid) {
65 valid &= validateTotalAmount(item, true);
66 }
67
68 return valid;
69 }
70
71
72
73
74
75
76
77 public boolean checkTotalPercentOrTotalAmountsEqual(PurApItem item) {
78 boolean valid = false;
79
80 boolean validPercent = validateTotalPercent(item, false);
81 if (validPercent) {
82 return true;
83 }
84
85 boolean validAmount = validateTotalAmount(item, false);
86 if (validAmount) {
87 return true;
88 }
89
90 KualiDecimal desiredAmount = (item.getTotalAmount() == null) ? new KualiDecimal(0) : item.getTotalAmount();
91
92 if (!validPercent && !validAmount) {
93 GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNTING_PERCENT_OR_AMOUNT_INVALID, item.getItemIdentifierString(), desiredAmount.toString());
94 } else {
95 if (!validPercent) {
96 GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNTING_TOTAL, item.getItemIdentifierString());
97 } else {
98 if (!validAmount) {
99 GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNTING_TOTAL_AMOUNT, item.getItemIdentifierString(), desiredAmount.toString());
100 }
101 }
102 }
103
104 return valid;
105 }
106
107
108
109
110
111
112
113
114
115 public boolean validateTotalPercent(PurApItem item, boolean writeErrorMessage) {
116 boolean valid = true;
117
118 if (item.getSourceAccountingLines().size() == 0) {
119 return valid;
120 }
121
122
123 BigDecimal totalPercent = BigDecimal.ZERO;
124 BigDecimal desiredPercent = new BigDecimal("100");
125 for (PurApAccountingLine account : item.getSourceAccountingLines()) {
126 if (account.getAccountLinePercent() != null) {
127 totalPercent = totalPercent.add(account.getAccountLinePercent());
128 } else {
129 totalPercent = totalPercent.add(BigDecimal.ZERO);
130 }
131 }
132 if (desiredPercent.compareTo(totalPercent) != 0) {
133 if (writeErrorMessage) {
134 GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNTING_TOTAL, item.getItemIdentifierString());
135 }
136
137 valid = false;
138 }
139
140 return valid;
141 }
142
143
144
145
146
147
148
149
150 public boolean validateTotalAmount(PurApItem item, boolean writeErrorMessage) {
151 boolean valid = true;
152
153 if (item.getSourceAccountingLines().size() == 0) {
154 return valid;
155 }
156
157 if (item.getItemQuantity() == null || item.getItemUnitPrice() == null || item.getTotalAmount().compareTo(KualiDecimal.ZERO) == 0) {
158
159 return valid;
160 }
161
162
163 KualiDecimal totalAmount = KualiDecimal.ZERO;
164
165 KualiDecimal desiredAmount = (item.getTotalAmount() == null) ? new KualiDecimal(0) : item.getTotalAmount();
166 for (PurApAccountingLine account : item.getSourceAccountingLines()) {
167 if (account.getAmount() != null) {
168 totalAmount = totalAmount.add(account.getAmount());
169 } else {
170 totalAmount = totalAmount.add(KualiDecimal.ZERO);
171 }
172 }
173
174 if (desiredAmount.compareTo(totalAmount) != 0) {
175 if (writeErrorMessage) {
176 GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNTING_TOTAL_AMOUNT, item.getItemIdentifierString(), desiredAmount.toString());
177 }
178 valid = false;
179 }
180
181 return valid;
182 }
183
184
185
186
187
188
189
190 public void setItem(PurApItem item) {
191 this.item = item;
192 }
193 }