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  
17  package org.kuali.ole.fp.document;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import org.kuali.ole.fp.businessobject.ProcurementCardHolder;
24  import org.kuali.ole.fp.businessobject.ProcurementCardSourceAccountingLine;
25  import org.kuali.ole.fp.businessobject.ProcurementCardTargetAccountingLine;
26  import org.kuali.ole.fp.businessobject.ProcurementCardTransactionDetail;
27  import org.kuali.ole.integration.cam.CapitalAssetManagementModuleService;
28  import org.kuali.ole.sys.businessobject.AccountingLine;
29  import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntrySourceDetail;
30  import org.kuali.ole.sys.businessobject.SourceAccountingLine;
31  import org.kuali.ole.sys.businessobject.TargetAccountingLine;
32  import org.kuali.ole.sys.context.SpringContext;
33  import org.kuali.ole.sys.document.AmountTotaling;
34  import org.kuali.ole.sys.document.service.DebitDeterminerService;
35  import org.kuali.rice.kew.api.KewApiConstants;
36  import org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange;
37  import org.kuali.rice.kns.service.DataDictionaryService;
38  import org.kuali.rice.krad.rules.rule.event.KualiDocumentEvent;
39  import org.kuali.rice.krad.rules.rule.event.SaveDocumentEvent;
40  
41  /**
42   * This is the Procurement Card Document Class. The procurement cards distributes expenses from clearing accounts. It is a two-sided
43   * document, but only target lines are displayed because source lines cannot be changed. Transaction, Card, and Vendor information
44   * are associated with the document to help better distribute the expense.
45   */
46  public class ProcurementCardDocument extends CapitalAccountingLinesDocumentBase implements AmountTotaling, CapitalAssetEditable {
47      protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ProcurementCardDocument.class);
48  
49      protected ProcurementCardHolder procurementCardHolder;
50  
51      protected List transactionEntries;
52      protected ProcurementCardTargetAccountingLine newTargetLine;
53      protected transient CapitalAssetManagementModuleService capitalAssetManagementModuleService;
54  
55      /**
56       * Default constructor.
57       */
58      public ProcurementCardDocument() {
59          super();
60          transactionEntries = new ArrayList<ProcurementCardTransactionDetail>();
61      }
62  
63      /**
64       * @return Returns the transactionEntries.
65       */
66      public List getTransactionEntries() {
67          return transactionEntries;
68      }
69  
70      /**
71       * @param transactionEntries The transactionEntries to set.
72       */
73      public void setTransactionEntries(List transactionEntries) {
74          this.transactionEntries = transactionEntries;
75      }
76  
77      /**
78       * Gets the procurementCardHolder attribute.
79       * 
80       * @return Returns the procurementCardHolder.
81       */
82      public ProcurementCardHolder getProcurementCardHolder() {
83          return procurementCardHolder;
84      }
85  
86      /**
87       * Sets the procurementCardHolder attribute value.
88       * 
89       * @param procurementCardHolder The procurementCardHolder to set.
90       */
91      public void setProcurementCardHolder(ProcurementCardHolder procurementCardHolder) {
92          this.procurementCardHolder = procurementCardHolder;
93      }
94  
95      /**
96       * Removes the target accounting line at the given index from the transaction detail entry.
97       * 
98       * @param index
99       */
100     public void removeTargetAccountingLine(int index) {
101         ProcurementCardTargetAccountingLine line = (ProcurementCardTargetAccountingLine) getTargetAccountingLines().get(index);
102 
103         for (Iterator iter = transactionEntries.iterator(); iter.hasNext();) {
104             ProcurementCardTransactionDetail transactionEntry = (ProcurementCardTransactionDetail) iter.next();
105             if (transactionEntry.getFinancialDocumentTransactionLineNumber().equals(line.getFinancialDocumentTransactionLineNumber())) {
106                 transactionEntry.getTargetAccountingLines().remove(line);
107             }
108         }
109     }
110 
111     /**
112      * Override to set the accounting line in the transaction detail object.
113      * 
114      * @see org.kuali.ole.sys.document.AccountingDocument#addSourceAccountingLine(SourceAccountingLine)
115      */
116     
117     public void addSourceAccountingLine(SourceAccountingLine sourceLine) {
118         ProcurementCardSourceAccountingLine line = (ProcurementCardSourceAccountingLine) sourceLine;
119 
120         line.setSequenceNumber(this.getNextSourceLineNumber());
121 
122         for (Iterator iter = transactionEntries.iterator(); iter.hasNext();) {
123             ProcurementCardTransactionDetail transactionEntry = (ProcurementCardTransactionDetail) iter.next();
124             if (transactionEntry.getFinancialDocumentTransactionLineNumber().equals(line.getFinancialDocumentTransactionLineNumber())) {
125                 transactionEntry.getSourceAccountingLines().add(line);
126             }
127         }
128 
129         this.nextSourceLineNumber = new Integer(this.getNextSourceLineNumber().intValue() + 1);
130     }
131 
132     /**
133      * Override to set the accounting line in the transaction detail object.
134      * 
135      * @see org.kuali.ole.sys.document.AccountingDocument#addTargetAccountingLine(TargetAccountingLine)
136      */
137     @Override
138     public void addTargetAccountingLine(TargetAccountingLine targetLine) {
139         ProcurementCardTargetAccountingLine line = (ProcurementCardTargetAccountingLine) targetLine;
140 
141         line.setSequenceNumber(this.getNextTargetLineNumber());
142 
143         for (Iterator iter = transactionEntries.iterator(); iter.hasNext();) {
144             ProcurementCardTransactionDetail transactionEntry = (ProcurementCardTransactionDetail) iter.next();
145             if (transactionEntry.getFinancialDocumentTransactionLineNumber().equals(line.getFinancialDocumentTransactionLineNumber())) {
146                 transactionEntry.getTargetAccountingLines().add(line);
147             }
148         }
149 
150         this.nextTargetLineNumber = new Integer(this.getNextTargetLineNumber().intValue() + 1);
151     }
152 
153     /**
154      * Override to get source accounting lines out of transactions
155      * 
156      * @see org.kuali.ole.sys.document.AccountingDocument#getSourceAccountingLines()
157      */
158     @Override
159     public List getSourceAccountingLines() {
160         List sourceAccountingLines = new ArrayList();
161 
162         for (Iterator iter = transactionEntries.iterator(); iter.hasNext();) {
163             ProcurementCardTransactionDetail transactionEntry = (ProcurementCardTransactionDetail) iter.next();
164             for (Iterator iterator = transactionEntry.getSourceAccountingLines().iterator(); iterator.hasNext();) {
165                 SourceAccountingLine sourceLine = (SourceAccountingLine) iterator.next();
166                 sourceAccountingLines.add(sourceLine);
167             }
168         }
169 
170         return sourceAccountingLines;
171     }
172 
173     /**
174      * Override to get target accounting lines out of transactions
175      * 
176      * @see org.kuali.ole.sys.document.AccountingDocument#getTargetAccountingLines()
177      */
178     @Override
179     public List getTargetAccountingLines() {
180         List targetAccountingLines = new ArrayList();
181 
182         for (Iterator iter = transactionEntries.iterator(); iter.hasNext();) {
183             ProcurementCardTransactionDetail transactionEntry = (ProcurementCardTransactionDetail) iter.next();
184             for (Iterator iterator = transactionEntry.getTargetAccountingLines().iterator(); iterator.hasNext();) {
185                 TargetAccountingLine targetLine = (TargetAccountingLine) iterator.next();
186                 targetAccountingLines.add(targetLine);
187             }
188         }
189 
190         return targetAccountingLines;
191     }
192 
193     /**
194      * @see org.kuali.ole.sys.document.AccountingDocumentBase#getSourceAccountingLineClass()
195      */
196     @Override
197     public Class getSourceAccountingLineClass() {
198         return ProcurementCardSourceAccountingLine.class;
199     }
200 
201     /**
202      * @see org.kuali.ole.sys.document.AccountingDocumentBase#getTargetAccountingLineClass()
203      */
204     @Override
205     public Class getTargetAccountingLineClass() {
206         return ProcurementCardTargetAccountingLine.class;
207     }
208 
209     @Override
210     public void doRouteStatusChange(DocumentRouteStatusChange statusChangeEvent) {
211         super.doRouteStatusChange(statusChangeEvent);
212 
213         // Updating for rice-1.0.0 api changes. doRouteStatusChange() went away, so
214         // that functionality needs to be a part of doRouteStatusChange now.
215         // handleRouteStatusChange did not happen on a save
216         if (!KewApiConstants.ACTION_TAKEN_SAVED_CD.equals(statusChangeEvent.getDocumentEventCode())) {
217             this.getCapitalAssetManagementModuleService().deleteDocumentAssetLocks(this);
218         }
219     }
220 
221     /**
222      * On procurement card documents, positive source amounts are credits, negative source amounts are debits.
223      * 
224      * @param transactionalDocument The document the accounting line being checked is located in.
225      * @param accountingLine The accounting line being analyzed.
226      * @return True if the accounting line given is a debit accounting line, false otherwise.
227      * @throws Throws an IllegalStateException if one of the following rules are violated: the accounting line amount is zero or the
228      *         accounting line is not an expense or income accounting line.
229      * @see org.kuali.module.financial.rules.FinancialDocumentRuleBase#isDebit(FinancialDocument,
230      *      org.kuali.rice.krad.bo.AccountingLine)
231      * @see org.kuali.ole.sys.document.validation.impl.AccountingDocumentRuleBase.IsDebitUtils#isDebitConsideringSection(AccountingDocumentRuleBase,
232      *      AccountingDocument, AccountingLine)
233      */
234     public boolean isDebit(GeneralLedgerPendingEntrySourceDetail postable) throws IllegalStateException {
235         // disallow error correction
236         DebitDeterminerService isDebitUtils = SpringContext.getBean(DebitDeterminerService.class);
237         isDebitUtils.disallowErrorCorrectionDocumentCheck(this);
238         return isDebitUtils.isDebitConsideringSection(this, (AccountingLine) postable);
239     }
240 
241     /**
242      * @see org.kuali.rice.krad.document.DocumentBase#postProcessSave(org.kuali.rice.krad.rule.event.KualiDocumentEvent)
243      */
244     @Override
245     public void postProcessSave(KualiDocumentEvent event) {
246         super.postProcessSave(event);
247         if (!(event instanceof SaveDocumentEvent)) { // don't lock until they route
248             String documentTypeName = SpringContext.getBean(DataDictionaryService.class).getDocumentTypeNameByClass(this.getClass());
249             this.getCapitalAssetManagementModuleService().generateCapitalAssetLock(this, documentTypeName);
250         }
251     }
252 
253     /**
254      * @return CapitalAssetManagementModuleService
255      */
256     protected CapitalAssetManagementModuleService getCapitalAssetManagementModuleService() {
257         if (capitalAssetManagementModuleService == null) {
258             capitalAssetManagementModuleService = SpringContext.getBean(CapitalAssetManagementModuleService.class);
259         }
260         return capitalAssetManagementModuleService;
261     }
262 
263 }