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.validation.PurchasingAccountsPayableItemPreCalculationRule;
23 import org.kuali.rice.kns.rules.DocumentRuleBase;
24 import org.kuali.rice.krad.util.GlobalVariables;
25
26 import java.math.BigDecimal;
27
28 public class PurchasingAccountsPayablesItemPreCalculateDocumentRule extends DocumentRuleBase implements PurchasingAccountsPayableItemPreCalculationRule {
29
30 public boolean checkPercentOrTotalAmountsEqual(PurApItem item) {
31 boolean valid = true;
32
33 valid &= validatePercent(item);
34
35 if (valid) {
36 valid &= validateTotalAmount(item);
37 }
38
39 return valid;
40 }
41
42
43
44
45 public boolean validatePercent(PurApItem item) {
46 boolean valid = true;
47
48
49 BigDecimal totalPercent = BigDecimal.ZERO;
50 BigDecimal desiredPercent = new BigDecimal("100");
51 for (PurApAccountingLine account : item.getSourceAccountingLines()) {
52 if (account.getAccountLinePercent() != null) {
53 totalPercent = totalPercent.add(account.getAccountLinePercent());
54 } else {
55 totalPercent = totalPercent.add(BigDecimal.ZERO);
56 }
57 }
58 if (desiredPercent.compareTo(totalPercent) != 0) {
59 GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNTING_TOTAL, item.getItemIdentifierString());
60 valid = false;
61 }
62
63 return valid;
64 }
65
66
67
68
69
70 public boolean validateTotalAmount(PurApItem item) {
71 boolean valid = true;
72
73
74 BigDecimal totalAmount = BigDecimal.ZERO;
75 BigDecimal desiredAmount =
76 (item.getTotalAmount() == null) ? new BigDecimal(0) : item.getTotalAmount().bigDecimalValue();
77 for (PurApAccountingLine account : item.getSourceAccountingLines()) {
78 if (account.getAmount() != null) {
79 totalAmount = totalAmount.add(account.getAmount().bigDecimalValue());
80 } else {
81 totalAmount = totalAmount.add(BigDecimal.ZERO);
82 }
83 }
84 if (desiredAmount.compareTo(totalAmount) != 0) {
85 GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNTING_TOTAL_AMOUNT, item.getItemIdentifierString(), desiredAmount.toString());
86 valid = false;
87 }
88
89 return valid;
90 }
91 }