View Javadoc
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.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   * Validate Asset Distribution (allocation) sums
37   */
38  public class AssetPaymentAllocationValidation extends GenericValidation {
39  
40  	private AssetPaymentService assetPaymentService;
41  
42  	/**
43  	 * @see org.kuali.kfs.sys.document.validation.Validation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent)
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  	 * Allocation by amount must sum to the accounting line total
63  	 *
64  	 * @param assetPaymentDocument
65  	 * @return true if the amounts are correct
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  	 * @param assetPaymentDocument
81  	 * @return sum of the source accounting lines amounts.
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  	 * @param assetPaymentDocument
94  	 * @return sum of allocated user value.
95  	 */
96  	private KualiDecimal getAllocatedTotal(AssetPaymentDocument assetPaymentDocument) {
97  		KualiDecimal total = KualiDecimal.ZERO;
98  
99  		for (AssetPaymentAssetDetail apad : assetPaymentDocument.getAssetPaymentAssetDetail()) {
100 		    //KFSCNTRB-1209: if the document is created by the system from fp/purap side then
101 		    //use the property allocatedAmount to sum up the amounts of the assets else
102 		    //use allocatedUserValue to sum up the total.
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 	 * Allocation by percentages must total 100%
114 	 *
115 	 * @param assetPaymentDocument
116 	 * @return true if the percentage is correct
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 }