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;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.kuali.ole.fp.businessobject.CreditCardDetail;
22  import org.kuali.ole.sys.OLEConstants;
23  import org.kuali.ole.sys.OLEKeyConstants;
24  import org.kuali.ole.sys.OLEParameterKeyConstants;
25  import org.kuali.ole.sys.businessobject.Bank;
26  import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry;
27  import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntrySequenceHelper;
28  import org.kuali.ole.sys.context.SpringContext;
29  import org.kuali.ole.sys.document.AmountTotaling;
30  import org.kuali.ole.sys.document.service.AccountingDocumentRuleHelperService;
31  import org.kuali.ole.sys.service.BankService;
32  import org.kuali.ole.sys.service.GeneralLedgerPendingEntryService;
33  import org.kuali.rice.core.api.util.type.KualiDecimal;
34  import org.kuali.rice.core.web.format.CurrencyFormatter;
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  import org.kuali.rice.krad.util.GlobalVariables;
41  import org.kuali.rice.krad.util.ObjectUtils;
42  
43  /**
44   * This is the business object that represents the CreditCardReceipt document in Kuali. This is a transactional document that will
45   * eventually post transactions to the G/L. It integrates with workflow. Since a Credit Card Receipt is a one sided transactional
46   * document, only accepting funds into the university, the accounting line data will be held in the source accounting line data
47   * structure only.
48   */
49  public class CreditCardReceiptDocument extends CashReceiptFamilyBase implements Copyable, AmountTotaling {
50      public static final String CREDIT_CARD_RECEIPT_DOCUMENT_TYPE_CODE = "CCR";
51  
52      // holds details about each credit card receipt
53      protected List<CreditCardDetail> creditCardReceipts = new ArrayList<CreditCardDetail>();
54  
55      // incrementers for detail lines
56      protected Integer nextCcCrLineNumber = new Integer(1);
57  
58      // monetary attributes
59      protected KualiDecimal totalCreditCardAmount = KualiDecimal.ZERO;
60      protected String creditCardReceiptBankCode;
61      protected Bank bank;
62  
63      /**
64       * Default constructor that calls super.
65       */
66      public CreditCardReceiptDocument() {
67          super();
68          
69          bank = new Bank();
70      }
71  
72  
73      /**
74       * Sets the bank code for a new document to the setup default for the Non Check Disbursement document.
75       */
76      public void setDefaultBankCode() {
77          Bank defaultBank = SpringContext.getBean(BankService.class).getDefaultBankByDocType(this.getClass());
78          if (defaultBank != null) {
79              this.creditCardReceiptBankCode = defaultBank.getBankCode();
80              this.bank = defaultBank;
81          }
82      }
83      
84      @Override
85      public boolean documentPerformsSufficientFundsCheck() {
86          return false;
87      }
88  
89      /**
90       * Gets the total credit card amount.
91       * 
92       * @return KualiDecimal
93       */
94      public KualiDecimal getTotalCreditCardAmount() {
95          return totalCreditCardAmount;
96      }
97  
98      /**
99       * This method returns the credit card total amount as a currency formatted string.
100      * 
101      * @return String
102      */
103     public String getCurrencyFormattedTotalCreditCardAmount() {
104         return (String) new CurrencyFormatter().format(totalCreditCardAmount);
105     }
106 
107     /**
108      * Sets the total credit card amount which is the sum of all credit card receipts on this document.
109      * 
110      * @param creditCardAmount
111      */
112     public void setTotalCreditCardAmount(KualiDecimal creditCardAmount) {
113         this.totalCreditCardAmount = creditCardAmount;
114     }
115 
116     /**
117      * Gets the list of credit card receipts which is a list of CreditCardDetail business objects.
118      * 
119      * @return List
120      */
121     public List<CreditCardDetail> getCreditCardReceipts() {
122         return creditCardReceipts;
123     }
124 
125     /**
126      * Sets the credit card receipts list.
127      * 
128      * @param creditCardReceipts
129      */
130     public void setCreditCardReceipts(List<CreditCardDetail> creditCardReceipts) {
131         this.creditCardReceipts = creditCardReceipts;
132     }
133 
134     /**
135      * Adds a new credit card receipt to the list.
136      * 
137      * @param creditCardReceiptDetail
138      */
139     public void addCreditCardReceipt(CreditCardDetail creditCardReceiptDetail) {
140         // these three make up the primary key for a credit card detail record
141         prepareNewCreditCardReceipt(creditCardReceiptDetail);
142 
143         // add the new detail record to the list
144         this.creditCardReceipts.add(creditCardReceiptDetail);
145 
146         // increment line number
147         this.nextCcCrLineNumber = new Integer(this.nextCcCrLineNumber.intValue() + 1);
148 
149         // update the overall amount
150         this.totalCreditCardAmount = this.totalCreditCardAmount.add(creditCardReceiptDetail.getCreditCardAdvanceDepositAmount());
151     }
152 
153     /**
154      * This is a helper method that automatically populates document specfic information into the credit card receipt
155      * (CreditCardDetail) instance.
156      * 
157      * @param creditCardReceiptDetail
158      */
159     public final void prepareNewCreditCardReceipt(CreditCardDetail creditCardReceiptDetail) {
160         creditCardReceiptDetail.setFinancialDocumentLineNumber(this.nextCcCrLineNumber);
161         creditCardReceiptDetail.setDocumentNumber(this.getDocumentNumber());
162         creditCardReceiptDetail.setFinancialDocumentTypeCode(SpringContext.getBean(DataDictionaryService.class).getDocumentTypeNameByClass(this.getClass()));
163     }
164 
165     /**
166      * Retrieve a particular credit card receipt at a given index in the list of credit card receipts.
167      * 
168      * @param index
169      * @return CreditCardReceiptDetail
170      */
171     public CreditCardDetail getCreditCardReceipt(int index) {
172         while (this.creditCardReceipts.size() <= index) {
173             creditCardReceipts.add(new CreditCardDetail());
174         }
175         return creditCardReceipts.get(index);
176     }
177 
178     /**
179      * This method removes a credit card receipt from the list and updates the total appropriately.
180      * 
181      * @param index
182      */
183     public void removeCreditCardReceipt(int index) {
184         CreditCardDetail creditCardReceiptDetail = creditCardReceipts.remove(index);
185         this.totalCreditCardAmount = this.totalCreditCardAmount.subtract(creditCardReceiptDetail.getCreditCardAdvanceDepositAmount());
186     }
187 
188     /**
189      * @return Integer
190      */
191     public Integer getNextCcCrLineNumber() {
192         return nextCcCrLineNumber;
193     }
194 
195     /**
196      * @param nextCcCrLineNumber
197      */
198     public void setNextCcCrLineNumber(Integer nextCcCrLineNumber) {
199         this.nextCcCrLineNumber = nextCcCrLineNumber;
200     }
201 
202     /**
203      * This method returns the overall total of the document - the credit card total.
204      * 
205      * @see org.kuali.ole.sys.document.AccountingDocumentBase#getTotalDollarAmount()
206      * @return KualiDecimal
207      */
208     @Override
209     public KualiDecimal getTotalDollarAmount() {
210         return this.totalCreditCardAmount;
211     }
212 
213     /**
214      * This method returns the sum of all of the credit card receipts for this document.
215      * 
216      * @return KualiDecimal
217      */
218     public KualiDecimal calculateCreditCardReceiptTotal() {
219         KualiDecimal total = KualiDecimal.ZERO;
220         for (CreditCardDetail detail : getCreditCardReceipts()) {
221             if (null != detail.getCreditCardAdvanceDepositAmount()) {
222                 total = total.add(detail.getCreditCardAdvanceDepositAmount());
223             }
224         }
225         return total;
226     }
227 
228 
229     /**
230      * Overrides super to call super and then also add in the new list of credit card receipts that have to be managed.
231      * 
232      * @see org.kuali.rice.krad.document.TransactionalDocumentBase#buildListOfDeletionAwareLists()
233      */
234     @Override
235     public List buildListOfDeletionAwareLists() {
236         List managedLists = super.buildListOfDeletionAwareLists();
237         managedLists.add(getCreditCardReceipts());
238 
239         return managedLists;
240     }
241     
242     /**
243      * Generates bank offset GLPEs for deposits, if enabled.
244      * 
245      * @param financialDocument submitted accounting document
246      * @param sequenceHelper helper class for keep track of sequence for GLPEs
247      * @return true if generation of GLPE's is successful for credit card receipt document
248      * 
249      * @see org.kuali.rice.krad.rule.GenerateGeneralLedgerDocumentPendingEntriesRule#processGenerateDocumentGeneralLedgerPendingEntries(org.kuali.rice.krad.document.FinancialDocument,org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntrySequenceHelper)
250      */
251     @Override
252     public boolean generateDocumentGeneralLedgerPendingEntries(GeneralLedgerPendingEntrySequenceHelper sequenceHelper) {
253         boolean success = true;
254         
255         GeneralLedgerPendingEntryService glpeService = SpringContext.getBean(GeneralLedgerPendingEntryService.class);
256         
257         if (SpringContext.getBean(BankService.class).isBankSpecificationEnabled()) {
258             final KualiDecimal bankOffsetAmount = glpeService.getOffsetToCashAmount(this).negated();
259             GeneralLedgerPendingEntry bankOffsetEntry = new GeneralLedgerPendingEntry();
260             
261             Bank offsetBank = getOffsetBank();
262             if (ObjectUtils.isNull(offsetBank)) {
263                 success = false;
264                 GlobalVariables.getMessageMap().putError("newCreditCardReceipt.financialDocumentCreditCardTypeCode", OLEKeyConstants.CreditCardReceipt.ERROR_DOCUMENT_CREDIT_CARD_BANK_MUST_EXIST_WHEN_BANK_ENHANCEMENT_ENABLED, new String[] { OLEParameterKeyConstants.ENABLE_BANK_SPECIFICATION_IND, OLEParameterKeyConstants.DEFAULT_BANK_BY_DOCUMENT_TYPE });
265             }
266             else {
267                 success &= glpeService.populateBankOffsetGeneralLedgerPendingEntry(offsetBank, bankOffsetAmount, this, getPostingYear(), sequenceHelper, bankOffsetEntry, OLEConstants.CREDIT_CARD_RECEIPTS_LINE_ERRORS);
268                
269                 // An unsuccessfully populated bank offset entry may contain invalid relations, so don't add it 
270                 if (success) {
271                     AccountingDocumentRuleHelperService accountingDocumentRuleUtil = SpringContext.getBean(AccountingDocumentRuleHelperService.class);
272                     bankOffsetEntry.setTransactionLedgerEntryDescription(accountingDocumentRuleUtil.formatProperty(OLEKeyConstants.Bank.DESCRIPTION_GLPE_BANK_OFFSET));
273                     getGeneralLedgerPendingEntries().add(bankOffsetEntry);
274                     sequenceHelper.increment();
275 
276                     GeneralLedgerPendingEntry offsetEntry = new GeneralLedgerPendingEntry(bankOffsetEntry);
277                     success &= glpeService.populateOffsetGeneralLedgerPendingEntry(getPostingYear(), bankOffsetEntry, sequenceHelper, offsetEntry);
278                     getGeneralLedgerPendingEntries().add(offsetEntry);
279                     sequenceHelper.increment();
280                 }
281             }
282         }
283         
284         return success;
285     }
286     
287     /**
288      * Assigns default bank code
289      */
290     public void initiateDocument() {
291         // default bank code
292         Bank defaultBank = SpringContext.getBean(BankService.class).getDefaultBankByDocType(this.getClass());
293         if (defaultBank != null) {
294             this.creditCardReceiptBankCode = defaultBank.getBankCode();
295             this.bank = defaultBank;
296         }
297     }
298     
299     /**
300      * Returns the default bank code for Credit Card Receipt documents.
301      */
302     protected Bank getOffsetBank() {
303         return SpringContext.getBean(BankService.class).getByPrimaryId(creditCardReceiptBankCode);
304     }
305     
306     /**
307      * Gets the creditCardReceiptBankCode attribute. 
308      * @return Returns the creditCardReceiptBankCode.
309      */
310     public String getCreditCardReceiptBankCode() {
311         return creditCardReceiptBankCode;
312     }
313     
314 
315     /**
316      * Sets the creditCardReceiptBankCode attribute value.
317      * @param creditCardReceiptBankCode The creditCardReceiptBankCode to set.
318      */
319     public void setCreditCardReceiptBankCode(String creditCardReceiptBankCode) {
320         this.creditCardReceiptBankCode = creditCardReceiptBankCode;
321     }
322 
323 
324     /**
325      * Gets the bank attribute. 
326      * @return Returns the bank.
327      */
328     public Bank getBank() {
329         return bank;
330     }
331 
332 
333     /**
334      * Sets the bank attribute value.
335      * @param bank The bank to set.
336      */
337     public void setBank(Bank bank) {
338         this.bank = bank;
339     }
340 
341 
342     @Override
343     public void postProcessSave(KualiDocumentEvent event) {
344         super.postProcessSave(event);
345         if (!(event instanceof SaveDocumentEvent)) { // don't lock until they route
346             String documentTypeName = SpringContext.getBean(DataDictionaryService.class).getDocumentTypeNameByClass(this.getClass());
347             this.getCapitalAssetManagementModuleService().generateCapitalAssetLock(this,documentTypeName);
348         }        
349     }
350     
351     @Override
352     public void doRouteStatusChange(DocumentRouteStatusChange statusChangeEvent) {
353         super.doRouteStatusChange(statusChangeEvent);
354         this.getCapitalAssetManagementModuleService().deleteDocumentAssetLocks(this); 
355     }
356 }