1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.kuali.kfs.module.cam.document.validation.impl;
20
21 import java.math.BigDecimal;
22
23 import org.kuali.kfs.module.cam.CamsKeyConstants;
24 import org.kuali.kfs.module.cam.CamsPropertyConstants;
25 import org.kuali.kfs.module.cam.businessobject.AssetPaymentAssetDetail;
26 import org.kuali.kfs.module.cam.document.AssetPaymentDocument;
27 import org.kuali.kfs.module.cam.document.service.AssetPaymentService;
28 import org.kuali.kfs.module.cam.util.distribution.AssetDistribution;
29 import org.kuali.kfs.sys.businessobject.AccountingLineBase;
30 import org.kuali.kfs.sys.document.validation.GenericValidation;
31 import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
32 import org.kuali.rice.core.api.util.type.KualiDecimal;
33 import org.kuali.rice.krad.util.GlobalVariables;
34
35
36
37
38 public class AssetPaymentAllocationValidation extends GenericValidation {
39
40 private AssetPaymentService assetPaymentService;
41
42
43
44
45 @Override
46 public boolean validate(AttributedDocumentEvent event) {
47 boolean valid = true;
48
49 AssetPaymentDocument assetPaymentDocument = (AssetPaymentDocument) event.getDocument();
50 AssetDistribution distributor = assetPaymentDocument.getAssetPaymentDistributor();
51
52 if (CamsPropertyConstants.AssetPaymentAllocation.ASSET_DISTRIBUTION_BY_PERCENTAGE_CODE.equals(assetPaymentDocument.getAssetPaymentAllocationTypeCode())) {
53 valid &= validatePercentSum(assetPaymentDocument);
54 } else if (CamsPropertyConstants.AssetPaymentAllocation.ASSET_DISTRIBUTION_BY_AMOUNT_CODE.equals(assetPaymentDocument.getAssetPaymentAllocationTypeCode())) {
55 valid = validateAmountSum(assetPaymentDocument);
56 }
57
58 return valid;
59 }
60
61
62
63
64
65
66
67 protected boolean validateAmountSum(AssetPaymentDocument assetPaymentDocument) {
68 KualiDecimal total = getAllocatedTotal(assetPaymentDocument);
69
70 KualiDecimal sourceTotal = getSourceLinesTotal(assetPaymentDocument);
71
72 if (!total.equals(sourceTotal)) {
73 GlobalVariables.getMessageMap().putErrorForSectionId(CamsPropertyConstants.COMMON_ERROR_SECTION_ID, CamsKeyConstants.AssetPaymentAllocation.ERROR_AMOUNT_NOT_EQUAL);
74 return false;
75 }
76 return true;
77 }
78
79
80
81
82
83 private KualiDecimal getSourceLinesTotal(AssetPaymentDocument assetPaymentDocument) {
84 KualiDecimal sourceTotal = KualiDecimal.ZERO;
85 for (Object sal : assetPaymentDocument.getSourceAccountingLines()) {
86 sourceTotal = sourceTotal.add(((AccountingLineBase) sal).getAmount());
87 }
88 return sourceTotal;
89 }
90
91
92
93
94
95
96 private KualiDecimal getAllocatedTotal(AssetPaymentDocument assetPaymentDocument) {
97 KualiDecimal total = KualiDecimal.ZERO;
98
99 for (AssetPaymentAssetDetail apad : assetPaymentDocument.getAssetPaymentAssetDetail()) {
100
101
102
103 if (assetPaymentDocument.isAllocationFromFPDocuments()) {
104 total = total.add(apad.getAllocatedAmount());
105 } else {
106 total = total.add(apad.getAllocatedUserValue());
107 }
108 }
109 return total;
110 }
111
112
113
114
115
116
117
118 private boolean validatePercentSum(AssetPaymentDocument assetPaymentDocument) {
119 BigDecimal total = new BigDecimal(0d);
120 for (AssetPaymentAssetDetail apad : assetPaymentDocument.getAssetPaymentAssetDetail()) {
121 BigDecimal buggyFix = new BigDecimal("" + apad.getAllocatedUserValuePct().doubleValue());
122 total = total.add(buggyFix);
123 }
124
125 if (total.doubleValue() != 100.00d) {
126 GlobalVariables.getMessageMap().putErrorForSectionId(CamsPropertyConstants.COMMON_ERROR_SECTION_ID, CamsKeyConstants.AssetPaymentAllocation.ERROR_PERCENT_NOT_100);
127 return false;
128 }
129 return true;
130 }
131
132 public AssetPaymentService getAssetPaymentService() {
133 return assetPaymentService;
134 }
135
136 public void setAssetPaymentService(AssetPaymentService assetPaymentService) {
137 this.assetPaymentService = assetPaymentService;
138 }
139
140 }