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