View Javadoc
1   /*
2    * Copyright 2006 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.fp.document.web.struts;
17  
18  import java.math.BigDecimal;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.HashMap;
22  import java.util.HashSet;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.apache.commons.lang.StringUtils;
31  import org.apache.struts.action.ActionForm;
32  import org.apache.struts.action.ActionForward;
33  import org.apache.struts.action.ActionMapping;
34  import org.kuali.ole.fp.businessobject.CapitalAccountingLines;
35  import org.kuali.ole.fp.businessobject.CapitalAssetAccountsGroupDetails;
36  import org.kuali.ole.fp.businessobject.CapitalAssetInformation;
37  import org.kuali.ole.fp.businessobject.CapitalAssetInformationDetail;
38  import org.kuali.ole.fp.document.CapitalAccountingLinesDocumentBase;
39  import org.kuali.ole.fp.document.CapitalAssetEditable;
40  import org.kuali.ole.fp.document.CapitalAssetInformationDocumentBase;
41  import org.kuali.ole.integration.cam.businessobject.Asset;
42  import org.kuali.ole.sys.OLEConstants;
43  import org.kuali.ole.sys.OLEKeyConstants;
44  import org.kuali.ole.sys.OLEPropertyConstants;
45  import org.kuali.ole.sys.businessobject.AccountingLine;
46  import org.kuali.ole.sys.context.SpringContext;
47  import org.kuali.ole.sys.document.AccountingDocument;
48  import org.kuali.ole.sys.service.SegmentedLookupResultsService;
49  import org.kuali.ole.sys.web.struts.KualiAccountingDocumentActionBase;
50  import org.kuali.ole.sys.web.struts.KualiAccountingDocumentFormBase;
51  import org.kuali.rice.core.api.util.type.KualiDecimal;
52  import org.kuali.rice.kns.util.WebUtils;
53  import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
54  import org.kuali.rice.kns.web.struts.form.KualiForm;
55  import org.kuali.rice.krad.bo.PersistableBusinessObject;
56  import org.kuali.rice.krad.util.GlobalVariables;
57  import org.kuali.rice.krad.util.ObjectUtils;
58  
59  /**
60   * This is the action class for the CapitalAssetInformationActionBase.
61   */
62  public abstract class CapitalAssetInformationActionBase extends KualiAccountingDocumentActionBase {
63      protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CapitalAssetInformationActionBase.class);
64  
65      /**
66       * Multi-value asset lookup is implemented through the integration package by module's service
67       * to gather the results. The results are processed for any capital accounting lines where
68       * the line is marked for selection.  After the capital assets are populated with the
69       * selected asset numbers, the system control amount is redistributed equally among the assets
70       * when the distribution method is "distribute cost equally".
71       *
72       * @see org.kuali.rice.kns.web.struts.action.KualiAction#refresh(org.apache.struts.action.ActionMapping,
73       *      org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
74       */
75      @Override
76      public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
77          super.refresh(mapping, form, request, response);
78  
79          //process the multiple value lookup data
80          CapitalAssetInformationFormBase capitalAssetInformationFormBase = (CapitalAssetInformationFormBase) form;
81  
82          Collection<PersistableBusinessObject> rawValues = null;
83          Map<String, Set<String>> segmentedSelection = new HashMap<String, Set<String>>();
84  
85          // If multiple asset lookup was used to select the assets, then....
86          if (StringUtils.equals(OLEConstants.MULTIPLE_VALUE, capitalAssetInformationFormBase.getRefreshCaller())) {
87              String lookupResultsSequenceNumber = capitalAssetInformationFormBase.getLookupResultsSequenceNumber();
88  
89              if (StringUtils.isNotBlank(lookupResultsSequenceNumber)) {
90                  // actually returning from a multiple value lookup
91                  Set<String> selectedIds = SpringContext.getBean(SegmentedLookupResultsService.class).retrieveSetOfSelectedObjectIds(lookupResultsSequenceNumber, GlobalVariables.getUserSession().getPerson().getPrincipalId());
92                  for (String selectedId : selectedIds) {
93                      String selectedObjId = StringUtils.substringBefore(selectedId, ".");
94                      if (!segmentedSelection.containsKey(selectedObjId)) {
95                          segmentedSelection.put(selectedObjId, new HashSet<String>());
96                      }
97                  }
98                  // Retrieving selected data from table.
99                  if (LOG.isDebugEnabled()) {
100                     LOG.debug("Asking segmentation service for object ids " + segmentedSelection.keySet());
101                 }
102                 rawValues = SpringContext.getBean(SegmentedLookupResultsService.class).retrieveSelectedResultBOs(lookupResultsSequenceNumber, segmentedSelection.keySet(), Asset.class, GlobalVariables.getUserSession().getPerson().getPrincipalId());
103             }
104 
105             if (rawValues == null || rawValues.size() == 0) {
106                 //redistribute capital asset amount to its group accounting lines on refresh
107                 DistributeCapitalAssetAmountToGroupAccountingLines((KualiAccountingDocumentFormBase) form);
108 
109                 return mapping.findForward(OLEConstants.MAPPING_BASIC);
110             }
111 
112             KualiAccountingDocumentFormBase kualiAccountingDocumentFormBase = (KualiAccountingDocumentFormBase) form;
113             CapitalAccountingLinesFormBase calfb = (CapitalAccountingLinesFormBase) form;
114             CapitalAccountingLinesDocumentBase caldb = (CapitalAccountingLinesDocumentBase) calfb.getFinancialDocument();
115             List<CapitalAccountingLines> capitalAccountingLines = caldb.getCapitalAccountingLines();
116             List<CapitalAssetInformation> capitalAssetInformation = this.getCurrentCapitalAssetInformationObject(kualiAccountingDocumentFormBase);
117 
118             List<CapitalAccountingLines> selectedCapitalAccountingLines = new ArrayList<CapitalAccountingLines>();
119 
120             for (CapitalAccountingLines capitalAccountingLine : capitalAccountingLines) {
121                 if (capitalAccountingLine.isSelectLine()  && !capitalAccountingLine.isAmountDistributed()) {
122                     selectedCapitalAccountingLines.add(capitalAccountingLine);
123                 }
124             }
125 
126             //process the data and create assets only for those accounting lines
127             //where capital accounting line is "selected" and its amount is greater than already allocated.
128             if (rawValues != null) {
129                         for (PersistableBusinessObject bo : rawValues) {
130                             Asset asset = (Asset) bo;
131 
132                          //   boolean addIt = true;
133                     boolean addIt = modifyAssetAlreadyExists(capitalAssetInformation, asset.getCapitalAssetNumber());
134 
135                             // If it doesn't already exist in the list add it.
136                             if (addIt) {
137                         createNewModifyCapitalAsset(selectedCapitalAccountingLines, capitalAssetInformation, calfb.getDocument().getDocumentNumber(), OLEConstants.CapitalAssets.CAPITAL_ASSET_MODIFY_ACTION_INDICATOR, getNextCapitalAssetLineNumber(kualiAccountingDocumentFormBase), asset.getCapitalAssetNumber());
138                     }
139                 }
140 
141                 checkCapitalAccountingLinesSelected(calfb);
142 
143                 // remove the blank capital asset modify records now...
144                 removeEmptyCapitalAssetModify(capitalAssetInformation);
145 
146                 //now redistribute the amount for all assets if needed....
147                 redistributeModifyCapitalAssetAmount(mapping, form, request, response);
148             }
149         }
150 
151         //redistribute capital asset amount to its group accounting lines on refresh
152         DistributeCapitalAssetAmountToGroupAccountingLines((KualiAccountingDocumentFormBase) form);
153 
154         return mapping.findForward(OLEConstants.MAPPING_BASIC);
155 
156     }
157 
158     /**
159      * Remove if and any blank capital asset modify lines.
160      * @param capitalAssetInformation
161      */
162     protected void removeEmptyCapitalAssetModify(List<CapitalAssetInformation> capitalAssetInformation) {
163         List<CapitalAssetInformation> removeCapitalAssetModify = new ArrayList<CapitalAssetInformation>();
164 
165         for (CapitalAssetInformation capitalAssetRecord : capitalAssetInformation) {
166             if (ObjectUtils.isNull(capitalAssetRecord.getCapitalAssetNumber()) && OLEConstants.CapitalAssets.CAPITAL_ASSET_MODIFY_ACTION_INDICATOR.equalsIgnoreCase(capitalAssetRecord.getCapitalAssetActionIndicator())) {
167                 removeCapitalAssetModify.add(capitalAssetRecord);
168             }
169         }
170 
171         if (!removeCapitalAssetModify.isEmpty()) {
172             capitalAssetInformation.removeAll(removeCapitalAssetModify);
173         }
174     }
175 
176     /**
177      * sums the capital assets amount distributed so far for a given capital accounting line
178      *
179      * @param currentCapitalAssetInformation
180      * @param existingCapitalAsset
181      * @return capitalAssetsAmount amount that has been distributed for the specific capital accounting line
182      */
183     protected KualiDecimal getCapitalAssetsAmountAllocated(List<CapitalAssetInformation> currentCapitalAssetInformation, CapitalAccountingLines capitalAccountingLine) {
184         //check the capital assets records totals
185         KualiDecimal capitalAssetsAmount = KualiDecimal.ZERO;
186 
187 
188         for (CapitalAssetInformation capitalAsset : currentCapitalAssetInformation) {
189             List<CapitalAssetAccountsGroupDetails> groupAccountLines = capitalAsset.getCapitalAssetAccountsGroupDetails();
190             for (CapitalAssetAccountsGroupDetails groupAccountLine : groupAccountLines) {
191                 if (groupAccountLine.getCapitalAssetLineNumber().compareTo(capitalAsset.getCapitalAssetLineNumber()) == 0 &&
192                         groupAccountLine.getSequenceNumber().compareTo(capitalAccountingLine.getSequenceNumber()) == 0 &&
193                         groupAccountLine.getFinancialDocumentLineTypeCode().equals(OLEConstants.SOURCE.equals(capitalAccountingLine.getLineType()) ? OLEConstants.SOURCE_ACCT_LINE_TYPE_CODE : OLEConstants.TARGET_ACCT_LINE_TYPE_CODE) &&
194                         groupAccountLine.getChartOfAccountsCode().equals(capitalAccountingLine.getChartOfAccountsCode()) &&
195                         groupAccountLine.getAccountNumber().equals(capitalAccountingLine.getAccountNumber()) &&
196                         groupAccountLine.getFinancialObjectCode().equals(capitalAccountingLine.getFinancialObjectCode())) {
197                     capitalAssetsAmount = capitalAssetsAmount.add(groupAccountLine.getAmount());
198                 }
199              }
200          }
201 
202         return capitalAssetsAmount;
203     }
204 
205     /**
206      * checks the capital asset information list for the specific capital asset number
207      * that was returned as part of the multi-value lookup.
208      *
209      * @param capitalAssetInformation
210      * @param capitalAssetNumber
211      * @return true if asset does not exist in the list else return false
212      */
213     protected boolean modifyAssetAlreadyExists(List<CapitalAssetInformation> capitalAssetInformation, Long capitalAssetNumber) {
214        boolean addIt = true;
215        KualiDecimal capitalAssetCreatedAmount = KualiDecimal.ZERO;
216 
217        for (CapitalAssetInformation capitalAsset : capitalAssetInformation) {
218            if (OLEConstants.CapitalAssets.CAPITAL_ASSET_MODIFY_ACTION_INDICATOR.equals(capitalAsset.getCapitalAssetActionIndicator()) &&
219                    ObjectUtils.isNotNull(capitalAsset.getCapitalAssetNumber()) &&
220                    capitalAsset.getCapitalAssetNumber().compareTo(capitalAssetNumber) == 0) {
221                 addIt = false;
222                 break;
223             }
224         }
225 
226         return addIt;
227     }
228 
229     /**
230      * checks if the selected capital accounting lines have a capital asset created by checking the
231      * accounts associated with the capital asset information.
232      *
233      * @param capitalAccountingLine
234      * @param capitalAsset
235      * @return true if capital accounting line has a capital asset else return false.
236      */
237     protected boolean capitalAssetExists(List<CapitalAccountingLines> capitalAccountingLines, CapitalAssetInformation capitalAsset, String actionTypeCode) {
238         boolean exists = true;
239 
240         List<CapitalAssetAccountsGroupDetails> groupAccountLines = capitalAsset.getCapitalAssetAccountsGroupDetails();
241 
242         for(CapitalAccountingLines capitalAccountLine : capitalAccountingLines) {
243             for (CapitalAssetAccountsGroupDetails groupAccountLine : groupAccountLines) {
244                 if (groupAccountLine.getCapitalAssetLineNumber().compareTo(capitalAsset.getCapitalAssetLineNumber()) == 0 &&
245                         groupAccountLine.getSequenceNumber().compareTo(capitalAccountLine.getSequenceNumber()) == 0 &&
246                         groupAccountLine.getFinancialDocumentLineTypeCode().equals(OLEConstants.SOURCE.equals(capitalAccountLine.getLineType()) ? OLEConstants.SOURCE_ACCT_LINE_TYPE_CODE : OLEConstants.TARGET_ACCT_LINE_TYPE_CODE) &&
247                         groupAccountLine.getChartOfAccountsCode().equals(capitalAccountLine.getChartOfAccountsCode()) &&
248                         groupAccountLine.getAccountNumber().equals(capitalAccountLine.getAccountNumber()) &&
249                         groupAccountLine.getFinancialObjectCode().equals(capitalAccountLine.getFinancialObjectCode())) {
250                      return exists;
251                 }
252             }
253         }
254 
255         return false;
256     }
257 
258     /**
259      *
260      * @param form
261      */
262     protected void redistributeCostEquallyForModifiedAssets(ActionForm form) {
263         KualiDecimal remainingAmountToDistribute = KualiDecimal.ZERO;
264 
265         CapitalAccountingLinesFormBase calfb = (CapitalAccountingLinesFormBase) form;
266         CapitalAccountingLinesDocumentBase caldb = (CapitalAccountingLinesDocumentBase) calfb.getFinancialDocument();
267 
268         String distributionCode = calfb.getCapitalAccountingLine().getDistributionCode();
269         List<CapitalAccountingLines> capitalAccountingLines = caldb.getCapitalAccountingLines();
270 
271         List<CapitalAccountingLines> selectedCapitalAccountingLines = new ArrayList<CapitalAccountingLines>();
272 
273         for (CapitalAccountingLines capitalAccountingLine : capitalAccountingLines) {
274             if (capitalAccountingLine.isSelectLine()  && !capitalAccountingLine.isAmountDistributed()) {
275                 selectedCapitalAccountingLines.add(capitalAccountingLine);
276                 remainingAmountToDistribute = remainingAmountToDistribute.add(capitalAccountingLine.getAmount());
277             }
278         }
279 
280             KualiAccountingDocumentFormBase kualiAccountingDocumentFormBase = (KualiAccountingDocumentFormBase) form;
281             List<CapitalAssetInformation> capitalAssetInformation = this.getCurrentCapitalAssetInformationObject(kualiAccountingDocumentFormBase);
282         redistributeAmountsForAccountingsLineForModifyAssets(selectedCapitalAccountingLines, capitalAssetInformation, remainingAmountToDistribute);
283 
284         //now process any capital assets that has distribution set to "by amount"
285         redistributeAmountsForAccountingsLineForModifyAssetsByAmounts(selectedCapitalAccountingLines, capitalAssetInformation, remainingAmountToDistribute);
286     }
287 
288     /**
289      *
290      * @param accountingLine
291      * @param capitalAssetInformation
292      * @param remainingAmountToDistribute
293      */
294     protected void redistributeAmountsForAccountingsLineForModifyAssets(List<CapitalAccountingLines> selectedCapitalAccountingLines, List<CapitalAssetInformation> capitalAssetInformation, KualiDecimal remainingAmountToDistribute) {
295         //get the total capital assets quantity
296         int totalQuantity = getNumberOfModifiedAssetsExist(selectedCapitalAccountingLines, capitalAssetInformation);
297         if (totalQuantity > 0) {
298             KualiDecimal equalModifyAssetAmount = remainingAmountToDistribute.divide(new KualiDecimal(totalQuantity), true);
299 
300         int lastAssetIndex = 0;
301         CapitalAssetInformation lastCapitalAsset = new CapitalAssetInformation();
302 
303         if (equalModifyAssetAmount.compareTo(KualiDecimal.ZERO) != 0) {
304             for (CapitalAssetInformation capitalAsset : capitalAssetInformation) {
305                 if (OLEConstants.CapitalAssets.CAPITAL_ASSET_MODIFY_ACTION_INDICATOR.equals(capitalAsset.getCapitalAssetActionIndicator()) &&
306                             (ObjectUtils.isNotNull(capitalAsset.getCapitalAssetNumber())) &&
307                             (OLEConstants.CapitalAssets.DISTRIBUTE_COST_EQUALLY_CODE.equalsIgnoreCase(capitalAsset.getDistributionAmountCode()))) {
308                         if (capitalAssetExists(selectedCapitalAccountingLines, capitalAsset, OLEConstants.CapitalAssets.CAPITAL_ASSET_MODIFY_ACTION_INDICATOR)) {
309                     capitalAsset.setCapitalAssetQuantity(1);
310                             redistributeEqualAmounts(selectedCapitalAccountingLines, capitalAsset, equalModifyAssetAmount, totalQuantity);
311                     lastAssetIndex++;
312                     //get a reference to the last capital create asset to fix any variances...
313                     lastCapitalAsset = capitalAsset;
314                 }
315             }
316         }
317             }
318 
319         //apply any variance left to the last
320         KualiDecimal varianceForAssets = remainingAmountToDistribute.subtract(equalModifyAssetAmount.multiply(new KualiDecimal(lastAssetIndex)));
321         if (varianceForAssets.isNonZero()) {
322                 lastCapitalAsset.setCapitalAssetLineAmount(lastCapitalAsset.getCapitalAssetLineAmount().add(varianceForAssets));
323                 redistributeEqualAmountsOnLastCapitalAsset(selectedCapitalAccountingLines, lastCapitalAsset, capitalAssetInformation, OLEConstants.CapitalAssets.CAPITAL_ASSET_MODIFY_ACTION_INDICATOR);
324             }
325         }
326     }
327 
328     /**
329      * for modified assets the amount is distributed
330      * @param selectedCapitalAccountingLines
331      * @param capitalAssetInformation
332      */
333     protected void redistributeAmountsForAccountingsLineForModifyAssetsByAmounts(List<CapitalAccountingLines> selectedCapitalAccountingLines, List<CapitalAssetInformation> capitalAssetInformation, KualiDecimal remainingAmountToDistribute) {
334         KualiDecimal appliedAccountingLinesTotal = KualiDecimal.ZERO;
335         CapitalAssetAccountsGroupDetails lastAccountingLine = new CapitalAssetAccountsGroupDetails();
336 
337         for (CapitalAccountingLines capitalAccountLine : selectedCapitalAccountingLines) {
338             for (CapitalAssetInformation capitalAsset : capitalAssetInformation) {
339                 if (OLEConstants.CapitalAssets.DISTRIBUTE_COST_BY_INDIVIDUAL_ASSET_AMOUNT_CODE.equalsIgnoreCase(capitalAsset.getDistributionAmountCode())) {
340                     if (capitalAsset.getCapitalAssetLineAmount().compareTo(getAccountingLinesTotalAmount(capitalAsset)) != 0) {
341                         List<CapitalAssetAccountsGroupDetails> groupAccountLines = capitalAsset.getCapitalAssetAccountsGroupDetails();
342                         for (CapitalAssetAccountsGroupDetails groupAccountLine : groupAccountLines) {
343                             if (groupAccountLine.getCapitalAssetLineNumber().compareTo(capitalAsset.getCapitalAssetLineNumber()) == 0 &&
344                                     groupAccountLine.getSequenceNumber().compareTo(capitalAccountLine.getSequenceNumber()) == 0 &&
345                                     groupAccountLine.getFinancialDocumentLineTypeCode().equals(OLEConstants.SOURCE.equals(capitalAccountLine.getLineType()) ? OLEConstants.SOURCE_ACCT_LINE_TYPE_CODE : OLEConstants.TARGET_ACCT_LINE_TYPE_CODE) &&
346                                     groupAccountLine.getChartOfAccountsCode().equals(capitalAccountLine.getChartOfAccountsCode()) &&
347                                     groupAccountLine.getAccountNumber().equals(capitalAccountLine.getAccountNumber()) &&
348                                     groupAccountLine.getFinancialObjectCode().equals(capitalAccountLine.getFinancialObjectCode())) {
349                                     //found the accounting line
350                                 groupAccountLine.setAmount((capitalAsset.getCapitalAssetLineAmount().multiply(capitalAccountLine.getAccountLinePercent())).divide(new KualiDecimal(100)));
351                             }
352                         }
353                     }
354                 }
355             }
356         }
357     }
358 
359     /**
360      * redistributes the amounts to the capital asset and its related group accounting lines.
361      * Adjusts any variance to the last capital asset accounting line.
362      *
363      * @param selectedCapitalAccountingLines
364      * @param capitalAsset
365      * @param amount
366      * @param totalQuantity
367      */
368     protected void redistributeEqualAmounts(List<CapitalAccountingLines> selectedCapitalAccountingLines, CapitalAssetInformation capitalAsset, KualiDecimal amount, int totalQuantity) {
369         int assetQuantity = 0;
370         KualiDecimal totalCapitalAssetQuantity = new KualiDecimal(totalQuantity);
371 
372         if (ObjectUtils.isNotNull(capitalAsset.getCapitalAssetQuantity())) {
373             assetQuantity = capitalAsset.getCapitalAssetQuantity();
374         }
375 
376         capitalAsset.setCapitalAssetLineAmount(capitalAsset.getCapitalAssetLineAmount().add(amount.multiply(new KualiDecimal(assetQuantity))));
377         int lastAccountIndex = 0;
378         KualiDecimal appliedAccountingLinesTotal = KualiDecimal.ZERO;
379 
380         CapitalAssetAccountsGroupDetails lastLine = new CapitalAssetAccountsGroupDetails();
381 
382         List<CapitalAssetAccountsGroupDetails> groupAccountLines = capitalAsset.getCapitalAssetAccountsGroupDetails();
383 
384         for (CapitalAssetAccountsGroupDetails groupAccountLine : groupAccountLines) {
385             KualiDecimal capitalAccountingLineAmount = getCapitalAssetAccountLineAmount(selectedCapitalAccountingLines, groupAccountLine, capitalAsset);
386             KualiDecimal calculatedLineAmount = capitalAccountingLineAmount.divide(totalCapitalAssetQuantity, true);
387             groupAccountLine.setAmount(calculatedLineAmount.multiply(new KualiDecimal(assetQuantity)));
388             appliedAccountingLinesTotal = appliedAccountingLinesTotal.add(groupAccountLine.getAmount());
389 
390             lastAccountIndex++;
391             lastLine = groupAccountLine;
392         }
393 
394         //apply any variance left to the last
395         KualiDecimal varianceForLines = capitalAsset.getCapitalAssetLineAmount().subtract(appliedAccountingLinesTotal);
396         if (varianceForLines.isNonZero()) {
397             lastLine.setAmount(lastLine.getAmount().add(varianceForLines));
398         }
399     }
400 
401     /**
402      * redistributes the amounts to the capital asset and its related group accounting lines.
403      * Adjusts any variance to the last capital asset accounting line.
404      *
405      * @param selectedCapitalAccountingLines
406      * @param capitalAsset
407      * @param capitalAssetInformation
408      * @param actionTypeCode
409      */
410     protected void redistributeEqualAmountsOnLastCapitalAsset(List<CapitalAccountingLines> selectedCapitalAccountingLines, CapitalAssetInformation lastCapitalAsset, List<CapitalAssetInformation> capitalAssetInformation, String actionTypeCode) {
411         for (CapitalAccountingLines capitalAccountingLine : selectedCapitalAccountingLines ) {
412             KualiDecimal lineAmount = capitalAccountingLine.getAmount();
413             KualiDecimal distributedAmount = getAccountingLinesDistributedAmount(capitalAccountingLine, capitalAssetInformation, actionTypeCode);
414             KualiDecimal difference = lineAmount.subtract(distributedAmount);
415             if (!difference.isZero()) {
416                 adjustAccountingLineAmountOnLastCapitalAsset(capitalAccountingLine, lastCapitalAsset, difference);
417             }
418         }
419             }
420 
421     /**
422      * Gets the amount on the capital assets line for the selected capital accounting line by
423      * finding the group accounting line.  When group accounting line is found in the selected
424      * capital accounting lines, the amount from that capital accounting line is returned.
425      * @param selectedCapitalAccountingLines
426      * @param groupAccountLine
427      * @return lineAmount
428      */
429     protected KualiDecimal getCapitalAssetAccountLineAmount(List<CapitalAccountingLines> selectedCapitalAccountingLines, CapitalAssetAccountsGroupDetails groupAccountLine, CapitalAssetInformation capitalAsset) {
430         KualiDecimal lineAmount = KualiDecimal.ZERO;
431 
432         for (CapitalAccountingLines  capitalAccountingLine : selectedCapitalAccountingLines) {
433             if (groupAccountLine.getCapitalAssetLineNumber().compareTo(capitalAsset.getCapitalAssetLineNumber()) == 0 &&
434                     groupAccountLine.getSequenceNumber().compareTo(capitalAccountingLine.getSequenceNumber()) == 0 &&
435                     groupAccountLine.getFinancialDocumentLineTypeCode().equals(OLEConstants.SOURCE.equals(capitalAccountingLine.getLineType()) ? OLEConstants.SOURCE_ACCT_LINE_TYPE_CODE : OLEConstants.TARGET_ACCT_LINE_TYPE_CODE) &&
436                     groupAccountLine.getChartOfAccountsCode().equals(capitalAccountingLine.getChartOfAccountsCode()) &&
437                     groupAccountLine.getAccountNumber().equals(capitalAccountingLine.getAccountNumber()) &&
438                     groupAccountLine.getFinancialObjectCode().equals(capitalAccountingLine.getFinancialObjectCode())) {
439                 return capitalAccountingLine.getAmount();
440             }
441         }
442 
443         return lineAmount;
444     }
445 
446     /**
447      *
448      * @param capitalAccountingLines
449      * @param capitalAssetInformation
450      */
451     protected void redistributeIndividualAmountsForAccountingLinesForModifyAssets(List<CapitalAccountingLines> selectedCapitalAccountingLines, List<CapitalAssetInformation> capitalAssetInformation) {
452         for (CapitalAssetInformation capitalAsset : capitalAssetInformation) {
453             if (OLEConstants.CapitalAssets.CAPITAL_ASSET_MODIFY_ACTION_INDICATOR.equals(capitalAsset.getCapitalAssetActionIndicator())) {
454                 if (capitalAssetExists(selectedCapitalAccountingLines, capitalAsset, OLEConstants.CapitalAssets.CAPITAL_ASSET_MODIFY_ACTION_INDICATOR)) {
455                     redistributeIndividualAmounts(selectedCapitalAccountingLines, capitalAsset);
456                 }
457             }
458         }
459     }
460 
461     /**
462      * checks the capital accounting line's amount to the sum of the distributed
463      * accounting lines amounts and adjusts if there are any variances..
464      *
465      * @param selectedCapitalAccountingLines
466      * @param capitalAssetInformation
467      */
468     protected void adjustCapitalAssetsAccountingLinesAmounts(List<CapitalAccountingLines> selectedCapitalAccountingLines, List<CapitalAssetInformation> capitalAssetInformation) {
469         for (CapitalAccountingLines capitalAcctLine : selectedCapitalAccountingLines) {
470             adjustAccountingLinesAmounts(capitalAcctLine, capitalAssetInformation);
471         }
472     }
473 
474     /**
475      * for each capital account line, compares its amounts to the accounting lines
476      * on capital assets and adjusts its accounting lines amounts for any variances.
477      *
478      * @param capitalAcctLine
479      * @param capitalAssetInformation
480      */
481     protected void adjustAccountingLinesAmounts(CapitalAccountingLines capitalAcctLine, List<CapitalAssetInformation> capitalAssetInformation) {
482 
483         CapitalAssetAccountsGroupDetails lastAcctLine = null;
484 
485         KualiDecimal totalAccountsAmount = KualiDecimal.ZERO;
486 
487         for (CapitalAssetInformation capitalAsset : capitalAssetInformation) {
488             if (capitalAsset.getCapitalAssetLineAmount().compareTo(getAccountingLinesTotalAmount(capitalAsset)) != 0) {
489                 List<CapitalAssetAccountsGroupDetails> groupAccountLines = capitalAsset.getCapitalAssetAccountsGroupDetails();
490                 for (CapitalAssetAccountsGroupDetails groupAccountLine : groupAccountLines) {
491                     if (groupAccountLine.getCapitalAssetLineNumber().compareTo(capitalAsset.getCapitalAssetLineNumber()) == 0 &&
492                             groupAccountLine.getSequenceNumber().compareTo(capitalAcctLine.getSequenceNumber()) == 0 &&
493                             groupAccountLine.getFinancialDocumentLineTypeCode().equals(OLEConstants.SOURCE.equals(capitalAcctLine.getLineType()) ? OLEConstants.SOURCE_ACCT_LINE_TYPE_CODE : OLEConstants.TARGET_ACCT_LINE_TYPE_CODE) &&
494                             groupAccountLine.getChartOfAccountsCode().equals(capitalAcctLine.getChartOfAccountsCode()) &&
495                             groupAccountLine.getAccountNumber().equals(capitalAcctLine.getAccountNumber()) &&
496                             groupAccountLine.getFinancialObjectCode().equals(capitalAcctLine.getFinancialObjectCode())) {
497                         totalAccountsAmount = totalAccountsAmount.add(groupAccountLine.getAmount());
498                         lastAcctLine = groupAccountLine;
499                     }
500                 }
501             }
502         }
503 
504         KualiDecimal variance = capitalAcctLine.getAmount().subtract(totalAccountsAmount);
505         if (variance.isNonZero() && ObjectUtils.isNotNull(lastAcctLine)) {
506             lastAcctLine.setAmount(lastAcctLine.getAmount().add(variance));
507         }
508     }
509 
510     /**
511      * adjusts variances on capital assets where distribution method is set
512      * as "distribute evenly" and capital asset amount is odd value.  Reduce the
513      * capital asset amount line by 0.01 and then adjust the account amounts. Finally
514      * any variance between capital accounting lines and capital assets is added
515      * to the last capital asset and its accounting lines.
516      *
517      * @param capitalAssetInformation
518      */
519     protected void adjustVarianceOnCapitalAssets(List<CapitalAssetInformation> capitalAssetInformation) {
520         KualiDecimal adjustedCapitalAssetBalance = KualiDecimal.ZERO;
521         CapitalAssetInformation lastCapitalAsset = null;
522 
523         for (CapitalAssetInformation capitalAsset : capitalAssetInformation) {
524             //look at only cost equall assets...
525             if (OLEConstants.CapitalAssets.DISTRIBUTE_COST_EQUALLY_CODE.equalsIgnoreCase(capitalAsset.getDistributionAmountCode())) {
526                 if (capitalAsset.getCapitalAssetLineAmount().mod(new KualiDecimal(2)) != KualiDecimal.ZERO) {
527                     capitalAsset.setCapitalAssetLineAmount(capitalAsset.getCapitalAssetLineAmount().subtract(new KualiDecimal(0.01)));
528                     adjustedCapitalAssetBalance = adjustedCapitalAssetBalance.add(new KualiDecimal(0.01));
529                     lastCapitalAsset = capitalAsset;
530                 }
531             }
532         }
533 
534         if (ObjectUtils.isNotNull(lastCapitalAsset) && adjustedCapitalAssetBalance.isNonZero()) {
535             lastCapitalAsset.setCapitalAssetLineAmount(lastCapitalAsset.getCapitalAssetLineAmount().add(adjustedCapitalAssetBalance));
536         }
537     }
538 
539     /**
540      *
541      * @param capitalAccountingLines
542      * @param capitalAssetInformation
543      */
544     protected void redistributeIndividualAmountsForAccountingLinesForCreateAssets(List<CapitalAccountingLines> selectedCapitalAccountingLines, List<CapitalAssetInformation> capitalAssetInformation) {
545         for (CapitalAssetInformation capitalAsset : capitalAssetInformation) {
546             if (OLEConstants.CapitalAssets.CAPITAL_ASSET_CREATE_ACTION_INDICATOR.equals(capitalAsset.getCapitalAssetActionIndicator())) {
547                 if (capitalAssetExists(selectedCapitalAccountingLines, capitalAsset, OLEConstants.CapitalAssets.CAPITAL_ASSET_CREATE_ACTION_INDICATOR)) {
548                     redistributeIndividualAmounts(selectedCapitalAccountingLines, capitalAsset);
549                 }
550             }
551         }
552     }
553 
554     /**
555      *
556      * @param selectedCapitalAccountingLines
557      * @param capitalAsset
558      */
559     protected void redistributeIndividualAmounts(List<CapitalAccountingLines> selectedCapitalAccountingLines, CapitalAssetInformation capitalAsset) {
560         KualiDecimal capitalAssetAmount = capitalAsset.getCapitalAssetLineAmount();
561 
562         KualiDecimal totalCapitalAccountsAmount = getTotalCapitalAccountsAmounts(selectedCapitalAccountingLines);
563 
564         CapitalAssetAccountsGroupDetails lastAccountLine = new CapitalAssetAccountsGroupDetails();
565         int lastAccountLineIndex = 0;
566         KualiDecimal distributedAmount = KualiDecimal.ZERO;
567 
568         List<CapitalAssetAccountsGroupDetails> groupAccountLines = capitalAsset.getCapitalAssetAccountsGroupDetails();
569         for (CapitalAssetAccountsGroupDetails groupAccountLine : groupAccountLines) {
570             BigDecimal linePercent = getCapitalAccountingLinePercent(selectedCapitalAccountingLines, groupAccountLine, totalCapitalAccountsAmount);
571             groupAccountLine.setAmount(new KualiDecimal(capitalAssetAmount.bigDecimalValue().multiply(linePercent)));
572             lastAccountLine = groupAccountLine;
573             distributedAmount = distributedAmount.add(groupAccountLine.getAmount());
574 
575             lastAccountLineIndex++;
576         }
577 
578         lastAccountLine.setAmount(lastAccountLine.getAmount().add(capitalAssetAmount.subtract(distributedAmount)));
579     }
580 
581     /**
582      *
583      * @param selectedCapitalAccountingLines
584      * @param groupAccountLine
585      * @param totalCapitalAccountsAmount
586      * @return percent
587      */
588     protected BigDecimal getCapitalAccountingLinePercent(List<CapitalAccountingLines> selectedCapitalAccountingLines, CapitalAssetAccountsGroupDetails groupAccountLine, KualiDecimal totalCapitalAccountsAmount) {
589         for (CapitalAccountingLines capitalAccountingLine : selectedCapitalAccountingLines) {
590             if (groupAccountLine.getSequenceNumber().compareTo(capitalAccountingLine.getSequenceNumber()) == 0 &&
591                     groupAccountLine.getFinancialDocumentLineTypeCode().equals(OLEConstants.SOURCE.equals(capitalAccountingLine.getLineType()) ? OLEConstants.SOURCE_ACCT_LINE_TYPE_CODE : OLEConstants.TARGET_ACCT_LINE_TYPE_CODE) &&
592                     groupAccountLine.getChartOfAccountsCode().equals(capitalAccountingLine.getChartOfAccountsCode()) &&
593                     groupAccountLine.getAccountNumber().equals(capitalAccountingLine.getAccountNumber()) &&
594                     groupAccountLine.getFinancialObjectCode().equals(capitalAccountingLine.getFinancialObjectCode())) {
595                 return (capitalAccountingLine.getAmount().bigDecimalValue().divide(totalCapitalAccountsAmount.bigDecimalValue(), OLEConstants.CapitalAssets.CAPITAL_ACCOUNT_LINE_PERCENT_SCALE, OLEConstants.CapitalAssets.PERCENT_SCALE));
596             }
597         }
598 
599         return BigDecimal.ZERO;
600     }
601 
602     /**
603      *
604      * @param capitalAccountingLines
605      * @param capitalAssetInformation
606      * @param remainingAmountToDistribute
607      */
608     protected void redistributeEqualAmountsForAccountingLineForCreateAssets(List<CapitalAccountingLines> selectedCapitalAccountingLines, List<CapitalAssetInformation> capitalAssetInformation, KualiDecimal remainingAmountToDistribute) {
609         //get the total capital assets quantity
610         int totalQuantity = getTotalQuantityOfCreateAssets(selectedCapitalAccountingLines, capitalAssetInformation);
611         if (totalQuantity > 0) {
612             KualiDecimal equalCreateAssetAmount = remainingAmountToDistribute.divide(new KualiDecimal(totalQuantity), true);
613 
614         int lastAssetIndex = 0;
615         CapitalAssetInformation lastCapitalAsset = new CapitalAssetInformation();
616 
617         if (equalCreateAssetAmount.compareTo(KualiDecimal.ZERO) != 0) {
618             for (CapitalAssetInformation capitalAsset : capitalAssetInformation) {
619                 if (OLEConstants.CapitalAssets.CAPITAL_ASSET_CREATE_ACTION_INDICATOR.equals(capitalAsset.getCapitalAssetActionIndicator()) &&
620                             (OLEConstants.CapitalAssets.DISTRIBUTE_COST_EQUALLY_CODE.equalsIgnoreCase(capitalAsset.getDistributionAmountCode()))) {
621                         if (capitalAssetExists(selectedCapitalAccountingLines, capitalAsset, OLEConstants.CapitalAssets.CAPITAL_ASSET_CREATE_ACTION_INDICATOR)) {
622                             redistributeEqualAmounts(selectedCapitalAccountingLines, capitalAsset, equalCreateAssetAmount, totalQuantity);
623                             if (ObjectUtils.isNotNull(capitalAsset.getCapitalAssetQuantity())) {
624                                 lastAssetIndex = lastAssetIndex + capitalAsset.getCapitalAssetQuantity();
625                             }
626                     //get a reference to the last capital create asset to fix any variances...
627                     lastCapitalAsset = capitalAsset;
628                 }
629             }
630         }
631             }
632 
633         //apply any variance left to the last
634         KualiDecimal varianceForAssets = remainingAmountToDistribute.subtract(equalCreateAssetAmount.multiply(new KualiDecimal(lastAssetIndex)));
635         if (varianceForAssets.isNonZero()) {
636                 lastCapitalAsset.setCapitalAssetLineAmount(lastCapitalAsset.getCapitalAssetLineAmount().add(varianceForAssets));
637                 redistributeEqualAmountsOnLastCapitalAsset(selectedCapitalAccountingLines, lastCapitalAsset, capitalAssetInformation, OLEConstants.CapitalAssets.CAPITAL_ASSET_CREATE_ACTION_INDICATOR);
638             }
639         }
640     }
641 
642     /**
643      *
644      * @param selectedCapitalAccountingLines
645      * @param capitalAssetInformation
646      * @return createAssetsCount count of create assets
647      */
648     protected int getTotalQuantityOfCreateAssets(List<CapitalAccountingLines> selectedCapitalAccountingLines, List<CapitalAssetInformation> capitalAssetInformation) {
649         int totalQuantity = 0;
650 
651         for (CapitalAssetInformation capitalAsset : capitalAssetInformation) {
652             if (OLEConstants.CapitalAssets.CAPITAL_ASSET_CREATE_ACTION_INDICATOR.equals(capitalAsset.getCapitalAssetActionIndicator())) {
653                 if (capitalAssetExists(selectedCapitalAccountingLines, capitalAsset, OLEConstants.CapitalAssets.CAPITAL_ASSET_CREATE_ACTION_INDICATOR)) {
654                     if (ObjectUtils.isNotNull(capitalAsset.getCapitalAssetQuantity())) {
655                         totalQuantity += capitalAsset.getCapitalAssetQuantity();
656                     }
657                 }
658             }
659         }
660 
661         return totalQuantity;
662     }
663 
664     /**
665      *
666      * @param selectedCapitalAccountingLines
667      * @param capitalAssetInformation
668      * @return createAssetsCount count of create assets
669      */
670     protected int numberOfCreateAssetsExist(List<CapitalAccountingLines> selectedCapitalAccountingLines, List<CapitalAssetInformation> capitalAssetInformation) {
671         int createAssetsCount = 0;
672 
673         for (CapitalAssetInformation capitalAsset : capitalAssetInformation) {
674             if (OLEConstants.CapitalAssets.CAPITAL_ASSET_CREATE_ACTION_INDICATOR.equals(capitalAsset.getCapitalAssetActionIndicator())) {
675                 if (capitalAssetExists(selectedCapitalAccountingLines, capitalAsset, OLEConstants.CapitalAssets.CAPITAL_ASSET_CREATE_ACTION_INDICATOR)) {
676                 createAssetsCount++;
677             }
678         }
679         }
680 
681         if (createAssetsCount == 0) {
682             return 1;
683         }
684 
685         return createAssetsCount;
686     }
687 
688     /**
689      *
690      * @param selectedCapitalAccountingLines
691      * @param capitalAssetInformation
692      * @return modifiedAssetsCount number of modified assets
693      */
694     protected int getNumberOfModifiedAssetsExist(List<CapitalAccountingLines> selectedCapitalAccountingLines, List<CapitalAssetInformation> capitalAssetInformation) {
695         int modifiedAssetsCount = 0;
696 
697         for (CapitalAssetInformation capitalAsset : capitalAssetInformation) {
698             if (OLEConstants.CapitalAssets.CAPITAL_ASSET_MODIFY_ACTION_INDICATOR.equals(capitalAsset.getCapitalAssetActionIndicator()) &&
699                     ObjectUtils.isNotNull(capitalAsset.getCapitalAssetNumber())) {
700                 if (capitalAssetExists(selectedCapitalAccountingLines, capitalAsset, OLEConstants.CapitalAssets.CAPITAL_ASSET_MODIFY_ACTION_INDICATOR)) {
701                 modifiedAssetsCount++;
702             }
703         }
704         }
705 
706         if (modifiedAssetsCount == 0) {
707             return 1;
708         }
709 
710         return modifiedAssetsCount;
711     }
712 
713     /**
714      * Clear the capital asset information that the user has entered
715      *
716      * @param mapping
717      * @param form
718      * @param request
719      * @param response
720      * @return action forward string
721      * @throws Exception
722      */
723     public ActionForward clearCapitalAssetInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
724         LOG.debug("clearCapitalAssetInfo() - start");
725 
726         KualiAccountingDocumentFormBase kualiAccountingDocumentFormBase = (KualiAccountingDocumentFormBase) form;
727         List<CapitalAssetInformation> capitalAssetInformation = this.getCurrentCapitalAssetInformationObject(kualiAccountingDocumentFormBase);
728 
729         if (capitalAssetInformation == null) {
730             return mapping.findForward(OLEConstants.MAPPING_BASIC);
731         }
732 
733         int clearIndex = getSelectedLine(request);
734         this.resetCapitalAssetInfo(capitalAssetInformation.get(clearIndex));
735 
736         //now process the remaining capital asset records
737         processRemainingCapitalAssetInfo(form, capitalAssetInformation);
738 
739         return mapping.findForward(OLEConstants.MAPPING_BASIC);
740     }
741 
742     /**
743      * clear up the modify capital asset information.  The amount field is reset to 0
744      * Processes any remaining capital assets so that it recalculates the system control
745      * and system control remaining amounts.
746      *
747      * @param mapping
748      * @param form
749      * @param request
750      * @param response
751      * @return action forward string
752      * @throws Exception
753      */
754     public ActionForward clearCapitalAssetModify(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
755         LOG.debug("clearCapitalAssetModify() - start");
756 
757         KualiAccountingDocumentFormBase kualiAccountingDocumentFormBase = (KualiAccountingDocumentFormBase) form;
758         List<CapitalAssetInformation> capitalAssetInformation = this.getCurrentCapitalAssetInformationObject(kualiAccountingDocumentFormBase);
759 
760         if (capitalAssetInformation == null) {
761             return mapping.findForward(OLEConstants.MAPPING_BASIC);
762         }
763 
764         int clearIndex = getSelectedLine(request);
765         capitalAssetInformation.get(clearIndex).setCapitalAssetLineAmount(KualiDecimal.ZERO);
766 
767         //zero out the amount distribute on the accounting lines...
768         for (CapitalAssetAccountsGroupDetails groupAccountLine : capitalAssetInformation.get(clearIndex).getCapitalAssetAccountsGroupDetails()) {
769             groupAccountLine.setAmount(KualiDecimal.ZERO);
770         }
771 
772         //now process the remaining capital asset records
773         processRemainingCapitalAssetInfo(form, capitalAssetInformation);
774 
775         return mapping.findForward(OLEConstants.MAPPING_BASIC);
776     }
777 
778     /**
779      * inserts capital asset information into capital assets list.
780      * Also recalculates the system control and system control remaining amounts.
781      * Puts a global error message if the user does not enter capital asset quantity.
782      * If the quantity is > 1, it will insert that many tag/location detail records for this
783      * capital asset item.
784      *
785      * @param mapping
786      * @param form
787      * @param request
788      * @param response
789      * @return action forward string
790      * @throws Exception
791      */
792     public ActionForward insertCapitalAssetInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
793         LOG.debug("insertCapitalAssetInfo() - start");
794 
795         KualiAccountingDocumentFormBase kualiAccountingDocumentFormBase = (KualiAccountingDocumentFormBase) form;
796         List<CapitalAssetInformation> capitalAssetInformation = this.getCurrentCapitalAssetInformationObject(kualiAccountingDocumentFormBase);
797 
798         if (capitalAssetInformation == null) {
799             return mapping.findForward(OLEConstants.MAPPING_BASIC);
800         }
801 
802         int addIndex = getSelectedLine(request);
803         if (capitalAssetInformation.get(addIndex).getCapitalAssetQuantity() == null || capitalAssetInformation.get(addIndex).getCapitalAssetQuantity() <= 0) {
804             GlobalVariables.getMessageMap().putError(OLEConstants.EDIT_CAPITAL_ASSET_INFORMATION_ERRORS, OLEKeyConstants.ERROR_DOCUMENT_CAPITAL_ASSET_QUANTITY_REQUIRED);
805         }
806         else {
807             this.addCapitalAssetInfoDetailLines(capitalAssetInformation.get(addIndex));
808         }
809 
810         /*
811         DistributeCapitalAssetAmountToGroupAccountingLines(kualiAccountingDocumentFormBase);
812         CapitalAccountingLinesFormBase capitalAccountingLinesFormBase = (CapitalAccountingLinesFormBase) kualiAccountingDocumentFormBase;
813         checkSelectForCapitalAccountingLines(capitalAccountingLinesFormBase);
814 
815         checkCapitalAccountingLinesSelected(capitalAccountingLinesFormBase);
816         calculatePercentsForSelectedCapitalAccountingLines(capitalAccountingLinesFormBase);
817 
818         CapitalAccountingLinesDocumentBase caldb = (CapitalAccountingLinesDocumentBase) capitalAccountingLinesFormBase.getFinancialDocument();
819         String distributionAmountCode = capitalAccountingLinesFormBase.getCapitalAccountingLine().getDistributionCode();
820 
821         List<CapitalAccountingLines> capitalAccountingLines = caldb.getCapitalAccountingLines();
822 
823         List<CapitalAccountingLines> selectedCapitalAccountingLines = new ArrayList<CapitalAccountingLines>();
824 
825         for (CapitalAccountingLines capitalAccountingLine : capitalAccountingLines) {
826             if (capitalAccountingLine.isSelectLine() && !capitalAccountingLine.isAmountDistributed()) {
827                 capitalAccountingLine.setDistributionAmountCode(OLEConstants.CapitalAssets.CAPITAL_ASSET_MODIFY_ACTION_INDICATOR);
828                 selectedCapitalAccountingLines.add(capitalAccountingLine);
829             }
830         }
831 
832         //redistribute the capital asset modify amount to the group accounting lines
833         //based on amount.
834         redistributeToGroupAccountingLinesFromAssetsByAmounts(selectedCapitalAccountingLines, capitalAssetInformation.get(addIndex));
835 
836         */
837 
838         //now process the remaining capital asset records
839         processRemainingCapitalAssetInfo(form, capitalAssetInformation);
840 
841         return mapping.findForward(OLEConstants.MAPPING_BASIC);
842     }
843 
844     /**
845      * inserts capital asset information into capital assets list.
846      * Also recalculates the system control and system control remaining amounts.
847      * Puts a global error message if the user does not enter capital asset quantity.
848      * If the quantity is > 1, it will insert that many tag/location detail records for this
849      * capital asset item.
850      *
851      * @param mapping
852      * @param form
853      * @param request
854      * @param response
855      * @return action forward string
856      * @throws Exception
857      */
858     public ActionForward addCapitalAssetTagLocationInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
859         LOG.debug("addCapitalAssetTagLocationInfo() - start");
860 
861         KualiAccountingDocumentFormBase kualiAccountingDocumentFormBase = (KualiAccountingDocumentFormBase) form;
862         List<CapitalAssetInformation> capitalAssetInformation = this.getCurrentCapitalAssetInformationObject(kualiAccountingDocumentFormBase);
863 
864         if (capitalAssetInformation == null) {
865             return mapping.findForward(OLEConstants.MAPPING_BASIC);
866         }
867 
868         int addIndex = getSelectedLine(request);
869         if (capitalAssetInformation.get(addIndex).getCapitalAssetQuantity() == null || capitalAssetInformation.get(addIndex).getCapitalAssetQuantity() <= 0) {
870             GlobalVariables.getMessageMap().putError(OLEConstants.EDIT_CAPITAL_ASSET_INFORMATION_ERRORS, OLEKeyConstants.ERROR_DOCUMENT_CAPITAL_ASSET_QUANTITY_REQUIRED);
871         }
872         else {
873             this.addCapitalAssetInfoDetailLines(capitalAssetInformation.get(addIndex));
874         }
875 
876         //now process the remaining capital asset records
877         processRemainingCapitalAssetInfo(form, capitalAssetInformation);
878 
879         return mapping.findForward(OLEConstants.MAPPING_BASIC);
880     }
881 
882     /**
883      * refreshes capital asset modify information to the modify capital assets list.
884      * Also recalculates the system control and system control remaining amounts.
885      * Puts a global error message if the user does not enter capital asset number.
886      *
887      * @param mapping
888      * @param form
889      * @param request
890      * @param response
891      * @return action forward string
892      * @throws Exception
893      */
894     public ActionForward refreshCapitalAssetModify(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
895         LOG.debug("refreshCapitalAssetModify() - start");
896 
897         KualiAccountingDocumentFormBase kualiAccountingDocumentFormBase = (KualiAccountingDocumentFormBase) form;
898         List<CapitalAssetInformation> capitalAssetInformation = this.getCurrentCapitalAssetInformationObject(kualiAccountingDocumentFormBase);
899 
900         if (capitalAssetInformation == null) {
901             return mapping.findForward(OLEConstants.MAPPING_BASIC);
902         }
903 
904         int refreshIndex = getSelectedLine(request);
905 
906         if (capitalAssetInformation.get(refreshIndex).getCapitalAssetNumber() == null) {
907             GlobalVariables.getMessageMap().putError(OLEConstants.EDIT_CAPITAL_ASSET_MODIFY_ERRORS, OLEKeyConstants.ERROR_DOCUMENT_CAPITAL_ASSET_NUMBER_REQUIRED);
908         }
909 
910         //now process the remaining capital asset records
911         processRemainingCapitalAssetInfo(form, capitalAssetInformation);
912 
913         return mapping.findForward(OLEConstants.MAPPING_BASIC);
914     }
915 
916     /**
917      * deletes the capital asset information
918      */
919     public ActionForward deleteCapitalAssetInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
920         LOG.debug("deleteCapitalAssetInfoDetail() - start");
921 
922         KualiAccountingDocumentFormBase kualiAccountingDocumentFormBase = (KualiAccountingDocumentFormBase) form;
923         List<CapitalAssetInformation> capitalAssetInformation = this.getCurrentCapitalAssetInformationObject(kualiAccountingDocumentFormBase);
924 
925         if (capitalAssetInformation == null) {
926             return mapping.findForward(OLEConstants.MAPPING_BASIC);
927         }
928 
929         int lineIndexForCapitalAssetInfo = this.getLineToDelete(request);
930 
931         if (capitalAssetInformation.get(lineIndexForCapitalAssetInfo).getCapitalAssetInformationDetails() != null &&
932                 capitalAssetInformation.get(lineIndexForCapitalAssetInfo).getCapitalAssetInformationDetails().size() > 0) {
933             capitalAssetInformation.get(lineIndexForCapitalAssetInfo).getCapitalAssetInformationDetails().clear();
934 
935         }
936 
937         if (capitalAssetInformation.get(lineIndexForCapitalAssetInfo).getCapitalAssetAccountsGroupDetails() != null &&
938                 capitalAssetInformation.get(lineIndexForCapitalAssetInfo).getCapitalAssetAccountsGroupDetails().size() > 0) {
939             capitalAssetInformation.get(lineIndexForCapitalAssetInfo).getCapitalAssetAccountsGroupDetails().clear();
940         }
941 
942         capitalAssetInformation.remove(lineIndexForCapitalAssetInfo);
943 
944         //now process the remaining capital asset records
945         processRemainingCapitalAssetInfo(form, capitalAssetInformation);
946 
947         return mapping.findForward(OLEConstants.MAPPING_BASIC);
948     }
949 
950     /**
951      * deletes the capital asset information
952      */
953     public ActionForward deleteCapitalAssetModify(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
954         LOG.debug("deleteCapitalAssetModify() - start");
955 
956         KualiAccountingDocumentFormBase kualiAccountingDocumentFormBase = (KualiAccountingDocumentFormBase) form;
957         List<CapitalAssetInformation> capitalAssetInformation = this.getCurrentCapitalAssetInformationObject(kualiAccountingDocumentFormBase);
958 
959         if (capitalAssetInformation == null) {
960             return mapping.findForward(OLEConstants.MAPPING_BASIC);
961         }
962 
963         int lineIndexForCapitalAssetInfo = this.getLineToDelete(request);
964 
965         capitalAssetInformation.remove(lineIndexForCapitalAssetInfo);
966 
967         //now process the remaining capital asset records
968         processRemainingCapitalAssetInfo(form, capitalAssetInformation);
969 
970         return mapping.findForward(OLEConstants.MAPPING_BASIC);
971     }
972 
973     /**
974      * process any remaining capital asset info in the list to check and calculate the
975      * remaining distributed amount.  Also checks to make sure if "select Lines" is to be
976      * checked on/off
977      * @param form
978      * @param capitalAssetInformation
979      */
980     protected void processRemainingCapitalAssetInfo(ActionForm form, List<CapitalAssetInformation> capitalAssetInformation) {
981         CapitalAccountingLinesFormBase calfb = (CapitalAccountingLinesFormBase) form;
982 
983         //recalculate the amount remaining to be distributed and save the value on the form
984         calculateRemainingDistributedAmount(calfb, capitalAssetInformation);
985 
986         //set the amountDistributed property to true if the total amount of all the capital assets
987         //for a given capital accounting line is greater or equal to the line amount.
988         checkCapitalAccountingLinesSelected(calfb);
989 
990         //redistribute each capital asset amount to its group accounting lines...
991         KualiAccountingDocumentFormBase kualiAccountingDocumentFormBase = (KualiAccountingDocumentFormBase) form;
992         DistributeCapitalAssetAmountToGroupAccountingLines(kualiAccountingDocumentFormBase);
993 
994         setTabStatesForCapitalAssets(form);
995     }
996 
997     /**
998      * delete a detail line from the capital asset information
999      */
1000     public ActionForward deleteCapitalAssetInfoDetailLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
1001         LOG.debug("deleteCapitalAssetInfoDetailLine() - start");
1002 
1003         KualiAccountingDocumentFormBase kualiAccountingDocumentFormBase = (KualiAccountingDocumentFormBase) form;
1004         List<CapitalAssetInformation> capitalAssetInformation =  this.getCurrentCapitalAssetInformationObject(kualiAccountingDocumentFormBase);
1005 
1006         if (capitalAssetInformation == null) {
1007             return mapping.findForward(OLEConstants.MAPPING_BASIC);
1008         }
1009 
1010         int lineIndexForCapitalAssetInfo = this.getLineToDelete(request);
1011         capitalAssetInformation.get(lineIndexForCapitalAssetInfo).getCapitalAssetInformationDetails().remove(0);
1012 
1013         return mapping.findForward(OLEConstants.MAPPING_BASIC);
1014     }
1015 
1016     /**
1017      * get the capital asset information object currently associated with the document
1018      */
1019     protected List<CapitalAssetInformation> getCurrentCapitalAssetInformationObject(KualiAccountingDocumentFormBase kualiAccountingDocumentFormBase) {
1020         LOG.debug("getCurrentCapitalAssetInformationObject() - start");
1021 
1022         CapitalAssetInformationDocumentBase capitalAssetInformationDocumentBase = (CapitalAssetInformationDocumentBase) kualiAccountingDocumentFormBase.getFinancialDocument();
1023 
1024         if (!(capitalAssetInformationDocumentBase instanceof CapitalAssetEditable)) {
1025             return null;
1026         }
1027 
1028         List<CapitalAssetInformation> capitalAssetInformation = capitalAssetInformationDocumentBase.getCapitalAssetInformation();
1029         return capitalAssetInformation;
1030     }
1031 
1032     /**
1033      * reset the nonkey fields of the given capital asset information
1034      * removes the corresponding capital asset information detail record from the list.
1035      * @param capitalAssetInformation the given capital asset information
1036      */
1037     protected void resetCapitalAssetInfo(CapitalAssetInformation capitalAssetInformation) {
1038         if (capitalAssetInformation != null) {
1039             capitalAssetInformation.setCapitalAssetDescription(null);
1040             capitalAssetInformation.setCapitalAssetManufacturerModelNumber(null);
1041             capitalAssetInformation.setCapitalAssetManufacturerName(null);
1042 
1043             capitalAssetInformation.setCapitalAssetNumber(null);
1044             capitalAssetInformation.setCapitalAssetTypeCode(null);
1045             capitalAssetInformation.setCapitalAssetQuantity(null);
1046 
1047             capitalAssetInformation.setVendorDetailAssignedIdentifier(null);
1048             capitalAssetInformation.setVendorHeaderGeneratedIdentifier(null);
1049             // Set the BO to null cause it won't be updated automatically when vendorDetailAssetIdentifier and
1050             // VendorHeanderGeneratedIndentifier set to null.
1051             capitalAssetInformation.setVendorDetail(null);
1052             capitalAssetInformation.setVendorName(null);
1053             capitalAssetInformation.setCapitalAssetLineAmount(KualiDecimal.ZERO);
1054             capitalAssetInformation.getCapitalAssetInformationDetails().clear();
1055 
1056             //zero out the amount distribute on the accounting lines...
1057             for (CapitalAssetAccountsGroupDetails groupAccountLine : capitalAssetInformation.getCapitalAssetAccountsGroupDetails()) {
1058                 groupAccountLine.setAmount(KualiDecimal.ZERO);
1059             }
1060         }
1061     }
1062 
1063     /**
1064      * Overridden to guarantee that form of copied document is set to whatever the entry mode of the document is
1065      * @see org.kuali.rice.kns.web.struts.action.KualiTransactionalDocumentActionBase#copy(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
1066      */
1067     @Override
1068     public ActionForward copy(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
1069         CapitalAccountingLinesFormBase capitalAccountingLinesFormBase = (CapitalAccountingLinesFormBase) form;;
1070         CapitalAccountingLinesDocumentBase caldb = (CapitalAccountingLinesDocumentBase) capitalAccountingLinesFormBase.getFinancialDocument();
1071 
1072         List<CapitalAccountingLines> capitalAccountingLines = caldb.getCapitalAccountingLines();
1073         List<CapitalAccountingLines> copiedCapitalAccountingLines = new ArrayList<CapitalAccountingLines>();
1074         for (CapitalAccountingLines capitalAccountingLine : capitalAccountingLines) {
1075             copiedCapitalAccountingLines.add(capitalAccountingLine);
1076         }
1077         capitalAccountingLines.clear();
1078 
1079         ActionForward forward = super.copy(mapping, form, request, response);
1080 
1081         caldb.setCapitalAccountingLines(copiedCapitalAccountingLines);
1082 
1083         // if the copied document has capital asset collection, remove the collection
1084         KualiAccountingDocumentFormBase kualiAccountingDocumentFormBase = (KualiAccountingDocumentFormBase) form;
1085         AccountingDocument document = kualiAccountingDocumentFormBase.getFinancialDocument();
1086         if (document instanceof CapitalAssetEditable) {
1087             CapitalAssetEditable capitalAssetEditable = (CapitalAssetEditable) document;
1088 
1089             List<CapitalAssetInformation> capitalAssets = capitalAssetEditable.getCapitalAssetInformation();
1090             for (CapitalAssetInformation capitalAsset : capitalAssets) {
1091                 Long capitalAssetNumber = capitalAsset.getCapitalAssetNumber();
1092                 resetCapitalAssetInfo(capitalAsset);
1093 
1094                 //set capital asset number to copied asset line if "modify" asset
1095                 //because resetCapitalAssetInfo cleared the value.
1096                 if (OLEConstants.CapitalAssets.CAPITAL_ASSET_MODIFY_ACTION_INDICATOR.equalsIgnoreCase(capitalAsset.getCapitalAssetActionIndicator())) {
1097                     capitalAsset.setCapitalAssetNumber(capitalAssetNumber);
1098                 }
1099                 capitalAsset.setCapitalAssetProcessedIndicator(false);
1100             }
1101         }
1102 
1103         //setup the initial next sequence number column..
1104         KualiDocumentFormBase kualiDocumentFormBase = (KualiDocumentFormBase) form;
1105         setupIntialNextCapitalAssetLineNumber(kualiDocumentFormBase);
1106 
1107         checkCapitalAccountingLinesSelected(capitalAccountingLinesFormBase);
1108 
1109         return forward;
1110     }
1111 
1112     /**
1113      * setups the next capital asset line number
1114      *
1115      * @param kualiDocumentFormBase
1116      */
1117     protected void setupIntialNextCapitalAssetLineNumber(KualiDocumentFormBase kualiDocumentFormBase) {
1118         KualiAccountingDocumentFormBase kadfb = (KualiAccountingDocumentFormBase) kualiDocumentFormBase;
1119         CapitalAssetInformationDocumentBase caidb = (CapitalAssetInformationDocumentBase) kadfb.getFinancialDocument();
1120 
1121         List<CapitalAssetInformation> currentCapitalAssetInformation =  this.getCurrentCapitalAssetInformationObject(kadfb);
1122         for (CapitalAssetInformation capitalAsset : currentCapitalAssetInformation) {
1123             if (capitalAsset.getCapitalAssetLineNumber() > caidb.getNextCapitalAssetLineNumber()) {
1124                 caidb.setNextCapitalAssetLineNumber(capitalAsset.getCapitalAssetLineNumber());
1125             }
1126         }
1127 
1128         caidb.setNextCapitalAssetLineNumber(caidb.getNextCapitalAssetLineNumber()+1);
1129     }
1130 
1131     /**
1132      * calculates the percents for the selected capital accounting lines only
1133      *
1134      * @param calfb
1135      */
1136     protected void calculatePercentsForSelectedCapitalAccountingLines(CapitalAccountingLinesFormBase calfb) {
1137         CapitalAccountingLinesDocumentBase caldb = (CapitalAccountingLinesDocumentBase) calfb.getFinancialDocument();
1138         List<CapitalAccountingLines> capitalAccountingLines = caldb.getCapitalAccountingLines();
1139 
1140         KualiDecimal totalCapitalLinesSelectedAmount = (calculateTotalCapitalLinesSelectedAmount(capitalAccountingLines)).abs();
1141         if (totalCapitalLinesSelectedAmount.isNonZero()) {
1142             for (CapitalAccountingLines capitalAccountingLine : capitalAccountingLines) {
1143                 if (capitalAccountingLine.isSelectLine() && !capitalAccountingLine.isAmountDistributed()) {
1144                     capitalAccountingLine.setAccountLinePercent(capitalAccountingLine.getAmount().divide(totalCapitalLinesSelectedAmount).multiply(new KualiDecimal(100), true));
1145                 }
1146             }
1147         }
1148     }
1149 
1150     /**
1151      *
1152      * @param capitalAccountingLines
1153      * @return
1154      */
1155     protected KualiDecimal calculateTotalCapitalLinesSelectedAmount(List<CapitalAccountingLines> capitalAccountingLines) {
1156         KualiDecimal totalLineAmount = KualiDecimal.ZERO;
1157 
1158         for (CapitalAccountingLines capitalAccountingLine : capitalAccountingLines) {
1159             if (capitalAccountingLine.isSelectLine() && !capitalAccountingLine.isAmountDistributed()) {
1160                 totalLineAmount = totalLineAmount.add(capitalAccountingLine.getAmount());
1161             }
1162         }
1163 
1164         return totalLineAmount;
1165     }
1166 
1167     /**
1168      * Populates capital asset information collection with capital accounting lines.
1169      * Based on actionType, capitalassetactionindicator attribute is filled with 'C' for create
1170      * and 'M' for modify assets, which will be used to differentiate to pull the records in
1171      * create asset screen or modify asset screen.
1172      *
1173      * @param calfb
1174      * @param actionType
1175      * @param distributionAmountCode
1176      */
1177     protected void createCapitalAssetForGroupAccountingLines(CapitalAccountingLinesFormBase calfb, String actionType, String distributionAmountCode) {
1178         CapitalAccountingLinesDocumentBase caldb = (CapitalAccountingLinesDocumentBase) calfb.getFinancialDocument();
1179 
1180         List<CapitalAccountingLines> capitalAccountingLines = caldb.getCapitalAccountingLines();
1181         List<CapitalAssetInformation> capitalAssetInformation = new ArrayList<CapitalAssetInformation>();
1182 
1183         KualiAccountingDocumentFormBase kadfb = calfb;
1184 
1185         List<CapitalAssetInformation> currentCapitalAssetInformation =  this.getCurrentCapitalAssetInformationObject(kadfb);
1186 
1187         String documentNumber = calfb.getDocument().getDocumentNumber();
1188         calfb.setSystemControlAmount(KualiDecimal.ZERO);
1189 
1190         List<CapitalAccountingLines> selectedCapitalAccountingLines = new ArrayList<CapitalAccountingLines>();
1191 
1192         for (CapitalAccountingLines capitalAccountingLine : capitalAccountingLines) {
1193             if (capitalAccountingLine.isSelectLine() && !capitalAccountingLine.isAmountDistributed()) {
1194                 capitalAccountingLine.setDistributionAmountCode(distributionAmountCode);
1195                 selectedCapitalAccountingLines.add(capitalAccountingLine);
1196             }
1197         }
1198 
1199         CapitalAssetInformation existingCapitalAsset = capitalAssetCreated(selectedCapitalAccountingLines, currentCapitalAssetInformation);
1200                 if (ObjectUtils.isNotNull(existingCapitalAsset)) {
1201             if (!accountingLinesAmountDistributed(selectedCapitalAccountingLines, existingCapitalAsset)) {
1202                         //accounting line amount not completely distributed yet so we need to create more assets
1203                         //add the capital information record to the list of asset information
1204                 createNewCapitalAsset(selectedCapitalAccountingLines, currentCapitalAssetInformation, documentNumber, actionType, getNextCapitalAssetLineNumber(kadfb));
1205             }
1206         }
1207         else {
1208             //add the capital information record to the list of asset information
1209             createNewCapitalAsset(selectedCapitalAccountingLines, currentCapitalAssetInformation, documentNumber, actionType, getNextCapitalAssetLineNumber(kadfb));
1210         }
1211     }
1212 
1213     /**
1214      * helper method to add accounting details for this new capital asset record
1215      *
1216      * @param capitalAccountingLines
1217      * @param currentCapitalAssetInformation
1218      * @param documentNumber
1219      * @param actionType
1220      * @param nextCapitalAssetLineNumnber
1221      */
1222     protected void createNewCapitalAsset(List<CapitalAccountingLines> capitalAccountingLines, List<CapitalAssetInformation> currentCapitalAssetInformation, String documentNumber, String actionType, Integer nextCapitalAssetLineNumber) {
1223                         CapitalAssetInformation capitalAsset = new CapitalAssetInformation();
1224         capitalAsset.setCapitalAssetLineAmount(KualiDecimal.ZERO);
1225                         capitalAsset.setDocumentNumber(documentNumber);
1226         capitalAsset.setCapitalAssetLineNumber(nextCapitalAssetLineNumber);
1227                         capitalAsset.setCapitalAssetActionIndicator(actionType);
1228                         capitalAsset.setCapitalAssetProcessedIndicator(false);
1229 
1230         //now setup the account line information associated with this capital asset
1231         for (CapitalAccountingLines capitalAccountingLine : capitalAccountingLines) {
1232             capitalAsset.setDistributionAmountCode(capitalAccountingLine.getDistributionAmountCode());
1233             createCapitalAssetAccountingLinesDetails(capitalAccountingLine, capitalAsset);
1234                     }
1235 
1236         currentCapitalAssetInformation.add(capitalAsset);
1237                 }
1238 
1239     /**
1240      * helper method to add accounting details for this new modify capital asset record
1241      *
1242      * @param capitalAccountingLines
1243      * @param currentCapitalAssetInformation
1244      * @param documentNumber
1245      * @param actionType
1246      * @param nextCapitalAssetLineNumnber
1247      * @param capitalAssetNumber
1248      */
1249     protected void createNewModifyCapitalAsset(List<CapitalAccountingLines> capitalAccountingLines, List<CapitalAssetInformation> currentCapitalAssetInformation, String documentNumber, String actionType, Integer nextCapitalAssetLineNumber, long capitalAssetNumber) {
1250                     CapitalAssetInformation capitalAsset = new CapitalAssetInformation();
1251         capitalAsset.setCapitalAssetNumber(capitalAssetNumber);
1252         capitalAsset.setCapitalAssetLineAmount(KualiDecimal.ZERO);
1253                     capitalAsset.setDocumentNumber(documentNumber);
1254         capitalAsset.setCapitalAssetLineNumber(nextCapitalAssetLineNumber);
1255                     capitalAsset.setCapitalAssetActionIndicator(actionType);
1256                     capitalAsset.setCapitalAssetProcessedIndicator(false);
1257 
1258         //now setup the account line information associated with this capital asset
1259         for (CapitalAccountingLines capitalAccountingLine : capitalAccountingLines) {
1260             capitalAsset.setDistributionAmountCode(capitalAccountingLine.getDistributionAmountCode());
1261             createCapitalAssetAccountingLinesDetails(capitalAccountingLine, capitalAsset);
1262         }
1263 
1264                     currentCapitalAssetInformation.add(capitalAsset);
1265                 }
1266 
1267     /**
1268      *
1269      *
1270      * @param capitalAccountingLine
1271      * @param capitalAsset
1272      */
1273     protected void createCapitalAssetAccountingLinesDetails(CapitalAccountingLines capitalAccountingLine, CapitalAssetInformation capitalAsset) {
1274         //now setup the account line information associated with this capital asset
1275         CapitalAssetAccountsGroupDetails capitalAssetAccountLine = new CapitalAssetAccountsGroupDetails();
1276         capitalAssetAccountLine.setDocumentNumber(capitalAsset.getDocumentNumber());
1277         capitalAssetAccountLine.setChartOfAccountsCode(capitalAccountingLine.getChartOfAccountsCode());
1278         capitalAssetAccountLine.setAccountNumber(capitalAccountingLine.getAccountNumber());
1279         capitalAssetAccountLine.setSubAccountNumber(capitalAccountingLine.getSubAccountNumber());
1280         capitalAssetAccountLine.setFinancialDocumentLineTypeCode(OLEConstants.SOURCE.equals(capitalAccountingLine.getLineType()) ? OLEConstants.SOURCE_ACCT_LINE_TYPE_CODE : OLEConstants.TARGET_ACCT_LINE_TYPE_CODE);
1281         capitalAssetAccountLine.setCapitalAssetAccountLineNumber(getNextAccountingLineNumber(capitalAccountingLine, capitalAsset));
1282         capitalAssetAccountLine.setCapitalAssetLineNumber(capitalAsset.getCapitalAssetLineNumber());
1283         capitalAssetAccountLine.setFinancialObjectCode(capitalAccountingLine.getFinancialObjectCode());
1284         capitalAssetAccountLine.setFinancialSubObjectCode(capitalAccountingLine.getFinancialSubObjectCode());
1285         capitalAssetAccountLine.setProjectCode(capitalAccountingLine.getProjectCode());
1286         capitalAssetAccountLine.setOrganizationReferenceId(capitalAccountingLine.getOrganizationReferenceId());
1287         capitalAssetAccountLine.setSequenceNumber(capitalAccountingLine.getSequenceNumber());
1288         capitalAssetAccountLine.setAmount(KualiDecimal.ZERO);
1289         capitalAsset.getCapitalAssetAccountsGroupDetails().add(capitalAssetAccountLine);
1290             }
1291 
1292     /**
1293      * calculates the next accounting line number for accounts details for each capital asset.
1294      * Goes through the current records and gets the last accounting line number.
1295      *
1296      * @param capitalAsset
1297      * @return nextAccountingLineNumber
1298      */
1299     protected Integer getNextAccountingLineNumber(CapitalAccountingLines capitalAccountingLine, CapitalAssetInformation capitalAsset) {
1300         Integer nextAccountingLineNumber = 0;
1301         List<CapitalAssetAccountsGroupDetails> capitalAssetAccountLines = capitalAsset.getCapitalAssetAccountsGroupDetails();
1302         for (CapitalAssetAccountsGroupDetails capitalAssetAccountLine : capitalAssetAccountLines) {
1303             nextAccountingLineNumber = capitalAssetAccountLine.getCapitalAssetAccountLineNumber();
1304         }
1305 
1306         return ++nextAccountingLineNumber;
1307     }
1308 
1309     /**
1310      * @param capitalAccountingLine
1311      * @param capitalAssetInformation
1312      * @return return existingCapitalAsset
1313      */
1314     protected CapitalAssetInformation getCapitalAssetCreated(CapitalAccountingLines capitalAccountingLine, List<CapitalAssetInformation> capitalAssetInformation) {
1315         CapitalAssetInformation existingCapitalAsset = null;
1316         if (ObjectUtils.isNull(capitalAssetInformation) ||capitalAssetInformation.size() <= 0) {
1317             return existingCapitalAsset;
1318         }
1319 
1320         for (CapitalAssetInformation capitalAsset : capitalAssetInformation) {
1321             List<CapitalAssetAccountsGroupDetails> groupAccountLines = capitalAsset.getCapitalAssetAccountsGroupDetails();
1322             for (CapitalAssetAccountsGroupDetails groupAccountLine : groupAccountLines) {
1323                 if (groupAccountLine.getCapitalAssetLineNumber().compareTo(capitalAsset.getCapitalAssetLineNumber()) == 0 &&
1324                         groupAccountLine.getSequenceNumber().compareTo(capitalAccountingLine.getSequenceNumber()) == 0 &&
1325                         groupAccountLine.getFinancialDocumentLineTypeCode().equals(OLEConstants.SOURCE.equals(capitalAccountingLine.getLineType()) ? OLEConstants.SOURCE_ACCT_LINE_TYPE_CODE : OLEConstants.TARGET_ACCT_LINE_TYPE_CODE) &&
1326                         groupAccountLine.getChartOfAccountsCode().equals(capitalAccountingLine.getChartOfAccountsCode()) &&
1327                         groupAccountLine.getAccountNumber().equals(capitalAccountingLine.getAccountNumber()) &&
1328                         groupAccountLine.getFinancialObjectCode().equals(capitalAccountingLine.getFinancialObjectCode())) {
1329                      return capitalAsset;
1330                 }
1331            }
1332         }
1333 
1334         return existingCapitalAsset;
1335     }
1336 
1337     /**
1338      *
1339      * @param capitalAccountingLine
1340      * @param capitalAssetInformation
1341      * @return capitalAsset
1342      */
1343     protected CapitalAssetInformation capitalAssetCreated(List<CapitalAccountingLines> capitalAccountingLines, List<CapitalAssetInformation> capitalAssetInformation) {
1344         CapitalAssetInformation existingCapitalAsset = null;
1345 
1346         if (ObjectUtils.isNull(capitalAssetInformation) && capitalAssetInformation.size() <= 0) {
1347             return existingCapitalAsset;
1348         }
1349 
1350         for (CapitalAccountingLines capitalAccountingLine : capitalAccountingLines) {
1351             existingCapitalAsset = getCapitalAssetCreated(capitalAccountingLine, capitalAssetInformation);
1352             if (ObjectUtils.isNotNull(existingCapitalAsset)) {
1353                 return existingCapitalAsset;
1354             }
1355         }
1356 
1357         return existingCapitalAsset;
1358     }
1359 
1360     /**
1361      *
1362      * @param capitalAccountingLine
1363      * @param capitalAssetInformation
1364      * @return modify capital asset
1365      */
1366     protected CapitalAssetInformation modifyCapitalAssetCreated(CapitalAccountingLines capitalAccountingLine, List<CapitalAssetInformation> capitalAssetInformation) {
1367         CapitalAssetInformation existingCapitalAsset = null;
1368 
1369         if (ObjectUtils.isNull(capitalAssetInformation) && capitalAssetInformation.size() <= 0) {
1370             return existingCapitalAsset;
1371         }
1372 
1373         for (CapitalAssetInformation capitalAsset : capitalAssetInformation) {
1374             if (OLEConstants.CapitalAssets.CAPITAL_ASSET_MODIFY_ACTION_INDICATOR.equals(capitalAsset.getCapitalAssetActionIndicator()) &&
1375                     ObjectUtils.isNull(capitalAsset.getCapitalAssetNumber())) {
1376                 List<CapitalAssetAccountsGroupDetails> groupAccountLines = capitalAsset.getCapitalAssetAccountsGroupDetails();
1377                 for (CapitalAssetAccountsGroupDetails groupAccountLine : groupAccountLines) {
1378                     if (groupAccountLine.getCapitalAssetLineNumber().compareTo(capitalAsset.getCapitalAssetLineNumber()) == 0 &&
1379                             groupAccountLine.getSequenceNumber().compareTo(capitalAccountingLine.getSequenceNumber()) == 0 &&
1380                             groupAccountLine.getFinancialDocumentLineTypeCode().equals(OLEConstants.SOURCE.equals(capitalAccountingLine.getLineType()) ? OLEConstants.SOURCE_ACCT_LINE_TYPE_CODE : OLEConstants.TARGET_ACCT_LINE_TYPE_CODE) &&
1381                             groupAccountLine.getChartOfAccountsCode().equals(capitalAccountingLine.getChartOfAccountsCode()) &&
1382                             groupAccountLine.getAccountNumber().equals(capitalAccountingLine.getAccountNumber()) &&
1383                             groupAccountLine.getFinancialObjectCode().equals(capitalAccountingLine.getFinancialObjectCode())) {
1384                         return capitalAsset;
1385                     }
1386                 }
1387             }
1388         }
1389 
1390         return existingCapitalAsset;
1391     }
1392 
1393 
1394     /**
1395      *
1396      * @param capitalAssetInformation
1397      * @param existingCapitalAssetInformation
1398      * @return
1399      */
1400     protected Integer getNextCapitalAssetLineNumber(KualiAccountingDocumentFormBase kualiAccountingDocumentFormBase) {
1401         int nextCapitalAssetLineNumber = 1;
1402         CapitalAssetInformationDocumentBase caidb = (CapitalAssetInformationDocumentBase) kualiAccountingDocumentFormBase.getFinancialDocument();
1403         nextCapitalAssetLineNumber = caidb.getNextCapitalAssetLineNumber();
1404         caidb.setNextCapitalAssetLineNumber(nextCapitalAssetLineNumber+1);
1405 
1406         return nextCapitalAssetLineNumber;
1407     }
1408 
1409     /**
1410      *
1411      * @param kadfb
1412      * @param existingCapitalAsset
1413      * @return true if accounting line amount equals to capital asset amount, else false.
1414      */
1415     protected boolean accountingLinesAmountDistributed(List<CapitalAccountingLines> capitalAccountingLines, CapitalAssetInformation existingCapitalAsset) {
1416 
1417         KualiDecimal accountingLineAmount = KualiDecimal.ZERO;
1418         for (CapitalAccountingLines capitalAccountingLine : capitalAccountingLines) {
1419             accountingLineAmount = accountingLineAmount.add(capitalAccountingLine.getAmount().abs());
1420         }
1421 
1422         KualiDecimal capitalAssetsAmount = existingCapitalAsset.getCapitalAssetLineAmount();
1423 
1424         return accountingLineAmount.equals(capitalAssetsAmount);
1425         }
1426 
1427     /**
1428      *
1429      * @param kadfb
1430      * @param capitalAssetsInformation
1431      * @return true if accounting line amount equals to capital asset amount, else false.
1432      */
1433     protected boolean capitalAccountingLineAmountDistributed(CapitalAccountingLines capitalAccountingLine, List<CapitalAssetInformation> capitalAssetsInformation) {
1434         KualiDecimal amountDistributed = KualiDecimal.ZERO;
1435         for (CapitalAssetInformation capitalAsset : capitalAssetsInformation) {
1436             amountDistributed = amountDistributed.add(getAccountingLineAmount(capitalAsset, capitalAccountingLine));
1437         }
1438 
1439         KualiDecimal capitalAccountingLineAmount = capitalAccountingLine.getAmount();
1440 
1441         return amountDistributed.equals(capitalAccountingLineAmount);
1442     }
1443 
1444     /**
1445      *
1446      *
1447      * @param capitalAsset
1448      * @param capitalAccountingLine
1449      * @return accountLineAmount
1450      */
1451     protected KualiDecimal getAccountingLineAmount(CapitalAssetInformation capitalAsset, CapitalAccountingLines capitalAccountingLine) {
1452         KualiDecimal accountLineAmount = KualiDecimal.ZERO;
1453 
1454         List<CapitalAssetAccountsGroupDetails> groupAccountLines = capitalAsset.getCapitalAssetAccountsGroupDetails();
1455 
1456         for (CapitalAssetAccountsGroupDetails groupAccountLine : groupAccountLines) {
1457             if (groupAccountLine.getCapitalAssetLineNumber().compareTo(capitalAsset.getCapitalAssetLineNumber()) == 0 &&
1458                     groupAccountLine.getSequenceNumber().compareTo(capitalAccountingLine.getSequenceNumber()) == 0 &&
1459                     groupAccountLine.getFinancialDocumentLineTypeCode().equals(OLEConstants.SOURCE.equals(capitalAccountingLine.getLineType()) ? OLEConstants.SOURCE_ACCT_LINE_TYPE_CODE : OLEConstants.TARGET_ACCT_LINE_TYPE_CODE) &&
1460                     groupAccountLine.getChartOfAccountsCode().equals(capitalAccountingLine.getChartOfAccountsCode()) &&
1461                     groupAccountLine.getAccountNumber().equals(capitalAccountingLine.getAccountNumber()) &&
1462                     groupAccountLine.getFinancialObjectCode().equals(capitalAccountingLine.getFinancialObjectCode())) {
1463                  return groupAccountLine.getAmount();
1464             }
1465         }
1466 
1467         return accountLineAmount;
1468     }
1469 
1470     /**
1471      * adds any missing capital accounting line details as an accounting line into the collection of
1472      * accounting lines for this capital asset based on the action type.
1473      *
1474      * @param capitalAccountingLines
1475      * @param existingCapitalAsset
1476      */
1477     protected void addMissingAccountingLinesToCapitalAsset(List<CapitalAccountingLines> capitalAccountingLines, CapitalAssetInformation existingCapitalAsset) {
1478         List<CapitalAssetAccountsGroupDetails> groupAccountLines = existingCapitalAsset.getCapitalAssetAccountsGroupDetails();
1479 
1480         for (CapitalAccountingLines capitalAccountingLine : capitalAccountingLines) {
1481             if (capitalAccountingLineMissing(capitalAccountingLine, groupAccountLines, existingCapitalAsset.getCapitalAssetLineNumber())) {
1482                 createCapitalAssetAccountingLinesDetails(capitalAccountingLine, existingCapitalAsset);
1483             }
1484         }
1485     }
1486 
1487     /**
1488      * creates a new tag/location details record and adds to the collection for capital asset
1489      * @param capitalAsset
1490      */
1491     protected void createCapitalAssetInformationDetail(CapitalAssetInformation capitalAsset) {
1492         CapitalAssetInformationDetail assetDetail = new CapitalAssetInformationDetail();
1493         assetDetail.setDocumentNumber(capitalAsset.getDocumentNumber());
1494         assetDetail.setCapitalAssetLineNumber(capitalAsset.getCapitalAssetLineNumber());
1495         assetDetail.setItemLineNumber(getNextLineItemNumber(capitalAsset));
1496         capitalAsset.getCapitalAssetInformationDetails().add(0,assetDetail);
1497     }
1498 
1499     /**
1500      * calculates the next line item number for tag/location details for each capital asset.
1501      * Goes through the current records and gets the last number.
1502      *
1503      * @param capitalAsset
1504      * @return nextLineNumber
1505      */
1506     protected Integer getNextLineItemNumber(CapitalAssetInformation capitalAsset) {
1507         Integer nextLineNumber = 0;
1508         List<CapitalAssetInformationDetail> capitalAssetDetails = capitalAsset.getCapitalAssetInformationDetails();
1509         for (CapitalAssetInformationDetail capitalAssetDetail : capitalAssetDetails) {
1510             nextLineNumber = capitalAssetDetail.getItemLineNumber();
1511         }
1512 
1513         return ++nextLineNumber;
1514     }
1515 
1516     protected void calculateRemainingDistributedAmount(CapitalAccountingLinesFormBase calfb, List<CapitalAssetInformation> capitalAssetInformation) {
1517         calfb.setCreatedAssetsControlAmount(calfb.getSystemControlAmount());
1518 
1519         //get amount allocated so far....or the system control remainder amount field.
1520         for (CapitalAssetInformation capitalAsset : capitalAssetInformation) {
1521             calfb.setCreatedAssetsControlAmount(calfb.getCreatedAssetsControlAmount().subtract(capitalAsset.getCapitalAssetLineAmount()));
1522         }
1523     }
1524 
1525     /**
1526      * checks the current list of accounting lines created for the capital asset against the given
1527      * capital accounting line and returns true or false
1528      *
1529      * @param capitalAccountingLine
1530      * @param groupAccountLines
1531      * @param capitalAssetLineNumber
1532      * @return true if line exists else return false
1533      */
1534     protected boolean capitalAccountingLineMissing(CapitalAccountingLines capitalAccountingLine, List<CapitalAssetAccountsGroupDetails> groupAccountLines, Integer capitalAssetLineNumber) {
1535         boolean missing = true;
1536 
1537         for (CapitalAssetAccountsGroupDetails groupAccountLine : groupAccountLines) {
1538             if (groupAccountLine.getCapitalAssetLineNumber().compareTo(capitalAssetLineNumber) == 0 &&
1539                     groupAccountLine.getSequenceNumber().compareTo(capitalAccountingLine.getSequenceNumber()) == 0 &&
1540                     groupAccountLine.getFinancialDocumentLineTypeCode().equals(OLEConstants.SOURCE.equals(capitalAccountingLine.getLineType()) ? OLEConstants.SOURCE_ACCT_LINE_TYPE_CODE : OLEConstants.TARGET_ACCT_LINE_TYPE_CODE) &&
1541                     groupAccountLine.getChartOfAccountsCode().equals(capitalAccountingLine.getChartOfAccountsCode()) &&
1542                     groupAccountLine.getAccountNumber().equals(capitalAccountingLine.getAccountNumber()) &&
1543                     groupAccountLine.getFinancialObjectCode().equals(capitalAccountingLine.getFinancialObjectCode())) {
1544                  return false;
1545             }
1546         }
1547 
1548         return missing;
1549     }
1550 
1551     /**
1552      * sets the capital accounting lines select and amount distributed values to true if
1553      * there are capital asset records for a given capital accounting line. The system control
1554      * amount and system control remaining amounts are calculated here.
1555      *
1556      * @param calfb
1557      */
1558     protected void checkCapitalAccountingLinesSelected(CapitalAccountingLinesFormBase calfb) {
1559         CapitalAccountingLinesDocumentBase caldb = (CapitalAccountingLinesDocumentBase) calfb.getFinancialDocument();
1560 
1561         List<CapitalAccountingLines> capitalAccountingLines = caldb.getCapitalAccountingLines();
1562 
1563         KualiAccountingDocumentFormBase kadfb = calfb;
1564 
1565         List<CapitalAssetInformation> currentCapitalAssetInformation =  this.getCurrentCapitalAssetInformationObject(kadfb);
1566 
1567         calfb.setCreatedAssetsControlAmount(KualiDecimal.ZERO);
1568         calfb.setSystemControlAmount(KualiDecimal.ZERO);
1569 
1570         for (CapitalAccountingLines capitalAccountingLine : capitalAccountingLines) {
1571             if (capitalAccountingLine.isSelectLine()) {
1572                 calfb.setSystemControlAmount(calfb.getSystemControlAmount().add(capitalAccountingLine.getAmount()));
1573             }
1574 
1575             if (currentCapitalAssetInformation.size() <= 0) {
1576                 capitalAccountingLine.setAccountLinePercent(null);
1577                 capitalAccountingLine.setAmountDistributed(false);
1578                 capitalAccountingLine.setSelectLine(false);
1579             } else {
1580                    CapitalAssetInformation existingCapitalAsset = getCapitalAssetCreated(capitalAccountingLine, currentCapitalAssetInformation);
1581                    if (ObjectUtils.isNotNull(existingCapitalAsset)) {
1582                        capitalAccountingLine.setSelectLine(true);
1583                    } else {
1584                        capitalAccountingLine.setAccountLinePercent(null);
1585                        capitalAccountingLine.setSelectLine(false);
1586             }
1587         }
1588 
1589             if (capitalAccountingLineAmountDistributed(capitalAccountingLine, currentCapitalAssetInformation)) {
1590                 capitalAccountingLine.setAmountDistributed(true);
1591             } else {
1592                 capitalAccountingLine.setAmountDistributed(false);
1593             }
1594         }
1595 
1596         KualiDecimal capitalAssetsTotal = KualiDecimal.ZERO;
1597 
1598         //get amount allocated so far....or the system control remainder amount field.
1599         for (CapitalAssetInformation capitalAsset : currentCapitalAssetInformation) {
1600             capitalAssetsTotal= capitalAssetsTotal.add(capitalAsset.getCapitalAssetLineAmount());
1601         }
1602 
1603         calfb.setCreatedAssetsControlAmount(calfb.getSystemControlAmount().subtract(capitalAssetsTotal));
1604     }
1605 
1606     /**
1607      * sets the capital assets screens for create and modify and accounting lines
1608      * for capitalization screen as open. If accounting lines for capitalizataion list is
1609      * not empty then set "Accounting Lines for Capitalization" tab to open else set to close.
1610      * If capital asset with capital asset action indicator = 'C' then set "Create Capital Asset"
1611      * tab to open else set to close
1612      * If capital asset with capital asset action indicator = 'M' then set "Modify Capital Asset"
1613      * tab to open else set to close
1614      *
1615      * @param form
1616      */
1617     protected void setTabStatesForCapitalAssets(ActionForm form) {
1618         KualiForm kualiForm = (KualiForm) form;
1619 
1620         CapitalAccountingLinesFormBase capitalAccountingLinesFormBase = (CapitalAccountingLinesFormBase) form;
1621 
1622 
1623         Map<String, String> tabStates = kualiForm.getTabStates();
1624         Map<String, String> newTabStates = new HashMap<String, String>();
1625 
1626         CapitalAssetInformationFormBase capitalAssetInformationFormBase = (CapitalAssetInformationFormBase) form;
1627         CapitalAccountingLinesDocumentBase caldb = (CapitalAccountingLinesDocumentBase) capitalAssetInformationFormBase.getFinancialDocument();
1628 
1629         //generated tab key for the three tabs
1630         String tabIdForAccountingLinesForCapitalization = WebUtils.generateTabKey(OLEConstants.CapitalAssets.ACCOUNTING_LINES_FOR_CAPITALIZATION_TAB_TITLE);
1631         String tabIdForCreateCapitalAsset = WebUtils.generateTabKey(OLEConstants.CapitalAssets.CREATE_CAPITAL_ASSETS_TAB_TITLE);
1632         String tabIdForModifyCapitalAsset = WebUtils.generateTabKey(OLEConstants.CapitalAssets.MODIFY_CAPITAL_ASSETS_TAB_TITLE);
1633 
1634         tabStates.remove(tabIdForAccountingLinesForCapitalization);
1635         tabStates.remove(tabIdForCreateCapitalAsset);
1636         tabStates.remove(tabIdForModifyCapitalAsset);
1637 
1638         //if there are any capital accounting lines for capitalization exists then
1639         if (caldb.getCapitalAccountingLines().size() > 0) {
1640             tabStates.put(tabIdForAccountingLinesForCapitalization, OLEConstants.CapitalAssets.CAPITAL_ASSET_TAB_STATE_OPEN);
1641         }
1642         else {
1643             tabStates.put(tabIdForAccountingLinesForCapitalization, OLEConstants.CapitalAssets.CAPITAL_ASSET_TAB_STATE_CLOSE);
1644         }
1645 
1646         if (checkCreateAssetsExist(capitalAccountingLinesFormBase)) {
1647             tabStates.put(tabIdForCreateCapitalAsset, OLEConstants.CapitalAssets.CAPITAL_ASSET_TAB_STATE_OPEN);
1648         }
1649         else {
1650             tabStates.put(tabIdForCreateCapitalAsset, OLEConstants.CapitalAssets.CAPITAL_ASSET_TAB_STATE_CLOSE);
1651         }
1652 
1653         if (checkModifyAssetsExist(capitalAccountingLinesFormBase)) {
1654             tabStates.put(tabIdForModifyCapitalAsset, OLEConstants.CapitalAssets.CAPITAL_ASSET_TAB_STATE_OPEN);
1655         }
1656         else {
1657             tabStates.put(tabIdForModifyCapitalAsset, OLEConstants.CapitalAssets.CAPITAL_ASSET_TAB_STATE_CLOSE);
1658         }
1659 
1660         kualiForm.setTabStates(tabStates);
1661     }
1662 
1663     /**
1664      *
1665      * @param capitalAccountingLinesFormBase
1666      * @return true if a capital asset with capital asset action indicator = 'C' else false;
1667      */
1668     protected boolean checkCreateAssetsExist(CapitalAccountingLinesFormBase capitalAccountingLinesFormBase) {
1669         boolean exists = false;
1670 
1671         CapitalAssetInformationDocumentBase capitalAssetInformationDocumentBase = (CapitalAssetInformationDocumentBase) capitalAccountingLinesFormBase.getFinancialDocument();
1672 
1673         List<CapitalAssetInformation> capitalAssets = capitalAssetInformationDocumentBase.getCapitalAssetInformation();
1674         for (CapitalAssetInformation capitalAsset : capitalAssets) {
1675             if (OLEConstants.CapitalAssets.CAPITAL_ASSET_CREATE_ACTION_INDICATOR.equals(capitalAsset.getCapitalAssetActionIndicator())) {
1676                 return true;
1677             }
1678         }
1679 
1680         return exists;
1681     }
1682 
1683     /**
1684      *
1685      * @param capitalAccountingLinesFormBase
1686      * @return true if a capital asset with capital asset action indicator = 'C' else false;
1687      */
1688     protected boolean checkModifyAssetsExist(CapitalAccountingLinesFormBase capitalAccountingLinesFormBase) {
1689         boolean exists = false;
1690 
1691         CapitalAssetInformationDocumentBase capitalAssetInformationDocumentBase = (CapitalAssetInformationDocumentBase) capitalAccountingLinesFormBase.getFinancialDocument();
1692 
1693         List<CapitalAssetInformation> capitalAssets = capitalAssetInformationDocumentBase.getCapitalAssetInformation();
1694         for (CapitalAssetInformation capitalAsset : capitalAssets) {
1695             if (OLEConstants.CapitalAssets.CAPITAL_ASSET_MODIFY_ACTION_INDICATOR.equals(capitalAsset.getCapitalAssetActionIndicator())) {
1696                 return true;
1697             }
1698         }
1699 
1700         return exists;
1701     }
1702 
1703     /**
1704      *
1705      * @param mapping
1706      * @param form
1707      * @param request
1708      * @param response
1709      * @return actionForward
1710      * @throws Exception
1711      */
1712     public ActionForward redistributeCreateCapitalAssetAmount(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
1713         LOG.debug("redistributeCreateCapitalAssetAmount() - start");
1714 
1715         KualiDecimal remainingAmountToDistribute = KualiDecimal.ZERO;
1716 
1717         CapitalAccountingLinesFormBase calfb = (CapitalAccountingLinesFormBase) form;
1718         CapitalAccountingLinesDocumentBase caldb = (CapitalAccountingLinesDocumentBase) calfb.getFinancialDocument();
1719         List<CapitalAccountingLines> selectedCapitalAccountingLines = new ArrayList<CapitalAccountingLines>();
1720         String distributionCode = calfb.getCapitalAccountingLine().getDistributionCode();
1721 
1722         remainingAmountToDistribute = getRemainingAmounToDistribute(selectedCapitalAccountingLines, form);
1723 
1724         KualiAccountingDocumentFormBase kualiAccountingDocumentFormBase = (KualiAccountingDocumentFormBase) form;
1725         List<CapitalAssetInformation> capitalAssetInformation = this.getCurrentCapitalAssetInformationObject(kualiAccountingDocumentFormBase);
1726 
1727         //run the process to redistribute the accounting line amount to the capital assets.
1728         redistributeEqualAmountsForAccountingLineForCreateAssets(selectedCapitalAccountingLines, capitalAssetInformation, remainingAmountToDistribute);
1729 
1730         redistributeIndividualAmountsForAccountingLinesForCreateAssets(selectedCapitalAccountingLines, capitalAssetInformation);
1731 
1732         //adjust any variance from capital accounting lines to the distributed accounting lines amounts....
1733         adjustCapitalAssetsAccountingLinesAmounts(selectedCapitalAccountingLines, capitalAssetInformation);
1734 
1735         //now perform calculations of adjusting variance when asset amounts are odd
1736     /////    adjustVarianceOnCapitalAssets(capitalAssetInformation);
1737 
1738         checkCapitalAccountingLinesSelected(calfb);
1739 
1740         return mapping.findForward(OLEConstants.MAPPING_BASIC);
1741     }
1742 
1743     /**
1744      * redistributes the capital asset amount for the modify capital asset lines.
1745      * @param mapping
1746      * @param form
1747      * @param request
1748      * @param response
1749      * @return ActionForward
1750      * @throws Exception
1751      */
1752     public ActionForward redistributeModifyCapitalAssetAmount(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
1753         LOG.debug("redistributeModifyCapitalAssetAmount() - start");
1754 
1755         KualiDecimal remainingAmountToDistribute = KualiDecimal.ZERO;
1756         CapitalAccountingLinesFormBase calfb = (CapitalAccountingLinesFormBase) form;
1757         CapitalAccountingLinesDocumentBase caldb = (CapitalAccountingLinesDocumentBase) calfb.getFinancialDocument();
1758         List<CapitalAccountingLines> selectedCapitalAccountingLines = new ArrayList<CapitalAccountingLines>();
1759         String distributionCode = calfb.getCapitalAccountingLine().getDistributionCode();
1760 
1761         remainingAmountToDistribute = getRemainingAmounToDistribute(selectedCapitalAccountingLines, form);
1762 
1763         KualiAccountingDocumentFormBase kualiAccountingDocumentFormBase = (KualiAccountingDocumentFormBase) form;
1764         List<CapitalAssetInformation> capitalAssetInformation = this.getCurrentCapitalAssetInformationObject(kualiAccountingDocumentFormBase);
1765 
1766         //run the process to redistribute the accounting line amount to the capital assets.
1767         redistributeAmountsForAccountingsLineForModifyAssets(selectedCapitalAccountingLines, capitalAssetInformation, remainingAmountToDistribute);
1768 
1769         redistributeIndividualAmountsForAccountingLinesForModifyAssets(selectedCapitalAccountingLines, capitalAssetInformation);
1770 
1771         //now process any capital assets that has distribution set to "by amount"
1772         redistributeAmountsForAccountingsLineForModifyAssetsByAmounts(selectedCapitalAccountingLines, capitalAssetInformation, remainingAmountToDistribute);
1773 
1774         //adjust any variance from capital accounting lines to the distributed accounting lines amounts....
1775         adjustCapitalAssetsAccountingLinesAmounts(selectedCapitalAccountingLines, capitalAssetInformation);
1776 
1777         processRemainingCapitalAssetInfo(form, capitalAssetInformation);
1778 
1779         //redistribute capital asset amount to its group accounting lines on refresh
1780         DistributeCapitalAssetAmountToGroupAccountingLines((KualiAccountingDocumentFormBase) form);
1781 
1782         checkCapitalAccountingLinesSelected(calfb);
1783 
1784         return mapping.findForward(OLEConstants.MAPPING_BASIC);
1785     }
1786 
1787     /**
1788      * Calculates the remaining amount to distribute by taking selecte capital accounting lines
1789      * and subtracting the allocated capital asset accounting lines amounts totals.
1790      *
1791      * @param selectedCapitalAccountingLines
1792      * @param form
1793      * @return remainingAmountToDistribute
1794      */
1795     protected KualiDecimal getRemainingAmounToDistribute(List<CapitalAccountingLines> selectedCapitalAccountingLines, ActionForm form) {
1796         KualiDecimal remainingAmountToDistribute = KualiDecimal.ZERO;
1797         KualiDecimal capitalAccountsAmountToDistribute = KualiDecimal.ZERO;
1798         KualiDecimal capitalAssetsAllocatedAmount = KualiDecimal.ZERO;
1799 
1800         CapitalAccountingLinesFormBase calfb = (CapitalAccountingLinesFormBase) form;
1801 
1802         CapitalAssetInformationDocumentBase capitalAssetInformationDocumentBase = (CapitalAssetInformationDocumentBase) calfb.getFinancialDocument();
1803         List<CapitalAssetInformation> capitalAssets = capitalAssetInformationDocumentBase.getCapitalAssetInformation();
1804 
1805         CapitalAccountingLinesDocumentBase caldb = (CapitalAccountingLinesDocumentBase) calfb.getFinancialDocument();
1806         List<CapitalAccountingLines> capitalAccountingLines = caldb.getCapitalAccountingLines();
1807 
1808         for (CapitalAccountingLines capitalAccountingLine : capitalAccountingLines) {
1809             if (capitalAccountingLine.isSelectLine()  && !capitalAccountingLine.isAmountDistributed()) {
1810                 selectedCapitalAccountingLines.add(capitalAccountingLine);
1811                 capitalAccountsAmountToDistribute = capitalAccountsAmountToDistribute.add(capitalAccountingLine.getAmount());
1812                 capitalAssetsAllocatedAmount = capitalAssetsAllocatedAmount.add(getCapitalAssetsAmountAllocated(capitalAssets, capitalAccountingLine));
1813             }
1814         }
1815 
1816         remainingAmountToDistribute = capitalAccountsAmountToDistribute.subtract(capitalAssetsAllocatedAmount);
1817 
1818         return remainingAmountToDistribute;
1819     }
1820 
1821     /**
1822      * Removes any matching accounting line in capital asset records
1823      * whenever an accounting line is removed.
1824      * @param financialDocumentForm
1825      * @param line
1826      */
1827     protected void deleteCapitalAssetLines(KualiAccountingDocumentFormBase financialDocumentForm, AccountingLine accountingLine) {
1828         CapitalAssetInformationDocumentBase capitalAssetInformationDocumentBase = (CapitalAssetInformationDocumentBase) financialDocumentForm.getFinancialDocument();
1829 
1830         List<CapitalAssetInformation> removalCaiList = new ArrayList<CapitalAssetInformation>();
1831 
1832         List<CapitalAssetInformation> capitalAssets = capitalAssetInformationDocumentBase.getCapitalAssetInformation();
1833         for (CapitalAssetInformation capitalAsset : capitalAssets) {
1834             removeDistributedAccountingLine(capitalAsset, accountingLine);
1835             if (capitalAsset.getCapitalAssetAccountsGroupDetails().size() == 0) {
1836                 capitalAsset.getCapitalAssetInformationDetails().clear();
1837                     removalCaiList.add(capitalAsset);
1838             }
1839         }
1840 
1841         //if the removal list is not empty, remove these bunch of capital asset records
1842         //for that accounting line.
1843         if (ObjectUtils.isNotNull(removalCaiList)) {
1844             capitalAssets.removeAll(removalCaiList);
1845         }
1846     }
1847 
1848     /**
1849      * Removes the matching accounting line that has been distributed to the capital asset
1850      *
1851      * @param capitalAsset
1852      * @param accountingLine
1853      */
1854     protected void removeDistributedAccountingLine(CapitalAssetInformation capitalAsset, AccountingLine accountingLine) {
1855         List<CapitalAssetAccountsGroupDetails> groupAccountLines = capitalAsset.getCapitalAssetAccountsGroupDetails();
1856         CapitalAssetAccountsGroupDetails accountLineToDelete = null;
1857 
1858         for (CapitalAssetAccountsGroupDetails groupAccountLine : groupAccountLines) {
1859             if (groupAccountLine.getSequenceNumber().compareTo(accountingLine.getSequenceNumber()) == 0 &&
1860                     groupAccountLine.getFinancialDocumentLineTypeCode().equals(groupAccountLine.getFinancialDocumentLineTypeCode()) &&
1861                     groupAccountLine.getChartOfAccountsCode().equals(accountingLine.getChartOfAccountsCode()) &&
1862                     groupAccountLine.getAccountNumber().equals(accountingLine.getAccountNumber()) &&
1863                     groupAccountLine.getFinancialObjectCode().equals(accountingLine.getFinancialObjectCode())) {
1864                 accountLineToDelete = groupAccountLine;
1865                 break;
1866             }
1867         }
1868 
1869         if (ObjectUtils.isNotNull(accountLineToDelete)) {
1870             capitalAsset.setCapitalAssetLineAmount(capitalAsset.getCapitalAssetLineAmount().subtract(accountLineToDelete.getAmount()));
1871             groupAccountLines.remove(accountLineToDelete);
1872         }
1873     }
1874 
1875     /**
1876      * add detail lines into the given capital asset information
1877      *
1878      * @param capitalAssetInformation the given capital asset information
1879      */
1880     protected void addCapitalAssetInfoDetailLines(CapitalAssetInformation capitalAssetInformation) {
1881         LOG.debug("addCapitalAssetInfoDetailLines() - start");
1882 
1883         if (ObjectUtils.isNull(capitalAssetInformation)) {
1884             return;
1885         }
1886 
1887         Integer quantity = capitalAssetInformation.getCapitalAssetQuantity();
1888         if (quantity == null || quantity <= 0) {
1889             String errorPath = OLEPropertyConstants.DOCUMENT + "." + OLEPropertyConstants.CAPITAL_ASSET_INFORMATION;
1890             GlobalVariables.getMessageMap().putError(errorPath, OLEKeyConstants.ERROR_INVALID_CAPITAL_ASSET_QUANTITY);
1891             return;
1892         }
1893 
1894         List<CapitalAssetInformationDetail> detailLines = capitalAssetInformation.getCapitalAssetInformationDetails();
1895         Integer nextItemLineNumber = 0;
1896 
1897         if (ObjectUtils.isNotNull(detailLines) || detailLines.size() > 0) {
1898             for (CapitalAssetInformationDetail detailLine : detailLines) {
1899                 nextItemLineNumber  = detailLine.getItemLineNumber().intValue();
1900             }
1901         }
1902 
1903         // If details collection has old lines, this loop will add new lines to make the total equal to the quantity.
1904         for (int index = 1; detailLines.size() < quantity; index++) {
1905             CapitalAssetInformationDetail detailLine = new CapitalAssetInformationDetail();
1906             detailLine.setDocumentNumber(capitalAssetInformation.getDocumentNumber());
1907             detailLine.setCapitalAssetLineNumber(capitalAssetInformation.getCapitalAssetLineNumber());
1908             detailLine.setItemLineNumber(++nextItemLineNumber);
1909             detailLines.add(detailLine);
1910         }
1911     }
1912 
1913     /**
1914      * unchecks the capital accounting lines select when there are no capital assets created yet.
1915      *
1916      * @param calfb
1917      */
1918     protected void uncheckCapitalAccountingLinesSelected(CapitalAccountingLinesFormBase calfb) {
1919         CapitalAccountingLinesDocumentBase caldb = (CapitalAccountingLinesDocumentBase) calfb.getDocument();
1920 
1921         List<CapitalAccountingLines> capitalAccountingLines = caldb.getCapitalAccountingLines();
1922         KualiAccountingDocumentFormBase kadfb = calfb;
1923 
1924         List<CapitalAssetInformation> currentCapitalAssetInformation =  this.getCurrentCapitalAssetInformationObject(kadfb);
1925 
1926 
1927         List<CapitalAccountingLines> selectedCapitalAccountingLines = new ArrayList<CapitalAccountingLines>();
1928 
1929         for (CapitalAccountingLines capitalAccountingLine : capitalAccountingLines) {
1930             if (capitalAccountingLine.isSelectLine() && !capitalAccountingLine.isAmountDistributed()) {
1931                 calfb.setSystemControlAmount(calfb.getSystemControlAmount().add(capitalAccountingLine.getAmount()));
1932                 selectedCapitalAccountingLines.add(capitalAccountingLine);
1933             }
1934         }
1935 
1936         CapitalAssetInformation existingCapitalAsset = capitalAssetCreated(selectedCapitalAccountingLines, currentCapitalAssetInformation);
1937 
1938         for (CapitalAccountingLines capitalAccountingLine : selectedCapitalAccountingLines) {
1939             if (ObjectUtils.isNull(existingCapitalAsset)) {
1940                 capitalAccountingLine.setSelectLine(false);
1941             }
1942         }
1943     }
1944 
1945     /**
1946      * checks "select" check box on capital accounting lines if there are
1947      * corresponding capital asset records.
1948      *
1949      * @param calfb
1950      */
1951     protected void checkSelectForCapitalAccountingLines(CapitalAccountingLinesFormBase calfb) {
1952         CapitalAccountingLinesDocumentBase caldb = (CapitalAccountingLinesDocumentBase) calfb.getFinancialDocument();
1953         List<CapitalAccountingLines> capitalAccountingLines = caldb.getCapitalAccountingLines();
1954         KualiAccountingDocumentFormBase kadfb = calfb;
1955         List<CapitalAssetInformation> currentCapitalAssetInformation =  this.getCurrentCapitalAssetInformationObject(kadfb);
1956 
1957         for (CapitalAccountingLines capitalAccountingLine : capitalAccountingLines) {
1958             if (currentCapitalAssetInformation.size() <= 0) {
1959                 capitalAccountingLine.setSelectLine(false);
1960             } else {
1961                    CapitalAssetInformation existingCapitalAsset = getCapitalAssetCreated(capitalAccountingLine, currentCapitalAssetInformation);
1962                    if (ObjectUtils.isNotNull(existingCapitalAsset)) {
1963                        capitalAccountingLine.setSelectLine(true);
1964                    } else {
1965                        capitalAccountingLine.setSelectLine(false);
1966                    }
1967             }
1968         }
1969     }
1970 
1971     /**
1972      * gets the total of all accounting lines from that capital asset.
1973      *
1974      * @param capitalAssetInformation
1975      * @return accountingLinesTotalAmount
1976      */
1977     protected KualiDecimal getAccountingLinesTotalAmount(CapitalAssetInformation capitalAssetInformation) {
1978         KualiDecimal accountingLinesTotalAmount = KualiDecimal.ZERO;
1979 
1980         List<CapitalAssetAccountsGroupDetails> groupAccountLines = capitalAssetInformation.getCapitalAssetAccountsGroupDetails();
1981 
1982         for (CapitalAssetAccountsGroupDetails groupAccountLine : groupAccountLines) {
1983             accountingLinesTotalAmount = accountingLinesTotalAmount.add(groupAccountLine.getAmount());
1984         }
1985 
1986         return accountingLinesTotalAmount;
1987     }
1988 
1989     /**
1990      *
1991      *
1992      * @param capitalAccountingLine
1993      * @param capitalAssetInformation
1994      * @param actionTypeCode
1995      * @return accountingLinesTotalAmount
1996      */
1997     protected KualiDecimal getAccountingLinesDistributedAmount(CapitalAccountingLines capitalAccountingLine, List<CapitalAssetInformation> capitalAssetInformation, String actionTypeCode) {
1998         KualiDecimal accountingLinesTotalAmount = KualiDecimal.ZERO;
1999 
2000         for (CapitalAssetInformation capitalAsset : capitalAssetInformation) {
2001             if (capitalAsset.getCapitalAssetActionIndicator().equalsIgnoreCase(actionTypeCode)) {
2002                 accountingLinesTotalAmount = accountingLinesTotalAmount.add(getAccountingLineAmount(capitalAsset, capitalAccountingLine));
2003             }
2004         }
2005 
2006         return accountingLinesTotalAmount;
2007     }
2008 
2009     /**
2010      *
2011      * @param capitalAccountingLine
2012      * @param lastCapitalAsset
2013      * @param difference
2014      */
2015     protected void adjustAccountingLineAmountOnLastCapitalAsset(CapitalAccountingLines capitalAccountingLine, CapitalAssetInformation lastCapitalAsset, KualiDecimal difference) {
2016         List<CapitalAssetAccountsGroupDetails> groupAccountLines = lastCapitalAsset.getCapitalAssetAccountsGroupDetails();
2017 
2018         for (CapitalAssetAccountsGroupDetails groupAccountLine : groupAccountLines) {
2019             if (groupAccountLine.getCapitalAssetLineNumber().compareTo(lastCapitalAsset.getCapitalAssetLineNumber()) == 0 &&
2020                     groupAccountLine.getSequenceNumber().compareTo(capitalAccountingLine.getSequenceNumber()) == 0 &&
2021                     groupAccountLine.getFinancialDocumentLineTypeCode().equals(OLEConstants.SOURCE.equals(capitalAccountingLine.getLineType()) ? OLEConstants.SOURCE_ACCT_LINE_TYPE_CODE : OLEConstants.TARGET_ACCT_LINE_TYPE_CODE) &&
2022                     groupAccountLine.getChartOfAccountsCode().equals(capitalAccountingLine.getChartOfAccountsCode()) &&
2023                     groupAccountLine.getAccountNumber().equals(capitalAccountingLine.getAccountNumber()) &&
2024                     groupAccountLine.getFinancialObjectCode().equals(capitalAccountingLine.getFinancialObjectCode())) {
2025                 groupAccountLine.setAmount(groupAccountLine.getAmount().add(difference));
2026             }
2027         }
2028     }
2029 
2030     /**
2031      * when the user user hits refresh button, takes the amount in the amount field and
2032      * distributes to the group capital accounting lines for that asset only.
2033      * @param capitalAssetInformation
2034      * @param selectedCapitalAccountingLines
2035      */
2036     protected void redistributeToGroupAccountingLinesFromAssetsByAmounts(List<CapitalAccountingLines> selectedCapitalAccountingLines, CapitalAssetInformation capitalAsset) {
2037         KualiDecimal amountToDistribute = capitalAsset.getCapitalAssetLineAmount();
2038         KualiDecimal amountDistributed = KualiDecimal.ZERO;
2039 
2040         KualiDecimal totalCapitalAccountsAmount = getTotalCapitalAccountsAmounts(selectedCapitalAccountingLines);
2041 
2042         //to capture the last group accounting line to update its amount with any variance.
2043         CapitalAssetAccountsGroupDetails lastGroupAccountLine = new CapitalAssetAccountsGroupDetails();
2044 
2045         List<CapitalAssetAccountsGroupDetails> groupAccountLines = capitalAsset.getCapitalAssetAccountsGroupDetails();
2046         for (CapitalAssetAccountsGroupDetails groupAccountLine : groupAccountLines) {
2047             BigDecimal linePercent = getCapitalAccountingLinePercent(selectedCapitalAccountingLines, groupAccountLine, totalCapitalAccountsAmount);
2048             //found the accounting line
2049             lastGroupAccountLine = groupAccountLine;
2050 
2051             KualiDecimal groupAccountLineAmount = capitalAsset.getCapitalAssetLineAmount().multiply(new KualiDecimal(linePercent));
2052             groupAccountLine.setAmount(groupAccountLineAmount);
2053 
2054             //keep track of amoutn distributed so far.
2055             amountDistributed = amountDistributed.add(groupAccountLineAmount);
2056         }
2057 
2058         //add any variance in the amounts to the last group accounting line.
2059         lastGroupAccountLine.setAmount(lastGroupAccountLine.getAmount().add(amountToDistribute.subtract(amountDistributed)));
2060     }
2061 
2062     /**
2063      * calculates the total amount of the selected capital accounting lines
2064      *
2065      * @param capitalAccountingLines
2066      * @return total amount of the selected capital accounting lines.
2067      */
2068     protected KualiDecimal getTotalCapitalAccountsAmounts(List<CapitalAccountingLines> capitalAccountingLines) {
2069         KualiDecimal totalCapitalAccountsAmount = KualiDecimal.ZERO;
2070         for (CapitalAccountingLines capitalLine : capitalAccountingLines) {
2071             totalCapitalAccountsAmount = totalCapitalAccountsAmount.add(capitalLine.getAmount());
2072         }
2073 
2074         return totalCapitalAccountsAmount;
2075     }
2076 
2077     /**
2078      * This method redistributes the capital asset amount to its group accounting lines
2079      * based on the accounting line's percent.  Takes each capital assets amount and
2080      * distributes to the capital asset group accounting lines.
2081      *
2082      * @param kualiAccountingDocumentFormBase
2083      */
2084     protected void DistributeCapitalAssetAmountToGroupAccountingLines(KualiAccountingDocumentFormBase kualiAccountingDocumentFormBase) {
2085         CapitalAccountingLinesFormBase capitalAccountingLinesFormBase = (CapitalAccountingLinesFormBase) kualiAccountingDocumentFormBase;
2086         checkSelectForCapitalAccountingLines(capitalAccountingLinesFormBase);
2087 
2088         checkCapitalAccountingLinesSelected(capitalAccountingLinesFormBase);
2089         calculatePercentsForSelectedCapitalAccountingLines(capitalAccountingLinesFormBase);
2090 
2091         CapitalAccountingLinesDocumentBase caldb = (CapitalAccountingLinesDocumentBase) capitalAccountingLinesFormBase.getFinancialDocument();
2092         String distributionAmountCode = capitalAccountingLinesFormBase.getCapitalAccountingLine().getDistributionCode();
2093 
2094         List<CapitalAccountingLines> capitalAccountingLines = caldb.getCapitalAccountingLines();
2095 
2096         List<CapitalAccountingLines> selectedCapitalAccountingLines = new ArrayList<CapitalAccountingLines>();
2097 
2098         for (CapitalAccountingLines capitalAccountingLine : capitalAccountingLines) {
2099             if (capitalAccountingLine.isSelectLine() && !capitalAccountingLine.isAmountDistributed()) {
2100                 capitalAccountingLine.setDistributionAmountCode(OLEConstants.CapitalAssets.CAPITAL_ASSET_MODIFY_ACTION_INDICATOR);
2101                 selectedCapitalAccountingLines.add(capitalAccountingLine);
2102             }
2103         }
2104 
2105         List<CapitalAssetInformation> capitalAssetInformation = this.getCurrentCapitalAssetInformationObject(kualiAccountingDocumentFormBase);
2106 
2107         for (CapitalAssetInformation capitalAsset : capitalAssetInformation) {
2108         //redistribute the capital asset modify amount to the group accounting lines
2109         //based on amount.
2110             if (!capitalAssetAmountAlreadyDistributedToGroupAccountingLines(capitalAsset)) {
2111                 redistributeToGroupAccountingLinesFromAssetsByAmounts(selectedCapitalAccountingLines, capitalAsset);
2112             }
2113         }
2114     }
2115 
2116     /**
2117      * checks if the capital asset amount already distributed to its group accounting lines
2118      *
2119      * @param capitalAsset
2120      * @return true if amount already distributed else return false.
2121      */
2122     protected boolean capitalAssetAmountAlreadyDistributedToGroupAccountingLines(CapitalAssetInformation capitalAsset) {
2123         boolean amountDistributed = true;
2124 
2125         KualiDecimal capitalAssetAmount = capitalAsset.getCapitalAssetLineAmount();
2126         KualiDecimal totalAmountDistributed = KualiDecimal.ZERO;
2127 
2128         List<CapitalAssetAccountsGroupDetails> groupAccountLines = capitalAsset.getCapitalAssetAccountsGroupDetails();
2129         for (CapitalAssetAccountsGroupDetails groupAccountLine : groupAccountLines) {
2130             //keep track of amount distributed so far.
2131             totalAmountDistributed = totalAmountDistributed.add(groupAccountLine.getAmount());
2132         }
2133 
2134         return (capitalAssetAmount.compareTo(totalAmountDistributed) == 0);
2135     }
2136 }