View Javadoc
1   /*
2    * Copyright 2005 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;
17  
18  import java.util.ArrayList;
19  import java.util.Iterator;
20  import java.util.List;
21  
22  import org.kuali.ole.fp.businessobject.InternalBillingItem;
23  import org.kuali.ole.integration.cam.CapitalAssetManagementModuleService;
24  import org.kuali.ole.sys.OLEConstants;
25  import org.kuali.ole.sys.businessobject.AccountingLine;
26  import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntrySourceDetail;
27  import org.kuali.ole.sys.context.SpringContext;
28  import org.kuali.ole.sys.document.AmountTotaling;
29  import org.kuali.ole.sys.document.Correctable;
30  import org.kuali.ole.sys.document.service.DebitDeterminerService;
31  import org.kuali.rice.core.api.util.type.KualiDecimal;
32  import org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange;
33  import org.kuali.rice.kns.service.DataDictionaryService;
34  import org.kuali.rice.krad.document.Copyable;
35  import org.kuali.rice.krad.rules.rule.event.KualiDocumentEvent;
36  import org.kuali.rice.krad.rules.rule.event.SaveDocumentEvent;
37  
38  
39  /**
40   * This is the business object that represents the InternalBillingDocument in Kuali. This is a transactional document that will
41   * eventually post transactions to the G/L. It integrates with workflow and also contains two groupings of accounting lines: Expense
42   * and Income.
43   */
44  public class InternalBillingDocument extends CapitalAccountingLinesDocumentBase implements Copyable, Correctable, AmountTotaling {
45  
46      protected List items;
47      protected Integer nextItemLineNumber;
48  
49      protected transient CapitalAssetManagementModuleService capitalAssetManagementModuleService;
50  
51      /**
52       * Initializes the array lists and some basic info.
53       */
54      public InternalBillingDocument() {
55          super();
56          setItems(new ArrayList());
57          this.nextItemLineNumber = new Integer(1);
58      }
59  
60      /**
61       * This method determines if an accounting line is a debit accounting line by calling IsDebitUtils.isDebitConsideringSection().
62       * 
63       * @param transactionalDocument The document containing the accounting line being analyzed.
64       * @param accountingLine The accounting line being reviewed to determine if it is a debit line or not.
65       * @return True if the accounting line is a debit accounting line, false otherwise.
66       * @see IsDebitUtils#isDebitConsideringSection(FinancialDocumentRuleBase, FinancialDocument, AccountingLine)
67       * @see org.kuali.rice.krad.rule.AccountingLineRule#isDebit(org.kuali.rice.krad.document.FinancialDocument,
68       *      org.kuali.rice.krad.bo.AccountingLine)
69       */
70      @Override
71      public boolean isDebit(GeneralLedgerPendingEntrySourceDetail postable) {
72          DebitDeterminerService isDebitUtils = SpringContext.getBean(DebitDeterminerService.class);
73          return isDebitUtils.isDebitConsideringSection(this, (AccountingLine) postable);
74      }
75      
76      /**
77       * Adds a new item to the item list.
78       * 
79       * @param item
80       */
81      public void addItem(InternalBillingItem item) {
82          item.setItemSequenceId(this.nextItemLineNumber);
83          this.items.add(item);
84          this.nextItemLineNumber = new Integer(this.nextItemLineNumber.intValue() + 1);
85      }
86  
87      /**
88       * Retrieve a particular item at a given index in the list of items. For Struts, the requested item and any intervening ones are
89       * initialized if necessary.
90       * 
91       * @param index
92       * @return the item
93       */
94      public InternalBillingItem getItem(int index) {
95          while (getItems().size() <= index) {
96              getItems().add(new InternalBillingItem());
97          }
98          return (InternalBillingItem) getItems().get(index);
99      }
100 
101     /**
102      * @return Returns the items.
103      */
104     public List getItems() {
105         return items;
106     }
107 
108     /**
109      * Iterates through the list of items and sums up their totals.
110      * 
111      * @return the total
112      */
113     public KualiDecimal getItemTotal() {
114         KualiDecimal total = KualiDecimal.ZERO;
115         for (Iterator iterator = items.iterator(); iterator.hasNext();) {
116             total = total.add(((InternalBillingItem) iterator.next()).getTotal());
117         }
118         return total;
119     }
120 
121     /**
122      * Retrieves the next item line number.
123      * 
124      * @return The next available item line number
125      */
126     public Integer getNextItemLineNumber() {
127         return this.nextItemLineNumber;
128     }
129 
130     /**
131      * @param items
132      */
133     public void setItems(List items) {
134         this.items = items;
135     }
136 
137     /**
138      * Setter for OJB to get from database and JSP to maintain state in hidden fields. This property is also incremented by the
139      * <code>addItem</code> method.
140      * 
141      * @param nextItemLineNumber
142      */
143     public void setNextItemLineNumber(Integer nextItemLineNumber) {
144         this.nextItemLineNumber = nextItemLineNumber;
145     }
146 
147     /**
148      * @return "Income"
149      */
150     @Override
151     public String getSourceAccountingLinesSectionTitle() {
152         return OLEConstants.INCOME;
153     }
154 
155     /**
156      * @return "Expense"
157      */
158     @Override
159     public String getTargetAccountingLinesSectionTitle() {
160         return OLEConstants.EXPENSE;
161     }
162 
163     /**
164      * @see org.kuali.ole.sys.document.GeneralLedgerPostingDocumentBase#doRouteStatusChange()
165      */
166     @Override
167     public void doRouteStatusChange(DocumentRouteStatusChange statusChangeEvent) {
168         super.doRouteStatusChange(statusChangeEvent);
169         this.getCapitalAssetManagementModuleService().deleteDocumentAssetLocks(this);
170     }
171 
172 
173     /**
174      * @see org.kuali.rice.krad.document.DocumentBase#postProcessSave(org.kuali.rice.krad.rule.event.KualiDocumentEvent)
175      */
176     @Override
177     public void postProcessSave(KualiDocumentEvent event) {
178         super.postProcessSave(event);
179         if (!(event instanceof SaveDocumentEvent)) { // don't lock until they route
180             String documentTypeName = SpringContext.getBean(DataDictionaryService.class).getDocumentTypeNameByClass(this.getClass());
181             this.getCapitalAssetManagementModuleService().generateCapitalAssetLock(this, documentTypeName);
182         }
183     }
184 
185 
186     /**
187      * @return CapitalAssetManagementModuleService
188      */
189     CapitalAssetManagementModuleService getCapitalAssetManagementModuleService() {
190         if (capitalAssetManagementModuleService == null) {
191             capitalAssetManagementModuleService = SpringContext.getBean(CapitalAssetManagementModuleService.class);
192         }
193         return capitalAssetManagementModuleService;
194     }
195 }